Skip to content
Open
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
14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "blstrs"
description = "High performance implementation of BLS12 381"
version = "0.7.1"
version = "0.8.0-pre.0"
authors = ["dignifiedquire <[email protected]>"]
edition = "2018"
license = "MIT/Apache-2.0"
Expand All @@ -16,18 +16,18 @@ rustdoc-args = [ "--html-in-header", "katex-header.html" ]

[dependencies]
blst = { version = "0.3.10", default-features = true }
rand_core = "0.6"
ff = "0.13"
group = { version = "0.13", features = ["tests"] }
pairing_lib = { version = "0.23", package = "pairing" }
subtle = "2.2.1"
rand_core = "0.9"
ff = "0.14.0-pre.0"
group = { version = "0.14.0-pre.0", features = ["tests"] }
pairing_lib = { version = "0.24.0-pre.0", package = "pairing" }
subtle = "2.6"

serde = { version = "1.0", features = ["derive"], optional = true }
ec-gpu = { version = "0.2.0", optional = true }
byte-slice-cast = "1.0.0"

[dev-dependencies]
rand_xorshift = "0.3.0"
rand_xorshift = "0.4.0"
serde_json = "1.0.57"
hex = "0.4.2"

Expand Down
1 change: 0 additions & 1 deletion rust-toolchain

This file was deleted.

33 changes: 16 additions & 17 deletions src/fp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use core::{
ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign},
};
use ff::Field;
use rand_core::RngCore;
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};

use crate::fp2::Fp2;
Expand Down Expand Up @@ -505,22 +504,6 @@ const NUM_BITS: u32 = 381;
const REPR_SHAVE_BITS: usize = 384 - NUM_BITS as usize;

impl Field for Fp {
fn random(mut rng: impl RngCore) -> Self {
loop {
let mut raw = [0u64; 6];
for int in raw.iter_mut() {
*int = rng.next_u64();
}

// Mask away the unused most-significant bits.
raw[5] &= 0xffffffffffffffff >> REPR_SHAVE_BITS;

if let Some(fp) = Fp::from_u64s_le(&raw).into() {
return fp;
}
}
}

const ZERO: Self = ZERO;

// Returns `1 mod p` in Montgomery form `1 * R mod p`;
Expand Down Expand Up @@ -559,6 +542,22 @@ impl Field for Fp {
// ff::helpers::sqrt_ratio_generic(num, div)
unimplemented!()
}

fn try_from_rng<R: rand_core::TryRngCore + ?Sized>(rng: &mut R) -> Result<Self, R::Error> {
loop {
let mut raw = [0u64; 6];
for int in raw.iter_mut() {
*int = rng.try_next_u64()?;
}

// Mask away the unused most-significant bits.
raw[5] &= 0xffffffffffffffff >> REPR_SHAVE_BITS;

if let Some(fp) = Fp::from_u64s_le(&raw).into() {
return Ok(fp);
}
}
}
}

impl Fp {
Expand Down
9 changes: 4 additions & 5 deletions src/fp12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use core::{
};

use ff::Field;
use rand_core::RngCore;
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};

use crate::{fp::Fp, fp2::Fp2, fp6::Fp6};
Expand Down Expand Up @@ -211,10 +210,6 @@ impl_sum!(Fp12);
impl_product!(Fp12);

impl Field for Fp12 {
fn random(mut rng: impl RngCore) -> Self {
Fp12::new(Fp6::random(&mut rng), Fp6::random(&mut rng))
}

const ZERO: Self = Fp12::new(Fp6::ZERO, Fp6::ZERO);

const ONE: Self = Fp12::new(Fp6::ONE, Fp6::ZERO);
Expand Down Expand Up @@ -250,6 +245,10 @@ impl Field for Fp12 {
// ff::helpers::sqrt_ratio_generic(num, div)
unimplemented!()
}

fn try_from_rng<R: rand_core::TryRngCore + ?Sized>(rng: &mut R) -> Result<Self, R::Error> {
Ok(Fp12::new(Fp6::try_from_rng(rng)?, Fp6::try_from_rng(rng)?))
}
}

impl Fp12 {
Expand Down
9 changes: 4 additions & 5 deletions src/fp2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use core::{
ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign},
};
use ff::Field;
use rand_core::RngCore;
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};

use crate::fp::{Fp, FROBENIUS_COEFF_FP2_C1};
Expand Down Expand Up @@ -249,10 +248,6 @@ impl Fp2 {
}

impl Field for Fp2 {
fn random(mut rng: impl RngCore) -> Self {
Fp2::new(Fp::random(&mut rng), Fp::random(&mut rng))
}

const ZERO: Self = Fp2(blst_fp2 {
fp: [Fp::ZERO.0, Fp::ZERO.0],
});
Expand Down Expand Up @@ -294,6 +289,10 @@ impl Field for Fp2 {
// ff::helpers::sqrt_ratio_generic(num, div)
unimplemented!()
}

fn try_from_rng<R: rand_core::TryRngCore + ?Sized>(rng: &mut R) -> Result<Self, R::Error> {
Ok(Fp2::new(Fp::try_from_rng(rng)?, Fp::try_from_rng(rng)?))
}
}

#[cfg(feature = "gpu")]
Expand Down
17 changes: 8 additions & 9 deletions src/fp6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use crate::{
};

use ff::Field;
use rand_core::RngCore;
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};

/// This represents an element $c_0 + c_1 v + c_2 v^2$ of $\mathbb{F}_{p^6} = \mathbb{F}_{p^2} / v^3 - u - 1$.
Expand Down Expand Up @@ -253,14 +252,6 @@ impl_sum!(Fp6);
impl_product!(Fp6);

impl Field for Fp6 {
fn random(mut rng: impl RngCore) -> Self {
Fp6::new(
Fp2::random(&mut rng),
Fp2::random(&mut rng),
Fp2::random(&mut rng),
)
}

const ZERO: Self = Fp6::new(Fp2::ZERO, Fp2::ZERO, Fp2::ZERO);

const ONE: Self = Fp6::new(Fp2::ONE, Fp2::ZERO, Fp2::ZERO);
Expand Down Expand Up @@ -355,6 +346,14 @@ impl Field for Fp6 {
// ff::helpers::sqrt_ratio_generic(num, div)
unimplemented!()
}

fn try_from_rng<R: rand_core::TryRngCore + ?Sized>(rng: &mut R) -> Result<Self, R::Error> {
Ok(Fp6::new(
Fp2::try_from_rng(rng)?,
Fp2::try_from_rng(rng)?,
Fp2::try_from_rng(rng)?,
))
}
}

impl Fp6 {
Expand Down
44 changes: 22 additions & 22 deletions src/g1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use group::{
prime::{PrimeCurve, PrimeCurveAffine, PrimeGroup},
Curve, Group, GroupEncoding, UncompressedEncoding, WnafGroup,
};
use rand_core::RngCore;
use subtle::{Choice, ConditionallySelectable, CtOption};

use crate::{fp::Fp, Bls12, Engine, G2Affine, Gt, PairingCurveAffine, Scalar};
Expand Down Expand Up @@ -651,27 +650,6 @@ impl G1Projective {
impl Group for G1Projective {
type Scalar = Scalar;

fn random(mut rng: impl RngCore) -> Self {
let mut out = blst_p1::default();
let mut msg = [0u8; 64];
rng.fill_bytes(&mut msg);
const DST: [u8; 16] = [0; 16];
const AUG: [u8; 16] = [0; 16];

unsafe {
blst_encode_to_g1(
&mut out,
msg.as_ptr(),
msg.len(),
DST.as_ptr(),
DST.len(),
AUG.as_ptr(),
AUG.len(),
)
};

G1Projective(out)
}

fn identity() -> Self {
G1Projective(blst_p1::default())
Expand All @@ -690,6 +668,28 @@ impl Group for G1Projective {
unsafe { blst_p1_double(&mut double, &self.0) };
G1Projective(double)
}

fn try_from_rng<R: rand_core::TryRngCore + ?Sized>(rng: &mut R) -> Result<Self, R::Error> {
let mut out = blst_p1::default();
let mut msg = [0u8; 64];
rng.try_fill_bytes(&mut msg)?;
const DST: [u8; 16] = [0; 16];
const AUG: [u8; 16] = [0; 16];

unsafe {
blst_encode_to_g1(
&mut out,
msg.as_ptr(),
msg.len(),
DST.as_ptr(),
DST.len(),
AUG.as_ptr(),
AUG.len(),
)
};

Ok(G1Projective(out))
}
}

impl WnafGroup for G1Projective {
Expand Down
43 changes: 21 additions & 22 deletions src/g2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use group::{
prime::{PrimeCurve, PrimeCurveAffine, PrimeGroup},
Curve, Group, GroupEncoding, UncompressedEncoding, WnafGroup,
};
use rand_core::RngCore;
use subtle::{Choice, ConditionallySelectable, CtOption};

use crate::{fp2::Fp2, Bls12, Engine, G1Affine, Gt, PairingCurveAffine, Scalar};
Expand Down Expand Up @@ -618,10 +617,28 @@ impl G2Projective {
impl Group for G2Projective {
type Scalar = Scalar;

fn random(mut rng: impl RngCore) -> Self {
fn identity() -> Self {
G2Projective(blst_p2::default())
}

fn generator() -> Self {
G2Projective(unsafe { *blst_p2_generator() })
}

fn is_identity(&self) -> Choice {
unsafe { Choice::from(blst_p2_is_inf(&self.0) as u8) }
}

fn double(&self) -> Self {
let mut double = blst_p2::default();
unsafe { blst_p2_double(&mut double, &self.0) };
G2Projective(double)
}

fn try_from_rng<R: rand_core::TryRngCore + ?Sized>(rng: &mut R) -> Result<Self, R::Error> {
let mut out = blst_p2::default();
let mut msg = [0u8; 64];
rng.fill_bytes(&mut msg);
rng.try_fill_bytes(&mut msg)?;
const DST: [u8; 16] = [0; 16];
const AUG: [u8; 16] = [0; 16];

Expand All @@ -637,25 +654,7 @@ impl Group for G2Projective {
)
};

G2Projective(out)
}

fn identity() -> Self {
G2Projective(blst_p2::default())
}

fn generator() -> Self {
G2Projective(unsafe { *blst_p2_generator() })
}

fn is_identity(&self) -> Choice {
unsafe { Choice::from(blst_p2_is_inf(&self.0) as u8) }
}

fn double(&self) -> Self {
let mut double = blst_p2::default();
unsafe { blst_p2_double(&mut double, &self.0) };
G2Projective(double)
Ok(G2Projective(out))
}
}

Expand Down
31 changes: 15 additions & 16 deletions src/gt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use core::{
use blst::*;
use ff::Field;
use group::Group;
use rand_core::RngCore;
use subtle::{Choice, ConstantTimeEq};

use crate::{fp::Fp, fp12::Fp12, fp2::Fp2, fp6::Fp6, traits::Compress, Scalar};
Expand Down Expand Up @@ -158,21 +157,7 @@ where

impl Group for Gt {
type Scalar = Scalar;

fn random(mut rng: impl RngCore) -> Self {
loop {
let mut out = Fp12::random(&mut rng);

// Not all elements of Fp12 are elements of the prime-order multiplicative
// subgroup. We run the random element through final_exponentiation to obtain
// a valid element, which requires that it is non-zero.
if !bool::from(out.is_zero()) {
unsafe { blst_final_exp(&mut out.0, &out.0) };
return Gt(out);
}
}
}


/// Returns the group identity, which is $1$.
fn identity() -> Self {
Gt(Fp12::ONE)
Expand Down Expand Up @@ -303,6 +288,20 @@ impl Group for Gt {
fn double(&self) -> Self {
Gt(self.0.square())
}

fn try_from_rng<R: rand_core::TryRngCore + ?Sized>(rng: &mut R) -> Result<Self, R::Error> {
loop {
let mut out = Fp12::try_from_rng(rng)?;

// Not all elements of Fp12 are elements of the prime-order multiplicative
// subgroup. We run the random element through final_exponentiation to obtain
// a valid element, which requires that it is non-zero.
if !bool::from(out.is_zero()) {
unsafe { blst_final_exp(&mut out.0, &out.0) };
return Ok(Gt(out));
}
}
}
}

/// Compressed representation of `Fp12`.
Expand Down
Loading