@@ -8,6 +8,8 @@ use crate::{
88} ;
99use ark_ff_macros:: unroll_for_loops;
1010
11+ pub const PRECOMP_TABLE_SIZE : usize = 65536 ;
12+
1113/// A trait that specifies the constants and arithmetic procedures
1214/// for Montgomery arithmetic over the prime field defined by `MODULUS`.
1315///
@@ -81,7 +83,7 @@ pub trait MontConfig<const N: usize>: 'static + Sync + Send + Sized {
8183 sqrt_precomputation :: < N , Self > ( ) ;
8284
8385 #[ allow( long_running_const_eval) ]
84- const SMALL_ELEMENT_MONTGOMERY_PRECOMP : [ Fp < MontBackend < Self , N > , N > ; 65536 ] =
86+ const SMALL_ELEMENT_MONTGOMERY_PRECOMP : [ Fp < MontBackend < Self , N > , N > ; PRECOMP_TABLE_SIZE ] =
8587 small_element_montgomery_precomputation :: < N , Self > ( ) ;
8688
8789 /// (MODULUS + 1) / 4 when MODULUS % 4 == 3. Used for square root precomputations.
@@ -363,7 +365,7 @@ pub trait MontConfig<const N: usize>: 'static + Sync + Send + Sized {
363365 }
364366
365367 fn from_u64 ( r : u64 ) -> Option < Fp < MontBackend < Self , N > , N > > {
366- if r < 65536 {
368+ if r < PRECOMP_TABLE_SIZE as u64 {
367369 Some ( Self :: SMALL_ELEMENT_MONTGOMERY_PRECOMP [ r as usize ] )
368370 } else if BigInt :: from ( r) >= <MontBackend < Self , N > >:: MODULUS {
369371 None
@@ -577,13 +579,15 @@ pub const fn sqrt_precomputation<const N: usize, T: MontConfig<N>>(
577579 }
578580}
579581
582+ /// Adapted the `bn256-table` feature from `halo2curves`:
583+ /// https://github.com/privacy-scaling-explorations/halo2curves/blob/main/script/bn256.py
580584pub const fn small_element_montgomery_precomputation < const N : usize , T : MontConfig < N > > (
581- ) -> [ Fp < MontBackend < T , N > , N > ; 65536 ] {
582- let mut lookup_table: [ Fp < MontBackend < T , N > , N > ; 65536 ] =
583- [ <Fp < MontBackend < T , N > , N > >:: ZERO ; 65536 ] ;
585+ ) -> [ Fp < MontBackend < T , N > , N > ; PRECOMP_TABLE_SIZE ] {
586+ let mut lookup_table: [ Fp < MontBackend < T , N > , N > ; PRECOMP_TABLE_SIZE ] =
587+ [ <Fp < MontBackend < T , N > , N > >:: ZERO ; PRECOMP_TABLE_SIZE ] ;
584588
585589 let mut i: usize = 1 ;
586- while i < 65536 {
590+ while i < PRECOMP_TABLE_SIZE {
587591 let mut limbs = [ 0u64 ; N ] ;
588592 limbs[ 0 ] = i as u64 ;
589593 lookup_table[ i] = <Fp < MontBackend < T , N > , N > >:: new ( BigInt :: new ( limbs) ) ;
@@ -657,7 +661,7 @@ impl<T: MontConfig<N>, const N: usize> FpConfig<N> for MontBackend<T, N> {
657661 const SMALL_SUBGROUP_BASE_ADICITY : Option < u32 > = T :: SMALL_SUBGROUP_BASE_ADICITY ;
658662 const LARGE_SUBGROUP_ROOT_OF_UNITY : Option < Fp < Self , N > > = T :: LARGE_SUBGROUP_ROOT_OF_UNITY ;
659663 const SQRT_PRECOMP : Option < crate :: SqrtPrecomputation < Fp < Self , N > > > = T :: SQRT_PRECOMP ;
660- const SMALL_ELEMENT_MONTGOMERY_PRECOMP : [ Fp < Self , N > ; 65536 ] =
664+ const SMALL_ELEMENT_MONTGOMERY_PRECOMP : [ Fp < Self , N > ; PRECOMP_TABLE_SIZE ] =
661665 T :: SMALL_ELEMENT_MONTGOMERY_PRECOMP ;
662666
663667 fn add_assign ( a : & mut Fp < Self , N > , b : & Fp < Self , N > ) {
0 commit comments