2022-06-20 20:32:29 -07:00
|
|
|
//! Loads each kernel assembly file and concatenates them.
|
|
|
|
|
|
2022-07-08 08:56:46 -07:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
|
|
use ethereum_types::U256;
|
2022-07-20 00:10:52 -07:00
|
|
|
use hex_literal::hex;
|
2022-06-20 20:32:29 -07:00
|
|
|
use itertools::Itertools;
|
2022-07-14 11:31:47 -07:00
|
|
|
use once_cell::sync::Lazy;
|
2022-06-20 20:32:29 -07:00
|
|
|
|
|
|
|
|
use super::assembler::{assemble, Kernel};
|
|
|
|
|
use crate::cpu::kernel::parser::parse;
|
2022-07-06 19:47:58 -07:00
|
|
|
use crate::cpu::kernel::txn_fields::NormalizedTxnField;
|
2022-07-16 09:59:23 -07:00
|
|
|
use crate::memory::segments::Segment;
|
2022-06-20 20:32:29 -07:00
|
|
|
|
2022-07-14 11:31:47 -07:00
|
|
|
pub static KERNEL: Lazy<Kernel> = Lazy::new(combined_kernel);
|
2022-06-20 20:32:29 -07:00
|
|
|
|
2022-07-27 16:49:26 +02:00
|
|
|
const EC_CONSTANTS: [(&str, [u8; 32]); 3] = [
|
|
|
|
|
(
|
|
|
|
|
"BN_BASE",
|
|
|
|
|
hex!("30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47"),
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
"SECP_BASE",
|
|
|
|
|
hex!("fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"),
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
"SECP_SCALAR",
|
|
|
|
|
hex!("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"),
|
|
|
|
|
),
|
|
|
|
|
];
|
|
|
|
|
|
2022-07-08 08:56:46 -07:00
|
|
|
pub fn evm_constants() -> HashMap<String, U256> {
|
|
|
|
|
let mut c = HashMap::new();
|
2022-07-27 16:49:26 +02:00
|
|
|
for (name, value) in EC_CONSTANTS {
|
|
|
|
|
c.insert(name.into(), U256::from_big_endian(&value));
|
|
|
|
|
}
|
2022-07-16 09:59:23 -07:00
|
|
|
for segment in Segment::all() {
|
|
|
|
|
c.insert(segment.var_name().into(), (segment as u32).into());
|
|
|
|
|
}
|
2022-07-06 19:47:58 -07:00
|
|
|
for txn_field in NormalizedTxnField::all() {
|
|
|
|
|
c.insert(txn_field.var_name().into(), (txn_field as u32).into());
|
|
|
|
|
}
|
2022-07-08 08:56:46 -07:00
|
|
|
c
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-20 20:32:29 -07:00
|
|
|
#[allow(dead_code)] // TODO: Should be used once witness generation is done.
|
|
|
|
|
pub(crate) fn combined_kernel() -> Kernel {
|
|
|
|
|
let files = vec![
|
2022-07-18 13:48:51 -07:00
|
|
|
include_str!("asm/assertions.asm"),
|
2022-06-25 23:10:08 -07:00
|
|
|
include_str!("asm/basic_macros.asm"),
|
2022-07-30 22:24:11 -07:00
|
|
|
include_str!("asm/curve/bn254/curve_mul.asm"),
|
|
|
|
|
include_str!("asm/curve/bn254/curve_add.asm"),
|
|
|
|
|
include_str!("asm/curve/bn254/moddiv.asm"),
|
|
|
|
|
include_str!("asm/curve/common.asm"),
|
|
|
|
|
include_str!("asm/curve/secp256k1/curve_mul.asm"),
|
|
|
|
|
include_str!("asm/curve/secp256k1/curve_add.asm"),
|
|
|
|
|
include_str!("asm/curve/secp256k1/moddiv.asm"),
|
|
|
|
|
include_str!("asm/curve/secp256k1/lift_x.asm"),
|
|
|
|
|
include_str!("asm/curve/secp256k1/inverse_scalar.asm"),
|
2022-07-05 21:24:51 +02:00
|
|
|
include_str!("asm/exp.asm"),
|
2022-07-28 04:36:33 +10:00
|
|
|
include_str!("asm/halt.asm"),
|
2022-07-18 15:58:12 -07:00
|
|
|
include_str!("asm/memory.asm"),
|
2022-07-13 18:48:25 +02:00
|
|
|
include_str!("asm/ecrecover.asm"),
|
2022-07-06 19:47:58 -07:00
|
|
|
include_str!("asm/rlp/encode.asm"),
|
|
|
|
|
include_str!("asm/rlp/decode.asm"),
|
|
|
|
|
include_str!("asm/rlp/read_to_memory.asm"),
|
2022-07-24 08:42:06 -07:00
|
|
|
include_str!("asm/storage/read.asm"),
|
|
|
|
|
include_str!("asm/storage/write.asm"),
|
2022-07-06 19:47:58 -07:00
|
|
|
include_str!("asm/transactions/process_normalized.asm"),
|
|
|
|
|
include_str!("asm/transactions/router.asm"),
|
|
|
|
|
include_str!("asm/transactions/type_0.asm"),
|
|
|
|
|
include_str!("asm/transactions/type_1.asm"),
|
|
|
|
|
include_str!("asm/transactions/type_2.asm"),
|
2022-06-20 20:32:29 -07:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
let parsed_files = files.iter().map(|f| parse(f)).collect_vec();
|
2022-07-08 08:56:46 -07:00
|
|
|
assemble(parsed_files, evm_constants())
|
2022-06-20 20:32:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
2022-07-13 09:53:44 -07:00
|
|
|
use log::debug;
|
2022-07-07 18:06:24 +02:00
|
|
|
|
2022-06-20 20:32:29 -07:00
|
|
|
use crate::cpu::kernel::aggregator::combined_kernel;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn make_kernel() {
|
2022-07-30 22:31:07 -07:00
|
|
|
env_logger::init_from_env(
|
|
|
|
|
env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "debug"),
|
|
|
|
|
);
|
2022-07-13 09:53:44 -07:00
|
|
|
|
2022-06-20 20:32:29 -07:00
|
|
|
// Make sure we can parse and assemble the entire kernel.
|
2022-07-07 08:59:53 -07:00
|
|
|
let kernel = combined_kernel();
|
2022-07-30 22:31:07 -07:00
|
|
|
debug!("Total kernel size: {} bytes", kernel.code.len());
|
2022-06-20 20:32:29 -07:00
|
|
|
}
|
|
|
|
|
}
|