Merge branch 'main' of github.com:mir-protocol/plonky2 into fp318

This commit is contained in:
Dmitry Vagner 2023-03-15 19:34:49 -07:00
commit 2195bdd489
12 changed files with 223 additions and 129 deletions

View File

@ -134,17 +134,13 @@ pubkey_to_addr:
// stack: hash, v, r, s, retdest
DUP2
// stack: v, hash, v, r, s, retdest
PUSH 27
// stack: 27, v, hash, v, r, s, retdest
EQ
%eq_const(27)
// stack: v==27, hash, v, r, s, retdest
DUP3
// stack: v, v==27, hash, v, r, s, retdest
PUSH 28
// stack: 28, v, v==27, hash, v, r, s, retdest
EQ
%eq_const(28)
// stack: v==28, v==27, hash, v, r, s, retdest
OR
ADD // OR
// stack: (v==28 || v==27), hash, v, r, s, retdest
DUP5
// stack: s, (v==28 || v==27), hash, v, r, s, retdest
@ -154,7 +150,7 @@ pubkey_to_addr:
// stack: r, (s >= N || s==0), (v==28 || v==27), hash, v, r, s, retdest
%secp_is_out_of_bounds
// stack: (r >= N || r==0), (s >= N || s==0), (v==28 || v==27), hash, v, r, s, retdest
OR
ADD // OR
// stack: (r >= N || r==0 || s >= N || s==0), (v==28 || v==27), hash, v, r, s, retdest
ISZERO
// stack: (r < N & r!=0 & s < N & s!=0), (v==28 || v==27), hash, v, r, s, retdest
@ -178,7 +174,7 @@ pubkey_to_addr:
// stack: x < N, x==0
ISZERO
// stack: x >= N, x==0
OR
ADD // OR
// stack: x >= N || x==0
%endmacro

View File

@ -1,19 +1,3 @@
// Load the initial hash value (the IV, but with params XOR'd into the first word).
%macro blake2b_initial_hash_value
%blake2b_iv_i(7)
%blake2b_iv_i(6)
%blake2b_iv_i(5)
%blake2b_iv_i(4)
%blake2b_iv_i(3)
%blake2b_iv_i(2)
%blake2b_iv_i(1)
// stack: IV_1, IV_2, IV_3, IV_4, IV_5, IV_6, IV_7
PUSH 0x01010040 // params: key = 00, digest_size = 64 = 0x40
%blake2b_iv_i(0)
XOR
// stack: IV_0 ^ params, IV_1, IV_2, IV_3, IV_4, IV_5, IV_6, IV_7
%endmacro
// Address where the working version of the hash value is stored.
%macro blake2b_hash_value_addr
PUSH 0

View File

@ -2,7 +2,9 @@ global blake2b_compression:
// stack: retdest
PUSH 0
// stack: cur_block = 0, retdest
%blake2b_initial_hash_value
PUSH compression_loop
// stack: compression_loop, cur_block, retdest
%jump(blake2b_initial_hash_value)
compression_loop:
// stack: h_0, ..., h_7, cur_block, retdest
@ -181,40 +183,20 @@ compression_loop:
POP
POP
// stack: cur_block, retdest
%blake2b_internal_state_addr
// stack: start, cur_block, retdest
PUSH 0
// stack: round=0, start, cur_block, retdest
// Run 12 rounds of G functions.
%rep 12
// stack: round, start, cur_block, retdest
%call_blake2b_g_function(0, 4, 8, 12, 0, 1)
%call_blake2b_g_function(1, 5, 9, 13, 2, 3)
%call_blake2b_g_function(2, 6, 10, 14, 4, 5)
%call_blake2b_g_function(3, 7, 11, 15, 6, 7)
%call_blake2b_g_function(0, 5, 10, 15, 8, 9)
%call_blake2b_g_function(1, 6, 11, 12, 10, 11)
%call_blake2b_g_function(2, 7, 8, 13, 12, 13)
%call_blake2b_g_function(3, 4, 9, 14, 14, 15)
// stack: round, start, cur_block, retdest
%increment
// stack: round + 1, start, cur_block, retdest
%endrep
// stack: 12, start, cur_block, retdest
POP
POP
PUSH g_functions_return
// stack: g_functions_return, cur_block, retdest
%blake2b_internal_state_addr
// stack: start, g_functions_return, cur_block, retdest
%jump(run_12_rounds_g_function)
g_functions_return:
// Finalize hash value.
// stack: cur_block, retdest
%blake2b_generate_new_hash_value(7)
%blake2b_generate_new_hash_value(6)
%blake2b_generate_new_hash_value(5)
%blake2b_generate_new_hash_value(4)
%blake2b_generate_new_hash_value(3)
%blake2b_generate_new_hash_value(2)
%blake2b_generate_new_hash_value(1)
%blake2b_generate_new_hash_value(0)
PUSH hash_generate_return
// stack: hash_generate_return, cur_block, retdest
%jump(blake2b_generate_all_hash_values)
hash_generate_return:
// stack: h_0', h_1', h_2', h_3', h_4', h_5', h_6', h_7', cur_block, retdest
DUP9
// stack: cur_block, h_0', h_1', h_2', h_3', h_4', h_5', h_6', h_7', cur_block, retdest

View File

@ -3,28 +3,38 @@
// are in the range 0..16) in the internal state.
// The internal state is stored in memory starting at the address start.
// stack: a, b, c, d, x, y, start
%stack (indices: 4) -> (indices, indices)
DUP4
DUP4
DUP4
DUP4
// stack: a, b, c, d, a, b, c, d, x, y, start
DUP11
// stack: start, a, b, c, d, a, b, c, d, x, y, start
%stack (start, a, b, c, d) -> (d, start, c, start, b, start, a, start)
// stack: d, start, c, start, b, start, a, start, a, b, c, d, x, y, start
ADD
%mload_kernel_general
// stack: v[d], c, start, b, start, a, start, a, b, c, d, x, y, start
%stack (vd, remaining: 6) -> (remaining, vd)
// stack: c, start, b, start, a, start, v[d], a, b, c, d, x, y, start
// stack: v[a], b, c, d, a, b, c, d, x, y, start
SWAP1
// stack: b, v[a], c, d, a, b, c, d, x, y, start
DUP11
// stack: start, b, v[a], c, d, a, b, c, d, x, y, start
ADD
%mload_kernel_general
%stack (vc, remaining: 4) -> (remaining, vc)
// stack: b, start, a, start, v[c], v[d], a, b, c, d, x, y, start
// stack: v[b], v[a], c, d, a, b, c, d, x, y, start
SWAP2
// stack: c, v[a], v[b], d, a, b, c, d, x, y, start
DUP11
// stack: start, c, v[a], v[b], d, a, b, c, d, x, y, start
ADD
%mload_kernel_general
// stack: v[b], a, start, v[c], v[d], a, b, c, d, x, y, start
%stack (vb, remaining: 2) -> (remaining, vb)
// stack: a, start, v[b], v[c], v[d], a, b, c, d, x, y, start
// stack: v[c], v[a], v[b], d, a, b, c, d, x, y, start
SWAP3
// stack: d, v[a], v[b], v[c], a, b, c, d, x, y, start
DUP11
// stack: start, d, v[a], v[b], v[c], a, b, c, d, x, y, start
ADD
%mload_kernel_general
// stack: v[d], v[a], v[b], v[c], a, b, c, d, x, y, start
%stack (vd, vs: 3) -> (vs, vd)
// stack: v[a], v[b], v[c], v[d], a, b, c, d, x, y, start
DUP2
// stack: v[b], v[a], v[b], v[c], v[d], a, b, c, d, x, y, start
@ -124,3 +134,45 @@
%blake2b_g_function
// stack: round, start
%endmacro
run_g_function_round:
// stack: round, start, retdest
%call_blake2b_g_function(0, 4, 8, 12, 0, 1)
%call_blake2b_g_function(1, 5, 9, 13, 2, 3)
%call_blake2b_g_function(2, 6, 10, 14, 4, 5)
%call_blake2b_g_function(3, 7, 11, 15, 6, 7)
%call_blake2b_g_function(0, 5, 10, 15, 8, 9)
%call_blake2b_g_function(1, 6, 11, 12, 10, 11)
%call_blake2b_g_function(2, 7, 8, 13, 12, 13)
%call_blake2b_g_function(3, 4, 9, 14, 14, 15)
%stack (r, s, ret) -> (ret, r, s)
// stack: retdest, round, start
JUMP
global run_12_rounds_g_function:
// stack: start, retdest
PUSH 0
// stack: round=0, start, retdest
run_next_round_g_function:
// stack: round, start, retdest
PUSH run_next_round_g_function_return
// stack: run_next_round_g_function_return, round, start, retdest
SWAP2
// stack: start, round, run_next_round_g_function_return, retdest
SWAP1
// stack: round, start, run_next_round_g_function_return, retdest
%jump(run_g_function_round)
run_next_round_g_function_return:
// stack: round, start, retdest
%increment
// stack: round+1, start, retdest
DUP1
// stack: round+1, round+1, start, retdest
%lt_const(12)
// stack: round+1 < 12, round+1, start, retdest
%jumpi(run_next_round_g_function)
// stack: round+1, start, retdest
%pop2
// stack: retdest
JUMP

View File

@ -1,18 +1,54 @@
%macro blake2b_generate_new_hash_value(i)
blake2b_generate_new_hash_value:
// stack: i, retdest
%blake2b_hash_value_addr
%add_const($i)
// stack: addr, i, retdest
DUP2
ADD
%mload_kernel_general
// stack: h_i, ...
// stack: h_i, i, retdest
%blake2b_internal_state_addr
%add_const($i)
// stack: addr, h_i, i, retdest
DUP3
ADD
%mload_kernel_general
// stack: v_i, h_i, ...
// stack: v_i, h_i, i, retdest
%blake2b_internal_state_addr
%add_const($i)
// stack: addr, v_i, h_i, i, retdest
SWAP1
// stack: v_i, addr, h_i, i, retdest
SWAP3
// stack: i, addr, h_i, v_i, retdest
ADD
%add_const(8)
%mload_kernel_general
// stack: v_(i+8), v_i, h_i, ...
// stack: v_(i+8), h_i, v_i, retdest
XOR
XOR
// stack: h_i' = v_(i+8) ^ v_i ^ h_i, ...
%endmacro
// stack: h_i' = v_(i+8) ^ v_i ^ h_i, retdest
SWAP1
JUMP
global blake2b_generate_all_hash_values:
// stack: retdest
PUSH 8
// stack: i=8, retdest
blake2b_generate_hash_loop:
// stack: i, h_i', ..., h_7', retdest
%decrement
// stack: i-1, h_i', ..., h_7', retdest
PUSH blake2b_generate_hash_return
// stack: blake2b_generate_hash_return, i-1, h_i', ..., h_7', retdest
DUP2
// stack: i-1, blake2b_generate_hash_return, i-1, h_i', ..., h_7', retdest
%jump(blake2b_generate_new_hash_value)
blake2b_generate_hash_return:
// stack: h_(i-1)', i-1, h_i', ..., h_7', retdest
SWAP1
// stack: i-1, h_(i-1)', h_i', ..., h_7', retdest
DUP1
// stack: i-1, i-1, h_(i-1)', ..., h_7', retdest
%jumpi(blake2b_generate_hash_loop)
// stack: i-1=0, h_0', ..., h_7', retdest
%stack (i, h: 8, ret) -> (ret, h)
// stack: retdest, h_0'...h_7'
JUMP

View File

@ -33,30 +33,63 @@ global blake2b_iv_const:
BYTES 91, 224, 205, 25
BYTES 19, 126, 33, 121
%macro blake2b_iv
// stack: i, ...
global blake2b_iv:
// stack: i, retdest
PUSH blake2b_iv_const
// stack: blake2b_iv_const, i, ...
// stack: blake2b_iv_const, i, retdest
SWAP1
// stack: i, blake2b_iv_const, ...
// stack: i, blake2b_iv_const, retdest
%mul_const(8)
ADD
// stack: blake2b_iv_const + 2 * i, ...
// stack: blake2b_iv_const + 2 * i, retdest
DUP1
// stack: blake2b_iv_const + 2 * i, blake2b_iv_const + 2 * i, ...
// stack: blake2b_iv_const + 2 * i, blake2b_iv_const + 2 * i, retdest
%add_const(4)
// stack: blake2b_iv_const + 2 * i + 1, blake2b_iv_const + 2 * i, ...
// stack: blake2b_iv_const + 2 * i + 1, blake2b_iv_const + 2 * i, retdest
%mload_kernel_code_u32
SWAP1
%mload_kernel_code_u32
// stack: IV_i[32:], IV_i[:32], ...
// stack: IV_i[32:], IV_i[:32], retdest
%shl_const(32)
// stack: IV_i[32:] << 32, IV_i[:32], ...
OR
// stack: IV_i, ...
// stack: IV_i[32:] << 32, IV_i[:32], retdest
ADD // OR
// stack: IV_i, retdest
SWAP1
JUMP
%macro blake2b_iv
%stack (i) -> (i, %%after)
%jump(blake2b_iv)
%%after:
%endmacro
%macro blake2b_iv_i(i)
PUSH $i
%blake2b_iv
%endmacro
// Load the initial hash value (the IV, but with params XOR'd into the first word).
global blake2b_initial_hash_value:
// stack: retdest
PUSH 8
// stack: i=8, retdest
blake2b_initial_hash_loop:
// stack: i, IV_i, ..., IV_7, retdest
%decrement
// stack: i-1, IV_i, ..., IV_7, retdest
PUSH blake2b_initial_hash_return
// stack: blake2b_initial_hash_return, i-1, IV_i, ..., IV_7, retdest
DUP2
// stack: i-1, blake2b_initial_hash_return, i-1, IV_i, ..., IV_7, retdest
%jump(blake2b_iv)
blake2b_initial_hash_return:
// stack: IV_(i-1), i-1, IV_i, ..., IV_7, retdest
SWAP1
// stack: i-1, IV_(i-1), IV_i, ..., IV_7, retdest
DUP1
// stack: i-1, i-1, IV_(i-1), ..., IV_7, retdest
%jumpi(blake2b_initial_hash_loop)
// stack: i-1=0, IV_0, ..., IV_7, retdest
POP
// stack: IV_0, ..., IV_7, retdest
PUSH 0x01010040 // params: key = 00, digest_size = 64 = 0x40
XOR
// stack: IV_0 ^ params, IV_1, IV_2, IV_3, IV_4, IV_5, IV_6, IV_7, retdest
%stack(iv: 8, ret) -> (ret, iv)
JUMP

View File

@ -58,17 +58,28 @@ global permutation_9_constants:
BYTES 15, 11, 9, 14
BYTES 3, 12, 13, 0
%macro blake2b_permutation
// stack: round, i
global blake2b_permutation:
// stack: i, round, retdest
PUSH permutation_0_constants
// stack: permutation_0_constants, round, i
SWAP1
// stack: round, permutation_1_constants, i
// stack: permutation_0_constants, i, round, retdest
SWAP2
// stack: round, i, permutation_0_constants, retdest
%mod_const(10)
// stack: round % 10, permutation_1_constants, i
// stack: round % 10, i, permutation_0_constants, retdest
%mul_const(16)
ADD
// stack: permutation_(round)_constants, i
ADD
%mload_kernel_code
// stack: permutation_(round%10)_constants[i], retdest
SWAP1
JUMP
%macro blake2b_permutation
// stack: round, i
PUSH %%after
// stack: %%after, round, i
SWAP2
// stack: i, round, %%after
%jump(blake2b_permutation)
%%after:
%endmacro

View File

@ -18,7 +18,7 @@ global rol:
// stack: x << n, x >> (32-n), retdest
%as_u32
// stack: u32(x << n), x >> (32-n), retdest
OR
ADD // OR
// stack: u32(x << n) | (x >> (32-n)), retdest
SWAP1
JUMP

View File

@ -81,21 +81,21 @@ process:
SWAP1
%reverse_bytes_u32
%shl_const(96)
OR
ADD // OR
// stack: b' a', c, d, e, VARS
SWAP1
%reverse_bytes_u32
%shl_const(64)
OR
ADD // OR
// stack: c' b' a', d, e, VARS
SWAP1
%reverse_bytes_u32
%shl_const(32)
OR
ADD // OR
// stack: d' c' b' a', e, VARS
SWAP1
%reverse_bytes_u32
OR
ADD // OR
// stack: e' d' c' b' a', VARS
%stack (result, VARS: 3, retdest) -> (retdest, result)
// stack: 0xdeadbeef, result

View File

@ -263,19 +263,19 @@ compression_end:
POP
// stack: a[0]+a[64], b[0]+b[64], c[0]+c[64], d[0]+d[64], e[0]+e[64], f[0]+f[64], g[0]+g[64], h[0]+h[64], scratch_space_addr, message_schedule_addr, i, retdest
%shl_const(32)
OR
ADD // OR
%shl_const(32)
OR
ADD // OR
%shl_const(32)
OR
ADD // OR
%shl_const(32)
OR
ADD // OR
%shl_const(32)
OR
ADD // OR
%shl_const(32)
OR
ADD // OR
%shl_const(32)
OR
ADD // OR
// stack: concat(a[0]+a[64], b[0]+b[64], c[0]+c[64], d[0]+d[64], e[0]+e[64], f[0]+f[64], g[0]+g[64], h[0]+h[64]), scratch_space_addr, message_schedule_addr, i, retdest
SWAP3
// stack: i, scratch_space_addr, message_schedule_addr, concat(a[0]+a[64], b[0]+b[64], c[0]+c[64], d[0]+d[64], e[0]+e[64], f[0]+f[64], g[0]+g[64], h[0]+h[64]), retdest

View File

@ -79,21 +79,21 @@
DUP2
%increment
%mload_kernel($segment)
OR
ADD // OR
// stack: (c_3 << 8) | c_2, offset
%shl_const(8)
// stack: ((c_3 << 8) | c_2) << 8, offset
DUP2
%add_const(2)
%mload_kernel($segment)
OR
ADD // OR
// stack: (((c_3 << 8) | c_2) << 8) | c_1, offset
%shl_const(8)
// stack: ((((c_3 << 8) | c_2) << 8) | c_1) << 8, offset
SWAP1
%add_const(3)
%mload_kernel($segment)
OR
ADD // OR
// stack: (((((c_3 << 8) | c_2) << 8) | c_1) << 8) | c_0
%endmacro
@ -107,19 +107,19 @@
%increment
%mload_kernel($segment)
%shl_const(8)
OR
ADD
// stack: c0 | (c1 << 8) , offset
DUP2
%add_const(2)
%mload_kernel($segment)
%shl_const(16)
OR
ADD
// stack: c0 | (c1 << 8) | (c2 << 16), offset
SWAP1
%add_const(3)
%mload_kernel($segment)
%shl_const(24)
OR
ADD // OR
// stack: c0 | (c1 << 8) | (c2 << 16) | (c3 << 24)
%endmacro
@ -137,7 +137,7 @@
// stack: hi, lo
%shl_const(32)
// stack: hi << 32, lo
OR
ADD // OR
// stack: (hi << 32) | lo
%endmacro
@ -152,49 +152,49 @@
DUP2
%add_const(4)
%mload_kernel_u32($segment)
OR
ADD // OR
// stack: (c_7 << 32) | c_6, offset
%shl_const(32)
// stack: ((c_7 << 32) | c_6) << 32, offset
DUP2
%add_const(8)
%mload_kernel_u32($segment)
OR
ADD // OR
// stack: (c_7 << 64) | (c_6 << 32) | c_5, offset
%shl_const(32)
// stack: ((c_7 << 64) | (c_6 << 32) | c_5) << 32, offset
DUP2
%add_const(12)
%mload_kernel_u32($segment)
OR
ADD // OR
// stack: (c_7 << 96) | (c_6 << 64) | (c_5 << 32) | c_4, offset
%shl_const(32)
// stack: ((c_7 << 96) | (c_6 << 64) | (c_5 << 32) | c_4) << 32, offset
DUP2
%add_const(16)
%mload_kernel_u32($segment)
OR
ADD // OR
// stack: (c_7 << 128) | (c_6 << 96) | (c_5 << 64) | (c_4 << 32) | c_3, offset
%shl_const(32)
// stack: ((c_7 << 128) | (c_6 << 96) | (c_5 << 64) | (c_4 << 32) | c_3) << 32, offset
DUP2
%add_const(20)
%mload_kernel_u32($segment)
OR
ADD // OR
// stack: (c_7 << 160) | (c_6 << 128) | (c_5 << 96) | (c_4 << 64) | (c_3 << 32) | c_2, offset
%shl_const(32)
// stack: ((c_7 << 160) | (c_6 << 128) | (c_5 << 96) | (c_4 << 64) | (c_3 << 32) | c_2) << 32, offset
DUP2
%add_const(24)
%mload_kernel_u32($segment)
OR
ADD // OR
// stack: (c_7 << 192) | (c_6 << 160) | (c_5 << 128) | (c_4 << 96) | (c_3 << 64) | (c_2 << 32) | c_1, offset
%shl_const(32)
// stack: ((c_7 << 192) | (c_6 << 160) | (c_5 << 128) | (c_4 << 96) | (c_3 << 64) | (c_2 << 32) | c_1) << 32, offset
DUP2
%add_const(28)
%mload_kernel_u32($segment)
OR
ADD // OR
// stack: (c_7 << 224) | (c_6 << 192) | (c_5 << 160) | (c_4 << 128) | (c_3 << 96) | (c_2 << 64) | (c_1 << 32) | c_0, offset
SWAP1
POP

View File

@ -309,9 +309,9 @@
BYTE
%shl_const(24)
// stack: d000, b0, a, c00
OR
OR
OR
ADD // OR
ADD // OR
ADD // OR
// stack: dcba
%endmacro
@ -332,7 +332,7 @@
%reverse_bytes_u32
// stack: word_lo_inverted, word_hi_inverted
%shl_const(32)
OR
ADD // OR
// stack: word_inverted
%endmacro
@ -341,7 +341,7 @@
// stack: a, b, c, d
%rep 3
%shl_const(64)
OR
ADD // OR
%endrep
// stack: a || b || c || d
%endmacro