Skip to content

Commit f8f4695

Browse files
committed
optimization on fq::half and fq::div6
1 parent d631ffa commit f8f4695

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

src/circuits/bn254/fp254impl.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use std::{str::FromStr};
1+
use std::str::FromStr;
22
use ark_ff::{AdditiveGroup, Field};
33
use num_bigint::BigUint;
4-
use crate::{bag::*, circuits::{basic::selector, bigint::{utils::bits_from_biguint, U254}, bn254::{utils::{bits_from_fq, wires_for_fq, wires_set_from_fq}}}};
4+
use crate::{bag::*, circuits::{basic::selector, bigint::{utils::bits_from_biguint, U254}, bn254::utils::{bits_from_fq, wires_for_fq, wires_set_from_fq}}};
55

66
pub trait Fp254Impl {
77
const MODULUS: &'static str;
@@ -27,6 +27,10 @@ pub trait Fp254Impl {
2727

2828
fn half_modulus() -> BigUint;
2929

30+
fn one_third_modulus() -> BigUint;
31+
32+
fn two_third_modulus() -> BigUint;
33+
3034
fn self_or_zero(a: Wires, s: Wirex) -> Circuit {
3135
U254::self_or_zero(a, s)
3236
}
@@ -148,7 +152,7 @@ pub trait Fp254Impl {
148152

149153
let selector = a[0].clone();
150154
let wires_1 = circuit.extend(U254::half(a.clone()));
151-
let wires_2 = circuit.extend(Self::add_constant(wires_1.clone(), ark_bn254::Fq::from( ark_bn254::Fq::from(1))/ ark_bn254::Fq::from(2) ));
155+
let wires_2 = circuit.extend(U254::add_constant_without_carry(wires_1.clone(), Self::half_modulus() ));
152156
let result = circuit.extend(U254::select(wires_2, wires_1, selector));
153157
circuit.add_wires(result);
154158
circuit
@@ -410,10 +414,10 @@ pub trait Fp254Impl {
410414
r1 = circuit.extend(selector(not_r1.clone(), r1.clone(), edge_case))[0].clone();
411415
};
412416
// residue for r2
413-
let result_plus_one_third = circuit.extend(Self::add_constant(result.clone(), ark_bn254::Fq::from(1) / ark_bn254::Fq::from(3)));
417+
let result_plus_one_third = circuit.extend(U254::add_constant_without_carry(result.clone(), Self::one_third_modulus()));
414418
result = circuit.extend(U254::select(result_plus_one_third, result.clone(), r2.clone()));
415419
// residue for r1
416-
let result_plus_two_third = circuit.extend(Self::add_constant(result.clone(), ark_bn254::Fq::from(2) / ark_bn254::Fq::from(3)));
420+
let result_plus_two_third = circuit.extend(U254::add_constant_without_carry(result.clone(), Self::two_third_modulus()));
417421
result = circuit.extend(U254::select(result_plus_two_third, result.clone(), r1.clone()));
418422
circuit.add_wires(result.clone());
419423
circuit

src/circuits/bn254/fq.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ impl Fp254Impl for Fq {
1111
fn half_modulus() -> BigUint {
1212
BigUint::from(ark_bn254::Fq::from(1) / ark_bn254::Fq::from(2))
1313
}
14+
15+
fn one_third_modulus() -> BigUint {
16+
BigUint::from(ark_bn254::Fq::from(1) / ark_bn254::Fq::from(3))
17+
}
18+
fn two_third_modulus() -> BigUint {
19+
BigUint::from(ark_bn254::Fq::from(2) / ark_bn254::Fq::from(3))
20+
}
1421
}
1522

1623
#[cfg(test)]

src/circuits/bn254/fr.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,11 @@ impl Fp254Impl for Fr {
1111
fn half_modulus() -> BigUint {
1212
BigUint::from(ark_bn254::Fr::from(1) / ark_bn254::Fr::from(2))
1313
}
14+
15+
fn one_third_modulus() -> BigUint {
16+
BigUint::from(ark_bn254::Fr::from(1) / ark_bn254::Fr::from(3))
17+
}
18+
fn two_third_modulus() -> BigUint {
19+
BigUint::from(ark_bn254::Fr::from(2) / ark_bn254::Fr::from(3))
20+
}
1421
}

src/core/circuit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl Circuit {
4141
// for gate_type in ["and", "nand", "or", "xor", "xnor", "not"] {
4242
// println!("{:?}\t: {:?}", gate_type, self.1.iter().filter(|gate| gate.name == gate_type).count());
4343
// }
44-
// println!("total gate count: {:?}", self.gate_count());
44+
println!("total gate count: {:?}", self.gate_count());
4545
}
4646
}
4747

0 commit comments

Comments
 (0)