1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//! Implementation of aggregate signatures specified in <https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-bls-signature-04>
#[cfg(feature = "ffi")]
mod ffi;

use crate::{
    common::{SerdeBase16Serialize, Serialize, *},
    curve_arithmetic::{Curve, Pairing, Value},
    random_oracle::RandomOracle,
    sigma_protocols::{common::*, dlog::*},
};
use ff::Field;
use rand::Rng;
use rayon::iter::*;
use sha2::{digest::Output, Digest, Sha512};

/// Size of the aggregate signature public key in bytes.
pub const PUBLIC_KEY_SIZE: usize = 96;
/// Size of the aggregate signature secret key in bytes.
pub const SECRET_KEY_SIZE: usize = 32;
/// Size of the aggregate signature in bytes.
pub const SIGNATURE_SIZE: usize = 48;

/// A Secret Key is a scalar in the scalarfield of the pairing.
///
/// EQUALITY IS NOT CONSTANT TIME!!! Do not use in production
/// the trait is implemented only for testing purposes
#[derive(Debug, Eq, Serialize)]
pub struct SecretKey<P: Pairing>(P::ScalarField);

impl<P: Pairing> SecretKey<P> {
    pub fn generate<R: Rng>(rng: &mut R) -> SecretKey<P> { SecretKey(P::generate_scalar(rng)) }

    /// Sign a message using the SecretKey
    pub fn sign(&self, m: &[u8]) -> Signature<P> {
        let g1_hash = P::G1::hash_to_group(m);
        let signature = g1_hash.mul_by_scalar(&self.0);
        Signature(signature)
    }

    /// Prove knowledge of the secret key with respect to the challenge given
    /// via the random oracle.
    pub fn prove<R: Rng>(&self, csprng: &mut R, ro: &mut RandomOracle) -> Proof<P> {
        let prover = Dlog {
            public: P::G2::one_point().mul_by_scalar(&self.0),
            coeff:  P::G2::one_point(),
        };
        let secret = DlogSecret {
            secret: Value::new(self.0),
        };
        prove(ro, &prover, secret, csprng)
            .expect("Input-data is valid, so proving should succeed for this dlog proof.")
    }
}

impl<P: Pairing> Clone for SecretKey<P> {
    fn clone(&self) -> Self { SecretKey(self.0) }
}

impl<P: Pairing> Copy for SecretKey<P> {}

/// NOT CONSTANT TIME!!!
/// USE ONLY FOR TESTING!!!
impl<P: Pairing> PartialEq for SecretKey<P> {
    fn eq(&self, other: &Self) -> bool { self.0 == other.0 }
}

/// A Public Key is a point on the second curve of the pairing
#[derive(Debug, Eq, Serialize, SerdeBase16Serialize)]
pub struct PublicKey<P: Pairing>(P::G2);

impl<P: Pairing> PublicKey<P> {
    /// Derived from a secret key sk by exponentiating the generator of G2 with
    /// sk.
    ///
    /// For now, the generator used is the default generator of the underlying
    /// library however, this should be parametrized in the future
    pub fn from_secret(sk: &SecretKey<P>) -> PublicKey<P> {
        PublicKey(P::G2::one_point().mul_by_scalar(&sk.0))
    }

    /// Verifies a single message and signature pair using this PublicKey by
    /// checking that     pairing(sig, g_2) == pairing(g1_hash(m),
    /// public_key) where g_2 is the generator of G2.
    ///
    /// For now, the generator used is the default generator of the underlying
    /// library however, this should be parametrized in the future
    pub fn verify(&self, m: &[u8], signature: Signature<P>) -> bool {
        let g1_hash = P::G1::hash_to_group(m);
        // compute pairings in parallel
        P::check_pairing_eq(&signature.0, &P::G2::one_point(), &g1_hash, &self.0)
    }

    /// Check proof of knowledge of the secret key with respect to the public
    /// key and the challenge which is given in terms of a random oracle.
    pub fn check_proof(&self, ro: &mut RandomOracle, proof: &Proof<P>) -> bool {
        let verifier = Dlog {
            public: self.0,
            coeff:  P::G2::one_point(),
        };
        verify(ro, &verifier, proof)
    }
}

impl<P: Pairing> Clone for PublicKey<P> {
    fn clone(&self) -> Self { PublicKey(self.0) }
}

impl<P: Pairing> Copy for PublicKey<P> {}

impl<P: Pairing> PartialEq for PublicKey<P> {
    fn eq(&self, other: &Self) -> bool { self.0 == other.0 }
}

#[derive(Debug, Eq, Serialize)]
pub struct Signature<P: Pairing>(P::G1);

impl<P: Pairing> Signature<P> {
    /// Aggregates this signatures with the given signature.
    pub fn aggregate(&self, to_aggregate: Signature<P>) -> Signature<P> {
        Signature(self.0.plus_point(&to_aggregate.0))
    }

    /// The empty signature is the unit with respect to aggregation,
    /// and can be used as a dummy signature.
    pub fn empty() -> Self { Signature(P::G1::zero_point()) }
}

impl<P: Pairing> Clone for Signature<P> {
    fn clone(&self) -> Self { Signature(self.0) }
}

impl<P: Pairing> Copy for Signature<P> {}

impl<P: Pairing> PartialEq for Signature<P> {
    fn eq(&self, other: &Self) -> bool { self.0 == other.0 }
}

/// A proof of knowledge of a secretkey
pub type Proof<P> = SigmaProof<Response<<P as Pairing>::G2>>;

/// Verifies an aggregate signature on pairs `(messages m_i, PK_i)` `for i=1..n`
/// by checking     `pairing(sig, g_2) == product_{i=0}^n (
/// pairing(g1_hash(m_i), PK_i) )` where `g_2` is the generator of G2.
/// Verification returns false if any two messages are not distinct
///
/// For now, the generator used is the default generator of the underlying
/// library however, this should be parametrized in the future
pub fn verify_aggregate_sig<P: Pairing>(
    m_pk_pairs: &[(&[u8], PublicKey<P>)],
    signature: Signature<P>,
) -> bool {
    // Check for duplicates in messages. Reject if any
    if has_duplicates(m_pk_pairs) {
        return false;
    }
    // verifying against the empty set of signers always fails
    if m_pk_pairs.is_empty() {
        return false;
    }

    let product = m_pk_pairs
        .par_iter()
        .fold(<P::TargetField as Field>::one, |prod, (m, pk)| {
            let g1_hash = P::G1::hash_to_group(m);
            let paired = P::pair(&g1_hash, &pk.0);
            let mut p = prod;
            p.mul_assign(&paired);
            p
        })
        .reduce(<P::TargetField as Field>::one, |prod, x| {
            let mut p = prod;
            p.mul_assign(&x);
            p
        });

    P::pair(&signature.0, &P::G2::one_point()) == product
}

/// Verifies an aggregate signature on pairs `(messages m_i, set_i)` `for
/// i=1..n` but where `set_i` denotes the set of public keys corresponding to
/// the secret keys that signed m_i. This implements a combination of
/// AggregateVerify from Section 3.1.1 and FastAggregateVerify from Section
/// 3.3.4 of <https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-bls-signature-05>.
///
/// In particular, this function returns:
///
/// pairing(sig, g_2) == ∏ᵢ pairing(H(mᵢ), ∑ⱼ pkᵢⱼ)
///
/// Each message must be associated with at least one key. However, the result
/// of having a message with no keys will be the same as if the message was not
/// included.
///
/// For security, the holder of a key must be required to prove knowledge of the
/// secret key.
/// Precondition:
/// There must be at least one message, at least one key for each message and
/// each key may only occur once.
/// Note. This will accept a signature on the empty list. But this violates the
/// precondition stated above.
pub fn verify_aggregate_sig_hybrid<P: Pairing>(
    m_pk_pairs: &[(&[u8], &[PublicKey<P>])],
    signature: Signature<P>,
) -> bool {
    let product = m_pk_pairs
        .par_iter()
        .fold(<P::TargetField as Field>::one, |prod, (m, pks)| {
            let sum_pk_i = if pks.len() < 150 {
                pks.iter()
                    .fold(P::G2::zero_point(), |s, x| s.plus_point(&x.0))
            } else {
                pks.par_iter()
                    .fold(P::G2::zero_point, |s, x| s.plus_point(&x.0))
                    .reduce(P::G2::zero_point, |s, x| s.plus_point(&x))
            };
            let g1_hash = P::G1::hash_to_group(m);
            let paired = P::pair(&g1_hash, &sum_pk_i);
            let mut p = prod;
            p.mul_assign(&paired);
            p
        })
        .reduce(<P::TargetField as Field>::one, |prod, x| {
            let mut p = prod;
            p.mul_assign(&x);
            p
        });

    P::pair(&signature.0, &P::G2::one_point()) == product
}

/// Verifies an aggregate signature on the same message m under keys PK_i for
/// i=1..n by checking     pairing(sig, g_2) == pairing(g1_hash(m), sum_{i=0}^n
/// (PK_i)) where g_2 is the generator of G2.
///
/// For now, the generator used is the default generator of the underlying
/// library however, this should be parametrized in the future
pub fn verify_aggregate_sig_trusted_keys<P: Pairing>(
    m: &[u8],
    pks: &[PublicKey<P>],
    signature: Signature<P>,
) -> bool {
    // verifying against the empty set of signers always fails
    if pks.is_empty() {
        return false;
    }

    let sum = if pks.len() < 150 {
        pks.iter()
            .fold(P::G2::zero_point(), |s, x| s.plus_point(&x.0))
    } else {
        pks.par_iter()
            .fold(P::G2::zero_point, |s, x| s.plus_point(&x.0))
            .reduce(P::G2::zero_point, |s, x| s.plus_point(&x))
    };

    // compute pairings in parallel
    P::check_pairing_eq(
        &signature.0,
        &P::G2::one_point(),
        &P::G1::hash_to_group(m),
        &sum,
    )
}

// Checks for duplicates in a list of messages
// This is not very efficient - the sorting algorithm can exit as soon as it
// encounters an equality and report that a duplicate indeed exists.
// Consider building hashmap or Btree and exit as soon as a duplicate is seen
fn has_duplicates<T>(messages: &[(&[u8], T)]) -> bool {
    let mut message_hashes: Vec<_> = messages.iter().map(|x| hash_message(x.0)).collect();
    message_hashes.sort_unstable();
    for i in 1..message_hashes.len() {
        if message_hashes[i - 1] == message_hashes[i] {
            return true;
        }
    }
    false
}

// hashes a message using Sha512
fn hash_message(m: &[u8]) -> Output<Sha512> { Sha512::digest(m) }

#[cfg(test)]
mod test {
    use super::*;
    use pairing::bls12_381::Bls12;
    use rand::{rngs::StdRng, thread_rng, SeedableRng};
    use std::convert::TryFrom;

    const SIGNERS: usize = 500;
    const TEST_ITERATIONS: usize = 10;

    // returns a pair of lists (sks, pks), such that sks[i] and pks[i] are
    // corresponding secret and public key
    fn get_sks_pks<P: Pairing>(
        amt: usize,
        rng: &mut StdRng,
    ) -> (Vec<SecretKey<P>>, Vec<PublicKey<P>>) {
        let sks: Vec<SecretKey<P>> = (0..amt).map(|_| SecretKey::<P>::generate(rng)).collect();

        let pks: Vec<PublicKey<P>> = sks.iter().map(PublicKey::<P>::from_secret).collect();
        (sks, pks)
    }

    // returns a list of random bytes (of length 32)
    fn get_random_messages<R: Rng>(amt: usize, rng: &mut R) -> Vec<[u8; 32]> {
        (0..amt).map(|_| rng.gen::<[u8; 32]>()).collect()
    }

    #[test]
    fn test_sign_and_verify() {
        let mut rng: StdRng = SeedableRng::from_rng(thread_rng()).unwrap();

        for _ in 0..TEST_ITERATIONS {
            let sk = SecretKey::<Bls12>::generate(&mut rng);
            let pk = PublicKey::from_secret(&sk);

            // should verify correctly
            let m = rng.gen::<[u8; 32]>();
            let signature = sk.sign(&m);
            assert!(pk.verify(&m, signature));

            // should not verify!
            let signature = sk.sign(&m);
            let sk2 = SecretKey::<Bls12>::generate(&mut rng);
            let pk2 = PublicKey::from_secret(&sk2);
            assert!(!pk2.verify(&m, signature))
        }
    }

    macro_rules! aggregate_sigs {
        ($messages:expr, $sks:expr) => {{
            let mut sig = $sks[0].sign(&$messages[0]);
            for i in 1..$sks.len() {
                let my_sig = $sks[i].sign(&$messages[i]);
                sig = sig.aggregate(my_sig);
            }
            sig
        }};
    }

    #[test]
    fn test_verify_aggregate_sig() {
        let mut rng: StdRng = SeedableRng::from_rng(thread_rng()).unwrap();

        let (sks, pks) = get_sks_pks(SIGNERS, &mut rng);

        for _ in 0..TEST_ITERATIONS {
            let ms = get_random_messages(SIGNERS, &mut rng);
            let sig = aggregate_sigs!(ms, sks);

            let mut m_pk_pairs: Vec<(&[u8], PublicKey<Bls12>)> = Vec::new();
            let mut m_pk_pairs2: Vec<(&[u8], &[PublicKey<Bls12>])> = Vec::new();
            for i in 0..SIGNERS {
                m_pk_pairs.push((&ms[i], pks[i]));
                m_pk_pairs2.push((&ms[i], std::slice::from_ref(&pks[i])));
            }

            // signature should verify
            assert!(verify_aggregate_sig(&m_pk_pairs, sig));
            assert!(verify_aggregate_sig_hybrid(&m_pk_pairs2, sig));

            let (m_, pk_) = m_pk_pairs.pop().unwrap();
            let (_, pks_) = m_pk_pairs2.pop().unwrap();
            let new_pk = PublicKey::<Bls12>::from_secret(&SecretKey::<Bls12>::generate(&mut rng));
            m_pk_pairs.push((m_, new_pk));
            m_pk_pairs2.push((m_, std::slice::from_ref(&new_pk)));

            // altering a public key should make verification fail
            assert!(!verify_aggregate_sig(&m_pk_pairs, sig));
            assert!(!verify_aggregate_sig_hybrid(&m_pk_pairs2, sig));

            let new_m: [u8; 32] = rng.gen::<[u8; 32]>();
            m_pk_pairs.pop();
            m_pk_pairs.push((&new_m, pk_));
            m_pk_pairs2.pop();
            m_pk_pairs2.push((&new_m, pks_));

            // altering a message should make verification fail
            assert!(!verify_aggregate_sig(&m_pk_pairs, sig));
            assert!(!verify_aggregate_sig_hybrid(&m_pk_pairs2, sig));
        }
    }

    #[test]
    fn test_verify_aggregate_sig_trusted_keys() {
        let mut rng: StdRng = SeedableRng::from_rng(thread_rng()).unwrap();
        for _ in 0..TEST_ITERATIONS {
            let (sks, pks) = get_sks_pks(SIGNERS, &mut rng);
            let m: [u8; 32] = rng.gen::<[u8; 32]>();
            let sigs: Vec<Signature<Bls12>> = sks.iter().map(|sk| sk.sign(&m)).collect();
            let mut agg_sig = sigs[0];
            sigs.iter().skip(1).for_each(|x| {
                agg_sig = agg_sig.aggregate(*x);
            });

            assert!(verify_aggregate_sig_trusted_keys(&m, &pks, agg_sig));

            // test changing message makes verification fails
            let m_alt: [u8; 32] = rng.gen::<[u8; 32]>();
            assert!(!verify_aggregate_sig_trusted_keys(&m_alt, &pks, agg_sig));

            // test that adding or removing a public key makes verification fail
            let mut pks_alt = pks.clone();
            pks_alt.push(PublicKey::<Bls12>::from_secret(
                &SecretKey::<Bls12>::generate(&mut rng),
            ));
            assert!(!verify_aggregate_sig_trusted_keys(&m, &pks_alt, agg_sig));

            // test that removing a public key makes verification fail
            pks_alt.pop();
            pks_alt.pop();
            assert!(!verify_aggregate_sig_trusted_keys(&m, &pks_alt, agg_sig));

            let agg_sig_alt = Signature(<Bls12 as Pairing>::G1::generate(&mut rng));
            assert!(!verify_aggregate_sig_trusted_keys(&m, &pks, agg_sig_alt));
        }
    }

    #[test]
    fn test_verification_empty_signers() {
        let mut rng: StdRng = SeedableRng::from_rng(thread_rng()).unwrap();
        for _ in 0..TEST_ITERATIONS {
            let sk = SecretKey::<Bls12>::generate(&mut rng);
            let m: [u8; 32] = rng.gen::<[u8; 32]>();
            let sig = sk.sign(&m);

            assert!(!verify_aggregate_sig(&[], sig));
            assert!(!verify_aggregate_sig_trusted_keys(&m, &[], sig));
        }
    }

    #[test]
    fn test_has_duplicates() {
        let mut rng: StdRng = SeedableRng::from_rng(thread_rng()).unwrap();

        for _ in 0..TEST_ITERATIONS {
            let signers: u64 = u64::try_from(SIGNERS)
                .expect("The number of signers should be convertible to u64.");
            let mut ms: Vec<[u8; 8]> = (0..signers).map(|x| x.to_le_bytes()).collect();

            // Make a duplication in the messages
            let random_idx1: usize = rng.gen_range(0, SIGNERS);
            let mut random_idx2: usize = rng.gen_range(0, SIGNERS);
            while random_idx1 == random_idx2 {
                random_idx2 = rng.gen_range(0, SIGNERS)
            }
            ms[random_idx1] = ms[random_idx2];
            let vs: Vec<(&[u8], ())> = ms.iter().map(|x| (&x[..], ())).collect();

            let result = has_duplicates(&vs);
            assert!(result);
        }
    }

    #[test]
    fn test_to_from_bytes_identity() {
        let mut rng: StdRng = SeedableRng::from_rng(thread_rng()).unwrap();

        for _ in 0..100 {
            let m = rng.gen::<[u8; 32]>();
            let mut c = Vec::new();
            c.push(rng.gen::<u8>());
            let mut ro = RandomOracle::domain(&c);

            let sk = SecretKey::<Bls12>::generate(&mut rng);
            let pk = PublicKey::<Bls12>::from_secret(&sk);
            let sig = sk.sign(&m);
            let proof = sk.prove(&mut rng, &mut ro.split());

            let sk_from_bytes = serialize_deserialize(&sk);
            let pk_from_bytes = serialize_deserialize(&pk);
            let sig_from_bytes = serialize_deserialize(&sig);
            let proof_from_bytes = serialize_deserialize(&proof);

            let sk_from_bytes = sk_from_bytes.expect("Serialization failed.");
            let pk_from_bytes = pk_from_bytes.expect("Serialization failed.");
            let sig_from_bytes = sig_from_bytes.expect("Serialization failed.");
            let proof_from_bytes = proof_from_bytes.expect("Serialization failed.");

            assert_eq!(sig.0, sig_from_bytes.0);
            assert_eq!(sk.0, sk_from_bytes.0);
            assert_eq!(pk.0, pk_from_bytes.0);
            assert!(pk.check_proof(&mut ro.split(), &proof_from_bytes));
            assert!(pk.verify(&m, sig_from_bytes));
            assert!(pk_from_bytes.verify(&m, sig));
            assert!(pk.check_proof(&mut ro, &proof))
        }
    }

    #[test]
    fn test_to_bytes_correct_length() {
        let mut rng: StdRng = SeedableRng::from_rng(thread_rng()).unwrap();

        for _ in 0..100 {
            let m = rng.gen::<[u8; 32]>();
            let mut c = Vec::new();
            c.push(rng.gen::<u8>());
            let mut ro = RandomOracle::domain(&c);

            let sk = SecretKey::<Bls12>::generate(&mut rng);
            let pk = PublicKey::<Bls12>::from_secret(&sk);
            let sig = sk.sign(&m);
            let proof = sk.prove(&mut rng, &mut ro);

            let sk_bytes = to_bytes(&sk);
            let pk_bytes = to_bytes(&pk);
            let sig_bytes = to_bytes(&sig);
            let proof_bytes = to_bytes(&proof);

            assert_eq!(sk_bytes.len(), 32);
            assert_eq!(pk_bytes.len(), 96);
            assert_eq!(sig_bytes.len(), 48);
            assert_eq!(proof_bytes.len(), 64);
        }
    }

    #[test]
    fn test_proof_of_knowledge() {
        let mut csprng = thread_rng();
        for i in 0..100 {
            let n = (i % 32) + 1;
            let mut c1: Vec<u8>;
            let mut c2: Vec<u8>;
            loop {
                c1 = Vec::new();
                c2 = Vec::new();
                for _ in 0..n {
                    c1.push(csprng.gen::<u8>());
                    c2.push(csprng.gen::<u8>());
                }

                if c1 != c2 {
                    break;
                }
            }
            let mut ro1 = RandomOracle::domain(c1);
            let mut ro2 = RandomOracle::domain(c2);

            let sk = SecretKey::<Bls12>::generate(&mut csprng);
            let pk = PublicKey::<Bls12>::from_secret(&sk);
            let proof = sk.prove(&mut csprng, &mut ro1.split());

            assert!(pk.check_proof(&mut ro1.split(), &proof));
            // check that it doesn't verify a proof with the wrong context
            assert!(!(pk.check_proof(&mut ro2, &proof)));
            // check that the proof doesn't work for a different key
            let mut sk2: SecretKey<Bls12>;
            loop {
                sk2 = SecretKey::<Bls12>::generate(&mut csprng);
                if sk != sk2 {
                    break;
                }
            }
            let pk2 = PublicKey::<Bls12>::from_secret(&sk2);
            assert!(!(pk2.check_proof(&mut ro1, &proof)));
        }
    }
}