278 lines
7.5 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
/// def hash(state, _block):
///
/// 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
/// u32(state[1] + stateL[2] + stateR[3]),
/// u32(state[2] + stateL[3] + stateR[4]),
/// u32(state[3] + stateL[4] + stateR[0]),
/// u32(state[4] + stateL[0] + stateR[1]),
/// u32(state[0] + stateL[1] + stateR[2])
///
/// In mix, we denote state[i], stateL[i], stateR[i] by si, li, ri
2022-08-23 12:03:06 -07:00
global hash:
2022-09-09 17:38:36 -07:00
JUMPDEST
2022-08-23 12:03:06 -07:00
// stack: *state, retdest
2022-09-09 17:38:36 -07:00
PUSH switch
PUSH 1
PUSH 5
PUSH 16
PUSH 0
PUSH 0
2022-08-25 15:38:18 -07:00
// stack: 0, 0, 16, 5, 1, switch, *state, retdest
2022-09-09 17:38:36 -07:00
DUP11
DUP11
DUP11
DUP11
DUP11
2022-08-25 15:38:18 -07:00
// stack: *state, 0, 0, 16, 5, 1, switch, *state, retdest
2022-08-23 12:03:06 -07:00
%jump(loop)
switch:
2022-09-09 17:38:36 -07:00
JUMPDEST
2022-08-23 12:03:06 -07:00
// stack: *stateL, *state, retdest
2022-09-09 17:38:36 -07:00
PUSH mix
PUSH 0
PUSH 5
PUSH 16
PUSH 0
PUSH 0
2022-08-25 15:38:18 -07:00
// stack: 0, 0, 16, 5, 0, mix, *stateL, *state, retdest
2022-09-09 17:38:36 -07:00
DUP16
DUP16
DUP16
DUP16
DUP16
2022-08-25 15:38:18 -07:00
// stack: *state, 0, 0, 16, 5, 0, mix, *stateL, *state, retdest
2022-08-23 12:03:06 -07:00
%jump(loop)
mix:
2022-09-09 17:38:36 -07:00
JUMPDEST
2022-08-23 12:03:06 -07:00
// stack: r0, r1, r2, r3, r4, l0, l1, l2, l3, l4, s0, s1, s2, s3, s4, retdest
2022-09-09 17:38:36 -07:00
SWAP10
2022-08-23 12:03:06 -07:00
// stack: s0, r1, r2, r3, r4, l0, l1, l2, l3, l4, r0, s1, s2, s3, s4, retdest
2022-09-09 17:38:36 -07:00
SWAP1
2022-08-23 12:03:06 -07:00
// stack: r1, s0, r2, r3, r4, l0, l1, l2, l3, l4, r0, s1, s2, s3, s4, retdest
2022-09-09 17:38:36 -07:00
SWAP6
2022-08-23 12:03:06 -07:00
// stack: l1, s0, r2, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, retdest
%add3_32
// stack: s0+l1+r2, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, retdest
2022-09-09 17:38:36 -07:00
SWAP13
2022-08-23 12:03:06 -07:00
// stack: retdest, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, s0+l1+r2
2022-09-09 17:38:36 -07:00
SWAP11
2022-08-23 12:03:06 -07:00
// stack: s3, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, retdest, s4, s0+l1+r2
2022-09-09 17:38:36 -07:00
SWAP10
2022-08-23 12:03:06 -07:00
// stack: s2, r3, r4, l0, r1, l2, l3, l4, r0, s1, s3, retdest, s4, s0+l1+r2
2022-09-09 17:38:36 -07:00
SWAP1
2022-08-23 12:03:06 -07:00
// stack: r3, s2, r4, l0, r1, l2, l3, l4, r0, s1, s3, retdest, s4, s0+l1+r2
2022-09-09 17:38:36 -07:00
SWAP6
2022-08-23 12:03:06 -07:00
// stack: l3, s2, r4, l0, r1, l2, r3, l4, r0, s1, s3, retdest, s4, s0+l1+r2
%add3_32
// stack: s2+l3+r4, l0, r1, l2, r3, l4, r0, s1, s3, retdest, s4, s0+l1+r2
2022-09-09 17:38:36 -07:00
SWAP8
2022-08-23 12:03:06 -07:00
// stack: s3, l0, r1, l2, r3, l4, r0, s1, s2+l3+r4, retdest, s4, s0+l1+r2
2022-09-09 17:38:36 -07:00
SWAP10
2022-08-23 12:03:06 -07:00
// stack: s4, l0, r1, l2, r3, l4, r0, s1, s2+l3+r4, retdest, s3, s0+l1+r2
%add3_32
// stack: s4+l0+r1, l2, r3, l4, r0, s1, s2+l3+r4, retdest, s3, s0+l1+r2
2022-09-09 17:38:36 -07:00
SWAP8
2022-08-23 12:03:06 -07:00
// stack: s3, l2, r3, l4, r0, s1, s2+l3+r4, retdest, s4+l0+r1, s0+l1+r2
2022-09-09 17:38:36 -07:00
SWAP5
2022-08-23 12:03:06 -07:00
// stack: s1, l2, r3, l4, r0, s3, s2+l3+r4, retdest, s4+l0+r1, s0+l1+r2
%add3_32
// stack: s1+l2+r3, l4, r0, s3, s2+l3+r4, retdest, s4+l0+r1, s0+l1+r2
2022-09-09 17:38:36 -07:00
SWAP3
2022-08-23 12:03:06 -07:00
// stack: s3, l4, r0, s1+l2+r3, s2+l3+r4, retdest, s4+l0+r1, s0+l1+r2
%add3_32
// stack: s3+l4+r0, s1+l2+r3, s2+l3+r4, retdest, s4+l0+r1, s0+l1+r2
2022-09-09 17:38:36 -07:00
SWAP3
2022-08-23 12:03:06 -07:00
// stack: retdest, s1+l2+r3, s2+l3+r4, s3+l4+r0, s4+l0+r1, s0+l1+r2
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():
/// F = load_F(sides, rounds)
/// K = load_K(sides, rounds)
///
/// def round(*state, rounds, sides):
/// while boxes:
/// box(*state, F, K)
/// boxes -= 1
/// boxes = 16
/// rounds -= 1
loop:
2022-09-09 17:38:36 -07:00
JUMPDEST
2022-08-25 15:38:18 -07:00
// stack: *state, F, K, 16, rounds, sides, retdest
2022-09-09 17:38:36 -07:00
DUP9
2022-08-25 15:38:18 -07:00
// stack: round, *state, F, K, 16, rounds, sides, retdest
%jumpi(update_round_vars)
// stack: *state, F, K, 16, 0, sides, retdest
%stack (a, b, c, d, e, F, K, boxes, rounds, sides, retdest) -> (retdest, a, b, c, d, e)
// 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-09 17:38:36 -07:00
JUMPDEST
2022-08-25 15:38:18 -07:00
// stack: *state, F , K , 16, rounds, sides, retdest
2022-09-09 17:38:36 -07:00
DUP9
DUP11
%get_round
DUP1
2022-08-25 15:38:18 -07:00
// stack: rnd, rnd, *state, F , K , 16, rounds, sides, retdest
2022-09-09 17:38:36 -07:00
SWAP7
POP
%push_F
SWAP7
2022-08-25 15:38:18 -07:00
// stack: rnd, rnd, *state, F', K , 16, rounds, sides, retdest
2022-09-09 17:38:36 -07:00
SWAP8
POP
%load_K
SWAP7
POP
2022-08-25 15:38:18 -07:00
// stack: *state, F', K', 16, rounds, sides, retdest
%jump(round)
round:
2022-09-09 17:38:36 -07:00
JUMPDEST
2022-08-25 15:38:18 -07:00
// stack: *state, F, K, boxes, rounds , sides, retdest
2022-09-09 17:38:36 -07:00
DUP8
2022-08-25 15:38:18 -07:00
// stack: boxes, *state, F, K, boxes, rounds , sides, retdest
%jumpi(box)
// stack: *state, F, K, 0, rounds , sides, retdest
2022-09-09 17:38:36 -07:00
SWAP7
POP
PUSH 16
SWAP7
2022-08-25 15:38:18 -07:00
// stack: *state, F, K, 16, rounds , sides, retdest
2022-09-09 17:38:36 -07:00
PUSH 1
DUP10
SUB
SWAP9
POP
2022-08-25 15:38:18 -07:00
// stack: *state, F, K, 16, rounds-1, sides, retdest
%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)
/// r = load_r(box)
/// x = load_block(r)
/// a += x + K
/// s = load_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-09 17:38:36 -07:00
JUMPDEST
2022-08-25 15:38:18 -07:00
// stack: a, b, c, d, e, F, K, boxes, rounds, sides
2022-09-09 17:38:36 -07:00
PUSH pre_rol
DUP5
DUP5
DUP5
DUP10
2022-08-25 15:38:18 -07:00
// stack: F, b, c, d, pre_rol, a, b, c, d, e, F, K, boxes, rounds, sides
2022-09-09 17:38:36 -07:00
JUMP
2022-08-25 15:38:18 -07:00
pre_rol:
2022-09-09 17:38:36 -07:00
JUMPDEST
2022-08-25 15:38:18 -07:00
// stack: F(b, c, d), a, b, c, d, e, F, K, boxes, rounds, sides
2022-09-09 17:38:36 -07:00
ADD
2022-08-25 15:38:18 -07:00
// stack: a, b, c, d, e, F, K, boxes, rounds, sides
2022-08-25 16:18:45 -07:00
%get_box_from_stack
2022-08-25 15:38:18 -07:00
// stack: box, a, b, c, d, e, F, K, boxes, rounds, sides
2022-09-09 17:38:36 -07:00
DUP1
%load_r
2022-08-25 15:38:18 -07:00
// stack: r, box, a, b, c, d, e, F, K, boxes, rounds, sides
2022-08-25 16:18:45 -07:00
%mload_kernel(@SEGMENT_KERNEL_GENERAL)
2022-08-25 15:38:18 -07:00
// stack: x, box, a, b, c, d, e, F, K, boxes, rounds, sides
2022-09-09 17:38:36 -07:00
SWAP1
SWAP2
2022-08-25 15:38:18 -07:00
// stack: a, x, box, b, c, d, e, F, K, boxes, rounds, sides
2022-09-09 17:38:36 -07:00
ADD
DUP8
ADD
%u32
2022-08-25 15:38:18 -07:00
// stack: a, box, b, c, d, e, F, K, boxes, rounds, sides
2022-09-09 17:38:36 -07:00
PUSH mid_rol
SWAP2
2022-08-25 15:38:18 -07:00
// stack: box, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides
%load_s
// stack: s, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides
2022-09-08 19:35:37 -07:00
%jump(rol)
2022-08-25 15:38:18 -07:00
mid_rol:
2022-09-09 17:38:36 -07:00
JUMPDEST
2022-08-25 15:38:18 -07:00
// stack: a, b, c, d, e, F, K, boxes, rounds, sides
2022-09-09 17:38:36 -07:00
DUP5
2022-08-25 15:38:18 -07:00
// stack: e, a, b, c, d, e, F, K, boxes, rounds, sides
2022-09-09 17:38:36 -07:00
ADD
%u32
2022-08-25 15:38:18 -07:00
// stack: a, b, c, d, e, F, K, boxes, rounds, sides
2022-09-09 17:38:36 -07:00
SWAP1
SWAP2
PUSH post_rol
SWAP1
PUSH 10
2022-08-25 15:38:18 -07:00
// stack: 10, c, post_rol, b, a, d, e, F, K, boxes, rounds, sides
2022-09-08 19:35:37 -07:00
%jump(rol)
2022-08-25 15:38:18 -07:00
post_rol:
2022-09-09 17:38:36 -07:00
JUMPDEST
2022-08-25 16:18:45 -07:00
// stack: c, a, b, d, e, F, K, boxes , rounds, sides
2022-09-09 17:38:36 -07:00
SWAP3
2022-08-25 16:18:45 -07:00
// stack: d, a, b, c, e, F, K, boxes , rounds, sides
2022-09-09 17:38:36 -07:00
SWAP4
2022-08-25 16:18:45 -07:00
// stack: e, a, b, c, d, F, K, boxes , rounds, sides
2022-09-09 17:38:36 -07:00
SWAP7
PUSH 1
SWAP1
SUB
SWAP7
2022-08-25 15:38:18 -07:00
// stack: e, a, b, c, d, F, K, boxes-1, rounds, sides
%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-08-25 16:18:45 -07:00
%macro get_box_from_stack
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