From 78cf02fd7b35e4a2398fedd96f68c92953badea7 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Wed, 22 Mar 2017 02:53:41 +0300 Subject: [PATCH] more exposed --- src/lib.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 17b86b4..353cbab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,6 +27,12 @@ impl Fr { pub fn interpret(buf: &[u8; 64]) -> Fr { Fr(fields::Fr::interpret(buf)) } + pub fn from_slice(slice: &[u8]) -> Result { + arith::U256::from_slice(slice) + .map_err(|_| FieldError::InvalidSliceLength) // todo: maybe more sensful error handling + .and_then(|x| fields::Fr::new(x).ok_or(FieldError::NotMember)) + .map(|x| Fr(x)) + } } impl Add for Fr { @@ -53,6 +59,12 @@ impl Mul for Fr { fn mul(self, other: Fr) -> Fr { Fr(self.0 * other.0) } } +#[derive(Debug)] +pub enum FieldError { + InvalidSliceLength, + NotMember, +} + #[derive(Copy, Clone, PartialEq, Eq, RustcDecodable, RustcEncodable)] #[repr(C)] pub struct Fq(fields::Fq); @@ -68,6 +80,12 @@ impl Fq { pub fn interpret(buf: &[u8; 64]) -> Fq { Fq(fields::Fq::interpret(buf)) } + pub fn from_slice(slice: &[u8]) -> Result { + arith::U256::from_slice(slice) + .map_err(|_| FieldError::InvalidSliceLength) // todo: maybe more sensful error handling + .and_then(|x| fields::Fq::new(x).ok_or(FieldError::NotMember)) + .map(|x| Fq(x)) + } } impl Add for Fq {