almost done

This commit is contained in:
Dmitry Vagner 2022-10-03 15:30:17 -07:00
parent 1475cddb3d
commit 53014b732f
4 changed files with 29 additions and 12 deletions

View File

@ -22,6 +22,7 @@ pest = "2.1.3"
pest_derive = "2.1.0"
rand = "0.8.5"
rand_chacha = "0.3.1"
ripemd = "0.1.3"
rlp = "0.5.1"
serde = { version = "1.0.144", features = ["derive"] }
sha2 = "0.10.2"

View File

@ -82,7 +82,7 @@ global process:
%reverse_bytes_u32
OR
// stack: e' d' c' b' a', VARS
%stack (result, VARS: 3) -> (0xdeadbeef, result)
%stack (result, VARS: 3, retdest) -> (retdest, result)
// stack: 0xdeadbeef, result
JUMP

View File

@ -3,7 +3,8 @@ use std::str::FromStr;
use anyhow::Result;
use ethereum_types::U256;
use rand::{thread_rng, Rng};
use sha2::{Digest, Sha256};
use ripemd::{Digest, Ripemd160};
use sha2::Sha256;
use crate::cpu::kernel::aggregator::combined_kernel;
use crate::cpu::kernel::interpreter::run_with_kernel;
@ -15,6 +16,13 @@ fn sha2(input: Vec<u8>) -> U256 {
U256::from(&hasher.finalize()[..])
}
/// Standard RipeMD implementation.
fn ripemd(input: Vec<u8>) -> U256 {
let mut hasher = Ripemd160::new();
hasher.update(input);
U256::from(&hasher.finalize()[..])
}
fn test_hash(hash_fn_label: &str, standard_implementation: &dyn Fn(Vec<u8>) -> U256) -> Result<()> {
let kernel = combined_kernel();
let mut rng = thread_rng();
@ -48,3 +56,8 @@ fn test_hash(hash_fn_label: &str, standard_implementation: &dyn Fn(Vec<u8>) -> U
fn test_sha2() -> Result<()> {
test_hash("sha2", &sha2)
}
#[test]
fn test_ripemd() -> Result<()> {
test_hash("ripemd_stack", &ripemd)
}

View File

@ -1,21 +1,24 @@
use anyhow::Result;
use ethereum_types::U256;
use itertools::Itertools;
use crate::cpu::kernel::aggregator::combined_kernel;
use crate::cpu::kernel::interpreter::run_with_kernel;
fn make_input(word: &str) -> Vec<u8> {
let mut bytes: Vec<u8> = vec![word.len().try_into().unwrap()];
bytes.append(&mut word.as_bytes().to_vec());
bytes
fn make_input(word: &str) -> Vec<u32> {
let mut input: Vec<u32> = vec![word.len().try_into().unwrap()];
input.append(&mut word.as_bytes().iter().map(|&x| x as u32).collect_vec());
input.push(u32::from_str_radix("deadbeef", 16).unwrap());
dbg!(input.clone());
input
}
#[test]
fn test_ripemd() -> Result<()> {
// #[test]
fn test_ripemd_reference() -> Result<()> {
let reference = vec![
("", "0x9c1185a5c5e9fc54612808977ee8f548b2258d31"),
("a", "0x0bdc9d2d256b3ee9daae347be6f4dc835a467ffe"),
("abc", "0x8eb208f7e05d987a9b044a8e98c6b087f15a0bfc"),
// ("", "0x9c1185a5c5e9fc54612808977ee8f548b2258d31"),
// ("a", "0x0bdc9d2d256b3ee9daae347be6f4dc835a467ffe"),
// ("abc", "0x8eb208f7e05d987a9b044a8e98c6b087f15a0bfc"),
(
"message digest",
"0x5d0689ef49d2fae572b881b123a85ffa21595f36",
@ -39,7 +42,7 @@ fn test_ripemd() -> Result<()> {
];
for (x, y) in reference {
let input: Vec<u8> = make_input(x);
let input: Vec<u32> = make_input(x);
let expected = U256::from(y);
let kernel = combined_kernel();