Skip to content

Commit 57dd91b

Browse files
authored
chore: Add more Hash impls to stdlib (#4470)
# Description ## Problem\* Resolves a TODO in #4241 ## Summary\* Adds the remaining Hash impls for primitive types in the stdlib ## Additional Context I've marked this as "no documentation needed" but we should probably document somewhere that these types are hashable. Where should this go? The existing "hash methods" page doesn't seem to fit. ## Documentation\* Check one: - [x] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[Exceptional Case]** Documentation to be submitted in a separate PR. # PR Checklist\* - [ ] I have tested the changes locally. - [ ] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings.
1 parent d4213a0 commit 57dd91b

File tree

1 file changed

+97
-4
lines changed

1 file changed

+97
-4
lines changed

noir_stdlib/src/hash.nr

+97-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod poseidon2;
44
mod pedersen;
55

66
use crate::default::Default;
7+
use crate::uint128::U128;
78

89
#[foreign(sha256)]
910
// docs:start:sha256
@@ -120,10 +121,102 @@ where
120121
}
121122
}
122123

123-
// TODO: add implementations for the remainder of primitive types.
124-
impl Hash for Field{
124+
impl Hash for Field {
125125
fn hash<H>(self, state: &mut H) where H: Hasher{
126-
let input: [Field] = [self];
127-
H::write(state, input);
126+
H::write(state, [self]);
127+
}
128+
}
129+
130+
impl Hash for u8 {
131+
fn hash<H>(self, state: &mut H) where H: Hasher{
132+
H::write(state, [self as Field]);
133+
}
134+
}
135+
136+
impl Hash for u32 {
137+
fn hash<H>(self, state: &mut H) where H: Hasher{
138+
H::write(state, [self as Field]);
139+
}
140+
}
141+
142+
impl Hash for u64 {
143+
fn hash<H>(self, state: &mut H) where H: Hasher{
144+
H::write(state, [self as Field]);
145+
}
146+
}
147+
148+
impl Hash for i8 {
149+
fn hash<H>(self, state: &mut H) where H: Hasher{
150+
H::write(state, [self as Field]);
151+
}
152+
}
153+
154+
impl Hash for i32 {
155+
fn hash<H>(self, state: &mut H) where H: Hasher{
156+
H::write(state, [self as Field]);
157+
}
158+
}
159+
160+
impl Hash for i64 {
161+
fn hash<H>(self, state: &mut H) where H: Hasher{
162+
H::write(state, [self as Field]);
163+
}
164+
}
165+
166+
impl Hash for bool {
167+
fn hash<H>(self, state: &mut H) where H: Hasher{
168+
H::write(state, [self as Field]);
169+
}
170+
}
171+
172+
impl Hash for () {
173+
fn hash<H>(_self: Self, _state: &mut H) where H: Hasher {}
174+
}
175+
176+
impl Hash for U128 {
177+
fn hash<H>(self, state: &mut H) where H: Hasher{
178+
H::write(state, [self.lo as Field, self.hi as Field]);
179+
}
180+
}
181+
182+
impl<T, N> Hash for [T; N] where T: Hash {
183+
fn hash<H>(self, state: &mut H) where H: Hasher{
184+
for elem in self {
185+
elem.hash(state);
186+
}
187+
}
188+
}
189+
190+
impl<A, B> Hash for (A, B) where A: Hash, B: Hash {
191+
fn hash<H>(self, state: &mut H) where H: Hasher{
192+
self.0.hash(state);
193+
self.1.hash(state);
194+
}
195+
}
196+
197+
impl<A, B, C> Hash for (A, B, C) where A: Hash, B: Hash, C: Hash {
198+
fn hash<H>(self, state: &mut H) where H: Hasher{
199+
self.0.hash(state);
200+
self.1.hash(state);
201+
self.2.hash(state);
202+
}
203+
}
204+
205+
impl<A, B, C, D> Hash for (A, B, C, D) where A: Hash, B: Hash, C: Hash, D: Hash {
206+
fn hash<H>(self, state: &mut H) where H: Hasher{
207+
self.0.hash(state);
208+
self.1.hash(state);
209+
self.2.hash(state);
210+
self.3.hash(state);
211+
}
212+
}
213+
214+
impl<A, B, C, D, E> Hash for (A, B, C, D, E) where A: Hash, B: Hash, C: Hash, D: Hash, E: Hash {
215+
fn hash<H>(self, state: &mut H) where H: Hasher{
216+
self.0.hash(state);
217+
self.1.hash(state);
218+
self.2.hash(state);
219+
self.3.hash(state);
220+
self.4.hash(state);
128221
}
129222
}

0 commit comments

Comments
 (0)