Skip to content

Commit 51f3260

Browse files
committed
exp_by_const returns montgomery when exponent is zero
1 parent 0834f30 commit 51f3260

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

g16ckt/src/gadgets/bn254/fp254impl.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,11 @@ pub trait Fp254Impl {
696696
exp: &BigUint,
697697
) -> BigIntWires {
698698
if exp.is_zero() {
699-
return BigIntWires::new_constant(a.len(), &BigUint::one()).unwrap();
699+
// a^0 => 1 and Mont(1) => ark::Fq(R)
700+
let r = ark_bn254::Fq::from(Self::montgomery_r_as_biguint())
701+
.into_bigint()
702+
.into();
703+
return BigIntWires::new_constant(Self::N_BITS, &r).unwrap();
700704
}
701705

702706
if exp.is_one() {

g16ckt/src/gadgets/bn254/fq.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,33 @@ pub(super) mod tests {
680680
assert_eq!(result.output_value.value, expected_c);
681681
}
682682

683+
#[test]
684+
fn test_exp_by_constant_montgomery() {
685+
let a_v = rnd();
686+
687+
// test for random input
688+
let k = rnd().into_bigint();
689+
let expected_c = a_v.pow(k);
690+
let input = FqInput::new([Fq::as_montgomery(a_v)]);
691+
let result =
692+
CircuitBuilder::streaming_execute::<_, _, FqOutput>(input, 10_000, |ctx, input| {
693+
let [aa_wire] = input;
694+
Fq::exp_by_constant_montgomery(ctx, aa_wire, &k.into())
695+
});
696+
assert_eq!(result.output_value.value, Fq::as_montgomery(expected_c));
697+
698+
// test for zero exponent
699+
let k: ark_ff::BigInt<4> = ark_ff::BigInt::zero();
700+
let expected_c = a_v.pow(k);
701+
let input = FqInput::new([Fq::as_montgomery(a_v)]);
702+
let result =
703+
CircuitBuilder::streaming_execute::<_, _, FqOutput>(input, 10_000, |ctx, input| {
704+
let [aa_wire] = input;
705+
Fq::exp_by_constant_montgomery(ctx, aa_wire, &k.into())
706+
});
707+
assert_eq!(result.output_value.value, Fq::as_montgomery(expected_c));
708+
}
709+
683710
#[test]
684711
fn test_fq_multiplexer() {
685712
let w = 1;

0 commit comments

Comments
 (0)