|
| 1 | +use curve25519_dalek::ristretto::RistrettoPoint; |
| 2 | +use group::Group; |
| 3 | +use rand::rngs::OsRng; |
| 4 | + |
| 5 | +use super::test_utils::{ |
| 6 | + bbs_blind_commitment_computation, discrete_logarithm, dleq, pedersen_commitment, |
| 7 | + pedersen_commitment_dleq, |
| 8 | +}; |
| 9 | +use crate::codec::ShakeCodec; |
| 10 | +use crate::composition::{Protocol, ProtocolWitness}; |
| 11 | +use crate::fiat_shamir::{HasGroupMorphism, NISigmaProtocol}; |
| 12 | +use crate::schnorr_protocol::SchnorrProtocol; |
| 13 | + |
| 14 | +type G = RistrettoPoint; |
| 15 | + |
| 16 | +#[allow(non_snake_case)] |
| 17 | +#[test] |
| 18 | +fn composition_proof_correct() { |
| 19 | + // Composition and verification of proof for the following protocol : |
| 20 | + // |
| 21 | + // protocol = And( |
| 22 | + // Or( dleq, pedersen_commitment ), |
| 23 | + // Simple( discrete_logarithm ), |
| 24 | + // And( pedersen_commitment_dleq, bbs_blind_commitment_computation ) |
| 25 | + // ) |
| 26 | + let mut rng = OsRng; |
| 27 | + let domain_sep = b"hello world"; |
| 28 | + |
| 29 | + // definitions of the underlying protocols |
| 30 | + let (morph1, witness1) = dleq(<G as Group>::Scalar::random(&mut rng), G::random(&mut rng)); |
| 31 | + let (morph2, _) = pedersen_commitment( |
| 32 | + G::random(&mut rng), |
| 33 | + <G as Group>::Scalar::random(&mut rng), |
| 34 | + <G as Group>::Scalar::random(&mut rng), |
| 35 | + ); |
| 36 | + let (morph3, witness3) = discrete_logarithm(<G as Group>::Scalar::random(&mut rng)); |
| 37 | + let (morph4, witness4) = pedersen_commitment_dleq( |
| 38 | + (0..4) |
| 39 | + .map(|_| G::random(&mut rng)) |
| 40 | + .collect::<Vec<_>>() |
| 41 | + .try_into() |
| 42 | + .unwrap(), |
| 43 | + (0..2) |
| 44 | + .map(|_| <G as Group>::Scalar::random(&mut rng)) |
| 45 | + .collect::<Vec<_>>() |
| 46 | + .try_into() |
| 47 | + .unwrap(), |
| 48 | + ); |
| 49 | + let (morph5, witness5) = bbs_blind_commitment_computation( |
| 50 | + (0..4) |
| 51 | + .map(|_| G::random(&mut rng)) |
| 52 | + .collect::<Vec<_>>() |
| 53 | + .try_into() |
| 54 | + .unwrap(), |
| 55 | + (0..3) |
| 56 | + .map(|_| <G as Group>::Scalar::random(&mut rng)) |
| 57 | + .collect::<Vec<_>>() |
| 58 | + .try_into() |
| 59 | + .unwrap(), |
| 60 | + <G as Group>::Scalar::random(&mut rng), |
| 61 | + ); |
| 62 | + |
| 63 | + // second layer protocol definitions |
| 64 | + let or_protocol1 = Protocol::Or(vec![ |
| 65 | + Protocol::Simple(SchnorrProtocol::from(morph1)), |
| 66 | + Protocol::Simple(SchnorrProtocol::from(morph2)), |
| 67 | + ]); |
| 68 | + let or_witness1 = ProtocolWitness::Or(0, vec![ProtocolWitness::Simple(witness1)]); |
| 69 | + |
| 70 | + let simple_protocol1 = Protocol::from(morph3); |
| 71 | + let simple_witness1 = ProtocolWitness::Simple(witness3); |
| 72 | + |
| 73 | + let and_protocol1 = Protocol::And(vec![ |
| 74 | + Protocol::Simple(SchnorrProtocol::from(morph4)), |
| 75 | + Protocol::Simple(SchnorrProtocol::from(morph5)), |
| 76 | + ]); |
| 77 | + let and_witness1 = ProtocolWitness::And(vec![ |
| 78 | + ProtocolWitness::Simple(witness4), |
| 79 | + ProtocolWitness::Simple(witness5), |
| 80 | + ]); |
| 81 | + |
| 82 | + // definition of the final protocol |
| 83 | + let protocol = Protocol::And(vec![or_protocol1, simple_protocol1, and_protocol1]); |
| 84 | + let witness = ProtocolWitness::And(vec![or_witness1, simple_witness1, and_witness1]); |
| 85 | + |
| 86 | + let mut nizk = |
| 87 | + NISigmaProtocol::<Protocol<RistrettoPoint>, ShakeCodec<G>>::new(domain_sep, protocol); |
| 88 | + |
| 89 | + nizk.sigmap |
| 90 | + .absorb_morphism_structure(&mut nizk.hash_state) |
| 91 | + .unwrap(); |
| 92 | + |
| 93 | + // Batchable and compact proofs |
| 94 | + let proof_batchable_bytes = nizk.prove_batchable(&witness, &mut rng).unwrap(); |
| 95 | + let proof_compact_bytes = nizk.prove_compact(&witness, &mut rng).unwrap(); |
| 96 | + // Verify proofs |
| 97 | + let verified_batchable = nizk.verify_batchable(&proof_batchable_bytes).is_ok(); |
| 98 | + let verified_compact = nizk.verify_compact(&proof_compact_bytes).is_ok(); |
| 99 | + assert!( |
| 100 | + verified_batchable & verified_compact, |
| 101 | + "Fiat-Shamir Schnorr proof verification failed" |
| 102 | + ); |
| 103 | +} |
0 commit comments