use blocks

This commit is contained in:
Dmitry Vagner 2022-09-21 13:42:13 -07:00
parent 64041e1c59
commit 6cac2d79f8
6 changed files with 67 additions and 53 deletions

View File

@ -115,7 +115,7 @@ loop:
// stack: round, STATE, F, K, 16, rounds, sides, virt, retdest
%jumpi(update_round_vars)
// stack: STATE, F, K, 16, 0, sides, virt, retdest
%stack (a, b, c, d, e, F, K, boxes, rounds, sides, virt, retdest) -> (retdest, a, b, c, d, e)
%stack (STATE: 5, F, K, boxes, rounds, sides, virt, retdest) -> (retdest, STATE)
// stack: retdest, STATE
JUMP
update_round_vars:

View File

@ -1,8 +1,8 @@
global ripemd_storage: // starts by initializing buffer
// stack: i (init 64)
// stack: i [init: 64]
%store_zeros(64, ripemd_storage)
// stack:
%jump(store_size)
// %jump(store_size)
store_size:
// stack: length
@ -82,9 +82,7 @@ store_input:
%macro store_zeros(N, label)
// stack: i
PUSH 0
DUP2
PUSH $N
%stack (i) -> ($N, i, 0, i)
SUB
// stack: offset = N-i, 0, i
%mstore_ripemd

View File

@ -16,6 +16,7 @@ global ripemd_alt:
// stack: length, INPUT
%stack (length) -> (64, length, 0x80, 63, length, length)
// stack: 64, length, 0x80, 63, length, length, INPUT
%jump(ripemd_storage) // stores the following into memory
// init _buffer at virt 0 [consumes 64]
// store _size at virt 64 [consumes length]
@ -24,35 +25,37 @@ global ripemd_alt:
global ripemd:
// stack: ADDR, length
%stack (a, b, c, length) -> (64, length, 0x80, 63, a, b, c, length, length)
// stack: 64, length, 0x80, 63, a, b, c, length, length
%stack (ADDR: 3, length) -> (64, length, 0x80, 63, ADDR, length, length)
// stack: 64, length, 0x80, 63, ADDR, length, length
%jump(ripemd_storage) // stores the following into memory
// init _buffer at virt 0 [consumes 64]
// store _size at virt 64 [consumes length]
// store _padding at virt 72 [consumes 0x80, 63]
// store _input at virt 136 [consumes ADDR, length]
global ripemd_init:
// stack: length
%stack (length) -> ( 0, length, 136, ripemd_1, ripemd_2, process)
// stack: count = 0, length, virt = 136, ripemd_1, ripemd_2, process
%stack (c, l, o, l1, l2, l3) -> (0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, c, l, o, l1, l2, l3)
// stack: 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, count, length, virt, *labels
%stack (ARGS: 3, LABELS: 3) -> (0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, ARGS, LABELS)
// stack: 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, count, length, virt, LABELS
%jump(ripemd_update)
ripemd_1:
// stack: STATE, count, length , virt, *labels
// stack: STATE, count, length , virt , LABELS
DUP7
// stack: length, STATE, count, length , virt, *labels
// stack: length, STATE, count, length , virt , LABELS
%padlength
// stack: padlength, STATE, count, length , virt, *labels
// stack: padlength, STATE, count, length , virt , LABELS
SWAP7
POP
// stack: STATE, count, length = padlength, virt, *labels
%stack (a, b, c, d, e, count, length, virt) -> (a, b, c, d, e, count, length, 72)
// stack: STATE, count, length = padlength, virt , LABELS
%stack (STATE: 5, count, length, virt) -> (STATE, count, length, 72)
// STATE, count, length , virt = 72, LABELS
%jump(ripemd_update)
ripemd_2:
// stack: STATE, count, length, virt, *labels
%stack (a, b, c, d, e, count, length, virt) -> (a, b, c, d, e, count, 8, 64)
// stack: STATE, count, length, virt, *labels
// stack: STATE, count, length , virt , LABELS
%stack (STATE: 5, count, length, virt) -> (STATE, count, 8, 64)
// stack: STATE, count, length = 8, virt = 64, LABELS
%jump(ripemd_update)
process:
// stack: a , b, c, d, e, count, length, virt

View File

@ -20,7 +20,7 @@
global ripemd_update:
// stack: STATE, count, length, virt, retdest
%stack (a, b, c, d, e, count, length, virt) -> (count, 8, 64, a, b, c, d, e, count, length, virt)
%stack (STATE: 5, count, length, virt) -> (count, 8, 64, STATE, count, length, virt)
DIV
MOD
// stack: have, STATE, count, length, virt, retdest
@ -29,12 +29,12 @@ global ripemd_update:
SUB
PUSH 0
// stack: shift, need, have, STATE, count, length, virt, retdest
%stack (shift, need, have, a, b, c, d, e, count, length) -> (length, need, a, b, c, d, e, 0, shift, need, have, count, length)
%stack (shift, need, have, STATE: 5, count, length) -> (length, need, STATE, 0, shift, need, have, count, length)
// stack: length, need, STATE, 0, shift, need, have, count, length, virt, retdest
LT
NOT
// stack: Q, STATE, 0, shift, need, have, count, length, virt, retdest
%stack (Q, a, b, c, d, e, i, shift, need, have) -> (have, Q, Q, a, b, c, d, e, i, shift, need, have)
%stack (Q, STATE: 5, i, shift, need, have) -> (have, Q, Q, STATE, i, shift, need, have)
AND
// stack: P, Q, STATE, 0, shift, need, have, count, length, virt, retdest
%jumpi(update_1)
@ -42,12 +42,12 @@ global ripemd_update:
%jumpi(update_2)
final_update:
// stack: shift, need, have, STATE, count, length, virt, retdest
%stack (shift, need, have, a, b, c, d, e, count, length) -> (length, shift, return_step, shift, need, have, a, b, c, d, e, count, length)
%stack (shift, need, have, STATE: 5, count, length) -> (length, shift, return_step, shift, need, have, STATE, count, length)
SUB
// stack: ARGS, shift, need, have, STATE, count, length, virt, retdest
%stack (a, r, shift, need, have, a, b, c, d, e, count, length, virt) -> (shift, virt, have, a, r, shift, need, have, a, b, c, d, e, count, length, virt)
%stack (ARGS: 2, shift, need, have, STATE: 5, count, length, virt) -> (shift, virt, have, ARGS: 2, shift, need, have, STATE, count, length, virt)
ADD
// stack: ARGS, shift, need, have, STATE, count, length, virt, retdest
// stack: ARGS: 4, shift, need, have, STATE, count, length, virt, retdest
PUSH 0
DUP4
GT
@ -64,7 +64,7 @@ return_step:
ADD
SWAP8
// stack: shift, need, have, STATE, count += 8*length, length, virt, retdest
%stack (shift, need, have, a, b, c, d, e, count, length, virt, retdest) -> (retdest, a, b, c, d, e, count, length, virt)
%stack (shift, need, have, STATE: 5, count, length, virt, retdest) -> (retdest, STATE, count, length, virt)
JUMP
@ -76,12 +76,12 @@ return_step:
update_1:
// stack: Q, STATE, 0, shift, need, have, count, length, virt, retdest
%stack (Q, a, b, c, d, e, i, shift, need, have, count, length, virt) -> (virt, have, need, update_1a, a, b, c, d, e, i, shift, need, have, count, length, virt)
%stack (Q, STATE: 5, i, shift, need, have, count, length, virt) -> (virt, have, need, update_1a, STATE, i, shift, need, have, count, length, virt)
%jump(buffer_update)
update_1a:
// stack: STATE, 0, shift, need, have, count, length, virt, retdest
%stack (a, b, c, d, e, i, shift, need, have) -> (a, b, c, d, e, i, update_2, need, need, 0)
// stack: STATE, 0, update_2, shift, need, have, count, length, virt, retdest
%stack (STATE: 5, i, shift, need, have) -> (STATE, i, update_2, need, need, 0)
// stack: STATE, 0, update_2, shift = need, need, have = 0, count, length, virt, retdest
%jump(compress)
/// def update_2():
@ -93,7 +93,7 @@ update_1a:
update_2:
// stack: STATE, shift, need, have, count, length, virt, retdest
%stack (a, b, c, d, e, shift, need, have, count, length) -> (length, shift, a, b, c, d, e, shift, need, have, count, length)
%stack (STATE: 5, shift, need, have, count, length) -> (length, shift, STATE, shift, need, have, count, length)
SUB
SUB
// stack: cond, STATE, shift, need, have, count, length, virt, retdest
@ -101,7 +101,7 @@ update_2:
DUP8
ADD
// stack: offset, cond, STATE, shift, need, have, count, length, virt, retdest
%stack (offset, cond, a, b, c, d, e) -> (cond, 0, a, b, c, d, e, offset, compression_loop, cond)
%stack (offset, cond, STATE: 5) -> (cond, 0, STATE, offset, compression_loop, cond)
LT
NOT
// cond >= 0, STATE, offset, compression_loop, cond, shift, need, have, count, length, virt, retdest
@ -118,10 +118,10 @@ compression_loop:
%add_const(64)
SWAP7
// stack: STATE, offset+64, cond-64, shift+64, need, have, count, length, virt, retdest
%stack (a, b, c, d, e, offset, cond, shift) -> (cond, 0, a, b, c, d, e, offset, compression_loop, cond, shift)
%stack (STATE: 5, offset, cond, shift) -> (cond, 0, STATE, offset, compression_loop, cond, shift)
%jumpi(compress)
// stack: STATE, offset , label, cond , shift , need, have, count, length, virt, retdest
%stack (a, b, c, d, e, offset, label, cond, shift, need, have, count, length, virt, retdest) -> (shift, need, have, a, b, c, d, e, count, length, virt, retdest)
%stack (STATE: 5, offset, label, cond, shift, need, have, count, length, virt, retdest) -> (shift, need, have, STATE, count, length, virt, retdest)
%jump(final_update)

View File

@ -1,4 +1,4 @@
use std::env;
// use std::env;
use anyhow::Result;
use ethereum_types::U256;
@ -8,13 +8,12 @@ use crate::cpu::kernel::interpreter::run;
#[test]
fn test_ripemd() -> Result<()> {
env::set_var("RUST_BACKTRACE", "1");
// env::set_var("RUST_BACKTRACE", "1");
let expected = "0xf71c27109c692c1b56bbdceb5b9d2865b3708dbc";
println!("{}", expected);
// let expected = "0xf71c27109c692c1b56bbdceb5b9d2865b3708dbc";
// println!("{}", expected);
let kernel = combined_kernel();
let ripemd = kernel.global_labels["ripemd_alt"];
let input: Vec<u32> = vec![
26 , 0x61, 0x62,
@ -23,15 +22,29 @@ fn test_ripemd() -> Result<()> {
0x6b, 0x6c, 0x6d, 0x6e,
0x6f, 0x70, 0x71, 0x72,
0x73, 0x74, 0x75, 0x76,
0x77, 0x78, 0x79, 0x7a
0x77, 0x78, 0x79, 0x7a,
];
let initial_stack = input.iter().map(|&x| U256::from(x as u32)).collect();
let hashed = run(&kernel.code, ripemd, initial_stack, &kernel.prover_inputs)?;
let result = hashed.stack()[1];
let actual = format!("{:X}", result);
println!("{}", actual);
assert_eq!(expected, actual);
// let input: Vec<u32> = vec![0xabcd, 0x1234, 0x6789];
let stack_init = input.iter().map(|&x| U256::from(x as u32)).collect();
let ripemd = kernel.global_labels["F0"];
let hashed = run(
&kernel.code,
ripemd,
stack_init,
&kernel.prover_inputs)?;
let result = hashed.stack();
for term in input {
println!("{:X}", term);
}
for term in result {
println!("{:X}", term);
}
// assert_eq!(expected, actual);
Ok(())
}