better test API

This commit is contained in:
Dmitry Vagner 2022-10-17 11:02:24 -04:00
parent adc8c33b0d
commit 3cff0928c4
7 changed files with 52 additions and 31 deletions

View File

@ -33,7 +33,9 @@ pub(crate) fn combined_kernel() -> Kernel {
include_str!("asm/curve/secp256k1/moddiv.asm"),
include_str!("asm/exp.asm"),
include_str!("asm/halt.asm"),
include_str!("asm/fields/fp6.asm"),
include_str!("asm/fields/fp6mul.asm"),
include_str!("asm/fields/fp12.asm"),
include_str!("asm/main.asm"),
include_str!("asm/memory/core.asm"),
include_str!("asm/memory/memcpy.asm"),

View File

@ -8,12 +8,24 @@
///
/// Note: each symbol in the stack comments takes up six words
global test_mul_Fp12:
// stack: f, f', g, g'
%store_fp6(0)
%store_fp6(6)
%store_fp6(12)
%store_fp6(18)
PUSH return_on_stack
// stack: return_on_stack
%jump(mul_Fp12)
return_on_stack:
// stack:
%load_fp6(24)
%load_fp6(30)
// stack: h, h'
%jump(0xdeadbeef)
global mul_Fp12:
pre_mul:
calc:
%load_fp6(6)
%load_fp6(18)
%dup2_fp6
@ -48,7 +60,7 @@ calc:
%mul_fp6
// stack: (f+f')(g+g')
%load_fp6(42)
%bus_fp6(42)
%bus_fp6
// stack: (f+f')(g+g') - f'g'
%load_fp6(48)
%swap_fp6
@ -63,5 +75,4 @@ calc:
%add_fp6
// stack: sh(f'g') + fg
%store_fp6(24)
%jump(0xdeadbeef)
JUMP

View File

@ -139,26 +139,26 @@
%macro add_fp6
// stack: f0, f1, f2, f3, f4, f5, g0, g1, g2, g3, g4, g5
SWAP7
ADD
ADDFP254
SWAP6
// stack: f0, f2, f3, f4, f5, g0, h1, g2, g3, g4, g5
SWAP7
ADD
ADDFP254
SWAP6
// stack: f0, f3, f4, f5, g0, h1, h2, g3, g4, g5
SWAP7
ADD
ADDFP254
SWAP6
// stack: f0, f4, f5, g0, h1, h2, h3, g4, g5
SWAP7
ADD
ADDFP254
SWAP6
// stack: f0, f5, g0, h1, h2, h3, h4, g5
SWAP7
ADD
ADDFP254
SWAP6
// stack: f0, g0, h1, h2, h3, h4, h5
ADD
ADDFP254
// stack: h0, h1, h2, h3, h4, h5
%endmacro
@ -166,26 +166,26 @@
%macro bus_fp6
// stack: f0, f1, f2, f3, f4, f5, g0, g1, g2, g3, g4, g5
SWAP7
SUB
SUBFP254
SWAP6
// stack: f0, f2, f3, f4, f5, g0, h1, g2, g3, g4, g5
SWAP7
SUB
SUBFP254
SWAP6
// stack: f0, f3, f4, f5, g0, h1, h2, g3, g4, g5
SWAP7
SUB
SUBFP254
SWAP6
// stack: f0, f4, f5, g0, h1, h2, h3, g4, g5
SWAP7
SUB
SUBFP254
SWAP6
// stack: f0, f5, g0, h1, h2, h3, h4, g5
SWAP7
SUB
SUBFP254
SWAP6
// stack: f0, g0, h1, h2, h3, h4, h5
SUB
SUBFP254
// stack: h0, h1, h2, h3, h4, h5
%endmacro
@ -196,11 +196,11 @@
DUP2
// stack: a , b, a , b
%mul_const(9)
SUB
SUBFP254
// stack: 9a - b, a , b
SWAP2
// stack: b , a, 9a - b
%mul_const(9)
ADD
ADDFP254
// stack: 9b + a, 9a - b
%endmacro

View File

@ -1,4 +1,4 @@
macro mul_Fp6
%macro mul_fp6
// stack: c0, c0_, c1, c1_, c2, c2_, d0, d0_, d1, d1_, d2, d2_
SWAP3
// stack: c1_, c0_, c1, c0, c2, c2_, d0, d0_, d1, d1_, d2, d2_

View File

@ -370,7 +370,7 @@ impl<'a> Interpreter<'a> {
let y = self.pop();
self.push(U256::try_from(x.full_mul(y) % 101).unwrap());
}
fn run_sub(&mut self) {
let x = self.pop();
let y = self.pop();

View File

@ -4,19 +4,27 @@ use ethereum_types::U256;
use crate::cpu::kernel::aggregator::combined_kernel;
use crate::cpu::kernel::interpreter::run_with_kernel;
#[test]
fn test_field() -> Result<()> {
let kernel = combined_kernel();
let initial_offset = kernel.global_labels["mul_Fp12"];
let initial_stack: Vec<U256> = vec![1, 1, 0, 0, 1, 0, 3, 0, 0, 1, 0, 0].iter().map(|&x| U256::from(x as u32)).rev().collect();
let initial_offset = kernel.global_labels["test_mul_Fp12"];
let initial_stack: Vec<U256> = vec![
1, 1, 0, 0, 1, 0, 3, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 3, 0, 0, 1, 0, 0,
]
.iter()
.map(|&x| U256::from(x as u32))
.rev()
.collect();
let final_stack: Vec<U256> = run_with_kernel(&kernel, initial_offset, initial_stack)?
.stack()
.to_vec();
let expected: Vec<U256> = vec![5, 5, 9, 0, 5, 3, 17, 12, 100, 1, 3, 0]
.iter()
.map(|&x| U256::from(x as u32))
.rev()
.collect();
let expected: Vec<U256> = vec![2, 12, 100, 1, 3, 0].iter().map(|&x| U256::from(x as u32)).rev().collect();
assert_eq!(final_stack, expected);
Ok(())
}

View File

@ -2,8 +2,8 @@ mod core;
mod curve_ops;
mod ecrecover;
mod exp;
mod hash;
mod fields;
mod hash;
mod mpt;
mod packing;
mod ripemd;