use std::hint::black_box; use criterion::{BenchmarkId, Criterion}; use rand::RngCore; use crate::{ api::{ ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxBigAdd, VecZnxBigAddInplace, VecZnxBigAddSmall, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigAutomorphism, VecZnxBigAutomorphismInplace, VecZnxBigAutomorphismInplaceTmpBytes, VecZnxBigNegate, VecZnxBigNegateInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSub, VecZnxBigSubABInplace, VecZnxBigSubBAInplace, VecZnxBigSubSmallA, VecZnxBigSubSmallB, }, layouts::{Backend, DataViewMut, Module, ScratchOwned, VecZnx, VecZnxBig}, source::Source, }; pub fn bench_vec_znx_big_add(c: &mut Criterion, label: &str) where Module: VecZnxBigAdd + ModuleNew + VecZnxBigAlloc, { let group_name: String = format!("vec_znx_big_add::{}", label); let mut group = c.benchmark_group(group_name); fn runner(params: [usize; 3]) -> impl FnMut() where Module: VecZnxBigAdd + ModuleNew + VecZnxBigAlloc, { let n: usize = params[0]; let cols: usize = params[1]; let size: usize = params[2]; let module: Module = Module::::new(n as u64); let mut source: Source = Source::new([0u8; 32]); let mut a: VecZnxBig, B> = module.vec_znx_big_alloc(cols, size); let mut b: VecZnxBig, B> = module.vec_znx_big_alloc(cols, size); let mut c: VecZnxBig, B> = module.vec_znx_big_alloc(cols, size); // Fill a with random bytes source.fill_bytes(a.data_mut()); source.fill_bytes(b.data_mut()); source.fill_bytes(c.data_mut()); move || { for i in 0..cols { module.vec_znx_big_add(&mut c, i, &a, i, &b, i); } black_box(()); } } for params in [[10, 2, 2], [11, 2, 4], [12, 2, 8], [13, 2, 16], [14, 2, 32]] { let id: BenchmarkId = BenchmarkId::from_parameter(format!("{}x({}x{})", 1 << params[0], params[1], params[2],)); let mut runner = runner::(params); group.bench_with_input(id, &(), |b, _| b.iter(&mut runner)); } group.finish(); } pub fn bench_vec_znx_big_add_inplace(c: &mut Criterion, label: &str) where Module: VecZnxBigAddInplace + ModuleNew + VecZnxBigAlloc, { let group_name: String = format!("vec_znx_big_add_inplace::{}", label); let mut group = c.benchmark_group(group_name); fn runner(params: [usize; 3]) -> impl FnMut() where Module: VecZnxBigAddInplace + ModuleNew + VecZnxBigAlloc, { let n: usize = params[0]; let cols: usize = params[1]; let size: usize = params[2]; let module: Module = Module::::new(n as u64); let mut source: Source = Source::new([0u8; 32]); let mut a: VecZnxBig, B> = module.vec_znx_big_alloc(cols, size); let mut c: VecZnxBig, B> = module.vec_znx_big_alloc(cols, size); // Fill a with random i64 source.fill_bytes(a.data_mut()); source.fill_bytes(c.data_mut()); move || { for i in 0..cols { module.vec_znx_big_add_inplace(&mut c, i, &a, i); } black_box(()); } } for params in [[10, 2, 2], [11, 2, 4], [12, 2, 8], [13, 2, 16], [14, 2, 32]] { let id: BenchmarkId = BenchmarkId::from_parameter(format!("{}x({}x{})", 1 << params[0], params[1], params[2],)); let mut runner = runner::(params); group.bench_with_input(id, &(), |b, _| b.iter(&mut runner)); } group.finish(); } pub fn bench_vec_znx_big_add_small(c: &mut Criterion, label: &str) where Module: VecZnxBigAddSmall + ModuleNew + VecZnxBigAlloc, { let group_name: String = format!("vec_znx_big_add_small::{}", label); let mut group = c.benchmark_group(group_name); fn runner(params: [usize; 3]) -> impl FnMut() where Module: VecZnxBigAddSmall + ModuleNew + VecZnxBigAlloc, { let n: usize = params[0]; let cols: usize = params[1]; let size: usize = params[2]; let module: Module = Module::::new(n as u64); let mut source: Source = Source::new([0u8; 32]); let mut a: VecZnxBig, B> = module.vec_znx_big_alloc(cols, size); let mut b: VecZnx> = VecZnx::alloc(n, cols, size); let mut c: VecZnxBig, B> = module.vec_znx_big_alloc(cols, size); // Fill a with random i64 source.fill_bytes(a.data_mut()); source.fill_bytes(b.data_mut()); source.fill_bytes(c.data_mut()); move || { for i in 0..cols { module.vec_znx_big_add_small(&mut c, i, &a, i, &b, i); } black_box(()); } } for params in [[10, 2, 2], [11, 2, 4], [12, 2, 8], [13, 2, 16], [14, 2, 32]] { let id: BenchmarkId = BenchmarkId::from_parameter(format!("{}x({}x{})", 1 << params[0], params[1], params[2],)); let mut runner = runner::(params); group.bench_with_input(id, &(), |b, _| b.iter(&mut runner)); } group.finish(); } pub fn bench_vec_znx_big_add_small_inplace(c: &mut Criterion, label: &str) where Module: VecZnxBigAddSmallInplace + ModuleNew + VecZnxBigAlloc, { let group_name: String = format!("vec_znx_big_add_small_inplace::{}", label); let mut group = c.benchmark_group(group_name); fn runner(params: [usize; 3]) -> impl FnMut() where Module: VecZnxBigAddSmallInplace + ModuleNew + VecZnxBigAlloc, { let n: usize = params[0]; let cols: usize = params[1]; let size: usize = params[2]; let module: Module = Module::::new(n as u64); let mut source: Source = Source::new([0u8; 32]); let mut a: VecZnx> = VecZnx::alloc(n, cols, size); let mut c: VecZnxBig, B> = module.vec_znx_big_alloc(cols, size); // Fill a with random i64 source.fill_bytes(a.data_mut()); source.fill_bytes(c.data_mut()); move || { for i in 0..cols { module.vec_znx_big_add_small_inplace(&mut c, i, &a, i); } black_box(()); } } for params in [[10, 2, 2], [11, 2, 4], [12, 2, 8], [13, 2, 16], [14, 2, 32]] { let id: BenchmarkId = BenchmarkId::from_parameter(format!("{}x({}x{})", 1 << params[0], params[1], params[2],)); let mut runner = runner::(params); group.bench_with_input(id, &(), |b, _| b.iter(&mut runner)); } group.finish(); } pub fn bench_vec_znx_big_automorphism(c: &mut Criterion, label: &str) where Module: VecZnxBigAutomorphism + ModuleNew + VecZnxBigAlloc, { let group_name: String = format!("vec_znx_big_automorphism::{}", label); let mut group = c.benchmark_group(group_name); fn runner(params: [usize; 3]) -> impl FnMut() where Module: VecZnxBigAutomorphism + ModuleNew + VecZnxBigAlloc, { let n: usize = params[0]; let cols: usize = params[1]; let size: usize = params[2]; let module: Module = Module::::new(n as u64); let mut source: Source = Source::new([0u8; 32]); let mut a: VecZnxBig, B> = module.vec_znx_big_alloc(cols, size); let mut res: VecZnxBig, B> = module.vec_znx_big_alloc(cols, size); // Fill a with random i64 source.fill_bytes(a.data_mut()); source.fill_bytes(res.data_mut()); move || { for i in 0..cols { module.vec_znx_big_automorphism(-7, &mut res, i, &a, i); } black_box(()); } } for params in [[10, 2, 2], [11, 2, 4], [12, 2, 8], [13, 2, 16], [14, 2, 32]] { let id: BenchmarkId = BenchmarkId::from_parameter(format!("{}x({}x{})", 1 << params[0], params[1], params[2],)); let mut runner = runner::(params); group.bench_with_input(id, &(), |b, _| b.iter(&mut runner)); } group.finish(); } pub fn bench_vec_znx_automorphism_inplace(c: &mut Criterion, label: &str) where Module: VecZnxBigAutomorphismInplace + VecZnxBigAutomorphismInplaceTmpBytes + ModuleNew + VecZnxBigAlloc, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, { let group_name: String = format!("vec_znx_automorphism_inplace::{}", label); let mut group = c.benchmark_group(group_name); fn runner(params: [usize; 3]) -> impl FnMut() where Module: VecZnxBigAutomorphismInplace + ModuleNew + VecZnxBigAutomorphismInplaceTmpBytes + VecZnxBigAlloc, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, { let n: usize = params[0]; let cols: usize = params[1]; let size: usize = params[2]; let module: Module = Module::::new(n as u64); let mut source: Source = Source::new([0u8; 32]); let mut res: VecZnxBig, B> = module.vec_znx_big_alloc(cols, size); let mut scratch: ScratchOwned = ScratchOwned::alloc(module.vec_znx_big_automorphism_inplace_tmp_bytes()); // Fill a with random i64 source.fill_bytes(res.data_mut()); move || { for i in 0..cols { module.vec_znx_big_automorphism_inplace(-7, &mut res, i, scratch.borrow()); } black_box(()); } } for params in [[10, 2, 2], [11, 2, 4], [12, 2, 8], [13, 2, 16], [14, 2, 32]] { let id: BenchmarkId = BenchmarkId::from_parameter(format!("{}x({}x{})", 1 << params[0], params[1], params[2],)); let mut runner = runner::(params); group.bench_with_input(id, &(), |b, _| b.iter(&mut runner)); } group.finish(); } pub fn bench_vec_znx_big_negate(c: &mut Criterion, label: &str) where Module: VecZnxBigNegate + ModuleNew + VecZnxBigAlloc, { let group_name: String = format!("vec_znx_big_negate::{}", label); let mut group = c.benchmark_group(group_name); fn runner(params: [usize; 3]) -> impl FnMut() where Module: VecZnxBigNegate + ModuleNew + VecZnxBigAlloc, { let module: Module = Module::::new(1 << params[0]); let cols: usize = params[1]; let size: usize = params[2]; let mut source: Source = Source::new([0u8; 32]); let mut a: VecZnxBig, B> = module.vec_znx_big_alloc(cols, size); let mut b: VecZnxBig, B> = module.vec_znx_big_alloc(cols, size); // Fill a with random i64 source.fill_bytes(a.data_mut()); source.fill_bytes(b.data_mut()); move || { for i in 0..cols { module.vec_znx_big_negate(&mut b, i, &a, i); } black_box(()); } } for params in [[10, 2, 2], [11, 2, 4], [12, 2, 8], [13, 2, 16], [14, 2, 32]] { let id: BenchmarkId = BenchmarkId::from_parameter(format!("{}x({}x{})", 1 << params[0], params[1], params[2],)); let mut runner = runner::(params); group.bench_with_input(id, &(), |b, _| b.iter(&mut runner)); } group.finish(); } pub fn bench_vec_znx_big_negate_inplace(c: &mut Criterion, label: &str) where Module: VecZnxBigNegateInplace + ModuleNew + VecZnxBigAlloc, { let group_name: String = format!("vec_znx_negate_big_inplace::{}", label); let mut group = c.benchmark_group(group_name); fn runner(params: [usize; 3]) -> impl FnMut() where Module: VecZnxBigNegateInplace + ModuleNew + VecZnxBigAlloc, { let module: Module = Module::::new(1 << params[0]); let cols: usize = params[1]; let size: usize = params[2]; let mut source: Source = Source::new([0u8; 32]); let mut a: VecZnxBig, B> = module.vec_znx_big_alloc(cols, size); // Fill a with random i64 source.fill_bytes(a.data_mut()); move || { for i in 0..cols { module.vec_znx_big_negate_inplace(&mut a, i); } black_box(()); } } for params in [[10, 2, 2], [11, 2, 4], [12, 2, 8], [13, 2, 16], [14, 2, 32]] { let id: BenchmarkId = BenchmarkId::from_parameter(format!("{}x({}x{})", 1 << params[0], params[1], params[2])); let mut runner = runner::(params); group.bench_with_input(id, &(), |b, _| b.iter(&mut runner)); } group.finish(); } pub fn bench_vec_znx_normalize(c: &mut Criterion, label: &str) where Module: VecZnxBigNormalize + ModuleNew + VecZnxBigNormalizeTmpBytes + VecZnxBigAlloc, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, { let group_name: String = format!("vec_znx_big_normalize::{}", label); let mut group = c.benchmark_group(group_name); fn runner(params: [usize; 3]) -> impl FnMut() where Module: VecZnxBigNormalize + ModuleNew + VecZnxBigNormalizeTmpBytes + VecZnxBigAlloc, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, { let n: usize = params[0]; let cols: usize = params[1]; let size: usize = params[2]; let module: Module = Module::::new(n as u64); let basek: usize = 50; let mut source: Source = Source::new([0u8; 32]); let mut a: VecZnxBig, B> = module.vec_znx_big_alloc(cols, size); let mut res: VecZnx> = VecZnx::alloc(module.n(), cols, size); // Fill a with random i64 source.fill_bytes(a.data_mut()); source.fill_bytes(res.data_mut()); let mut scratch: ScratchOwned = ScratchOwned::alloc(module.vec_znx_big_normalize_tmp_bytes()); move || { for i in 0..cols { module.vec_znx_big_normalize(basek, &mut res, i, &a, i, scratch.borrow()); } black_box(()); } } for params in [[10, 2, 2], [11, 2, 4], [12, 2, 8], [13, 2, 16], [14, 2, 32]] { let id: BenchmarkId = BenchmarkId::from_parameter(format!("{}x({}x{})", 1 << params[0], params[1], params[2],)); let mut runner = runner::(params); group.bench_with_input(id, &(), |b, _| b.iter(&mut runner)); } group.finish(); } pub fn bench_vec_znx_big_sub(c: &mut Criterion, label: &str) where Module: VecZnxBigSub + ModuleNew + VecZnxBigAlloc, { let group_name: String = format!("vec_znx_big_sub::{}", label); let mut group = c.benchmark_group(group_name); fn runner(params: [usize; 3]) -> impl FnMut() where Module: VecZnxBigSub + ModuleNew + VecZnxBigAlloc, { let module: Module = Module::::new(1 << params[0]); let cols: usize = params[1]; let size: usize = params[2]; let mut source: Source = Source::new([0u8; 32]); let mut a: VecZnxBig, B> = module.vec_znx_big_alloc(cols, size); let mut b: VecZnxBig, B> = module.vec_znx_big_alloc(cols, size); let mut c: VecZnxBig, B> = module.vec_znx_big_alloc(cols, size); // Fill a with random bytes source.fill_bytes(a.data_mut()); source.fill_bytes(b.data_mut()); source.fill_bytes(c.data_mut()); move || { for i in 0..cols { module.vec_znx_big_sub(&mut c, i, &a, i, &b, i); } black_box(()); } } for params in [[10, 2, 2], [11, 2, 4], [12, 2, 8], [13, 2, 16], [14, 2, 32]] { let id: BenchmarkId = BenchmarkId::from_parameter(format!("{}x({}x{})", 1 << params[0], params[1], params[2],)); let mut runner = runner::(params); group.bench_with_input(id, &(), |b, _| b.iter(&mut runner)); } group.finish(); } pub fn bench_vec_znx_big_sub_ab_inplace(c: &mut Criterion, label: &str) where Module: VecZnxBigSubABInplace + ModuleNew + VecZnxBigAlloc, { let group_name: String = format!("vec_znx_big_sub_inplace::{}", label); let mut group = c.benchmark_group(group_name); fn runner(params: [usize; 3]) -> impl FnMut() where Module: VecZnxBigSubABInplace + ModuleNew + VecZnxBigAlloc, { let module: Module = Module::::new(1 << params[0]); let cols: usize = params[1]; let size: usize = params[2]; let mut source: Source = Source::new([0u8; 32]); let mut a: VecZnxBig, B> = module.vec_znx_big_alloc(cols, size); let mut c: VecZnxBig, B> = module.vec_znx_big_alloc(cols, size); // Fill a with random bytes source.fill_bytes(a.data_mut()); source.fill_bytes(c.data_mut()); move || { for i in 0..cols { module.vec_znx_big_sub_ab_inplace(&mut c, i, &a, i); } black_box(()); } } for params in [[10, 2, 2], [11, 2, 4], [12, 2, 8], [13, 2, 16], [14, 2, 32]] { let id: BenchmarkId = BenchmarkId::from_parameter(format!("{}x({}x{})", 1 << params[0], params[1], params[2],)); let mut runner = runner::(params); group.bench_with_input(id, &(), |b, _| b.iter(&mut runner)); } group.finish(); } pub fn bench_vec_znx_big_sub_ba_inplace(c: &mut Criterion, label: &str) where Module: VecZnxBigSubBAInplace + ModuleNew + VecZnxBigAlloc, { let group_name: String = format!("vec_znx_big_sub_inplace::{}", label); let mut group = c.benchmark_group(group_name); fn runner(params: [usize; 3]) -> impl FnMut() where Module: VecZnxBigSubBAInplace + ModuleNew + VecZnxBigAlloc, { let module: Module = Module::::new(1 << params[0]); let cols: usize = params[1]; let size: usize = params[2]; let mut source: Source = Source::new([0u8; 32]); let mut a: VecZnxBig, B> = module.vec_znx_big_alloc(cols, size); let mut c: VecZnxBig, B> = module.vec_znx_big_alloc(cols, size); // Fill a with random bytes source.fill_bytes(a.data_mut()); source.fill_bytes(c.data_mut()); move || { for i in 0..cols { module.vec_znx_big_sub_ba_inplace(&mut c, i, &a, i); } black_box(()); } } for params in [[10, 2, 2], [11, 2, 4], [12, 2, 8], [13, 2, 16], [14, 2, 32]] { let id: BenchmarkId = BenchmarkId::from_parameter(format!("{}x({}x{})", 1 << params[0], params[1], params[2],)); let mut runner = runner::(params); group.bench_with_input(id, &(), |b, _| b.iter(&mut runner)); } group.finish(); } pub fn bench_vec_znx_big_sub_small_a(c: &mut Criterion, label: &str) where Module: VecZnxBigSubSmallA + ModuleNew + VecZnxBigAlloc, { let group_name: String = format!("vec_znx_big_sub_small_a::{}", label); let mut group = c.benchmark_group(group_name); fn runner(params: [usize; 3]) -> impl FnMut() where Module: VecZnxBigSubSmallA + ModuleNew + VecZnxBigAlloc, { let module: Module = Module::::new(1 << params[0]); let cols: usize = params[1]; let size: usize = params[2]; let mut source: Source = Source::new([0u8; 32]); let mut a: VecZnx> = VecZnx::alloc(module.n(), cols, size); let mut b: VecZnxBig, B> = module.vec_znx_big_alloc(cols, size); let mut c: VecZnxBig, B> = module.vec_znx_big_alloc(cols, size); // Fill a with random bytes source.fill_bytes(a.data_mut()); source.fill_bytes(b.data_mut()); source.fill_bytes(c.data_mut()); move || { for i in 0..cols { module.vec_znx_big_sub_small_a(&mut c, i, &a, i, &b, i); } black_box(()); } } for params in [[10, 2, 2], [11, 2, 4], [12, 2, 8], [13, 2, 16], [14, 2, 32]] { let id: BenchmarkId = BenchmarkId::from_parameter(format!("{}x({}x{})", 1 << params[0], params[1], params[2],)); let mut runner = runner::(params); group.bench_with_input(id, &(), |b, _| b.iter(&mut runner)); } group.finish(); } pub fn bench_vec_znx_big_sub_small_b(c: &mut Criterion, label: &str) where Module: VecZnxBigSubSmallB + ModuleNew + VecZnxBigAlloc, { let group_name: String = format!("vec_znx_big_sub_small_b::{}", label); let mut group = c.benchmark_group(group_name); fn runner(params: [usize; 3]) -> impl FnMut() where Module: VecZnxBigSubSmallB + ModuleNew + VecZnxBigAlloc, { let module: Module = Module::::new(1 << params[0]); let cols: usize = params[1]; let size: usize = params[2]; let mut source: Source = Source::new([0u8; 32]); let mut a: VecZnxBig, B> = module.vec_znx_big_alloc(cols, size); let mut b: VecZnx> = VecZnx::alloc(module.n(), cols, size); let mut c: VecZnxBig, B> = module.vec_znx_big_alloc(cols, size); // Fill a with random bytes source.fill_bytes(a.data_mut()); source.fill_bytes(b.data_mut()); source.fill_bytes(c.data_mut()); move || { for i in 0..cols { module.vec_znx_big_sub_small_b(&mut c, i, &a, i, &b, i); } black_box(()); } } for params in [[10, 2, 2], [11, 2, 4], [12, 2, 8], [13, 2, 16], [14, 2, 32]] { let id: BenchmarkId = BenchmarkId::from_parameter(format!("{}x({}x{})", 1 << params[0], params[1], params[2],)); let mut runner = runner::(params); group.bench_with_input(id, &(), |b, _| b.iter(&mut runner)); } group.finish(); }