From 563ec0db3cb184ec31cf37663fe0ba4fa0d0f837 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Wed, 13 Mar 2019 17:22:25 +0300 Subject: [PATCH] from_slice for u512 --- src/arith.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/arith.rs b/src/arith.rs index 1bd9079..7c09437 100644 --- a/src/arith.rs +++ b/src/arith.rs @@ -80,6 +80,22 @@ impl U512 { U512(res) } + pub fn from_slice(s: &[u8]) -> Result { + if s.len() != 64 { + return Err(Error::InvalidLength { + expected: 32, + actual: s.len(), + }); + } + + let mut n = [0; 4]; + for (l, i) in (0..4).rev().zip((0..4).map(|i| i * 16)) { + n[l] = BigEndian::read_u128(&s[i..]); + } + + Ok(U512(n)) + } + /// Get a random U512 pub fn random(rng: &mut R) -> U512 { U512(rng.gen())