rev stack

This commit is contained in:
Dmitry Vagner 2023-04-03 17:49:13 -07:00
parent d52c15e879
commit 26da6dc7da
3 changed files with 43 additions and 67 deletions

View File

@ -8,18 +8,18 @@ use crate::cpu::kernel::interpreter::{
use crate::extension_tower::{Fp2, Stack, BLS381};
use crate::memory::segments::Segment::KernelGeneral;
fn run_and_return_bls(label: String, x: BLS381, y: BLS381) -> BLS381 {
fn run_and_return_bls(label: &str, x: BLS381, y: BLS381) -> BLS381 {
let mut stack = x.to_stack();
stack.extend(y.to_stack());
let setup = InterpreterMemoryInitialization {
label,
label: label.to_string(),
stack,
segment: KernelGeneral,
memory: vec![],
};
let interpreter = run_interpreter_with_memory(setup).unwrap();
let output = interpreter.stack();
BLS381::from_stack(output)
let output: Vec<U256> = interpreter.stack().iter().rev().cloned().collect();
BLS381::from_stack(&output)
}
#[test]
@ -28,9 +28,9 @@ fn test_bls_ops() -> Result<()> {
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);
let output_add = run_and_return_bls("test_add_fp381", x, y);
let output_mul = run_and_return_bls("test_mul_fp381", x, y);
let output_sub = run_and_return_bls("test_sub_fp381", x, y);
assert_eq!(output_add, x + y);
assert_eq!(output_mul, x * y);
@ -39,19 +39,19 @@ fn test_bls_ops() -> Result<()> {
Ok(())
}
fn run_and_return_bls_fp2(label: String, x: Fp2<BLS381>, y: Fp2<BLS381>) -> Fp2<BLS381> {
fn run_and_return_bls_fp2(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: label.to_string(),
stack,
segment: KernelGeneral,
memory: vec![],
};
let interpreter = run_interpreter_with_memory(setup).unwrap();
let output = interpreter.stack();
Fp2::from_stack(output)
let output: Vec<U256> = interpreter.stack().iter().rev().cloned().collect();
Fp2::<BLS381>::from_stack(&output)
}
#[test]
@ -60,9 +60,9 @@ fn test_bls_fp2() -> Result<()> {
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);
let output_add = run_and_return_bls_fp2("add_fp381_2", x, y);
let output_mul = run_and_return_bls_fp2("mul_fp381_2", x, y);
let output_sub = run_and_return_bls_fp2("sub_fp381_2", x, y);
assert_eq!(output_add, x + y);
assert_eq!(output_mul, x * y);

View File

@ -20,22 +20,21 @@ fn extract_stack(interpreter: Interpreter<'static>) -> Vec<U256> {
.collect::<Vec<U256>>()
}
fn setup_mul_fp6_test(
f: Fp6<BN254>,
g: Fp6<BN254>,
label: &str,
) -> InterpreterMemoryInitialization {
fn run_and_return_bn_fp6(f: Fp6<BN254>, g: Fp6<BN254>, label: &str) -> Fp6<BN254> {
let mut stack = f.to_stack();
if label == "mul_fp254_6" {
stack.extend(g.to_stack());
}
stack.push(U256::from(0xdeadbeefu32));
InterpreterMemoryInitialization {
let setup = InterpreterMemoryInitialization {
label: label.to_string(),
stack,
segment: BnPairing,
memory: vec![],
}
};
let interpreter = run_interpreter_with_memory(setup).unwrap();
let output: Vec<U256> = interpreter.stack().iter().rev().cloned().collect();
Fp6::<BN254>::from_stack(&output)
}
#[test]
@ -44,32 +43,23 @@ fn test_mul_fp6() -> Result<()> {
let f: Fp6<BN254> = rng.gen::<Fp6<BN254>>();
let g: Fp6<BN254> = rng.gen::<Fp6<BN254>>();
let setup_normal: InterpreterMemoryInitialization = setup_mul_fp6_test(f, g, "mul_fp254_6");
let setup_square: InterpreterMemoryInitialization = setup_mul_fp6_test(f, f, "square_fp254_6");
let output_mul: Fp6<BN254> = run_and_return_bn_fp6(f, g, "mul_fp254_6");
let output_square: Fp6<BN254> = run_and_return_bn_fp6(f, f, "square_fp254_6");
let intrptr_normal: Interpreter = run_interpreter_with_memory(setup_normal).unwrap();
let intrptr_square: Interpreter = run_interpreter_with_memory(setup_square).unwrap();
let out_normal: Vec<U256> = extract_stack(intrptr_normal);
let out_square: Vec<U256> = extract_stack(intrptr_square);
let exp_normal: Vec<U256> = (f * g).to_stack();
let exp_square: Vec<U256> = (f * f).to_stack();
assert_eq!(out_normal, exp_normal);
assert_eq!(out_square, exp_square);
assert_eq!(output_mul, f * g);
assert_eq!(output_square, f * f);
Ok(())
}
fn setup_mul_fp12_test(
out: usize,
fn run_and_return_bn_fp12(
f: Fp12<BN254>,
g: Fp12<BN254>,
label: &str,
) -> InterpreterMemoryInitialization {
) -> Fp12<BN254> {
let in0: usize = 200;
let in1: usize = 212;
let out: usize = 224;
let mut stack = vec![
U256::from(in0),
@ -80,45 +70,31 @@ fn setup_mul_fp12_test(
if label == "square_fp254_12" {
stack.remove(0);
}
InterpreterMemoryInitialization {
let setup = InterpreterMemoryInitialization {
label: label.to_string(),
stack,
segment: BnPairing,
memory: vec![(in0, f.to_stack()), (in1, g.to_stack())],
}
};
let interpreter = run_interpreter_with_memory(setup).unwrap();
let output = interpreter.extract_kernel_memory(BnPairing, out..out + 12);
Fp12::<BN254>::from_stack(&output)
}
#[test]
fn test_mul_fp12() -> Result<()> {
let out: usize = 224;
let mut rng = rand::thread_rng();
let f: Fp12<BN254> = rng.gen::<Fp12<BN254>>();
let g: Fp12<BN254> = rng.gen::<Fp12<BN254>>();
let h: Fp12<BN254> = gen_fp12_sparse(&mut rng);
let setup_normal: InterpreterMemoryInitialization =
setup_mul_fp12_test(out, f, g, "mul_fp254_12");
let setup_sparse: InterpreterMemoryInitialization =
setup_mul_fp12_test(out, f, h, "mul_fp254_12_sparse");
let setup_square: InterpreterMemoryInitialization =
setup_mul_fp12_test(out, f, f, "square_fp254_12");
let output_normal = run_and_return_bn_fp12(f, g, "mul_fp254_12");
let output_sparse = run_and_return_bn_fp12(f, h, "mul_fp254_12_sparse");
let output_square = run_and_return_bn_fp12(f, f, "square_fp254_12");
let intrptr_normal: Interpreter = run_interpreter_with_memory(setup_normal).unwrap();
let intrptr_sparse: Interpreter = run_interpreter_with_memory(setup_sparse).unwrap();
let intrptr_square: Interpreter = run_interpreter_with_memory(setup_square).unwrap();
let out_normal: Vec<U256> = intrptr_normal.extract_kernel_memory(BnPairing, out..out + 12);
let out_sparse: Vec<U256> = intrptr_sparse.extract_kernel_memory(BnPairing, out..out + 12);
let out_square: Vec<U256> = intrptr_square.extract_kernel_memory(BnPairing, out..out + 12);
let exp_normal: Vec<U256> = (f * g).to_stack();
let exp_sparse: Vec<U256> = (f * h).to_stack();
let exp_square: Vec<U256> = (f * f).to_stack();
assert_eq!(out_normal, exp_normal);
assert_eq!(out_sparse, exp_sparse);
assert_eq!(out_square, exp_square);
assert_eq!(output_normal, f * g);
assert_eq!(output_sparse, f * h);
assert_eq!(output_square, f * f);
Ok(())
}

View File

@ -1213,8 +1213,8 @@ impl Stack for BLS381 {
fn from_stack(stack: &[U256]) -> BLS381 {
let mut val = [0u64; 8];
val[..4].copy_from_slice(&stack[1].0);
val[4..].copy_from_slice(&stack[0].0);
val[..4].copy_from_slice(&stack[0].0);
val[4..].copy_from_slice(&stack[1].0);
BLS381 { val: U512(val) }
}
}
@ -1227,8 +1227,8 @@ impl Stack for Fp2<BLS381> {
}
fn from_stack(stack: &[U256]) -> Fp2<BLS381> {
let re = BLS381::from_stack(&stack[2..4]);
let im = BLS381::from_stack(&stack[0..2]);
let re = BLS381::from_stack(&stack[0..2]);
let im = BLS381::from_stack(&stack[2..4]);
Fp2 { re, im }
}
}
@ -1254,7 +1254,7 @@ impl Stack for Fp12<BN254> {
fn from_stack(stack: &[U256]) -> Self {
let mut f = [U256::zero(); 12];
f.copy_from_slice(stack);
f.copy_from_slice(stack);
unsafe { transmute(f) }
}
}