|
| 1 | +use bls12_381::{G1Projective as G, Scalar}; |
| 2 | +use core::str; |
| 3 | +use hex::FromHex; |
| 4 | +use json::JsonValue; |
| 5 | +use std::fs; |
| 6 | + |
| 7 | +use crate::codec::KeccakByteSchnorrCodec; |
| 8 | +use crate::fiat_shamir::NISigmaProtocol; |
| 9 | +use crate::tests::spec::{ |
| 10 | + custom_schnorr_protocol::SchnorrProtocolCustom, random::SRandom, rng::TestDRNG, |
| 11 | +}; |
| 12 | +use crate::tests::test_utils::{ |
| 13 | + bbs_blind_commitment_computation, discrete_logarithm, dleq, pedersen_commitment, |
| 14 | + pedersen_commitment_dleq, |
| 15 | +}; |
| 16 | + |
| 17 | +type NIProtocol = NISigmaProtocol<SchnorrProtocolCustom<G>, KeccakByteSchnorrCodec<G>>; |
| 18 | + |
| 19 | +/// Macro to generate non-interactive sigma protocols test functions |
| 20 | +macro_rules! generate_ni_function { |
| 21 | + ($name:ident, $test_fn:ident, $($param:tt),*) => { |
| 22 | + #[allow(non_snake_case)] |
| 23 | + fn $name(seed: &[u8], session_id: &[u8]) -> (Vec<Scalar>, Vec<u8>, Vec<u8>) { |
| 24 | + let mut rng = TestDRNG::new(seed); |
| 25 | + let (instance, witness) = $test_fn($(generate_ni_function!(@arg rng, $param)),*); |
| 26 | + |
| 27 | + let statement = instance.label(); |
| 28 | + let protocol = SchnorrProtocolCustom(instance); |
| 29 | + let nizk = NIProtocol::new(session_id, protocol); |
| 30 | + |
| 31 | + let proof_bytes = nizk.prove_batchable(&witness, &mut rng).unwrap(); |
| 32 | + let verified = nizk.verify_batchable(&proof_bytes).is_ok(); |
| 33 | + assert!(verified, "Fiat-Shamir Schnorr proof verification failed"); |
| 34 | + (witness, proof_bytes, statement) |
| 35 | + } |
| 36 | + }; |
| 37 | + |
| 38 | + (@arg $rng:ident, $type:ident) => { |
| 39 | + G::$type(&mut $rng) |
| 40 | + }; |
| 41 | + (@arg $rng:ident, [$type:ident; $count:expr]) => { |
| 42 | + (0..$count).map(|_| G::$type(&mut $rng)).collect::<Vec<_>>().try_into().unwrap() |
| 43 | + }; |
| 44 | +} |
| 45 | + |
| 46 | +generate_ni_function!(NI_discrete_logarithm, discrete_logarithm, srandom); |
| 47 | +generate_ni_function!(NI_dleq, dleq, prandom, srandom); |
| 48 | +generate_ni_function!( |
| 49 | + NI_pedersen_commitment, |
| 50 | + pedersen_commitment, |
| 51 | + prandom, |
| 52 | + srandom, |
| 53 | + srandom |
| 54 | +); |
| 55 | +generate_ni_function!( |
| 56 | + NI_pedersen_commitment_dleq, |
| 57 | + pedersen_commitment_dleq, |
| 58 | + [prandom; 4], |
| 59 | + [srandom; 2] |
| 60 | +); |
| 61 | +generate_ni_function!( |
| 62 | + NI_bbs_blind_commitment_computation, |
| 63 | + bbs_blind_commitment_computation, |
| 64 | + [prandom; 4], |
| 65 | + [srandom; 3], |
| 66 | + srandom |
| 67 | +); |
| 68 | + |
| 69 | +#[allow(clippy::type_complexity)] |
| 70 | +#[allow(non_snake_case)] |
| 71 | +#[test] |
| 72 | +fn test_spec_testvectors() { |
| 73 | + let seed = b"hello world"; |
| 74 | + let session_id = b"yellow submarineyellow submarine"; |
| 75 | + let vectors = extract_vectors("src/tests/spec/vectors/fixedLabelVectors.json").unwrap(); |
| 76 | + |
| 77 | + let functions: [fn(&[u8], &[u8]) -> (Vec<Scalar>, Vec<u8>, Vec<u8>); 5] = [ |
| 78 | + NI_discrete_logarithm, |
| 79 | + NI_dleq, |
| 80 | + NI_pedersen_commitment, |
| 81 | + NI_pedersen_commitment_dleq, |
| 82 | + NI_bbs_blind_commitment_computation, |
| 83 | + ]; |
| 84 | + |
| 85 | + for (i, f) in functions.iter().enumerate() { |
| 86 | + let (_, proof_bytes, statement) = f(seed, session_id); |
| 87 | + assert_eq!( |
| 88 | + session_id.as_slice(), |
| 89 | + vectors[i].0.as_slice(), |
| 90 | + "context for test vector {i} does not match" |
| 91 | + ); |
| 92 | + assert_eq!( |
| 93 | + proof_bytes, vectors[i].1, |
| 94 | + "proof bytes for test vector {i} does not match" |
| 95 | + ); |
| 96 | + assert_eq!( |
| 97 | + statement, vectors[i].2, |
| 98 | + "statement for test vector {i} does not match" |
| 99 | + ); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +#[allow(clippy::type_complexity)] |
| 104 | +fn extract_vectors(path: &str) -> json::Result<Vec<(Vec<u8>, Vec<u8>, Vec<u8>)>> { |
| 105 | + let content = fs::read_to_string(path).expect("Unable to read JSON file"); |
| 106 | + let root: JsonValue = json::parse(&content).expect("JSON parsing error"); |
| 107 | + root.entries() |
| 108 | + .map(|(_, obj)| { |
| 109 | + let context_hex = obj["Context"] |
| 110 | + .as_str() |
| 111 | + .expect("Context field not found or not a string"); |
| 112 | + let proof_hex = obj["Proof"] |
| 113 | + .as_str() |
| 114 | + .expect("Proof field not found or not a string"); |
| 115 | + let statement_hex = obj["Statement"] |
| 116 | + .as_str() |
| 117 | + .expect("Statement field not found or not a string"); |
| 118 | + Ok(( |
| 119 | + Vec::from_hex(context_hex).unwrap(), |
| 120 | + Vec::from_hex(proof_hex).unwrap(), |
| 121 | + Vec::from_hex(statement_hex).unwrap(), |
| 122 | + )) |
| 123 | + }) |
| 124 | + .collect() |
| 125 | +} |
0 commit comments