remove jumpdests / add macro

This commit is contained in:
Dmitry Vagner 2022-09-16 19:18:26 -07:00
parent 5163968747
commit fa69f2a7fb
5 changed files with 140 additions and 131 deletions

View File

@ -88,7 +88,7 @@
// Load a little-endian u32, consisting of 4 bytes (c_0, c_1, c_2, c_3),
// from kernel code.
%macro mload_kernel_code_LE_u32
%macro mload_kernel_code_u32_LE
// stack: offset
DUP1
%mload_kernel_code

View File

@ -21,12 +21,11 @@
/// u32(s4 + l0 + r1),
/// u32(s0 + l1 + r2)
///
/// where si, li, ri, oi, OS, RD respectively denote
/// state[i], stateL[i], stateR[i], output[i], offset, retdest
/// where si, li, ri, oi, BL, RD respectively denote
/// state[i], stateL[i], stateR[i], output[i], block, retdest
global hash:
JUMPDEST
// stack: *state, offset, retdest
// stack: *state, block, retdest
PUSH switch
DUP7
PUSH 1
@ -34,17 +33,16 @@ global hash:
PUSH 16
PUSH 0
PUSH 0
// stack: 0, 0, 16, 5, 1, offset, switch, *state, offset, retdest
// stack: 0, 0, 16, 5, 1, block, switch, *state, block, retdest
DUP12
DUP12
DUP12
DUP12
DUP12
// stack: *state, 0, 0, 16, 5, 1, offset, switch, *state, offset, retdest
// stack: *state, 0, 0, 16, 5, 1, block, switch, *state, block, retdest
%jump(loop)
switch:
JUMPDEST
// stack: *stateL, *state, offset, retdest
// stack: *stateL, *state, block, retdest
PUSH mix
DUP12
PUSH 0
@ -52,57 +50,56 @@ switch:
PUSH 16
PUSH 0
PUSH 0
// stack: 0, 0, 16, 5, 0, offset, mix, *stateL, *state, offset, retdest
// stack: 0, 0, 16, 5, 0, block, mix, *stateL, *state, block, retdest
DUP17
DUP17
DUP17
DUP17
DUP17
// stack: *state, 0, 0, 16, 5, 0, offset, mix, *stateL, *state, offset, retdest
// stack: *state, 0, 0, 16, 5, 0, block, mix, *stateL, *state, block, retdest
%jump(loop)
mix:
JUMPDEST
// stack: r0, r1, r2, r3, r4, l0, l1, l2, l3, l4, s0, s1, s2, s3, s4, OS, RD
// stack: r0, r1, r2, r3, r4, l0, l1, l2, l3, l4, s0, s1, s2, s3, s4, BL, RD
SWAP10
// stack: s0, r1, r2, r3, r4, l0, l1, l2, l3, l4, r0, s1, s2, s3, s4, OS, RD
// stack: s0, r1, r2, r3, r4, l0, l1, l2, l3, l4, r0, s1, s2, s3, s4, BL, RD
SWAP1
// stack: r1, s0, r2, r3, r4, l0, l1, l2, l3, l4, r0, s1, s2, s3, s4, OS, RD
// stack: r1, s0, r2, r3, r4, l0, l1, l2, l3, l4, r0, s1, s2, s3, s4, BL, RD
SWAP6
// stack: l1, s0, r2, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, OS, RD
// stack: l1, s0, r2, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, BL, RD
%add3_32
// stack: o4, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, OS, RD
// stack: o4, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, BL, RD
SWAP14
// stack: RD, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, OS, o4
// stack: RD, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, BL, o4
SWAP11
// stack: s3, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, RD, s4, OS, o4
// stack: s3, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, RD, s4, BL, o4
SWAP10
// stack: s2, r3, r4, l0, r1, l2, l3, l4, r0, s1, s3, RD, s4, OS, o4
// stack: s2, r3, r4, l0, r1, l2, l3, l4, r0, s1, s3, RD, s4, BL, o4
SWAP1
// stack: r3, s2, r4, l0, r1, l2, l3, l4, r0, s1, s3, RD, s4, OS, o4
// stack: r3, s2, r4, l0, r1, l2, l3, l4, r0, s1, s3, RD, s4, BL, o4
SWAP6
// stack: l3, s2, r4, l0, r1, l2, r3, l4, r0, s1, s3, RD, s4, OS, o4
// stack: l3, s2, r4, l0, r1, l2, r3, l4, r0, s1, s3, RD, s4, BL, o4
%add3_32
// stack: o1, l0, r1, l2, r3, l4, r0, s1, s3, RD, s4, OS, o4
// stack: o1, l0, r1, l2, r3, l4, r0, s1, s3, RD, s4, BL, o4
SWAP9
// stack: RD, l0, r1, l2, r3, l4, r0, s1, s3, o1, s4, OS, o4
// stack: RD, l0, r1, l2, r3, l4, r0, s1, s3, o1, s4, BL, o4
SWAP10
// stack: s4, l0, r1, l2, r3, l4, r0, s1, s3, o1, RD, OS, o4
// stack: s4, l0, r1, l2, r3, l4, r0, s1, s3, o1, RD, BL, o4
%add3_32
// stack: o3, l2, r3, l4, r0, s1, s3, o1, RD, OS, o4
// stack: o3, l2, r3, l4, r0, s1, s3, o1, RD, BL, o4
SWAP9
// stack: OS, l2, r3, l4, r0, s1, s3, o1, RD, o3, o4
// stack: BL, l2, r3, l4, r0, s1, s3, o1, RD, o3, o4
SWAP5
// stack: s1, l2, r3, l4, r0, OS, s3, o1, RD, o3, o4
// stack: s1, l2, r3, l4, r0, BL, s3, o1, RD, o3, o4
%add3_32
// stack: o0, l4, r0, OS, s3, o1, RD, o3, o4
// stack: o0, l4, r0, BL, s3, o1, RD, o3, o4
SWAP4
// stack: s3, l4, r0, OS, o0, o1, RD, o3, o4
// stack: s3, l4, r0, BL, o0, o1, RD, o3, o4
%add3_32
// stack: o2, OS, o0, o1, RD, o3, o4
// stack: o2, BL, o0, o1, RD, o3, o4
SWAP4
// stack: RD, OS, o0, o1, o2, o3, o4
// stack: RD, BL, o0, o1, o2, o3, o4
SWAP1
// stack: OS, RD, o0, o1, o2, o3, o4
// stack: BL, RD, o0, o1, o2, o3, o4
POP
// stack: RD, o0, o1, o2, o3, o4
JUMP
@ -114,8 +111,8 @@ mix:
/// round(*state, F, K, rounds, sides)
///
/// def update_round_vars():
/// F = load_F(sides, rounds)
/// K = load_K(sides, rounds)
/// F = load(F)(sides, rounds)
/// K = load(K)(sides, rounds)
///
/// def round(*state, rounds, sides):
/// while boxes:
@ -125,54 +122,51 @@ mix:
/// rounds -= 1
loop:
JUMPDEST
// stack: *state, F, K, 16, rounds, sides, offset, retdest
loop:
// stack: *state, F, K, 16, rounds, sides, block, retdest
DUP9
// stack: round, *state, F, K, 16, rounds, sides, offset, retdest
// stack: round, *state, F, K, 16, rounds, sides, block, retdest
%jumpi(update_round_vars)
// stack: *state, F, K, 16, 0, sides, offset, retdest
%stack (a, b, c, d, e, F, K, boxes, rounds, sides, offset, retdest) -> (retdest, a, b, c, d, e)
// 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)
// stack: retdest, *state
JUMP
update_round_vars:
JUMPDEST
// stack: *state, F , K , 16, rounds, sides, offset, retdest
// stack: *state, F , K , 16, rounds, sides, block, retdest
DUP9
DUP11
%get_round
DUP1
// stack: rnd, rnd, *state, F , K , 16, rounds, sides, offset, retdest
// stack: rnd, rnd, *state, F , K , 16, rounds, sides, block, retdest
SWAP7
POP
%push_F
SWAP7
// stack: rnd, rnd, *state, F', K , 16, rounds, sides, offset, retdest
// stack: rnd, rnd, *state, F', K , 16, rounds, sides, block, retdest
SWAP8
POP
%load_K
%load_u32(K_data)
SWAP7
POP
// stack: *state, F', K', 16, rounds, sides, offset, retdest
// stack: *state, F', K', 16, rounds, sides, block, retdest
%jump(round)
round:
JUMPDEST
// stack: *state, F, K, boxes, rounds , sides, offset, retdest
// stack: *state, F, K, boxes, rounds , sides, block, retdest
DUP8
// stack: boxes, *state, F, K, boxes, rounds , sides, offset, retdest
// stack: boxes, *state, F, K, boxes, rounds , sides, block, retdest
%jumpi(box)
// stack: *state, F, K, 0, rounds , sides, offset, retdest
// stack: *state, F, K, 0, rounds , sides, block, retdest
SWAP7
POP
PUSH 16
SWAP7
// stack: *state, F, K, 16, rounds , sides, offset, retdest
// stack: *state, F, K, 16, rounds , sides, block, retdest
PUSH 1
DUP10
SUB
SWAP9
POP
// stack: *state, F, K, 16, rounds-1, sides, offset, retdest
// stack: *state, F, K, 16, rounds-1, sides, block, retdest
%jump(loop)
@ -195,70 +189,66 @@ round:
box:
JUMPDEST
// stack: a, b, c, d, e, F, K, boxes, rounds, sides, offset
// stack: a, b, c, d, e, F, K, boxes, rounds, sides, block
PUSH pre_rol
DUP5
DUP5
DUP5
DUP10
// stack: F, b, c, d, pre_rol, a, b, c, d, e, F, K, boxes, rounds, sides, offset
// stack: F, b, c, d, pre_rol, a, b, c, d, e, F, K, boxes, rounds, sides, block
JUMP
pre_rol:
JUMPDEST
// stack: F(b, c, d), a, b, c, d, e, F, K, boxes, rounds, sides, offset
// stack: F(b, c, d), a, b, c, d, e, F, K, boxes, rounds, sides, block
ADD
// stack: a, b, c, d, e, F, K, boxes, rounds, sides, offset
%get_box_from_stack
// stack: box, a, b, c, d, e, F, K, boxes, rounds, sides, offset
// stack: a, b, c, d, e, F, K, boxes, rounds, sides, block
%get_box
// stack: box, a, b, c, d, e, F, K, boxes, rounds, sides, block
DUP12
DUP2
%load_r
// stack: r, offset, box, a, b, c, d, e, F, K, boxes, rounds, sides, offset
%load_byte(R_data)
// stack: r, block, box, a, b, c, d, e, F, K, boxes, rounds, sides, block
%load_block
// stack: x, box, a, b, c, d, e, F, K, boxes, rounds, sides, offset
// stack: x, box, a, b, c, d, e, F, K, boxes, rounds, sides, block
SWAP1
SWAP2
// stack: a, x, box, b, c, d, e, F, K, boxes, rounds, sides, offset
// stack: a, x, box, b, c, d, e, F, K, boxes, rounds, sides, block
ADD
DUP8
ADD
%u32
// stack: a, box, b, c, d, e, F, K, boxes, rounds, sides, offset
// stack: a, box, b, c, d, e, F, K, boxes, rounds, sides, block
PUSH mid_rol
SWAP2
// stack: box, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides, offset
%load_s
// stack: s, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides, offset
// 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
%jump(rol)
mid_rol:
JUMPDEST
// stack: a, b, c, d, e, F, K, boxes, rounds, sides, offset
// stack: a, b, c, d, e, F, K, boxes, rounds, sides, block
DUP5
// stack: e, a, b, c, d, e, F, K, boxes, rounds, sides, offset
// stack: e, a, b, c, d, e, F, K, boxes, rounds, sides, block
ADD
%u32
// stack: a, b, c, d, e, F, K, boxes, rounds, sides, offset
// stack: a, b, c, d, e, F, K, boxes, rounds, sides, block
SWAP1
SWAP2
PUSH post_rol
SWAP1
PUSH 10
// stack: 10, c, post_rol, b, a, d, e, F, K, boxes, rounds, sides, offset
// stack: 10, c, post_rol, b, a, d, e, F, K, boxes, rounds, sides, block
%jump(rol)
post_rol:
JUMPDEST
// stack: c, a, b, d, e, F, K, boxes , rounds, sides, offset
// stack: c, a, b, d, e, F, K, boxes , rounds, sides, block
SWAP3
// stack: d, a, b, c, e, F, K, boxes , rounds, sides, offset
// stack: d, a, b, c, e, F, K, boxes , rounds, sides, block
SWAP4
// stack: e, a, b, c, d, F, K, boxes , rounds, sides, offset
// stack: e, a, b, c, d, F, K, boxes , rounds, sides, block
SWAP7
PUSH 1
SWAP1
SUB
SWAP7
// stack: e, a, b, c, d, F, K, boxes-1, rounds, sides, offset
// stack: e, a, b, c, d, F, K, boxes-1, rounds, sides, block
%jump(round)
@ -269,7 +259,7 @@ post_rol:
%endmacro
%macro get_box_from_stack
%macro get_box
// stack: *7_args, boxes, rounds, sides
DUP10
%mul_const(80)
@ -286,6 +276,6 @@ post_rol:
%macro load_block
// stack: r, offset
// stack: r, block
%mload_kernel(@SEGMENT_KERNEL_GENERAL)
%endmacro

View File

@ -1,9 +1,11 @@
%macro load_K
%macro load_u32(loc)
// stack: rnd
%mul_const(4) push K_data add
// stack: K_data + 4*rnd
%mul_const(4)
push $loc
ADD
// stack: loc + 4*rnd
%mload_kernel_code_u32
// stack: K
// stack: u32
%end_macro
K_data:
@ -21,76 +23,70 @@ K_data:
BYTES 0x00, 0x00, 0x00, 0x00
%macro load_s
%macro load_byte(loc)
// stack: box
push S_data add
// stack: S_data + box
PUSH $loc
ADD
// stack: loc + box
%mload_kernel_code
// stack: s
%end_macro
// stack: u8
%endmacro
S_data:
// Left Round 1
// Left Round 0
BYTES 11, 14, 15, 12
BYTES 05, 08, 07, 09
BYTES 11, 13, 14, 15
BYTES 06, 07, 09, 08
// Left Round 2
// Left Round 1
BYTES 07, 06, 08, 13
BYTES 11, 09, 07, 15
BYTES 07, 12, 15, 09
BYTES 11, 07, 13, 12
// Left Round 3
// Left Round 2
BYTES 11, 13, 06, 07
BYTES 14, 09, 13, 15
BYTES 14, 08, 13, 06
BYTES 05, 12, 07, 05
// Left Round 4
// Left Round 3
BYTES 11, 12, 14, 15
BYTES 14, 15, 09, 08
BYTES 09, 14, 05, 06
BYTES 08, 06, 05, 12
// Left Round 5
// Left Round 4
BYTES 09, 15, 05, 11
BYTES 06, 08, 13, 12
BYTES 05, 12, 13, 14
BYTES 11, 08, 05, 06
// Right Round 1
// Right Round 0
BYTES 08, 09, 09, 11
BYTES 13, 15, 15, 05
BYTES 07, 07, 08, 11
BYTES 14, 14, 12, 06
// Right Round 2
// Right Round 1
BYTES 09, 13, 15, 07
BYTES 12, 08, 09, 11
BYTES 07, 07, 12, 07
BYTES 06, 15, 13, 11
// Right Round 3
// Right Round 2
BYTES 09, 07, 15, 11
BYTES 08, 06, 06, 14
BYTES 12, 13, 05, 14
BYTES 13, 13, 07, 05
// Right Round 4
// Right Round 3
BYTES 15, 05, 08, 11
BYTES 14, 14, 06, 14
BYTES 06, 09, 12, 09
BYTES 12, 05, 15, 08
// Right Round 5
// Right Round 4
BYTES 08, 05, 12, 09
BYTES 12, 05, 14, 06
BYTES 08, 13, 06, 05
BYTES 15, 13, 11, 11
%macro load_r
// stack: box
push R_data add
// stack: R_data + box
%mload_kernel_code
// stack: r
%end_macro
R_data:
// Left Round 0
BYTES 00, 04, 08, 12

View File

@ -38,3 +38,26 @@ process:
POP
// stack: retdest, e'd'c'b'a'
JUMP
/// def padlength(count):
/// x = 56 - (count // 8) % 64
/// return x + 64*(x < 9)
%macro padlength
// stack: count
%div_const(8)
// stack: count // 8
%mod_const(64)
// stack: (count // 8) % 64
PUSH 56
SUB
// stack: x = 56 - (count // 8) % 64
DUP1
%lt_const(9)
// stack: x < 9 , x
%mul_const(64)
// stack: 64*(x < 9) , x
ADD
// stack: 64*(x < 9) + x
%endmacro

View File

@ -40,15 +40,15 @@ global rol:
%macro this_F(i, F)
// stack: acc, rnd
// stack: acc, rnd
DUP2
// stack: rnd, acc, rnd
%eq_const(i)
// stack: rnd==i, acc, j
%mul_const(F)
// stack: (rnd==i)*F, acc, rnd
// stack: rnd , acc, rnd
%eq_const($i)
// stack: rnd==i , acc, j
%mul_const($F)
// stack: (rnd==i)*F , acc, rnd
ADD
// stack: acc + (rnd==j)*F, rnd
// stack: (rnd==j)*F + acc, rnd
%endmacro
@ -71,19 +71,19 @@ global F0:
global F1:
// stack: x, y, z, retdest
DUP1
// stack: x, x, y, z, retdest
// stack: x, x, y, z, retdest
SWAP2
// stack: y, x, x, z, retdest
// stack: y, x, x, z, retdest
AND
// stack: y & x, x, z, retdest
SWAP2
// stack: z, x, y & x, retdest
// stack: z, x, y & x , retdest
SWAP1
// stack: x, z, y & x, retdest
// stack: x, z, y & x , retdest
%not_32
// stack: ~x, z, y & x, retdest
// stack: ~x, z, y & x , retdest
AND
// stack: ~x & z, y & x, retdest
// stack: ~x & z , y & x , retdest
OR
// stack: (~x & z) | (y & x), retdest
SWAP1
@ -94,13 +94,13 @@ global F1:
/// return (x | u32(~y)) ^ z
global F2:
// stack: x, y, z, retdest
// stack: x , y, z, retdest
SWAP1
// stack: y, x, z, retdest
// stack: y , x, z, retdest
%not_32
// stack: ~y, x, z, retdest
// stack: ~y , x , z, retdest
OR
// stack: ~y | x, z, retdest
// stack: ~y | x , z, retdest
XOR
// stack: (~y | x) ^ z, retdest
SWAP1
@ -111,17 +111,17 @@ global F2:
/// return (x & z) | (u32(~z) & y)
global F3:
// stack: x, y, z, retdest
// stack: x, y , z , retdest
DUP3
// stack: z, x, y, z, retdest
// stack: z , x, y , z , retdest
AND
// stack: z & x, y, z, retdest
// stack: z & x, y , z , retdest
SWAP2
// stack: z, y, z & x, retdest
// stack: z, y, z & x , retdest
%not_32
// stack: ~z, y, z & x, retdest
// stack: ~z , y, z & x , retdest
AND
// stack: ~z & y, z & x, retdest
// stack: ~z & y, z & x , retdest
OR
// stack: (~z & y) | (z & x), retdest
SWAP1