Merge branch 'main' into evm_generation

This commit is contained in:
Daniel Lubarov 2022-07-11 09:58:38 -07:00
commit 8a2a035411
12 changed files with 1720 additions and 65 deletions

View File

@ -1,30 +1,217 @@
//! Loads each kernel assembly file and concatenates them.
use std::collections::HashMap;
use ethereum_types::U256;
use itertools::Itertools;
use super::assembler::{assemble, Kernel};
use crate::cpu::kernel::parser::parse;
pub fn evm_constants() -> HashMap<String, U256> {
let mut c = HashMap::new();
c.insert("SEGMENT_ID_TXN_DATA".into(), 0.into()); // TODO: Replace with actual segment ID.
c
}
#[allow(dead_code)] // TODO: Should be used once witness generation is done.
pub(crate) fn combined_kernel() -> Kernel {
let files = vec![
include_str!("asm/basic_macros.asm"),
include_str!("asm/exp.asm"),
include_str!("asm/curve_mul.asm"),
include_str!("asm/curve_add.asm"),
include_str!("asm/moddiv.asm"),
include_str!("asm/storage_read.asm"),
include_str!("asm/storage_write.asm"),
];
let parsed_files = files.iter().map(|f| parse(f)).collect_vec();
assemble(parsed_files)
assemble(parsed_files, evm_constants())
}
#[cfg(test)]
mod tests {
use std::str::FromStr;
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;
#[test]
fn make_kernel() {
// Make sure we can parse and assemble the entire kernel.
combined_kernel();
let kernel = combined_kernel();
println!("Kernel size: {} bytes", kernel.code.len());
}
fn u256ify<'a>(hexes: impl IntoIterator<Item = &'a str>) -> Result<Vec<U256>> {
Ok(hexes
.into_iter()
.map(U256::from_str)
.collect::<Result<Vec<_>, _>>()?)
}
#[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 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![U256::from_str("0xdeadbeef")?, b, a];
let stack_with_kernel = run(&kernel.code, exp, initial_stack);
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);
assert_eq!(stack_with_kernel, stack_with_opcode);
// 0 base
let initial_stack = vec![U256::from_str("0xdeadbeef")?, b, U256::zero()];
let stack_with_kernel = run(&kernel.code, exp, initial_stack);
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);
assert_eq!(stack_with_kernel, stack_with_opcode);
// 0 exponent
let initial_stack = vec![U256::from_str("0xdeadbeef")?, U256::zero(), a];
let stack_with_kernel = run(&kernel.code, exp, initial_stack);
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);
assert_eq!(stack_with_kernel, stack_with_opcode);
Ok(())
}
#[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 identity = ("0x0", "0x0");
let invalid = ("0x0", "0x3"); // Not on curve
let point0 = (
"0x1feee7ec986e198890cb83be8b8ba09ee953b3f149db6d9bfdaa5c308a33e58d",
"0x2051cc9a9edd46231604fd88f351e95ec72a285be93e289ac59cb48561efb2c6",
);
let point1 = (
"0x15b64d0a5f329fb672029298be8050f444626e6de11903caffa74b388075be1b",
"0x2d9e07340bd5cd7b70687b98f2500ff930a89a30d7b6a3e04b1b4d345319d234",
);
// point2 = point0 + point1
let point2 = (
"0x18659c0e0a8fedcb8747cf463fc7cfa05f667d84e771d0a9521fc1a550688f0c",
"0x283ed10b42703e187e7a808aeb45c6b457bc4cc7d704e53b3348a1e3b0bfa55b",
);
// point3 = 2 * point0
let point3 = (
"0x17da2b7b1a01c8dfdf0f5a6415833c7d755d219aa7e2c4cd0ac83d87d0ca4217",
"0xc9ace9de14aac8114541b50c19320eb40f0eeac3621526d9e34dbcf4c3a6c0f",
);
let s = "0xabb2a34c0e7956cfe6cef9ddb7e810c45ea19a6ebadd79c21959af09f5ba480a";
// point4 = s * point0
let point4 = (
"0xe519344959cc17021fe98878f947f5c1b1675325533a620c1684cfa6367e6c0",
"0x7496a7575b0b6a821e19ce780ecc3e0b156e605327798693defeb9f265b7a6f",
);
// Standard addition #1
let initial_stack = u256ify(["0xdeadbeef", point0.1, point0.0, point1.1, point1.0])?;
let stack = run(&kernel.code, ec_add, initial_stack);
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(&kernel.code, ec_add, initial_stack);
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(&kernel.code, ec_add, initial_stack);
assert_eq!(stack, u256ify([point3.1, point3.0])?);
// Standard doubling #2
let initial_stack = u256ify(["0xdeadbeef", point0.1, point0.0])?;
let stack = run(&kernel.code, ec_double, initial_stack);
assert_eq!(stack, u256ify([point3.1, point3.0])?);
// Standard doubling #3
let initial_stack = u256ify(["0xdeadbeef", "0x2", point0.1, point0.0])?;
let stack = run(&kernel.code, ec_mul, initial_stack);
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(&kernel.code, ec_add, initial_stack);
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(&kernel.code, ec_add, initial_stack);
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(&kernel.code, ec_add, initial_stack);
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(&kernel.code, ec_add, initial_stack);
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(&kernel.code, ec_add, initial_stack);
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(&kernel.code, ec_add, initial_stack);
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(&kernel.code, ec_add, initial_stack);
assert_eq!(stack, vec![U256::MAX, U256::MAX]);
// Scalar multiplication #1
let initial_stack = u256ify(["0xdeadbeef", s, point0.1, point0.0])?;
let stack = run(&kernel.code, ec_mul, initial_stack);
assert_eq!(stack, u256ify([point4.1, point4.0])?);
// Scalar multiplication #2
let initial_stack = u256ify(["0xdeadbeef", "0x0", point0.1, point0.0])?;
let stack = run(&kernel.code, ec_mul, initial_stack);
assert_eq!(stack, u256ify([identity.1, identity.0])?);
// Scalar multiplication #3
let initial_stack = u256ify(["0xdeadbeef", "0x1", point0.1, point0.0])?;
let stack = run(&kernel.code, ec_mul, initial_stack);
assert_eq!(stack, u256ify([point0.1, point0.0])?);
// Scalar multiplication #4
let initial_stack = u256ify(["0xdeadbeef", s, identity.1, identity.0])?;
let stack = run(&kernel.code, ec_mul, initial_stack);
assert_eq!(stack, u256ify([identity.1, identity.0])?);
// Scalar multiplication #5
let initial_stack = u256ify(["0xdeadbeef", s, invalid.1, invalid.0])?;
let stack = run(&kernel.code, ec_mul, initial_stack);
assert_eq!(stack, vec![U256::MAX, U256::MAX]);
// Multiple calls
let ec_mul_hex = format!("0x{:x}", ec_mul);
let initial_stack = u256ify([
"0xdeadbeef",
s,
&ec_mul_hex,
identity.1,
identity.0,
point0.1,
point0.0,
])?;
let stack = run(&kernel.code, ec_add, initial_stack);
assert_eq!(stack, u256ify([point4.1, point4.0])?);
Ok(())
}
}

View File

@ -1,3 +1,28 @@
%macro jump(dst)
push $dst
jump
%endmacro
%macro jumpi(dst)
push $dst
jumpi
%endmacro
%macro pop2
pop
pop
%endmacro
%macro pop3
pop
%pop2
%endmacro
%macro pop4
%pop2
%pop2
%endmacro
// If pred is zero, yields z; otherwise, yields nz
%macro select
// stack: pred, nz, z

View File

@ -0,0 +1,361 @@
// #define N 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47 // BN254 base field order
// BN254 elliptic curve addition.
// Uses the standard affine addition formula.
global ec_add:
// Uncomment for test inputs.
// PUSH 0xdeadbeef
// PUSH 2
// PUSH 1
// PUSH 0x1bf9384aa3f0b3ad763aee81940cacdde1af71617c06f46e11510f14f3d5d121
// PUSH 0xe7313274bb29566ff0c8220eb9841de1d96c2923c6a4028f7dd3c6a14cee770
JUMPDEST
// stack: x0, y0, x1, y1, retdest
// Check if points are valid BN254 points.
DUP2
// stack: y0, x0, y0, x1, y1, retdest
DUP2
// stack: x0, y0, x0, y0, x1, y1, retdest
%ec_check
// stack: isValid(x0, y0), x0, y0, x1, y1, retdest
DUP5
// stack: x1, isValid(x0, y0), x0, y0, x1, y1, retdest
DUP5
// stack: x1, y1, isValid(x0, y0), x0, y0, x1, y1, retdest
%ec_check
// stack: isValid(x1, y1), isValid(x0, y0), x0, y0, x1, y1, retdest
AND
// stack: isValid(x1, y1) & isValid(x0, y0), x0, y0, x1, y1, retdest
%jumpi(ec_add_valid_points)
// stack: x0, y0, x1, y1, retdest
// Otherwise return
%pop4
// stack: retdest
%ec_invalid_input
// BN254 elliptic curve addition.
// Assumption: (x0,y0) and (x1,y1) are valid points.
global ec_add_valid_points:
JUMPDEST
// stack: x0, y0, x1, y1, retdest
// Check if the first point is the identity.
DUP2
// stack: y0, x0, y0, x1, y1, retdest
DUP2
// stack: x0, y0, x0, y0, x1, y1, retdest
%ec_isidentity
// stack: (x0,y0)==(0,0), x0, y0, x1, y1, retdest
%jumpi(ec_add_first_zero)
// stack: x0, y0, x1, y1, retdest
// Check if the first point is the identity.
DUP4
// stack: y1, x0, y0, x1, y1, retdest
DUP4
// stack: x1, y1, x0, y0, x1, y1, retdest
%ec_isidentity
// stack: (x1,y1)==(0,0), x0, y0, x1, y1, retdest
%jumpi(ec_add_snd_zero)
// stack: x0, y0, x1, y1, retdest
// Check if both points have the same x-coordinate.
DUP3
// stack: x1, x0, y0, x1, y1, retdest
DUP2
// stack: x0, x1, x0, y0, x1, y1, retdest
EQ
// stack: x0 == x1, x0, y0, x1, y1, retdest
%jumpi(ec_add_equal_first_coord)
// stack: x0, y0, x1, y1, retdest
// Otherwise, we can use the standard formula.
// Compute lambda = (y0 - y1)/(x0 - x1)
DUP4
// stack: y1, x0, y0, x1, y1, retdest
DUP3
// stack: y0, y1, x0, y0, x1, y1, retdest
%submod
// stack: y0 - y1, x0, y0, x1, y1, retdest
DUP4
// stack: x1, y0 - y1, x0, y0, x1, y1, retdest
DUP3
// stack: x0, x1, y0 - y1, x0, y0, x1, y1, retdest
%submod
// stack: x0 - x1, y0 - y1, x0, y0, x1, y1, retdest
%moddiv
// stack: lambda, x0, y0, x1, y1, retdest
%jump(ec_add_valid_points_with_lambda)
// BN254 elliptic curve addition.
// Assumption: (x0,y0) == (0,0)
ec_add_first_zero:
JUMPDEST
// stack: x0, y0, x1, y1, retdest
// Just return (x1,y1)
%pop2
// stack: x1, y1, retdest
SWAP1
// stack: y1, x1, retdest
SWAP2
// stack: retdest, x1, y1
JUMP
// BN254 elliptic curve addition.
// Assumption: (x1,y1) == (0,0)
ec_add_snd_zero:
JUMPDEST
// stack: x0, y0, x1, y1, retdest
// Just return (x1,y1)
SWAP2
// stack: x1, y0, x0, y1, retdest
POP
// stack: y0, x0, y1, retdest
SWAP2
// stack: y1, x0, y0, retdest
POP
// stack: x0, y0, retdest
SWAP1
// stack: y0, x0, retdest
SWAP2
// stack: retdest, x0, y0
JUMP
// BN254 elliptic curve addition.
// Assumption: lambda = (y0 - y1)/(x0 - x1)
ec_add_valid_points_with_lambda:
JUMPDEST
// stack: lambda, x0, y0, x1, y1, retdest
// Compute x2 = lambda^2 - x1 - x0
DUP2
// stack: x0, lambda, x0, y0, x1, y1, retdest
DUP5
// stack: x1, x0, lambda, x0, y0, x1, y1, retdest
%bn_base
// stack: N, x1, x0, lambda, x0, y0, x1, y1, retdest
DUP4
// stack: lambda, N, x1, x0, lambda, x0, y0, x1, y1, retdest
DUP1
// stack: lambda, lambda, N, x1, x0, lambda, x0, y0, x1, y1, retdest
MULMOD
// stack: lambda^2, x1, x0, lambda, x0, y0, x1, y1, retdest
%submod
// stack: lambda^2 - x1, x0, lambda, x0, y0, x1, y1, retdest
%submod
// stack: x2, lambda, x0, y0, x1, y1, retdest
// Compute y2 = lambda*(x1 - x2) - y1
%bn_base
// stack: N, x2, lambda, x0, y0, x1, y1, retdest
DUP2
// stack: x2, N, x2, lambda, x0, y0, x1, y1, retdest
DUP7
// stack: x1, x2, N, x2, lambda, x0, y0, x1, y1, retdest
%submod
// stack: x1 - x2, N, x2, lambda, x0, y0, x1, y1, retdest
DUP4
// stack: lambda, x1 - x2, N, x2, lambda, x0, y0, x1, y1, retdest
MULMOD
// stack: lambda * (x1 - x2), x2, lambda, x0, y0, x1, y1, retdest
DUP7
// stack: y1, lambda * (x1 - x2), x2, lambda, x0, y0, x1, y1, retdest
SWAP1
// stack: lambda * (x1 - x2), y1, x2, lambda, x0, y0, x1, y1, retdest
%submod
// stack: y2, x2, lambda, x0, y0, x1, y1, retdest
// Return x2,y2
SWAP5
// stack: x1, x2, lambda, x0, y0, y2, y1, retdest
POP
// stack: x2, lambda, x0, y0, y2, y1, retdest
SWAP5
// stack: y1, lambda, x0, y0, y2, x2, retdest
%pop4
// stack: y2, x2, retdest
SWAP2
// stack: retdest, x2, y2
JUMP
// BN254 elliptic curve addition.
// Assumption: (x0,y0) and (x1,y1) are valid points and x0 == x1
ec_add_equal_first_coord:
JUMPDEST
// stack: x0, y0, x1, y1, retdest with x0 == x1
// Check if the points are equal
DUP2
// stack: y0, x0, y0, x1, y1, retdest
DUP5
// stack: y1, y0, x0, y0, x1, y1, retdest
EQ
// stack: y1 == y0, x0, y0, x1, y1, retdest
%jumpi(ec_add_equal_points)
// stack: x0, y0, x1, y1, retdest
// Otherwise, one is the negation of the other so we can return (0,0).
%pop4
// stack: retdest
PUSH 0
// stack: 0, retdest
PUSH 0
// stack: 0, 0, retdest
SWAP2
// stack: retdest, 0, 0
JUMP
// BN254 elliptic curve addition.
// Assumption: x0 == x1 and y0 == y1
// Standard doubling formula.
ec_add_equal_points:
JUMPDEST
// stack: x0, y0, x1, y1, retdest
// Compute lambda = 3/2 * x0^2 / y0
%bn_base
// stack: N, x0, y0, x1, y1, retdest
%bn_base
// stack: N, N, x0, y0, x1, y1, retdest
DUP3
// stack: x0, N, N, x0, y0, x1, y1, retdest
DUP1
// stack: x0, x0, N, N, x0, y0, x1, y1, retdest
MULMOD
// stack: x0^2, N, x0, y0, x1, y1, retdest with
PUSH 0x183227397098d014dc2822db40c0ac2ecbc0b548b438e5469e10460b6c3e7ea5 // 3/2 in the base field
// stack: 3/2, x0^2, N, x0, y0, x1, y1, retdest
MULMOD
// stack: 3/2 * x0^2, x0, y0, x1, y1, retdest
DUP3
// stack: y0, 3/2 * x0^2, x0, y0, x1, y1, retdest
%moddiv
// stack: lambda, x0, y0, x1, y1, retdest
%jump(ec_add_valid_points_with_lambda)
// BN254 elliptic curve doubling.
// Assumption: (x0,y0) is a valid point.
// Standard doubling formula.
global ec_double:
JUMPDEST
// stack: x0, y0, retdest
DUP2
// stack: y0, x0, y0, retdest
DUP2
// stack: x0, y0, x0, y0, retdest
%jump(ec_add_equal_points)
// Push the order of the BN254 base field.
%macro bn_base
PUSH 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47
%endmacro
// Assumption: x, y < N and 2N < 2^256.
// Note: Doesn't hold for Secp256k1 base field.
%macro submod
// stack: x, y
%bn_base
// stack: N, x, y
ADD
// stack: N + x, y // Doesn't overflow since 2N < 2^256
SUB
// stack: N + x - y // Doesn't underflow since y < N
%bn_base
// stack: N, N + x - y
SWAP1
// stack: N + x - y, N
MOD
// stack: (N + x - y) % N = (x-y) % N
%endmacro
// Check if (x,y) is a valid curve point.
// Puts y^2 % N == (x^3 + 3) % N & (x < N) & (y < N) || (x,y)==(0,0) on top of the stack.
%macro ec_check
// stack: x, y
%bn_base
// stack: N, x, y
DUP2
// stack: x, N, x, y
LT
// stack: x < N, x, y
%bn_base
// stack: N, x < N, x, y
DUP4
// stack: y, N, x < N, x, y
LT
// stack: y < N, x < N, x, y
AND
// stack: (y < N) & (x < N), x, y
SWAP2
// stack: y, x, (y < N) & (x < N), x
SWAP1
// stack: x, y, (y < N) & (x < N)
%bn_base
// stack: N, x, y, b
%bn_base
// stack: N, N, x, y, b
DUP3
// stack: x, N, N, x, y, b
%bn_base
// stack: N, x, N, N, x, y, b
DUP2
// stack: x, N, x, N, N, x, y, b
DUP1
// stack: x, x, N, x, N, N, x, y, b
MULMOD
// stack: x^2 % N, x, N, N, x, y, b
MULMOD
// stack: x^3 % N, N, x, y, b
PUSH 3
// stack: 3, x^3 % N, N, x, y, b
ADDMOD
// stack: (x^3 + 3) % N, x, y, b
DUP3
// stack: y, (x^3 + 3) % N, x, y, b
%bn_base
// stack: N, y, (x^3 + 3) % N, x, y, b
SWAP1
// stack: y, N, (x^3 + 3) % N, x, y, b
DUP1
// stack: y, y, N, (x^3 + 3) % N, x, y, b
MULMOD
// stack: y^2 % N, (x^3 + 3) % N, x, y, b
EQ
// stack: y^2 % N == (x^3 + 3) % N, x, y, b
SWAP2
// stack: y, x, y^2 % N == (x^3 + 3) % N, b
%ec_isidentity
// stack: (x,y)==(0,0), y^2 % N == (x^3 + 3) % N, b
SWAP2
// stack: b, y^2 % N == (x^3 + 3) % N, (x,y)==(0,0)
AND
// stack: y^2 % N == (x^3 + 3) % N & (x < N) & (y < N), (x,y)==(0,0)
OR
// stack: y^2 % N == (x^3 + 3) % N & (x < N) & (y < N) || (x,y)==(0,0)
%endmacro
// Check if (x,y)==(0,0)
%macro ec_isidentity
// stack: x, y
OR
// stack: x | y
ISZERO
// stack: (x,y) == (0,0)
%endmacro
// Return (u256::MAX, u256::MAX) which is used to indicate the input was invalid.
%macro ec_invalid_input
// stack: retdest
PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
// stack: u256::MAX, retdest
PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
// stack: u256::MAX, u256::MAX, retdest
SWAP2
// stack: retdest, u256::MAX, u256::MAX
JUMP
%endmacro

View File

@ -0,0 +1,114 @@
// BN254 elliptic curve scalar multiplication.
// Recursive implementation, same algorithm as in `exp.asm`.
global ec_mul:
// Uncomment for test inputs.
// PUSH 0xdeadbeef
// PUSH 0xd
// PUSH 2
// PUSH 1
JUMPDEST
// stack: x, y, s, retdest
DUP2
// stack: y, x, y, s, retdest
DUP2
// stack: x, y, x, y, s, retdest
%ec_isidentity
// stack: (x,y)==(0,0), x, y, s, retdest
%jumpi(ret_zero)
// stack: x, y, s, retdest
DUP2
// stack: y, x, y, s, retdest
DUP2
// stack: x, y, x, y, s, retdest
%ec_check
// stack: isValid(x, y), x, y, s, retdest
%jumpi(ec_mul_valid_point)
// stack: x, y, s, retdest
%pop3
%ec_invalid_input
// Same algorithm as in `exp.asm`
ec_mul_valid_point:
JUMPDEST
// stack: x, y, s, retdest
DUP3
// stack: s, x, y, s, retdest
%jumpi(step_case)
// stack: x, y, s, retdest
%jump(ret_zero)
step_case:
JUMPDEST
// stack: x, y, s, retdest
PUSH recursion_return
// stack: recursion_return, x, y, s, retdest
PUSH 2
// stack: 2, recursion_return, x, y, s, retdest
DUP5
// stack: s, 2, recursion_return, x, y, s, retdest
DIV
// stack: s / 2, recursion_return, x, y, s, retdest
PUSH step_case_contd
// stack: step_case_contd, s / 2, recursion_return, x, y, s, retdest
DUP5
// stack: y, step_case_contd, s / 2, recursion_return, x, y, s, retdest
DUP5
// stack: x, y, step_case_contd, s / 2, recursion_return, x, y, s, retdest
%jump(ec_double)
// Assumption: 2(x,y) = (x',y')
step_case_contd:
JUMPDEST
// stack: x', y', s / 2, recursion_return, x, y, s, retdest
%jump(ec_mul_valid_point)
recursion_return:
JUMPDEST
// stack: x', y', x, y, s, retdest
SWAP4
// stack: s, y', x, y, x', retdest
PUSH 1
// stack: 1, s, y', x, y, x', retdest
AND
// stack: s & 1, y', x, y, x', retdest
SWAP1
// stack: y', s & 1, x, y, x', retdest
SWAP2
// stack: x, s & 1, y', y, x', retdest
SWAP3
// stack: y, s & 1, y', x, x', retdest
SWAP4
// stack: x', s & 1, y', x, y, retdest
SWAP1
// stack: s & 1, x', y', x, y, retdest
%jumpi(odd_scalar)
// stack: x', y', x, y, retdest
SWAP3
// stack: y, y', x, x', retdest
POP
// stack: y', x, x', retdest
SWAP1
// stack: x, y', x', retdest
POP
// stack: y', x', retdest
SWAP2
// stack: retdest, x', y'
JUMP
odd_scalar:
JUMPDEST
// stack: x', y', x, y, retdest
%jump(ec_add_valid_points)
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

@ -10,12 +10,11 @@
/// Note that this correctly handles exp(0, 0) == 1.
global exp:
jumpdest
// stack: x, e, retdest
dup2
// stack: e, x, e, retdest
push step_case
// stack: step_case, e, x, e, retdest
jumpi
%jumpi(step_case)
// stack: x, e, retdest
pop
// stack: e, retdest
@ -28,6 +27,7 @@ global exp:
jump
step_case:
jumpdest
// stack: x, e, retdest
push recursion_return
// stack: recursion_return, x, e, retdest
@ -41,10 +41,9 @@ step_case:
// stack: x, e / 2, recursion_return, x, e, retdest
%square
// stack: x * x, e / 2, recursion_return, x, e, retdest
push exp
// stack: exp, x * x, e / 2, recursion_return, x, e, retdest
jump
%jump(exp)
recursion_return:
jumpdest
// stack: exp(x * x, e / 2), x, e, retdest
push 2
// stack: 2, exp(x * x, e / 2), x, e, retdest

View File

@ -0,0 +1,506 @@
/// Division modulo 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47, the BN254 base field order
/// To replace with more efficient method using non-determinism later.
// Returns y * (x^-1) where the inverse is taken modulo N
%macro moddiv
// stack: x, y
%inverse
// stack: x^-1, y
%mulmodn
%endmacro
%macro mulmodn
// stack: x, y
PUSH 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47
// stack: N, x, y
SWAP2
// stack: y, x, N
MULMOD
%endmacro
%macro squaremodn
// stack: x
DUP1
// stack: x, x
%mulmodn
%endmacro
// Computes the inverse modulo N using x^-1 = x^(N-2) mod N and square-and-multiply modular exponentiation.
%macro inverse
DUP1
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
%squaremodn
%squaremodn
DUP2
%mulmodn
%squaremodn
%squaremodn
DUP2
%mulmodn
SWAP1
// stack: x, x^-1
POP
// stack: x^-1
%endmacro

View File

@ -1,6 +1,10 @@
use std::collections::HashMap;
use ethereum_types::U256;
use itertools::izip;
use super::ast::PushTarget;
use crate::cpu::kernel::ast::Literal;
use crate::cpu::kernel::{
ast::{File, Item},
opcodes::{get_opcode, get_push_opcode},
@ -17,47 +21,68 @@ pub struct Kernel {
pub(crate) global_labels: HashMap<String, usize>,
}
pub(crate) fn assemble(files: Vec<File>) -> Kernel {
struct Macro {
params: Vec<String>,
items: Vec<Item>,
}
impl Macro {
fn get_param_index(&self, param: &str) -> usize {
self.params
.iter()
.position(|p| p == param)
.unwrap_or_else(|| panic!("No such param: {} {:?}", param, &self.params))
}
}
pub(crate) fn assemble(files: Vec<File>, constants: HashMap<String, U256>) -> Kernel {
let macros = find_macros(&files);
let mut code = vec![];
let mut global_labels = HashMap::new();
let mut offset = 0;
let mut expanded_files = Vec::with_capacity(files.len());
let mut local_labels = Vec::with_capacity(files.len());
for file in files {
let expanded_file = expand_macros(file.body, &macros);
assemble_file(expanded_file, &mut code, &mut global_labels);
let expanded_file = inline_constants(expanded_file, &constants);
local_labels.push(find_labels(&expanded_file, &mut offset, &mut global_labels));
expanded_files.push(expanded_file);
}
let mut code = vec![];
for (file, locals) in izip!(expanded_files, local_labels) {
assemble_file(file, &mut code, locals, &global_labels);
}
assert_eq!(code.len(), offset, "Code length doesn't match offset.");
Kernel {
code,
global_labels,
}
}
fn find_macros(files: &[File]) -> HashMap<String, Vec<Item>> {
fn find_macros(files: &[File]) -> HashMap<String, Macro> {
let mut macros = HashMap::new();
for file in files {
for item in &file.body {
if let Item::MacroDef(name, items) = item {
macros.insert(name.clone(), items.clone());
if let Item::MacroDef(name, params, items) = item {
let _macro = Macro {
params: params.clone(),
items: items.clone(),
};
macros.insert(name.clone(), _macro);
}
}
}
macros
}
fn expand_macros(body: Vec<Item>, macros: &HashMap<String, Vec<Item>>) -> Vec<Item> {
fn expand_macros(body: Vec<Item>, macros: &HashMap<String, Macro>) -> Vec<Item> {
let mut expanded = vec![];
for item in body {
match item {
Item::MacroDef(_, _) => {
Item::MacroDef(_, _, _) => {
// At this phase, we no longer need macro definitions.
}
Item::MacroCall(m) => {
let mut expanded_item = macros
.get(&m)
.cloned()
.unwrap_or_else(|| panic!("No such macro: {}", m));
// Recursively expand any macros in the expanded code.
expanded_item = expand_macros(expanded_item, macros);
expanded.extend(expanded_item);
Item::MacroCall(m, args) => {
expanded.extend(expand_macro_call(m, args, macros));
}
item => {
expanded.push(item);
@ -67,33 +92,95 @@ fn expand_macros(body: Vec<Item>, macros: &HashMap<String, Vec<Item>>) -> Vec<It
expanded
}
fn assemble_file(body: Vec<Item>, code: &mut Vec<u8>, global_labels: &mut HashMap<String, usize>) {
// First discover the offset of each label in this file.
fn expand_macro_call(
name: String,
args: Vec<PushTarget>,
macros: &HashMap<String, Macro>,
) -> Vec<Item> {
let _macro = macros
.get(&name)
.unwrap_or_else(|| panic!("No such macro: {}", name));
assert_eq!(
args.len(),
_macro.params.len(),
"Macro `{}`: expected {} arguments, got {}",
name,
_macro.params.len(),
args.len()
);
let expanded_item = _macro
.items
.iter()
.map(|item| {
if let Item::Push(PushTarget::MacroVar(var)) = item {
let param_index = _macro.get_param_index(var);
Item::Push(args[param_index].clone())
} else {
item.clone()
}
})
.collect();
// Recursively expand any macros in the expanded code.
expand_macros(expanded_item, macros)
}
fn inline_constants(body: Vec<Item>, constants: &HashMap<String, U256>) -> Vec<Item> {
body.into_iter()
.map(|item| {
if let Item::Push(PushTarget::Constant(c)) = item {
let value = constants
.get(&c)
.unwrap_or_else(|| panic!("No such constant: {}", c));
let literal = Literal::Decimal(value.to_string());
Item::Push(PushTarget::Literal(literal))
} else {
item
}
})
.collect()
}
fn find_labels(
body: &[Item],
offset: &mut usize,
global_labels: &mut HashMap<String, usize>,
) -> HashMap<String, usize> {
// Discover the offset of each label in this file.
let mut local_labels = HashMap::<String, usize>::new();
let mut offset = code.len();
for item in &body {
for item in body {
match item {
Item::MacroDef(_, _) | Item::MacroCall(_) => {
Item::MacroDef(_, _, _) | Item::MacroCall(_, _) => {
panic!("Macros should have been expanded already")
}
Item::GlobalLabelDeclaration(label) => {
let old = global_labels.insert(label.clone(), offset);
let old = global_labels.insert(label.clone(), *offset);
assert!(old.is_none(), "Duplicate global label: {}", label);
}
Item::LocalLabelDeclaration(label) => {
let old = local_labels.insert(label.clone(), offset);
let old = local_labels.insert(label.clone(), *offset);
assert!(old.is_none(), "Duplicate local label: {}", label);
}
Item::Push(target) => offset += 1 + push_target_size(target) as usize,
Item::StandardOp(_) => offset += 1,
Item::Bytes(bytes) => offset += bytes.len(),
Item::Push(target) => *offset += 1 + push_target_size(target) as usize,
Item::StandardOp(_) => *offset += 1,
Item::Bytes(bytes) => *offset += bytes.len(),
}
}
local_labels
}
// Now that we have label offsets, we can assemble the file.
fn assemble_file(
body: Vec<Item>,
code: &mut Vec<u8>,
local_labels: HashMap<String, usize>,
global_labels: &HashMap<String, usize>,
) {
// Assemble the file.
for item in body {
match item {
Item::MacroDef(_, _) | Item::MacroCall(_) => {
Item::MacroDef(_, _, _) | Item::MacroCall(_, _) => {
panic!("Macros should have been expanded already")
}
Item::GlobalLabelDeclaration(_) | Item::LocalLabelDeclaration(_) => {
@ -114,6 +201,8 @@ fn assemble_file(body: Vec<Item>, code: &mut Vec<u8>, global_labels: &mut HashMa
.map(|i| offset.to_le_bytes()[i as usize])
.collect()
}
PushTarget::MacroVar(v) => panic!("Variable not in a macro: {}", v),
PushTarget::Constant(c) => panic!("Constant wasn't inlined: {}", c),
};
code.push(get_push_opcode(target_bytes.len() as u8));
code.extend(target_bytes);
@ -124,12 +213,6 @@ fn assemble_file(body: Vec<Item>, code: &mut Vec<u8>, global_labels: &mut HashMa
Item::Bytes(bytes) => code.extend(bytes.iter().map(|b| b.to_u8())),
}
}
assert_eq!(
code.len(),
offset,
"The two phases gave different code lengths"
);
}
/// The size of a `PushTarget`, in bytes.
@ -137,6 +220,8 @@ fn push_target_size(target: &PushTarget) -> u8 {
match target {
PushTarget::Literal(lit) => lit.to_trimmed_be_bytes().len() as u8,
PushTarget::Label(_) => BYTES_PER_OFFSET,
PushTarget::MacroVar(v) => panic!("Variable not in a macro: {}", v),
PushTarget::Constant(c) => panic!("Constant wasn't inlined: {}", c),
}
}
@ -202,7 +287,7 @@ mod tests {
};
let program = vec![file_1, file_2];
assert_eq!(assemble(program), expected_kernel);
assert_eq!(assemble(program, HashMap::new()), expected_kernel);
}
#[test]
@ -220,7 +305,7 @@ mod tests {
Item::StandardOp("JUMPDEST".to_string()),
],
};
assemble(vec![file_1, file_2]);
assemble(vec![file_1, file_2], HashMap::new());
}
#[test]
@ -234,7 +319,7 @@ mod tests {
Item::StandardOp("ADD".to_string()),
],
};
assemble(vec![file]);
assemble(vec![file], HashMap::new());
}
#[test]
@ -251,7 +336,7 @@ mod tests {
]),
],
};
let code = assemble(vec![file]).code;
let code = assemble(vec![file], HashMap::new()).code;
assert_eq!(code, vec![0x12, 42, 0xfe, 255]);
}
@ -266,8 +351,52 @@ mod tests {
assert_eq!(kernel.code, vec![add, add]);
}
#[test]
fn macro_with_vars() {
let kernel = parse_and_assemble(&[
"%macro add(x, y) PUSH $x PUSH $y ADD %endmacro",
"%add(2, 3)",
]);
let push1 = get_push_opcode(1);
let add = get_opcode("ADD");
assert_eq!(kernel.code, vec![push1, 2, push1, 3, add]);
}
#[test]
#[should_panic]
fn macro_with_wrong_vars() {
parse_and_assemble(&[
"%macro add(x, y) PUSH $x PUSH $y ADD %endmacro",
"%add(2, 3, 4)",
]);
}
#[test]
#[should_panic]
fn var_not_in_macro() {
parse_and_assemble(&["push $abc"]);
}
#[test]
fn constants() {
let code = &["PUSH @DEAD_BEEF"];
let mut constants = HashMap::new();
constants.insert("DEAD_BEEF".into(), 0xDEADBEEFu64.into());
let kernel = parse_and_assemble_with_constants(code, constants);
let push4 = get_push_opcode(4);
assert_eq!(kernel.code, vec![push4, 0xDE, 0xAD, 0xBE, 0xEF]);
}
fn parse_and_assemble(files: &[&str]) -> Kernel {
parse_and_assemble_with_constants(files, HashMap::new())
}
fn parse_and_assemble_with_constants(
files: &[&str],
constants: HashMap<String, U256>,
) -> Kernel {
let parsed_files = files.iter().map(|f| parse(f)).collect_vec();
assemble(parsed_files)
assemble(parsed_files, constants)
}
}

View File

@ -8,10 +8,10 @@ pub(crate) struct File {
#[derive(Clone, Debug)]
pub(crate) enum Item {
/// Defines a new macro.
MacroDef(String, Vec<Item>),
/// Calls a macro.
MacroCall(String),
/// Defines a new macro: name, params, body.
MacroDef(String, Vec<String>, Vec<Item>),
/// Calls a macro: name, args.
MacroCall(String, Vec<PushTarget>),
/// Declares a global label.
GlobalLabelDeclaration(String),
/// Declares a label that is local to the current file.
@ -29,6 +29,8 @@ pub(crate) enum Item {
pub(crate) enum PushTarget {
Literal(Literal),
Label(String),
MacroVar(String),
Constant(String),
}
#[derive(Clone, Debug)]

View File

@ -12,13 +12,19 @@ literal_decimal = @{ ASCII_DIGIT+ }
literal_hex = @{ ^"0x" ~ ASCII_HEX_DIGIT+ }
literal = { literal_hex | literal_decimal }
variable = ${ "$" ~ identifier }
constant = ${ "@" ~ identifier }
item = { macro_def | macro_call | global_label | local_label | bytes_item | push_instruction | nullary_instruction }
macro_def = { ^"%macro" ~ identifier ~ item* ~ ^"%endmacro" }
macro_call = ${ "%" ~ !(^"macro" | ^"endmacro") ~ identifier }
macro_def = { ^"%macro" ~ identifier ~ macro_paramlist? ~ item* ~ ^"%endmacro" }
macro_call = ${ "%" ~ !(^"macro" | ^"endmacro") ~ identifier ~ macro_arglist? }
macro_paramlist = { "(" ~ identifier ~ ("," ~ identifier)* ~ ")" }
macro_arglist = !{ "(" ~ push_target ~ ("," ~ push_target)* ~ ")" }
global_label = { ^"GLOBAL " ~ identifier ~ ":" }
local_label = { identifier ~ ":" }
bytes_item = { ^"BYTES " ~ literal ~ ("," ~ literal)* }
push_instruction = { ^"PUSH " ~ (literal | identifier) }
push_instruction = { ^"PUSH " ~ push_target }
push_target = { literal | identifier | variable | constant }
nullary_instruction = { identifier }
file = { SOI ~ item* ~ silent_eoi }

View File

@ -0,0 +1,286 @@
use ethereum_types::{U256, U512};
struct Interpreter<'a> {
code: &'a [u8],
offset: usize,
stack: Vec<U256>,
}
pub fn run(code: &[u8], initial_offset: usize, initial_stack: Vec<U256>) -> Vec<U256> {
let mut interpreter = Interpreter {
code,
offset: initial_offset,
stack: initial_stack,
};
// Halt the execution if a jump to 0xdeadbeef was done.
while interpreter.offset != 0xdeadbeef {
interpreter.run_opcode();
}
interpreter.stack
}
impl<'a> Interpreter<'a> {
fn slice(&self, n: usize) -> &[u8] {
&self.code[self.offset..self.offset + n]
}
fn incr(&mut self, n: usize) {
self.offset += n;
}
fn push(&mut self, x: U256) {
self.stack.push(x);
}
fn push_bool(&mut self, x: bool) {
self.stack.push(if x { U256::one() } else { U256::zero() });
}
fn pop(&mut self) -> U256 {
self.stack.pop().expect("Pop on empty stack.")
}
fn run_opcode(&mut self) {
let opcode = self.code[self.offset];
self.incr(1);
match opcode {
0x00 => todo!(), // "STOP",
0x01 => self.run_add(), // "ADD",
0x02 => self.run_mul(), // "MUL",
0x03 => self.run_sub(), // "SUB",
0x04 => self.run_div(), // "DIV",
0x05 => todo!(), // "SDIV",
0x06 => self.run_mod(), // "MOD",
0x07 => todo!(), // "SMOD",
0x08 => self.run_addmod(), // "ADDMOD",
0x09 => self.run_mulmod(), // "MULMOD",
0x0a => self.run_exp(), // "EXP",
0x0b => todo!(), // "SIGNEXTEND",
0x10 => self.run_lt(), // "LT",
0x11 => self.run_gt(), // "GT",
0x12 => todo!(), // "SLT",
0x13 => todo!(), // "SGT",
0x14 => self.run_eq(), // "EQ",
0x15 => self.run_iszero(), // "ISZERO",
0x16 => self.run_and(), // "AND",
0x17 => self.run_or(), // "OR",
0x18 => self.run_xor(), // "XOR",
0x19 => self.run_not(), // "NOT",
0x1a => todo!(), // "BYTE",
0x1b => todo!(), // "SHL",
0x1c => todo!(), // "SHR",
0x1d => todo!(), // "SAR",
0x20 => todo!(), // "KECCAK256",
0x30 => todo!(), // "ADDRESS",
0x31 => todo!(), // "BALANCE",
0x32 => todo!(), // "ORIGIN",
0x33 => todo!(), // "CALLER",
0x34 => todo!(), // "CALLVALUE",
0x35 => todo!(), // "CALLDATALOAD",
0x36 => todo!(), // "CALLDATASIZE",
0x37 => todo!(), // "CALLDATACOPY",
0x38 => todo!(), // "CODESIZE",
0x39 => todo!(), // "CODECOPY",
0x3a => todo!(), // "GASPRICE",
0x3b => todo!(), // "EXTCODESIZE",
0x3c => todo!(), // "EXTCODECOPY",
0x3d => todo!(), // "RETURNDATASIZE",
0x3e => todo!(), // "RETURNDATACOPY",
0x3f => todo!(), // "EXTCODEHASH",
0x40 => todo!(), // "BLOCKHASH",
0x41 => todo!(), // "COINBASE",
0x42 => todo!(), // "TIMESTAMP",
0x43 => todo!(), // "NUMBER",
0x44 => todo!(), // "DIFFICULTY",
0x45 => todo!(), // "GASLIMIT",
0x46 => todo!(), // "CHAINID",
0x48 => todo!(), // "BASEFEE",
0x50 => self.run_pop(), // "POP",
0x51 => todo!(), // "MLOAD",
0x52 => todo!(), // "MSTORE",
0x53 => todo!(), // "MSTORE8",
0x54 => todo!(), // "SLOAD",
0x55 => todo!(), // "SSTORE",
0x56 => self.run_jump(), // "JUMP",
0x57 => self.run_jumpi(), // "JUMPI",
0x58 => todo!(), // "GETPC",
0x59 => todo!(), // "MSIZE",
0x5a => todo!(), // "GAS",
0x5b => (), // "JUMPDEST",
x if (0x60..0x80).contains(&x) => self.run_push(x - 0x5f), // "PUSH"
x if (0x80..0x90).contains(&x) => self.run_dup(x - 0x7f), // "DUP"
x if (0x90..0xa0).contains(&x) => self.run_swap(x - 0x8f), // "SWAP"
0xa0 => todo!(), // "LOG0",
0xa1 => todo!(), // "LOG1",
0xa2 => todo!(), // "LOG2",
0xa3 => todo!(), // "LOG3",
0xa4 => todo!(), // "LOG4",
0xf0 => todo!(), // "CREATE",
0xf1 => todo!(), // "CALL",
0xf2 => todo!(), // "CALLCODE",
0xf3 => todo!(), // "RETURN",
0xf4 => todo!(), // "DELEGATECALL",
0xf5 => todo!(), // "CREATE2",
0xfa => todo!(), // "STATICCALL",
0xfd => todo!(), // "REVERT",
0xfe => todo!(), // "INVALID",
0xff => todo!(), // "SELFDESTRUCT",
_ => panic!("Unrecognized opcode {}.", opcode),
};
}
fn run_add(&mut self) {
let x = self.pop();
let y = self.pop();
self.push(x.overflowing_add(y).0);
}
fn run_mul(&mut self) {
let x = self.pop();
let y = self.pop();
self.push(x.overflowing_mul(y).0);
}
fn run_sub(&mut self) {
let x = self.pop();
let y = self.pop();
self.push(x.overflowing_sub(y).0);
}
fn run_div(&mut self) {
let x = self.pop();
let y = self.pop();
self.push(if y.is_zero() { U256::zero() } else { x / y });
}
fn run_mod(&mut self) {
let x = self.pop();
let y = self.pop();
self.push(if y.is_zero() { U256::zero() } else { x % y });
}
fn run_addmod(&mut self) {
let x = U512::from(self.pop());
let y = U512::from(self.pop());
let z = U512::from(self.pop());
self.push(if z.is_zero() {
U256::zero()
} else {
U256::try_from((x + y) % z).unwrap()
});
}
fn run_mulmod(&mut self) {
let x = self.pop();
let y = self.pop();
let z = U512::from(self.pop());
self.push(if z.is_zero() {
U256::zero()
} else {
U256::try_from(x.full_mul(y) % z).unwrap()
});
}
fn run_exp(&mut self) {
let x = self.pop();
let y = self.pop();
self.push(x.overflowing_pow(y).0);
}
fn run_lt(&mut self) {
let x = self.pop();
let y = self.pop();
self.push_bool(x < y);
}
fn run_gt(&mut self) {
let x = self.pop();
let y = self.pop();
self.push_bool(x > y);
}
fn run_eq(&mut self) {
let x = self.pop();
let y = self.pop();
self.push_bool(x == y);
}
fn run_iszero(&mut self) {
let x = self.pop();
self.push_bool(x.is_zero());
}
fn run_and(&mut self) {
let x = self.pop();
let y = self.pop();
self.push(x & y);
}
fn run_or(&mut self) {
let x = self.pop();
let y = self.pop();
self.push(x | y);
}
fn run_xor(&mut self) {
let x = self.pop();
let y = self.pop();
self.push(x ^ y);
}
fn run_not(&mut self) {
let x = self.pop();
self.push(!x);
}
fn run_pop(&mut self) {
self.pop();
}
fn run_jump(&mut self) {
let x = self.pop().as_usize();
self.offset = x;
if let Some(&landing_opcode) = self.code.get(self.offset) {
assert_eq!(landing_opcode, 0x5b, "Destination is not a JUMPDEST.");
}
}
fn run_jumpi(&mut self) {
let x = self.pop().as_usize();
let b = self.pop();
if !b.is_zero() {
self.offset = x;
if let Some(&landing_opcode) = self.code.get(self.offset) {
assert_eq!(landing_opcode, 0x5b, "Destination is not a JUMPDEST.");
}
}
}
fn run_push(&mut self, num_bytes: u8) {
let x = U256::from_big_endian(self.slice(num_bytes as usize));
self.incr(num_bytes as usize);
self.push(x);
}
fn run_dup(&mut self, n: u8) {
self.push(self.stack[self.stack.len() - n as usize]);
}
fn run_swap(&mut self, n: u8) {
let len = self.stack.len();
self.stack.swap(len - 1, len - n as usize - 1);
}
}
#[cfg(test)]
mod tests {
use crate::cpu::kernel::interpreter::run;
#[test]
fn test_run() {
let code = vec![
0x60, 0x1, 0x60, 0x2, 0x1, 0x63, 0xde, 0xad, 0xbe, 0xef, 0x56,
]; // PUSH1, 1, PUSH1, 2, ADD, PUSH4 deadbeef, JUMP
assert_eq!(run(&code, 0, vec![]), vec![0x3.into()]);
}
}

View File

@ -4,13 +4,18 @@ mod ast;
mod opcodes;
mod parser;
#[cfg(test)]
mod interpreter;
use assembler::assemble;
use parser::parse;
use crate::cpu::kernel::aggregator::evm_constants;
/// Assemble files, outputting bytes.
/// This is for debugging the kernel only.
pub fn assemble_to_bytes(files: &[String]) -> Vec<u8> {
let parsed_files: Vec<_> = files.iter().map(|f| parse(f)).collect();
let kernel = assemble(parsed_files);
let kernel = assemble(parsed_files, evm_constants());
kernel.code
}

View File

@ -18,14 +18,11 @@ pub(crate) fn parse(s: &str) -> File {
}
fn parse_item(item: Pair<Rule>) -> Item {
assert_eq!(item.as_rule(), Rule::item);
let item = item.into_inner().next().unwrap();
match item.as_rule() {
Rule::macro_def => {
let mut inner = item.into_inner();
let name = inner.next().unwrap().as_str().into();
Item::MacroDef(name, inner.map(parse_item).collect())
}
Rule::macro_call => Item::MacroCall(item.into_inner().next().unwrap().as_str().into()),
Rule::macro_def => parse_macro_def(item),
Rule::macro_call => parse_macro_call(item),
Rule::global_label => {
Item::GlobalLabelDeclaration(item.into_inner().next().unwrap().as_str().into())
}
@ -39,11 +36,49 @@ fn parse_item(item: Pair<Rule>) -> Item {
}
}
fn parse_macro_def(item: Pair<Rule>) -> Item {
assert_eq!(item.as_rule(), Rule::macro_def);
let mut inner = item.into_inner().peekable();
let name = inner.next().unwrap().as_str().into();
// The parameter list is optional.
let params = if let Some(Rule::macro_paramlist) = inner.peek().map(|pair| pair.as_rule()) {
let params = inner.next().unwrap().into_inner();
params.map(|param| param.as_str().to_string()).collect()
} else {
vec![]
};
Item::MacroDef(name, params, inner.map(parse_item).collect())
}
fn parse_macro_call(item: Pair<Rule>) -> Item {
assert_eq!(item.as_rule(), Rule::macro_call);
let mut inner = item.into_inner();
let name = inner.next().unwrap().as_str().into();
// The arg list is optional.
let args = if let Some(arglist) = inner.next() {
assert_eq!(arglist.as_rule(), Rule::macro_arglist);
arglist.into_inner().map(parse_push_target).collect()
} else {
vec![]
};
Item::MacroCall(name, args)
}
fn parse_push_target(target: Pair<Rule>) -> PushTarget {
match target.as_rule() {
Rule::identifier => PushTarget::Label(target.as_str().into()),
Rule::literal => PushTarget::Literal(parse_literal(target)),
_ => panic!("Unexpected {:?}", target.as_rule()),
assert_eq!(target.as_rule(), Rule::push_target);
let inner = target.into_inner().next().unwrap();
match inner.as_rule() {
Rule::literal => PushTarget::Literal(parse_literal(inner)),
Rule::identifier => PushTarget::Label(inner.as_str().into()),
Rule::variable => PushTarget::MacroVar(inner.into_inner().next().unwrap().as_str().into()),
Rule::constant => PushTarget::Constant(inner.into_inner().next().unwrap().as_str().into()),
_ => panic!("Unexpected {:?}", inner.as_rule()),
}
}