Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(stdlib): Make Hasher trait generic over input type #7138

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 29 additions & 30 deletions noir_stdlib/src/hash/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ pub fn poseidon2_permutation<let N: u32>(_input: [Field; N], _state_length: u32)
pub trait Hash {
fn hash<H>(self, state: &mut H)
where
H: Hasher;
H: Hasher<Field>;
}

// docs:start:derive_hash
Expand All @@ -152,35 +152,34 @@ comptime fn derive_hash(s: StructDefinition) -> Quoted {
// docs:end:derive_hash

// Hasher trait shall be implemented by algorithms to provide hash-agnostic means.
// TODO: consider making the types generic here ([u8], [Field], etc.)
pub trait Hasher {
pub trait Hasher<T: Hash> {
fn finish(self) -> Field;

fn write(&mut self, input: Field);
fn write(&mut self, input: T);
}

// BuildHasher is a factory trait, responsible for production of specific Hasher.
pub trait BuildHasher<H>
pub trait BuildHasher<H, T: Hash>
where
H: Hasher,
H: Hasher<T>,
{
fn build_hasher(self) -> H;
}

pub struct BuildHasherDefault<H>;
pub struct BuildHasherDefault<H, T: Hash>;

impl<H> BuildHasher<H> for BuildHasherDefault<H>
impl<H, T: Hash> BuildHasher<H, T> for BuildHasherDefault<H, T>
where
H: Hasher + Default,
H: Hasher<T> + Default,
{
fn build_hasher(_self: Self) -> H {
H::default()
}
}

impl<H> Default for BuildHasherDefault<H>
impl<H, T: Hash> Default for BuildHasherDefault<H, T>
where
H: Hasher + Default,
H: Hasher<T> + Default,
{
fn default() -> Self {
BuildHasherDefault {}
Expand All @@ -190,7 +189,7 @@ where
impl Hash for Field {
fn hash<H>(self, state: &mut H)
where
H: Hasher,
H: Hasher<Field>,
{
H::write(state, self);
}
Expand All @@ -199,7 +198,7 @@ impl Hash for Field {
impl Hash for u1 {
fn hash<H>(self, state: &mut H)
where
H: Hasher,
H: Hasher<Field>,
{
H::write(state, self as Field);
}
Expand All @@ -208,7 +207,7 @@ impl Hash for u1 {
impl Hash for u8 {
fn hash<H>(self, state: &mut H)
where
H: Hasher,
H: Hasher<Field>,
{
H::write(state, self as Field);
}
Expand All @@ -217,7 +216,7 @@ impl Hash for u8 {
impl Hash for u16 {
fn hash<H>(self, state: &mut H)
where
H: Hasher,
H: Hasher<Field>,
{
H::write(state, self as Field);
}
Expand All @@ -226,7 +225,7 @@ impl Hash for u16 {
impl Hash for u32 {
fn hash<H>(self, state: &mut H)
where
H: Hasher,
H: Hasher<Field>,
{
H::write(state, self as Field);
}
Expand All @@ -235,7 +234,7 @@ impl Hash for u32 {
impl Hash for u64 {
fn hash<H>(self, state: &mut H)
where
H: Hasher,
H: Hasher<Field>,
{
H::write(state, self as Field);
}
Expand All @@ -244,7 +243,7 @@ impl Hash for u64 {
impl Hash for i8 {
fn hash<H>(self, state: &mut H)
where
H: Hasher,
H: Hasher<Field>,
{
H::write(state, self as Field);
}
Expand All @@ -253,7 +252,7 @@ impl Hash for i8 {
impl Hash for i16 {
fn hash<H>(self, state: &mut H)
where
H: Hasher,
H: Hasher<Field>,
{
H::write(state, self as Field);
}
Expand All @@ -262,7 +261,7 @@ impl Hash for i16 {
impl Hash for i32 {
fn hash<H>(self, state: &mut H)
where
H: Hasher,
H: Hasher<Field>,
{
H::write(state, self as Field);
}
Expand All @@ -271,7 +270,7 @@ impl Hash for i32 {
impl Hash for i64 {
fn hash<H>(self, state: &mut H)
where
H: Hasher,
H: Hasher<Field>,
{
H::write(state, self as Field);
}
Expand All @@ -280,7 +279,7 @@ impl Hash for i64 {
impl Hash for bool {
fn hash<H>(self, state: &mut H)
where
H: Hasher,
H: Hasher<Field>,
{
H::write(state, self as Field);
}
Expand All @@ -289,14 +288,14 @@ impl Hash for bool {
impl Hash for () {
fn hash<H>(_self: Self, _state: &mut H)
where
H: Hasher,
H: Hasher<Field>,
{}
}

impl Hash for U128 {
fn hash<H>(self, state: &mut H)
where
H: Hasher,
H: Hasher<Field>,
{
H::write(state, self.lo as Field);
H::write(state, self.hi as Field);
Expand All @@ -309,7 +308,7 @@ where
{
fn hash<H>(self, state: &mut H)
where
H: Hasher,
H: Hasher<Field>,
{
for elem in self {
elem.hash(state);
Expand All @@ -323,7 +322,7 @@ where
{
fn hash<H>(self, state: &mut H)
where
H: Hasher,
H: Hasher<Field>,
{
self.len().hash(state);
for elem in self {
Expand All @@ -339,7 +338,7 @@ where
{
fn hash<H>(self, state: &mut H)
where
H: Hasher,
H: Hasher<Field>,
{
self.0.hash(state);
self.1.hash(state);
Expand All @@ -354,7 +353,7 @@ where
{
fn hash<H>(self, state: &mut H)
where
H: Hasher,
H: Hasher<Field>,
{
self.0.hash(state);
self.1.hash(state);
Expand All @@ -371,7 +370,7 @@ where
{
fn hash<H>(self, state: &mut H)
where
H: Hasher,
H: Hasher<Field>,
{
self.0.hash(state);
self.1.hash(state);
Expand All @@ -390,7 +389,7 @@ where
{
fn hash<H>(self, state: &mut H)
where
H: Hasher,
H: Hasher<Field>,
{
self.0.hash(state);
self.1.hash(state);
Expand Down
Loading