Skip to content

Commit

Permalink
Derive de/ser
Browse files Browse the repository at this point in the history
  • Loading branch information
iquerejeta committed May 5, 2023
1 parent a756020 commit abf0604
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 45 deletions.
6 changes: 5 additions & 1 deletion src/bls12_381/fp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ use std::io::Write;
use std::ops::Deref;
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};

#[cfg(feature = "derive_serde")]
use serde::{Deserialize, Serialize};

use crate::util::{adc, mac, sbb};
use crate::{
impl_add_binop_specify_output, impl_binops_additive, impl_binops_additive_specify_output,
Expand All @@ -20,6 +23,7 @@ use crate::{
// integers in little-endian order. `Fp` values are always in
// Montgomery form; i.e., Scalar(a) = aR mod p, with R = 2^384.
#[derive(Copy, Clone)]
#[cfg_attr(feature = "derive_serde", derive(Serialize, Deserialize))]
pub struct Fp(pub(crate) [u64; 6]);

impl Ord for Fp {
Expand Down Expand Up @@ -872,7 +876,7 @@ impl crate::serde::SerdeObject for Fp {
reader.read_exact(&mut buf).unwrap();
u64::from_le_bytes(buf)
});
Self( inner )
Self(inner)
}
fn read_raw<R: std::io::Read>(reader: &mut R) -> std::io::Result<Self> {
let mut inner = [0u64; 6];
Expand Down
4 changes: 4 additions & 0 deletions src/bls12_381/fp2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ use std::convert::TryInto;
use std::ops::{BitAnd, Deref};
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};

#[cfg(feature = "derive_serde")]
use serde::{Deserialize, Serialize};

use crate::bls12_381::fp::Fp;
use crate::{
impl_add_binop_specify_output, impl_binops_additive, impl_binops_additive_specify_output,
impl_binops_multiplicative, impl_binops_multiplicative_mixed, impl_sub_binop_specify_output,
};

#[derive(Copy, Clone)]
#[cfg_attr(feature = "derive_serde", derive(Serialize, Deserialize))]
pub struct Fp2 {
pub c0: Fp,
pub c1: Fp,
Expand Down
4 changes: 2 additions & 2 deletions src/bls12_381/g1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use ff::PrimeField;
use ff::WithSmallOrderMulGroup;
use group::cofactor::CofactorGroup;

#[cfg(feature = "alloc")]
use group::WnafGroup;
use pasta_curves::arithmetic::{Coordinates, CurveAffine, CurveExt};
#[cfg(feature = "derive_serde")]
use serde::{Deserialize, Serialize};

use crate::bls12_381::fp::Fp;
use crate::bls12_381::Scalar;
Expand Down
4 changes: 2 additions & 2 deletions src/bls12_381/g2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use group::{prime::PrimeCurveAffine, Curve, Group, GroupEncoding};
use rand_core::RngCore;
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};

#[cfg(feature = "alloc")]
use group::WnafGroup;
use pasta_curves::arithmetic::{Coordinates, CurveAffine, CurveExt};
#[cfg(feature = "derive_serde")]
use serde::{Deserialize, Serialize};

use ff::WithSmallOrderMulGroup;
use ff::{Field, PrimeField};
Expand Down
44 changes: 22 additions & 22 deletions src/jubjub/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,12 @@ impl PartialEq for ExtendedPoint {
}

impl<T> Sum<T> for ExtendedPoint
where
T: Borrow<ExtendedPoint>,
where
T: Borrow<ExtendedPoint>,
{
fn sum<I>(iter: I) -> Self
where
I: Iterator<Item = T>,
where
I: Iterator<Item = T>,
{
iter.fold(Self::identity(), |acc, item| acc + item.borrow())
}
Expand Down Expand Up @@ -811,7 +811,7 @@ impl ExtendedPoint {
z: vv_minus_uu,
t: zz2 - vv_minus_uu,
}
.into_extended()
.into_extended()
}

#[inline]
Expand Down Expand Up @@ -912,7 +912,7 @@ impl<'a, 'b> Add<&'b ExtendedNielsPoint> for &'a ExtendedPoint {
z: d + c,
t: d - c,
}
.into_extended()
.into_extended()
}
}

Expand All @@ -932,7 +932,7 @@ impl<'a, 'b> Sub<&'b ExtendedNielsPoint> for &'a ExtendedPoint {
z: d - c,
t: d + c,
}
.into_extended()
.into_extended()
}
}

Expand Down Expand Up @@ -960,7 +960,7 @@ impl<'a, 'b> Add<&'b AffineNielsPoint> for &'a ExtendedPoint {
z: d + c,
t: d - c,
}
.into_extended()
.into_extended()
}
}

Expand All @@ -980,7 +980,7 @@ impl<'a, 'b> Sub<&'b AffineNielsPoint> for &'a ExtendedPoint {
z: d - c,
t: d + c,
}
.into_extended()
.into_extended()
}
}

Expand Down Expand Up @@ -1176,12 +1176,12 @@ impl SubgroupPoint {
}

impl<T> Sum<T> for SubgroupPoint
where
T: Borrow<SubgroupPoint>,
where
T: Borrow<SubgroupPoint>,
{
fn sum<I>(iter: I) -> Self
where
I: Iterator<Item = T>,
where
I: Iterator<Item = T>,
{
iter.fold(Self::identity(), |acc, item| acc + item.borrow())
}
Expand Down Expand Up @@ -1267,11 +1267,11 @@ impl Group for ExtendedPoint {
let v2 = v.square();
let p = ((v2 - Fq::one())
* ((Fq::one() + EDWARDS_D * v2).invert().unwrap_or(Fq::zero())))
.sqrt()
.map(|u| AffinePoint {
u: if flip_sign { -u } else { u },
v,
});
.sqrt()
.map(|u| AffinePoint {
u: if flip_sign { -u } else { u },
v,
});

if p.is_some().into() {
let p = p.unwrap().to_curve();
Expand Down Expand Up @@ -1566,7 +1566,7 @@ fn test_assoc() {
0x3793_de18_2f9f_b1d2,
]),
})
.mul_by_cofactor();
.mul_by_cofactor();
assert!(p.is_on_curve_vartime());

assert_eq!(
Expand All @@ -1591,7 +1591,7 @@ fn test_batch_normalize() {
0x3793_de18_2f9f_b1d2,
]),
})
.mul_by_cofactor();
.mul_by_cofactor();

let mut v = vec![];
for _ in 0..10 {
Expand Down Expand Up @@ -1837,7 +1837,7 @@ fn test_mul_consistency() {
0x3793_de18_2f9f_b1d2,
]),
})
.mul_by_cofactor();
.mul_by_cofactor();
assert_eq!(p * c, (p * a) * b);

// Test Mul implemented on ExtendedNielsPoint
Expand Down Expand Up @@ -1981,4 +1981,4 @@ fn test_zip_216() {
assert_eq!(b, &encoded);
}
}
}
}
34 changes: 17 additions & 17 deletions src/jubjub/fr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,24 +176,24 @@ impl_binops_additive!(Fr, Fr);
impl_binops_multiplicative!(Fr, Fr);

impl<T> core::iter::Sum<T> for Fr
where
T: core::borrow::Borrow<Fr>,
where
T: core::borrow::Borrow<Fr>,
{
fn sum<I>(iter: I) -> Self
where
I: Iterator<Item = T>,
where
I: Iterator<Item = T>,
{
iter.fold(Self::zero(), |acc, item| acc + item.borrow())
}
}

impl<T> core::iter::Product<T> for Fr
where
T: core::borrow::Borrow<Fr>,
where
T: core::borrow::Borrow<Fr>,
{
fn product<I>(iter: I) -> Self
where
I: Iterator<Item = T>,
where
I: Iterator<Item = T>,
{
iter.fold(Self::one(), |acc, item| acc * item.borrow())
}
Expand Down Expand Up @@ -882,7 +882,7 @@ fn test_from_bytes() {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0
])
.unwrap(),
.unwrap(),
Fr::zero()
);

Expand All @@ -891,7 +891,7 @@ fn test_from_bytes() {
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0
])
.unwrap(),
.unwrap(),
Fr::one()
);

Expand All @@ -900,7 +900,7 @@ fn test_from_bytes() {
217, 7, 150, 185, 179, 11, 248, 37, 80, 231, 182, 102, 47, 214, 21, 243, 244, 20, 136,
235, 238, 20, 37, 147, 198, 85, 145, 71, 111, 252, 166, 9
])
.unwrap(),
.unwrap(),
R2
);

Expand All @@ -910,7 +910,7 @@ fn test_from_bytes() {
182, 44, 247, 214, 94, 14, 151, 208, 130, 16, 200, 204, 147, 32, 104, 166, 0, 59, 52,
1, 1, 59, 103, 6, 169, 175, 51, 101, 234, 180, 125, 14
])
.is_some()
.is_some()
));

// modulus is invalid
Expand All @@ -919,7 +919,7 @@ fn test_from_bytes() {
183, 44, 247, 214, 94, 14, 151, 208, 130, 16, 200, 204, 147, 32, 104, 166, 0, 59, 52,
1, 1, 59, 103, 6, 169, 175, 51, 101, 234, 180, 125, 14
])
.is_none()
.is_none()
));

// Anything larger than the modulus is invalid
Expand All @@ -928,23 +928,23 @@ fn test_from_bytes() {
184, 44, 247, 214, 94, 14, 151, 208, 130, 16, 200, 204, 147, 32, 104, 166, 0, 59, 52,
1, 1, 59, 103, 6, 169, 175, 51, 101, 234, 180, 125, 14
])
.is_none()
.is_none()
));

assert!(bool::from(
Fr::from_bytes(&[
183, 44, 247, 214, 94, 14, 151, 208, 130, 16, 200, 204, 147, 32, 104, 166, 0, 59, 52,
1, 1, 59, 104, 6, 169, 175, 51, 101, 234, 180, 125, 14
])
.is_none()
.is_none()
));

assert!(bool::from(
Fr::from_bytes(&[
183, 44, 247, 214, 94, 14, 151, 208, 130, 16, 200, 204, 147, 32, 104, 166, 0, 59, 52,
1, 1, 59, 103, 6, 169, 175, 51, 101, 234, 180, 125, 15
])
.is_none()
.is_none()
));
}

Expand Down Expand Up @@ -1229,4 +1229,4 @@ fn test_from_raw() {
assert_eq!(Fr::from_raw(MODULUS.0), Fr::zero());

assert_eq!(Fr::from_raw([1, 0, 0, 0]), R);
}
}
2 changes: 1 addition & 1 deletion src/jubjub/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ mod curve;
mod fr;

pub use curve::*;
pub use fr::*;
pub use fr::*;

0 comments on commit abf0604

Please sign in to comment.