forked from starkware-libs/cairo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.cairo
More file actions
231 lines (216 loc) · 7.19 KB
/
Copy pathhash.cairo
File metadata and controls
231 lines (216 loc) · 7.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//! Generic hashing support.
//!
//! This module provides a hash state abstraction that can be updated with values and finalized to
//! produce a hash. This allows for flexible and efficient hashing of any type with different hash
//! functions.
//!
//! The simplest way to make a type hashable is to use `#[derive(Hash)]`. Hashing a value is done by
//! initiating a `HashState` corresponding to a hash function, updating it with the value, and then
//! finalizing it to get the hash result.
//!
//! # Examples
//!
//! Basic usage with Pedersen and Poseidon hash:
//!
//! ```
//! use core::pedersen::PedersenTrait;
//! use core::poseidon::PoseidonTrait;
//!
//! #[derive(Copy, Drop, Hash)]
//! struct Person {
//! id: u32,
//! phone: u64,
//! }
//!
//! fn main() {
//! let person1 = Person { id: 1, phone: 555_666_7777 };
//! let person2 = Person { id: 2, phone: 555_666_7778 };
//!
//! assert!(
//! PedersenTrait::new(0)
//! .update_with(person1)
//! .finalize() != PedersenTrait::new(0)
//! .update_with(person2)
//! .finalize(),
//! );
//! assert!(
//! PoseidonTrait::new()
//! .update_with(person1)
//! .finalize() != PoseidonTrait::new()
//! .update_with(person2)
//! .finalize(),
//! );
//! }
//! ```
#[allow(unused_imports)]
use crate::traits::Into;
/// A trait for hash state accumulators.
///
/// Provides methods to update a hash state with new values and finalize it into a hash result.
pub trait HashStateTrait<S> {
/// Updates the current hash state `self` with the given `felt252` value and returns a new hash
/// state.
///
/// # Examples
///
/// ```
/// use core::pedersen::PedersenTrait;
/// use core::hash::HashStateTrait;
///
/// let mut state = PedersenTrait::new(0);
/// state = state.update(1);
/// ```
#[must_use]
fn update(self: S, value: felt252) -> S;
/// Takes the current state `self` and returns the hash result.
///
/// # Examples
///
/// ```
/// use core::pedersen::PedersenTrait;
/// use core::hash::HashStateTrait;
///
/// let mut state = PedersenTrait::new(0);
/// let hash = state.finalize();
/// ```
#[must_use]
fn finalize(self: S) -> felt252;
}
/// A trait for values that can be hashed.
///
/// This trait should be implemented for any type that can be included in a hash calculation.
/// The most common way to implement this trait is by using `#[derive(Hash)]`.
pub trait Hash<T, S, +HashStateTrait<S>> {
/// Updates the hash state with the given value and returns a new hash state.
///
/// # Examples
///
/// ```
/// use core::pedersen::PedersenTrait;
/// use core::hash::Hash;
///
/// let mut state = PedersenTrait::new(0);
/// let new_state = Hash::update_state(state, 1);
/// ```
#[must_use]
fn update_state(state: S, value: T) -> S;
}
/// A trait for hashing values using a `felt252` as hash state, used for backwards compatibility.
/// NOTE: Implement `Hash` instead of this trait if possible.
pub trait LegacyHash<T> {
/// Takes a `felt252` state and a value of type `T` and returns the hash result.
///
/// # Examples
///
/// ```
/// use core::pedersen::PedersenTrait;
/// use core::hash::LegacyHash;
///
/// let hash = LegacyHash::hash(0, 1);
/// ```
#[must_use]
fn hash(state: felt252, value: T) -> felt252;
}
/// Implementation of `LegacyHash` for types that implement `Hash` for backwards compatibility.
impl LegacyHashForHash<T, +Hash<T, crate::pedersen::HashState>> of LegacyHash<T> {
#[inline]
fn hash(state: felt252, value: T) -> felt252 {
crate::pedersen::HashState { state }.update_with(value).state
}
}
/// Extension trait for hash state accumulators.
///
/// This trait adds the `update_with` method to hash states, allowing you to directly hash values of
/// any type T that implements `Hash`, rather than having to manually convert values to felt252
/// first. This provides a more ergonomic API when working with complex types.
pub trait HashStateExTrait<S, T> {
/// Updates the hash state with the given value and returns the updated state.
///
/// # Examples
///
/// ```
/// use core::pedersen::PedersenTrait;
/// use core::hash::HashStateExTrait;
///
/// #[derive(Copy, Drop, Hash)]
/// struct Point { x: u32, y: u32 }
///
/// let point = Point { x: 1, y: 2 };
/// let hash = PedersenTrait::new(0)
/// .update_with(point)
/// .update_with(42)
/// .finalize();
/// ```
#[must_use]
fn update_with(self: S, value: T) -> S;
}
/// Implementation for `HashStateExTrait` for types that implement `Hash` for backwards
/// compatibility.
impl HashStateEx<S, +HashStateTrait<S>, T, +Hash<T, S>> of HashStateExTrait<S, T> {
#[inline]
fn update_with(self: S, value: T) -> S {
Hash::update_state(self, value)
}
}
impl HashFelt252<S, +HashStateTrait<S>> of Hash<felt252, S> {
#[inline]
fn update_state(state: S, value: felt252) -> S {
state.update(value)
}
}
/// Implementation for `Hash` for types that can be converted into `felt252` using the `Into` trait.
///
/// # Examples
///
/// ```
/// impl MyTypeHash<S, +HashStateTrait<S>, +Drop<S>> =
/// core::hash::into_felt252_based::HashImpl<MyType, S>;
/// ```
pub mod into_felt252_based {
pub impl HashImpl<
T, S, +Into<T, felt252>, +super::HashStateTrait<S>, +Drop<S>,
> of super::Hash<T, S> {
#[inline]
fn update_state(state: S, value: T) -> S {
state.update(value.into())
}
}
}
impl HashBool<S, +HashStateTrait<S>, +Drop<S>> = into_felt252_based::HashImpl<bool, S>;
impl HashU8<S, +HashStateTrait<S>, +Drop<S>> = into_felt252_based::HashImpl<u8, S>;
impl HashU16<S, +HashStateTrait<S>, +Drop<S>> = into_felt252_based::HashImpl<u16, S>;
impl HashU32<S, +HashStateTrait<S>, +Drop<S>> = into_felt252_based::HashImpl<u32, S>;
impl HashU64<S, +HashStateTrait<S>, +Drop<S>> = into_felt252_based::HashImpl<u64, S>;
impl HashU128<S, +HashStateTrait<S>, +Drop<S>> = into_felt252_based::HashImpl<u128, S>;
impl HashI8<S, +HashStateTrait<S>, +Drop<S>> = into_felt252_based::HashImpl<i8, S>;
impl HashI16<S, +HashStateTrait<S>, +Drop<S>> = into_felt252_based::HashImpl<i16, S>;
impl HashI32<S, +HashStateTrait<S>, +Drop<S>> = into_felt252_based::HashImpl<i32, S>;
impl HashI64<S, +HashStateTrait<S>, +Drop<S>> = into_felt252_based::HashImpl<i64, S>;
impl HashI128<S, +HashStateTrait<S>, +Drop<S>> = into_felt252_based::HashImpl<i128, S>;
impl TupleSize0Hash<S, +HashStateTrait<S>> of Hash<(), S> {
#[inline]
fn update_state(state: S, value: ()) -> S {
state
}
}
impl FixedSizedArray0Hash<T, S, +HashStateTrait<S>, +Drop<T>> of Hash<[T; 0], S> {
#[inline]
fn update_state(state: S, value: [T; 0]) -> S {
state
}
}
impl TupleNextHash<
T,
S,
+HashStateTrait<S>,
impl TH: crate::metaprogramming::TupleSplit<T>,
+Hash<TH::Head, S>,
+Hash<TH::Rest, S>,
+Drop<TH::Rest>,
> of Hash<T, S> {
#[inline]
fn update_state(state: S, value: T) -> S {
let (head, rest) = TH::split_head(value);
state.update_with(head).update_with(rest)
}
}