22
33use std:: ops:: Mul ;
44
5- use ark_bls12_381:: { Config , Fr , G1Affine , G1Projective } ;
5+ use ark_bls12_381:: { Fr , G1Affine , G1Projective } ;
66use ark_ec:: CurveGroup ;
7- use ark_ec:: short_weierstrass:: Projective ;
8- use ark_ff:: { batch_inversion, BigInt , Field , PrimeField } ;
7+ use ark_ff:: { batch_inversion, Field , PrimeField } ;
98use ark_serialize:: { CanonicalDeserialize , CanonicalSerialize , Read , SerializationError , Write } ;
109use ark_std:: rand:: RngCore ;
1110use ark_std:: { One , UniformRand , Zero } ;
@@ -365,16 +364,18 @@ impl InnerProductProof {
365364}
366365
367366impl WeightedInnerProductProof {
368- /// Create an inner product proof
367+ /// Create a weighted inner product proof
369368 ///
370369 /// # Arguments
371370 ///
372371 /// * `crs_G_vec` - $\bm{G}$ CRS vector
373- /// * `crs_G_prime_vec` - $\bm{G'}$ CRS blinder vector
372+ /// * `crs_H_vec` - $\bm{G'}$ CRS blinder vector
373+ /// * `crs_G` - $G$ CRS element
374374 /// * `crs_H` - $H$ CRS element
375- /// * `C` - commitment to `vec_c`
376- /// * `D` - commitment to `vec_d`
375+ /// * `P` - commitment to `vec_c`, `vec_d` and `z`
377376 /// * `z` - inner product result
377+ /// * `y` - weight scalar
378+ /// * `alpha` scalar
378379 /// * `vec_c` - first inner product vector (*witness*)
379380 /// * `vec_d` - second inner product vector (*witness*)
380381 #[ allow( clippy:: too_many_arguments) ]
@@ -389,9 +390,9 @@ impl WeightedInnerProductProof {
389390
390391 mut vec_c : Vec < Fr > ,
391392 mut vec_d : Vec < Fr > ,
392- y : Fr ,
393- alpha : Fr ,
394-
393+ y : Fr ,
394+ mut alpha : Fr ,
395+
395396 transcript : & mut Transcript ,
396397 rng : & mut T ,
397398 ) -> WeightedInnerProductProof {
@@ -444,6 +445,10 @@ impl WeightedInnerProductProof {
444445 let mut slice_H = & mut crs_H_vec[ ..] ;
445446 let mut slice_c = & mut vec_c[ ..] ;
446447 let mut slice_d = & mut vec_d[ ..] ;
448+ let mut c_hat = vec ! [ Fr :: zero( ) ; if n==1 { n} else { n/2 } ] ;
449+ let mut d_hat = vec ! [ Fr :: zero( ) ; if n==1 { n} else { n/2 } ] ;
450+ let mut vec_G_hat_affine = vec ! [ G1Affine :: identity( ) ; if n==1 { n} else { n/2 } ] ;
451+ let mut vec_H_hat_affine = vec ! [ G1Affine :: identity( ) ; if n==1 { n} else { n/2 } ] ;
447452
448453 while slice_c. len ( ) > 1 {
449454 n /= 2 ;
@@ -478,10 +483,10 @@ impl WeightedInnerProductProof {
478483 // Note that no element in vectors c_L and d_R can be 0
479484 // since 0 is an invalid secret key!
480485 // L = <yninv_cL * G_R> + <d_R * H_L> + (z_L * g) + (x_L * h)
481- let g_zL = * crs_G * z_L;
486+ let g_zL: G1Projective = * crs_G * z_L;
482487 let x_L_Fr = Fr :: rand ( rng) ;
483- let h_x_L = * crs_H * x_L_Fr;
484- let g_zL_h_xL = g_zL + & h_x_L;
488+ let h_x_L: G1Projective = * crs_H * x_L_Fr;
489+ let g_zL_h_xL: G1Projective = g_zL + h_x_L;
485490 let yninv_cL_GR = G_R . iter ( ) . zip ( yninv_cL) . fold ( g_zL_h_xL, |acc, x| {
486491 if x. 1 != Fr :: zero ( ) {
487492 let cLi = x. 1 ;
@@ -491,10 +496,10 @@ impl WeightedInnerProductProof {
491496 acc
492497 }
493498 } ) ;
494- let L = H_L . iter ( ) . zip ( d_R) . fold ( yninv_cL_GR, |acc, x| {
495- if * x. 1 != Fr :: zero ( ) {
496- let dRi = * x. 1 ;
497- let dRi_HLi = * x. 0 * & dRi;
499+ let L = H_L . iter ( ) . zip ( & mut * d_R) . fold ( yninv_cL_GR, |acc, x| {
500+ if x. 1 != & Fr :: zero ( ) {
501+ let dRi = x. 1 ;
502+ let dRi_HLi = * x. 0 * dRi;
498503 acc + & dRi_HLi
499504 } else {
500505 acc
@@ -506,10 +511,10 @@ impl WeightedInnerProductProof {
506511 // since 0 is an invalid secret key!
507512 //
508513 // R = <yn_c_R * G_R> + <d_L * H_R> + (z_R * g) + (x_R * h)
509- let g_zR = * crs_G * z_R;
514+ let g_zR: G1Projective = * crs_G * z_R;
510515 let x_R_Fr = Fr :: rand ( rng) ;
511- let h_x_R = * crs_H * x_R_Fr;
512- let g_zR_h_xR = g_zR + & h_x_R;
516+ let h_x_R: G1Projective = * crs_H * x_R_Fr;
517+ let g_zR_h_xR: G1Projective = g_zR + h_x_R;
513518 let cR_GL = G_L . iter ( ) . zip ( yn_c_R. clone ( ) ) . fold ( g_zR_h_xR, |acc, x| {
514519 if x. 1 != Fr :: zero ( ) {
515520 let cRi = x. 1 ;
@@ -519,11 +524,11 @@ impl WeightedInnerProductProof {
519524 acc
520525 }
521526 } ) ;
522- let R = H_R . iter ( ) . zip ( d_L) . fold ( cR_GL, |acc, x| {
523- if * x. 1 != Fr :: zero ( ) {
524- let cRi = * x. 1 ;
525- let cRi_GLi = * x. 0 * & cRi ;
526- acc + & cRi_GLi
527+ let R = H_R . iter ( ) . zip ( & mut * d_L) . fold ( cR_GL, |acc, x| {
528+ if x. 1 != & Fr :: zero ( ) {
529+ let dLi = x. 1 ;
530+ let dLi_HRi = * x. 0 * dLi ;
531+ acc + & dLi_HRi
527532 } else {
528533 acc
529534 }
@@ -537,60 +542,67 @@ impl WeightedInnerProductProof {
537542 let e = transcript. get_and_append_challenge ( b"ipa_e" ) ;
538543 let e_inv = e. inverse ( ) . expect ( "e must have an inverse" ) ;
539544
540- let e_squared = e * e;
541- let e_inv_squared = e_squared. inverse ( ) . expect ( "e_squared must have an inverse" ) ;
545+ let e_squared = & e * & e;
546+ let e_inv_squared = & e_squared. inverse ( ) . expect ( "e_squared must have an inverse" ) ;
542547
543548 // Fold input vectors and basis
544549 // We make c_hat
545- let c_hat = ( 0 ..n)
550+ c_hat = ( 0 ..n)
546551 . map ( |i| {
547- let cLe = c_L[ i] * e;
548- let cR_minuse = yn_c_R[ i] * e_inv;
549- cLe + cR_minuse
552+ let cLe = & c_L[ i] * & e;
553+ let cR_minuse = & yn_c_R[ i] * & e_inv;
554+ & cLe + & cR_minuse
550555 } )
551556 . collect :: < Vec < Fr > > ( ) ;
552557 // c = &mut c_hat[..];
553558
554559 // We make c_hat
555- let d_hat = ( 0 ..n)
560+ d_hat = ( 0 ..n)
556561 . map ( |i| {
557- let dRe = d_R[ i] * e;
558- let dL_minuse = d_L[ i] * e_inv;
559- dRe + dL_minuse
562+ let dRe = & d_R[ i] * & e;
563+ let dL_minuse = & d_L[ i] * & e_inv;
564+ & dRe + & dL_minuse
560565 } )
561566 . collect :: < Vec < Fr > > ( ) ;
562567 // d = &mut d_hat[..];
563568
564569 // Now we make alpha_hat
565- let e2_xL = e_squared * x_L_Fr;
566- let einv2_xR = e_inv_squared * x_R_Fr;
567- let e2_xL_einv2_xR = e2_xL + einv2_xR;
568- let alpha_hat = alpha + e2_xL_einv2_xR;
570+ let e2_xL = & e_squared * & x_L_Fr;
571+ let einv2_xR = e_inv_squared * & x_R_Fr;
572+ let e2_xL_einv2_xR = & e2_xL + & einv2_xR;
573+ alpha = alpha + e2_xL_einv2_xR; // TODO: Send Alpha to next recusive step
569574
570575 // Now we make G_hat
571576 let e_yinv = e * powers_y_inv[ n-1 ] ;
572- let G_hat = ( 0 ..n)
577+ let mut G_hat = ( 0 ..n)
573578 . map ( |i| {
574- let GLe_inv = G_L [ i] * e_inv;
575- let GRe_yinv = G_R [ i] * e_yinv;
576- * GRe_yinv + * GLe_inv
579+ let GLe_inv : G1Projective = G_L [ i] * e_inv;
580+ let GRe_yinv : G1Projective = G_R [ i] * e_yinv;
581+ GRe_yinv + GLe_inv
577582 } )
578- . collect :: < Vec < G1Affine > > ( ) ;
583+ . collect :: < Vec < G1Projective > > ( ) ;
579584 // G = &mut G_hat[..];
580585
581- let H_hat = ( 0 ..n)
586+ let mut H_hat = ( 0 ..n)
582587 . map ( |i| {
583- let HLe = H_L [ i] * e;
584- let HRe_inv = H_R [ i] * e_inv;
585- * HLe + * HRe_inv
588+ let HLe : G1Projective = H_L [ i] * e;
589+ let HRe_inv : G1Projective = H_R [ i] * e_inv;
590+ HLe + HRe_inv
586591 } )
587- . collect :: < Vec < G1Affine > > ( ) ;
592+ . collect :: < Vec < G1Projective > > ( ) ;
593+
588594
595+ vec_G_hat_affine = G_hat . iter ( )
596+ . map ( |x| x. into_affine ( ) )
597+ . collect :: < Vec < G1Affine > > ( ) ;
598+ vec_H_hat_affine = H_hat . iter ( )
599+ . map ( |x| x. into_affine ( ) )
600+ . collect :: < Vec < G1Affine > > ( ) ;
589601 // Save the rescaled vector for splitting in the next loop
590- slice_c = & mut c_hat. as_slice ( ) ;
591- slice_d = & mut d_hat. as_slice ( ) ;
592- slice_G = & mut G_hat . as_slice ( ) ;
593- slice_H = & mut H_hat . as_slice ( ) ;
602+ slice_c = c_hat. as_mut_slice ( ) ;
603+ slice_d = d_hat. as_mut_slice ( ) ;
604+ slice_G = vec_G_hat_affine . as_mut_slice ( ) ;
605+ slice_H = vec_H_hat_affine . as_mut_slice ( ) ;
594606 }
595607
596608 // n should now be equal to 1, and every vector should therefore have length 1
@@ -654,7 +666,7 @@ impl WeightedInnerProductProof {
654666 n : usize ,
655667 transcript : & mut Transcript ,
656668 ) -> Result < ( Vec < Fr > , Vec < Fr > , Vec < Fr > , Vec < Fr > ) , ProofError > {
657- let lg_n = self . vec_c_L . len ( ) ;
669+ let lg_n = self . vec_L . len ( ) ;
658670 if lg_n >= 32 {
659671 return Err ( ProofError :: VerificationError ) ;
660672 }
@@ -666,14 +678,12 @@ impl WeightedInnerProductProof {
666678
667679 // 1. Recompute gamma_k,...,gamma_1 based on the proof transcript
668680 let mut challenges: Vec < Fr > = Vec :: with_capacity ( lg_n) ;
669- for i in 0 ..self . vec_c_L . len ( ) {
681+ for i in 0 ..self . vec_L . len ( ) {
670682 transcript. append_list (
671683 b"ipa_loop" ,
672684 & [
673- & self . vec_c_L [ i] ,
674- & self . vec_d_L [ i] ,
675- & self . vec_c_R [ i] ,
676- & self . vec_d_R [ i] ,
685+ & self . vec_L [ i] ,
686+ & self . vec_R [ i] ,
677687 ] ,
678688 ) ;
679689 challenges. push ( transcript. get_and_append_challenge ( b"ipa_gamma" ) ) ;
@@ -699,7 +709,7 @@ impl WeightedInnerProductProof {
699709 Ok ( ( challenges, challenges_inv, vec_s, vec_inv_s) )
700710 }
701711
702- /// Verify an inner product proof
712+ /* /// Verify an inner product proof
703713 ///
704714 /// # Arguments
705715 ///
@@ -798,7 +808,7 @@ impl WeightedInnerProductProof {
798808 c_final: Fr::deserialize_compressed(&mut r)?,
799809 d_final: Fr::deserialize_compressed(&mut r)?,
800810 })
801- }
811+ }*/
802812}
803813
804814
@@ -808,7 +818,8 @@ mod tests {
808818 use ark_std:: rand:: { rngs:: StdRng , Rng , SeedableRng } ;
809819 use ark_std:: UniformRand ;
810820 use core:: iter;
811-
821+ use sha2:: digest:: Update ;
822+ use sha2:: Sha256 ;
812823 use crate :: msm_accumulator:: MsmAccumulator ;
813824
814825 #[ test]
@@ -917,26 +928,32 @@ mod tests {
917928 . map ( |( G_i , u_i) | G_i . mul ( * u_i) . into_affine ( ) )
918929 . collect ( ) ;
919930 let crs_H = G1Projective :: rand ( & mut rng) ;
931+ let crs_G = G1Projective :: rand ( & mut rng) ;
920932
921933 // Generate some random vectors
922- let vec_b: Vec < Fr > = iter:: repeat_with ( || rng. gen ( ) ) . take ( n) . collect ( ) ;
923934 let vec_c: Vec < Fr > = iter:: repeat_with ( || rng. gen ( ) ) . take ( n) . collect ( ) ;
935+ let vec_d: Vec < Fr > = iter:: repeat_with ( || rng. gen ( ) ) . take ( n) . collect ( ) ;
924936
925- let z = inner_product ( & vec_b , & vec_c ) ;
937+ let z = inner_product ( & vec_c , & vec_d ) ;
926938
927939 // Create commitments
928- let B = msm ( & crs_G_vec, & vec_b) ;
929- let C = msm ( & crs_G_prime_vec, & vec_c) ;
940+ let P = msm ( & crs_G_prime_vec, & vec_c) ;
930941
931- let proof = InnerProductProof :: new (
942+ let y_scalar = Fr :: rand ( & mut rng) ;
943+
944+ let alpha = Fr :: rand ( & mut rng) ;
945+
946+ let proof = WeightedInnerProductProof :: new (
932947 crs_G_vec. clone ( ) ,
933948 crs_G_prime_vec. clone ( ) ,
934949 & crs_H,
935- B . clone ( ) ,
936- C . clone ( ) ,
950+ & crs_G ,
951+ P . clone ( ) ,
937952 z,
938- vec_b. clone ( ) ,
939953 vec_c. clone ( ) ,
954+ vec_d. clone ( ) ,
955+ y_scalar,
956+ alpha,
940957 & mut transcript_prover,
941958 & mut rng,
942959 ) ;
@@ -945,7 +962,7 @@ mod tests {
945962 let mut transcript_verifier = merlin:: Transcript :: new ( b"IPA" ) ;
946963 let mut msm_accumulator = MsmAccumulator :: new ( ) ;
947964
948- assert ! ( proof
965+ /* assert!(proof
949966 .verify(
950967 &crs_G_vec,
951968 &crs_H,
@@ -957,7 +974,7 @@ mod tests {
957974 &mut msm_accumulator,
958975 &mut rng,
959976 )
960- . is_ok( ) ) ;
977+ .is_ok());*/
961978
962979 assert ! ( msm_accumulator. verify( ) . is_ok( ) ) ;
963980
@@ -966,7 +983,7 @@ mod tests {
966983 let mut transcript_verifier = merlin:: Transcript :: new ( b"IPA" ) ;
967984 let mut msm_accumulator = MsmAccumulator :: new ( ) ;
968985
969- assert ! ( proof
986+ /* assert!(proof
970987 .verify(
971988 &crs_G_vec,
972989 &crs_H,
@@ -978,7 +995,7 @@ mod tests {
978995 &mut msm_accumulator,
979996 &mut rng,
980997 )
981- . is_ok( ) ) ;
998+ .is_ok());*/
982999
9831000 assert ! ( msm_accumulator. verify( ) . is_err( ) ) ;
9841001 }
0 commit comments