2022-11-04 12:03:29 +00:00
|
|
|
use std::io::{self, Read, Write};
|
|
|
|
|
|
|
|
use zeroize::Zeroize;
|
|
|
|
|
2023-03-02 14:29:37 +00:00
|
|
|
use transcript::{Transcript, SecureDigest, DigestTranscript};
|
2022-11-04 12:03:29 +00:00
|
|
|
|
2023-03-07 08:46:16 +00:00
|
|
|
use ciphersuite::{
|
|
|
|
group::{
|
|
|
|
ff::{Field, PrimeField},
|
|
|
|
Group, GroupEncoding,
|
|
|
|
},
|
|
|
|
Ciphersuite,
|
2022-11-04 12:03:29 +00:00
|
|
|
};
|
|
|
|
use multiexp::multiexp_vartime;
|
|
|
|
|
|
|
|
use crate::SchnorrSignature;
|
|
|
|
|
2023-03-02 15:04:18 +00:00
|
|
|
// Returns a unbiased scalar weight to use on a signature in order to prevent malleability
|
2023-03-07 10:30:21 +00:00
|
|
|
fn weight<D: Send + Clone + SecureDigest, F: PrimeField>(digest: &mut DigestTranscript<D>) -> F {
|
2023-03-02 15:04:18 +00:00
|
|
|
let mut bytes = digest.challenge(b"aggregation_weight");
|
2022-11-04 12:03:29 +00:00
|
|
|
debug_assert_eq!(bytes.len() % 8, 0);
|
2023-03-02 15:04:18 +00:00
|
|
|
// This should be guaranteed thanks to SecureDigest
|
|
|
|
debug_assert!(bytes.len() >= 32);
|
2022-11-04 12:03:29 +00:00
|
|
|
|
|
|
|
let mut res = F::zero();
|
|
|
|
let mut i = 0;
|
2023-03-02 15:04:18 +00:00
|
|
|
|
|
|
|
// Derive a scalar from enough bits of entropy that bias is < 2^128
|
|
|
|
// This can't be const due to its usage of a generic
|
|
|
|
// Also due to the usize::try_from, yet that could be replaced with an `as`
|
|
|
|
// The + 7 forces it to round up
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
let BYTES: usize = usize::try_from(((F::NUM_BITS + 128) + 7) / 8).unwrap();
|
|
|
|
|
|
|
|
let mut remaining = BYTES;
|
|
|
|
|
|
|
|
// We load bits in as u64s
|
|
|
|
const WORD_LEN_IN_BITS: usize = 64;
|
|
|
|
const WORD_LEN_IN_BYTES: usize = WORD_LEN_IN_BITS / 8;
|
|
|
|
|
|
|
|
let mut first = true;
|
|
|
|
while i < remaining {
|
|
|
|
// Shift over the already loaded bits
|
|
|
|
if !first {
|
|
|
|
for _ in 0 .. WORD_LEN_IN_BITS {
|
2022-11-04 12:03:29 +00:00
|
|
|
res += res;
|
|
|
|
}
|
|
|
|
}
|
2023-03-02 15:04:18 +00:00
|
|
|
first = false;
|
|
|
|
|
|
|
|
// Add the next 64 bits
|
|
|
|
res += F::from(u64::from_be_bytes(bytes[i .. (i + WORD_LEN_IN_BYTES)].try_into().unwrap()));
|
|
|
|
i += WORD_LEN_IN_BYTES;
|
|
|
|
|
|
|
|
// If we've exhausted this challenge, get another
|
|
|
|
if i == bytes.len() {
|
|
|
|
bytes = digest.challenge(b"aggregation_weight_continued");
|
|
|
|
remaining -= i;
|
|
|
|
i = 0;
|
|
|
|
}
|
2022-11-04 12:03:29 +00:00
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2022-12-09 03:10:12 +00:00
|
|
|
/// Aggregate Schnorr signature as defined in <https://eprint.iacr.org/2021/350>.
|
2022-11-04 12:03:29 +00:00
|
|
|
#[allow(non_snake_case)]
|
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, Zeroize)]
|
|
|
|
pub struct SchnorrAggregate<C: Ciphersuite> {
|
|
|
|
pub Rs: Vec<C::G>,
|
|
|
|
pub s: C::F,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<C: Ciphersuite> SchnorrAggregate<C> {
|
|
|
|
/// Read a SchnorrAggregate from something implementing Read.
|
|
|
|
pub fn read<R: Read>(reader: &mut R) -> io::Result<Self> {
|
|
|
|
let mut len = [0; 4];
|
|
|
|
reader.read_exact(&mut len)?;
|
|
|
|
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
let mut Rs = vec![];
|
|
|
|
for _ in 0 .. u32::from_le_bytes(len) {
|
|
|
|
Rs.push(C::read_G(reader)?);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(SchnorrAggregate { Rs, s: C::read_F(reader)? })
|
|
|
|
}
|
|
|
|
|
2023-03-02 14:14:36 +00:00
|
|
|
/// Write a SchnorrAggregate to something implementing Write.
|
2022-11-04 12:03:29 +00:00
|
|
|
pub fn write<W: Write>(&self, writer: &mut W) -> io::Result<()> {
|
|
|
|
writer.write_all(
|
|
|
|
&u32::try_from(self.Rs.len())
|
|
|
|
.expect("more than 4 billion signatures in aggregate")
|
|
|
|
.to_le_bytes(),
|
|
|
|
)?;
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
for R in &self.Rs {
|
|
|
|
writer.write_all(R.to_bytes().as_ref())?;
|
|
|
|
}
|
|
|
|
writer.write_all(self.s.to_repr().as_ref())
|
|
|
|
}
|
|
|
|
|
2023-03-07 10:34:29 +00:00
|
|
|
/// Serialize a SchnorrAggregate, returning a `Vec<u8>`.
|
2022-11-04 12:03:29 +00:00
|
|
|
pub fn serialize(&self) -> Vec<u8> {
|
|
|
|
let mut buf = vec![];
|
|
|
|
self.write(&mut buf).unwrap();
|
|
|
|
buf
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Perform signature verification.
|
2023-03-02 14:14:36 +00:00
|
|
|
///
|
2023-03-02 14:29:37 +00:00
|
|
|
/// Challenges must be properly crafted, which means being binding to the public key, nonce, and
|
|
|
|
/// any message. Failure to do so will let a malicious adversary to forge signatures for
|
2023-03-02 14:14:36 +00:00
|
|
|
/// different keys/messages.
|
2023-03-02 14:29:37 +00:00
|
|
|
///
|
|
|
|
/// The DST used here must prevent a collision with whatever hash function produced the
|
|
|
|
/// challenges.
|
2022-11-04 12:03:29 +00:00
|
|
|
#[must_use]
|
2023-03-02 15:04:18 +00:00
|
|
|
pub fn verify(&self, dst: &'static [u8], keys_and_challenges: &[(C::G, C::F)]) -> bool {
|
2022-11-04 12:03:29 +00:00
|
|
|
if self.Rs.len() != keys_and_challenges.len() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-03-02 15:04:18 +00:00
|
|
|
let mut digest = DigestTranscript::<C::H>::new(dst);
|
2023-03-02 14:29:37 +00:00
|
|
|
digest.domain_separate(b"signatures");
|
|
|
|
for (_, challenge) in keys_and_challenges {
|
|
|
|
digest.append_message(b"challenge", challenge.to_repr());
|
2022-11-04 12:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut pairs = Vec::with_capacity((2 * keys_and_challenges.len()) + 1);
|
|
|
|
for (i, (key, challenge)) in keys_and_challenges.iter().enumerate() {
|
2023-03-02 15:04:18 +00:00
|
|
|
let z = weight(&mut digest);
|
2022-11-04 12:03:29 +00:00
|
|
|
pairs.push((z, self.Rs[i]));
|
|
|
|
pairs.push((z * challenge, *key));
|
|
|
|
}
|
|
|
|
pairs.push((-self.s, C::generator()));
|
|
|
|
multiexp_vartime(&pairs).is_identity().into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
#[derive(Clone, Debug, Zeroize)]
|
2023-03-02 15:04:18 +00:00
|
|
|
pub struct SchnorrAggregator<C: Ciphersuite> {
|
|
|
|
digest: DigestTranscript<C::H>,
|
2022-11-04 12:03:29 +00:00
|
|
|
sigs: Vec<SchnorrSignature<C>>,
|
|
|
|
}
|
|
|
|
|
2023-03-02 15:04:18 +00:00
|
|
|
impl<C: Ciphersuite> SchnorrAggregator<C> {
|
2022-11-04 12:03:29 +00:00
|
|
|
/// Create a new aggregator.
|
2023-03-02 14:29:37 +00:00
|
|
|
///
|
|
|
|
/// The DST used here must prevent a collision with whatever hash function produced the
|
|
|
|
/// challenges.
|
|
|
|
pub fn new(dst: &'static [u8]) -> Self {
|
2023-03-02 15:04:18 +00:00
|
|
|
let mut res = Self { digest: DigestTranscript::<C::H>::new(dst), sigs: vec![] };
|
2023-03-02 14:29:37 +00:00
|
|
|
res.digest.domain_separate(b"signatures");
|
|
|
|
res
|
2022-11-04 12:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Aggregate a signature.
|
2023-03-02 14:29:37 +00:00
|
|
|
pub fn aggregate(&mut self, challenge: C::F, sig: SchnorrSignature<C>) {
|
|
|
|
self.digest.append_message(b"challenge", challenge.to_repr());
|
2022-11-04 12:03:29 +00:00
|
|
|
self.sigs.push(sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Complete aggregation, returning None if none were aggregated.
|
2023-03-02 14:29:37 +00:00
|
|
|
pub fn complete(mut self) -> Option<SchnorrAggregate<C>> {
|
2022-11-04 12:03:29 +00:00
|
|
|
if self.sigs.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut aggregate =
|
|
|
|
SchnorrAggregate { Rs: Vec::with_capacity(self.sigs.len()), s: C::F::zero() };
|
|
|
|
for i in 0 .. self.sigs.len() {
|
|
|
|
aggregate.Rs.push(self.sigs[i].R);
|
2023-03-02 15:04:18 +00:00
|
|
|
aggregate.s += self.sigs[i].s * weight::<_, C::F>(&mut self.digest);
|
2022-11-04 12:03:29 +00:00
|
|
|
}
|
|
|
|
Some(aggregate)
|
|
|
|
}
|
|
|
|
}
|