1- use std:: { str:: FromStr } ;
1+ use std:: str:: FromStr ;
22use ark_ff:: { AdditiveGroup , Field } ;
33use 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
66pub 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
0 commit comments