Skip to content

Commit 5339f7c

Browse files
committed
Add attribution comment + put 2^16 in a constant
1 parent 72fc43e commit 5339f7c

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

bench-templates/src/macros/field.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ macro_rules! prime_field {
404404
})
405405
});
406406
let u64s = (0..SAMPLES)
407-
.map(|_| rng.next_u64() % 65536)
407+
.map(|_| rng.next_u64())
408408
.collect::<Vec<_>>();
409409
conversions.bench_function("From u64", |b| {
410410
let mut i = 0;

ff/src/fields/models/fp/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub trait FpConfig<const N: usize>: Send + Sync + 'static + Sized {
6666

6767
/// Precomputed lookup table for values 0..2^16 in Montgomery form.
6868
/// Otherwise, conversion to Montgomery form requires a multiplication by R^2.
69-
const SMALL_ELEMENT_MONTGOMERY_PRECOMP: [Fp<Self, N>; 65536];
69+
const SMALL_ELEMENT_MONTGOMERY_PRECOMP: [Fp<Self, N>; PRECOMP_TABLE_SIZE];
7070

7171
/// Set a += b.
7272
fn add_assign(a: &mut Fp<Self, N>, b: &Fp<Self, N>);

ff/src/fields/models/fp/montgomery_backend.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use crate::{
88
};
99
use 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
580584
pub 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

Comments
 (0)