plonky2/evm/src/cpu/kernel/asm/ripemd/compression.asm

276 lines
8.0 KiB
NASM
Raw Normal View History

2022-08-23 12:03:06 -07:00
/// Variables beginning with _ are not maintained on the stack
/// Note that state takes up 5 stack slots
2022-09-18 10:20:25 -07:00
/// def compression(state, _block):
2022-08-23 12:03:06 -07:00
///
/// stateL = state
/// stateL = loop(stateL)
///
/// stateR = state
/// stateR = loop(stateR)
///
2022-08-25 15:38:18 -07:00
/// return mix(state, stateL, stateR)
///
///
/// def mix(*stateR, *stateL, *state):
/// return
2022-09-13 14:32:41 -07:00
/// u32(s1 + l2 + r3),
/// u32(s2 + l3 + r4),
/// u32(s3 + l4 + r0),
/// u32(s4 + l0 + r1),
/// u32(s0 + l1 + r2)
2022-08-25 15:38:18 -07:00
///
2022-09-16 19:18:26 -07:00
/// where si, li, ri, oi, BL, RD respectively denote
/// state[i], stateL[i], stateR[i], output[i], block, retdest
2022-08-23 12:03:06 -07:00
2022-09-18 10:20:25 -07:00
global compression:
2022-09-16 19:18:26 -07:00
// stack: *state, block, retdest
2022-09-13 14:32:41 -07:00
PUSH switch
DUP7
2022-09-09 17:38:36 -07:00
PUSH 1
PUSH 5
PUSH 16
PUSH 0
PUSH 0
2022-09-16 19:18:26 -07:00
// stack: 0, 0, 16, 5, 1, block, switch, *state, block, retdest
2022-09-13 14:32:41 -07:00
DUP12
DUP12
DUP12
DUP12
DUP12
2022-09-16 19:18:26 -07:00
// stack: *state, 0, 0, 16, 5, 1, block, switch, *state, block, retdest
2022-08-23 12:03:06 -07:00
%jump(loop)
switch:
2022-09-18 10:20:25 -07:00
// stack: *stateL, *state, block, retdest
2022-09-13 14:32:41 -07:00
PUSH mix
DUP12
PUSH 0
2022-09-09 17:38:36 -07:00
PUSH 5
PUSH 16
PUSH 0
PUSH 0
2022-09-18 10:20:25 -07:00
// stack: 0, 0, 16, 5, 0, block, mix, *stateL, *state, block, retdest
2022-09-13 14:32:41 -07:00
DUP17
DUP17
DUP17
DUP17
DUP17
2022-09-16 19:18:26 -07:00
// stack: *state, 0, 0, 16, 5, 0, block, mix, *stateL, *state, block, retdest
2022-08-23 12:03:06 -07:00
%jump(loop)
mix:
2022-09-16 19:18:26 -07:00
// stack: r0, r1, r2, r3, r4, l0, l1, l2, l3, l4, s0, s1, s2, s3, s4, BL, RD
2022-09-09 17:38:36 -07:00
SWAP10
2022-09-16 19:18:26 -07:00
// stack: s0, r1, r2, r3, r4, l0, l1, l2, l3, l4, r0, s1, s2, s3, s4, BL, RD
2022-09-09 17:38:36 -07:00
SWAP1
2022-09-16 19:18:26 -07:00
// stack: r1, s0, r2, r3, r4, l0, l1, l2, l3, l4, r0, s1, s2, s3, s4, BL, RD
2022-09-09 17:38:36 -07:00
SWAP6
2022-09-16 19:18:26 -07:00
// stack: l1, s0, r2, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, BL, RD
2022-08-23 12:03:06 -07:00
%add3_32
2022-09-16 19:18:26 -07:00
// stack: o4, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, BL, RD
2022-09-13 14:32:41 -07:00
SWAP14
2022-09-16 19:18:26 -07:00
// stack: RD, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, BL, o4
2022-09-09 17:38:36 -07:00
SWAP11
2022-09-16 19:18:26 -07:00
// stack: s3, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, RD, s4, BL, o4
2022-09-09 17:38:36 -07:00
SWAP10
2022-09-16 19:18:26 -07:00
// stack: s2, r3, r4, l0, r1, l2, l3, l4, r0, s1, s3, RD, s4, BL, o4
2022-09-09 17:38:36 -07:00
SWAP1
2022-09-16 19:18:26 -07:00
// stack: r3, s2, r4, l0, r1, l2, l3, l4, r0, s1, s3, RD, s4, BL, o4
2022-09-09 17:38:36 -07:00
SWAP6
2022-09-16 19:18:26 -07:00
// stack: l3, s2, r4, l0, r1, l2, r3, l4, r0, s1, s3, RD, s4, BL, o4
2022-08-23 12:03:06 -07:00
%add3_32
2022-09-16 19:18:26 -07:00
// stack: o1, l0, r1, l2, r3, l4, r0, s1, s3, RD, s4, BL, o4
2022-09-13 14:32:41 -07:00
SWAP9
2022-09-16 19:18:26 -07:00
// stack: RD, l0, r1, l2, r3, l4, r0, s1, s3, o1, s4, BL, o4
2022-09-09 17:38:36 -07:00
SWAP10
2022-09-16 19:18:26 -07:00
// stack: s4, l0, r1, l2, r3, l4, r0, s1, s3, o1, RD, BL, o4
2022-08-23 12:03:06 -07:00
%add3_32
2022-09-16 19:18:26 -07:00
// stack: o3, l2, r3, l4, r0, s1, s3, o1, RD, BL, o4
2022-09-13 14:32:41 -07:00
SWAP9
2022-09-16 19:18:26 -07:00
// stack: BL, l2, r3, l4, r0, s1, s3, o1, RD, o3, o4
2022-09-09 17:38:36 -07:00
SWAP5
2022-09-16 19:18:26 -07:00
// stack: s1, l2, r3, l4, r0, BL, s3, o1, RD, o3, o4
2022-08-23 12:03:06 -07:00
%add3_32
2022-09-16 19:18:26 -07:00
// stack: o0, l4, r0, BL, s3, o1, RD, o3, o4
2022-09-13 14:32:41 -07:00
SWAP4
2022-09-16 19:18:26 -07:00
// stack: s3, l4, r0, BL, o0, o1, RD, o3, o4
2022-09-13 14:32:41 -07:00
%add3_32
2022-09-16 19:18:26 -07:00
// stack: o2, BL, o0, o1, RD, o3, o4
2022-09-13 14:32:41 -07:00
SWAP4
2022-09-16 19:18:26 -07:00
// stack: RD, BL, o0, o1, o2, o3, o4
2022-09-13 14:32:41 -07:00
SWAP1
2022-09-16 19:18:26 -07:00
// stack: BL, RD, o0, o1, o2, o3, o4
2022-09-13 14:32:41 -07:00
POP
// stack: RD, o0, o1, o2, o3, o4
2022-09-09 17:38:36 -07:00
JUMP
2022-08-25 15:38:18 -07:00
/// def loop(*state):
/// while rounds:
/// update_round_vars()
/// round(*state, F, K, rounds, sides)
///
/// def update_round_vars():
2022-09-16 19:18:26 -07:00
/// F = load(F)(sides, rounds)
/// K = load(K)(sides, rounds)
2022-08-25 15:38:18 -07:00
///
/// def round(*state, rounds, sides):
/// while boxes:
/// box(*state, F, K)
/// boxes -= 1
/// boxes = 16
/// rounds -= 1
2022-09-16 19:18:26 -07:00
loop:
// stack: *state, F, K, 16, rounds, sides, block, retdest
2022-09-09 17:38:36 -07:00
DUP9
2022-09-16 19:18:26 -07:00
// stack: round, *state, F, K, 16, rounds, sides, block, retdest
2022-08-25 15:38:18 -07:00
%jumpi(update_round_vars)
2022-09-16 19:18:26 -07:00
// stack: *state, F, K, 16, 0, sides, block, retdest
%stack (a, b, c, d, e, F, K, boxes, rounds, sides, block, retdest) -> (retdest, a, b, c, d, e)
2022-08-25 15:38:18 -07:00
// stack: retdest, *state
2022-09-09 17:38:36 -07:00
JUMP
2022-08-25 15:38:18 -07:00
update_round_vars:
2022-09-16 19:18:26 -07:00
// stack: *state, F , K , 16, rounds, sides, block, retdest
2022-09-09 17:38:36 -07:00
DUP9
DUP11
%get_round
DUP1
2022-09-16 19:18:26 -07:00
// stack: rnd, rnd, *state, F , K , 16, rounds, sides, block, retdest
2022-09-09 17:38:36 -07:00
SWAP7
POP
%push_F
SWAP7
2022-09-16 19:18:26 -07:00
// stack: rnd, rnd, *state, F', K , 16, rounds, sides, block, retdest
2022-09-09 17:38:36 -07:00
SWAP8
POP
2022-09-16 19:18:26 -07:00
%load_u32(K_data)
2022-09-09 17:38:36 -07:00
SWAP7
POP
2022-09-16 19:18:26 -07:00
// stack: *state, F', K', 16, rounds, sides, block, retdest
2022-08-25 15:38:18 -07:00
%jump(round)
round:
2022-09-16 19:18:26 -07:00
// stack: *state, F, K, boxes, rounds , sides, block, retdest
2022-09-09 17:38:36 -07:00
DUP8
2022-09-16 19:18:26 -07:00
// stack: boxes, *state, F, K, boxes, rounds , sides, block, retdest
2022-08-25 15:38:18 -07:00
%jumpi(box)
2022-09-16 19:18:26 -07:00
// stack: *state, F, K, 0, rounds , sides, block, retdest
2022-09-09 17:38:36 -07:00
SWAP7
POP
PUSH 16
SWAP7
2022-09-16 19:18:26 -07:00
// stack: *state, F, K, 16, rounds , sides, block, retdest
2022-09-09 17:38:36 -07:00
PUSH 1
DUP10
SUB
SWAP9
POP
2022-09-16 19:18:26 -07:00
// stack: *state, F, K, 16, rounds-1, sides, block, retdest
2022-08-25 15:38:18 -07:00
%jump(loop)
/// Note that we unpack *state to a, b, c, d, e
/// All additions are u32
///
/// def box(a, b, c, d, e, F, K):
///
2022-08-25 16:18:45 -07:00
/// box = get_box(sides, rounds, boxes)
2022-08-25 15:38:18 -07:00
/// a += F(b, c, d)
2022-09-18 10:20:25 -07:00
/// r = load_byte(r)(box)
2022-08-25 15:38:18 -07:00
/// x = load_block(r)
/// a += x + K
2022-09-18 10:20:25 -07:00
/// s = load_byte(s)(box)
2022-09-08 19:35:37 -07:00
/// a = rol(s, a)
2022-08-25 15:38:18 -07:00
/// a += e
2022-09-08 19:35:37 -07:00
/// c = rol(10, c)
2022-08-25 15:38:18 -07:00
///
/// return e, a, b, c, d, F, K
box:
2022-09-16 19:18:26 -07:00
// stack: a, b, c, d, e, F, K, boxes, rounds, sides, block
2022-09-09 17:38:36 -07:00
PUSH pre_rol
DUP5
DUP5
DUP5
DUP10
2022-09-16 19:18:26 -07:00
// stack: F, b, c, d, pre_rol, a, b, c, d, e, F, K, boxes, rounds, sides, block
2022-09-09 17:38:36 -07:00
JUMP
2022-08-25 15:38:18 -07:00
pre_rol:
2022-09-18 10:20:25 -07:00
// stack: F(b, c, d), a, b, c, d, e, F, K, boxes, rounds, sides, block
2022-09-09 17:38:36 -07:00
ADD
2022-09-18 10:20:25 -07:00
// stack: a, b, c, d, e, F, K, boxes, rounds, sides, block
2022-09-16 19:18:26 -07:00
%get_box
2022-09-18 10:20:25 -07:00
// stack: box, a, b, c, d, e, F, K, boxes, rounds, sides, block
DUP1
2022-09-16 19:18:26 -07:00
%load_byte(R_data)
2022-09-18 10:20:25 -07:00
DUP13
// stack: block, r, box, a, b, c, d, e, F, K, boxes, rounds, sides, block
2022-09-13 14:32:41 -07:00
%load_block
2022-09-16 19:18:26 -07:00
// stack: x, box, a, b, c, d, e, F, K, boxes, rounds, sides, block
2022-09-09 17:38:36 -07:00
SWAP1
SWAP2
2022-09-16 19:18:26 -07:00
// stack: a, x, box, b, c, d, e, F, K, boxes, rounds, sides, block
2022-09-09 17:38:36 -07:00
ADD
DUP8
ADD
%u32
2022-09-16 19:18:26 -07:00
// stack: a, box, b, c, d, e, F, K, boxes, rounds, sides, block
2022-09-09 17:38:36 -07:00
PUSH mid_rol
SWAP2
2022-09-16 19:18:26 -07:00
// stack: box, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides, block
%load_byte(S_data)
// stack: s, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides, block
2022-09-08 19:35:37 -07:00
%jump(rol)
2022-08-25 15:38:18 -07:00
mid_rol:
2022-09-16 19:18:26 -07:00
// stack: a, b, c, d, e, F, K, boxes, rounds, sides, block
2022-09-09 17:38:36 -07:00
DUP5
2022-09-16 19:18:26 -07:00
// stack: e, a, b, c, d, e, F, K, boxes, rounds, sides, block
2022-09-09 17:38:36 -07:00
ADD
%u32
2022-09-16 19:18:26 -07:00
// stack: a, b, c, d, e, F, K, boxes, rounds, sides, block
2022-09-09 17:38:36 -07:00
SWAP1
SWAP2
PUSH post_rol
SWAP1
PUSH 10
2022-09-16 19:18:26 -07:00
// stack: 10, c, post_rol, b, a, d, e, F, K, boxes, rounds, sides, block
2022-09-08 19:35:37 -07:00
%jump(rol)
2022-08-25 15:38:18 -07:00
post_rol:
2022-09-16 19:18:26 -07:00
// stack: c, a, b, d, e, F, K, boxes , rounds, sides, block
2022-09-09 17:38:36 -07:00
SWAP3
2022-09-16 19:18:26 -07:00
// stack: d, a, b, c, e, F, K, boxes , rounds, sides, block
2022-09-09 17:38:36 -07:00
SWAP4
2022-09-16 19:18:26 -07:00
// stack: e, a, b, c, d, F, K, boxes , rounds, sides, block
2022-09-09 17:38:36 -07:00
SWAP7
PUSH 1
SWAP1
SUB
SWAP7
2022-09-16 19:18:26 -07:00
// stack: e, a, b, c, d, F, K, boxes-1, rounds, sides, block
2022-08-25 15:38:18 -07:00
%jump(round)
%macro get_round
// stack: sides, rounds
2022-09-09 17:38:36 -07:00
%mul_const(5) PUSH 10 sub sub
2022-08-25 15:38:18 -07:00
// stack: 10 - 5*sides - rounds
2022-09-08 15:47:18 -07:00
%endmacro
2022-08-25 15:38:18 -07:00
2022-09-16 19:18:26 -07:00
%macro get_box
2022-08-25 15:38:18 -07:00
// stack: *7_args, boxes, rounds, sides
2022-09-09 17:38:36 -07:00
DUP10
%mul_const(80)
DUP10
%mul_const(16)
DUP10
2022-08-25 15:38:18 -07:00
// stack: boxes , 16*rounds , 80*sides, *7_args, boxes, rounds, sides
2022-09-09 17:38:36 -07:00
PUSH 176
SUB
SUB
SUB
// stack: 176 - boxes - 16*rounds - 80*sides, *7_args, boxes, rounds, sides
2022-09-08 15:47:18 -07:00
%endmacro