commit
156366ea42
20
src/fq.rs
20
src/fq.rs
|
@ -15,7 +15,7 @@ pub struct Fq(pub(crate) [u64; 4]);
|
||||||
|
|
||||||
impl fmt::Debug for Fq {
|
impl fmt::Debug for Fq {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
let tmp = self.into_bytes();
|
let tmp = self.to_bytes();
|
||||||
write!(f, "0x")?;
|
write!(f, "0x")?;
|
||||||
for &b in tmp.iter().rev() {
|
for &b in tmp.iter().rev() {
|
||||||
write!(f, "{:02x}", b)?;
|
write!(f, "{:02x}", b)?;
|
||||||
|
@ -228,7 +228,7 @@ impl Fq {
|
||||||
|
|
||||||
/// Converts an element of `Fq` into a byte representation in
|
/// Converts an element of `Fq` into a byte representation in
|
||||||
/// little-endian byte order.
|
/// little-endian byte order.
|
||||||
pub fn into_bytes(&self) -> [u8; 32] {
|
pub fn to_bytes(&self) -> [u8; 32] {
|
||||||
// Turn into canonical form by computing
|
// Turn into canonical form by computing
|
||||||
// (a.R) / R = a
|
// (a.R) / R = a
|
||||||
let tmp = Fq::montgomery_reduce(self.0[0], self.0[1], self.0[2], self.0[3], 0, 0, 0, 0);
|
let tmp = Fq::montgomery_reduce(self.0[0], self.0[1], self.0[2], self.0[3], 0, 0, 0, 0);
|
||||||
|
@ -604,7 +604,7 @@ impl Fq {
|
||||||
|
|
||||||
impl<'a> From<&'a Fq> for [u8; 32] {
|
impl<'a> From<&'a Fq> for [u8; 32] {
|
||||||
fn from(value: &'a Fq) -> [u8; 32] {
|
fn from(value: &'a Fq) -> [u8; 32] {
|
||||||
value.into_bytes()
|
value.to_bytes()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -651,9 +651,9 @@ fn test_equality() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_into_bytes() {
|
fn test_to_bytes() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Fq::zero().into_bytes(),
|
Fq::zero().to_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, 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
|
||||||
|
@ -661,7 +661,7 @@ fn test_into_bytes() {
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Fq::one().into_bytes(),
|
Fq::one().to_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,
|
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
|
0, 0, 0
|
||||||
|
@ -669,7 +669,7 @@ fn test_into_bytes() {
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
R2.into_bytes(),
|
R2.to_bytes(),
|
||||||
[
|
[
|
||||||
254, 255, 255, 255, 1, 0, 0, 0, 2, 72, 3, 0, 250, 183, 132, 88, 245, 79, 188, 236, 239,
|
254, 255, 255, 255, 1, 0, 0, 0, 2, 72, 3, 0, 250, 183, 132, 88, 245, 79, 188, 236, 239,
|
||||||
79, 140, 153, 111, 5, 197, 172, 89, 177, 36, 24
|
79, 140, 153, 111, 5, 197, 172, 89, 177, 36, 24
|
||||||
|
@ -677,7 +677,7 @@ fn test_into_bytes() {
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
(-&Fq::one()).into_bytes(),
|
(-&Fq::one()).to_bytes(),
|
||||||
[
|
[
|
||||||
0, 0, 0, 0, 255, 255, 255, 255, 254, 91, 254, 255, 2, 164, 189, 83, 5, 216, 161, 9, 8,
|
0, 0, 0, 0, 255, 255, 255, 255, 254, 91, 254, 255, 2, 164, 189, 83, 5, 216, 161, 9, 8,
|
||||||
216, 57, 51, 72, 125, 157, 41, 83, 167, 237, 115
|
216, 57, 51, 72, 125, 157, 41, 83, 167, 237, 115
|
||||||
|
@ -914,7 +914,7 @@ fn test_multiplication() {
|
||||||
|
|
||||||
let mut tmp2 = Fq::zero();
|
let mut tmp2 = Fq::zero();
|
||||||
for b in cur
|
for b in cur
|
||||||
.into_bytes()
|
.to_bytes()
|
||||||
.iter()
|
.iter()
|
||||||
.rev()
|
.rev()
|
||||||
.flat_map(|byte| (0..8).rev().map(move |i| ((byte >> i) & 1u8) == 1u8))
|
.flat_map(|byte| (0..8).rev().map(move |i| ((byte >> i) & 1u8) == 1u8))
|
||||||
|
@ -943,7 +943,7 @@ fn test_squaring() {
|
||||||
|
|
||||||
let mut tmp2 = Fq::zero();
|
let mut tmp2 = Fq::zero();
|
||||||
for b in cur
|
for b in cur
|
||||||
.into_bytes()
|
.to_bytes()
|
||||||
.iter()
|
.iter()
|
||||||
.rev()
|
.rev()
|
||||||
.flat_map(|byte| (0..8).rev().map(move |i| ((byte >> i) & 1u8) == 1u8))
|
.flat_map(|byte| (0..8).rev().map(move |i| ((byte >> i) & 1u8) == 1u8))
|
||||||
|
|
20
src/fr.rs
20
src/fr.rs
|
@ -15,7 +15,7 @@ pub struct Fr(pub(crate) [u64; 4]);
|
||||||
|
|
||||||
impl fmt::Debug for Fr {
|
impl fmt::Debug for Fr {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
let tmp = self.into_bytes();
|
let tmp = self.to_bytes();
|
||||||
write!(f, "0x")?;
|
write!(f, "0x")?;
|
||||||
for &b in tmp.iter().rev() {
|
for &b in tmp.iter().rev() {
|
||||||
write!(f, "{:02x}", b)?;
|
write!(f, "{:02x}", b)?;
|
||||||
|
@ -217,7 +217,7 @@ impl Fr {
|
||||||
|
|
||||||
/// Converts an element of `Fr` into a byte representation in
|
/// Converts an element of `Fr` into a byte representation in
|
||||||
/// little-endian byte order.
|
/// little-endian byte order.
|
||||||
pub fn into_bytes(&self) -> [u8; 32] {
|
pub fn to_bytes(&self) -> [u8; 32] {
|
||||||
// Turn into canonical form by computing
|
// Turn into canonical form by computing
|
||||||
// (a.R) / R = a
|
// (a.R) / R = a
|
||||||
let tmp = Fr::montgomery_reduce(self.0[0], self.0[1], self.0[2], self.0[3], 0, 0, 0, 0);
|
let tmp = Fr::montgomery_reduce(self.0[0], self.0[1], self.0[2], self.0[3], 0, 0, 0, 0);
|
||||||
|
@ -557,7 +557,7 @@ impl Fr {
|
||||||
|
|
||||||
impl<'a> From<&'a Fr> for [u8; 32] {
|
impl<'a> From<&'a Fr> for [u8; 32] {
|
||||||
fn from(value: &'a Fr) -> [u8; 32] {
|
fn from(value: &'a Fr) -> [u8; 32] {
|
||||||
value.into_bytes()
|
value.to_bytes()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -604,9 +604,9 @@ fn test_equality() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_into_bytes() {
|
fn test_to_bytes() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Fr::zero().into_bytes(),
|
Fr::zero().to_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, 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
|
||||||
|
@ -614,7 +614,7 @@ fn test_into_bytes() {
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Fr::one().into_bytes(),
|
Fr::one().to_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,
|
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
|
0, 0, 0
|
||||||
|
@ -622,7 +622,7 @@ fn test_into_bytes() {
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
R2.into_bytes(),
|
R2.to_bytes(),
|
||||||
[
|
[
|
||||||
217, 7, 150, 185, 179, 11, 248, 37, 80, 231, 182, 102, 47, 214, 21, 243, 244, 20, 136,
|
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
|
235, 238, 20, 37, 147, 198, 85, 145, 71, 111, 252, 166, 9
|
||||||
|
@ -630,7 +630,7 @@ fn test_into_bytes() {
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
(-&Fr::one()).into_bytes(),
|
(-&Fr::one()).to_bytes(),
|
||||||
[
|
[
|
||||||
182, 44, 247, 214, 94, 14, 151, 208, 130, 16, 200, 204, 147, 32, 104, 166, 0, 59, 52,
|
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
|
1, 1, 59, 103, 6, 169, 175, 51, 101, 234, 180, 125, 14
|
||||||
|
@ -869,7 +869,7 @@ fn test_multiplication() {
|
||||||
|
|
||||||
let mut tmp2 = Fr::zero();
|
let mut tmp2 = Fr::zero();
|
||||||
for b in cur
|
for b in cur
|
||||||
.into_bytes()
|
.to_bytes()
|
||||||
.iter()
|
.iter()
|
||||||
.rev()
|
.rev()
|
||||||
.flat_map(|byte| (0..8).rev().map(move |i| ((byte >> i) & 1u8) == 1u8))
|
.flat_map(|byte| (0..8).rev().map(move |i| ((byte >> i) & 1u8) == 1u8))
|
||||||
|
@ -898,7 +898,7 @@ fn test_squaring() {
|
||||||
|
|
||||||
let mut tmp2 = Fr::zero();
|
let mut tmp2 = Fr::zero();
|
||||||
for b in cur
|
for b in cur
|
||||||
.into_bytes()
|
.to_bytes()
|
||||||
.iter()
|
.iter()
|
||||||
.rev()
|
.rev()
|
||||||
.flat_map(|byte| (0..8).rev().map(move |i| ((byte >> i) & 1u8) == 1u8))
|
.flat_map(|byte| (0..8).rev().map(move |i| ((byte >> i) & 1u8) == 1u8))
|
||||||
|
|
19
src/lib.rs
19
src/lib.rs
|
@ -256,7 +256,7 @@ impl<'a, 'b> Mul<&'b Fr> for &'a AffineNielsPoint {
|
||||||
type Output = ExtendedPoint;
|
type Output = ExtendedPoint;
|
||||||
|
|
||||||
fn mul(self, other: &'b Fr) -> ExtendedPoint {
|
fn mul(self, other: &'b Fr) -> ExtendedPoint {
|
||||||
self.multiply(&other.into_bytes())
|
self.multiply(&other.to_bytes())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -340,7 +340,7 @@ impl<'a, 'b> Mul<&'b Fr> for &'a ExtendedNielsPoint {
|
||||||
type Output = ExtendedPoint;
|
type Output = ExtendedPoint;
|
||||||
|
|
||||||
fn mul(self, other: &'b Fr) -> ExtendedPoint {
|
fn mul(self, other: &'b Fr) -> ExtendedPoint {
|
||||||
self.multiply(&other.into_bytes())
|
self.multiply(&other.to_bytes())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -398,9 +398,9 @@ impl AffinePoint {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Converts this element into its byte representation.
|
/// Converts this element into its byte representation.
|
||||||
pub fn into_bytes(&self) -> [u8; 32] {
|
pub fn to_bytes(&self) -> [u8; 32] {
|
||||||
let mut tmp = self.v.into_bytes();
|
let mut tmp = self.v.to_bytes();
|
||||||
let u = self.u.into_bytes();
|
let u = self.u.to_bytes();
|
||||||
|
|
||||||
// Encode the sign of the u-coordinate in the most
|
// Encode the sign of the u-coordinate in the most
|
||||||
// significant bit.
|
// significant bit.
|
||||||
|
@ -438,7 +438,7 @@ impl AffinePoint {
|
||||||
.sqrt()
|
.sqrt()
|
||||||
.and_then(|u| {
|
.and_then(|u| {
|
||||||
// Fix the sign of `u` if necessary
|
// Fix the sign of `u` if necessary
|
||||||
let flip_sign = Choice::from((u.into_bytes()[0] ^ sign) & 1);
|
let flip_sign = Choice::from((u.to_bytes()[0] ^ sign) & 1);
|
||||||
let u_negated = -u;
|
let u_negated = -u;
|
||||||
let final_u = Fq::conditional_select(&u, &u_negated, flip_sign);
|
let final_u = Fq::conditional_select(&u, &u_negated, flip_sign);
|
||||||
|
|
||||||
|
@ -659,7 +659,7 @@ impl<'a, 'b> Mul<&'b Fr> for &'a ExtendedPoint {
|
||||||
type Output = ExtendedPoint;
|
type Output = ExtendedPoint;
|
||||||
|
|
||||||
fn mul(self, other: &'b Fr) -> ExtendedPoint {
|
fn mul(self, other: &'b Fr) -> ExtendedPoint {
|
||||||
self.multiply(&other.into_bytes())
|
self.multiply(&other.to_bytes())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -829,7 +829,8 @@ impl CompletedPoint {
|
||||||
///
|
///
|
||||||
/// The resulting T coordinate is utvz/zt = uv, and so
|
/// The resulting T coordinate is utvz/zt = uv, and so
|
||||||
/// T1 = u, T2 = v, without loss of generality.
|
/// T1 = u, T2 = v, without loss of generality.
|
||||||
fn into_extended(&self) -> ExtendedPoint {
|
#[inline]
|
||||||
|
fn into_extended(self) -> ExtendedPoint {
|
||||||
ExtendedPoint {
|
ExtendedPoint {
|
||||||
u: &self.u * &self.t,
|
u: &self.u * &self.t,
|
||||||
v: &self.v * &self.z,
|
v: &self.v * &self.z,
|
||||||
|
@ -1314,7 +1315,7 @@ fn test_serialization_consistency() {
|
||||||
for expected_serialized in v {
|
for expected_serialized in v {
|
||||||
assert!(p.is_on_curve_vartime());
|
assert!(p.is_on_curve_vartime());
|
||||||
let affine = AffinePoint::from(p);
|
let affine = AffinePoint::from(p);
|
||||||
let serialized = affine.into_bytes();
|
let serialized = affine.to_bytes();
|
||||||
let deserialized = AffinePoint::from_bytes(serialized).unwrap();
|
let deserialized = AffinePoint::from_bytes(serialized).unwrap();
|
||||||
assert_eq!(affine, deserialized);
|
assert_eq!(affine, deserialized);
|
||||||
assert_eq!(expected_serialized, serialized);
|
assert_eq!(expected_serialized, serialized);
|
||||||
|
|
|
@ -8,7 +8,7 @@ fn test_to_and_from_bytes() {
|
||||||
let mut rng = new_rng();
|
let mut rng = new_rng();
|
||||||
for _ in 0..NUM_BLACK_BOX_CHECKS {
|
for _ in 0..NUM_BLACK_BOX_CHECKS {
|
||||||
let a = Fq::new_random(&mut rng);
|
let a = Fq::new_random(&mut rng);
|
||||||
assert_eq!(a, Fq::from_bytes(&Fq::into_bytes(&a)).unwrap());
|
assert_eq!(a, Fq::from_bytes(&Fq::to_bytes(&a)).unwrap());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ fn test_to_and_from_bytes() {
|
||||||
let mut rng = new_rng();
|
let mut rng = new_rng();
|
||||||
for _ in 0..NUM_BLACK_BOX_CHECKS {
|
for _ in 0..NUM_BLACK_BOX_CHECKS {
|
||||||
let a = Fr::new_random(&mut rng);
|
let a = Fr::new_random(&mut rng);
|
||||||
assert_eq!(a, Fr::from_bytes(&Fr::into_bytes(&a)).unwrap());
|
assert_eq!(a, Fr::from_bytes(&Fr::to_bytes(&a)).unwrap());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue