11use std:: iter:: zip;
2-
32use ark_ec:: { bn:: BnConfig , short_weierstrass:: SWCurveConfig } ;
4- use ark_ff:: { AdditiveGroup , Field , Fp2Config } ;
3+ use ark_ff:: { AdditiveGroup , Field } ;
54use crate :: { bag:: * , circuits:: bn254:: { fp254impl:: Fp254Impl , fq:: Fq , fq12:: Fq12 , fq2:: Fq2 , utils:: { fq12_from_wires, fq2_from_wires, g1p_from_wires, g2a_from_wires, wires_set_from_fq12, wires_set_from_fq2} } } ;
65
76pub fn double_in_place ( r : & mut ark_bn254:: G2Projective ) -> ( ark_bn254:: Fq2 , ark_bn254:: Fq2 , ark_bn254:: Fq2 ) {
@@ -121,12 +120,12 @@ pub fn add_in_place_circuit(r: Wires, q: Wires) -> Circuit {
121120 let theta = circuit. extend ( Fq2 :: sub ( ry. clone ( ) , wires_1. clone ( ) ) ) ;
122121
123122 let wires_2 = circuit. extend ( Fq2 :: mul ( qx. clone ( ) , rz. clone ( ) ) ) ;
124- let lamda = circuit. extend ( Fq2 :: sub ( rx. clone ( ) , wires_2. clone ( ) ) ) ;
123+ let lambda = circuit. extend ( Fq2 :: sub ( rx. clone ( ) , wires_2. clone ( ) ) ) ;
125124
126125 let c = circuit. extend ( Fq2 :: square ( theta. clone ( ) ) ) ;
127- let d = circuit. extend ( Fq2 :: square ( lamda . clone ( ) ) ) ;
126+ let d = circuit. extend ( Fq2 :: square ( lambda . clone ( ) ) ) ;
128127
129- let e = circuit. extend ( Fq2 :: mul ( lamda . clone ( ) , d. clone ( ) ) ) ;
128+ let e = circuit. extend ( Fq2 :: mul ( lambda . clone ( ) , d. clone ( ) ) ) ;
130129
131130 let f = circuit. extend ( Fq2 :: mul ( rz. clone ( ) , c. clone ( ) ) ) ;
132131
@@ -140,10 +139,10 @@ pub fn add_in_place_circuit(r: Wires, q: Wires) -> Circuit {
140139 let neg_theta = circuit. extend ( Fq2 :: neg ( theta. clone ( ) ) ) ;
141140
142141 let wires_5 = circuit. extend ( Fq2 :: mul ( theta. clone ( ) , qx. clone ( ) ) ) ;
143- let wires_6 = circuit. extend ( Fq2 :: mul ( lamda . clone ( ) , qy. clone ( ) ) ) ;
142+ let wires_6 = circuit. extend ( Fq2 :: mul ( lambda . clone ( ) , qy. clone ( ) ) ) ;
144143 let j = circuit. extend ( Fq2 :: sub ( wires_5. clone ( ) , wires_6. clone ( ) ) ) ;
145144
146- let mut new_r = circuit. extend ( Fq2 :: mul ( lamda . clone ( ) , h. clone ( ) ) ) ;
145+ let mut new_r = circuit. extend ( Fq2 :: mul ( lambda . clone ( ) , h. clone ( ) ) ) ;
147146 let wires_7 = circuit. extend ( Fq2 :: sub ( g. clone ( ) , h. clone ( ) ) ) ;
148147 let wires_8 = circuit. extend ( Fq2 :: mul ( theta. clone ( ) , wires_7. clone ( ) ) ) ;
149148 let wires_9 = circuit. extend ( Fq2 :: mul ( e. clone ( ) , ry. clone ( ) ) ) ;
@@ -152,7 +151,7 @@ pub fn add_in_place_circuit(r: Wires, q: Wires) -> Circuit {
152151 let new_r_z = circuit. extend ( Fq2 :: mul ( rz. clone ( ) , e. clone ( ) ) ) ;
153152 new_r. extend ( new_r_z) ;
154153
155- circuit. add_wires ( lamda ) ;
154+ circuit. add_wires ( lambda ) ;
156155 circuit. add_wires ( neg_theta) ;
157156 circuit. add_wires ( j) ;
158157 circuit. add_wires ( new_r) ;
@@ -176,28 +175,11 @@ pub fn add_in_place_evaluate(r: Wires, q: Wires) -> ((Wires, Wires, Wires), Wire
176175
177176}
178177
179- pub fn frobenius_in_place ( a : ark_bn254:: Fq2 , power : usize ) -> ark_bn254:: Fq2 {
180- let c0 = a. c0 ;
181- let mut c1 = a. c1 ;
182- c1 *= & ark_bn254:: Fq2Config :: FROBENIUS_COEFF_FP2_C1 [ power % 2 ] ;
183- ark_bn254:: Fq2 :: new ( c0, c1)
184- }
185-
186- pub fn frobenius_in_place_circuit ( a : Wires , power : usize ) -> Circuit {
187- let mut circuit = Circuit :: empty ( ) ;
188- let c0 = a[ 0 ..Fq :: N_BITS ] . to_vec ( ) ;
189- let c1 = a[ Fq :: N_BITS ..2 * Fq :: N_BITS ] . to_vec ( ) ;
190- let new_c1 = circuit. extend ( Fq :: mul_by_constant ( c1, ark_bn254:: Fq2Config :: FROBENIUS_COEFF_FP2_C1 [ power % 2 ] ) ) ;
191- circuit. add_wires ( c0) ;
192- circuit. add_wires ( new_c1) ;
193- circuit
194- }
195-
196178pub fn mul_by_char ( r : ark_bn254:: G2Affine ) -> ark_bn254:: G2Affine {
197179 let mut s = r;
198- s. x = frobenius_in_place ( s. x , 1 ) ;
180+ s. x = s. x . frobenius_map ( 1 ) ;
199181 s. x *= & ark_bn254:: Config :: TWIST_MUL_BY_Q_X ;
200- s. y = frobenius_in_place ( s. y , 1 ) ;
182+ s. y = s. y . frobenius_map ( 1 ) ;
201183 s. y *= & ark_bn254:: Config :: TWIST_MUL_BY_Q_Y ;
202184 s
203185}
@@ -207,9 +189,9 @@ pub fn mul_by_char_circuit(r: Wires) -> Circuit {
207189 let r_x = r[ 0 ..Fq2 :: N_BITS ] . to_vec ( ) ;
208190 let r_y = r[ Fq2 :: N_BITS ..2 * Fq2 :: N_BITS ] . to_vec ( ) ;
209191
210- let mut s_x = circuit. extend ( frobenius_in_place_circuit ( r_x, 1 ) ) ;
192+ let mut s_x = circuit. extend ( Fq2 :: frobenius ( r_x, 1 ) ) ;
211193 s_x = circuit. extend ( Fq2 :: mul_by_constant ( s_x, ark_bn254:: Config :: TWIST_MUL_BY_Q_X . clone ( ) ) ) ;
212- let mut s_y = circuit. extend ( frobenius_in_place_circuit ( r_y, 1 ) ) ;
194+ let mut s_y = circuit. extend ( Fq2 :: frobenius ( r_y, 1 ) ) ;
213195 s_y = circuit. extend ( Fq2 :: mul_by_constant ( s_y, ark_bn254:: Config :: TWIST_MUL_BY_Q_Y . clone ( ) ) ) ;
214196 circuit. add_wires ( s_x) ;
215197 circuit. add_wires ( s_y) ;
@@ -404,18 +386,6 @@ pub fn ell_by_constant_circuit_evaluate(f: Wires, coeffs: (ark_bn254::Fq2, ark_b
404386 ( circuit. 0 , n)
405387}
406388
407- pub fn fq12_square_evaluate ( f : Wires ) -> ( Wires , usize ) {
408- let circuit = Fq12 :: square ( f) ;
409-
410- let n = circuit. 1 . len ( ) ;
411-
412- for mut gate in circuit. 1 {
413- gate. evaluate ( ) ;
414- }
415-
416- ( circuit. 0 , n)
417- }
418-
419389pub fn miller_loop ( p : ark_bn254:: G1Projective , q : ark_bn254:: G2Affine ) -> ark_bn254:: Fq12 {
420390 let qell = ell_coeffs ( q) ;
421391 let mut q_ell = qell. iter ( ) ;
@@ -450,7 +420,7 @@ pub fn miller_loop_circuit_evaluate(p: Wires, q: Wires) -> (Wires, usize) {
450420
451421 for i in ( 1 ..ark_bn254:: Config :: ATE_LOOP_COUNT . len ( ) ) . rev ( ) {
452422 if i != ark_bn254:: Config :: ATE_LOOP_COUNT . len ( ) - 1 {
453- let ( new_f, gc) = ( wires_set_from_fq12 ( fq12_from_wires ( f) . square ( ) ) , 70631715 ) ; // fq12_square_evaluate (f);
423+ let ( new_f, gc) = ( wires_set_from_fq12 ( fq12_from_wires ( f) . square ( ) ) , 70631715 ) ; // Fq12::square_evaluate (f);
454424 f = new_f;
455425 gate_count += gc;
456426 }
@@ -551,7 +521,7 @@ pub fn multi_miller_loop_circuit_evaluate(ps: Vec<Wires>, qs: Vec<Wires>) -> (Wi
551521
552522 for i in ( 1 ..ark_bn254:: Config :: ATE_LOOP_COUNT . len ( ) ) . rev ( ) {
553523 if i != ark_bn254:: Config :: ATE_LOOP_COUNT . len ( ) - 1 {
554- let ( new_f, gc) = ( wires_set_from_fq12 ( fq12_from_wires ( f) . square ( ) ) , 70631715 ) ; // fq12_square_evaluate (f);
524+ let ( new_f, gc) = ( wires_set_from_fq12 ( fq12_from_wires ( f) . square ( ) ) , 70631715 ) ; // Fq12::square_evaluate (f);
555525 f = new_f;
556526 gate_count += gc;
557527 }
@@ -603,7 +573,7 @@ pub fn multi_miller_loop_groth16_circuit_evaluate(p1: Wires, p2: Wires, p3: Wire
603573
604574 for i in ( 1 ..ark_bn254:: Config :: ATE_LOOP_COUNT . len ( ) ) . rev ( ) {
605575 if i != ark_bn254:: Config :: ATE_LOOP_COUNT . len ( ) - 1 {
606- let ( new_f, gc) = ( wires_set_from_fq12 ( fq12_from_wires ( f) . square ( ) ) , 70631715 ) ; // fq12_square_evaluate (f);
576+ let ( new_f, gc) = ( wires_set_from_fq12 ( fq12_from_wires ( f) . square ( ) ) , 70631715 ) ; // Fq12::square_evaluate (f);
607577 f = new_f;
608578 gate_count += gc;
609579 }
0 commit comments