fix update

This commit is contained in:
Dmitry Vagner 2022-09-23 08:05:14 -07:00
parent bbeac10bff
commit c6b62eaf2f
6 changed files with 33 additions and 23 deletions

View File

@ -207,7 +207,8 @@ pre_rol:
%mload_kernel_code_label(R_data)
ADD
// stack: virt + r, box, a, b, c, d, e, F, K, boxes, rounds, sides, virt
%mload_kernel_code_u32_LE(Input_Block) // %load_u32_from_block
// %mload_kernel_code_u32_LE(Input_Block)
%load_u32_from_block
// stack: x, box, a, b, c, d, e, F, K, boxes, rounds, sides, virt
SWAP1
SWAP2

View File

@ -2,7 +2,7 @@ global ripemd_storage: // starts by initializing buffer
// stack: i [init: 64]
%store_zeros(64, ripemd_storage)
// stack:
// %jump(store_size)
%jump(store_size)
store_size:
// stack: length
@ -42,7 +42,7 @@ store_input_alt:
%mstore_ripemd
// stack: rem, length, REM_INP
%sub_const(1)
DUP1
DUP1
// stack: rem - 1, rem - 1, length, REM_INP
%jumpi(store_input_alt)
// stack: 0, length

View File

@ -40,6 +40,7 @@ global ripemd_init:
// stack: 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, count, length, virt, LABELS
%jump(ripemd_update)
ripemd_1:
%break
// stack: STATE, count, length , virt , LABELS
DUP7
// stack: length, STATE, count, length , virt , LABELS

View File

@ -11,7 +11,7 @@
/// update_1()
/// if Q:
/// update_2()
/// R = length - shift > 0
/// R = length > shift
/// if R:
/// buffer_update(virt + shift, have, length - shift)
///
@ -32,24 +32,24 @@ global ripemd_update:
%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
ISZERO
// stack: Q, STATE, 0, shift, need, have, count, length, virt, retdest
%stack (Q, STATE: 5, i, shift, need, have) -> (have, Q, Q, STATE, i, shift, need, have)
%stack (Q, STATE: 5, i, shift, need, have) -> (have, Q, Q, STATE, i, shift, need, have)
%eq_const(0)
AND
// stack: P, Q, STATE, 0, shift, need, have, count, length, virt, retdest
%jumpi(update_1)
// stack: Q, STATE, 0, shift, need, have, count, length, virt, retdest
%jumpi(update_2)
final_update:
// stack: shift, need, have, STATE, count, length, virt, retdest
// stack: shift, need, have, STATE, count, length, virt, retdest
%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 (ARGS: 2, shift, need, have, STATE: 5, count, length, virt) -> (shift, virt, have, ARGS, shift, need, have, STATE, count, length, virt)
ADD
// stack: ARGS: 4, shift, need, have, STATE, count, length, virt, retdest
PUSH 0
DUP4
%stack (ARGS: 4, shift, need, have, STATE, count, length) -> (length, shift, ARGS, shift, need, have, STATE, count, length)
GT
// stack: R, ARGS, shift, need, have, STATE, count, length, virt, retdest
%jumpi(buffer_update)
@ -57,7 +57,7 @@ final_update:
%pop3
JUMP
return_step:
// stack: shift, need, have, STATE, count, length, virt, retdest
// stack: shift, need, have, STATE, count , length, virt, retdest
SWAP8
DUP10
%mul_const(8)
@ -103,7 +103,7 @@ update_2:
// stack: offset, cond, STATE, shift, need, have, count, length, virt, retdest
%stack (offset, cond, STATE: 5) -> (cond, 0, STATE, offset, compression_loop, cond)
LT
NOT
ISZERO
// cond >= 0, STATE, offset, compression_loop, cond, shift, need, have, count, length, virt, retdest
%jumpi(compress)
compression_loop:
@ -129,7 +129,7 @@ compression_loop:
/// for i in range(times):
/// buffer[set+i] = bytestring[get+i]
buffer_update:
buffer_update:
// stack: get , set , times , retdest
DUP2
DUP2

View File

@ -298,3 +298,7 @@
ADD
// stack: dcba
%endmacro
%macro break
%jump(0xdeadbeef)
%endmacro

View File

@ -4,26 +4,30 @@ use ethereum_types::U256;
use crate::cpu::kernel::aggregator::combined_kernel;
use crate::cpu::kernel::interpreter::run_with_kernel;
#[test]
fn test_ripemd() -> Result<()> {
// let expected = "0xf71c27109c692c1b56bbdceb5b9d2865b3708dbc";
let expected: Vec<&str> = vec!["10271CF7", "1B2C699C", "EBDCBB56", "65289D5B", "BC8D70B3"];
let expected = "0xf71c27109c692c1b56bbdceb5b9d2865b3708dbc";
// let expected: Vec<&str> = vec!["10271CF7", "1B2C699C", "EBDCBB56", "65289D5B", "BC8D70B3"];
println!("{:#?}", expected);
let input: Vec<u32> = vec![0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0, 0, 0xdeadbeef];
// let input: Vec<u32> = vec![0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0, 0, 0xdeadbeef];
// let input: Vec<u32> = vec![
// 0x1a, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,
// 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a,
// ];
let input: Vec<u32> = vec![
26, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,
0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a
];
let kernel = combined_kernel();
let stack_input: Vec<U256> = input.iter().map(|&x| U256::from(x as u32)).rev().collect();
let stack_output = run_with_kernel(&kernel, kernel.global_labels["compress"], stack_input)?;
let actual: Vec<String> = stack_output.stack().iter().map(|&x| format!("{:X}", x)).rev().collect();
let stack_output = run_with_kernel(&kernel, kernel.global_labels["ripemd_alt"], stack_input)?;
let actual: Vec<String> = stack_output
.stack()
.iter()
.map(|&x| format!("{:X}", x))
.rev()
.collect();
println!("{:#?}", actual);
assert_eq!(expected, actual);
assert_eq!(expected, actual[0]);
Ok(())
}