Skip to content

Commit 94f4949

Browse files
committed
small changes
1 parent e5102ca commit 94f4949

File tree

3 files changed

+51
-11
lines changed

3 files changed

+51
-11
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,8 @@ pub trait MontConfig<const N: usize>: 'static + Sync + Send + Sized {
408408
} else if BigInt::from(r) >= <MontBackend<Self, N>>::MODULUS {
409409
None
410410
} else {
411-
Some(Fp::new_unchecked(Self::R2).mul_u64(r))
411+
// Multiply R (one in Montgomery form) with the u64
412+
Some(Fp::new_unchecked(Self::R).mul_u64(r))
412413
}
413414
}
414415

@@ -939,9 +940,10 @@ impl<T: MontConfig<N>, const N: usize> Fp<MontBackend<T, N>, N> {
939940
/// Multiply by a u128.
940941
/// Uses optimized mul_u64 if the input fits within u64,
941942
/// otherwise falls back to standard multiplication.
943+
/// DO NOT USE right now, highly inefficient (2x slower than regular mul)
942944
#[inline(always)]
943945
pub fn mul_u128(self, other: u128) -> Self {
944-
if other <= u64::MAX as u128 {
946+
if other >> 64 == 0 {
945947
self.mul_u64(other as u64)
946948
} else {
947949
// Fallback: Convert u128 to Fp and multiply.
@@ -953,6 +955,7 @@ impl<T: MontConfig<N>, const N: usize> Fp<MontBackend<T, N>, N> {
953955
/// Multiply by an i128.
954956
/// Uses optimized mul_i64 if the input fits within i64,
955957
/// otherwise falls back to standard multiplication.
958+
/// DO NOT USE right now, highly inefficient (2x slower than regular mul)
956959
#[inline(always)]
957960
pub fn mul_i128(self, other: i128) -> Self {
958961
if other >= i64::MIN as i128 && other <= i64::MAX as i128 {

test-curves/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ path = "benches/mnt6_753.rs"
7979
harness = false
8080

8181
[[bench]]
82-
name = "field_mul_u64"
83-
path = "benches/field_mul_u64.rs"
82+
name = "small_mul"
83+
path = "benches/small_mul.rs"
8484
harness = false
85-
required-features = ["secp256k1"]
85+
required-features = ["bn254"]
8686

8787
[[bench]]
8888
name = "bn254"

test-curves/benches/field_mul_u64.rs renamed to test-curves/benches/small_mul.rs

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
11
use ark_ff::{Field, UniformRand};
22
use ark_std::rand::{rngs::StdRng, Rng, SeedableRng};
3-
use ark_test_curves::secp256k1::Fr;
3+
use ark_test_curves::bn254::Fr;
44
use criterion::{criterion_group, criterion_main, Criterion};
55

6-
fn mul_u64_bench(c: &mut Criterion) {
6+
fn mul_small_bench(c: &mut Criterion) {
77
const SAMPLES: usize = 1000;
88
// Use a fixed seed for reproducibility
99
let mut rng = StdRng::seed_from_u64(0u64);
1010

1111
let a_s = (0..SAMPLES)
1212
.map(|_| Fr::rand(&mut rng))
1313
.collect::<Vec<_>>();
14-
let b_s = (0..SAMPLES)
14+
15+
let b_u64_s = (0..SAMPLES)
1516
.map(|_| rng.gen::<u64>())
1617
.collect::<Vec<_>>();
1718
// Convert u64 to Fr for standard multiplication benchmark
18-
let b_fr_s = b_s.iter().map(|&b| Fr::from(b)).collect::<Vec<_>>();
19+
let b_fr_s = b_u64_s.iter().map(|&b| Fr::from(b)).collect::<Vec<_>>();
20+
21+
let b_i64_s = (0..SAMPLES)
22+
.map(|_| rng.gen::<i64>())
23+
.collect::<Vec<_>>();
24+
25+
let b_u128_s = (0..SAMPLES)
26+
.map(|_| rng.gen::<u128>())
27+
.collect::<Vec<_>>();
28+
29+
let b_i128_s = (0..SAMPLES)
30+
.map(|_| rng.gen::<i128>())
31+
.collect::<Vec<_>>();
1932

2033
// Generate another set of random Fr elements for addition
2134
let c_s = (0..SAMPLES)
@@ -29,7 +42,31 @@ fn mul_u64_bench(c: &mut Criterion) {
2942
bench.iter(|| {
3043
i = (i + 1) % SAMPLES;
3144
// Make sure the computation is not optimized away
32-
criterion::black_box(a_s[i].mul_u64(b_s[i]))
45+
criterion::black_box(a_s[i].mul_u64(b_u64_s[i]))
46+
})
47+
});
48+
49+
group.bench_function("mul_i64", |bench| {
50+
let mut i = 0;
51+
bench.iter(|| {
52+
i = (i + 1) % SAMPLES;
53+
criterion::black_box(a_s[i].mul_i64(b_i64_s[i]))
54+
})
55+
});
56+
57+
group.bench_function("mul_u128", |bench| {
58+
let mut i = 0;
59+
bench.iter(|| {
60+
i = (i + 1) % SAMPLES;
61+
criterion::black_box(a_s[i].mul_u128(b_u128_s[i]))
62+
})
63+
});
64+
65+
group.bench_function("mul_i128", |bench| {
66+
let mut i = 0;
67+
bench.iter(|| {
68+
i = (i + 1) % SAMPLES;
69+
criterion::black_box(a_s[i].mul_i128(b_i128_s[i]))
3370
})
3471
});
3572

@@ -63,5 +100,5 @@ fn mul_u64_bench(c: &mut Criterion) {
63100
group.finish();
64101
}
65102

66-
criterion_group!(benches, mul_u64_bench);
103+
criterion_group!(benches, mul_small_bench);
67104
criterion_main!(benches);

0 commit comments

Comments
 (0)