Skip to content

Commit

Permalink
integrate new APIs from blst
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Aug 25, 2020
1 parent 108181c commit 16caac5
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 83 deletions.
47 changes: 11 additions & 36 deletions src/fp12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use fff::Field;
use crate::{Fp, Fp2, Fp6};

/// This represents an element $c_0 + c_1 w$ of $\mathbb{F}_{p^12} = \mathbb{F}_{p^6} / w^2 - v$.
#[derive(Copy, Clone)]
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct Fp12(pub(crate) blst_fp12);

impl fmt::Debug for Fp12 {
Expand Down Expand Up @@ -65,25 +65,6 @@ impl Default for Fp12 {
}
}

impl Eq for Fp12 {}

impl PartialEq for Fp12 {
#[inline]
fn eq(&self, other: &Self) -> bool {
for (a, b) in self.0.fp6.iter().zip(other.0.fp6.iter()) {
for (a, b) in a.fp2.iter().zip(b.fp2.iter()) {
for (a, b) in a.fp.iter().zip(b.fp.iter()) {
if &a.l != &b.l {
return false;
}
}
}
}

true
}
}

impl<'a> Neg for &'a Fp12 {
type Output = Fp12;

Expand Down Expand Up @@ -452,13 +433,11 @@ impl Field for Fp12 {
}

fn zero() -> Self {
Fp12(blst_fp12::default())
Fp12::new(Fp6::zero(), Fp6::zero())
}

fn one() -> Self {
Fp12(blst_fp12 {
fp6: [Fp6::one().into(), Fp6::zero().into()],
})
Fp12::new(Fp6::one(), Fp6::zero())
}

fn is_zero(&self) -> bool {
Expand Down Expand Up @@ -531,18 +510,14 @@ impl Field for Fp12 {
}

fn inverse(&self) -> Option<Self> {
let mut c0s = self.c0();
c0s.square();
let mut c1s = self.c1();
c1s.square();
c1s.mul_by_nonresidue();
c0s -= &c1s;

c0s.inverse().map(|t| {
Fp12(blst_fp12 {
fp6: [(t * self.c0()).0, (-(t * self.c1())).0],
})
})
if self.is_zero() {
return None;
}
let mut out = blst_fp12::default();

unsafe { blst_fp12_inverse(&mut out, &self.0) }

Some(Fp12(out))
}
}

Expand Down
39 changes: 22 additions & 17 deletions src/g1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ impl groupy::CurveAffine for G1Affine {
}

fn one() -> Self {
G1Affine(unsafe { BLS12_381_G1 })
G1Affine(unsafe { *blst_p1_affine_generator() })
}

fn is_zero(&self) -> bool {
self == &Self::zero()
unsafe { blst_p1_affine_is_inf(&self.0) }
}

fn mul<S: Into<<Self::Scalar as PrimeField>::Repr>>(&self, by: S) -> Self::Projective {
Expand Down Expand Up @@ -262,9 +262,7 @@ impl G1Affine {
/// Returns true if this point is on the curve. This should always return
/// true unless an "unchecked" API was used.
pub fn is_on_curve(&self) -> bool {
let on_curve = unsafe { blst_p1_affine_on_curve(&self.0) };
// FIXME: is_zero check should happen in blst
on_curve || self.is_zero()
unsafe { blst_p1_affine_on_curve(&self.0) }
}

pub fn from_raw_unchecked(x: Fp, y: Fp, _infinity: bool) -> Self {
Expand Down Expand Up @@ -330,8 +328,10 @@ impl Eq for G1Projective {}
impl PartialEq for G1Projective {
#[inline]
fn eq(&self, other: &Self) -> bool {
// TODO: more efficiente method
G1Affine::from(self) == G1Affine::from(other)
let self_is_zero = self.is_zero();
let other_is_zero = other.is_zero();
(self_is_zero && other_is_zero)
|| (!self_is_zero && !other_is_zero && unsafe { blst_p1_is_equal(&self.0, &other.0) })
}
}

Expand Down Expand Up @@ -539,17 +539,15 @@ impl groupy::CurveProjective for G1Projective {
}

fn zero() -> Self {
// The point at infinity is always represented by Z = 0.
G1Projective(blst_p1::default())
}

fn one() -> Self {
G1Affine::one().into()
G1Projective(unsafe { *blst_p1_generator() })
}

// The point at infinity is always represented by Z = 0.
fn is_zero(&self) -> bool {
self == &Self::zero()
unsafe { blst_p1_is_inf(&self.0) }
}

fn is_normalized(&self) -> bool {
Expand All @@ -559,13 +557,12 @@ impl groupy::CurveProjective for G1Projective {
fn batch_normalization<S: std::borrow::BorrowMut<Self>>(v: &mut [S]) {
for el in v {
let el = el.borrow_mut();
let mut out = blst_p1_affine::default();
let mut tmp = blst_p1_affine::default();

unsafe { blst_p1_to_affine(&mut out, &el.0) };

el.0.x = out.x;
el.0.y = out.y;
el.0.z = Fp::one().0;
unsafe {
blst_p1_to_affine(&mut tmp, &el.0);
blst_p1_from_affine(&mut el.0, &tmp);
}
}
}

Expand Down Expand Up @@ -1234,4 +1231,12 @@ mod tests {
use groupy::tests::curve_tests;
curve_tests::<G1Projective>();
}

#[test]
fn test_g1_is_zero() {
assert!(G1Projective::zero().is_zero());
assert!(!G1Projective::one().is_zero());
assert!(G1Affine::zero().is_zero());
assert!(!G1Affine::one().is_zero());
}
}
76 changes: 46 additions & 30 deletions src/g2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ where
where
I: Iterator<Item = T>,
{
use groupy::CurveProjective;
iter.fold(Self::zero(), |acc, item| acc + item.borrow())
}
}
Expand All @@ -154,7 +155,7 @@ impl groupy::CurveAffine for G2Affine {
}

fn is_zero(&self) -> bool {
self == &Self::zero()
unsafe { blst_p2_affine_is_inf(&self.0) }
}

fn mul<S: Into<<Self::Scalar as PrimeField>::Repr>>(&self, by: S) -> Self::Projective {
Expand All @@ -178,7 +179,7 @@ impl G2Affine {

/// Returns a fixed generator of unknown exponent.
pub fn one() -> Self {
G2Affine(unsafe { BLS12_381_G2 })
G2Affine(unsafe { *blst_p2_affine_generator() })
}

/// Determines if this point represents the point at infinity; the additive identity.
Expand Down Expand Up @@ -347,8 +348,12 @@ impl Eq for G2Projective {}
impl PartialEq for G2Projective {
#[inline]
fn eq(&self, other: &Self) -> bool {
// TODO: more efficiente method
G2Affine::from(self) == G2Affine::from(other)
use groupy::CurveProjective;

let self_is_zero = self.is_zero();
let other_is_zero = other.is_zero();
(self_is_zero && other_is_zero)
|| (!self_is_zero && !other_is_zero && unsafe { blst_p2_is_equal(&self.0, &other.0) })
}
}

Expand Down Expand Up @@ -414,21 +419,6 @@ impl_binops_multiplicative!(G2Projective, Scalar);
impl_binops_multiplicative_mixed!(G2Affine, Scalar, G2Projective);

impl G2Projective {
/// Returns the additive identity.
pub fn zero() -> Self {
G2Projective(blst_p2::default())
}

/// Returns a fixed generator of unknown exponent.
pub fn one() -> Self {
G2Affine::one().into()
}

/// Determines if this point represents the point at infinity; the additive identity.
pub fn is_zero(&self) -> bool {
self == &Self::zero()
}

/// Serializes this element into compressed form.
pub fn to_compressed(&self) -> [u8; 48] {
let mut out = [0u8; 48];
Expand Down Expand Up @@ -571,18 +561,15 @@ impl groupy::CurveProjective for G2Projective {
}

fn zero() -> Self {
// The point at infinity is always represented by Z = 0.
G2Projective(blst_p2::default())
}

fn one() -> Self {
G2Affine::one().into()
G2Projective(unsafe { *blst_p2_generator() })
}

// The point at infinity is always represented by
// Z = 0.
fn is_zero(&self) -> bool {
self == &Self::zero()
unsafe { blst_p2_is_inf(&self.0) }
}

fn is_normalized(&self) -> bool {
Expand All @@ -592,13 +579,12 @@ impl groupy::CurveProjective for G2Projective {
fn batch_normalization<S: std::borrow::BorrowMut<Self>>(v: &mut [S]) {
for el in v {
let el = el.borrow_mut();
let mut out = blst_p2_affine::default();

unsafe { blst_p2_to_affine(&mut out, &el.0) };
let mut tmp = blst_p2_affine::default();

el.0.x = out.x;
el.0.y = out.y;
el.0.z = Fp2::one().0;
unsafe {
blst_p2_to_affine(&mut tmp, &el.0);
blst_p2_from_affine(&mut el.0, &tmp);
};
}
}

Expand Down Expand Up @@ -1144,9 +1130,39 @@ mod tests {
);
}

#[test]
fn test_affine_point_equality() {
let a = G2Affine::one();
let b = G2Affine::zero();

assert!(a == a);
assert!(b == b);
assert!(a != b);
assert!(b != a);
}

#[test]
fn test_projective_point_equality() {
let a = G2Projective::one();
let b = G2Projective::zero();

assert!(a == a);
assert!(b == b);
assert!(a != b);
assert!(b != a);
}

#[test]
fn groupy_g2_curve_tests() {
use groupy::tests::curve_tests;
curve_tests::<G2Projective>();
}

#[test]
fn test_g2_is_zero() {
assert!(G2Projective::zero().is_zero());
assert!(!G2Projective::one().is_zero());
assert!(G2Affine::zero().is_zero());
assert!(!G2Affine::one().is_zero());
}
}

0 comments on commit 16caac5

Please sign in to comment.