Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add precomp method (blst) #267

Draft
wants to merge 7 commits into
base: kw/msm-ref-pippenger
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 29 additions & 13 deletions cryptography/bls12_381/benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ use crate_crypto_internal_eth_kzg_bls12_381::{
batch_inversion,
ff::Field,
fixed_base_msm::{FixedBaseMSM, UsePrecomp},
fixed_base_msm_blst::FixedBaseMSMPrecompBLST,
fixed_base_msm_pippenger::FixedBaseMSMPippenger,
g1_batch_normalize, g2_batch_normalize,
group::Group,
lincomb::{g1_lincomb, g1_lincomb_unsafe, g2_lincomb, g2_lincomb_unsafe},
G1Projective, G2Projective,
G1Point, G1Projective, G2Projective,
};
use criterion::{criterion_group, criterion_main, Criterion};
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};

pub fn batch_inversion(c: &mut Criterion) {
const NUM_ELEMENTS: usize = 8192;
Expand All @@ -30,18 +31,33 @@ pub fn fixed_base_msm(c: &mut Criterion) {
.into_iter()
.map(|p| p.into())
.collect();
let fbm = FixedBaseMSM::new(generators.clone(), UsePrecomp::Yes { width: 8 });
let scalars: Vec<_> = random_scalars(length);
// let fbm = FixedBaseMSM::new(generators.clone(), UsePrecomp::Yes { width: 8 });

c.bench_function("bls12_381 fixed_base_msm length=64 width=8", |b| {
b.iter(|| fbm.msm(scalars.clone()))
});
// c.bench_function("bls12_381 fixed_base_msm length=64 width=8", |b| {
// b.iter(|| fbm.msm(scalars.clone()))
// });

let fixed_base_pip = FixedBaseMSMPippenger::new(&generators);
// let fixed_base_pip = FixedBaseMSMPippenger::new(&generators);

c.bench_function("bls12_381 fixed based pippenger algorithm", |b| {
b.iter(|| fixed_base_pip.msm(&scalars))
});
// c.bench_function("bls12_381 fixed based pippenger algorithm", |b| {
// b.iter(|| fixed_base_pip.msm(&scalars))
// });

let mut group = c.benchmark_group("bls12_381 fixed base windowed algorithm");

for window_size in 2..=14 {
// Test window sizes from 2 to 10
// Create the FixedBaseMSMPrecompBLST instance outside the benchmarked portion
let fixed_base = FixedBaseMSMPrecompBLST::new(&generators, window_size);

group.bench_with_input(
BenchmarkId::new("window_size", window_size),
&window_size,
|b, &_| b.iter(|| black_box(fixed_base.msm(black_box(&scalars)))),
);
}
group.finish();
}

pub fn bench_msm(c: &mut Criterion) {
Expand Down Expand Up @@ -97,9 +113,9 @@ fn random_g2_points(size: usize) -> Vec<G2Projective> {

criterion_group!(
benches,
batch_inversion,
fixed_base_msm,
bench_msm,
// batch_inversion,
// fixed_base_msm,
// bench_msm,
fixed_base_msm
);
criterion_main!(benches);
11 changes: 7 additions & 4 deletions cryptography/bls12_381/src/fixed_base_msm.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{fixed_base_msm_pippenger::FixedBaseMSMPippenger, G1Projective, Scalar};
use crate::{
fixed_base_msm_blst::FixedBaseMSMPrecompBLST, fixed_base_msm_pippenger::FixedBaseMSMPippenger,
G1Projective, Scalar,
};
use blstrs::{Fp, G1Affine};

/// FixedBaseMSMPrecomp computes a multi scalar multiplication using pre-computations.
Expand Down Expand Up @@ -27,7 +30,7 @@ pub enum UsePrecomp {
/// of memory.
#[derive(Debug)]
pub enum FixedBaseMSM {
Precomp(FixedBaseMSMPrecomp),
Precomp(FixedBaseMSMPrecompBLST),
// TODO: We are hijacking the NoPrecomp variant to store the
// TODO: new pippenger algorithm.
NoPrecomp(FixedBaseMSMPippenger),
Expand All @@ -37,15 +40,15 @@ impl FixedBaseMSM {
pub fn new(generators: Vec<G1Affine>, use_precomp: UsePrecomp) -> Self {
match use_precomp {
UsePrecomp::Yes { width } => {
FixedBaseMSM::Precomp(FixedBaseMSMPrecomp::new(generators, width))
FixedBaseMSM::Precomp(FixedBaseMSMPrecompBLST::new(&generators, width))
}
UsePrecomp::No => FixedBaseMSM::NoPrecomp(FixedBaseMSMPippenger::new(&generators)),
}
}

pub fn msm(&self, scalars: Vec<Scalar>) -> G1Projective {
match self {
FixedBaseMSM::Precomp(precomp) => precomp.msm(scalars),
FixedBaseMSM::Precomp(precomp) => precomp.msm(&scalars),
FixedBaseMSM::NoPrecomp(precomp) => precomp.msm(&scalars),
}
}
Expand Down
151 changes: 151 additions & 0 deletions cryptography/bls12_381/src/fixed_base_msm_blst.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
use std::time::{Duration, Instant};

use crate::{
batch_add::{batch_addition, multi_batch_addition},
booth_encoding::{self, get_booth_index},
fixed_base_msm_pippenger::FixedBaseMSMPippenger,
g1_batch_normalize, G1Projective, Scalar,
};
use blstrs::{Fp, G1Affine};
use ff::PrimeField;
use group::prime::PrimeCurveAffine;
use group::Group;

// FixedBasePrecomp blst way with some changes
#[derive(Debug)]
pub struct FixedBaseMSMPrecompBLST {
table: Vec<Vec<G1Affine>>, // TODO: Make this a Vec<> and then just do the maths in msm function for offsetting
// table: Vec<G1Affine>,
Comment on lines +16 to +18
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both seem to perform the same

wbits: usize,
}

impl FixedBaseMSMPrecompBLST {
pub fn new(points: &[G1Affine], wbits: usize) -> Self {
// For every point `P`, wbits indicates that we should compute
// 1 * P, ..., (2^{wbits} - 1) * P
//
// The total amount of memory is roughly (numPoints * 2^{wbits} - 1)
// where each point is 64 bytes.
//
//
let mut precomputed_points: Vec<_> = points
.into_iter()
.map(|point| Self::precompute_points(wbits, *point))
.collect();

Self {
table: precomputed_points,
wbits,
}
}

fn precompute_points(wbits: usize, point: G1Affine) -> Vec<G1Affine> {
let mut lookup_table = Vec::with_capacity(1 << wbits);

// Convert to projective for faster operations
let mut current = G1Projective::from(point);

// Compute and store multiples
for _ in 0..(1 << wbits) {
lookup_table.push(current);
current += point;
}

g1_batch_normalize(&lookup_table)
}

pub fn msm(&self, scalars: &[Scalar]) -> G1Projective {
let scalars_bytes: Vec<_> = scalars.iter().map(|a| a.to_bytes_le()).collect();
let number_of_windows = Scalar::NUM_BITS as usize / self.wbits + 1;

let mut windows_of_points = vec![Vec::with_capacity(scalars.len()); number_of_windows];

for window_idx in 0..number_of_windows {
for (scalar_idx, scalar_bytes) in scalars_bytes.iter().enumerate() {
let sub_table = &self.table[scalar_idx];
let point_idx = get_booth_index(window_idx, self.wbits, scalar_bytes.as_ref());

if point_idx == 0 {
continue;
}

let sign = point_idx.is_positive();
let point_idx = point_idx.unsigned_abs() as usize - 1;
let mut point = sub_table[point_idx];
// let mut point = self.table[(scalar_idx * 1 << self.wbits) + point_idx];
if !sign {
point = -point;
}

windows_of_points[window_idx].push(point);
}
}

// For each window, lets add all of the points together.
// let accumulated_points: Vec<_> = windows_of_points
// .into_iter()
// .map(|wp| batch_addition(wp))
// .collect();
let accumulated_points = multi_batch_addition(windows_of_points);

// Now accumulate the windows by doubling wbits times
let mut result = G1Projective::identity();
for point in accumulated_points.into_iter().rev() {
// Double the result 'wbits' times
for _ in 0..self.wbits {
result = result.double();
}
// Add the accumulated point for this window
result += point;
}

result
}
}

#[test]
fn precomp_lookup_table() {
use group::Group;
let lookup_table = FixedBaseMSMPrecompBLST::precompute_points(7, G1Affine::generator());

for i in 1..lookup_table.len() {
let expected = G1Projective::generator() * Scalar::from((i + 1) as u64);
assert_eq!(lookup_table[i], expected.into(),)
}
}
use ff::Field;

#[test]
fn msm_blst_precomp() {
let length = 64;
let generators: Vec<_> = (0..length)
.map(|_| G1Projective::random(&mut rand::thread_rng()).into())
.collect();
let scalars: Vec<_> = (0..length)
.map(|_| Scalar::random(&mut rand::thread_rng()))
.collect();

let res = crate::lincomb::g1_lincomb(&generators, &scalars)
.expect("number of generators and number of scalars is equal");

let fbm = FixedBaseMSMPrecompBLST::new(&generators, 7);
let result = fbm.msm(&scalars);

assert_eq!(res, result);
}

#[test]
fn bench_window_sizes_msm() {
let length = 64;
let generators: Vec<_> = (0..length)
.map(|_| G1Projective::random(&mut rand::thread_rng()).into())
.collect();
let scalars: Vec<_> = (0..length)
.map(|_| Scalar::random(&mut rand::thread_rng()))
.collect();

for i in 2..=14 {
let fbm = FixedBaseMSMPrecompBLST::new(&generators, i);
fbm.msm(&scalars);
}
}
1 change: 1 addition & 0 deletions cryptography/bls12_381/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod batch_add;
pub mod batch_inversion;
mod booth_encoding;
pub mod fixed_base_msm;
pub mod fixed_base_msm_blst;
pub mod fixed_base_msm_pippenger;
pub mod lincomb;

Expand Down