Skip to content

Commit

Permalink
impl TryFrom and From[u8; 32] for CompressedRistretto, MontgomeryPoin…
Browse files Browse the repository at this point in the history
…t, CompressedEdwardsY and Scalar
  • Loading branch information
vlopes11 committed Oct 29, 2019
1 parent a7f2c6c commit ccb97b6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/edwards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,12 @@ impl Default for CompressedEdwardsY {
}
}

impl From<[u8; 32]> for CompressedEdwardsY {
fn from(bytes: [u8; 32]) -> Self {
CompressedEdwardsY(bytes)
}
}

impl TryFrom<&[u8]> for CompressedEdwardsY {
type Error = CurveError;

Expand Down
6 changes: 6 additions & 0 deletions src/montgomery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ impl ValidityCheck for MontgomeryPoint {
}
}

impl From<[u8; 32]> for MontgomeryPoint {
fn from(bytes: [u8; 32]) -> Self {
MontgomeryPoint(bytes)
}
}

impl TryFrom<&[u8]> for MontgomeryPoint {
type Error = CurveError;

Expand Down
6 changes: 6 additions & 0 deletions src/ristretto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ impl TryFrom<&[u8]> for CompressedRistretto {
}
}

impl From<[u8; 32]> for CompressedRistretto {
fn from(bytes: [u8; 32]) -> Self {
CompressedRistretto(bytes)
}
}

impl CompressedRistretto {
/// Copy the bytes of this `CompressedRistretto`.
pub fn to_bytes(&self) -> [u8; 32] {
Expand Down
36 changes: 31 additions & 5 deletions src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@

use core::borrow::Borrow;
use core::cmp::{Eq, PartialEq};
use core::convert::TryFrom;
use core::fmt::Debug;
use core::iter::{Product, Sum};
use core::ops::Index;
Expand All @@ -148,6 +149,8 @@ use core::ops::{Add, AddAssign};
use core::ops::{Mul, MulAssign};
use core::ops::{Sub, SubAssign};

use errors::{CurveError, InternalError};

#[allow(unused_imports)]
use prelude::*;

Expand Down Expand Up @@ -242,11 +245,7 @@ impl Scalar {
/// require specific bit-patterns when performing scalar
/// multiplication.
pub fn from_bits(bytes: [u8; 32]) -> Scalar {
let mut s = Scalar{bytes: bytes};
// Ensure that s < 2^255 by masking the high bit
s.bytes[31] &= 0b0111_1111;

s
Scalar::from(bytes)
}
}

Expand Down Expand Up @@ -459,6 +458,33 @@ impl Default for Scalar {
}
}

impl TryFrom<&[u8]> for Scalar {
type Error = CurveError;

fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
if bytes.len() != 32 {
return Err(CurveError(
InternalError::BytesLengthError{name: "Scalar", length: 32}));
}

let mut s_bytes = [0x00u8; 32];
s_bytes.copy_from_slice(bytes);

Ok(Scalar::from(s_bytes))
}
}

impl From<[u8; 32]> for Scalar {
fn from(bytes: [u8; 32]) -> Self {
let mut s = Scalar{bytes: bytes};

// Ensure that s < 2^255 by masking the high bit
s.bytes[31] &= 0b0111_1111;

s
}
}

impl From<u8> for Scalar {
fn from(x: u8) -> Scalar {
let mut s_bytes = [0u8; 32];
Expand Down

0 comments on commit ccb97b6

Please sign in to comment.