2022-04-29 22:03:34 -04:00
|
|
|
use core::fmt::Debug;
|
2022-04-30 04:32:19 -04:00
|
|
|
use std::{rc::Rc, cell::RefCell};
|
2022-04-29 22:03:34 -04:00
|
|
|
|
2022-05-03 07:20:24 -04:00
|
|
|
use rand_core::{RngCore, CryptoRng};
|
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
|
|
|
|
|
};
|
|
|
|
|
|
2022-05-03 07:20:24 -04:00
|
|
|
use monero::util::ringct::{Key, Clsag};
|
|
|
|
|
|
2022-04-21 21:36:18 -04:00
|
|
|
use group::Group;
|
2022-05-03 07:20:24 -04:00
|
|
|
|
|
|
|
|
use transcript::Transcript as TranscriptTrait;
|
2022-04-30 04:32:19 -04:00
|
|
|
use frost::{Curve, FrostError, algorithm::Algorithm, MultisigView};
|
2022-05-03 08:49:46 -04:00
|
|
|
use dalek_ff_group as dfg;
|
2022-04-21 21:36:18 -04:00
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
|
hash_to_point,
|
2022-05-03 08:49:46 -04:00
|
|
|
frost::{Transcript, 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-05-03 07:20:24 -04:00
|
|
|
impl Input {
|
|
|
|
|
pub fn transcript<T: TranscriptTrait>(&self, transcript: &mut T) {
|
|
|
|
|
// Ring index
|
|
|
|
|
transcript.append_message(b"ring_index", &[self.i]);
|
|
|
|
|
|
|
|
|
|
// Ring
|
|
|
|
|
let mut ring = vec![];
|
|
|
|
|
for pair in &self.ring {
|
|
|
|
|
// Doesn't include global output indexes as CLSAG doesn't care and won't be affected by it
|
|
|
|
|
// They're just a mutable reference to this data
|
|
|
|
|
ring.extend(&pair[0].compress().to_bytes());
|
|
|
|
|
ring.extend(&pair[1].compress().to_bytes());
|
|
|
|
|
}
|
|
|
|
|
transcript.append_message(b"ring", &ring);
|
|
|
|
|
|
|
|
|
|
// Doesn't include the commitment's parts as the above ring + index includes the commitment
|
|
|
|
|
// The only potential malleability would be if the G/H relationship is known breaking the
|
|
|
|
|
// discrete log problem, which breaks everything already
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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 04:32:19 -04:00
|
|
|
pub struct Multisig {
|
2022-05-03 07:20:24 -04:00
|
|
|
commitments_H: Vec<u8>,
|
|
|
|
|
image: EdwardsPoint,
|
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-30 04:32:19 -04:00
|
|
|
msg: Rc<RefCell<[u8; 32]>>,
|
2022-05-03 07:20:24 -04:00
|
|
|
mask: Rc<RefCell<Scalar>>,
|
2022-04-29 22:03:34 -04:00
|
|
|
|
2022-04-21 21:36:18 -04:00
|
|
|
interim: Option<ClsagSignInterim>
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-30 04:32:19 -04:00
|
|
|
impl Multisig {
|
2022-04-29 15:28:04 -04:00
|
|
|
pub fn new(
|
2022-04-29 22:03:34 -04:00
|
|
|
input: Input,
|
2022-04-30 04:32:19 -04:00
|
|
|
msg: Rc<RefCell<[u8; 32]>>,
|
2022-05-03 07:20:24 -04:00
|
|
|
mask: Rc<RefCell<Scalar>>,
|
2022-04-30 04:32:19 -04:00
|
|
|
) -> Result<Multisig, MultisigError> {
|
2022-04-21 21:36:18 -04:00
|
|
|
Ok(
|
|
|
|
|
Multisig {
|
2022-05-03 07:20:24 -04:00
|
|
|
commitments_H: vec![],
|
|
|
|
|
image: EdwardsPoint::identity(),
|
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-30 04:32:19 -04:00
|
|
|
msg,
|
2022-05-03 07:20:24 -04:00
|
|
|
mask,
|
2022-04-30 01:43:15 -04:00
|
|
|
|
2022-04-21 21:36:18 -04:00
|
|
|
interim: None
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
2022-04-30 04:32:19 -04:00
|
|
|
|
|
|
|
|
pub fn serialized_len() -> usize {
|
|
|
|
|
3 * (32 + 64)
|
|
|
|
|
}
|
2022-04-21 21:36:18 -04:00
|
|
|
}
|
|
|
|
|
|
2022-04-30 04:32:19 -04:00
|
|
|
impl Algorithm<Ed25519> for Multisig {
|
2022-05-03 07:20:24 -04:00
|
|
|
type Transcript = Transcript;
|
2022-04-21 21:36:18 -04:00
|
|
|
type Signature = (Clsag, EdwardsPoint);
|
|
|
|
|
|
|
|
|
|
fn preprocess_addendum<R: RngCore + CryptoRng>(
|
|
|
|
|
rng: &mut R,
|
2022-04-30 04:32:19 -04:00
|
|
|
view: &MultisigView<Ed25519>,
|
2022-04-21 21:36:18 -04:00
|
|
|
nonces: &[dfg::Scalar; 2]
|
|
|
|
|
) -> Vec<u8> {
|
2022-04-30 04:32:19 -04:00
|
|
|
let (share, proof) = key_image::generate_share(rng, view);
|
2022-04-30 01:43:15 -04:00
|
|
|
|
2022-04-21 21:36:18 -04:00
|
|
|
#[allow(non_snake_case)]
|
2022-04-23 03:49:30 -04:00
|
|
|
let H = hash_to_point(&view.group_key().0);
|
2022-04-30 01:43:15 -04:00
|
|
|
#[allow(non_snake_case)]
|
|
|
|
|
let nH = (nonces[0].0 * H, nonces[1].0 * H);
|
|
|
|
|
|
2022-04-30 04:32:19 -04:00
|
|
|
let mut serialized = Vec::with_capacity(Multisig::serialized_len());
|
|
|
|
|
serialized.extend(share.compress().to_bytes());
|
2022-04-30 01:43:15 -04:00
|
|
|
serialized.extend(nH.0.compress().to_bytes());
|
|
|
|
|
serialized.extend(nH.1.compress().to_bytes());
|
|
|
|
|
serialized.extend(&DLEqProof::prove(rng, &nonces[0].0, &H, &nH.0).serialize());
|
|
|
|
|
serialized.extend(&DLEqProof::prove(rng, &nonces[1].0, &H, &nH.1).serialize());
|
|
|
|
|
serialized.extend(proof);
|
2022-04-21 21:36:18 -04:00
|
|
|
serialized
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn process_addendum(
|
|
|
|
|
&mut self,
|
2022-04-30 04:32:19 -04:00
|
|
|
view: &MultisigView<Ed25519>,
|
2022-04-21 21:36:18 -04:00
|
|
|
l: usize,
|
|
|
|
|
commitments: &[dfg::EdwardsPoint; 2],
|
|
|
|
|
serialized: &[u8]
|
|
|
|
|
) -> Result<(), FrostError> {
|
2022-04-30 04:32:19 -04:00
|
|
|
if serialized.len() != Multisig::serialized_len() {
|
2022-04-21 21:36:18 -04:00
|
|
|
// Not an optimal error but...
|
2022-04-30 01:43:15 -04:00
|
|
|
Err(FrostError::InvalidCommitmentQuantity(l, 9, serialized.len() / 32))?;
|
2022-04-21 21:36:18 -04:00
|
|
|
}
|
|
|
|
|
|
2022-04-30 01:43:15 -04:00
|
|
|
let (share, serialized) = key_image::verify_share(view, l, serialized).map_err(|_| FrostError::InvalidShare(l))?;
|
|
|
|
|
self.image += share;
|
|
|
|
|
|
2022-05-03 07:20:24 -04:00
|
|
|
let alt = &hash_to_point(&self.input.ring[usize::from(self.input.i)][0]);
|
|
|
|
|
|
|
|
|
|
// Uses the same format FROST does for the expected commitments (nonce * G where this is nonce * H)
|
|
|
|
|
self.commitments_H.extend(&u64::try_from(l).unwrap().to_le_bytes());
|
|
|
|
|
self.commitments_H.extend(&serialized[0 .. 64]);
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-04-30 01:43:15 -04:00
|
|
|
#[allow(non_snake_case)]
|
|
|
|
|
let H = (
|
|
|
|
|
<Ed25519 as Curve>::G_from_slice(&serialized[0 .. 32]).map_err(|_| FrostError::InvalidCommitment(l))?,
|
|
|
|
|
<Ed25519 as Curve>::G_from_slice(&serialized[32 .. 64]).map_err(|_| FrostError::InvalidCommitment(l))?
|
|
|
|
|
);
|
|
|
|
|
|
2022-04-21 21:36:18 -04:00
|
|
|
DLEqProof::deserialize(&serialized[64 .. 128]).ok_or(FrostError::InvalidCommitment(l))?.verify(
|
|
|
|
|
&alt,
|
|
|
|
|
&commitments[0],
|
2022-04-30 01:43:15 -04:00
|
|
|
&H.0
|
2022-04-21 21:36:18 -04:00
|
|
|
).map_err(|_| FrostError::InvalidCommitment(l))?;
|
|
|
|
|
|
|
|
|
|
DLEqProof::deserialize(&serialized[128 .. 192]).ok_or(FrostError::InvalidCommitment(l))?.verify(
|
|
|
|
|
&alt,
|
|
|
|
|
&commitments[1],
|
2022-04-30 01:43:15 -04:00
|
|
|
&H.1
|
2022-04-21 21:36:18 -04:00
|
|
|
).map_err(|_| FrostError::InvalidCommitment(l))?;
|
|
|
|
|
|
2022-04-30 01:43:15 -04:00
|
|
|
self.AH.0 += H.0;
|
|
|
|
|
self.AH.1 += H.1;
|
2022-04-21 21:36:18 -04:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-03 07:20:24 -04:00
|
|
|
fn transcript(&self) -> Option<Self::Transcript> {
|
|
|
|
|
let mut transcript = Self::Transcript::new(b"CLSAG");
|
|
|
|
|
self.input.transcript(&mut transcript);
|
|
|
|
|
// Given the fact there's only ever one possible value for this, this may technically not need
|
|
|
|
|
// to be committed to. If signing a TX, it's be double committed to thanks to the message
|
|
|
|
|
// It doesn't hurt to have though and ensures security boundaries are well formed
|
|
|
|
|
transcript.append_message(b"image", &self.image.compress().to_bytes());
|
|
|
|
|
// Given this is guaranteed to match commitments, which FROST commits to, this also technically
|
|
|
|
|
// doesn't need to be committed to if a canonical serialization is guaranteed
|
|
|
|
|
// It, again, doesn't hurt to include and ensures security boundaries are well formed
|
|
|
|
|
transcript.append_message(b"commitments_H", &self.commitments_H);
|
|
|
|
|
transcript.append_message(b"message", &*self.msg.borrow());
|
|
|
|
|
transcript.append_message(b"mask", &self.mask.borrow().to_bytes());
|
|
|
|
|
Some(transcript)
|
2022-04-29 15:28:04 -04:00
|
|
|
}
|
|
|
|
|
|
2022-04-21 21:36:18 -04:00
|
|
|
fn sign_share(
|
|
|
|
|
&mut self,
|
2022-04-30 04:32:19 -04:00
|
|
|
view: &MultisigView<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-05-03 07:20:24 -04:00
|
|
|
// Use the transcript to get a seeded random number generator
|
|
|
|
|
// The transcript contains private data, preventing passive adversaries from recreating this
|
|
|
|
|
// process even if they have access to commitments (specifically, the ring index being signed
|
|
|
|
|
// for, along with the mask which should not only require knowing the shared keys yet also the
|
|
|
|
|
// input commitment mask)
|
|
|
|
|
let mut rng = self.transcript().unwrap().seeded_rng(b"decoy_responses", None);
|
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 04:32:19 -04:00
|
|
|
&self.msg.borrow(),
|
2022-04-28 12:01:20 -04:00
|
|
|
&self.input,
|
2022-04-30 01:43:15 -04:00
|
|
|
&self.image,
|
2022-05-03 07:20:24 -04:00
|
|
|
*self.mask.borrow(),
|
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-05-03 07:20:24 -04:00
|
|
|
clsag.s[usize::from(self.input.i)] = Key { key: (sum.0 - interim.s).to_bytes() };
|
2022-04-30 04:32:19 -04:00
|
|
|
if verify(&clsag, &self.msg.borrow(), self.image, &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
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|