2022-04-29 22:03:34 -04:00
|
|
|
use core::fmt::Debug;
|
|
|
|
|
|
2022-04-28 17:12:54 -04:00
|
|
|
use rand_core::{RngCore, CryptoRng, SeedableRng};
|
|
|
|
|
use rand_chacha::ChaCha12Rng;
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-04-28 17:12:54 -04:00
|
|
|
use blake2::{Digest, Blake2b512};
|
2022-04-21 21:36:18 -04:00
|
|
|
|
|
|
|
|
use curve25519_dalek::{
|
|
|
|
|
constants::ED25519_BASEPOINT_TABLE,
|
2022-04-29 22:03:34 -04:00
|
|
|
traits::Identity,
|
2022-04-21 21:36:18 -04:00
|
|
|
scalar::Scalar,
|
|
|
|
|
edwards::EdwardsPoint
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use group::Group;
|
2022-04-28 17:12:54 -04:00
|
|
|
use dalek_ff_group as dfg;
|
2022-04-23 03:49:30 -04:00
|
|
|
use frost::{Curve, FrostError, algorithm::Algorithm, sign::ParamsView};
|
2022-04-21 21:36:18 -04:00
|
|
|
|
|
|
|
|
use monero::util::ringct::{Key, Clsag};
|
|
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
|
hash_to_point,
|
2022-04-28 12:01:20 -04:00
|
|
|
frost::{MultisigError, Ed25519, DLEqProof},
|
2022-04-29 22:03:34 -04:00
|
|
|
key_image,
|
2022-04-28 17:29:56 -04:00
|
|
|
clsag::{Input, sign_core, verify}
|
2022-04-21 21:36:18 -04:00
|
|
|
};
|
|
|
|
|
|
2022-04-30 01:41:05 -04:00
|
|
|
pub trait TransactionData: Clone + Debug {
|
|
|
|
|
fn msg(&self) -> [u8; 32];
|
|
|
|
|
fn mask_sum(&self) -> Scalar;
|
2022-04-29 22:03:34 -04:00
|
|
|
}
|
|
|
|
|
|
2022-04-21 21:36:18 -04:00
|
|
|
#[allow(non_snake_case)]
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
struct ClsagSignInterim {
|
|
|
|
|
c: Scalar,
|
2022-04-28 12:01:20 -04:00
|
|
|
s: Scalar,
|
2022-04-21 21:36:18 -04:00
|
|
|
|
|
|
|
|
clsag: Clsag,
|
|
|
|
|
C_out: EdwardsPoint
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
|
#[derive(Clone, Debug)]
|
2022-04-30 01:41:05 -04:00
|
|
|
pub struct Multisig<D: TransactionData> {
|
2022-04-21 21:36:18 -04:00
|
|
|
b: Vec<u8>,
|
2022-04-29 22:03:34 -04:00
|
|
|
AH: (dfg::EdwardsPoint, dfg::EdwardsPoint),
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-04-28 17:29:56 -04:00
|
|
|
input: Input,
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-04-29 22:03:34 -04:00
|
|
|
image: Option<EdwardsPoint>,
|
2022-04-30 01:41:05 -04:00
|
|
|
data: D,
|
2022-04-29 22:03:34 -04:00
|
|
|
|
2022-04-21 21:36:18 -04:00
|
|
|
interim: Option<ClsagSignInterim>
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-30 01:41:05 -04:00
|
|
|
impl<D: TransactionData> Multisig<D> {
|
2022-04-29 15:28:04 -04:00
|
|
|
pub fn new(
|
2022-04-29 22:03:34 -04:00
|
|
|
input: Input,
|
2022-04-30 01:41:05 -04:00
|
|
|
data: D
|
|
|
|
|
) -> Result<Multisig<D>, MultisigError> {
|
2022-04-21 21:36:18 -04:00
|
|
|
Ok(
|
|
|
|
|
Multisig {
|
|
|
|
|
b: vec![],
|
2022-04-29 22:03:34 -04:00
|
|
|
AH: (dfg::EdwardsPoint::identity(), dfg::EdwardsPoint::identity()),
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-04-28 12:01:20 -04:00
|
|
|
input,
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-04-29 22:03:34 -04:00
|
|
|
image: None,
|
2022-04-30 01:41:05 -04:00
|
|
|
data,
|
2022-04-21 21:36:18 -04:00
|
|
|
interim: None
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
2022-04-29 15:28:04 -04:00
|
|
|
|
2022-04-29 22:03:34 -04:00
|
|
|
pub fn set_image(&mut self, image: EdwardsPoint) {
|
|
|
|
|
self.image = Some(image);
|
2022-04-29 15:28:04 -04:00
|
|
|
}
|
2022-04-21 21:36:18 -04:00
|
|
|
}
|
|
|
|
|
|
2022-04-30 01:41:05 -04:00
|
|
|
impl<D: TransactionData> Algorithm<Ed25519> for Multisig<D> {
|
2022-04-21 21:36:18 -04:00
|
|
|
type Signature = (Clsag, EdwardsPoint);
|
|
|
|
|
|
|
|
|
|
// We arguably don't have to commit to at all thanks to xG and yG being committed to, both of
|
|
|
|
|
// those being proven to have the same scalar as xH and yH, yet it doesn't hurt
|
|
|
|
|
fn addendum_commit_len() -> usize {
|
|
|
|
|
64
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn preprocess_addendum<R: RngCore + CryptoRng>(
|
|
|
|
|
rng: &mut R,
|
2022-04-23 03:49:30 -04:00
|
|
|
view: &ParamsView<Ed25519>,
|
2022-04-21 21:36:18 -04:00
|
|
|
nonces: &[dfg::Scalar; 2]
|
|
|
|
|
) -> Vec<u8> {
|
|
|
|
|
#[allow(non_snake_case)]
|
2022-04-23 03:49:30 -04:00
|
|
|
let H = hash_to_point(&view.group_key().0);
|
2022-04-21 21:36:18 -04:00
|
|
|
let h0 = nonces[0].0 * H;
|
|
|
|
|
let h1 = nonces[1].0 * H;
|
2022-04-29 15:28:04 -04:00
|
|
|
let mut serialized = Vec::with_capacity(32 + 32 + 64 + 64);
|
2022-04-21 21:36:18 -04:00
|
|
|
serialized.extend(h0.compress().to_bytes());
|
|
|
|
|
serialized.extend(h1.compress().to_bytes());
|
|
|
|
|
serialized.extend(&DLEqProof::prove(rng, &nonces[0].0, &H, &h0).serialize());
|
|
|
|
|
serialized.extend(&DLEqProof::prove(rng, &nonces[1].0, &H, &h1).serialize());
|
|
|
|
|
serialized
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn process_addendum(
|
|
|
|
|
&mut self,
|
2022-04-23 03:49:30 -04:00
|
|
|
_: &ParamsView<Ed25519>,
|
2022-04-21 21:36:18 -04:00
|
|
|
l: usize,
|
|
|
|
|
commitments: &[dfg::EdwardsPoint; 2],
|
|
|
|
|
serialized: &[u8]
|
|
|
|
|
) -> Result<(), FrostError> {
|
|
|
|
|
if serialized.len() != 192 {
|
|
|
|
|
// Not an optimal error but...
|
|
|
|
|
Err(FrostError::InvalidCommitmentQuantity(l, 6, serialized.len() / 32))?;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-28 12:01:20 -04:00
|
|
|
let alt = &hash_to_point(&self.input.ring[self.input.i][0]);
|
2022-04-21 21:36:18 -04:00
|
|
|
|
|
|
|
|
let h0 = <Ed25519 as Curve>::G_from_slice(&serialized[0 .. 32]).map_err(|_| FrostError::InvalidCommitment(l))?;
|
|
|
|
|
DLEqProof::deserialize(&serialized[64 .. 128]).ok_or(FrostError::InvalidCommitment(l))?.verify(
|
|
|
|
|
&alt,
|
|
|
|
|
&commitments[0],
|
|
|
|
|
&h0
|
|
|
|
|
).map_err(|_| FrostError::InvalidCommitment(l))?;
|
|
|
|
|
|
|
|
|
|
let h1 = <Ed25519 as Curve>::G_from_slice(&serialized[32 .. 64]).map_err(|_| FrostError::InvalidCommitment(l))?;
|
|
|
|
|
DLEqProof::deserialize(&serialized[128 .. 192]).ok_or(FrostError::InvalidCommitment(l))?.verify(
|
|
|
|
|
&alt,
|
|
|
|
|
&commitments[1],
|
|
|
|
|
&h1
|
|
|
|
|
).map_err(|_| FrostError::InvalidCommitment(l))?;
|
|
|
|
|
|
|
|
|
|
self.b.extend(&l.to_le_bytes());
|
|
|
|
|
self.b.extend(&serialized[0 .. 64]);
|
2022-04-29 22:03:34 -04:00
|
|
|
self.AH.0 += h0;
|
|
|
|
|
self.AH.1 += h1;
|
2022-04-21 21:36:18 -04:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-29 15:28:04 -04:00
|
|
|
fn context(&self) -> Vec<u8> {
|
|
|
|
|
let mut context = vec![];
|
2022-04-29 22:03:34 -04:00
|
|
|
// This should be redundant as the image should be in the addendum if using InputMultisig and
|
|
|
|
|
// in msg if signing a Transaction, yet this ensures CLSAG takes responsibility for its own
|
|
|
|
|
// security boundaries
|
|
|
|
|
context.extend(&self.image.unwrap().compress().to_bytes());
|
2022-04-30 01:41:05 -04:00
|
|
|
context.extend(&self.data.msg());
|
2022-04-29 15:28:04 -04:00
|
|
|
context.extend(&self.input.context());
|
|
|
|
|
context
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-21 21:36:18 -04:00
|
|
|
fn sign_share(
|
|
|
|
|
&mut self,
|
2022-04-23 03:49:30 -04:00
|
|
|
view: &ParamsView<Ed25519>,
|
2022-04-21 21:36:18 -04:00
|
|
|
nonce_sum: dfg::EdwardsPoint,
|
2022-04-29 22:03:34 -04:00
|
|
|
b: dfg::Scalar,
|
2022-04-23 03:49:30 -04:00
|
|
|
nonce: dfg::Scalar,
|
|
|
|
|
_: &[u8]
|
2022-04-21 21:36:18 -04:00
|
|
|
) -> dfg::Scalar {
|
2022-04-29 22:03:34 -04:00
|
|
|
// Apply the binding factor to the H variant of the nonce
|
|
|
|
|
self.AH.0 += self.AH.1 * b;
|
|
|
|
|
|
2022-04-21 21:36:18 -04:00
|
|
|
// Use everyone's commitments to derive a random source all signers can agree upon
|
|
|
|
|
// Cannot be manipulated to effect and all signers must, and will, know this
|
2022-04-29 15:28:04 -04:00
|
|
|
// Uses the context as well to prevent passive observers of messages from being able to break
|
|
|
|
|
// privacy, as the context includes the index of the output in the ring, which can only be
|
|
|
|
|
// known if you have the view key and know which of the wallet's TXOs is being spent
|
2022-04-28 17:12:54 -04:00
|
|
|
let mut seed = b"CLSAG_randomness".to_vec();
|
|
|
|
|
seed.extend(&self.context());
|
|
|
|
|
seed.extend(&self.b);
|
|
|
|
|
let mut rng = ChaCha12Rng::from_seed(Blake2b512::digest(seed)[0 .. 32].try_into().unwrap());
|
2022-04-28 12:01:20 -04:00
|
|
|
|
2022-04-21 21:36:18 -04:00
|
|
|
#[allow(non_snake_case)]
|
2022-04-23 03:49:30 -04:00
|
|
|
let (clsag, c, mu_C, z, mu_P, C_out) = sign_core(
|
2022-04-28 17:12:54 -04:00
|
|
|
&mut rng,
|
2022-04-30 01:41:05 -04:00
|
|
|
&self.data.msg(),
|
2022-04-28 12:01:20 -04:00
|
|
|
&self.input,
|
2022-04-29 22:03:34 -04:00
|
|
|
&self.image.unwrap(),
|
2022-04-30 01:41:05 -04:00
|
|
|
self.data.mask_sum(),
|
2022-04-23 03:49:30 -04:00
|
|
|
nonce_sum.0,
|
2022-04-29 22:03:34 -04:00
|
|
|
self.AH.0.0
|
2022-04-23 03:49:30 -04:00
|
|
|
);
|
2022-04-28 12:01:20 -04:00
|
|
|
self.interim = Some(ClsagSignInterim { c: c * mu_P, s: c * mu_C * z, clsag, C_out });
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-04-28 12:01:20 -04:00
|
|
|
let share = dfg::Scalar(nonce.0 - (c * mu_P * view.secret_share().0));
|
2022-04-21 21:36:18 -04:00
|
|
|
|
|
|
|
|
share
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn verify(
|
|
|
|
|
&self,
|
|
|
|
|
_: dfg::EdwardsPoint,
|
|
|
|
|
_: dfg::EdwardsPoint,
|
|
|
|
|
sum: dfg::Scalar
|
|
|
|
|
) -> Option<Self::Signature> {
|
|
|
|
|
let interim = self.interim.as_ref().unwrap();
|
|
|
|
|
|
|
|
|
|
let mut clsag = interim.clsag.clone();
|
2022-04-28 12:01:20 -04:00
|
|
|
clsag.s[self.input.i] = Key { key: (sum.0 - interim.s).to_bytes() };
|
2022-04-30 01:41:05 -04:00
|
|
|
if verify(&clsag, &self.data.msg(), self.image.unwrap(), &self.input.ring, interim.C_out) {
|
2022-04-21 21:36:18 -04:00
|
|
|
return Some((clsag, interim.C_out));
|
|
|
|
|
}
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn verify_share(
|
|
|
|
|
&self,
|
|
|
|
|
verification_share: dfg::EdwardsPoint,
|
|
|
|
|
nonce: dfg::EdwardsPoint,
|
|
|
|
|
share: dfg::Scalar,
|
|
|
|
|
) -> bool {
|
|
|
|
|
let interim = self.interim.as_ref().unwrap();
|
|
|
|
|
return (&share.0 * &ED25519_BASEPOINT_TABLE) == (
|
2022-04-28 12:01:20 -04:00
|
|
|
nonce.0 - (interim.c * verification_share.0)
|
2022-04-21 21:36:18 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-29 22:03:34 -04:00
|
|
|
|
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
|
#[derive(Clone, Debug)]
|
2022-04-30 01:41:05 -04:00
|
|
|
pub struct InputMultisig<D: TransactionData>(EdwardsPoint, Multisig<D>);
|
2022-04-29 22:03:34 -04:00
|
|
|
|
2022-04-30 01:41:05 -04:00
|
|
|
impl<D: TransactionData> InputMultisig<D> {
|
2022-04-29 22:03:34 -04:00
|
|
|
pub fn new(
|
|
|
|
|
input: Input,
|
2022-04-30 01:41:05 -04:00
|
|
|
msg: D
|
|
|
|
|
) -> Result<InputMultisig<D>, MultisigError> {
|
2022-04-29 22:03:34 -04:00
|
|
|
Ok(InputMultisig(EdwardsPoint::identity(), Multisig::new(input, msg)?))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn image(&self) -> EdwardsPoint {
|
|
|
|
|
self.0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-30 01:41:05 -04:00
|
|
|
impl<D: TransactionData> Algorithm<Ed25519> for InputMultisig<D> {
|
2022-04-29 22:03:34 -04:00
|
|
|
type Signature = (Clsag, EdwardsPoint);
|
|
|
|
|
|
|
|
|
|
fn addendum_commit_len() -> usize {
|
2022-04-30 01:41:05 -04:00
|
|
|
32 + Multisig::<D>::addendum_commit_len()
|
2022-04-29 22:03:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn preprocess_addendum<R: RngCore + CryptoRng>(
|
|
|
|
|
rng: &mut R,
|
|
|
|
|
view: &ParamsView<Ed25519>,
|
|
|
|
|
nonces: &[dfg::Scalar; 2]
|
|
|
|
|
) -> Vec<u8> {
|
|
|
|
|
let (mut serialized, end) = key_image::generate_share(rng, view);
|
2022-04-30 01:41:05 -04:00
|
|
|
serialized.extend(Multisig::<D>::preprocess_addendum(rng, view, nonces));
|
2022-04-29 22:03:34 -04:00
|
|
|
serialized.extend(end);
|
|
|
|
|
serialized
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn process_addendum(
|
|
|
|
|
&mut self,
|
|
|
|
|
view: &ParamsView<Ed25519>,
|
|
|
|
|
l: usize,
|
|
|
|
|
commitments: &[dfg::EdwardsPoint; 2],
|
|
|
|
|
serialized: &[u8]
|
|
|
|
|
) -> Result<(), FrostError> {
|
|
|
|
|
let (image, serialized) = key_image::verify_share(view, l, serialized).map_err(|_| FrostError::InvalidShare(l))?;
|
|
|
|
|
self.0 += image;
|
|
|
|
|
if l == *view.included().last().unwrap() {
|
|
|
|
|
self.1.set_image(self.0);
|
|
|
|
|
}
|
|
|
|
|
self.1.process_addendum(view, l, commitments, &serialized)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn context(&self) -> Vec<u8> {
|
|
|
|
|
self.1.context()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn sign_share(
|
|
|
|
|
&mut self,
|
|
|
|
|
view: &ParamsView<Ed25519>,
|
|
|
|
|
nonce_sum: dfg::EdwardsPoint,
|
|
|
|
|
b: dfg::Scalar,
|
|
|
|
|
nonce: dfg::Scalar,
|
|
|
|
|
msg: &[u8]
|
|
|
|
|
) -> dfg::Scalar {
|
|
|
|
|
self.1.sign_share(view, nonce_sum, b, nonce, msg)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn verify(
|
|
|
|
|
&self,
|
|
|
|
|
group_key: dfg::EdwardsPoint,
|
|
|
|
|
nonce: dfg::EdwardsPoint,
|
|
|
|
|
sum: dfg::Scalar
|
|
|
|
|
) -> Option<Self::Signature> {
|
|
|
|
|
self.1.verify(group_key, nonce, sum)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn verify_share(
|
|
|
|
|
&self,
|
|
|
|
|
verification_share: dfg::EdwardsPoint,
|
|
|
|
|
nonce: dfg::EdwardsPoint,
|
|
|
|
|
share: dfg::Scalar,
|
|
|
|
|
) -> bool {
|
|
|
|
|
self.1.verify_share(verification_share, nonce, share)
|
|
|
|
|
}
|
|
|
|
|
}
|