getting there

This commit is contained in:
Dmitry Vagner 2023-04-14 09:49:22 -07:00
parent 0b9ef768d4
commit 0b85c8bbe0
3 changed files with 20 additions and 82 deletions

View File

@ -40,19 +40,6 @@
// stack: z0, z1
%endmacro
global test_add_fp381:
%add_fp381
%jump(0xdeadbeef)
global test_sub_fp381:
%sub_fp381
%jump(0xdeadbeef)
global test_mul_fp381:
%mul_fp381
%jump(0xdeadbeef)
%macro add_fp381_2
// stack: x_re, x_im, y_re, y_im
%stack (x_re: 2, x_im: 2, y_re: 2, y_im: 2) -> (y_im, x_im, y_re, x_re)
@ -77,15 +64,6 @@ global test_mul_fp381:
// stack: z_re, z_im
%endmacro
global test_add_fp381_2:
%add_fp381_2
%jump(0xdeadbeef)
global test_sub_fp381_2:
%sub_fp381_2
%jump(0xdeadbeef)
global mul_fp381_2:
// stack: x_re, x_im, y_re, y_im, jumpdest
DUP4

View File

@ -8,65 +8,25 @@ use crate::cpu::kernel::interpreter::{
use crate::extension_tower::{Fp2, Stack, BLS381};
use crate::memory::segments::Segment::KernelGeneral;
fn run_bls_ops(label: &str, x: BLS381, y: BLS381) -> BLS381 {
let mut stack = x.to_stack();
stack.extend(y.to_stack());
let setup = InterpreterMemoryInitialization {
label: label.to_string(),
stack,
segment: KernelGeneral,
memory: vec![],
};
let interpreter = run_interpreter_with_memory(setup).unwrap();
let output: Vec<U256> = interpreter.stack().iter().rev().cloned().collect();
BLS381::from_stack(&output)
}
#[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 output_add = run_bls_ops("test_add_fp381", x, y);
let output_sub = run_bls_ops("test_sub_fp381", x, y);
let output_mul = run_bls_ops("test_mul_fp381", x, y);
assert_eq!(output_add, x + y);
assert_eq!(output_sub, x - y);
assert_eq!(output_mul, x * y);
Ok(())
}
fn run_bls_fp2_ops(label: &str, x: Fp2<BLS381>, y: Fp2<BLS381>) -> Fp2<BLS381> {
let mut stack = x.to_stack();
stack.extend(y.to_stack());
stack.push(U256::from(0xdeadbeefu32));
let setup = InterpreterMemoryInitialization {
label: label.to_string(),
stack,
segment: KernelGeneral,
memory: vec![],
};
let interpreter = run_interpreter_with_memory(setup).unwrap();
let output: Vec<U256> = interpreter.stack().iter().rev().cloned().collect();
Fp2::<BLS381>::from_stack(&output)
}
#[test]
fn test_bls_fp2_ops() -> Result<()> {
fn test_bls_fp2_mul() -> Result<()> {
let mut rng = rand::thread_rng();
let x: Fp2<BLS381> = rng.gen::<Fp2<BLS381>>();
let y: Fp2<BLS381> = rng.gen::<Fp2<BLS381>>();
let output_add = run_bls_fp2_ops("test_add_fp381_2", x, y);
let output_sub = run_bls_fp2_ops("test_sub_fp381_2", x, y);
let output_mul = run_bls_fp2_ops("mul_fp381_2", x, y);
assert_eq!(output_add, x + y);
assert_eq!(output_sub, x - y);
assert_eq!(output_mul, x * y);
let mut stack = x.to_stack();
stack.extend(y.to_stack());
stack.push(U256::from(0xdeadbeefu32));
let setup = InterpreterMemoryInitialization {
label: "mul_fp381_2".to_string(),
stack,
segment: KernelGeneral,
memory: vec![],
};
let interpreter = run_interpreter_with_memory(setup).unwrap();
let stack: Vec<U256> = interpreter.stack().iter().rev().cloned().collect();
let output = Fp2::<BLS381>::from_stack(&stack);
assert_eq!(output, x * y);
Ok(())
}

View File

@ -1207,7 +1207,7 @@ pub trait Stack {
}
impl Stack for Fp6<BN254> {
fn to_stack(self) -> Vec<U256> {
fn to_stack(&self) -> Vec<U256> {
let f: [U256; 6] = unsafe { transmute(self) };
f.into_iter().collect()
}
@ -1220,7 +1220,7 @@ impl Stack for Fp6<BN254> {
}
impl Stack for Fp12<BN254> {
fn to_stack(self) -> Vec<U256> {
fn to_stack(&self) -> Vec<U256> {
let f: [U256; 12] = unsafe { transmute(self) };
f.into_iter().collect()
}
@ -1233,7 +1233,7 @@ impl Stack for Fp12<BN254> {
}
impl Stack for BLS381 {
fn to_stack(self) -> Vec<U256> {
fn to_stack(&self) -> Vec<U256> {
vec![self.lo(), self.hi()]
}
@ -1246,7 +1246,7 @@ impl Stack for BLS381 {
}
impl Stack for Fp2<BLS381> {
fn to_stack(self) -> Vec<U256> {
fn to_stack(&self) -> Vec<U256> {
let mut res = self.re.to_stack();
res.extend(self.im.to_stack());
res
@ -1260,7 +1260,7 @@ impl Stack for Fp2<BLS381> {
}
impl Stack for Fp6<BLS381> {
fn to_stack(self) -> Vec<U256> {
fn to_stack(&self) -> Vec<U256> {
let mut res = self.t0.to_stack();
res.extend(self.t1.to_stack());
res.extend(self.t2.to_stack());
@ -1276,7 +1276,7 @@ impl Stack for Fp6<BLS381> {
}
impl Stack for Fp12<BLS381> {
fn to_stack(self) -> Vec<U256> {
fn to_stack(&self) -> Vec<U256> {
let mut res = self.z0.to_stack();
res.extend(self.z1.to_stack());
res