From 2cef5fcecdefed6a78c41d2ecfa032e5cb815c3a Mon Sep 17 00:00:00 2001 From: Henry de Valence Date: Wed, 22 Feb 2017 22:08:18 -0800 Subject: [PATCH] Add CompressedEdwardsY::as_bytes --- src/curve.rs | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/src/curve.rs b/src/curve.rs index 34085d4..ec9c093 100644 --- a/src/curve.rs +++ b/src/curve.rs @@ -79,7 +79,7 @@ use core::fmt::Debug; use core::iter::Iterator; -use core::ops::{Add, Sub, Neg, Index}; +use core::ops::{Add, Sub, Neg}; use core::cmp::{PartialEq, Eq}; use constants; @@ -106,21 +106,18 @@ pub struct CompressedEdwardsY(pub [u8; 32]); impl Debug for CompressedEdwardsY { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "CompressedPoint: {:?}", &self.0[..]) - } -} - -impl Index for CompressedEdwardsY { - type Output = u8; - - fn index<'a>(&'a self, _index: usize) -> &'a u8 { - let ret: &'a u8 = &(self.0[_index]); - ret + write!(f, "CompressedPoint: {:?}", self.as_bytes()) } } impl CompressedEdwardsY { /// View this `CompressedEdwardsY` as an array of bytes. + pub fn as_bytes<'a>(&'a self) -> &'a [u8;32] { + &self.0 + } + + /// Copy this `CompressedEdwardsY` to an array of bytes. + /// XXX is this useful? pub fn to_bytes(&self) -> [u8;32] { self.0 } @@ -130,7 +127,7 @@ impl CompressedEdwardsY { /// Returns `None` if the input is not the `y`-coordinate of a /// curve point. pub fn decompress(&self) -> Option { // FromBytes() - let Y = FieldElement::from_bytes(&self.0); + let Y = FieldElement::from_bytes(self.as_bytes()); let Z = FieldElement::one(); let YY = Y.square(); let u = &YY - &Z; // u = y²-1 @@ -140,7 +137,7 @@ impl CompressedEdwardsY { if is_nonzero_square != 1u8 { return None; } // Flip the sign of X if it's not correct - let compressed_sign_bit = self[31] >> 7; + let compressed_sign_bit = self.as_bytes()[31] >> 7; let current_sign_bit = X.is_negative_ed25519(); X.conditional_negate(current_sign_bit ^ compressed_sign_bit); @@ -317,7 +314,8 @@ impl CTAssignable for AffineNielsPoint { impl CTEq for ExtendedPoint { fn ct_eq(&self, other: &ExtendedPoint) -> u8 { - arrays_equal_ct(&self.compress().0, &other.compress().0) + arrays_equal_ct( self.compress().as_bytes(), + other.compress().as_bytes()) } } @@ -977,7 +975,7 @@ mod test { /// Test sign handling in decompression #[test] fn test_decompression_sign_handling() { - let mut m_bp_bytes: [u8;32] = BASE_CMPRSSD.to_bytes().clone(); + let mut m_bp_bytes: [u8;32] = BASE_CMPRSSD.as_bytes().clone(); // Set the high bit of the last byte to flip the sign m_bp_bytes[31] |= 1 << 7; let m_bp = CompressedEdwardsY(m_bp_bytes).decompress().unwrap();