use crate::{
crh::{CRHScheme, TwoToOneCRHScheme},
Error,
};
#[cfg(not(feature = "std"))]
use ark_std::vec::Vec;
use ark_std::{borrow::Borrow, rand::Rng};
use sha2::digest::Digest;
pub use sha2::{digest, Sha256};
#[cfg(feature = "constraints")]
pub mod constraints;
impl CRHScheme for Sha256 {
type Input = [u8];
type Output = Vec<u8>;
type Parameters = ();
fn setup<R: Rng>(_rng: &mut R) -> Result<Self::Parameters, Error> {
Ok(())
}
fn evaluate<T: Borrow<Self::Input>>(
_parameters: &Self::Parameters,
input: T,
) -> Result<Self::Output, Error> {
Ok(Sha256::digest(input.borrow()).to_vec())
}
}
impl TwoToOneCRHScheme for Sha256 {
type Input = [u8];
type Output = Vec<u8>;
type Parameters = ();
fn setup<R: Rng>(_rng: &mut R) -> Result<Self::Parameters, Error> {
Ok(())
}
fn evaluate<T: Borrow<Self::Input>>(
_parameters: &Self::Parameters,
left_input: T,
right_input: T,
) -> Result<Self::Output, Error> {
let left_input = left_input.borrow();
let right_input = right_input.borrow();
let mut h = Sha256::default();
h.update(left_input);
h.update(right_input);
Ok(h.finalize().to_vec())
}
fn compress<T: Borrow<Self::Output>>(
parameters: &Self::Parameters,
left_input: T,
right_input: T,
) -> Result<Self::Output, Error> {
<Self as TwoToOneCRHScheme>::evaluate(
parameters,
left_input.borrow().as_slice(),
right_input.borrow().as_slice(),
)
}
}