2022-05-14 00:26:29 +00:00
|
|
|
#![allow(non_snake_case)]
|
|
|
|
|
|
|
|
use lazy_static::lazy_static;
|
2022-04-28 21:29:56 +00:00
|
|
|
use thiserror::Error;
|
2022-05-03 11:20:24 +00:00
|
|
|
use rand_core::{RngCore, CryptoRng};
|
2022-04-28 21:29:56 +00:00
|
|
|
|
2022-04-22 01:36:18 +00:00
|
|
|
use curve25519_dalek::{
|
|
|
|
constants::ED25519_BASEPOINT_TABLE,
|
|
|
|
scalar::Scalar,
|
|
|
|
traits::VartimePrecomputedMultiscalarMul,
|
|
|
|
edwards::{EdwardsPoint, VartimeEdwardsPrecomputation}
|
|
|
|
};
|
2022-05-14 00:26:29 +00:00
|
|
|
#[cfg(feature = "experimental")]
|
|
|
|
use curve25519_dalek::edwards::CompressedEdwardsY;
|
2022-04-22 01:36:18 +00:00
|
|
|
|
2022-05-06 23:07:37 +00:00
|
|
|
use monero::{consensus::Encodable, util::ringct::{Key, Clsag}};
|
2022-04-22 01:36:18 +00:00
|
|
|
|
2022-04-28 07:31:09 +00:00
|
|
|
use crate::{
|
|
|
|
Commitment,
|
2022-05-06 23:07:37 +00:00
|
|
|
transaction::decoys::Decoys,
|
2022-04-28 07:31:09 +00:00
|
|
|
random_scalar,
|
|
|
|
hash_to_scalar,
|
2022-05-14 00:26:29 +00:00
|
|
|
hash_to_point
|
2022-04-28 07:31:09 +00:00
|
|
|
};
|
2022-04-22 01:36:18 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "multisig")]
|
|
|
|
mod multisig;
|
|
|
|
#[cfg(feature = "multisig")]
|
2022-05-06 23:07:37 +00:00
|
|
|
pub use multisig::{Details, Multisig};
|
2022-04-22 01:36:18 +00:00
|
|
|
|
2022-04-28 21:29:56 +00:00
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
#[error("internal error ({0})")]
|
|
|
|
InternalError(String),
|
|
|
|
#[error("invalid ring member (member {0}, ring size {1})")]
|
|
|
|
InvalidRingMember(u8, u8),
|
|
|
|
#[error("invalid commitment")]
|
2022-05-14 00:26:29 +00:00
|
|
|
InvalidCommitment,
|
|
|
|
#[error("invalid D")]
|
|
|
|
InvalidD,
|
|
|
|
#[error("invalid s")]
|
|
|
|
InvalidS,
|
|
|
|
#[error("invalid c1")]
|
|
|
|
InvalidC1
|
2022-04-28 21:29:56 +00:00
|
|
|
}
|
|
|
|
|
2022-05-06 23:07:37 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2022-04-28 21:29:56 +00:00
|
|
|
pub struct Input {
|
2022-05-06 23:07:37 +00:00
|
|
|
// The actual commitment for the true spend
|
|
|
|
pub commitment: Commitment,
|
|
|
|
// True spend index, offsets, and ring
|
|
|
|
pub decoys: Decoys
|
2022-04-28 21:29:56 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 00:26:29 +00:00
|
|
|
lazy_static! {
|
|
|
|
static ref INV_EIGHT: Scalar = Scalar::from(8 as u8).invert();
|
|
|
|
}
|
|
|
|
|
2022-04-28 21:29:56 +00:00
|
|
|
impl Input {
|
|
|
|
pub fn new(
|
2022-05-06 23:07:37 +00:00
|
|
|
commitment: Commitment,
|
|
|
|
decoys: Decoys
|
2022-05-03 11:20:24 +00:00
|
|
|
) -> Result<Input, Error> {
|
2022-05-06 23:07:37 +00:00
|
|
|
let n = decoys.len();
|
2022-04-28 21:29:56 +00:00
|
|
|
if n > u8::MAX.into() {
|
|
|
|
Err(Error::InternalError("max ring size in this library is u8 max".to_string()))?;
|
|
|
|
}
|
2022-05-06 23:07:37 +00:00
|
|
|
if decoys.i >= (n as u8) {
|
|
|
|
Err(Error::InvalidRingMember(decoys.i, n as u8))?;
|
2022-04-28 21:29:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the commitment matches
|
2022-05-06 23:07:37 +00:00
|
|
|
if decoys.ring[usize::from(decoys.i)][1] != commitment.calculate() {
|
2022-04-28 21:29:56 +00:00
|
|
|
Err(Error::InvalidCommitment)?;
|
|
|
|
}
|
|
|
|
|
2022-05-06 23:07:37 +00:00
|
|
|
Ok(Input { commitment, decoys })
|
2022-04-28 21:29:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 00:26:29 +00:00
|
|
|
enum Mode {
|
|
|
|
Sign(usize, EdwardsPoint, EdwardsPoint),
|
|
|
|
#[cfg(feature = "experimental")]
|
|
|
|
Verify(Scalar)
|
|
|
|
}
|
2022-04-22 01:36:18 +00:00
|
|
|
|
2022-05-14 00:26:29 +00:00
|
|
|
fn core(
|
|
|
|
ring: &[[EdwardsPoint; 2]],
|
|
|
|
I: &EdwardsPoint,
|
|
|
|
pseudo_out: &EdwardsPoint,
|
|
|
|
msg: &[u8; 32],
|
|
|
|
D: &EdwardsPoint,
|
|
|
|
s: &[Scalar],
|
|
|
|
// Use a Result as Either for sign/verify
|
|
|
|
A_c1: Mode
|
|
|
|
) -> (([u8; 32], Scalar, Scalar), Scalar) {
|
|
|
|
let n = ring.len();
|
2022-04-22 01:36:18 +00:00
|
|
|
|
|
|
|
// Doesn't use a constant time table as dalek takes longer to generate those then they save
|
2022-05-14 00:26:29 +00:00
|
|
|
let images_precomp = VartimeEdwardsPrecomputation::new([I, D]);
|
|
|
|
let D = D * *INV_EIGHT;
|
2022-04-22 01:36:18 +00:00
|
|
|
|
|
|
|
let mut to_hash = vec![];
|
2022-05-14 00:26:29 +00:00
|
|
|
to_hash.reserve_exact(((2 * n) + 5) * 32);
|
2022-04-22 01:36:18 +00:00
|
|
|
const PREFIX: &str = "CLSAG_";
|
2022-05-03 12:49:46 +00:00
|
|
|
const AGG_0: &str = "CLSAG_agg_0";
|
|
|
|
const ROUND: &str = "round";
|
2022-04-22 01:36:18 +00:00
|
|
|
to_hash.extend(AGG_0.bytes());
|
|
|
|
to_hash.extend([0; 32 - AGG_0.len()]);
|
|
|
|
|
2022-05-14 00:26:29 +00:00
|
|
|
let mut P = vec![];
|
|
|
|
P.reserve_exact(n);
|
|
|
|
let mut C = vec![];
|
|
|
|
C.reserve_exact(n);
|
|
|
|
for member in ring {
|
|
|
|
P.push(member[0]);
|
|
|
|
C.push(member[1] - pseudo_out);
|
2022-04-22 01:36:18 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 00:26:29 +00:00
|
|
|
for member in ring {
|
|
|
|
to_hash.extend(member[0].compress().to_bytes());
|
2022-04-22 01:36:18 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 00:26:29 +00:00
|
|
|
for member in ring {
|
|
|
|
to_hash.extend(member[1].compress().to_bytes());
|
|
|
|
}
|
|
|
|
|
|
|
|
to_hash.extend(I.compress().to_bytes());
|
2022-04-22 01:36:18 +00:00
|
|
|
let D_bytes = D.compress().to_bytes();
|
|
|
|
to_hash.extend(D_bytes);
|
2022-05-14 00:26:29 +00:00
|
|
|
to_hash.extend(pseudo_out.compress().to_bytes());
|
2022-04-22 01:36:18 +00:00
|
|
|
let mu_P = hash_to_scalar(&to_hash);
|
|
|
|
to_hash[AGG_0.len() - 1] = '1' as u8;
|
|
|
|
let mu_C = hash_to_scalar(&to_hash);
|
|
|
|
|
|
|
|
to_hash.truncate(((2 * n) + 1) * 32);
|
2022-04-28 07:31:09 +00:00
|
|
|
for i in 0 .. ROUND.len() {
|
|
|
|
to_hash[PREFIX.len() + i] = ROUND.as_bytes()[i] as u8;
|
2022-04-22 01:36:18 +00:00
|
|
|
}
|
2022-05-14 00:26:29 +00:00
|
|
|
to_hash.extend(pseudo_out.compress().to_bytes());
|
2022-04-22 01:36:18 +00:00
|
|
|
to_hash.extend(msg);
|
|
|
|
|
2022-05-14 04:45:13 +00:00
|
|
|
let start;
|
2022-05-14 00:26:29 +00:00
|
|
|
let end;
|
2022-05-14 04:45:13 +00:00
|
|
|
let mut c;
|
2022-05-14 00:26:29 +00:00
|
|
|
match A_c1 {
|
|
|
|
Mode::Sign(r, A, AH) => {
|
2022-05-14 04:45:13 +00:00
|
|
|
start = r + 1;
|
|
|
|
end = r + n;
|
2022-05-14 00:26:29 +00:00
|
|
|
to_hash.extend(A.compress().to_bytes());
|
|
|
|
to_hash.extend(AH.compress().to_bytes());
|
|
|
|
c = hash_to_scalar(&to_hash);
|
|
|
|
},
|
|
|
|
|
|
|
|
#[cfg(feature = "experimental")]
|
|
|
|
Mode::Verify(c1) => {
|
2022-05-14 04:45:13 +00:00
|
|
|
start = 0;
|
|
|
|
end = n;
|
2022-05-14 00:26:29 +00:00
|
|
|
c = c1;
|
|
|
|
}
|
2022-04-22 01:36:18 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 04:45:13 +00:00
|
|
|
let mut c1 = None;
|
|
|
|
for i in (start .. end).map(|i| i % n) {
|
|
|
|
if i == 0 {
|
|
|
|
c1 = Some(c);
|
|
|
|
}
|
|
|
|
|
2022-04-22 01:36:18 +00:00
|
|
|
let c_p = mu_P * c;
|
|
|
|
let c_c = mu_C * c;
|
|
|
|
|
2022-04-28 07:31:09 +00:00
|
|
|
let L = (&s[i] * &ED25519_BASEPOINT_TABLE) + (c_p * P[i]) + (c_c * C[i]);
|
|
|
|
let PH = hash_to_point(&P[i]);
|
2022-04-22 01:36:18 +00:00
|
|
|
// Shouldn't be an issue as all of the variables in this vartime statement are public
|
2022-04-28 07:31:09 +00:00
|
|
|
let R = (s[i] * PH) + images_precomp.vartime_multiscalar_mul(&[c_p, c_c]);
|
2022-04-22 01:36:18 +00:00
|
|
|
|
|
|
|
to_hash.truncate(((2 * n) + 3) * 32);
|
|
|
|
to_hash.extend(L.compress().to_bytes());
|
|
|
|
to_hash.extend(R.compress().to_bytes());
|
|
|
|
c = hash_to_scalar(&to_hash);
|
|
|
|
}
|
|
|
|
|
2022-05-14 04:45:13 +00:00
|
|
|
((D_bytes, c * mu_P, c * mu_C), c1.unwrap_or(c))
|
2022-05-14 00:26:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn sign_core<R: RngCore + CryptoRng>(
|
|
|
|
rng: &mut R,
|
|
|
|
I: &EdwardsPoint,
|
|
|
|
input: &Input,
|
|
|
|
mask: Scalar,
|
|
|
|
msg: &[u8; 32],
|
|
|
|
A: EdwardsPoint,
|
|
|
|
AH: EdwardsPoint
|
|
|
|
) -> (Clsag, EdwardsPoint, Scalar, Scalar) {
|
|
|
|
let r: usize = input.decoys.i.into();
|
|
|
|
|
|
|
|
let pseudo_out = Commitment::new(mask, input.commitment.amount).calculate();
|
|
|
|
let z = input.commitment.mask - mask;
|
|
|
|
|
|
|
|
let H = hash_to_point(&input.decoys.ring[r][0]);
|
|
|
|
let D = H * z;
|
|
|
|
let mut s = Vec::with_capacity(input.decoys.ring.len());
|
|
|
|
for _ in 0 .. input.decoys.ring.len() {
|
|
|
|
s.push(random_scalar(rng));
|
|
|
|
}
|
|
|
|
let ((D_bytes, p, c), c1) = core(&input.decoys.ring, I, &pseudo_out, msg, &D, &s, Mode::Sign(r, A, AH));
|
|
|
|
|
2022-04-22 01:36:18 +00:00
|
|
|
(
|
|
|
|
Clsag {
|
2022-05-14 00:26:29 +00:00
|
|
|
D: Key { key: D_bytes },
|
2022-04-22 01:36:18 +00:00
|
|
|
s: s.iter().map(|s| Key { key: s.to_bytes() }).collect(),
|
2022-05-14 00:26:29 +00:00
|
|
|
c1: Key { key: c1.to_bytes() }
|
2022-04-22 01:36:18 +00:00
|
|
|
},
|
2022-05-14 00:26:29 +00:00
|
|
|
pseudo_out,
|
|
|
|
p,
|
|
|
|
c * z
|
2022-04-22 01:36:18 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn sign<R: RngCore + CryptoRng>(
|
|
|
|
rng: &mut R,
|
2022-05-06 23:07:37 +00:00
|
|
|
inputs: &[(Scalar, EdwardsPoint, Input)],
|
|
|
|
sum_outputs: Scalar,
|
|
|
|
msg: [u8; 32]
|
2022-04-28 07:31:09 +00:00
|
|
|
) -> Option<Vec<(Clsag, EdwardsPoint)>> {
|
|
|
|
if inputs.len() == 0 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let nonce = random_scalar(rng);
|
2022-04-22 01:36:18 +00:00
|
|
|
let mut rand_source = [0; 64];
|
|
|
|
rng.fill_bytes(&mut rand_source);
|
2022-04-28 07:31:09 +00:00
|
|
|
|
|
|
|
let mut res = Vec::with_capacity(inputs.len());
|
|
|
|
let mut sum_pseudo_outs = Scalar::zero();
|
|
|
|
for i in 0 .. inputs.len() {
|
|
|
|
let mut mask = random_scalar(rng);
|
|
|
|
if i == (inputs.len() - 1) {
|
|
|
|
mask = sum_outputs - sum_pseudo_outs;
|
|
|
|
} else {
|
|
|
|
sum_pseudo_outs += mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut rand_source = [0; 64];
|
|
|
|
rng.fill_bytes(&mut rand_source);
|
2022-05-14 00:26:29 +00:00
|
|
|
let (mut clsag, pseudo_out, p, c) = sign_core(
|
2022-04-28 21:12:54 +00:00
|
|
|
rng,
|
2022-04-28 07:31:09 +00:00
|
|
|
&inputs[i].1,
|
2022-04-30 02:03:34 +00:00
|
|
|
&inputs[i].2,
|
2022-04-28 07:31:09 +00:00
|
|
|
mask,
|
2022-05-06 23:07:37 +00:00
|
|
|
&msg,
|
|
|
|
&nonce * &ED25519_BASEPOINT_TABLE,
|
|
|
|
nonce * hash_to_point(&inputs[i].2.decoys.ring[usize::from(inputs[i].2.decoys.i)][0])
|
2022-04-28 07:31:09 +00:00
|
|
|
);
|
2022-05-06 23:07:37 +00:00
|
|
|
clsag.s[inputs[i].2.decoys.i as usize] = Key {
|
2022-05-14 00:26:29 +00:00
|
|
|
key: (nonce - ((p * inputs[i].0) + c)).to_bytes()
|
2022-04-28 07:31:09 +00:00
|
|
|
};
|
|
|
|
|
2022-05-14 00:26:29 +00:00
|
|
|
res.push((clsag, pseudo_out));
|
2022-04-28 07:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Some(res)
|
2022-04-22 01:36:18 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 00:26:29 +00:00
|
|
|
// Not extensively tested nor guaranteed to have expected parity with Monero
|
|
|
|
#[cfg(feature = "experimental")]
|
|
|
|
pub fn rust_verify(
|
|
|
|
clsag: &Clsag,
|
|
|
|
ring: &[[EdwardsPoint; 2]],
|
|
|
|
I: &EdwardsPoint,
|
|
|
|
pseudo_out: &EdwardsPoint,
|
|
|
|
msg: &[u8; 32]
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
let c1 = Scalar::from_canonical_bytes(clsag.c1.key).ok_or(Error::InvalidC1)?;
|
|
|
|
let (_, c1_calculated) = core(
|
|
|
|
ring,
|
|
|
|
I,
|
|
|
|
pseudo_out,
|
|
|
|
msg,
|
|
|
|
&CompressedEdwardsY(clsag.D.key).decompress().ok_or(Error::InvalidD)?.mul_by_cofactor(),
|
|
|
|
&clsag.s.iter().map(|s| Scalar::from_canonical_bytes(s.key).ok_or(Error::InvalidS)).collect::<Result<Vec<_>, _>>()?,
|
|
|
|
Mode::Verify(c1)
|
|
|
|
);
|
|
|
|
if c1_calculated != c1 {
|
|
|
|
Err(Error::InvalidC1)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-04-22 01:36:18 +00:00
|
|
|
// Uses Monero's C verification function to ensure compatibility with Monero
|
2022-05-14 00:26:29 +00:00
|
|
|
#[link(name = "wrapper")]
|
|
|
|
extern "C" {
|
|
|
|
pub(crate) fn c_verify_clsag(
|
|
|
|
serialized_len: usize,
|
|
|
|
serialized: *const u8,
|
|
|
|
ring_size: u8,
|
|
|
|
ring: *const u8,
|
|
|
|
I: *const u8,
|
|
|
|
pseudo_out: *const u8,
|
|
|
|
msg: *const u8
|
|
|
|
) -> bool;
|
|
|
|
}
|
|
|
|
|
2022-04-22 01:36:18 +00:00
|
|
|
pub fn verify(
|
|
|
|
clsag: &Clsag,
|
|
|
|
ring: &[[EdwardsPoint; 2]],
|
2022-05-14 00:26:29 +00:00
|
|
|
I: &EdwardsPoint,
|
|
|
|
pseudo_out: &EdwardsPoint,
|
2022-05-06 23:07:37 +00:00
|
|
|
msg: &[u8; 32]
|
2022-05-14 00:26:29 +00:00
|
|
|
) -> Result<(), Error> {
|
2022-04-22 01:36:18 +00:00
|
|
|
// Workaround for the fact monero-rs doesn't include the length of clsag.s in clsag encoding
|
|
|
|
// despite it being part of clsag encoding. Reason for the patch version pin
|
|
|
|
let mut serialized = vec![clsag.s.len() as u8];
|
|
|
|
clsag.consensus_encode(&mut serialized).unwrap();
|
|
|
|
|
2022-05-14 00:26:29 +00:00
|
|
|
let I_bytes = I.compress().to_bytes();
|
2022-04-22 01:36:18 +00:00
|
|
|
|
|
|
|
let mut ring_bytes = vec![];
|
|
|
|
for member in ring {
|
|
|
|
ring_bytes.extend(&member[0].compress().to_bytes());
|
|
|
|
ring_bytes.extend(&member[1].compress().to_bytes());
|
|
|
|
}
|
|
|
|
|
|
|
|
let pseudo_out_bytes = pseudo_out.compress().to_bytes();
|
|
|
|
|
|
|
|
unsafe {
|
2022-05-14 00:26:29 +00:00
|
|
|
if c_verify_clsag(
|
|
|
|
serialized.len(), serialized.as_ptr(),
|
|
|
|
ring.len() as u8, ring_bytes.as_ptr(),
|
|
|
|
I_bytes.as_ptr(), pseudo_out_bytes.as_ptr(), msg.as_ptr()
|
|
|
|
) {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(Error::InvalidC1)
|
|
|
|
}
|
2022-04-22 01:36:18 +00:00
|
|
|
}
|
|
|
|
}
|