addressed comments

This commit is contained in:
Nicholas Ward 2022-09-14 11:29:13 -07:00
parent 78a4b92e83
commit 7eba422792
3 changed files with 46 additions and 101 deletions

View File

@ -3,21 +3,15 @@
// stack: x, y
ADD
// stack: x + y
DUP1
// stack: x + y, x + y
%shr_const(32)
// stack: (x + y) >> 32, x + y
%shl_const(32)
// stack: ((x + y) >> 32) << 32, x + y
SWAP1
// stack: x + y, ((x + y) >> 32) << 32
SUB
// stack: x + y - ((x + y) >> 32) << 32
%and_const(0xFFFFFFFF)
// stack: (x + y) & u32::MAX
%endmacro
// 32-bit right rotation
%macro rotr
%macro rotr(rot)
// stack: value
PUSH $rot
// stack: rot, value
DUP2
DUP2
@ -42,43 +36,15 @@
ADD
%endmacro
// 32-bit left rotation
%macro rotl
// stack: rot, value
DUP2
DUP2
// stack: rot, value, rot, value
PUSH 32
SUB
// stack: 32 - rot, value, rot, value
SHR
// stack: value >> (32 - rot), rot, value
%stack (shifted, rot, value) -> (rot, value, shifted)
// stack: rot, value, value >> (32 - rot)
SHL
// stack: value << rot, value >> (32 - rot)
PUSH 32
PUSH 1
SWAP1
SHL
// stack: 1 << 32, value << rot, value >> (32 - rot)
SWAP1
MOD
// stack: (value << rot) % (1 << 32), value >> (32 - rot)
ADD
%endmacro
%macro sha2_sigma_0
// stack: x
DUP1
// stack: x, x
PUSH 7
%rotr
%rotr(7)
// stack: rotr(x, 7), x
%stack (rotated, x) -> (x, x, rotated)
// stack: x, x, rotr(x, 7)
PUSH 18
%rotr
%rotr(18)
// stack: rotr(x, 18), x, rotr(x, 7)
SWAP1
// stack: x, rotr(x, 18), rotr(x, 7)
@ -93,13 +59,11 @@
// stack: x
DUP1
// stack: x, x
PUSH 17
%rotr
%rotr(17)
// stack: rotr(x, 17), x
%stack (rotated, x) -> (x, x, rotated)
// stack: x, x, rotr(x, 17)
PUSH 19
%rotr
%rotr(19)
// stack: rotr(x, 19), x, rotr(x, 17)
SWAP1
// stack: x, rotr(x, 19), rotr(x, 17)
@ -114,18 +78,15 @@
// stack: x
DUP1
// stack: x, x
PUSH 2
%rotr
%rotr(2)
// stack: rotr(x, 2), x
%stack (rotated, x) -> (x, x, rotated)
// stack: x, x, rotr(x, 2)
PUSH 13
%rotr
%rotr(13)
// stack: rotr(x, 13), x, rotr(x, 2)
SWAP1
// stack: x, rotr(x, 13), rotr(x, 2)
PUSH 22
%rotr
%rotr(22)
// stack: rotr(x, 22), rotr(x, 13), rotr(x, 2)
XOR
XOR
@ -135,18 +96,15 @@
// stack: x
DUP1
// stack: x, x
PUSH 6
%rotr
%rotr(6)
// stack: rotr(x, 6), x
%stack (rotated, x) -> (x, x, rotated)
// stack: x, x, rotr(x, 6)
PUSH 11
%rotr
%rotr(11)
// stack: rotr(x, 11), x, rotr(x, 6)
SWAP1
// stack: x, rotr(x, 11), rotr(x, 6)
PUSH 25
%rotr
%rotr(25)
// stack: rotr(x, 25), rotr(x, 11), rotr(x, 6)
XOR
XOR

View File

@ -4,7 +4,7 @@
// stack: e, e, f, g, h, K[i], W[i]
%sha2_bigsigma_1
// stack: Sigma_1(e), e, f, g, h, K[i], W[i]
%stack (sig, efg: 3) -> (efg, sig)
%stack (sig, e, f, g) -> (e, f, g, sig)
// stack: e, f, g, Sigma_1(e), h, K[i], W[i]
%sha2_choice
// stack: Ch(e, f, g), Sigma_1(e), h, K[i], W[i]

View File

@ -1,46 +1,46 @@
%macro jump(dst)
push $dst
PUSH $dst
jump
%endmacro
%macro jumpi(dst)
push $dst
PUSH $dst
jumpi
%endmacro
%macro pop2
%rep 2
pop
POP
%endrep
%endmacro
%macro pop3
%rep 3
pop
POP
%endrep
%endmacro
%macro pop4
%rep 4
pop
POP
%endrep
%endmacro
%macro pop5
%rep 5
pop
POP
%endrep
%endmacro
%macro pop6
%rep 6
pop
POP
%endrep
%endmacro
%macro pop7
%rep 7
pop
POP
%endrep
%endmacro
@ -162,21 +162,21 @@
// If pred is zero, yields z; otherwise, yields nz
%macro select
// stack: pred, nz, z
iszero
ISZERO
// stack: pred == 0, nz, z
dup1
DUP1
// stack: pred == 0, pred == 0, nz, z
iszero
ISZERO
// stack: pred != 0, pred == 0, nz, z
swap3
SWAP3
// stack: z, pred == 0, nz, pred != 0
mul
MUL
// stack: (pred == 0) * z, nz, pred != 0
swap2
SWAP2
// stack: pred != 0, nz, (pred == 0) * z
mul
MUL
// stack: (pred != 0) * nz, (pred == 0) * z
add
ADD
// stack: (pred != 0) * nz + (pred == 0) * z
%endmacro
@ -184,27 +184,27 @@
// Assumes pred is boolean (either 0 or 1).
%macro select_bool
// stack: pred, nz, z
dup1
DUP1
// stack: pred, pred, nz, z
iszero
ISZERO
// stack: notpred, pred, nz, z
swap3
SWAP3
// stack: z, pred, nz, notpred
mul
MUL
// stack: pred * z, nz, notpred
swap2
SWAP2
// stack: notpred, nz, pred * z
mul
MUL
// stack: notpred * nz, pred * z
add
ADD
// stack: notpred * nz + pred * z
%endmacro
%macro square
// stack: x
dup1
DUP1
// stack: x, x
mul
MUL
// stack: x^2
%endmacro
@ -231,31 +231,18 @@
%endmacro
%macro increment
push 1
add
%add_const(1)
%endmacro
%macro decrement
push 1
swap1
sub
%sub_const(1)
%endmacro
%macro div2
push 2
swap1
div
%endmacro
%macro lt(x)
push $x
swap1
lt
%div_const(2)
%endmacro
%macro iseven
push 2
swap1
mod
iszero
%mod_const(2)
ISZERO
%endmacro