Skip to content

Commit

Permalink
renamed workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
weijiekoh committed Aug 3, 2023
1 parent b5a0934 commit 1beb000
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
File renamed without changes.
12 changes: 12 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> {
// 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);
}
Expand All @@ -33,7 +37,9 @@ pub fn split_biguint(a: BigUint) -> Vec<u8> {
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()]);
Expand All @@ -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<u32> {
let mut limbs: Vec<u32> = Vec::with_capacity(16);
for c in split_biguint(p.clone()).into_iter().chunks(4).into_iter() {
Expand All @@ -50,9 +57,11 @@ pub fn bigint_to_limbs(p: &BigUint) -> Vec<u32> {
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<BigUint>) -> Vec<u8> {
let mut input_as_bytes: Vec<Vec<u8>> = Vec::with_capacity(vals.len());
for i in 0..vals.len() {
Expand All @@ -63,7 +72,10 @@ pub fn bigints_to_bytes(vals: Vec<BigUint>) -> Vec<u8> {
}


/// 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<u32>) -> Vec<BigUint> {
assert!(b.len() % 16 == 0);
let chunks: Vec<Vec<u32>> = b
.into_iter().chunks(16)
.into_iter().map(|c| c.into_iter().collect())
Expand Down

0 comments on commit 1beb000

Please sign in to comment.