Add CompressedEdwardsY::as_bytes

This commit is contained in:
Henry de Valence 2017-02-22 22:08:18 -08:00
parent e6b7192d5e
commit 2cef5fcecd

View file

@ -79,7 +79,7 @@
use core::fmt::Debug; use core::fmt::Debug;
use core::iter::Iterator; use core::iter::Iterator;
use core::ops::{Add, Sub, Neg, Index}; use core::ops::{Add, Sub, Neg};
use core::cmp::{PartialEq, Eq}; use core::cmp::{PartialEq, Eq};
use constants; use constants;
@ -106,21 +106,18 @@ pub struct CompressedEdwardsY(pub [u8; 32]);
impl Debug for CompressedEdwardsY { impl Debug for CompressedEdwardsY {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "CompressedPoint: {:?}", &self.0[..]) write!(f, "CompressedPoint: {:?}", self.as_bytes())
}
}
impl Index<usize> for CompressedEdwardsY {
type Output = u8;
fn index<'a>(&'a self, _index: usize) -> &'a u8 {
let ret: &'a u8 = &(self.0[_index]);
ret
} }
} }
impl CompressedEdwardsY { impl CompressedEdwardsY {
/// View this `CompressedEdwardsY` as an array of bytes. /// 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] { pub fn to_bytes(&self) -> [u8;32] {
self.0 self.0
} }
@ -130,7 +127,7 @@ impl CompressedEdwardsY {
/// Returns `None` if the input is not the `y`-coordinate of a /// Returns `None` if the input is not the `y`-coordinate of a
/// curve point. /// curve point.
pub fn decompress(&self) -> Option<ExtendedPoint> { // FromBytes() pub fn decompress(&self) -> Option<ExtendedPoint> { // FromBytes()
let Y = FieldElement::from_bytes(&self.0); let Y = FieldElement::from_bytes(self.as_bytes());
let Z = FieldElement::one(); let Z = FieldElement::one();
let YY = Y.square(); let YY = Y.square();
let u = &YY - &Z; // u = y²-1 let u = &YY - &Z; // u = y²-1
@ -140,7 +137,7 @@ impl CompressedEdwardsY {
if is_nonzero_square != 1u8 { return None; } if is_nonzero_square != 1u8 { return None; }
// Flip the sign of X if it's not correct // 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(); let current_sign_bit = X.is_negative_ed25519();
X.conditional_negate(current_sign_bit ^ compressed_sign_bit); X.conditional_negate(current_sign_bit ^ compressed_sign_bit);
@ -317,7 +314,8 @@ impl CTAssignable for AffineNielsPoint {
impl CTEq for ExtendedPoint { impl CTEq for ExtendedPoint {
fn ct_eq(&self, other: &ExtendedPoint) -> u8 { 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 sign handling in decompression
#[test] #[test]
fn test_decompression_sign_handling() { 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 // Set the high bit of the last byte to flip the sign
m_bp_bytes[31] |= 1 << 7; m_bp_bytes[31] |= 1 << 7;
let m_bp = CompressedEdwardsY(m_bp_bytes).decompress().unwrap(); let m_bp = CompressedEdwardsY(m_bp_bytes).decompress().unwrap();