Skip to content

Commit a72ff37

Browse files
perf: Speed up (i)NTT
Memoize the parts of the NTT that depend only on the slice length, not the slice itself. Co-authored-by: Thorkil Schmidiger <thor@neptune.cash>
2 parents b4379e8 + 1cecaeb commit a72ff37

4 files changed

Lines changed: 283 additions & 256 deletions

File tree

twenty-first/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ name = "tip5"
6363
harness = false
6464

6565
[[bench]]
66-
name = "ntt_forward"
66+
name = "ntt"
6767
harness = false
6868

6969
[[bench]]

twenty-first/benches/ntt.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
use criterion::BenchmarkId;
2+
use criterion::Criterion;
3+
use criterion::Throughput;
4+
use criterion::criterion_group;
5+
use criterion::criterion_main;
6+
use twenty_first::math::b_field_element::BFieldElement;
7+
use twenty_first::math::other::random_elements;
8+
use twenty_first::math::traits::PrimitiveRootOfUnity;
9+
use twenty_first::math::x_field_element::XFieldElement;
10+
11+
criterion_main!(benches);
12+
criterion_group!(
13+
name = benches;
14+
config = Criterion::default().sample_size(10);
15+
targets = pre_compute_swap_indices::<{ 1 << 20 }>,
16+
pre_compute_swap_indices::<{ 1 << 26 }>,
17+
pre_compute_twiddle_factors::<{ 1 << 20 }>,
18+
pre_compute_twiddle_factors::<{ 1 << 26 }>,
19+
bfe_ntt::<{ 1 << 7 }>,
20+
bfe_ntt::<{ 1 << 18 }>,
21+
bfe_ntt::<{ 1 << 23 }>,
22+
xfe_ntt::<{ 1 << 7 }>,
23+
xfe_ntt::<{ 1 << 18 }>,
24+
xfe_ntt::<{ 1 << 23 }>,
25+
bfe_intt::<{ 1 << 7 }>,
26+
bfe_intt::<{ 1 << 18 }>,
27+
bfe_intt::<{ 1 << 23 }>,
28+
xfe_intt::<{ 1 << 7 }>,
29+
xfe_intt::<{ 1 << 18 }>,
30+
xfe_intt::<{ 1 << 23 }>,
31+
);
32+
33+
fn pre_compute_swap_indices<const LEN: usize>(c: &mut Criterion) {
34+
c.benchmark_group("compute_swap_indices")
35+
.bench_function(BenchmarkId::new("len", LEN.ilog2()), |b| {
36+
b.iter(|| twenty_first::math::ntt::swap_indices(LEN))
37+
});
38+
}
39+
40+
fn pre_compute_twiddle_factors<const LEN: u32>(c: &mut Criterion) {
41+
let root = BFieldElement::primitive_root_of_unity(LEN.into()).unwrap();
42+
c.benchmark_group("compute_twiddle_factors")
43+
.bench_function(BenchmarkId::new("len", LEN.ilog2()), |b| {
44+
b.iter(|| twenty_first::math::ntt::twiddle_factors(LEN, root))
45+
});
46+
}
47+
48+
fn bfe_ntt<const LEN: usize>(c: &mut Criterion) {
49+
let mut xs = random_elements::<BFieldElement>(LEN);
50+
c.benchmark_group("bfe_ntt")
51+
.throughput(Throughput::Elements(LEN as u64))
52+
.bench_function(BenchmarkId::new("len", LEN.ilog2()), |b| {
53+
b.iter(|| twenty_first::math::ntt::ntt(&mut xs))
54+
});
55+
}
56+
57+
fn xfe_ntt<const LEN: usize>(c: &mut Criterion) {
58+
let mut xs = random_elements::<XFieldElement>(LEN);
59+
c.benchmark_group("xfe_ntt")
60+
.throughput(Throughput::Elements(LEN as u64))
61+
.bench_function(BenchmarkId::new("len", LEN.ilog2()), |b| {
62+
b.iter(|| twenty_first::math::ntt::ntt(&mut xs))
63+
});
64+
}
65+
66+
fn bfe_intt<const LEN: usize>(c: &mut Criterion) {
67+
let mut xs = random_elements::<BFieldElement>(LEN);
68+
c.benchmark_group("bfe_intt")
69+
.throughput(Throughput::Elements(LEN as u64))
70+
.bench_function(BenchmarkId::new("len", LEN.ilog2()), |b| {
71+
b.iter(|| twenty_first::math::ntt::intt(&mut xs))
72+
});
73+
}
74+
75+
fn xfe_intt<const LEN: usize>(c: &mut Criterion) {
76+
let mut xs = random_elements::<XFieldElement>(LEN);
77+
c.benchmark_group("xfe_intt")
78+
.throughput(Throughput::Elements(LEN as u64))
79+
.bench_function(BenchmarkId::new("len", LEN.ilog2()), |b| {
80+
b.iter(|| twenty_first::math::ntt::intt(&mut xs))
81+
});
82+
}

twenty-first/benches/ntt_forward.rs

Lines changed: 0 additions & 71 deletions
This file was deleted.

0 commit comments

Comments
 (0)