73 lines
2.2 KiB
Rust
Raw Normal View History

2023-03-22 09:55:35 -07:00
use anyhow::Result;
2023-03-28 11:35:01 -07:00
use ethereum_types::U256;
2023-03-22 09:55:35 -07:00
use rand::Rng;
use crate::cpu::kernel::interpreter::{
2023-03-22 11:56:58 -07:00
run_interpreter_with_memory, InterpreterMemoryInitialization,
2023-03-22 09:55:35 -07:00
};
2023-03-28 11:12:59 -07:00
use crate::extension_tower::{Fp2, Stack, BLS381};
2023-03-22 09:55:35 -07:00
use crate::memory::segments::Segment::KernelGeneral;
2023-03-22 10:16:02 -07:00
fn run_and_return_bls(label: String, x: BLS381, y: BLS381) -> BLS381 {
2023-03-28 11:12:59 -07:00
let mut stack = x.to_stack();
stack.extend(y.to_stack());
2023-03-22 09:55:35 -07:00
let setup = InterpreterMemoryInitialization {
2023-03-22 10:16:02 -07:00
label,
2023-03-22 09:55:35 -07:00
stack,
segment: KernelGeneral,
memory: vec![],
};
let interpreter = run_interpreter_with_memory(setup).unwrap();
2023-03-22 10:16:02 -07:00
let output = interpreter.stack();
2023-03-28 11:12:59 -07:00
BLS381::from_stack(output)
2023-03-22 10:16:02 -07:00
}
#[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_and_return_bls("test_add_fp381".to_string(), x, y);
let output_mul = run_and_return_bls("test_mul_fp381".to_string(), x, y);
let output_sub = run_and_return_bls("test_sub_fp381".to_string(), x, y);
2023-03-22 09:55:35 -07:00
2023-03-22 10:16:02 -07:00
assert_eq!(output_add, x + y);
assert_eq!(output_mul, x * y);
assert_eq!(output_sub, x - y);
2023-03-22 09:55:35 -07:00
Ok(())
}
2023-03-28 11:12:59 -07:00
fn run_and_return_bls_fp2(label: String, x: Fp2<BLS381>, y: Fp2<BLS381>) -> Fp2<BLS381> {
let mut stack = x.to_stack();
stack.extend(y.to_stack());
2023-03-28 11:35:01 -07:00
stack.push(U256::from(0xdeadbeefu32));
2023-03-28 11:12:59 -07:00
let setup = InterpreterMemoryInitialization {
label,
stack,
segment: KernelGeneral,
memory: vec![],
};
let interpreter = run_interpreter_with_memory(setup).unwrap();
let output = interpreter.stack();
Fp2::from_stack(output)
}
#[test]
fn test_bls_fp2() -> 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_and_return_bls_fp2("add_fp381_2".to_string(), x, y);
let output_mul = run_and_return_bls_fp2("mul_fp381_2".to_string(), x, y);
let output_sub = run_and_return_bls_fp2("sub_fp381_2".to_string(), x, y);
assert_eq!(output_add, x + y);
assert_eq!(output_mul, x * y);
assert_eq!(output_sub, x - y);
Ok(())
}