mirror of
https://github.com/logos-storage/plonky2.git
synced 2026-01-03 22:33:06 +00:00
Use static KERNEL in tests
This commit is contained in:
parent
ea7fbed33a
commit
47e6093e37
@ -8,7 +8,6 @@ use keccak_hash::keccak;
|
||||
use plonky2::field::goldilocks_field::GoldilocksField;
|
||||
|
||||
use crate::cpu::kernel::aggregator::KERNEL;
|
||||
use crate::cpu::kernel::assembler::Kernel;
|
||||
use crate::cpu::kernel::constants::context_metadata::ContextMetadata;
|
||||
use crate::cpu::kernel::constants::global_metadata::GlobalMetadata;
|
||||
use crate::cpu::kernel::constants::txn_fields::NormalizedTxnField;
|
||||
@ -80,17 +79,15 @@ pub struct Interpreter<'a> {
|
||||
running: bool,
|
||||
}
|
||||
|
||||
pub fn run_with_kernel(
|
||||
// TODO: Remove param and just use KERNEL.
|
||||
kernel: &Kernel,
|
||||
pub fn run_interpreter(
|
||||
initial_offset: usize,
|
||||
initial_stack: Vec<U256>,
|
||||
) -> anyhow::Result<Interpreter> {
|
||||
) -> anyhow::Result<Interpreter<'static>> {
|
||||
run(
|
||||
&kernel.code,
|
||||
&KERNEL.code,
|
||||
initial_offset,
|
||||
initial_stack,
|
||||
&kernel.prover_inputs,
|
||||
&KERNEL.prover_inputs,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@ -3,17 +3,16 @@ mod bn {
|
||||
use anyhow::Result;
|
||||
use ethereum_types::U256;
|
||||
|
||||
use crate::cpu::kernel::aggregator::combined_kernel;
|
||||
use crate::cpu::kernel::interpreter::run_with_kernel;
|
||||
use crate::cpu::kernel::aggregator::KERNEL;
|
||||
use crate::cpu::kernel::interpreter::run_interpreter;
|
||||
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 ec_add = kernel.global_labels["ec_add"];
|
||||
let ec_double = kernel.global_labels["ec_double"];
|
||||
let ec_mul = kernel.global_labels["ec_mul"];
|
||||
let ec_add = KERNEL.global_labels["ec_add"];
|
||||
let ec_double = KERNEL.global_labels["ec_double"];
|
||||
let ec_mul = KERNEL.global_labels["ec_mul"];
|
||||
let identity = ("0x0", "0x0");
|
||||
let invalid = ("0x0", "0x3"); // Not on curve
|
||||
let point0 = (
|
||||
@ -43,110 +42,76 @@ mod bn {
|
||||
|
||||
// Standard addition #1
|
||||
let initial_stack = u256ify(["0xdeadbeef", point0.1, point0.0, point1.1, point1.0])?;
|
||||
let stack = run_with_kernel(&kernel, ec_add, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack = run_interpreter(ec_add, initial_stack)?.stack().to_vec();
|
||||
assert_eq!(stack, u256ify([point2.1, point2.0])?);
|
||||
// Standard addition #2
|
||||
let initial_stack = u256ify(["0xdeadbeef", point1.1, point1.0, point0.1, point0.0])?;
|
||||
let stack = run_with_kernel(&kernel, ec_add, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack = run_interpreter(ec_add, initial_stack)?.stack().to_vec();
|
||||
assert_eq!(stack, u256ify([point2.1, point2.0])?);
|
||||
|
||||
// Standard doubling #1
|
||||
let initial_stack = u256ify(["0xdeadbeef", point0.1, point0.0, point0.1, point0.0])?;
|
||||
let stack = run_with_kernel(&kernel, ec_add, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack = run_interpreter(ec_add, initial_stack)?.stack().to_vec();
|
||||
assert_eq!(stack, u256ify([point3.1, point3.0])?);
|
||||
// Standard doubling #2
|
||||
let initial_stack = u256ify(["0xdeadbeef", point0.1, point0.0])?;
|
||||
let stack = run_with_kernel(&kernel, ec_double, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack = run_interpreter(ec_double, initial_stack)?.stack().to_vec();
|
||||
assert_eq!(stack, u256ify([point3.1, point3.0])?);
|
||||
// Standard doubling #3
|
||||
let initial_stack = u256ify(["0xdeadbeef", "0x2", point0.1, point0.0])?;
|
||||
let stack = run_with_kernel(&kernel, ec_mul, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack = run_interpreter(ec_mul, initial_stack)?.stack().to_vec();
|
||||
assert_eq!(stack, u256ify([point3.1, point3.0])?);
|
||||
|
||||
// Addition with identity #1
|
||||
let initial_stack = u256ify(["0xdeadbeef", identity.1, identity.0, point1.1, point1.0])?;
|
||||
let stack = run_with_kernel(&kernel, ec_add, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack = run_interpreter(ec_add, initial_stack)?.stack().to_vec();
|
||||
assert_eq!(stack, u256ify([point1.1, point1.0])?);
|
||||
// Addition with identity #2
|
||||
let initial_stack = u256ify(["0xdeadbeef", point1.1, point1.0, identity.1, identity.0])?;
|
||||
let stack = run_with_kernel(&kernel, ec_add, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack = run_interpreter(ec_add, initial_stack)?.stack().to_vec();
|
||||
assert_eq!(stack, u256ify([point1.1, point1.0])?);
|
||||
// Addition with identity #3
|
||||
let initial_stack =
|
||||
u256ify(["0xdeadbeef", identity.1, identity.0, identity.1, identity.0])?;
|
||||
let stack = run_with_kernel(&kernel, ec_add, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack = run_interpreter(ec_add, initial_stack)?.stack().to_vec();
|
||||
assert_eq!(stack, u256ify([identity.1, identity.0])?);
|
||||
|
||||
// Addition with invalid point(s) #1
|
||||
let initial_stack = u256ify(["0xdeadbeef", point0.1, point0.0, invalid.1, invalid.0])?;
|
||||
let stack = run_with_kernel(&kernel, ec_add, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack = run_interpreter(ec_add, initial_stack)?.stack().to_vec();
|
||||
assert_eq!(stack, vec![U256::MAX, U256::MAX]);
|
||||
// Addition with invalid point(s) #2
|
||||
let initial_stack = u256ify(["0xdeadbeef", invalid.1, invalid.0, point0.1, point0.0])?;
|
||||
let stack = run_with_kernel(&kernel, ec_add, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack = run_interpreter(ec_add, initial_stack)?.stack().to_vec();
|
||||
assert_eq!(stack, vec![U256::MAX, U256::MAX]);
|
||||
// Addition with invalid point(s) #3
|
||||
let initial_stack = u256ify(["0xdeadbeef", invalid.1, invalid.0, identity.1, identity.0])?;
|
||||
let stack = run_with_kernel(&kernel, ec_add, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack = run_interpreter(ec_add, initial_stack)?.stack().to_vec();
|
||||
assert_eq!(stack, vec![U256::MAX, U256::MAX]);
|
||||
// Addition with invalid point(s) #4
|
||||
let initial_stack = u256ify(["0xdeadbeef", invalid.1, invalid.0, invalid.1, invalid.0])?;
|
||||
let stack = run_with_kernel(&kernel, ec_add, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack = run_interpreter(ec_add, initial_stack)?.stack().to_vec();
|
||||
assert_eq!(stack, vec![U256::MAX, U256::MAX]);
|
||||
|
||||
// Scalar multiplication #1
|
||||
let initial_stack = u256ify(["0xdeadbeef", s, point0.1, point0.0])?;
|
||||
let stack = run_with_kernel(&kernel, ec_mul, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack = run_interpreter(ec_mul, initial_stack)?.stack().to_vec();
|
||||
assert_eq!(stack, u256ify([point4.1, point4.0])?);
|
||||
// Scalar multiplication #2
|
||||
let initial_stack = u256ify(["0xdeadbeef", "0x0", point0.1, point0.0])?;
|
||||
let stack = run_with_kernel(&kernel, ec_mul, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack = run_interpreter(ec_mul, initial_stack)?.stack().to_vec();
|
||||
assert_eq!(stack, u256ify([identity.1, identity.0])?);
|
||||
// Scalar multiplication #3
|
||||
let initial_stack = u256ify(["0xdeadbeef", "0x1", point0.1, point0.0])?;
|
||||
let stack = run_with_kernel(&kernel, ec_mul, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack = run_interpreter(ec_mul, initial_stack)?.stack().to_vec();
|
||||
assert_eq!(stack, u256ify([point0.1, point0.0])?);
|
||||
// Scalar multiplication #4
|
||||
let initial_stack = u256ify(["0xdeadbeef", s, identity.1, identity.0])?;
|
||||
let stack = run_with_kernel(&kernel, ec_mul, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack = run_interpreter(ec_mul, initial_stack)?.stack().to_vec();
|
||||
assert_eq!(stack, u256ify([identity.1, identity.0])?);
|
||||
// Scalar multiplication #5
|
||||
let initial_stack = u256ify(["0xdeadbeef", s, invalid.1, invalid.0])?;
|
||||
let stack = run_with_kernel(&kernel, ec_mul, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack = run_interpreter(ec_mul, initial_stack)?.stack().to_vec();
|
||||
assert_eq!(stack, vec![U256::MAX, U256::MAX]);
|
||||
|
||||
// Multiple calls
|
||||
@ -160,9 +125,7 @@ mod bn {
|
||||
point0.1,
|
||||
point0.0,
|
||||
])?;
|
||||
let stack = run_with_kernel(&kernel, ec_add, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack = run_interpreter(ec_add, initial_stack)?.stack().to_vec();
|
||||
assert_eq!(stack, u256ify([point4.1, point4.0])?);
|
||||
|
||||
Ok(())
|
||||
@ -174,7 +137,7 @@ mod secp {
|
||||
use anyhow::Result;
|
||||
|
||||
use crate::cpu::kernel::aggregator::combined_kernel;
|
||||
use crate::cpu::kernel::interpreter::{run, run_with_kernel};
|
||||
use crate::cpu::kernel::interpreter::{run, run_interpreter};
|
||||
use crate::cpu::kernel::tests::u256ify;
|
||||
|
||||
#[test]
|
||||
@ -212,9 +175,7 @@ mod secp {
|
||||
|
||||
// Standard addition #1
|
||||
let initial_stack = u256ify(["0xdeadbeef", point0.1, point0.0, point1.1, point1.0])?;
|
||||
let stack = run_with_kernel(&kernel, ec_add, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack = run_interpreter(ec_add, initial_stack)?.stack().to_vec();
|
||||
assert_eq!(stack, u256ify([point2.1, point2.0])?);
|
||||
// Standard addition #2
|
||||
let initial_stack = u256ify(["0xdeadbeef", point1.1, point1.0, point0.1, point0.0])?;
|
||||
@ -225,66 +186,46 @@ mod secp {
|
||||
|
||||
// Standard doubling #1
|
||||
let initial_stack = u256ify(["0xdeadbeef", point0.1, point0.0, point0.1, point0.0])?;
|
||||
let stack = run_with_kernel(&kernel, ec_add, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack = run_interpreter(ec_add, initial_stack)?.stack().to_vec();
|
||||
assert_eq!(stack, u256ify([point3.1, point3.0])?);
|
||||
// Standard doubling #2
|
||||
let initial_stack = u256ify(["0xdeadbeef", point0.1, point0.0])?;
|
||||
let stack = run_with_kernel(&kernel, ec_double, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack = run_interpreter(ec_double, initial_stack)?.stack().to_vec();
|
||||
assert_eq!(stack, u256ify([point3.1, point3.0])?);
|
||||
// Standard doubling #3
|
||||
let initial_stack = u256ify(["0xdeadbeef", "0x2", point0.1, point0.0])?;
|
||||
let stack = run_with_kernel(&kernel, ec_mul, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack = run_interpreter(ec_mul, initial_stack)?.stack().to_vec();
|
||||
assert_eq!(stack, u256ify([point3.1, point3.0])?);
|
||||
|
||||
// Addition with identity #1
|
||||
let initial_stack = u256ify(["0xdeadbeef", identity.1, identity.0, point1.1, point1.0])?;
|
||||
let stack = run_with_kernel(&kernel, ec_add, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack = run_interpreter(ec_add, initial_stack)?.stack().to_vec();
|
||||
assert_eq!(stack, u256ify([point1.1, point1.0])?);
|
||||
// Addition with identity #2
|
||||
let initial_stack = u256ify(["0xdeadbeef", point1.1, point1.0, identity.1, identity.0])?;
|
||||
let stack = run_with_kernel(&kernel, ec_add, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack = run_interpreter(ec_add, initial_stack)?.stack().to_vec();
|
||||
assert_eq!(stack, u256ify([point1.1, point1.0])?);
|
||||
// Addition with identity #3
|
||||
let initial_stack =
|
||||
u256ify(["0xdeadbeef", identity.1, identity.0, identity.1, identity.0])?;
|
||||
let stack = run_with_kernel(&kernel, ec_add, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack = run_interpreter(ec_add, initial_stack)?.stack().to_vec();
|
||||
assert_eq!(stack, u256ify([identity.1, identity.0])?);
|
||||
|
||||
// Scalar multiplication #1
|
||||
let initial_stack = u256ify(["0xdeadbeef", s, point0.1, point0.0])?;
|
||||
let stack = run_with_kernel(&kernel, ec_mul, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack = run_interpreter(ec_mul, initial_stack)?.stack().to_vec();
|
||||
assert_eq!(stack, u256ify([point4.1, point4.0])?);
|
||||
// Scalar multiplication #2
|
||||
let initial_stack = u256ify(["0xdeadbeef", "0x0", point0.1, point0.0])?;
|
||||
let stack = run_with_kernel(&kernel, ec_mul, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack = run_interpreter(ec_mul, initial_stack)?.stack().to_vec();
|
||||
assert_eq!(stack, u256ify([identity.1, identity.0])?);
|
||||
// Scalar multiplication #3
|
||||
let initial_stack = u256ify(["0xdeadbeef", "0x1", point0.1, point0.0])?;
|
||||
let stack = run_with_kernel(&kernel, ec_mul, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack = run_interpreter(ec_mul, initial_stack)?.stack().to_vec();
|
||||
assert_eq!(stack, u256ify([point0.1, point0.0])?);
|
||||
// Scalar multiplication #4
|
||||
let initial_stack = u256ify(["0xdeadbeef", s, identity.1, identity.0])?;
|
||||
let stack = run_with_kernel(&kernel, ec_mul, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack = run_interpreter(ec_mul, initial_stack)?.stack().to_vec();
|
||||
assert_eq!(stack, u256ify([identity.1, identity.0])?);
|
||||
|
||||
// Multiple calls
|
||||
@ -298,9 +239,7 @@ mod secp {
|
||||
point0.1,
|
||||
point0.0,
|
||||
])?;
|
||||
let stack = run_with_kernel(&kernel, ec_add, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack = run_interpreter(ec_add, initial_stack)?.stack().to_vec();
|
||||
assert_eq!(stack, u256ify([point4.1, point4.0])?);
|
||||
|
||||
Ok(())
|
||||
|
||||
@ -3,35 +3,23 @@ use std::str::FromStr;
|
||||
use anyhow::Result;
|
||||
use ethereum_types::U256;
|
||||
|
||||
use crate::cpu::kernel::aggregator::combined_kernel;
|
||||
use crate::cpu::kernel::assembler::Kernel;
|
||||
use crate::cpu::kernel::interpreter::run_with_kernel;
|
||||
use crate::cpu::kernel::aggregator::KERNEL;
|
||||
use crate::cpu::kernel::interpreter::run_interpreter;
|
||||
use crate::cpu::kernel::tests::u256ify;
|
||||
|
||||
fn test_valid_ecrecover(
|
||||
hash: &str,
|
||||
v: &str,
|
||||
r: &str,
|
||||
s: &str,
|
||||
expected: &str,
|
||||
kernel: &Kernel,
|
||||
) -> Result<()> {
|
||||
let ecrecover = kernel.global_labels["ecrecover"];
|
||||
fn test_valid_ecrecover(hash: &str, v: &str, r: &str, s: &str, expected: &str) -> Result<()> {
|
||||
let ecrecover = KERNEL.global_labels["ecrecover"];
|
||||
let initial_stack = u256ify(["0xdeadbeef", s, r, v, hash])?;
|
||||
let stack = run_with_kernel(kernel, ecrecover, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack = run_interpreter(ecrecover, initial_stack)?.stack().to_vec();
|
||||
assert_eq!(stack[0], U256::from_str(expected).unwrap());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn test_invalid_ecrecover(hash: &str, v: &str, r: &str, s: &str, kernel: &Kernel) -> Result<()> {
|
||||
let ecrecover = kernel.global_labels["ecrecover"];
|
||||
fn test_invalid_ecrecover(hash: &str, v: &str, r: &str, s: &str) -> Result<()> {
|
||||
let ecrecover = KERNEL.global_labels["ecrecover"];
|
||||
let initial_stack = u256ify(["0xdeadbeef", s, r, v, hash])?;
|
||||
let stack = run_with_kernel(kernel, ecrecover, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack = run_interpreter(ecrecover, initial_stack)?.stack().to_vec();
|
||||
assert_eq!(stack, vec![U256::MAX]);
|
||||
|
||||
Ok(())
|
||||
@ -39,15 +27,12 @@ fn test_invalid_ecrecover(hash: &str, v: &str, r: &str, s: &str, kernel: &Kernel
|
||||
|
||||
#[test]
|
||||
fn test_ecrecover() -> Result<()> {
|
||||
let kernel = combined_kernel();
|
||||
|
||||
test_valid_ecrecover(
|
||||
"0x55f77e8909b1f1c9531c4a309bb2d40388e9ed4b87830c8f90363c6b36255fb9",
|
||||
"0x1b",
|
||||
"0xd667c5a20fa899b253924099e10ae92998626718585b8171eb98de468bbebc",
|
||||
"0x58351f48ce34bf134ee611fb5bf255a5733f0029561d345a7d46bfa344b60ac0",
|
||||
"0x67f3c0Da351384838d7F7641AB0fCAcF853E1844",
|
||||
&kernel,
|
||||
)?;
|
||||
test_valid_ecrecover(
|
||||
"0x55f77e8909b1f1c9531c4a309bb2d40388e9ed4b87830c8f90363c6b36255fb9",
|
||||
@ -55,7 +40,6 @@ fn test_ecrecover() -> Result<()> {
|
||||
"0xd667c5a20fa899b253924099e10ae92998626718585b8171eb98de468bbebc",
|
||||
"0x58351f48ce34bf134ee611fb5bf255a5733f0029561d345a7d46bfa344b60ac0",
|
||||
"0xaA58436DeABb64982a386B2De1A8015AA28fCCc0",
|
||||
&kernel,
|
||||
)?;
|
||||
test_valid_ecrecover(
|
||||
"0x0",
|
||||
@ -63,7 +47,6 @@ fn test_ecrecover() -> Result<()> {
|
||||
"0x1",
|
||||
"0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140",
|
||||
"0x3344c6f6eeCA588be132142DB0a32C71ABFAAe7B",
|
||||
&kernel,
|
||||
)?;
|
||||
|
||||
test_invalid_ecrecover(
|
||||
@ -71,28 +54,24 @@ fn test_ecrecover() -> Result<()> {
|
||||
"0x42", // v not in {27,28}
|
||||
"0x1",
|
||||
"0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140",
|
||||
&kernel,
|
||||
)?;
|
||||
test_invalid_ecrecover(
|
||||
"0x0",
|
||||
"0x42",
|
||||
"0xd667c5a20fa899b253924099e10ae92998626718585b8171eb98de468bbebc",
|
||||
"0x0", // s=0
|
||||
&kernel,
|
||||
)?;
|
||||
test_invalid_ecrecover(
|
||||
"0x0",
|
||||
"0x42",
|
||||
"0x0", // r=0
|
||||
"0xd667c5a20fa899b253924099e10ae92998626718585b8171eb98de468bbebc",
|
||||
&kernel,
|
||||
)?;
|
||||
test_invalid_ecrecover(
|
||||
"0x0",
|
||||
"0x1c",
|
||||
"0x3a18b21408d275dde53c0ea86f9c1982eca60193db0ce15008fa408d43024847", // r^3 + 7 isn't a square
|
||||
"0x5db9745f44089305b2f2c980276e7025a594828d878e6e36dd2abd34ca6b9e3d",
|
||||
&kernel,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
|
||||
@ -2,50 +2,43 @@ use anyhow::Result;
|
||||
use ethereum_types::U256;
|
||||
use rand::{thread_rng, Rng};
|
||||
|
||||
use crate::cpu::kernel::aggregator::combined_kernel;
|
||||
use crate::cpu::kernel::interpreter::{run, run_with_kernel};
|
||||
use crate::cpu::kernel::aggregator::KERNEL;
|
||||
use crate::cpu::kernel::interpreter::{run, run_interpreter};
|
||||
|
||||
#[test]
|
||||
fn test_exp() -> Result<()> {
|
||||
// Make sure we can parse and assemble the entire kernel.
|
||||
let kernel = combined_kernel();
|
||||
let exp = kernel.global_labels["exp"];
|
||||
let exp = KERNEL.global_labels["exp"];
|
||||
let mut rng = thread_rng();
|
||||
let a = U256([0; 4].map(|_| rng.gen()));
|
||||
let b = U256([0; 4].map(|_| rng.gen()));
|
||||
|
||||
// Random input
|
||||
let initial_stack = vec![0xDEADBEEFu32.into(), b, a];
|
||||
let stack_with_kernel = run_with_kernel(&kernel, exp, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack_with_kernel = run_interpreter(exp, initial_stack)?.stack().to_vec();
|
||||
let initial_stack = vec![b, a];
|
||||
let code = [0xa, 0x63, 0xde, 0xad, 0xbe, 0xef, 0x56]; // EXP, PUSH4 deadbeef, JUMP
|
||||
let stack_with_opcode = run(&code, 0, initial_stack, &kernel.prover_inputs)?
|
||||
let stack_with_opcode = run(&code, 0, initial_stack, &KERNEL.prover_inputs)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
assert_eq!(stack_with_kernel, stack_with_opcode);
|
||||
|
||||
// 0 base
|
||||
let initial_stack = vec![0xDEADBEEFu32.into(), b, U256::zero()];
|
||||
let stack_with_kernel = run_with_kernel(&kernel, exp, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack_with_kernel = run_interpreter(exp, initial_stack)?.stack().to_vec();
|
||||
let initial_stack = vec![b, U256::zero()];
|
||||
let code = [0xa, 0x63, 0xde, 0xad, 0xbe, 0xef, 0x56]; // EXP, PUSH4 deadbeef, JUMP
|
||||
let stack_with_opcode = run(&code, 0, initial_stack, &kernel.prover_inputs)?
|
||||
let stack_with_opcode = run(&code, 0, initial_stack, &KERNEL.prover_inputs)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
assert_eq!(stack_with_kernel, stack_with_opcode);
|
||||
|
||||
// 0 exponent
|
||||
let initial_stack = vec![0xDEADBEEFu32.into(), U256::zero(), a];
|
||||
let stack_with_kernel = run_with_kernel(&kernel, exp, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let stack_with_kernel = run_interpreter(exp, initial_stack)?.stack().to_vec();
|
||||
let initial_stack = vec![U256::zero(), a];
|
||||
let code = [0xa, 0x63, 0xde, 0xad, 0xbe, 0xef, 0x56]; // EXP, PUSH4 deadbeef, JUMP
|
||||
let stack_with_opcode = run(&code, 0, initial_stack, &kernel.prover_inputs)?
|
||||
let stack_with_opcode = run(&code, 0, initial_stack, &KERNEL.prover_inputs)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
assert_eq!(stack_with_kernel, stack_with_opcode);
|
||||
|
||||
@ -2,8 +2,8 @@ use anyhow::Result;
|
||||
use ethereum_types::U256;
|
||||
use rand::{thread_rng, Rng};
|
||||
|
||||
use crate::cpu::kernel::aggregator::combined_kernel;
|
||||
use crate::cpu::kernel::interpreter::run_with_kernel;
|
||||
use crate::cpu::kernel::aggregator::KERNEL;
|
||||
use crate::cpu::kernel::interpreter::run_interpreter;
|
||||
|
||||
// TODO: 107 is hardcoded as a dummy prime for testing
|
||||
// should be changed to the proper implementation prime
|
||||
@ -137,10 +137,9 @@ fn test_fp6() -> Result<()> {
|
||||
let mut input: Vec<u32> = [c, d].into_iter().flatten().flatten().collect();
|
||||
input.push(0xdeadbeef);
|
||||
|
||||
let kernel = combined_kernel();
|
||||
let initial_offset = kernel.global_labels["mul_fp6"];
|
||||
let initial_offset = KERNEL.global_labels["mul_fp6"];
|
||||
let initial_stack: Vec<U256> = as_stack(input);
|
||||
let final_stack: Vec<U256> = run_with_kernel(&kernel, initial_offset, initial_stack)?
|
||||
let final_stack: Vec<U256> = run_interpreter(initial_offset, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
|
||||
|
||||
@ -6,8 +6,8 @@ use rand::{thread_rng, Rng};
|
||||
use ripemd::{Digest, Ripemd160};
|
||||
use sha2::Sha256;
|
||||
|
||||
use crate::cpu::kernel::aggregator::combined_kernel;
|
||||
use crate::cpu::kernel::interpreter::run_with_kernel;
|
||||
use crate::cpu::kernel::aggregator::KERNEL;
|
||||
use crate::cpu::kernel::interpreter::run_interpreter;
|
||||
|
||||
/// Standard Sha2 implementation.
|
||||
fn sha2(input: Vec<u8>) -> U256 {
|
||||
@ -62,12 +62,11 @@ fn test_hash(hash_fn_label: &str, standard_implementation: &dyn Fn(Vec<u8>) -> U
|
||||
let initial_stack_custom = make_input_stack(message_custom);
|
||||
|
||||
// Make the kernel.
|
||||
let kernel = combined_kernel();
|
||||
let kernel_function = kernel.global_labels[hash_fn_label];
|
||||
let kernel_function = KERNEL.global_labels[hash_fn_label];
|
||||
|
||||
// Run the kernel code.
|
||||
let result_random = run_with_kernel(&kernel, kernel_function, initial_stack_random)?;
|
||||
let result_custom = run_with_kernel(&kernel, kernel_function, initial_stack_custom)?;
|
||||
let result_random = run_interpreter(kernel_function, initial_stack_random)?;
|
||||
let result_custom = run_interpreter(kernel_function, initial_stack_custom)?;
|
||||
|
||||
// Extract the final output.
|
||||
let actual_random = result_random.stack()[0];
|
||||
|
||||
@ -2,8 +2,8 @@ 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;
|
||||
use crate::cpu::kernel::aggregator::KERNEL;
|
||||
use crate::cpu::kernel::interpreter::run_interpreter;
|
||||
|
||||
fn make_input(word: &str) -> Vec<u32> {
|
||||
let mut input: Vec<u32> = vec![word.len().try_into().unwrap()];
|
||||
@ -44,10 +44,9 @@ fn test_ripemd_reference() -> Result<()> {
|
||||
let input: Vec<u32> = make_input(x);
|
||||
let expected = U256::from(y);
|
||||
|
||||
let kernel = combined_kernel();
|
||||
let initial_offset = kernel.global_labels["ripemd_stack"];
|
||||
let initial_offset = KERNEL.global_labels["ripemd_stack"];
|
||||
let initial_stack: Vec<U256> = input.iter().map(|&x| U256::from(x)).rev().collect();
|
||||
let final_stack: Vec<U256> = run_with_kernel(&kernel, initial_offset, initial_stack)?
|
||||
let final_stack: Vec<U256> = run_interpreter(initial_offset, initial_stack)?
|
||||
.stack()
|
||||
.to_vec();
|
||||
let actual = final_stack[0];
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user