-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsha256_packed.cairo
278 lines (256 loc) · 9.97 KB
/
sha256_packed.cairo
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
from starkware.cairo.common.alloc import alloc
from starkware.cairo.common.cairo_builtins import BitwiseBuiltin
from starkware.cairo.common.registers import get_fp_and_pc
// The goldilocks prime is defined as 2**64 - 2**32 + 1
const ALL_ONES = 2 ** 63 - 1;
// Given an array of size 16, extends it to the message schedule array (of size 64) by writing 48 more values.
func compute_message_schedule{bitwise_ptr: BitwiseBuiltin*}(message: felt*) {
alloc_locals;
// Defining the following constants as local variables saves some instructions.
local shift_mask3 = (2 ** 32 - 2 ** 3);
local shift_mask7 = (2 ** 32 - 2 ** 7);
local shift_mask10 = (2 ** 32 - 2 ** 10);
local shift_mask17 = (2 ** 32 - 2 ** 17);
local shift_mask18 = (2 ** 32 - 2 ** 18);
local shift_mask19 = (2 ** 32 - 2 ** 19);
local mask32ones = (2 ** 32 - 1);
// Loop variables.
tempvar bitwise_ptr = bitwise_ptr;
tempvar message = message + 16;
tempvar n = 64 - 16;
loop:
// Compute s0 = right_rot(w[i - 15], 7) ^ right_rot(w[i - 15], 18) ^ (w[i - 15] >> 3).
tempvar w0 = message[-15];
assert bitwise_ptr[0].x = w0;
assert bitwise_ptr[0].y = shift_mask7;
let w0_rot7 = (2 ** (32 - 7)) * w0 + (1 / 2 ** 7 - 2 ** (32 - 7)) * bitwise_ptr[0].x_and_y;
assert bitwise_ptr[1].x = w0;
assert bitwise_ptr[1].y = shift_mask18;
let w0_rot18 = (2 ** (32 - 18)) * w0 + (1 / 2 ** 18 - 2 ** (32 - 18)) * bitwise_ptr[1].x_and_y;
assert bitwise_ptr[2].x = w0;
assert bitwise_ptr[2].y = shift_mask3;
let w0_shift3 = (1 / 2 ** 3) * bitwise_ptr[2].x_and_y;
assert bitwise_ptr[3].x = w0_rot7;
assert bitwise_ptr[3].y = w0_rot18;
assert bitwise_ptr[4].x = bitwise_ptr[3].x_xor_y;
assert bitwise_ptr[4].y = w0_shift3;
let s0 = bitwise_ptr[4].x_xor_y;
let bitwise_ptr = bitwise_ptr + 5 * BitwiseBuiltin.SIZE;
// Compute s1 = right_rot(w[i - 2], 17) ^ right_rot(w[i - 2], 19) ^ (w[i - 2] >> 10).
tempvar w1 = message[-2];
assert bitwise_ptr[0].x = w1;
assert bitwise_ptr[0].y = shift_mask17;
let w1_rot17 = (2 ** (32 - 17)) * w1 + (1 / 2 ** 17 - 2 ** (32 - 17)) * bitwise_ptr[0].x_and_y;
assert bitwise_ptr[1].x = w1;
assert bitwise_ptr[1].y = shift_mask19;
let w1_rot19 = (2 ** (32 - 19)) * w1 + (1 / 2 ** 19 - 2 ** (32 - 19)) * bitwise_ptr[1].x_and_y;
assert bitwise_ptr[2].x = w1;
assert bitwise_ptr[2].y = shift_mask10;
let w1_shift10 = (1 / 2 ** 10) * bitwise_ptr[2].x_and_y;
assert bitwise_ptr[3].x = w1_rot17;
assert bitwise_ptr[3].y = w1_rot19;
assert bitwise_ptr[4].x = bitwise_ptr[3].x_xor_y;
assert bitwise_ptr[4].y = w1_shift10;
let s1 = bitwise_ptr[4].x_xor_y;
let bitwise_ptr = bitwise_ptr + 5 * BitwiseBuiltin.SIZE;
assert bitwise_ptr[0].x = message[-16] + s0 + message[-7] + s1;
assert bitwise_ptr[0].y = mask32ones;
assert message[0] = bitwise_ptr[0].x_and_y;
let bitwise_ptr = bitwise_ptr + BitwiseBuiltin.SIZE;
tempvar bitwise_ptr = bitwise_ptr;
tempvar message = message + 1;
tempvar n = n - 1;
jmp loop if n != 0;
return ();
}
func sha2_compress{bitwise_ptr: BitwiseBuiltin*}(
state: felt*, message: felt*, round_constants: felt*, res: felt*
) {
alloc_locals;
// Defining the following constants as local variables saves some instructions.
local shift_mask2 = (2 ** 32 - 2 ** 2);
local shift_mask13 = (2 ** 32 - 2 ** 13);
local shift_mask22 = (2 ** 32 - 2 ** 22);
local shift_mask6 = (2 ** 32 - 2 ** 6);
local shift_mask11 = (2 ** 32 - 2 ** 11);
local shift_mask25 = (2 ** 32 - 2 ** 25);
local mask32ones = (2 ** 32 - 1);
tempvar a = state[0];
tempvar b = state[1];
tempvar c = state[2];
tempvar d = state[3];
tempvar e = state[4];
tempvar f = state[5];
tempvar g = state[6];
tempvar h = state[7];
tempvar round_constants = round_constants;
tempvar message = message;
tempvar bitwise_ptr = bitwise_ptr;
tempvar n = 64;
loop:
// Compute s0 = right_rot(a, 2) ^ right_rot(a, 13) ^ right_rot(a, 22).
assert bitwise_ptr[0].x = a;
assert bitwise_ptr[0].y = shift_mask2;
let a_rot2 = (2 ** (32 - 2)) * a + (1 / 2 ** 2 - 2 ** (32 - 2)) * bitwise_ptr[0].x_and_y;
assert bitwise_ptr[1].x = a;
assert bitwise_ptr[1].y = shift_mask13;
let a_rot13 = (2 ** (32 - 13)) * a + (1 / 2 ** 13 - 2 ** (32 - 13)) * bitwise_ptr[1].x_and_y;
assert bitwise_ptr[2].x = a;
assert bitwise_ptr[2].y = shift_mask22;
let a_rot22 = (2 ** (32 - 22)) * a + (1 / 2 ** 22 - 2 ** (32 - 22)) * bitwise_ptr[2].x_and_y;
assert bitwise_ptr[3].x = a_rot2;
assert bitwise_ptr[3].y = a_rot13;
assert bitwise_ptr[4].x = bitwise_ptr[3].x_xor_y;
assert bitwise_ptr[4].y = a_rot22;
let s0 = bitwise_ptr[4].x_xor_y;
let bitwise_ptr = bitwise_ptr + 5 * BitwiseBuiltin.SIZE;
// Compute s1 = right_rot(e, 6) ^ right_rot(e, 11) ^ right_rot(e, 25).
assert bitwise_ptr[0].x = e;
assert bitwise_ptr[0].y = shift_mask6;
let e_rot6 = (2 ** (32 - 6)) * e + (1 / 2 ** 6 - 2 ** (32 - 6)) * bitwise_ptr[0].x_and_y;
assert bitwise_ptr[1].x = e;
assert bitwise_ptr[1].y = shift_mask11;
let e_rot11 = (2 ** (32 - 11)) * e + (1 / 2 ** 11 - 2 ** (32 - 11)) * bitwise_ptr[1].x_and_y;
assert bitwise_ptr[2].x = e;
assert bitwise_ptr[2].y = shift_mask25;
let e_rot25 = (2 ** (32 - 25)) * e + (1 / 2 ** 25 - 2 ** (32 - 25)) * bitwise_ptr[2].x_and_y;
assert bitwise_ptr[3].x = e_rot6;
assert bitwise_ptr[3].y = e_rot11;
assert bitwise_ptr[4].x = bitwise_ptr[3].x_xor_y;
assert bitwise_ptr[4].y = e_rot25;
let s1 = bitwise_ptr[4].x_xor_y;
let bitwise_ptr = bitwise_ptr + 5 * BitwiseBuiltin.SIZE;
// Compute ch = (e & f) ^ ((~e) & g).
assert bitwise_ptr[0].x = e;
assert bitwise_ptr[0].y = f;
assert bitwise_ptr[1].x = ALL_ONES - e;
assert bitwise_ptr[1].y = g;
let ch = bitwise_ptr[0].x_and_y + bitwise_ptr[1].x_and_y;
let bitwise_ptr = bitwise_ptr + 2 * BitwiseBuiltin.SIZE;
// Compute maj = (a & b) ^ (a & c) ^ (b & c).
assert bitwise_ptr[0].x = a;
assert bitwise_ptr[0].y = b;
assert bitwise_ptr[1].x = bitwise_ptr[0].x_xor_y;
assert bitwise_ptr[1].y = c;
let maj = (a + b + c - bitwise_ptr[1].x_xor_y) / 2;
let bitwise_ptr = bitwise_ptr + 2 * BitwiseBuiltin.SIZE;
tempvar temp1 = h + s1 + ch + round_constants[0] + message[0];
tempvar temp2 = s0 + maj;
assert bitwise_ptr[0].x = temp1 + temp2;
assert bitwise_ptr[0].y = mask32ones;
let new_a = bitwise_ptr[0].x_and_y;
assert bitwise_ptr[1].x = d + temp1;
assert bitwise_ptr[1].y = mask32ones;
let new_e = bitwise_ptr[1].x_and_y;
let bitwise_ptr = bitwise_ptr + 2 * BitwiseBuiltin.SIZE;
tempvar new_a = new_a;
tempvar new_b = a;
tempvar new_c = b;
tempvar new_d = c;
tempvar new_e = new_e;
tempvar new_f = e;
tempvar new_g = f;
tempvar new_h = g;
tempvar round_constants = round_constants + 1;
tempvar message = message + 1;
tempvar bitwise_ptr = bitwise_ptr;
tempvar n = n - 1;
jmp loop if n != 0;
// Add the compression result to the original state:
assert bitwise_ptr[0].x = state[0] + new_a;
assert bitwise_ptr[0].y = mask32ones;
assert res[0] = bitwise_ptr[0].x_and_y;
assert bitwise_ptr[1].x = state[1] + new_b;
assert bitwise_ptr[1].y = mask32ones;
assert res[1] = bitwise_ptr[1].x_and_y;
assert bitwise_ptr[2].x = state[2] + new_c;
assert bitwise_ptr[2].y = mask32ones;
assert res[2] = bitwise_ptr[2].x_and_y;
assert bitwise_ptr[3].x = state[3] + new_d;
assert bitwise_ptr[3].y = mask32ones;
assert res[3] = bitwise_ptr[3].x_and_y;
assert bitwise_ptr[4].x = state[4] + new_e;
assert bitwise_ptr[4].y = mask32ones;
assert res[4] = bitwise_ptr[4].x_and_y;
assert bitwise_ptr[5].x = state[5] + new_f;
assert bitwise_ptr[5].y = mask32ones;
assert res[5] = bitwise_ptr[5].x_and_y;
assert bitwise_ptr[6].x = state[6] + new_g;
assert bitwise_ptr[6].y = mask32ones;
assert res[6] = bitwise_ptr[6].x_and_y;
assert bitwise_ptr[7].x = state[7] + new_h;
assert bitwise_ptr[7].y = mask32ones;
assert res[7] = bitwise_ptr[7].x_and_y;
let bitwise_ptr = bitwise_ptr + 8 * BitwiseBuiltin.SIZE;
return ();
}
// Returns the 64 round constants of SHA256.
func get_round_constants() -> felt* {
alloc_locals;
let (__fp__, _) = get_fp_and_pc();
local round_constants = 0x428A2F98;
local a = 0x71374491;
local a = 0xB5C0FBCF;
local a = 0xE9B5DBA5;
local a = 0x3956C25B;
local a = 0x59F111F1;
local a = 0x923F82A4;
local a = 0xAB1C5ED5;
local a = 0xD807AA98;
local a = 0x12835B01;
local a = 0x243185BE;
local a = 0x550C7DC3;
local a = 0x72BE5D74;
local a = 0x80DEB1FE;
local a = 0x9BDC06A7;
local a = 0xC19BF174;
local a = 0xE49B69C1;
local a = 0xEFBE4786;
local a = 0x0FC19DC6;
local a = 0x240CA1CC;
local a = 0x2DE92C6F;
local a = 0x4A7484AA;
local a = 0x5CB0A9DC;
local a = 0x76F988DA;
local a = 0x983E5152;
local a = 0xA831C66D;
local a = 0xB00327C8;
local a = 0xBF597FC7;
local a = 0xC6E00BF3;
local a = 0xD5A79147;
local a = 0x06CA6351;
local a = 0x14292967;
local a = 0x27B70A85;
local a = 0x2E1B2138;
local a = 0x4D2C6DFC;
local a = 0x53380D13;
local a = 0x650A7354;
local a = 0x766A0ABB;
local a = 0x81C2C92E;
local a = 0x92722C85;
local a = 0xA2BFE8A1;
local a = 0xA81A664B;
local a = 0xC24B8B70;
local a = 0xC76C51A3;
local a = 0xD192E819;
local a = 0xD6990624;
local a = 0xF40E3585;
local a = 0x106AA070;
local a = 0x19A4C116;
local a = 0x1E376C08;
local a = 0x2748774C;
local a = 0x34B0BCB5;
local a = 0x391C0CB3;
local a = 0x4ED8AA4A;
local a = 0x5B9CCA4F;
local a = 0x682E6FF3;
local a = 0x748F82EE;
local a = 0x78A5636F;
local a = 0x84C87814;
local a = 0x8CC70208;
local a = 0x90BEFFFA;
local a = 0xA4506CEB;
local a = 0xBEF9A3F7;
local a = 0xC67178F2;
return &round_constants;
}