-
Notifications
You must be signed in to change notification settings - Fork 5
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(DO NOT MERGE):skunkworks #267
Draft
kevaundray
wants to merge
27
commits into
kw/msm-ref-pippenger
Choose a base branch
from
kw/msm-ref-pippenger-blst-method
base: kw/msm-ref-pippenger
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 6 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
66cafab
initial impl of blst method
kevaundray 18050fb
replace blst call with our method
kevaundray bb9fe61
cargo
kevaundray 5526357
benchmark
kevaundray eb14e85
fix outdated comment
kevaundray e8144ae
Update cryptography/bls12_381/src/fixed_base_msm_blst.rs
kevaundray 1519984
Update cryptography/bls12_381/src/lib.rs
kevaundray aa9ac00
add comment for precompute
kevaundray 1055475
initial limlee
kevaundray d27b08f
fixes plus eq3
kevaundray 229e3d9
add eq4
kevaundray dd243f3
add eq5
kevaundray 156e0df
add if statement for i_jt == 0
kevaundray e7e0a43
direct doubling plus paper implemented
kevaundray cb6d5a3
add direct doubling code that does not need arrays
kevaundray 762550b
modify main algorithm to handle direct doubling and handle edge cases
kevaundray 6a0765a
forgot to commit wnaf stuff
kevaundray 7e0e132
make code look more like window-precomp setup
kevaundray 1c0119c
move precomp into separate function
kevaundray a124863
commit intial seokim
kevaundray e807388
replace scalar muls with the two shifts needed (square shift and row …
kevaundray fbb931c
remove identity point
kevaundray f4008d6
precompute values along the square
kevaundray 1d6ad79
fix precomp and use it when choosing a point
kevaundray afab45e
remove variable doubling and just reverse th array and double
kevaundray e74f45e
commit remaining
kevaundray 0c6f1c3
add all-windows version
kevaundray File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>, | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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