From 4589ef44c377db180f0786a15b03e5026d0e9886 Mon Sep 17 00:00:00 2001 From: Lucas Meier Date: Mon, 12 Feb 2024 15:26:39 -0800 Subject: [PATCH] Add missing conversion method --- Cargo.toml | 2 +- src/min_curve/element.rs | 79 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 95e1cb0..39de8da 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "decaf377" version = "0.8.0" authors = ["Henry de Valence ", "redshiftzero "] 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 diff --git a/src/min_curve/element.rs b/src/min_curve/element.rs index 77f7dbd..b6e2fa8 100644 --- a/src/min_curve/element.rs +++ b/src/min_curve/element.rs @@ -383,6 +383,85 @@ mod test { } } +impl From<&Element> for Encoding { + fn from(point: &Element) -> Self { + point.vartime_compress() + } +} + +impl From for Encoding { + fn from(point: Element) -> Self { + point.vartime_compress() + } +} + +impl TryFrom<&[u8]> for Encoding { + type Error = EncodingError; + + fn try_from(bytes: &[u8]) -> Result { + 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 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 { + bytes.vartime_decompress() + } +} + +impl TryFrom for Element { + type Error = EncodingError; + fn try_from(bytes: Encoding) -> Result { + bytes.vartime_decompress() + } +} + +impl TryFrom<&[u8]> for Element { + type Error = EncodingError; + + fn try_from(bytes: &[u8]) -> Result { + 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 { + let encoding = Encoding(bytes); + encoding.try_into() + } +} + +impl From for [u8; 32] { + fn from(enc: Element) -> [u8; 32] { + enc.vartime_compress().0 + } +} + #[cfg(all(test, feature = "arkworks"))] mod proptests { use super::*;