Skip to content

Commit

Permalink
release 0.0.12
Browse files Browse the repository at this point in the history
  • Loading branch information
ashWhiteHat committed Sep 21, 2023
1 parent 2401a26 commit 2027711
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 151 deletions.
15 changes: 8 additions & 7 deletions Cargo.lock

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

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = [
]
name = "ec-pairing"
description = 'Tate pairing implementation'
version = "0.0.11"
version = "0.0.12"
edition = "2021"
license = "Apache-2.0"
homepage = 'https://github.com/KogarashiNetwork/Kogarashi/'
Expand All @@ -19,9 +19,9 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs", "--html-in-header", "katex-header.html"]

[dependencies]
zkstd = { version = "0.0.12", default-features = false }
bls-12-381 = { version = "0.0.12", default-features = false }
jub-jub = { version = "0.0.14", default-features = false }
zkstd = { version = "0.0.13", default-features = false }
bls-12-381 = { version = "0.0.13", default-features = false }
jub-jub = { version = "0.0.15", default-features = false }
parity-scale-codec = { version = "2.0.0", default-features = false, features = ["derive"] }

[dev-dependencies]
Expand Down
101 changes: 2 additions & 99 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
use bls_12_381::params::{BLS_X, BLS_X_IS_NEGATIVE};
use bls_12_381::{Fq12, Fr, G1Affine, G1Projective, G2Affine, G2PairingAffine, G2Projective, Gt};
use jub_jub::{Fp, JubjubAffine, JubjubExtended};
use zkstd::common::Vec;
use zkstd::common::*;
use zkstd::common::{G2Pairing, Group, Pairing, PairingRange, PrimeField, Ring, Vec};

/// Tate pairing struct holds necessary components for pairing.
/// `pairing` function takes G1 and G2 group elements and output
/// GT target group element.
#[derive(Debug, Clone, Eq, PartialEq, Default, Encode, Decode)]
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Default, Encode, Decode, Copy)]
pub struct TatePairing;

impl Pairing for TatePairing {
Expand Down Expand Up @@ -117,100 +117,3 @@ impl Pairing for TatePairing {
}
}
}

/// Performs a Variable Base Multiscalar Multiplication.
pub fn msm_curve_addtion<P: Pairing>(
points: &[P::G1Affine],
scalars: &[P::ScalarField],
) -> P::G1Projective {
let c = if scalars.len() < 32 {
3
} else {
ln_without_floats(scalars.len()) + 2
};

let num_bits = 255usize;
let fr_one = P::ScalarField::one();

let zero = P::G1Projective::ADDITIVE_IDENTITY;

let window_starts_iter = (0..num_bits).step_by(c);

// Each window is of size `c`.
// We divide up the bits 0..num_bits into windows of size `c`, and
// in parallel process each such window.
let window_sums: Vec<_> = window_starts_iter
.map(|w_start| {
let mut res = zero;
// We don't need the "zero" bucket, so we only have 2^c - 1 buckets
let mut buckets = vec![zero; (1 << c) - 1];
scalars
.iter()
.zip(points)
.filter(|(s, _)| *s != &P::ScalarField::zero())
.for_each(|(&scalar, base)| {
if scalar == fr_one {
// We only process unit scalars once in the first window.
if w_start == 0 {
res += *base;
}
} else {
let mut scalar = scalar.reduce();

// We right-shift by w_start, thus getting rid of the
// lower bits.
scalar.divn(w_start as u32);

// We mod the remaining bits by the window size.
let scalar = scalar.mod_by_window(c);

// If the scalar is non-zero, we update the corresponding
// bucket.
// (Recall that `buckets` doesn't have a zero bucket.)
if scalar != 0 {
buckets[(scalar - 1) as usize] += *base;
}
}
});

let mut running_sum = P::G1Projective::ADDITIVE_IDENTITY;
for b in buckets.into_iter().rev() {
running_sum += b;
res += running_sum;
}

res
})
.collect();

// We store the sum for the lowest window.
let lowest = *window_sums.first().unwrap();
// We're traversing windows from high to low.
let x = window_sums[1..]
.iter()
.rev()
.fold(zero, |mut total, sum_i| {
total += *sum_i;
for _ in 0..c {
total = total.double();
}
total
})
+ lowest;

x
}

fn ln_without_floats(a: usize) -> usize {
// log2(a) * ln(2)
(log2(a) * 69 / 100) as usize
}

fn log2(x: usize) -> u32 {
if x <= 1 {
return 0;
}

let n = x.leading_zeros();
core::mem::size_of::<usize>() as u32 * 8 - n
}
41 changes: 0 additions & 41 deletions tests/multi_scalar_multiplication.rs

This file was deleted.

0 comments on commit 2027711

Please sign in to comment.