From 1beb000de64789f29b0a5da5dc9e44b2097caccc Mon Sep 17 00:00:00 2001 From: Koh Wei Jie Date: Thu, 3 Aug 2023 16:24:09 +0800 Subject: [PATCH] renamed workflow --- .github/workflows/{npm-gulp.yml => publish.yml} | 0 src/utils.rs | 12 ++++++++++++ 2 files changed, 12 insertions(+) rename .github/workflows/{npm-gulp.yml => publish.yml} (100%) diff --git a/.github/workflows/npm-gulp.yml b/.github/workflows/publish.yml similarity index 100% rename from .github/workflows/npm-gulp.yml rename to .github/workflows/publish.yml diff --git a/src/utils.rs b/src/utils.rs index c3b717d..79635a8 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -8,10 +8,14 @@ pub fn split_u32(a: u32) -> [u32; 2] { [a_0, a_1] } +/// Convert a 32-byte BigUint into a bytearray of length 64, such that two zero-bytes are inserted +/// between each pair of bytes. pub fn split_biguint(a: BigUint) -> Vec { + // Convert the input to bytes let mut a_bytes = a.to_bytes_le().to_vec(); assert!(a_bytes.len() <= 32); + // Pad the byte vector with 0s such that the final length is 32 while a_bytes.len() < 32 { a_bytes.push(0u8); } @@ -33,7 +37,9 @@ pub fn split_biguint(a: BigUint) -> Vec { result } +/// Converts an array of 16 limbs to a BigUint. pub fn limbs_to_bigint256(limbs: &[u32]) -> BigUint { + assert!(limbs.len() == 16); let mut res = BigUint::zero(); for (i, limb) in limbs.iter().enumerate() { res += BigUint::from_slice(&[2]).pow((i * 16).try_into().unwrap()) * BigUint::from_slice(&[limb.clone()]); @@ -42,6 +48,7 @@ pub fn limbs_to_bigint256(limbs: &[u32]) -> BigUint { res } +/// Converts a BigUint to an array of 16 limbs. pub fn bigint_to_limbs(p: &BigUint) -> Vec { let mut limbs: Vec = Vec::with_capacity(16); for c in split_biguint(p.clone()).into_iter().chunks(4).into_iter() { @@ -50,9 +57,11 @@ pub fn bigint_to_limbs(p: &BigUint) -> Vec { limbs.push(limb); } + assert!(limbs.len() == 16); limbs } +/// Converts a vector of BigUints into a vector of bytes using split_biguint(). pub fn bigints_to_bytes(vals: Vec) -> Vec { let mut input_as_bytes: Vec> = Vec::with_capacity(vals.len()); for i in 0..vals.len() { @@ -63,7 +72,10 @@ pub fn bigints_to_bytes(vals: Vec) -> Vec { } +/// Converts a vector of u32s into BigUints. The input vector should have a length that is a +/// multiple of 16. pub fn u32s_to_bigints(b: Vec) -> Vec { + assert!(b.len() % 16 == 0); let chunks: Vec> = b .into_iter().chunks(16) .into_iter().map(|c| c.into_iter().collect())