tests pass

This commit is contained in:
Dmitry Vagner 2023-03-22 09:55:35 -07:00
parent 84a0bcf893
commit 1627a9a0d3
5 changed files with 53 additions and 3 deletions

View File

@ -35,6 +35,7 @@ pub(crate) fn combined_kernel() -> Kernel {
include_str!("asm/core/terminate.asm"),
include_str!("asm/core/transfer.asm"),
include_str!("asm/core/util.asm"),
include_str!("asm/curve/bls381/util.asm"),
include_str!("asm/curve/bn254/curve_arithmetic/constants.asm"),
include_str!("asm/curve/bn254/curve_arithmetic/curve_add.asm"),
include_str!("asm/curve/bn254/curve_arithmetic/curve_mul.asm"),

View File

@ -45,9 +45,9 @@ global test_add_fp381:
%jump(0xdeadbeef)
global test_mul_fp381:
%add_fp381
%mul_fp381
%jump(0xdeadbeef)
global test_sub_fp381:
%add_fp381
%sub_fp381
%jump(0xdeadbeef)

View File

@ -0,0 +1,48 @@
use anyhow::Result;
use ethereum_types::{U256, U512};
use rand::Rng;
use crate::cpu::kernel::interpreter::{
run_interpreter_with_memory, Interpreter, InterpreterMemoryInitialization,
};
use crate::extension_tower::{Stack, BLS381};
use crate::memory::segments::Segment::KernelGeneral;
fn extract_stack(interpreter: Interpreter<'static>) -> Vec<U256> {
interpreter
.stack()
.iter()
.rev()
.cloned()
.collect::<Vec<U256>>()
}
fn combine_u256s(hi: U256, lo: U256) -> U512 {
U512::from(lo) + (U512::from(hi) << 256)
}
#[test]
fn test_bls_ops() -> Result<()> {
let mut rng = rand::thread_rng();
let x: BLS381 = rng.gen::<BLS381>();
let y: BLS381 = rng.gen::<BLS381>();
let mut stack = x.on_stack();
stack.extend(y.on_stack());
let setup = InterpreterMemoryInitialization {
label: "test_mul_fp381".to_string(),
stack,
segment: KernelGeneral,
memory: vec![],
};
let interpreter = run_interpreter_with_memory(setup).unwrap();
let output = extract_stack(interpreter);
println!("{:#?}", output);
let output_512 = combine_u256s(output[1], output[0]);
let expected = x * y;
assert_eq!(expected.val, output_512);
Ok(())
}

View File

@ -1,6 +1,7 @@
mod account_code;
mod balance;
mod bignum;
mod bls381;
mod bn254;
mod core;
mod ecc;

View File

@ -61,7 +61,7 @@ impl<F: Field> GenerationState<F> {
fn run_sf(&self, input_fn: &ProverInputFn) -> U256 {
let field = EvmField::from_str(input_fn.0[1].as_str()).unwrap();
let inputs: [U256; 4] = match field {
Bn254Base => {
Bls381Base => {
let mut inputs: [U256; 4] = [U256::zero(); 4];
for i in 0..4 {
inputs[i] = stack_peek(self, i).expect("Empty stack");