From 8c4fccabea9400a367a1ee34947f49ab44ca0a1e Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Wed, 10 Jul 2024 11:39:04 +0200 Subject: [PATCH] Refactor --- src/pedersen.rs | 31 ++++++++++++++++++------------- src/s3id.rs | 38 ++++++++++++++++++-------------------- 2 files changed, 36 insertions(+), 33 deletions(-) diff --git a/src/pedersen.rs b/src/pedersen.rs index 88b2ced..8248cfb 100644 --- a/src/pedersen.rs +++ b/src/pedersen.rs @@ -693,11 +693,12 @@ mod test { #[test] fn pedersen_proof_2_pk() { + let mut rng = rand::thread_rng(); let pp = get_parameters(); - let msg_1 = Scalar::rand(&mut rand::thread_rng()); + let msg_1 = Scalar::rand(&mut rng); let (cm_1, o_1) = Commitment::commit(&msg_1); assert!(cm_1.verify(&msg_1, &o_1).is_ok()); - let msg_2 = Scalar::rand(&mut rand::thread_rng()); + let msg_2 = Scalar::rand(&mut rng); let (cm_2, o_2) = Commitment::commit(&msg_2); assert!(cm_2.verify(&msg_2, &o_2).is_ok()); @@ -710,12 +711,13 @@ mod test { #[test] fn multi_pedersen() { + let mut rng = rand::thread_rng(); let l = 10; let pp = MultiBasePublicParameters::new(l); - let value_0 = Scalar::rand(&mut rand::thread_rng()); + let value_0 = Scalar::rand(&mut rng); for idx in 0..l { - let value_i = Scalar::rand(&mut rand::thread_rng()); + let value_i = Scalar::rand(&mut rng); let (cm, o) = Commitment::index_commit(&value_0, idx, &value_i, &pp); assert!(cm .verify_index_commit(&value_0, idx, &value_i, &o, &pp) @@ -725,15 +727,16 @@ mod test { #[test] fn multi_pedersen_proof() { - let msg = Scalar::rand(&mut rand::thread_rng()); + let mut rng = rand::thread_rng(); + let msg = Scalar::rand(&mut rng); let (commitment, opening) = Commitment::commit(&msg); let l = 10; let pp = MultiBasePublicParameters::new(l); - let value_0 = Scalar::rand(&mut rand::thread_rng()); + let value_0 = Scalar::rand(&mut rng); for idx in 0..l { - let value_i = Scalar::rand(&mut rand::thread_rng()); + let value_i = Scalar::rand(&mut rng); let (cm, o) = Commitment::index_commit(&value_0, idx, &value_i, &pp); assert!(cm .verify_index_commit(&value_0, idx, &value_i, &o, &pp) @@ -749,13 +752,14 @@ mod test { #[test] fn multi_index_pedersen() { + let mut rng = rand::thread_rng(); let l = 10; let pp = MultiBasePublicParameters::new(l); - let value_0 = Scalar::rand(&mut rand::thread_rng()); + let value_0 = Scalar::rand(&mut rng); let values = [ - (2usize, Scalar::rand(&mut rand::thread_rng())), - (7usize, Scalar::rand(&mut rand::thread_rng())), + (2usize, Scalar::rand(&mut rng)), + (7usize, Scalar::rand(&mut rng)), ]; let (cm, o) = Commitment::multi_index_commit(&value_0, values.iter().copied(), &pp); assert!(cm @@ -765,13 +769,14 @@ mod test { #[test] fn multi_index_pedersen_proof() { + let mut rng = rand::thread_rng(); let l = 10; let pp = MultiBasePublicParameters::new(l); - let value_0 = Scalar::rand(&mut rand::thread_rng()); + let value_0 = Scalar::rand(&mut rng); let values = [ - (2usize, Scalar::rand(&mut rand::thread_rng())), - (7usize, Scalar::rand(&mut rand::thread_rng())), + (2usize, Scalar::rand(&mut rng)), + (7usize, Scalar::rand(&mut rng)), ]; let (cm, o) = Commitment::multi_index_commit(&value_0, values.iter().copied(), &pp); assert!(cm diff --git a/src/s3id.rs b/src/s3id.rs index 80070e9..68fd83c 100644 --- a/src/s3id.rs +++ b/src/s3id.rs @@ -8,7 +8,7 @@ use crate::{ atact::{self, AtACTError, Token}, bls381_helpers::{ gs::{CProof, CRS, PPE}, - hash_with_domain_separation, pairing_product, Scalar, G1G2, + hash_with_domain_separation, pairing_product, Gt, Scalar, G1G2, }, pedersen::{ self, get_parameters, Commitment, MultiBasePublicParameters, Opening, ProofMultiIndex, @@ -213,6 +213,21 @@ pub struct Proof { gs_pi_1: CProof, } +fn setup_ppe(target: Gt) -> PPE { + let pp2 = get_parameters(); + + let a_consts = vec![pp2.g.0.into()]; + let b_consts = vec![pp2.g.1.into()]; + let gamma = vec![vec![Scalar::zero()]]; + + PPE { + a_consts, + b_consts, + gamma, + target, + } +} + pub fn appcred( attributes: &[Scalar], signatures: &[Signature], @@ -249,20 +264,12 @@ pub fn appcred( let g1_1_vars = vec![zeta.0 .0.into()]; let g2_2_vars = vec![zeta.0 .1.into()]; - let a_consts = vec![pp2.g.0.into()]; - let b_consts = vec![pp2.g.1.into()]; - let gamma = vec![vec![Scalar::zero()]]; let target = pairing_product(&[(&zeta.0, &pp2.g), (&pp2.g, &zeta.0)]); // this is limitation of the GS implementation, we can only do one equation // where both variables in G1 and G2 are used; hence we prove the product of // these two equations to understand the performance characteristics - let equ_1 = PPE { - a_consts, - b_consts, - gamma, - target, - }; + let equ_1 = setup_ppe(target); let gs_pi_1 = equ_1.commit_and_prove(&g1_1_vars, &g2_2_vars, &pp.crs, &mut rng); let pi = tau.proof_multi_index_commit( @@ -303,20 +310,11 @@ pub fn verifycred( .sum(); let pk = &pp.atact_pp.pk; let tau = &cred.tau; - let pp2 = get_parameters(); let check = h + &tau.0; - let a_consts = vec![pp2.g.0.into()]; - let b_consts = vec![pp2.g.1.into()]; - let gamma = vec![vec![Scalar::zero()]]; let target = pairing_product(&[(&check, &pk.0), (&pk.0, &check)]); - let equ_1 = PPE { - a_consts, - b_consts, - gamma, - target, - }; + let equ_1 = setup_ppe(target); if equ_1.verify(&pi.gs_pi_1, &pp.crs) { Ok(()) } else {