mirror of
https://github.com/logos-storage/plonky2.git
synced 2026-01-10 01:33:07 +00:00
mul test from memory
This commit is contained in:
parent
7f135fc090
commit
5f2baea0df
@ -819,7 +819,6 @@ const FROB_Z: [Fp2; 12] = [
|
||||
},
|
||||
];
|
||||
|
||||
|
||||
pub fn fp12_to_array(f: Fp12) -> [U256; 12] {
|
||||
unsafe { transmute(f) }
|
||||
}
|
||||
|
||||
@ -1,13 +1,3 @@
|
||||
/// Note: uncomment this to test
|
||||
|
||||
global test_mul_fp12:
|
||||
// stack: inA, f, f', inB, g, g', mul_dest, inA, inB, out, return_fp12_on_stack, out
|
||||
%store_fp12
|
||||
// stack: inB, g, g', mul_dest, inA, inB, out, return_fp12_on_stack, out
|
||||
%store_fp12
|
||||
// stack: mul_dest, inA, inB, out, return_fp12_on_stack, out
|
||||
JUMP
|
||||
|
||||
///////////////////////////////////////
|
||||
///// GENERAL FP12 MULTIPLICATION /////
|
||||
///////////////////////////////////////
|
||||
@ -319,7 +309,6 @@ global mul_fp12_sparse:
|
||||
|
||||
global square_fp12_test:
|
||||
POP
|
||||
%jump(square_fp12)
|
||||
|
||||
global square_fp12:
|
||||
// stack: inp, out
|
||||
|
||||
@ -5,39 +5,74 @@ use ethereum_types::U256;
|
||||
|
||||
use crate::bn254_arithmetic::{fp12_to_vec, frob_fp12, gen_fp12, gen_fp12_sparse, Fp12};
|
||||
use crate::cpu::kernel::aggregator::KERNEL;
|
||||
use crate::cpu::kernel::interpreter::run_interpreter;
|
||||
use crate::cpu::kernel::interpreter::{run_interpreter, Interpreter};
|
||||
use crate::memory::segments::Segment;
|
||||
use crate::witness::memory::MemoryAddress;
|
||||
|
||||
struct InterpreterInit {
|
||||
offset: String,
|
||||
stack: Vec<U256>,
|
||||
memory: Vec<(usize, Vec<U256>)>,
|
||||
}
|
||||
|
||||
fn run_test_interpreter(init: InterpreterInit) -> Result<Vec<U256>> {
|
||||
let label = KERNEL.global_labels[&init.offset];
|
||||
let mut stack = init.stack;
|
||||
stack.reverse();
|
||||
let mut interpreter = Interpreter::new_with_kernel(label, stack);
|
||||
|
||||
for (pointer, data) in init.memory {
|
||||
for (i, term) in data.iter().enumerate() {
|
||||
interpreter.generation_state.memory.set(
|
||||
MemoryAddress::new(0, Segment::KernelGeneral, pointer + i),
|
||||
*term,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
interpreter.run()?;
|
||||
let mut output = interpreter.stack().to_vec();
|
||||
output.reverse();
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
fn get_address_from_label(lbl: &str) -> U256 {
|
||||
U256::from(KERNEL.global_labels[lbl])
|
||||
}
|
||||
|
||||
fn get_output(lbl: &str, stack: Vec<U256>) -> Vec<U256> {
|
||||
let label = KERNEL.global_labels[lbl];
|
||||
let mut input = stack;
|
||||
input.reverse();
|
||||
let mut output = run_interpreter(label, input).unwrap().stack().to_vec();
|
||||
output.reverse();
|
||||
output
|
||||
}
|
||||
|
||||
fn make_mul_stack(f: Fp12, g: Fp12, mul_label: &str) -> Vec<U256> {
|
||||
fn make_mul_interpreter(f: Fp12, g: Fp12, mul_label: String) -> InterpreterInit {
|
||||
let in0 = U256::from(64);
|
||||
let in1 = U256::from(76);
|
||||
let out = U256::from(88);
|
||||
|
||||
let mut stack = vec![in0];
|
||||
stack.extend(fp12_to_vec(f));
|
||||
stack.extend(vec![in1]);
|
||||
stack.extend(fp12_to_vec(g));
|
||||
stack.extend(vec![
|
||||
get_address_from_label(mul_label),
|
||||
let stack = vec![
|
||||
in0,
|
||||
in1,
|
||||
out,
|
||||
get_address_from_label("return_fp12_on_stack"),
|
||||
out,
|
||||
]);
|
||||
stack
|
||||
];
|
||||
|
||||
let memory = vec![
|
||||
(64usize, fp12_to_vec(f)),
|
||||
(76, fp12_to_vec(g))
|
||||
];
|
||||
|
||||
InterpreterInit { offset: mul_label, stack: stack, memory: memory }
|
||||
|
||||
// let mut stack = vec![in0];
|
||||
// stack.extend(fp12_to_vec(f));
|
||||
// stack.extend(vec![in1]);
|
||||
// stack.extend(fp12_to_vec(g));
|
||||
// stack.extend(vec![
|
||||
// get_address_from_label(mul_label),
|
||||
// in0,
|
||||
// in1,
|
||||
// out,
|
||||
// get_address_from_label("return_fp12_on_stack"),
|
||||
// out,
|
||||
// ]);
|
||||
// stack
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -46,13 +81,13 @@ fn test_mul_fp12() -> Result<()> {
|
||||
let g: Fp12 = gen_fp12();
|
||||
let h: Fp12 = gen_fp12_sparse();
|
||||
|
||||
let normal: Vec<U256> = make_mul_stack(f, g, "mul_fp12");
|
||||
let sparse: Vec<U256> = make_mul_stack(f, h, "mul_fp12_sparse");
|
||||
let square: Vec<U256> = make_mul_stack(f, f, "square_fp12_test");
|
||||
let normal: InterpreterInit = make_mul_interpreter(f, g, "mul_fp12".to_string());
|
||||
let sparse: InterpreterInit = make_mul_interpreter(f, h, "mul_fp12_sparse".to_string());
|
||||
let square: InterpreterInit = make_mul_interpreter(f, f, "square_fp12_test".to_string());
|
||||
|
||||
let out_normal: Vec<U256> = get_output("test_mul_fp12", normal);
|
||||
let out_sparse: Vec<U256> = get_output("test_mul_fp12", sparse);
|
||||
let out_square: Vec<U256> = get_output("test_mul_fp12", square);
|
||||
let out_normal: Vec<U256> = run_test_interpreter(normal).unwrap();
|
||||
let out_sparse: Vec<U256> = run_test_interpreter(sparse).unwrap();
|
||||
let out_square: Vec<U256> = run_test_interpreter(square).unwrap();
|
||||
|
||||
let exp_normal: Vec<U256> = fp12_to_vec(f * g);
|
||||
let exp_sparse: Vec<U256> = fp12_to_vec(f * h);
|
||||
@ -65,49 +100,49 @@ fn test_mul_fp12() -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_frob_fp12() -> Result<()> {
|
||||
let ptr = U256::from(100);
|
||||
let f: Fp12 = gen_fp12();
|
||||
// #[test]
|
||||
// fn test_frob_fp12() -> Result<()> {
|
||||
// let ptr = U256::from(100);
|
||||
// let f: Fp12 = gen_fp12();
|
||||
|
||||
let mut stack = vec![ptr];
|
||||
stack.extend(fp12_to_vec(f));
|
||||
stack.extend(vec![ptr]);
|
||||
// let mut stack = vec![ptr];
|
||||
// stack.extend(fp12_to_vec(f));
|
||||
// stack.extend(vec![ptr]);
|
||||
|
||||
let out_frob1: Vec<U256> = get_output("test_frob_fp12_1", stack.clone());
|
||||
let out_frob2: Vec<U256> = get_output("test_frob_fp12_2", stack.clone());
|
||||
let out_frob3: Vec<U256> = get_output("test_frob_fp12_3", stack.clone());
|
||||
let out_frob6: Vec<U256> = get_output("test_frob_fp12_6", stack);
|
||||
// let out_frob1: Vec<U256> = run_test_interpreter("test_frob_fp12_1", stack.clone());
|
||||
// let out_frob2: Vec<U256> = run_test_interpreter("test_frob_fp12_2", stack.clone());
|
||||
// let out_frob3: Vec<U256> = run_test_interpreter("test_frob_fp12_3", stack.clone());
|
||||
// let out_frob6: Vec<U256> = run_test_interpreter("test_frob_fp12_6", stack);
|
||||
|
||||
let exp_frob1: Vec<U256> = fp12_to_vec(frob_fp12(1, f));
|
||||
let exp_frob2: Vec<U256> = fp12_to_vec(frob_fp12(2, f));
|
||||
let exp_frob3: Vec<U256> = fp12_to_vec(frob_fp12(3, f));
|
||||
let exp_frob6: Vec<U256> = fp12_to_vec(frob_fp12(6, f));
|
||||
// let exp_frob1: Vec<U256> = fp12_to_vec(frob_fp12(1, f));
|
||||
// let exp_frob2: Vec<U256> = fp12_to_vec(frob_fp12(2, f));
|
||||
// let exp_frob3: Vec<U256> = fp12_to_vec(frob_fp12(3, f));
|
||||
// let exp_frob6: Vec<U256> = fp12_to_vec(frob_fp12(6, f));
|
||||
|
||||
assert_eq!(out_frob1, exp_frob1);
|
||||
assert_eq!(out_frob2, exp_frob2);
|
||||
assert_eq!(out_frob3, exp_frob3);
|
||||
assert_eq!(out_frob6, exp_frob6);
|
||||
// assert_eq!(out_frob1, exp_frob1);
|
||||
// assert_eq!(out_frob2, exp_frob2);
|
||||
// assert_eq!(out_frob3, exp_frob3);
|
||||
// assert_eq!(out_frob6, exp_frob6);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
// Ok(())
|
||||
// }
|
||||
|
||||
#[test]
|
||||
fn test_inv_fp12() -> Result<()> {
|
||||
let ptr = U256::from(200);
|
||||
let inv = U256::from(300);
|
||||
// #[test]
|
||||
// fn test_inv_fp12() -> Result<()> {
|
||||
// let ptr = U256::from(200);
|
||||
// let inv = U256::from(300);
|
||||
|
||||
let f: Fp12 = gen_fp12();
|
||||
let mut stack = vec![ptr];
|
||||
stack.extend(fp12_to_vec(f));
|
||||
stack.extend(vec![ptr, inv, U256::from_str("0xdeadbeef").unwrap()]);
|
||||
// let f: Fp12 = gen_fp12();
|
||||
// let mut stack = vec![ptr];
|
||||
// stack.extend(fp12_to_vec(f));
|
||||
// stack.extend(vec![ptr, inv, U256::from_str("0xdeadbeef").unwrap()]);
|
||||
|
||||
let output: Vec<U256> = get_output("test_inv_fp12", stack);
|
||||
// let output: Vec<U256> = run_test_interpreter("test_inv_fp12", stack);
|
||||
|
||||
assert_eq!(output, vec![]);
|
||||
// assert_eq!(output, vec![]);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
// Ok(())
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn test_power() -> Result<()> {
|
||||
@ -125,7 +160,7 @@ fn test_inv_fp12() -> Result<()> {
|
||||
// out,
|
||||
// ]);
|
||||
|
||||
// let output: Vec<U256> = get_output("test_pow", stack);
|
||||
// let output: Vec<U256> = run_test_interpreter("test_pow", stack);
|
||||
// let expected: Vec<U256> = fp12_to_vec(power(f));
|
||||
|
||||
// assert_eq!(output, expected);
|
||||
@ -158,7 +193,7 @@ fn test_inv_fp12() -> Result<()> {
|
||||
// let q: TwistedCurve = twisted_curve_generator();
|
||||
|
||||
// let stack = make_tate_stack(p, q);
|
||||
// let output = get_output("test_miller", stack);
|
||||
// let output = run_test_interpreter("test_miller", stack);
|
||||
// let expected = fp12_to_vec(miller_loop(p, q));
|
||||
|
||||
// assert_eq!(output, expected);
|
||||
@ -172,7 +207,7 @@ fn test_inv_fp12() -> Result<()> {
|
||||
// let q: TwistedCurve = twisted_curve_generator();
|
||||
|
||||
// let stack = make_tate_stack(p, q);
|
||||
// let output = get_output("test_tate", stack);
|
||||
// let output = run_test_interpreter("test_tate", stack);
|
||||
// let expected = fp12_to_vec(tate(p, q));
|
||||
|
||||
// assert_eq!(output, expected);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user