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

refactor: affine representation of BLS group elements #55

Draft
wants to merge 1 commit into
base: master
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
blst = "0.3.11"
blst = { git = "https://github.com/jacobkaufmann/blst.git", branch = "feat-rust-affines-from-affines" }
hex = { version = "0.4.3", optional = true }
rand = { version = "0.8.5", optional = true }
serde = { version = "1.0.189", features = ["derive"], optional = true }
Expand Down
101 changes: 47 additions & 54 deletions src/bls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use blst::{
blst_bendian_from_scalar, blst_final_exp, blst_fp12, blst_fp12_is_one, blst_fp12_mul, blst_fr,
blst_fr_add, blst_fr_cneg, blst_fr_eucl_inverse, blst_fr_from_scalar, blst_fr_from_uint64,
blst_fr_lshift, blst_fr_mul, blst_fr_rshift, blst_fr_sub, blst_lendian_from_scalar,
blst_miller_loop, blst_p1, blst_p1_add, blst_p1_affine, blst_p1_affine_in_g1, blst_p1_cneg,
blst_p1_compress, blst_p1_from_affine, blst_p1_mult, blst_p1_to_affine, blst_p1_uncompress,
blst_p2, blst_p2_add, blst_p2_affine, blst_p2_affine_in_g2, blst_p2_cneg, blst_p2_compress,
blst_p2_from_affine, blst_p2_mult, blst_p2_to_affine, blst_p2_uncompress, blst_scalar,
blst_scalar_fr_check, blst_scalar_from_bendian, blst_scalar_from_fr, blst_sha256,
blst_uint64_from_fr, p1_affines, p2_affines, BLS12_381_G1, BLS12_381_G2, BLS12_381_NEG_G1,
BLS12_381_NEG_G2, BLST_ERROR,
blst_miller_loop, blst_p1, blst_p1_add, blst_p1_affine, blst_p1_affine_compress,
blst_p1_affine_in_g1, blst_p1_cneg, blst_p1_from_affine, blst_p1_mult, blst_p1_to_affine,
blst_p1_uncompress, blst_p2, blst_p2_add, blst_p2_affine, blst_p2_affine_compress,
blst_p2_affine_in_g2, blst_p2_cneg, blst_p2_from_affine, blst_p2_mult, blst_p2_to_affine,
blst_p2_uncompress, blst_scalar, blst_scalar_fr_check, blst_scalar_from_bendian,
blst_scalar_from_fr, blst_sha256, blst_uint64_from_fr, p1_affines, p2_affines, BLS12_381_G1,
BLS12_381_G2, BLS12_381_NEG_G1, BLS12_381_NEG_G2, BLST_ERROR,
};

#[derive(Clone, Copy, Debug)]
Expand Down Expand Up @@ -380,26 +380,18 @@ macro_rules! impl_group {
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[repr(transparent)]
pub struct $p {
element: $gelt,
element: $affine,
}

impl $p {
// TODO: make available as `const`
pub fn generator() -> Self {
let mut out = MaybeUninit::<$gelt>::uninit();
unsafe {
$from_affine(out.as_mut_ptr(), &$gen);
Self::from(out.assume_init())
}
unsafe { $gen.into() }
}

// TODO: make available as `const`
pub fn neg_generator() -> Self {
let mut out = MaybeUninit::<$gelt>::uninit();
unsafe {
$from_affine(out.as_mut_ptr(), &$neg_gen);
Self::from(out.assume_init())
}
unsafe { $neg_gen.into() }
}

#[allow(dead_code)]
Expand All @@ -421,9 +413,9 @@ macro_rules! impl_group {

let points = unsafe {
// NOTE: we can perform the cast given `repr(transparent)` for the struct.
slice::from_raw_parts(points.as_ref().as_ptr() as *const $gelt, n)
slice::from_raw_parts(points.as_ref().as_ptr() as *const $affine, n)
};
let points = $affines::from(points);
let points = $affines::new(points);

let scalar_iter = scalars.as_ref().iter().take(n);
let mut scalars = Vec::with_capacity(n * Fr::BYTES);
Expand All @@ -432,19 +424,28 @@ macro_rules! impl_group {
}

let lincomb = points.mult(&scalars, 255);
lincomb.into()
}
}

Self { element: lincomb }
impl From<$affine> for $p {
fn from(element: $affine) -> Self {
Self { element }
}
}

impl From<$gelt> for $p {
fn from(element: $gelt) -> Self {
Self { element }
let mut out = MaybeUninit::<$affine>::uninit();
unsafe {
$to_affine(out.as_mut_ptr(), &element);
out.assume_init().into()
}
}
}

impl AsRef<$gelt> for $p {
fn as_ref(&self) -> &$gelt {
impl AsRef<$affine> for $p {
fn as_ref(&self) -> &$affine {
&self.element
}
}
Expand All @@ -453,12 +454,14 @@ macro_rules! impl_group {
type Output = Self;

fn add(self, rhs: Self) -> Self::Output {
let mut tmpl = MaybeUninit::<$gelt>::uninit();
let mut tmpr = MaybeUninit::<$gelt>::uninit();
let mut out = MaybeUninit::<$gelt>::uninit();
unsafe {
$add(out.as_mut_ptr(), &self.element, &rhs.element);
Self {
element: out.assume_init(),
}
$from_affine(tmpl.as_mut_ptr(), &self.element);
$from_affine(tmpr.as_mut_ptr(), &rhs.element);
$add(out.as_mut_ptr(), tmpl.as_ptr(), tmpr.as_ptr());
out.assume_init().into()
}
}
}
Expand All @@ -467,8 +470,11 @@ macro_rules! impl_group {
type Output = Self;

fn neg(mut self) -> Self::Output {
let mut tmp = MaybeUninit::<$gelt>::uninit();
unsafe {
$neg(&mut self.element, true);
$from_affine(tmp.as_mut_ptr(), &self.element);
$neg(tmp.as_mut_ptr(), true);
$to_affine(&mut self.element, tmp.as_ptr());
}
self
}
Expand All @@ -482,8 +488,9 @@ macro_rules! impl_group {
let mut out = MaybeUninit::<$gelt>::uninit();
unsafe {
blst_scalar_from_fr(&mut scalar, &rhs.element);
$mul(out.as_mut_ptr(), &self.element, scalar.b.as_ptr(), 255);
$p::from(out.assume_init())
$from_affine(out.as_mut_ptr(), &self.element);
$mul(out.as_mut_ptr(), out.as_ptr(), scalar.b.as_ptr(), 255);
out.assume_init().into()
}
}
}
Expand All @@ -506,27 +513,23 @@ macro_rules! impl_group {
type Error = ECGroupError;

fn decompress(compressed: impl AsRef<[u8]>) -> Result<Self, Self::Error> {
let mut affine = MaybeUninit::<$affine>::uninit();
let mut out = MaybeUninit::<$gelt>::uninit();
let mut out = MaybeUninit::<$affine>::uninit();
unsafe {
// NOTE: uncompress performs a curve check but not a subgroup check. if that changes,
// then we should encounter `unreachable` for `BLST_POINT_NOT_IN_GROUP` in tests.
match $uncompress(affine.as_mut_ptr(), compressed.as_ref().as_ptr()) {
match $uncompress(out.as_mut_ptr(), compressed.as_ref().as_ptr()) {
BLST_ERROR::BLST_SUCCESS => {}
BLST_ERROR::BLST_BAD_ENCODING => return Err(ECGroupError::InvalidEncoding),
BLST_ERROR::BLST_POINT_NOT_ON_CURVE => {
return Err(ECGroupError::NotOnCurve)
}
other => unreachable!("{other:?}"),
}
if !$affine_in_group(affine.as_ptr()) {
if !$affine_in_group(out.as_ptr()) {
return Err(ECGroupError::NotInGroup);
}

$from_affine(out.as_mut_ptr(), affine.as_ptr());
Ok(Self {
element: out.assume_init(),
})
Ok(out.assume_init().into())
}
}
}
Expand All @@ -546,7 +549,7 @@ impl_group!(
blst_p1_cneg,
blst_p1_mult,
blst_p1_affine_in_g1,
blst_p1_compress,
blst_p1_affine_compress,
blst_p1_uncompress,
48
);
Expand All @@ -564,32 +567,22 @@ impl_group!(
blst_p2_cneg,
blst_p2_mult,
blst_p2_affine_in_g2,
blst_p2_compress,
blst_p2_affine_compress,
blst_p2_uncompress,
96
);

pub fn verify_pairings((a1, a2): (P1, P2), (b1, b2): (P1, P2)) -> bool {
let mut a1_neg_affine = MaybeUninit::<blst_p1_affine>::uninit();
let mut a2_affine = MaybeUninit::<blst_p2_affine>::uninit();

let mut b1_affine = MaybeUninit::<blst_p1_affine>::uninit();
let mut b2_affine = MaybeUninit::<blst_p2_affine>::uninit();
let neg_a1 = -a1;

let mut e1 = MaybeUninit::<blst_fp12>::uninit();
let mut e2 = MaybeUninit::<blst_fp12>::uninit();
let mut prod = MaybeUninit::<blst_fp12>::uninit();
let mut exp = MaybeUninit::<blst_fp12>::uninit();

unsafe {
blst_p1_to_affine(a1_neg_affine.as_mut_ptr(), &a1.neg().element);
blst_p2_to_affine(a2_affine.as_mut_ptr(), &a2.element);

blst_p1_to_affine(b1_affine.as_mut_ptr(), &b1.element);
blst_p2_to_affine(b2_affine.as_mut_ptr(), &b2.element);

blst_miller_loop(e1.as_mut_ptr(), a2_affine.as_ptr(), a1_neg_affine.as_ptr());
blst_miller_loop(e2.as_mut_ptr(), b2_affine.as_ptr(), b1_affine.as_ptr());
blst_miller_loop(e1.as_mut_ptr(), &a2.element, &neg_a1.element);
blst_miller_loop(e2.as_mut_ptr(), &b2.element, &b1.element);

blst_fp12_mul(prod.as_mut_ptr(), e1.as_ptr(), e2.as_ptr());
blst_final_exp(exp.as_mut_ptr(), prod.as_ptr());
Expand Down
Loading