Skip to content

Commit

Permalink
chore: bench concrete
Browse files Browse the repository at this point in the history
  • Loading branch information
0xWOLAND committed Jan 3, 2024
1 parent e3c0de2 commit e4d1444
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 1 deletion.
42 changes: 42 additions & 0 deletions BENCHMARK_CONCRETE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Benchmarks

## Table of Contents

- [Overview](#overview)
- [Benchmark Results](#benchmark-results)
- [Polynomial Multiplication Benchmarks](#polynomial-multiplication-benchmarks)

## Overview

This benchmark comparison report shows the difference in performance between parallel, NTT-based and serial, brute-force
polynomial multiplication algorithms. Each row entry in the first table is an n-degree forward NTT and each row entry in the second table represents an n-degree polynomial multiplication.

Computer Stats:

```
CPU(s): 16
Thread(s) per core: 2
Core(s) per socket: 8
Socket(s): 1
```

## Benchmark Results

### Polynomial Multiplication Benchmarks

| | `NTT-Based` | `Concrete-NTT` |
|:------------|:--------------------------|:------------------------------------ |
| **`64`** | `982.22 us` (✅ **1.00x**) | `84.31 ns` (🚀 **11650.05x faster**) |
| **`128`** | `1.18 ms` (✅ **1.00x**) | `149.42 ns` (🚀 **7901.64x faster**) |
| **`256`** | `2.03 ms` (✅ **1.00x**) | `286.35 ns` (🚀 **7091.84x faster**) |
| **`512`** | `2.75 ms` (✅ **1.00x**) | `600.13 ns` (🚀 **4580.51x faster**) |
| **`1024`** | `4.99 ms` (✅ **1.00x**) | `1.32 us` (🚀 **3779.10x faster**) |
| **`2048`** | `9.42 ms` (✅ **1.00x**) | `2.74 us` (🚀 **3434.03x faster**) |
| **`4096`** | `18.04 ms` (✅ **1.00x**) | `5.84 us` (🚀 **3089.55x faster**) |
| **`8192`** | `35.30 ms` (✅ **1.00x**) | `12.27 us` (🚀 **2877.03x faster**) |
| **`16384`** | `72.15 ms` (✅ **1.00x**) | `25.50 us` (🚀 **2829.18x faster**) |
| **`32768`** | `155.51 ms` (✅ **1.00x**) | `53.88 us` (🚀 **2886.10x faster**) |

---
Made with [criterion-table](https://github.com/nu11ptr/criterion-table)

32 changes: 32 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ categories = ["cryptography", "data-structures"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
concrete-ntt = "0.1.1"
criterion = "0.5.1"
crypto-bigint = "0.5.3"
hex = "0.4.3"
Expand All @@ -21,7 +22,7 @@ rayon = "1.8.0"
criterion = { version = "0.5.1", features = ["html_reports"] }

[[bench]]
name = "benchmark"
name = "concrete"
harness = false

[features]
Expand Down
54 changes: 54 additions & 0 deletions benches/concrete.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use concrete_ntt::prime32::Plan;
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
use fast_ntt::{
ntt::{forward, working_modulus, Constants},
numbers::BigInt,
polynomial::{fast_mul, mul_brute, Polynomial, PolynomialFieldElement, PolynomialTrait},
};
use itertools::Itertools;

const deg: usize = 16;

fn bench_mul<T: PolynomialFieldElement>(x: usize, c: &Constants<T>) {
let ONE = T::from(1);
let a = Polynomial::new(vec![0; x].iter().map(|_| ONE).collect_vec());
let b = Polynomial::new(vec![0; x].iter().map(|_| ONE).collect_vec());
let _ = fast_mul(a, b, c);
}

fn bench_concrete<T: PolynomialFieldElement>(x: usize, plan: &Plan) {
let data = vec![1; x];

let mut transformed_fwd = data;
plan.fwd(&mut transformed_fwd);
}

fn criterion_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("Polynomial Multiplication Benchmarks");

(6..deg).for_each(|n| {
let id = BenchmarkId::new("NTT-Based", 1 << n);
let N = BigInt::from((2 * n).next_power_of_two());
let M = N << 1 + 1;
let c = working_modulus(N, M);
group.bench_with_input(id, &n, |b, n| {
b.iter(|| bench_mul(black_box(1 << n), black_box(&c)))
});

let id = BenchmarkId::new("Concrete-NTT", 1 << n);

let N = (1 << n);
let p = 1062862849;
let plan = Plan::try_new(N, p).unwrap();
group.bench_with_input(id, &n, |b, n| {
b.iter(|| bench_concrete::<BigInt>(black_box(1 << n), black_box(&plan)))
});
});
}

criterion_group! {
name = benches;
config = Criterion::default().sample_size(10);
targets = criterion_benchmark
}
criterion_main!(benches);

0 comments on commit e4d1444

Please sign in to comment.