Skip to content

Commit 9d8db07

Browse files
committed
Merge branch 'zelic/1_missing_input_validation_on_user_inputs' into zellic/4_2_remediation_strategy_for_missing_input_validation
2 parents 0834f30 + aae3e77 commit 9d8db07

File tree

8 files changed

+1428
-444
lines changed

8 files changed

+1428
-444
lines changed

g16ckt/examples/pairing_gate_counts.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ fn main() {
211211
move || {
212212
run_and_print("test_ell_montgomery", inputs, move |ctx, w| {
213213
let f0 = fq12_one_const();
214-
let coeffs = pairing::ell_coeffs_montgomery(ctx, &w.g2);
214+
// ignoring _is_valid because this function is used only for benchmarking over valid inputs
215+
let (coeffs, _is_valid) = pairing::ell_coeffs_montgomery(ctx, &w.g2);
215216
// Take first coeff triple and evaluate once
216217
let c = coeffs.into_iter().next().unwrap();
217218
let _f1 = pairing::ell_montgomery(ctx, &f0, &c, &w.g1);

g16ckt/src/gadgets/bn254/fq.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,14 +297,24 @@ impl Fq {
297297
&BigUint::from_str(Self::MODULUS_ADD_1_DIV_4).unwrap(),
298298
)
299299
}
300+
301+
/// Return a>b in standard form given inputs in montgomery form
302+
pub fn greater_than<C: CircuitContext>(circuit: &mut C, a: &Fq, b: &Fq) -> WireId {
303+
// First convert the inputs 'a' and 'b' back to standard form
304+
let a = Fq::mul_by_constant_montgomery(circuit, a, &ark_bn254::Fq::ONE);
305+
let b = Fq::mul_by_constant_montgomery(circuit, b, &ark_bn254::Fq::ONE);
306+
// only now perform comparison
307+
bigint::greater_than(circuit, &a, &b)
308+
}
300309
}
301310

302311
#[cfg(test)]
303312
pub(super) mod tests {
304313
use std::{array, iter};
305314

306315
use ark_ff::AdditiveGroup;
307-
use rand::Rng;
316+
use rand::{Rng, SeedableRng};
317+
use rand_chacha::ChaCha20Rng;
308318
use test_log::test;
309319
use tracing::trace;
310320

@@ -680,6 +690,29 @@ pub(super) mod tests {
680690
assert_eq!(result.output_value.value, expected_c);
681691
}
682692

693+
#[test]
694+
fn test_fq_sqrt_montgomery_roundtrip() {
695+
let mut rng = ChaCha20Rng::seed_from_u64(42);
696+
for _ in 0..5 {
697+
let aa_v = Fq::random(&mut rng);
698+
let sqrt_exists = aa_v.sqrt().is_some();
699+
700+
let aa_montgomery = Fq::as_montgomery(aa_v);
701+
let input = FqInput::new([aa_montgomery]);
702+
703+
let result =
704+
CircuitBuilder::streaming_execute::<_, _, FqOutput>(input, 10_000, |ctx, input| {
705+
let [aa_wire] = input;
706+
let sqrt = Fq::sqrt_montgomery(ctx, aa_wire);
707+
Fq::square_montgomery(ctx, &sqrt)
708+
});
709+
710+
let calc_aa_montgomery = result.output_value.value;
711+
712+
assert_eq!(sqrt_exists, calc_aa_montgomery == aa_montgomery);
713+
}
714+
}
715+
683716
#[test]
684717
fn test_fq_multiplexer() {
685718
let w = 1;

g16ckt/src/gadgets/bn254/fq2.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ use crate::{
1515
CircuitContext, Gate, WireId,
1616
circuit::WiresObject,
1717
gadgets::{
18-
bigint::{BigIntWires, select},
18+
basic,
19+
bigint::{self, BigIntWires, select},
1920
bn254::{fp254impl::Fp254Impl, fq::Fq},
2021
},
2122
};
@@ -444,6 +445,14 @@ impl Fq2 {
444445

445446
Fq2::from_components(c0_final, c1_final)
446447
}
448+
449+
/// Return a>b in standard form given inputs in montgomery form
450+
pub fn greater_than<C: CircuitContext>(circuit: &mut C, a: &Fq2, b: &Fq2) -> WireId {
451+
let c1_equal = bigint::equal(circuit, a.c1(), b.c1());
452+
let c1_greater = Fq::greater_than(circuit, a.c1(), b.c1());
453+
let c0_greater = Fq::greater_than(circuit, a.c0(), b.c0());
454+
basic::selector(circuit, c0_greater, c1_greater, c1_equal)
455+
}
447456
}
448457

449458
#[cfg(test)]

0 commit comments

Comments
 (0)