mirror of
https://github.com/vacp2p/zerokit.git
synced 2026-01-09 13:47:58 -05:00
Add hash_be and poseidon_hash_be functions
This commit is contained in:
@@ -7,8 +7,8 @@ use crate::protocol::{
|
||||
serialize_proof_values, serialize_witness, verify_proof,
|
||||
};
|
||||
use crate::utils::{
|
||||
bytes_le_to_fr, bytes_le_to_vec_fr, bytes_le_to_vec_u8, fr_byte_size, fr_to_bytes_be,
|
||||
fr_to_bytes_le, vec_fr_to_bytes_le, vec_u8_to_bytes_le,
|
||||
bytes_be_to_vec_fr, bytes_le_to_fr, bytes_le_to_vec_fr, bytes_le_to_vec_u8, fr_byte_size,
|
||||
fr_to_bytes_be, fr_to_bytes_le, vec_fr_to_bytes_le, vec_u8_to_bytes_le,
|
||||
};
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use {
|
||||
@@ -1493,6 +1493,20 @@ pub fn hash<R: Read, W: Write>(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// same as hash function but in BE format
|
||||
pub fn hash_be<R: Read, W: Write>(
|
||||
mut input_data: R,
|
||||
mut output_data: W,
|
||||
) -> Result<(), std::io::Error> {
|
||||
let mut serialized: Vec<u8> = Vec::new();
|
||||
input_data.read_to_end(&mut serialized)?;
|
||||
|
||||
let hash = hash_to_field(&serialized);
|
||||
output_data.write_all(&fr_to_bytes_be(&hash))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Hashes a set of elements to a single element in the working prime field, using Poseidon.
|
||||
///
|
||||
/// The result is computed as the Poseidon Hash of the input signal.
|
||||
@@ -1529,3 +1543,18 @@ pub fn poseidon_hash<R: Read, W: Write>(
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// same as poseidon_hash function but in BE format. Note that input is expected in BE format too.
|
||||
pub fn poseidon_hash_be<R: Read, W: Write>(
|
||||
mut input_data: R,
|
||||
mut output_data: W,
|
||||
) -> Result<(), RLNError> {
|
||||
let mut serialized: Vec<u8> = Vec::new();
|
||||
input_data.read_to_end(&mut serialized)?;
|
||||
|
||||
let (inputs, _) = bytes_be_to_vec_fr(&serialized)?;
|
||||
let hash = utils_poseidon_hash(inputs.as_ref());
|
||||
output_data.write_all(&fr_to_bytes_be(&hash))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -138,6 +138,24 @@ pub fn bytes_le_to_vec_fr(input: &[u8]) -> Result<(Vec<Fr>, usize), ConversionEr
|
||||
Ok((res, read))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn bytes_be_to_vec_fr(input: &[u8]) -> Result<(Vec<Fr>, usize), ConversionError> {
|
||||
let mut read: usize = 0;
|
||||
let mut res: Vec<Fr> = Vec::new();
|
||||
|
||||
let len = usize::try_from(u64::from_be_bytes(input[0..8].try_into()?))?;
|
||||
read += 8;
|
||||
|
||||
let el_size = fr_byte_size();
|
||||
for i in 0..len {
|
||||
let (curr_el, _) = bytes_be_to_fr(&input[8 + el_size * i..8 + el_size * (i + 1)]);
|
||||
res.push(curr_el);
|
||||
read += el_size;
|
||||
}
|
||||
|
||||
Ok((res, read))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn bytes_le_to_vec_usize(input: &[u8]) -> Result<Vec<usize>, ConversionError> {
|
||||
let nof_elem = usize::try_from(u64::from_le_bytes(input[0..8].try_into()?))?;
|
||||
|
||||
Reference in New Issue
Block a user