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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
//! Contains procedures for computing hashes using BLAKE3 and SHA256 hash
//! functions. The input and output elements are assumed to contain one 32-bit
//! value per element.
#[cfg(all(target_family = "wasm", miden))]
mod imp {
use alloc::vec::Vec;
use crate::{
felt,
intrinsics::{Digest, Felt, Word, assert_eq},
};
unsafe extern "C" {
/// Computes BLAKE3 1-to-1 hash.
///
/// Input: 32-bytes stored in the first 8 elements of the stack (32 bits per element).
/// Output: A 32-byte digest stored in the first 8 elements of stack (32 bits per element).
/// The output is passed back to the caller via a pointer.
#[link_name = "miden::core::crypto::hashes::blake3::hash"]
fn extern_blake3_hash(
e1: u32,
e2: u32,
e3: u32,
e4: u32,
e5: u32,
e6: u32,
e7: u32,
e8: u32,
ptr: *mut u8,
);
/// Computes BLAKE3 2-to-1 hash.
///
/// Input: 64-bytes stored in the first 16 elements of the stack (32 bits per element).
/// Output: A 32-byte digest stored in the first 8 elements of stack (32 bits per element)
/// The output is passed back to the caller via a pointer.
#[link_name = "miden::core::crypto::hashes::blake3::merge"]
fn extern_blake3_merge(
e1: u32,
e2: u32,
e3: u32,
e4: u32,
e5: u32,
e6: u32,
e7: u32,
e8: u32,
e9: u32,
e10: u32,
e11: u32,
e12: u32,
e13: u32,
e14: u32,
e15: u32,
e16: u32,
ptr: *mut u8,
);
}
unsafe extern "C" {
/// Computes SHA256 1-to-1 hash.
///
/// Input: 32-bytes stored in the first 8 elements of the stack (32 bits per element).
/// Output: A 32-byte digest stored in the first 8 elements of stack (32 bits per element).
/// The output is passed back to the caller via a pointer.
#[link_name = "miden::core::crypto::hashes::sha256::hash"]
fn extern_sha256_hash(
e1: u32,
e2: u32,
e3: u32,
e4: u32,
e5: u32,
e6: u32,
e7: u32,
e8: u32,
ptr: *mut u8,
);
/// Computes SHA256 2-to-1 hash.
///
/// Input: 64-bytes stored in the first 16 elements of the stack (32 bits per element).
/// Output: A 32-byte digest stored in the first 8 elements of stack (32 bits per element).
/// The output is passed back to the caller via a pointer.
#[link_name = "miden::core::crypto::hashes::sha256::merge"]
fn extern_sha256_merge(
e1: u32,
e2: u32,
e3: u32,
e4: u32,
e5: u32,
e6: u32,
e7: u32,
e8: u32,
e9: u32,
e10: u32,
e11: u32,
e12: u32,
e13: u32,
e14: u32,
e15: u32,
e16: u32,
ptr: *mut u8,
);
}
unsafe extern "C" {
/// Computes the hash of a sequence of field elements using the Rescue Prime Optimized (RPO)
/// hash function.
///
/// This maps to the `miden::core::crypto::hashes::poseidon2::hash_elements` procedure.
///
/// Input: A pointer to the memory location and the number of elements to hash
/// Output: One digest (4 field elements)
/// The output is passed back to the caller via a pointer.
#[link_name = "miden::core::crypto::hashes::poseidon2::hash_elements"]
pub fn extern_hash_elements(ptr: u32, num_elements: u32, result_ptr: *mut Felt);
/// Computes the hash of a sequence of words using the Rescue Prime Optimized (RPO) hash
/// function.
///
/// This maps to the `miden::core::crypto::hashes::poseidon2::hash_words` procedure.
///
/// Input: The start and end addresses (in field elements) of the words to hash.
/// Output: One digest (4 field elements)
/// The output is passed back to the caller via a pointer.
#[link_name = "miden::core::crypto::hashes::poseidon2::hash_words"]
pub fn extern_hash_words(start_addr: u32, end_addr: u32, result_ptr: *mut Felt);
}
/// Encodes 32 bytes as 8 little-endian u32 lanes.
#[inline(always)]
fn bytes_to_u32_le_8(input: [u8; 32]) -> [u32; 8] {
core::array::from_fn(|i| {
let off = i * 4;
u32::from_le_bytes([input[off], input[off + 1], input[off + 2], input[off + 3]])
})
}
/// Encodes 64 bytes as 16 little-endian u32 lanes.
#[inline(always)]
fn bytes_to_u32_le_16(input: [u8; 64]) -> [u32; 16] {
core::array::from_fn(|i| {
let off = i * 4;
u32::from_le_bytes([input[off], input[off + 1], input[off + 2], input[off + 3]])
})
}
/// Encodes 32 bytes as 8 big-endian u32 lanes.
#[inline(always)]
fn bytes_to_u32_be_8(input: [u8; 32]) -> [u32; 8] {
core::array::from_fn(|i| {
let off = i * 4;
u32::from_be_bytes([input[off], input[off + 1], input[off + 2], input[off + 3]])
})
}
/// Encodes 64 bytes as 16 big-endian u32 lanes.
#[inline(always)]
fn bytes_to_u32_be_16(input: [u8; 64]) -> [u32; 16] {
core::array::from_fn(|i| {
let off = i * 4;
u32::from_be_bytes([input[off], input[off + 1], input[off + 2], input[off + 3]])
})
}
#[inline(always)]
fn decode_be_lanes_in_place(bytes: &mut [u8]) {
for chunk in bytes.chunks_exact_mut(4) {
chunk.reverse();
}
}
/// Hashes a 32-byte input to a 32-byte output using the BLAKE3 hash function.
#[inline]
pub fn blake3_hash(input: [u8; 32]) -> [u8; 32] {
use crate::intrinsics::WordAligned;
let lanes = bytes_to_u32_le_8(input);
unsafe {
let mut ret_area = ::core::mem::MaybeUninit::<WordAligned<[u8; 32]>>::uninit();
let ptr = ret_area.as_mut_ptr() as *mut u8;
extern_blake3_hash(
lanes[0], lanes[1], lanes[2], lanes[3], lanes[4], lanes[5], lanes[6], lanes[7], ptr,
);
ret_area.assume_init().into_inner()
}
}
/// Hashes a 64-byte input to a 32-byte output using the BLAKE3 hash function.
#[inline]
pub fn blake3_merge(input: [u8; 64]) -> [u8; 32] {
let lanes = bytes_to_u32_le_16(input);
unsafe {
let mut ret_area = ::core::mem::MaybeUninit::<[u8; 32]>::uninit();
let ptr = ret_area.as_mut_ptr() as *mut u8;
extern_blake3_merge(
lanes[0], lanes[1], lanes[2], lanes[3], lanes[4], lanes[5], lanes[6], lanes[7],
lanes[8], lanes[9], lanes[10], lanes[11], lanes[12], lanes[13], lanes[14],
lanes[15], ptr,
);
ret_area.assume_init()
}
}
/// Hashes a 32-byte input to a 32-byte output using the SHA256 hash function.
#[inline]
pub fn sha256_hash(input: [u8; 32]) -> [u8; 32] {
use crate::intrinsics::WordAligned;
let lanes = bytes_to_u32_be_8(input);
unsafe {
let mut ret_area = ::core::mem::MaybeUninit::<WordAligned<[u8; 32]>>::uninit();
let ptr = ret_area.as_mut_ptr() as *mut u8;
extern_sha256_hash(
lanes[0], lanes[1], lanes[2], lanes[3], lanes[4], lanes[5], lanes[6], lanes[7], ptr,
);
let mut output = ret_area.assume_init().into_inner();
decode_be_lanes_in_place(&mut output);
output
}
}
/// Hashes a 64-byte input to a 32-byte output using the SHA256 hash function.
#[inline]
pub fn sha256_merge(input: [u8; 64]) -> [u8; 32] {
let lanes = bytes_to_u32_be_16(input);
unsafe {
let mut ret_area = ::core::mem::MaybeUninit::<[u8; 32]>::uninit();
let ptr = ret_area.as_mut_ptr() as *mut u8;
extern_sha256_merge(
lanes[0], lanes[1], lanes[2], lanes[3], lanes[4], lanes[5], lanes[6], lanes[7],
lanes[8], lanes[9], lanes[10], lanes[11], lanes[12], lanes[13], lanes[14],
lanes[15], ptr,
);
let mut output = ret_area.assume_init();
decode_be_lanes_in_place(&mut output);
output
}
}
/// Computes the hash of a sequence of field elements using the Rescue Prime Optimized (RPO)
/// hash function.
///
/// This maps to the `miden::core::crypto::hashes::poseidon2::hash_elements` procedure and to the
/// `miden::core::crypto::hashes::poseidon2::hash_words` word-optimized variant when the input
/// length is a multiple of 4.
///
/// # Arguments
/// * `elements` - A Vec of field elements to be hashed
#[inline]
pub fn hash_elements(elements: Vec<Felt>) -> Digest {
let rust_ptr = elements.as_ptr().addr() as u32;
let element_count = elements.len();
let num_elements = element_count as u32;
unsafe {
let mut ret_area = core::mem::MaybeUninit::<Word>::uninit();
let result_ptr = ret_area.as_mut_ptr() as *mut Felt;
let miden_ptr = rust_ptr / 4;
// Since our BumpAlloc produces word-aligned allocations the pointer should be word-aligned
assert_eq(Felt::new((miden_ptr % 4) as u64), felt!(0));
if element_count.is_multiple_of(4) {
let start_addr = miden_ptr;
let end_addr = start_addr + num_elements;
extern_hash_words(start_addr, end_addr, result_ptr);
} else {
extern_hash_elements(miden_ptr, num_elements, result_ptr);
}
Digest::from_word(ret_area.assume_init())
}
}
/// Computes the hash of a sequence of words using the Rescue Prime Optimized (RPO)
/// hash function.
///
/// This maps to the `miden::core::crypto::hashes::poseidon2::hash_words` procedure.
///
/// # Arguments
/// * `words` - A slice of words to be hashed
#[inline]
pub fn hash_words(words: &[Word]) -> Digest {
let rust_ptr = words.as_ptr().addr() as u32;
let miden_ptr = rust_ptr / 4;
// It's safe to assume the `words` ptr is word-aligned.
assert_eq(Felt::new((miden_ptr % 4) as u64), felt!(0));
unsafe {
let mut ret_area = core::mem::MaybeUninit::<Word>::uninit();
let result_ptr = ret_area.as_mut_ptr() as *mut Felt;
let start_addr = miden_ptr;
let end_addr = start_addr + (words.len() as u32 * 4);
extern_hash_words(start_addr, end_addr, result_ptr);
Digest::from_word(ret_area.assume_init())
}
}
}
#[cfg(not(all(target_family = "wasm", miden)))]
mod imp {
use alloc::vec::Vec;
use crate::intrinsics::{Digest, Felt, Word};
/// Computes BLAKE3 1-to-1 hash.
#[inline]
pub fn blake3_hash(_input: [u8; 32]) -> [u8; 32] {
unimplemented!(
"miden::core::crypto::hashes bindings are only available when targeting the Miden VM"
)
}
/// Computes BLAKE3 2-to-1 hash.
#[inline]
pub fn blake3_merge(_input: [u8; 64]) -> [u8; 32] {
unimplemented!(
"miden::core::crypto::hashes bindings are only available when targeting the Miden VM"
)
}
/// Computes SHA256 1-to-1 hash.
#[inline]
pub fn sha256_hash(_input: [u8; 32]) -> [u8; 32] {
unimplemented!(
"miden::core::crypto::hashes bindings are only available when targeting the Miden VM"
)
}
/// Computes SHA256 2-to-1 hash.
#[inline]
pub fn sha256_merge(_input: [u8; 64]) -> [u8; 32] {
unimplemented!(
"miden::core::crypto::hashes bindings are only available when targeting the Miden VM"
)
}
/// Computes the hash of a sequence of field elements using the Rescue Prime Optimized (RPO)
/// hash function.
#[inline]
pub fn hash_elements(_elements: Vec<Felt>) -> Digest {
unimplemented!(
"miden::core::crypto::hashes bindings are only available when targeting the Miden VM"
)
}
/// Computes the hash of a sequence of words using the Rescue Prime Optimized (RPO) hash
/// function.
#[inline]
pub fn hash_words(_words: &[Word]) -> Digest {
unimplemented!(
"miden::core::crypto::hashes bindings are only available when targeting the Miden VM"
)
}
/// ABI helper for `miden::core::crypto::hashes::poseidon2::hash_elements`.
#[inline]
pub fn extern_hash_elements(_ptr: u32, _num_elements: u32, _result_ptr: *mut Felt) {
unimplemented!(
"miden::core::crypto::hashes bindings are only available when targeting the Miden VM"
)
}
/// ABI helper for `miden::core::crypto::hashes::poseidon2::hash_words`.
#[inline]
pub fn extern_hash_words(_start_addr: u32, _end_addr: u32, _result_ptr: *mut Felt) {
unimplemented!(
"miden::core::crypto::hashes bindings are only available when targeting the Miden VM"
)
}
}
pub use imp::*;