Skip to content

Commit

Permalink
Add missing conversion method
Browse files Browse the repository at this point in the history
  • Loading branch information
cronokirby committed Feb 12, 2024
1 parent f8c185e commit 4589ef4
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "decaf377"
version = "0.8.0"
authors = ["Henry de Valence <hdevalence@hdevalence.ca>", "redshiftzero <jen@penumbralabs.xyz>"]
description = "A prime-order group designed for use in SNARKs over BLS12-377"
edition = "2018"
edition = "2021"
license = "MIT OR Apache-2.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
79 changes: 79 additions & 0 deletions src/min_curve/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,85 @@ mod test {
}
}

impl From<&Element> for Encoding {
fn from(point: &Element) -> Self {
point.vartime_compress()
}
}

impl From<Element> for Encoding {
fn from(point: Element) -> Self {
point.vartime_compress()
}
}

impl TryFrom<&[u8]> for Encoding {
type Error = EncodingError;

fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
if bytes.len() == 32 {
let mut arr = [0u8; 32];
arr.copy_from_slice(&bytes[0..32]);
Ok(Encoding(arr))
} else {
Err(EncodingError::InvalidSliceLength)
}
}
}

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

impl From<Encoding> for [u8; 32] {
fn from(enc: Encoding) -> [u8; 32] {
enc.0
}
}

impl TryFrom<&Encoding> for Element {
type Error = EncodingError;
fn try_from(bytes: &Encoding) -> Result<Self, Self::Error> {
bytes.vartime_decompress()
}
}

impl TryFrom<Encoding> for Element {
type Error = EncodingError;
fn try_from(bytes: Encoding) -> Result<Self, Self::Error> {
bytes.vartime_decompress()
}
}

impl TryFrom<&[u8]> for Element {
type Error = EncodingError;

fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
let b: [u8; 32] = bytes
.try_into()
.map_err(|_| EncodingError::InvalidSliceLength)?;

Encoding(b).try_into()
}
}

impl TryFrom<[u8; 32]> for Element {
type Error = EncodingError;

fn try_from(bytes: [u8; 32]) -> Result<Self, Self::Error> {
let encoding = Encoding(bytes);
encoding.try_into()
}
}

impl From<Element> for [u8; 32] {
fn from(enc: Element) -> [u8; 32] {
enc.vartime_compress().0
}
}

#[cfg(all(test, feature = "arkworks"))]
mod proptests {
use super::*;
Expand Down

0 comments on commit 4589ef4

Please sign in to comment.