Skip to content

Commit

Permalink
fix: Use little endian for the ReprFp type
Browse files Browse the repository at this point in the history
  • Loading branch information
CPerezz committed Sep 29, 2023
1 parent 01daad4 commit fedb948
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/fp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ impl Field for Fp {
}
}

/// This struct represents the [`Fp`] struct serialized as an array of 48 bytes
/// in Little Endian.
#[derive(Debug, Clone, Copy)]
pub struct ReprFp(pub(crate) [u8; 48]);

Expand All @@ -335,6 +337,20 @@ impl AsRef<[u8]> for ReprFp {
}
}

impl AsRef<[u8; 48]> for ReprFp {
fn as_ref(&self) -> &[u8; 48] {
&self.0
}
}

impl From<[u8; 48]> for ReprFp {
fn from(value: [u8; 48]) -> Self {
// This uses little endian and so, assumes the array passed in is
// in that format.
Self(value)
}
}

impl Default for ReprFp {
fn default() -> Self {
Self([0u8; 48])
Expand All @@ -345,11 +361,15 @@ impl PrimeField for Fp {
type Repr = ReprFp;

fn from_repr(r: Self::Repr) -> CtOption<Self> {
// This uses little endian and so, assumes the array passed in is
// in that format.
Self::from_bytes(&r.0)
}

fn to_repr(&self) -> Self::Repr {
ReprFp(self.to_bytes())
let mut le_bytes = self.to_bytes();
le_bytes.reverse();
ReprFp(le_bytes)
}

fn is_odd(&self) -> Choice {
Expand Down

0 comments on commit fedb948

Please sign in to comment.