2023-01-20 14:30:12 +07:00
|
|
|
use std::ops::Range;
|
2023-01-18 14:41:09 +07:00
|
|
|
use std::str::FromStr;
|
2022-12-15 14:08:23 -08:00
|
|
|
|
2022-10-12 10:06:34 -04:00
|
|
|
use anyhow::Result;
|
|
|
|
|
use ethereum_types::U256;
|
|
|
|
|
|
2023-01-18 12:56:30 +07:00
|
|
|
use crate::bn254_arithmetic::{fp12_to_vec, frob_fp12, gen_fp12, gen_fp12_sparse, Fp12};
|
2022-11-04 13:55:13 +01:00
|
|
|
use crate::cpu::kernel::aggregator::KERNEL;
|
2023-01-20 14:30:12 +07:00
|
|
|
use crate::cpu::kernel::interpreter::Interpreter;
|
2023-01-20 13:59:39 +07:00
|
|
|
use crate::memory::segments::Segment;
|
|
|
|
|
use crate::witness::memory::MemoryAddress;
|
|
|
|
|
|
2023-01-20 14:30:12 +07:00
|
|
|
struct InterpreterSetup {
|
2023-01-20 13:59:39 +07:00
|
|
|
offset: String,
|
|
|
|
|
stack: Vec<U256>,
|
|
|
|
|
memory: Vec<(usize, Vec<U256>)>,
|
2023-01-20 14:30:12 +07:00
|
|
|
output: Range<usize>,
|
2022-12-22 17:39:18 -08:00
|
|
|
}
|
|
|
|
|
|
2023-01-20 14:30:12 +07:00
|
|
|
fn get_interpreter_output(setup: InterpreterSetup) -> Result<Vec<U256>> {
|
|
|
|
|
let label = KERNEL.global_labels[&setup.offset];
|
|
|
|
|
let mut stack = setup.stack;
|
2023-01-20 13:59:39 +07:00
|
|
|
stack.reverse();
|
|
|
|
|
let mut interpreter = Interpreter::new_with_kernel(label, stack);
|
2023-01-20 14:30:12 +07:00
|
|
|
|
|
|
|
|
for (pointer, data) in setup.memory {
|
2023-01-20 13:59:39 +07:00
|
|
|
for (i, term) in data.iter().enumerate() {
|
|
|
|
|
interpreter.generation_state.memory.set(
|
|
|
|
|
MemoryAddress::new(0, Segment::KernelGeneral, pointer + i),
|
|
|
|
|
*term,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interpreter.run()?;
|
2023-01-20 14:30:12 +07:00
|
|
|
|
|
|
|
|
let kernel = &interpreter.generation_state.memory.contexts[interpreter.context].segments
|
|
|
|
|
[Segment::KernelGeneral as usize]
|
|
|
|
|
.content;
|
|
|
|
|
|
|
|
|
|
let mut output: Vec<U256> = vec![];
|
|
|
|
|
for i in setup.output {
|
|
|
|
|
output.push(kernel[i]);
|
|
|
|
|
}
|
2023-01-20 13:59:39 +07:00
|
|
|
Ok(output)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get_address_from_label(lbl: &str) -> U256 {
|
|
|
|
|
U256::from(KERNEL.global_labels[lbl])
|
2022-12-16 17:35:52 -08:00
|
|
|
}
|
|
|
|
|
|
2023-01-20 14:30:12 +07:00
|
|
|
fn make_mul_interpreter(f: Fp12, g: Fp12, mul_label: String) -> InterpreterSetup {
|
2022-12-22 17:39:18 -08:00
|
|
|
let in0 = U256::from(64);
|
|
|
|
|
let in1 = U256::from(76);
|
|
|
|
|
let out = U256::from(88);
|
|
|
|
|
|
2023-01-20 14:30:12 +07:00
|
|
|
let stack = vec![in0, in1, out, U256::from_str("0xdeadbeef").unwrap()];
|
2023-01-20 13:59:39 +07:00
|
|
|
|
2023-01-20 14:30:12 +07:00
|
|
|
let memory = vec![(64usize, fp12_to_vec(f)), (76, fp12_to_vec(g))];
|
2023-01-20 13:59:39 +07:00
|
|
|
|
2023-01-20 14:30:12 +07:00
|
|
|
InterpreterSetup {
|
|
|
|
|
offset: mul_label,
|
|
|
|
|
stack: stack,
|
|
|
|
|
memory: memory,
|
|
|
|
|
output: 88..100,
|
|
|
|
|
}
|
2023-01-20 13:59:39 +07:00
|
|
|
|
|
|
|
|
// 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
|
2022-11-14 15:58:37 -08:00
|
|
|
}
|
|
|
|
|
|
2022-12-14 20:16:50 -08:00
|
|
|
#[test]
|
2022-12-15 13:18:00 -08:00
|
|
|
fn test_mul_fp12() -> Result<()> {
|
2022-12-15 17:00:38 -08:00
|
|
|
let f: Fp12 = gen_fp12();
|
|
|
|
|
let g: Fp12 = gen_fp12();
|
|
|
|
|
let h: Fp12 = gen_fp12_sparse();
|
2022-11-15 13:34:47 -08:00
|
|
|
|
2023-01-20 14:30:12 +07:00
|
|
|
let normal: InterpreterSetup = make_mul_interpreter(f, g, "mul_fp12".to_string());
|
|
|
|
|
let sparse: InterpreterSetup = make_mul_interpreter(f, h, "mul_fp12_sparse".to_string());
|
|
|
|
|
let square: InterpreterSetup = make_mul_interpreter(f, f, "square_fp12_test".to_string());
|
2022-11-15 13:34:47 -08:00
|
|
|
|
2023-01-20 14:30:12 +07:00
|
|
|
let out_normal: Vec<U256> = get_interpreter_output(normal).unwrap();
|
|
|
|
|
let out_sparse: Vec<U256> = get_interpreter_output(sparse).unwrap();
|
|
|
|
|
let out_square: Vec<U256> = get_interpreter_output(square).unwrap();
|
2022-11-15 13:34:47 -08:00
|
|
|
|
2023-01-17 23:58:36 +07:00
|
|
|
let exp_normal: Vec<U256> = fp12_to_vec(f * g);
|
|
|
|
|
let exp_sparse: Vec<U256> = fp12_to_vec(f * h);
|
|
|
|
|
let exp_square: Vec<U256> = fp12_to_vec(f * f);
|
2022-11-15 13:34:47 -08:00
|
|
|
|
2022-12-15 13:18:00 -08:00
|
|
|
assert_eq!(out_normal, exp_normal);
|
|
|
|
|
assert_eq!(out_sparse, exp_sparse);
|
|
|
|
|
assert_eq!(out_square, exp_square);
|
2022-11-15 13:34:47 -08:00
|
|
|
|
|
|
|
|
Ok(())
|
2022-11-15 13:40:14 -08:00
|
|
|
}
|
2022-12-15 17:00:38 -08:00
|
|
|
|
2023-01-20 13:59:39 +07:00
|
|
|
// #[test]
|
|
|
|
|
// fn test_frob_fp12() -> Result<()> {
|
|
|
|
|
// let ptr = U256::from(100);
|
|
|
|
|
// let f: Fp12 = gen_fp12();
|
2022-12-15 17:00:38 -08:00
|
|
|
|
2023-01-20 13:59:39 +07:00
|
|
|
// let mut stack = vec![ptr];
|
|
|
|
|
// stack.extend(fp12_to_vec(f));
|
|
|
|
|
// stack.extend(vec![ptr]);
|
2022-12-15 17:00:38 -08:00
|
|
|
|
2023-01-20 14:30:12 +07:00
|
|
|
// let out_frob1: Vec<U256> = get_interpreter_output("test_frob_fp12_1", stack.clone());
|
|
|
|
|
// let out_frob2: Vec<U256> = get_interpreter_output("test_frob_fp12_2", stack.clone());
|
|
|
|
|
// let out_frob3: Vec<U256> = get_interpreter_output("test_frob_fp12_3", stack.clone());
|
|
|
|
|
// let out_frob6: Vec<U256> = get_interpreter_output("test_frob_fp12_6", stack);
|
2022-12-15 17:00:38 -08:00
|
|
|
|
2023-01-20 13:59:39 +07:00
|
|
|
// 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));
|
2022-12-15 17:00:38 -08:00
|
|
|
|
2023-01-20 13:59:39 +07:00
|
|
|
// assert_eq!(out_frob1, exp_frob1);
|
|
|
|
|
// assert_eq!(out_frob2, exp_frob2);
|
|
|
|
|
// assert_eq!(out_frob3, exp_frob3);
|
|
|
|
|
// assert_eq!(out_frob6, exp_frob6);
|
2022-12-15 17:00:38 -08:00
|
|
|
|
2023-01-20 13:59:39 +07:00
|
|
|
// Ok(())
|
|
|
|
|
// }
|
2022-12-19 14:39:23 -08:00
|
|
|
|
2023-01-20 13:59:39 +07:00
|
|
|
// #[test]
|
|
|
|
|
// fn test_inv_fp12() -> Result<()> {
|
|
|
|
|
// let ptr = U256::from(200);
|
|
|
|
|
// let inv = U256::from(300);
|
2022-12-20 11:57:45 -08:00
|
|
|
|
2023-01-20 13:59:39 +07:00
|
|
|
// 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()]);
|
2022-12-20 11:57:45 -08:00
|
|
|
|
2023-01-20 14:30:12 +07:00
|
|
|
// let output: Vec<U256> = get_interpreter_output("test_inv_fp12", stack);
|
2022-12-20 11:57:45 -08:00
|
|
|
|
2023-01-20 13:59:39 +07:00
|
|
|
// assert_eq!(output, vec![]);
|
2022-12-20 11:57:45 -08:00
|
|
|
|
2023-01-20 13:59:39 +07:00
|
|
|
// Ok(())
|
|
|
|
|
// }
|
2022-12-20 11:57:45 -08:00
|
|
|
|
2023-01-17 23:58:36 +07:00
|
|
|
// #[test]
|
|
|
|
|
// fn test_power() -> Result<()> {
|
|
|
|
|
// let ptr = U256::from(300);
|
|
|
|
|
// let out = U256::from(400);
|
2022-12-20 17:23:05 -08:00
|
|
|
|
2023-01-17 23:58:36 +07:00
|
|
|
// let f: Fp12 = gen_fp12();
|
2022-12-20 17:23:05 -08:00
|
|
|
|
2023-01-17 23:58:36 +07:00
|
|
|
// let mut stack = vec![ptr];
|
|
|
|
|
// stack.extend(fp12_to_vec(f));
|
|
|
|
|
// stack.extend(vec![
|
|
|
|
|
// ptr,
|
|
|
|
|
// out,
|
|
|
|
|
// get_address_from_label("return_fp12_on_stack"),
|
|
|
|
|
// out,
|
|
|
|
|
// ]);
|
2022-12-21 14:52:54 -08:00
|
|
|
|
2023-01-20 14:30:12 +07:00
|
|
|
// let output: Vec<U256> = get_interpreter_output("test_pow", stack);
|
2023-01-17 23:58:36 +07:00
|
|
|
// let expected: Vec<U256> = fp12_to_vec(power(f));
|
2022-12-21 14:52:54 -08:00
|
|
|
|
2023-01-17 23:58:36 +07:00
|
|
|
// assert_eq!(output, expected);
|
2022-12-21 14:52:54 -08:00
|
|
|
|
2023-01-17 23:58:36 +07:00
|
|
|
// Ok(())
|
|
|
|
|
// }
|
2022-12-21 14:52:54 -08:00
|
|
|
|
2023-01-17 23:58:36 +07:00
|
|
|
// fn make_tate_stack(p: Curve, q: TwistedCurve) -> Vec<U256> {
|
|
|
|
|
// let ptr = U256::from(300);
|
|
|
|
|
// let out = U256::from(400);
|
2022-12-27 14:55:47 -08:00
|
|
|
|
2023-01-17 23:58:36 +07:00
|
|
|
// let p_: Vec<U256> = p.into_iter().collect();
|
|
|
|
|
// let q_: Vec<U256> = q.into_iter().flatten().collect();
|
2022-12-27 14:55:47 -08:00
|
|
|
|
2023-01-17 23:58:36 +07:00
|
|
|
// let mut stack = vec![ptr];
|
|
|
|
|
// stack.extend(p_);
|
|
|
|
|
// stack.extend(q_);
|
|
|
|
|
// stack.extend(vec![
|
|
|
|
|
// ptr,
|
|
|
|
|
// out,
|
|
|
|
|
// get_address_from_label("return_fp12_on_stack"),
|
|
|
|
|
// out,
|
|
|
|
|
// ]);
|
|
|
|
|
// stack
|
|
|
|
|
// }
|
2022-12-27 14:55:47 -08:00
|
|
|
|
2023-01-17 23:58:36 +07:00
|
|
|
// #[test]
|
|
|
|
|
// fn test_miller() -> Result<()> {
|
|
|
|
|
// let p: Curve = curve_generator();
|
|
|
|
|
// let q: TwistedCurve = twisted_curve_generator();
|
2022-12-27 14:55:47 -08:00
|
|
|
|
2023-01-17 23:58:36 +07:00
|
|
|
// let stack = make_tate_stack(p, q);
|
2023-01-20 14:30:12 +07:00
|
|
|
// let output = get_interpreter_output("test_miller", stack);
|
2023-01-17 23:58:36 +07:00
|
|
|
// let expected = fp12_to_vec(miller_loop(p, q));
|
2022-12-27 18:38:20 -08:00
|
|
|
|
2023-01-17 23:58:36 +07:00
|
|
|
// assert_eq!(output, expected);
|
2022-12-27 18:38:20 -08:00
|
|
|
|
2023-01-17 23:58:36 +07:00
|
|
|
// Ok(())
|
|
|
|
|
// }
|
2022-12-27 18:38:20 -08:00
|
|
|
|
2023-01-17 23:58:36 +07:00
|
|
|
// #[test]
|
|
|
|
|
// fn test_tate() -> Result<()> {
|
|
|
|
|
// let p: Curve = curve_generator();
|
|
|
|
|
// let q: TwistedCurve = twisted_curve_generator();
|
|
|
|
|
|
|
|
|
|
// let stack = make_tate_stack(p, q);
|
2023-01-20 14:30:12 +07:00
|
|
|
// let output = get_interpreter_output("test_tate", stack);
|
2023-01-17 23:58:36 +07:00
|
|
|
// let expected = fp12_to_vec(tate(p, q));
|
|
|
|
|
|
|
|
|
|
// assert_eq!(output, expected);
|
2022-12-27 18:38:20 -08:00
|
|
|
|
2023-01-17 23:58:36 +07:00
|
|
|
// Ok(())
|
|
|
|
|
// }
|