239 lines
7.5 KiB
Rust
Raw Normal View History

2022-12-15 14:08:23 -08:00
use std::str::FromStr;
2022-10-12 10:06:34 -04:00
use anyhow::Result;
use ethereum_types::U256;
2022-12-21 14:52:54 -08:00
use crate::bn254::{
2022-12-22 17:07:24 -08:00
cord, fp12_to_vec, frob_fp12, gen_curve_point, gen_fp12, gen_fp12_sparse,
gen_twisted_curve_point, mul_fp12, power, tangent, Curve, Fp12, TwistedCurve,
2022-12-21 14:52:54 -08:00
};
2022-11-04 13:55:13 +01:00
use crate::cpu::kernel::aggregator::KERNEL;
2022-12-21 14:52:54 -08:00
use crate::cpu::kernel::interpreter::run_interpreter;
2022-10-12 10:06:34 -04:00
2022-12-22 17:07:24 -08:00
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
2022-12-16 17:35:52 -08:00
}
2022-12-15 13:20:16 -08:00
fn make_mul_stack(
2022-12-15 13:18:00 -08:00
in0: usize,
2022-12-14 19:14:14 -08:00
in1: usize,
out: usize,
2022-12-15 17:00:38 -08:00
f: Fp12,
g: Fp12,
2022-12-15 13:18:00 -08:00
mul_label: &str,
2022-11-14 15:58:37 -08:00
) -> Vec<U256> {
2022-12-15 13:18:00 -08:00
let in0 = U256::from(in0);
2022-12-14 19:14:14 -08:00
let in1 = U256::from(in1);
let out = U256::from(out);
2022-12-15 13:18:00 -08:00
let ret_stack = U256::from(KERNEL.global_labels["ret_stack"]);
let mul_dest = U256::from(KERNEL.global_labels[mul_label]);
2022-12-15 14:08:23 -08:00
let mut input = vec![in0];
2022-12-22 15:10:29 -08:00
input.extend(fp12_to_vec(f));
2022-12-15 13:18:00 -08:00
input.extend(vec![in1]);
2022-12-22 15:10:29 -08:00
input.extend(fp12_to_vec(g));
2022-12-15 13:18:00 -08:00
input.extend(vec![mul_dest, in0, in1, out, ret_stack, out]);
2022-12-14 19:31:21 -08:00
input
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<()> {
let in0 = 64;
let in1 = 76;
2022-11-14 16:41:36 -08:00
let out = 88;
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
2022-12-15 17:00:38 -08:00
let normal: Vec<U256> = make_mul_stack(in0, in1, out, f, g, "mul_fp12");
let sparse: Vec<U256> = make_mul_stack(in0, in1, out, f, h, "mul_fp12_sparse");
let square: Vec<U256> = make_mul_stack(in0, in1, out, f, f, "square_fp12_test");
2022-11-15 13:34:47 -08:00
2022-12-22 17:07:24 -08:00
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);
2022-11-15 13:34:47 -08:00
2022-12-22 17:07:24 -08:00
let exp_normal: Vec<U256> = fp12_to_vec(mul_fp12(f, g));
let exp_sparse: Vec<U256> = fp12_to_vec(mul_fp12(f, h));
let exp_square: Vec<U256> = fp12_to_vec(mul_fp12(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
#[test]
fn test_frob_fp12() -> Result<()> {
2022-12-21 14:52:54 -08:00
let ptr = U256::from(100);
2022-12-15 17:00:38 -08:00
let f: Fp12 = gen_fp12();
2022-12-21 14:52:54 -08:00
let mut stack = vec![ptr];
2022-12-22 15:10:29 -08:00
stack.extend(fp12_to_vec(f));
2022-12-21 14:52:54 -08:00
stack.extend(vec![ptr]);
2022-12-15 17:00:38 -08:00
2022-12-22 17:07:24 -08:00
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);
2022-12-15 17:00:38 -08:00
2022-12-22 17:07:24 -08: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
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(())
}
2022-12-19 14:39:23 -08:00
2022-12-21 14:52:54 -08:00
#[test]
fn test_inv_fp12() -> Result<()> {
2022-12-20 12:21:27 -08:00
let ptr = U256::from(200);
let inv = U256::from(300);
2022-12-20 11:57:45 -08:00
let f: Fp12 = gen_fp12();
2022-12-21 14:52:54 -08:00
let mut stack = vec![ptr];
2022-12-22 15:10:29 -08:00
stack.extend(fp12_to_vec(f));
2022-12-21 14:52:54 -08:00
stack.extend(vec![ptr, inv, U256::from_str("0xdeadbeef").unwrap()]);
2022-12-20 11:57:45 -08:00
2022-12-22 17:07:24 -08:00
let output: Vec<U256> = get_output("test_inv_fp12", stack);
2022-12-20 11:57:45 -08:00
assert_eq!(output, vec![]);
Ok(())
}
2022-12-20 12:47:36 -08:00
#[test]
2022-12-20 12:47:09 -08:00
fn test_pow_fp12() -> Result<()> {
2022-12-20 17:23:05 -08:00
let ptr = U256::from(300);
let out = U256::from(400);
2022-12-21 14:52:54 -08:00
let f: Fp12 = gen_fp12();
2022-12-20 17:23:05 -08:00
let ret_stack = U256::from(KERNEL.global_labels["ret_stack"]);
2022-12-21 14:52:54 -08:00
let mut stack = vec![ptr];
2022-12-22 15:10:29 -08:00
stack.extend(fp12_to_vec(f));
2022-12-21 14:52:54 -08:00
stack.extend(vec![ptr, out, ret_stack, out]);
2022-12-22 17:07:24 -08:00
let output: Vec<U256> = get_output("test_pow", stack);
let expected: Vec<U256> = fp12_to_vec(power(f));
2022-12-21 14:52:54 -08:00
2022-12-22 15:10:29 -08:00
assert_eq!(output, expected);
2022-12-21 14:52:54 -08:00
2022-12-22 15:10:29 -08:00
Ok(())
}
2022-12-21 14:52:54 -08:00
2022-12-22 15:10:29 -08:00
#[test]
2022-12-22 17:07:24 -08:00
fn test_line() -> Result<()> {
2022-12-22 15:10:29 -08:00
let p1: Curve = gen_curve_point();
let p2: Curve = gen_curve_point();
let q: TwistedCurve = gen_twisted_curve_point();
2022-12-22 17:07:24 -08:00
let p1_: Vec<U256> = p1.to_vec();
let p2_: Vec<U256> = p2.to_vec();
2022-12-22 15:10:29 -08:00
let q_: Vec<U256> = q.into_iter().flatten().collect();
2022-12-22 17:07:24 -08:00
let mut tan_stack = p1_.clone();
tan_stack.extend(q_.clone());
2022-12-21 14:52:54 -08:00
2022-12-22 17:07:24 -08:00
let mut cord_stack = p1_;
cord_stack.extend(p2_);
cord_stack.extend(q_);
2022-12-21 14:52:54 -08:00
2022-12-22 17:07:24 -08:00
let output_tan: Vec<U256> = get_output("test_tangent", tan_stack);
let output_cord: Vec<U256> = get_output("test_cord", cord_stack);
2022-12-21 14:52:54 -08:00
2022-12-22 17:07:24 -08:00
let expected_tan = fp12_to_vec(tangent(p1, q));
let expected_cord = fp12_to_vec(cord(p1, p2, q));
2022-12-21 14:52:54 -08:00
2022-12-22 17:07:24 -08:00
assert_eq!(output_tan, expected_tan);
assert_eq!(output_cord, expected_cord);
2022-12-21 14:52:54 -08:00
2022-12-22 15:10:29 -08:00
Ok(())
}
2022-12-21 14:52:54 -08:00
2022-12-22 15:10:29 -08:00
// fn make_miller_stack(p: [Fp; 2], q: [Fp2; 2]) -> Vec<U256> {
// let ptr = U256::from(300);
// let out = U256::from(400);
2022-12-21 14:52:54 -08:00
2022-12-22 15:10:29 -08:00
// let p: Vec<U256> = p.into_iter().collect();
2022-12-21 14:52:54 -08:00
// let q: Vec<U256> = q.into_iter().flatten().collect();
2022-12-22 15:10:29 -08:00
// let ret_stack = U256::from(KERNEL.global_labels["ret_stack"]);
// let mut input = vec![ptr];
// input.extend(p);
2022-12-21 14:52:54 -08:00
// input.extend(q);
2022-12-22 15:10:29 -08:00
// input.extend(vec![ptr, out, ret_stack]);
2022-12-21 14:52:54 -08:00
// input.reverse();
// input
// }
// #[test]
// fn test_miller() -> Result<()> {
// let p = [U256::from(1), U256::from(2)];
// let q = [
// [
// U256::from_str("0x1800deef121f1e76426a00665e5c4479674322d4f75edadd46debd5cd992f6ed")
// .unwrap(),
// U256::from_str("0x198e9393920d483a7260bfb731fb5d25f1aa493335a9e71297e485b7aef312c2")
// .unwrap(),
// ],
// [
// U256::from_str("0x12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa")
// .unwrap(),
// U256::from_str("0x90689d0585ff075ec9e99ad690c3395bc4b313370b38ef355acdadcd122975b")
// .unwrap(),
// ],
// ];
// let test_mill = KERNEL.global_labels["test_miller"];
// let stack = make_miller_stack(p, q);
// let output: Vec<U256> = run_interpreter(test_mill, stack)?.stack().to_vec();
// let mut expected: Vec<U256> = vec![
// U256::from_str("0xbf4dbb7e41fb58122aa29dcced57731d7cbb49b1fe9a73cb13416e1002376da")
// .unwrap(),
// U256::from_str("0x110b019c149b43a7fbd6d42d7553debcbebd35c148f63aaecf72a5fbda451ac6")
// .unwrap(),
// U256::from_str("0x27225e97ee6c877964c8f32e0b54e61ead09c3e818174cd8b5beabe7cd7385e8")
// .unwrap(),
// U256::from_str("0x5762cb6648b4b4c5df8a8874a21d937adf185d91f34e8ccf58f5b39196db02").unwrap(),
// U256::from_str("0x463002dc1a426b172f4a1e29486fc11eba01de99b559368139c8ef5271eb37f")
// .unwrap(),
// U256::from_str("0x753dcc72acdffcc45633803f1b555388969dd7c27d2a674a23a228f522480d9")
// .unwrap(),
// U256::from_str("0xd32a892d29151553101376a6638938135e30126f698a40a73f20c6ac64a4585")
// .unwrap(),
// U256::from_str("0x290afd3e28c223a624d9f5a737f9f9e4b4200b518333844d81acc445fa5910da")
// .unwrap(),
// U256::from_str("0x262e0ee72a8123b741dc113b8e2d207ee8bad011e0f6ae2015439960c789cf78")
// .unwrap(),
// U256::from_str("0x1588e0b23d868d7517e3021e620c69eb1521a49faa9bfcd4cf3a54127d4d14cb")
// .unwrap(),
// U256::from_str("0x1c23a135a7dfa96db62622c5fef4b9751d121523dd39ca1cefeacb3419835a53")
// .unwrap(),
// U256::from_str("0x2caeb873076ec8f37fa7af265d2966dd0024acbc63bd2b21f323084fc71f4a59")
// .unwrap(),
// ];
// expected.reverse();
// assert_eq!(output, expected);
// Ok(())
// }