|
1 | | -//! Definitions used in tests for this crate. |
| 1 | +use std::collections::HashMap; |
2 | 2 |
|
3 | 3 | use ff::Field; |
4 | 4 | use group::prime::PrimeGroup; |
| 5 | +use rand::rngs::OsRng; |
5 | 6 | use rand::RngCore; |
6 | 7 |
|
7 | | -use crate::linear_relation::{msm_pr, CanonicalLinearRelation, LinearRelation}; |
| 8 | +use crate::fiat_shamir::Nizk; |
| 9 | +use crate::{ |
| 10 | + codec::Shake128DuplexSponge, linear_relation::CanonicalLinearRelation, |
| 11 | + schnorr_protocol::SchnorrProof, |
| 12 | +}; |
| 13 | + |
| 14 | +use crate::linear_relation::{msm_pr, LinearRelation}; |
8 | 15 |
|
9 | 16 | /// LinearMap for knowledge of a discrete logarithm relative to a fixed basepoint. |
10 | 17 | #[allow(non_snake_case)] |
@@ -227,22 +234,10 @@ pub fn bbs_blind_commitment<G: PrimeGroup, R: RngCore>( |
227 | 234 | (instance, witness) |
228 | 235 | } |
229 | 236 |
|
230 | | -/// Test function with the requested LinearRelation code |
231 | | -#[allow(non_snake_case)] |
232 | | -pub fn test_linear_relation_example<G: PrimeGroup>() -> LinearRelation<G> { |
233 | | - use ff::Field; |
234 | | - |
235 | | - let mut sigma__lr = LinearRelation::<G>::new(); |
236 | | - let x = sigma__lr.allocate_scalar(); |
237 | | - let B = sigma__lr.allocate_element(); |
238 | | - let _sigma__eq1 = sigma__lr.allocate_eq((x + (-<G::Scalar as Field>::ONE)) * B + (-B)); |
239 | | - |
240 | | - sigma__lr |
241 | | -} |
242 | 237 |
|
243 | 238 | /// LinearMap for the user's specific relation: A * 1 + gen__disj1_x_r * B |
244 | 239 | #[allow(non_snake_case)] |
245 | | -pub fn user_specific_linear_combination<G: PrimeGroup, R: RngCore>( |
| 240 | +pub fn weird_linear_combination<G: PrimeGroup, R: RngCore>( |
246 | 241 | rng: &mut R, |
247 | 242 | ) -> (CanonicalLinearRelation<G>, Vec<G::Scalar>) { |
248 | 243 | let B = G::random(&mut *rng); |
@@ -270,3 +265,64 @@ pub fn user_specific_linear_combination<G: PrimeGroup, R: RngCore>( |
270 | 265 | let instance = (&sigma__lr).try_into().unwrap(); |
271 | 266 | (instance, witness) |
272 | 267 | } |
| 268 | + |
| 269 | +/// Generic helper function to test both relation correctness and NIZK functionality |
| 270 | +#[test] |
| 271 | +fn test_common_relations() { |
| 272 | + use group::Group; |
| 273 | + type G = bls12_381::G1Projective; |
| 274 | + |
| 275 | + let mut instance_generators = HashMap::< |
| 276 | + &str, |
| 277 | + Box<dyn Fn(&mut OsRng) -> (CanonicalLinearRelation<G>, Vec<<G as Group>::Scalar>)>, |
| 278 | + >::new(); |
| 279 | + |
| 280 | + instance_generators.insert("dlog", Box::new(discrete_logarithm)); |
| 281 | + instance_generators.insert("shifted_dlog", Box::new(shifted_discrete_logarithm)); |
| 282 | + instance_generators.insert("dleq", Box::new(dleq)); |
| 283 | + instance_generators.insert("shifted_dleq", Box::new(shifted_dleq)); |
| 284 | + instance_generators.insert("pedersen_commitment", Box::new(pedersen_commitment)); |
| 285 | + instance_generators.insert( |
| 286 | + "pedersen_commitment_dleq", |
| 287 | + Box::new(pedersen_commitment_dleq), |
| 288 | + ); |
| 289 | + instance_generators.insert("bbs_blind_commitment", Box::new(bbs_blind_commitment)); |
| 290 | + instance_generators.insert( |
| 291 | + "weird_linear_combination", |
| 292 | + Box::new(weird_linear_combination), |
| 293 | + ); |
| 294 | + |
| 295 | + for (relation_name, relation_sampler) in instance_generators.iter() { |
| 296 | + let mut rng = OsRng; |
| 297 | + let (canonical_relation, witness) = relation_sampler(&mut rng); |
| 298 | + |
| 299 | + // Test the NIZK protocol |
| 300 | + let protocol = SchnorrProof(canonical_relation); |
| 301 | + let domain_sep = format!("test-fiat-shamir-{}", relation_name) |
| 302 | + .as_bytes() |
| 303 | + .to_vec(); |
| 304 | + let nizk = Nizk::<SchnorrProof<G>, Shake128DuplexSponge<G>>::new(&domain_sep, protocol); |
| 305 | + |
| 306 | + // Test both proof types |
| 307 | + let proof_batchable = nizk.prove_batchable(&witness, &mut OsRng).expect(&format!( |
| 308 | + "Failed to create batchable proof for {}", |
| 309 | + relation_name |
| 310 | + )); |
| 311 | + let proof_compact = nizk.prove_compact(&witness, &mut OsRng).expect(&format!( |
| 312 | + "Failed to create compact proof for {}", |
| 313 | + relation_name |
| 314 | + )); |
| 315 | + |
| 316 | + // Verify both proof types |
| 317 | + assert!( |
| 318 | + nizk.verify_batchable(&proof_batchable).is_ok(), |
| 319 | + "Batchable proof verification failed for {}", |
| 320 | + relation_name |
| 321 | + ); |
| 322 | + assert!( |
| 323 | + nizk.verify_compact(&proof_compact).is_ok(), |
| 324 | + "Compact proof verification failed for {}", |
| 325 | + relation_name |
| 326 | + ); |
| 327 | + } |
| 328 | +} |
0 commit comments