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

151 lines
3.1 KiB
NASM
Raw Normal View History

2022-08-25 15:38:18 -07:00
/// def rol(n, x):
2022-08-09 15:11:18 -07:00
/// return (u32(x << n)) | (x >> (32 - n))
2022-08-25 15:38:18 -07:00
global rol:
// stack: n, x, retdest
2022-09-09 17:42:49 -07:00
SWAP1
DUP1
DUP3
2022-08-25 15:38:18 -07:00
// stack: n, x, x, n, retdest
2022-09-09 17:42:49 -07:00
PUSH 32
SUB
2022-08-25 15:38:18 -07:00
// stack: 32-n, x, x, n, retdest
2022-09-09 17:42:49 -07:00
SHR
2022-08-25 15:38:18 -07:00
// stack: x >> (32-n), x, n, retdest
2022-09-09 17:42:49 -07:00
SWAP2
2022-08-25 15:38:18 -07:00
// stack: n, x, x >> (32-n), retdest
2022-09-09 17:42:49 -07:00
SHL
2022-08-25 15:38:18 -07:00
// stack: x << n, x >> (32-n), retdest
2022-09-29 13:58:49 -07:00
%as_u32
2022-08-25 15:38:18 -07:00
// stack: u32(x << n), x >> (32-n), retdest
2022-09-09 17:42:49 -07:00
OR
2022-08-25 15:38:18 -07:00
// stack: u32(x << n) | (x >> (32-n)), retdest
2022-09-09 17:42:49 -07:00
SWAP1
JUMP
2022-08-25 15:38:18 -07:00
2022-09-21 20:06:41 -07:00
// def push_f(rnd):
2022-09-21 20:03:11 -07:00
// Fs = [F0, F1, F2, F3, F4, F4, F3, F2, F1, F0]
// acc = 0
// for i, F in enumerate(Fs):
// acc += (i==rnd)*F
// return acc, rnd
//
2022-10-03 14:01:47 -07:00
// %this_f(i,F) enacts
2022-09-21 20:03:11 -07:00
// acc += (i==rnd)*F
2022-08-25 15:38:18 -07:00
2022-09-21 20:06:41 -07:00
%macro push_f
2022-09-21 20:03:11 -07:00
// stack: rnd
PUSH 0
2022-10-03 14:01:47 -07:00
%this_f(0,F0)
%this_f(1,F1)
%this_f(2,F2)
%this_f(3,F3)
%this_f(4,F4)
%this_f(5,F4)
%this_f(6,F3)
%this_f(7,F2)
%this_f(8,F1)
%this_f(9,F0)
2022-09-21 20:03:11 -07:00
// stack: F, rnd
2022-08-25 15:38:18 -07:00
%endmacro
2022-10-03 14:01:47 -07:00
%macro this_f(i, F)
2022-09-16 19:18:26 -07:00
// stack: acc, rnd
2022-09-09 17:42:49 -07:00
DUP2
2022-09-16 19:18:26 -07:00
// stack: rnd , acc, rnd
%eq_const($i)
2022-09-21 20:03:11 -07:00
// stack: rnd==i , acc, rnd
2022-09-16 19:18:26 -07:00
%mul_const($F)
// stack: (rnd==i)*F , acc, rnd
2022-09-09 17:42:49 -07:00
ADD
2022-09-16 19:18:26 -07:00
// stack: (rnd==j)*F + acc, rnd
2022-08-25 15:38:18 -07:00
%endmacro
2022-07-27 14:34:00 -04:00
2022-08-09 15:11:18 -07:00
/// def F0(x, y, z):
/// return x ^ y ^ z
2022-09-16 10:31:30 -07:00
global F0:
2022-08-25 15:38:18 -07:00
// stack: x , y , z, retdest
2022-09-09 17:42:49 -07:00
XOR
2022-08-25 15:38:18 -07:00
// stack: x ^ y , z, retdest
2022-09-09 17:42:49 -07:00
XOR
2022-07-29 13:24:11 -04:00
// stack: x ^ y ^ z, retdest
2022-09-09 17:42:49 -07:00
SWAP1
JUMP
2022-07-27 14:34:00 -04:00
2022-08-09 15:11:18 -07:00
/// def F1(x, y, z):
/// return (x & y) | (u32(~x) & z)
2022-09-16 10:31:30 -07:00
global F1:
2022-08-25 15:38:18 -07:00
// stack: x, y, z, retdest
2022-09-09 17:42:49 -07:00
DUP1
2022-09-16 19:18:26 -07:00
// stack: x, x, y, z, retdest
2022-09-09 17:42:49 -07:00
SWAP2
2022-09-16 19:18:26 -07:00
// stack: y, x, x, z, retdest
2022-09-09 17:42:49 -07:00
AND
2022-08-25 15:38:18 -07:00
// stack: y & x, x, z, retdest
2022-09-09 17:42:49 -07:00
SWAP2
2022-09-16 19:18:26 -07:00
// stack: z, x, y & x , retdest
2022-09-09 17:42:49 -07:00
SWAP1
2022-09-16 19:18:26 -07:00
// stack: x, z, y & x , retdest
2022-09-21 20:03:11 -07:00
%not_u32
2022-09-16 19:18:26 -07:00
// stack: ~x, z, y & x , retdest
2022-09-09 17:42:49 -07:00
AND
2022-09-16 19:18:26 -07:00
// stack: ~x & z , y & x , retdest
2022-09-09 17:42:49 -07:00
OR
2022-07-29 13:24:11 -04:00
// stack: (~x & z) | (y & x), retdest
2022-09-09 17:42:49 -07:00
SWAP1
JUMP
2022-07-27 14:34:00 -04:00
2022-08-09 15:11:18 -07:00
/// def F2(x, y, z):
/// return (x | u32(~y)) ^ z
2022-07-27 14:34:00 -04:00
global F2:
2022-09-16 19:18:26 -07:00
// stack: x , y, z, retdest
2022-09-09 17:42:49 -07:00
SWAP1
2022-09-16 19:18:26 -07:00
// stack: y , x, z, retdest
2022-09-21 20:03:11 -07:00
%not_u32
2022-09-16 19:18:26 -07:00
// stack: ~y , x , z, retdest
2022-09-09 17:42:49 -07:00
OR
2022-09-16 19:18:26 -07:00
// stack: ~y | x , z, retdest
2022-09-09 17:42:49 -07:00
XOR
2022-08-09 15:11:18 -07:00
// stack: (~y | x) ^ z, retdest
2022-09-09 17:42:49 -07:00
SWAP1
JUMP
2022-07-27 14:34:00 -04:00
2022-08-09 15:11:18 -07:00
/// def F3(x, y, z):
/// return (x & z) | (u32(~z) & y)
2022-09-16 10:31:30 -07:00
global F3:
2022-09-16 19:18:26 -07:00
// stack: x, y , z , retdest
2022-09-09 17:42:49 -07:00
DUP3
2022-09-16 19:18:26 -07:00
// stack: z , x, y , z , retdest
2022-09-09 17:42:49 -07:00
AND
2022-09-16 19:18:26 -07:00
// stack: z & x, y , z , retdest
2022-09-09 17:42:49 -07:00
SWAP2
2022-09-16 19:18:26 -07:00
// stack: z, y, z & x , retdest
2022-09-21 20:03:11 -07:00
%not_u32
2022-09-16 19:18:26 -07:00
// stack: ~z , y, z & x , retdest
2022-09-09 17:42:49 -07:00
AND
2022-09-16 19:18:26 -07:00
// stack: ~z & y, z & x , retdest
2022-09-09 17:42:49 -07:00
OR
2022-07-29 13:24:11 -04:00
// stack: (~z & y) | (z & x), retdest
2022-09-09 17:42:49 -07:00
SWAP1
JUMP
2022-07-27 14:34:00 -04:00
2022-08-09 15:11:18 -07:00
/// def F4(x, y, z):
/// return x ^ (y | u32(~z))
2022-07-27 14:34:00 -04:00
global F4:
2022-09-16 10:31:30 -07:00
// stack: x, y, z, retdest
2022-09-09 17:42:49 -07:00
SWAP2
2022-09-16 10:31:30 -07:00
// stack: z, y, x, retdest
2022-09-21 20:03:11 -07:00
%not_u32
2022-09-16 10:31:30 -07:00
// stack: ~z, y, x, retdest
2022-09-09 17:42:49 -07:00
OR
2022-09-16 10:31:30 -07:00
// stack: ~z | y, x, retdest
2022-09-09 17:42:49 -07:00
XOR
2022-07-29 13:24:11 -04:00
// stack: (~z | y) ^ x, retdest
2022-09-21 13:42:13 -07:00
SWAP1
2022-09-09 17:42:49 -07:00
JUMP