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
use _alloc::{boxed::Box, vec::Vec};
use core::{cell::UnsafeCell, mem};
use risc0_zkp::core::{
fp::Fp,
fp4::Fp4,
sha::{Digest, DIGEST_WORDS},
};
use risc0_zkvm::{
platform::{
io::{SHADescriptor, GPIO_SHA},
memory, WORD_SIZE,
},
serde::to_vec_with_capacity,
};
use serde::Serialize;
use crate::align_up;
struct CurDesc(UnsafeCell<usize>);
unsafe impl Sync for CurDesc {}
static CUR_DESC: CurDesc = CurDesc(UnsafeCell::new(0));
const END_MARKER: u8 = 0x80;
const CHUNK_SIZE: usize = 64 / WORD_SIZE;
fn alloc_desc() -> *mut SHADescriptor {
unsafe {
let cur_desc = CUR_DESC.0.get();
let ptr = (memory::SHA.start() as *mut SHADescriptor).add(*cur_desc);
*cur_desc += 1;
ptr
}
}
pub fn raw_digest(data: &[u32]) -> &'static Digest {
assert_eq!(data.len() % CHUNK_SIZE, 0);
unsafe {
let alloced = Box::<mem::MaybeUninit<Digest>>::new(mem::MaybeUninit::<Digest>::uninit());
let digest = (*Box::into_raw(alloced)).as_mut_ptr();
raw_digest_to(data, digest);
&*digest
}
}
pub(crate) unsafe fn raw_digest_to(data: &[u32], digest: *mut Digest) {
assert_eq!(data.len() % CHUNK_SIZE, 0);
let type_count = data.len() / CHUNK_SIZE;
let desc_ptr = alloc_desc();
let ptr = data.as_ptr();
super::memory_barrier(ptr);
desc_ptr.write_volatile(SHADescriptor {
type_count,
idx: 0,
source: ptr as usize,
digest: digest as usize,
});
GPIO_SHA.as_ptr().write_volatile(desc_ptr);
}
pub(crate) const fn compute_capacity_needed(len_bytes: usize) -> usize {
let len_words = align_up(len_bytes + 1, WORD_SIZE) / WORD_SIZE + 2;
align_up(len_words, CHUNK_SIZE)
}
pub(crate) enum MemoryType {
Normal,
WOM,
}
pub(crate) fn add_trailer(data: &mut [u32], len_bytes: usize, memtype: MemoryType) {
assert_eq!(compute_capacity_needed(len_bytes), data.len());
let marker_word = len_bytes / WORD_SIZE;
match memtype {
MemoryType::WOM => {
assert_eq!(0, len_bytes % WORD_SIZE);
data[marker_word] = (END_MARKER as u32).to_le();
}
MemoryType::Normal => {
let as_u8: &mut [u8] = bytemuck::cast_slice_mut(data);
as_u8[len_bytes] = END_MARKER;
}
}
let size_word = data.len() - 1;
data[marker_word + 1..size_word].fill(0);
let len_bits = len_bytes * 8;
data[size_word] = (len_bits as u32).to_be();
}
pub fn digest<T: Serialize>(val: &T) -> &'static Digest {
let cap = compute_capacity_needed(mem::size_of_val(val));
let mut buf = to_vec_with_capacity(val, cap).unwrap();
let len_bytes = buf.len() * WORD_SIZE;
buf.resize(compute_capacity_needed(len_bytes), 0);
add_trailer(buf.as_mut_slice(), len_bytes, MemoryType::Normal);
raw_digest(buf.as_slice())
}
pub fn digest_u8_slice(data: &[u8]) -> &'static Digest {
let len_bytes = data.len();
let cap = compute_capacity_needed(len_bytes);
let mut data_u32 = Vec::<u32>::with_capacity(cap);
let whole_words = len_bytes / WORD_SIZE;
let words_copied: usize;
match bytemuck::try_cast_slice(&data[..whole_words * WORD_SIZE]) as Result<&[u32], _> {
Ok(words) => {
data_u32.extend_from_slice(words);
words_copied = whole_words;
}
Err(_) => {
words_copied = 0;
}
};
data_u32.resize(cap, 0);
let remaining_in = &data[words_copied * WORD_SIZE..];
let remaining_out: &mut [u8] = bytemuck::cast_slice_mut(&mut data_u32[words_copied..]);
remaining_out[..remaining_in.len()].clone_from_slice(remaining_in);
add_trailer(data_u32.as_mut_slice(), len_bytes, MemoryType::Normal);
raw_digest(data_u32.as_slice())
}
pub(crate) fn finalize() {
unsafe {
let ptr = alloc_desc();
let type_field_ptr: *mut usize = core::ptr::addr_of_mut!((*ptr).type_count);
type_field_ptr.write_volatile(0);
}
}
#[derive(Debug, Clone)]
pub struct Impl {}
impl risc0_zkp::core::sha::Sha for Impl {
type DigestPtr = &'static Digest;
fn hash_bytes(&self, bytes: &[u8]) -> Self::DigestPtr {
digest_u8_slice(bytes)
}
fn hash_pair(&self, a: &Digest, b: &Digest) -> Self::DigestPtr {
raw_digest(bytemuck::cast_slice(&[*a, *b]))
}
fn hash_fps(&self, fps: &[Fp]) -> Self::DigestPtr {
if fps.len() % CHUNK_SIZE == 0 {
raw_digest(bytemuck::cast_slice(fps))
} else {
let size = align_up(fps.len(), CHUNK_SIZE);
let mut buf: Vec<u32> = Vec::with_capacity(size);
buf.extend(bytemuck::cast_slice(fps));
buf.resize(size, 0);
raw_digest(&buf)
}
}
fn hash_fp4s(&self, fp4s: &[Fp4]) -> Self::DigestPtr {
self.hash_fps(bytemuck::cast_slice(fp4s))
}
fn mix(&self, pool: &mut Self::DigestPtr, val: &Digest) {
let mut digest = Box::<Digest>::new(Digest::default());
for i in 0..DIGEST_WORDS {
digest.get_mut()[i] = pool.get()[i] ^ val.get()[i];
}
unsafe {
let ptr: *const Digest = &*digest;
*pool = &*ptr
}
}
}