Passing tests

This commit is contained in:
wborgeaud 2022-07-14 14:26:01 +02:00
parent add2b42e16
commit 33a5934255
7 changed files with 65 additions and 39 deletions

View File

@ -20,9 +20,11 @@ rand = "0.8.5"
rand_chacha = "0.3.1"
rlp = "0.5.1"
keccak-rust = { git = "https://github.com/npwardberkeley/keccak-rust" }
keccak-hash = "0.9.0"
[dev-dependencies]
hex-literal = "0.3.4"
hex = "0.4.3"
[features]
asmtools = ["hex"]

View File

@ -38,12 +38,7 @@ pub(crate) fn combined_kernel() -> Kernel {
#[cfg(test)]
mod tests {
use std::str::FromStr;
use anyhow::Result;
use ethereum_types::U256;
use log::debug;
use rand::{thread_rng, Rng};
use crate::cpu::kernel::aggregator::combined_kernel;

View File

@ -14,7 +14,7 @@ global ec_mul:
// stack: x, y, x, y, s, retdest
%ec_isidentity
// stack: (x,y)==(0,0), x, y, s, retdest
%jumpi(ret_zero)
%jumpi(ret_zero_ec_mul)
// stack: x, y, s, retdest
DUP2
// stack: y, x, y, s, retdest
@ -35,7 +35,7 @@ ec_mul_valid_point:
// stack: s, x, y, s, retdest
%jumpi(step_case)
// stack: x, y, s, retdest
%jump(ret_zero)
%jump(ret_zero_ec_mul)
step_case:
JUMPDEST
@ -100,7 +100,7 @@ odd_scalar:
// stack: x', y', x, y, retdest
%jump(ec_add_valid_points)
ret_zero:
global ret_zero_ec_mul:
JUMPDEST
// stack: x, y, s, retdest
%pop3

View File

@ -69,11 +69,11 @@ ecrecover_with_first_point:
// stack: -hash, r^(-1), Y, X, retdest
%mulmodn_secp_scalar
// stack: u2, Y, X, retdest
PUSH 8
PUSH final_hashing
// stack: final_hashing, u2, Y, X, retdest
SWAP3
// stack: X, u2, Y, final_hashing, retdest
PUSH 7
PUSH ec_add_valid_points_secp
// stack: ec_add_valid_points_secp, X, u2, Y, final_hashing, retdest
SWAP1
// stack: X, ec_add_valid_points_secp, u2, Y, final_hashing, retdest
@ -115,8 +115,6 @@ final_hashing:
// stack: v==28, v==27, hash, v, r, s, retdest
OR
// stack: (v==28 || v==27), hash, v, r, s, retdest
ISZERO
// stack: (v==28 || v==27), hash, v, r, s, retdest
DUP5
// stack: s, (v==28 || v==27), hash, v, r, s, retdest
%secp_is_out_of_bounds

View File

@ -9,7 +9,7 @@ global ec_mul_secp:
// stack: x, y, x, y, s, retdest
%ec_isidentity
// stack: (x,y)==(0,0), x, y, s, retdest
%jumpi(ret_zero)
%jumpi(ret_zero_ec_mul)
// stack: x, y, s, retdest
DUP2
// stack: y, x, y, s, retdest
@ -30,7 +30,7 @@ global ec_mul_valid_point_secp:
// stack: s, x, y, s, retdest
%jumpi(step_case)
// stack: x, y, s, retdest
%jump(ret_zero)
%jump(ret_zero_ec_mul)
step_case:
JUMPDEST
@ -94,16 +94,3 @@ odd_scalar:
JUMPDEST
// stack: x', y', x, y, retdest
%jump(ec_add_valid_points_secp)
ret_zero:
JUMPDEST
// stack: x, y, s, retdest
%pop3
// stack: retdest
PUSH 0
// stack: 0, retdest
PUSH 0
// stack: 0, 0, retdest
SWAP2
// stack: retdest, 0, 0
JUMP

View File

@ -14,7 +14,7 @@
// stack: x
DUP1
// stack: x, x
%mulmodn_secp
%mulmodn_secp_scalar
%endmacro
// Computes the inverse modulo N using x^-1 = x^(N-2) mod N and square-and-multiply modular exponentiation.

View File

@ -1,22 +1,66 @@
use anyhow::Result;
use anyhow::{ensure, Result};
use ethereum_types::U256;
use hex_literal::hex;
use keccak_hash::keccak;
use crate::cpu::kernel::aggregator::combined_kernel;
use crate::cpu::kernel::assembler::Kernel;
use crate::cpu::kernel::interpreter::run;
use crate::cpu::kernel::tests::u256ify;
#[test]
fn test_ec_ops() -> Result<()> {
// Make sure we can parse and assemble the entire kernel.
let kernel = combined_kernel();
let ecrecover = kernel.global_labels["ecrecover"];
let hash = "0x0";
let v = "0x27";
let r = "0x1";
let s = "0x1";
fn pubkey_to_addr(x: U256, y: U256) -> Vec<u8> {
let mut buf = [0; 64];
x.to_big_endian(&mut buf[0..32]);
y.to_big_endian(&mut buf[32..64]);
let hash = keccak(buf);
hash.0[12..].to_vec()
}
fn test_valid_ecrecover(
hash: &str,
v: &str,
r: &str,
s: &str,
expected: &str,
kernel: &Kernel,
) -> Result<()> {
let ecrecover = kernel.global_labels["ecrecover"];
let initial_stack = u256ify([s, r, v, hash])?;
let stack = run(&kernel.code, ecrecover, initial_stack);
dbg!(stack);
let got = pubkey_to_addr(stack[1], stack[0]);
assert_eq!(got, hex::decode(expected).unwrap());
Ok(())
}
#[test]
fn test_ecrecover() -> Result<()> {
let kernel = combined_kernel();
test_valid_ecrecover(
"0x55f77e8909b1f1c9531c4a309bb2d40388e9ed4b87830c8f90363c6b36255fb9",
"0x1b",
"0xd667c5a20fa899b253924099e10ae92998626718585b8171eb98de468bbebc",
"0x58351f48ce34bf134ee611fb5bf255a5733f0029561d345a7d46bfa344b60ac0",
"67f3c0Da351384838d7F7641AB0fCAcF853E1844",
&kernel,
)?;
test_valid_ecrecover(
"0x55f77e8909b1f1c9531c4a309bb2d40388e9ed4b87830c8f90363c6b36255fb9",
"0x1c",
"0xd667c5a20fa899b253924099e10ae92998626718585b8171eb98de468bbebc",
"0x58351f48ce34bf134ee611fb5bf255a5733f0029561d345a7d46bfa344b60ac0",
"aA58436DeABb64982a386B2De1A8015AA28fCCc0",
&kernel,
)?;
// test_valid_ecrecover(
// "0x0",
// "0x1c",
// "0x3a18b21408d275dde53c0ea86f9c1982eca60193db0ce15008fa408d43024847",
// "0x5db9745f44089305b2f2c980276e7025a594828d878e6e36dd2abd34ca6b9e3d",
// "aA58436DeABb64982a386B2De1A8015AA28fCCc0",
// &kernel,
// )?;
Ok(())
}