Skip to content

Commit 85d2dbf

Browse files
committed
small changes
1 parent 4e546f1 commit 85d2dbf

File tree

7 files changed

+129
-77
lines changed

7 files changed

+129
-77
lines changed

src/circuits/bn254/fp254impl.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@ pub trait Fp254Impl {
230230
assert_eq!(a.len(), Self::N_BITS);
231231
let mut circuit = Circuit::empty();
232232

233+
if b == ark_bn254::Fq::ZERO {
234+
circuit.add_wires(wires_set_from_fq(ark_bn254::Fq::ZERO));
235+
return circuit;
236+
}
237+
233238
if b == ark_bn254::Fq::ONE {
234239
circuit.add_wires(a);
235240
return circuit;

src/circuits/bn254/fq.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use num_bigint::BigUint;
2-
32
use crate::circuits::bn254::fp254impl::Fp254Impl;
43

54
pub struct Fq;
@@ -20,6 +19,7 @@ impl Fp254Impl for Fq {
2019
fn one_third_modulus() -> BigUint {
2120
BigUint::from(ark_bn254::Fq::from(1) / ark_bn254::Fq::from(3))
2221
}
22+
2323
fn two_third_modulus() -> BigUint {
2424
BigUint::from(ark_bn254::Fq::from(2) / ark_bn254::Fq::from(3))
2525
}
@@ -28,7 +28,7 @@ impl Fp254Impl for Fq {
2828
#[cfg(test)]
2929
mod tests {
3030
use super::*;
31-
use crate::circuits::{bigint::utils::biguint_from_wires, bn254::utils::{fq_from_wires, random_fq, wires_set_from_fq}};
31+
use crate::circuits::bn254::utils::{fq_from_wires, random_fq, wires_set_from_fq};
3232
use ark_ff::Field;
3333

3434
#[test]
@@ -197,9 +197,9 @@ mod tests {
197197
for mut gate in circuit.1 {
198198
gate.evaluate();
199199
}
200-
let c = biguint_from_wires(circuit.0);
200+
let c = fq_from_wires(circuit.0);
201201
//println!("c = {}", c);
202-
assert_eq!(ark_bn254::Fq::from(c), a * b * r);
202+
assert_eq!(c, a * b * r);
203203
}
204204
}
205205
}

src/circuits/bn254/fq12.rs

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::iter::zip;
2-
32
use crate::{bag::*, circuits::bn254::{fp254impl::Fp254Impl, fq::Fq, fq2::Fq2, fq6::Fq6}};
43
use ark_ff::{Field, Fp12Config};
54

@@ -45,6 +44,18 @@ impl Fq12 {
4544
circuit
4645
}
4746

47+
pub fn equal_constant_evaluate(a: Wires, b: ark_bn254::Fq12) -> (Wires, usize) {
48+
let circuit = Fq12::equal_constant(a, b);
49+
50+
let n = circuit.1.len();
51+
52+
for mut gate in circuit.1 {
53+
gate.evaluate();
54+
}
55+
56+
(circuit.0, n)
57+
}
58+
4859
pub fn add(a: Wires, b: Wires) -> Circuit {
4960
assert_eq!(a.len(), Self::N_BITS);
5061
assert_eq!(b.len(), Self::N_BITS);
@@ -131,6 +142,18 @@ impl Fq12 {
131142
circuit
132143
}
133144

145+
pub fn mul_evaluate(a: Wires, b: Wires) -> (Wires, usize) {
146+
let circuit = Fq12::mul(a, b);
147+
148+
let n = circuit.1.len();
149+
150+
for mut gate in circuit.1 {
151+
gate.evaluate();
152+
}
153+
154+
(circuit.0, n)
155+
}
156+
134157
pub fn mul_by_constant(a: Wires, b: ark_bn254::Fq12) -> Circuit {
135158
assert_eq!(a.len(), Self::N_BITS);
136159
let mut circuit = Circuit::empty();
@@ -240,6 +263,18 @@ impl Fq12 {
240263
circuit
241264
}
242265

266+
pub fn square_evaluate(a: Wires) -> (Wires, usize) {
267+
let circuit = Fq12::square(a);
268+
269+
let n = circuit.1.len();
270+
271+
for mut gate in circuit.1 {
272+
gate.evaluate();
273+
}
274+
275+
(circuit.0, n)
276+
}
277+
243278
pub fn frobenius(a: Wires, i: usize) -> Circuit {
244279
assert_eq!(a.len(), Self::N_BITS);
245280
let mut circuit = Circuit::empty();
@@ -255,6 +290,44 @@ impl Fq12 {
255290
circuit.0.extend(result);
256291
circuit
257292
}
293+
294+
pub fn frobenius_evaluate(a: Wires, i: usize) -> (Wires, usize) {
295+
let circuit = Fq12::frobenius(a, i);
296+
297+
let n = circuit.1.len();
298+
299+
for mut gate in circuit.1 {
300+
gate.evaluate();
301+
}
302+
303+
(circuit.0, n)
304+
}
305+
306+
pub fn conjugate(a: Wires) -> Circuit {
307+
assert_eq!(a.len(), Self::N_BITS);
308+
let mut circuit = Circuit::empty();
309+
310+
let a_c0 = a[0..Fq6::N_BITS].to_vec();
311+
let a_c1 = a[Fq6::N_BITS..2*Fq6::N_BITS].to_vec();
312+
313+
let new_a_c1 = circuit.extend(Fq6::neg(a_c1));
314+
315+
circuit.0.extend(a_c0);
316+
circuit.0.extend(new_a_c1);
317+
circuit
318+
}
319+
320+
pub fn conjugate_evaluate(a: Wires) -> (Wires, usize) {
321+
let circuit = Fq12::conjugate(a);
322+
323+
let n = circuit.1.len();
324+
325+
for mut gate in circuit.1 {
326+
gate.evaluate();
327+
}
328+
329+
(circuit.0, n)
330+
}
258331
}
259332

260333
#[cfg(test)]
@@ -431,5 +504,36 @@ mod tests {
431504
}
432505
let c = fq12_from_wires(circuit.0);
433506
assert_eq!(c, a.frobenius_map(1));
507+
508+
let circuit = Fq12::frobenius(wires_set_from_fq12(a.clone()), 2);
509+
circuit.print_gate_type_counts();
510+
for mut gate in circuit.1 {
511+
gate.evaluate();
512+
}
513+
let c = fq12_from_wires(circuit.0);
514+
assert_eq!(c, a.frobenius_map(2));
515+
516+
let circuit = Fq12::frobenius(wires_set_from_fq12(a.clone()), 3);
517+
circuit.print_gate_type_counts();
518+
for mut gate in circuit.1 {
519+
gate.evaluate();
520+
}
521+
let c = fq12_from_wires(circuit.0);
522+
assert_eq!(c, a.frobenius_map(3));
523+
}
524+
525+
#[test]
526+
fn test_fq12_conjugate() {
527+
let a = random_fq12();
528+
529+
let circuit = Fq12::conjugate(wires_set_from_fq12(a.clone()));
530+
circuit.print_gate_type_counts();
531+
for mut gate in circuit.1 {
532+
gate.evaluate();
533+
}
534+
let mut b = a.clone();
535+
b.conjugate_in_place();
536+
let c = fq12_from_wires(circuit.0);
537+
assert_eq!(c, b);
434538
}
435539
}

src/circuits/bn254/fq2.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use ark_ff::{Field, Fp2Config};
2-
32
use crate::{bag::*, circuits::bn254::{fp254impl::Fp254Impl, fq::Fq}};
43

54
pub struct Fq2;

src/circuits/bn254/fq6.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,12 +405,10 @@ impl Fq6 {
405405
}
406406
}
407407

408-
409408
#[cfg(test)]
410409
mod tests {
411410
use ark_ff::{Field, Fp12Config};
412411
use serial_test::serial;
413-
414412
use crate::circuits::bn254::utils::{ fq6_from_wires, random_fq2, random_fq6, wires_set_from_fq2, wires_set_from_fq6};
415413
use super::*;
416414

src/circuits/bn254/pairing.rs

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::iter::zip;
2-
32
use ark_ec::{bn::BnConfig, short_weierstrass::SWCurveConfig};
4-
use ark_ff::{AdditiveGroup, Field, Fp2Config};
3+
use ark_ff::{AdditiveGroup, Field};
54
use 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

76
pub 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-
196178
pub 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-
419389
pub 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
}

src/circuits/groth16.rs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,6 @@ use crate::circuits::bn254::g1::G1Projective;
77
use crate::circuits::bn254::pairing::multi_miller_loop_groth16_circuit_evaluate;
88
use crate::circuits::bn254::utils::{fq12_from_wires, fr_from_wires, wires_set_from_fq12, wires_set_from_g1p};
99

10-
pub fn fq12_mul_evaluate(a: Wires, b: Wires) -> (Wires, usize) {
11-
let circuit = Fq12::mul(a, b);
12-
13-
let n = circuit.1.len();
14-
15-
for mut gate in circuit.1 {
16-
gate.evaluate();
17-
}
18-
19-
(circuit.0, n)
20-
}
21-
22-
pub fn fq12_equal_constant_evaluate(a: Wires, b: ark_bn254::Fq12) -> (Wires, usize) {
23-
let circuit = Fq12::equal_constant(a, b);
24-
25-
let n = circuit.1.len();
26-
27-
for mut gate in circuit.1 {
28-
gate.evaluate();
29-
}
30-
31-
(circuit.0, n)
32-
}
33-
3410
pub fn groth16_verifier(public: Vec<ark_bn254::Fr>, proof: ark_groth16::Proof<ark_bn254::Bn254>, vk: ark_groth16::VerifyingKey<ark_bn254::Bn254>) -> bool {
3511
let scalars = [
3612
vec![ark_bn254::Fr::ONE],
@@ -58,7 +34,7 @@ pub fn groth16_verifier_circuit(public: Wires, proof_a: Wires, proof_b: Wires, p
5834
let alpha_beta = ark_bn254::Bn254::final_exponentiation(ark_bn254::Bn254::multi_miller_loop([vk.alpha_g1.into_group()], [-vk.beta_g2])).unwrap().0.inverse().unwrap();
5935
let f = wires_set_from_fq12(ark_bn254::Bn254::final_exponentiation(MillerLoopOutput(fq12_from_wires(f))).unwrap().0);
6036

61-
let (result, gc) = fq12_equal_constant_evaluate(f, alpha_beta);
37+
let (result, gc) = Fq12::equal_constant_evaluate(f, alpha_beta);
6238
gate_count += gc;
6339
(result[0].clone(), gate_count)
6440
}

0 commit comments

Comments
 (0)