110 lines
2.5 KiB
NASM
Raw Normal View History

2022-07-14 09:29:29 -07:00
global count_bits:
// stack: n (assumed to be > 0)
push 0
// stack: 0, n
swap1
// stack: n, 0
count_bits_loop:
// stack: k, bits
%div2
// stack: k//2, bits
swap1
// stack: bits, k//2
%increment
// stack: bits+1, k//2
swap1
// stack: k//2, bits+1
%jumpi(count_bits_loop)
// stack: 0, bits
pop
// stack: bits
2022-07-14 14:58:28 -07:00
global sha2_store:
2022-07-14 09:29:29 -07:00
// stack: num_u256s, x[0], x[1], x[2], ... , x[num_u256s-1]
2022-07-14 14:58:28 -07:00
dup1
2022-07-14 09:29:29 -07:00
// stack: num_u256s, num_u256s, x[0], x[1], x[2], ... , x[num_u256s-1]
2022-07-14 14:58:28 -07:00
// TODO: use kernel memory, and start address not at 0
push 0
// stack: addr=0, num_u256s, num_u256s, x[0], x[1], x[2], ... , x[num_u256s-1]
mstore
2022-07-14 09:29:29 -07:00
// stack: num_u256s, x[0], x[1], x[2], ... , x[num_u256s-1]
2022-07-14 14:58:28 -07:00
push 1
// stack: addr=1, counter=num_u256s, x[0], x[1], x[2], ... , x[num_u256s-1]
2022-07-14 09:29:29 -07:00
2022-07-14 14:58:28 -07:00
sha2_store_loop:
2022-07-14 09:29:29 -07:00
JUMPDEST
2022-07-14 14:58:28 -07:00
// stack: addr, counter, x[num_u256s-counter], ... , x[num_u256s-1]
dup1
// stack: addr, addr, counter, x[num_u256s-counter], ... , x[num_u256s-1]
swap3
// stack: x[num_u256s-counter], addr, counter, addr, ... , x[num_u256s-1]
swap1
// stack: addr, x[num_u256s-counter], counter, addr, ... , x[num_u256s-1]
mstore
// stack: counter, addr, ... , x[num_u256s-1]
%decrement
// stack: counter-1, addr, ... , x[num_u256s-1]
iszero
%jumpi(sha2_store_end)
swap1
// stack: addr, counter-1, ... , x[num_u256s-1]
%increment
// stack: addr+1, counter-1, ... , x[num_u256s-1]
%jump(sha2_store_loop)
sha2_store_end:
2022-07-14 09:29:29 -07:00
JUMPDEST
2022-07-14 16:26:56 -07:00
global sha2_append1:
2022-07-14 14:58:28 -07:00
// TODO: use kernel memory, and start address not at 0
push 0
mload
// stack: num_u256s
mload
// stack: x[num_u256s-1]
dup1
// stack: x[num_u256s-1], x[num_u256s-1]
%count_bits
// stack: num_bits, x[num_u256s-1]
%eq(256)
2022-07-14 16:28:14 -07:00
%jumpi(append_if256)
%jump(append_else)
2022-07-14 16:26:56 -07:00
append_if256:
2022-07-14 09:29:29 -07:00
JUMPDEST
2022-07-14 14:58:28 -07:00
// stack: num_bits, x[num_u256s-1]
2022-07-14 09:29:29 -07:00
%pop2
2022-07-14 14:58:28 -07:00
push 0
mload
// stack: num_u256s
%increment
// stack: num_u256s+1
dup1
// stack: num_u256s+1, num_u256s+1
push 0
mstore
// stack: num_u256s+1
push 1
// stack: 1, num_u256s+1
swap1
// stack: num_u256s+1, 1
mstore
2022-07-14 16:28:14 -07:00
%jump(append_end)
2022-07-14 16:26:56 -07:00
append_else:
2022-07-14 09:29:29 -07:00
JUMPDEST
2022-07-14 14:58:28 -07:00
// stack: num_bits, x[num_u256s-1]
pop
// stack: x[num_u256s-1]
push 2
mul
// stack: 2*x[num_u256s-1]
%increment
// stack: 2*x[num_u256s-1]+1
push 0
mload
// stack: num_u256s, 2*x[num_u256s-1]+1
mstore
2022-07-14 16:26:56 -07:00
append_end:
2022-07-14 09:29:29 -07:00
JUMPDEST
2022-07-14 16:26:56 -07:00