tests passing

This commit is contained in:
Dmitry Vagner 2023-04-19 14:51:25 -07:00
parent 3628021a5b
commit bbe64674d0
2 changed files with 26 additions and 24 deletions

View File

@ -4,7 +4,7 @@
/// out *= miller_loop(P, Q)
/// return bn254_final_exponent(out)
global bn254_tate:
global bn254_pairing:
// stack: k , inp, out, retdest
DUP1
ISZERO
@ -14,9 +14,9 @@ global bn254_tate:
%sub_const(1)
// stack: k=k-1, inp, out, retdest
%stack (k, inp, out) -> (k, inp, 200, mul_fp254_12, 200, out, out, bn254_tate, k, inp, out)
// stack: k, inp, 200, mul_fp254_12, 200, out, out, bn254_tate, k, inp, out retdest
%stack (k, inp, out) -> (k, inp, 200, mul_fp254_12, 200, out, out, bn254_pairing, k, inp, out)
// stack: k, inp, 200, mul_fp254_12, 200, out, out, bn254_pairing, k, inp, out retdest
%mul_const(6)
ADD
// stack: inp_k, 200, mul_fp254_12, 200, out, out, bn254_tate, k, inp, out retdest
// stack: inp_k, 200, mul_fp254_12, 200, out, out, bn254_pairing, k, inp, out retdest
%jump(bn254_miller)

View File

@ -20,7 +20,7 @@ fn extract_stack(interpreter: Interpreter<'static>) -> Vec<U256> {
.collect::<Vec<U256>>()
}
fn run_mul_fp6(f: Fp6<BN254>, g: Fp6<BN254>, label: &str) -> Vec<U256> {
fn run_bn_mul_fp6(f: Fp6<BN254>, g: Fp6<BN254>, label: &str) -> Vec<U256> {
let mut stack = f.on_stack();
if label == "mul_fp254_6" {
stack.extend(g.on_stack());
@ -38,13 +38,13 @@ fn run_mul_fp6(f: Fp6<BN254>, g: Fp6<BN254>, label: &str) -> Vec<U256> {
}
#[test]
fn test_mul_fp6() -> Result<()> {
fn test_bn_mul_fp6() -> Result<()> {
let mut rng = rand::thread_rng();
let f: Fp6<BN254> = rng.gen::<Fp6<BN254>>();
let g: Fp6<BN254> = rng.gen::<Fp6<BN254>>();
let out_normal: Vec<U256> = run_mul_fp6(f, g, "mul_fp254_6");
let out_square: Vec<U256> = run_mul_fp6(f, f, "square_fp254_6");
let out_normal: Vec<U256> = run_bn_mul_fp6(f, g, "mul_fp254_6");
let out_square: Vec<U256> = run_bn_mul_fp6(f, f, "square_fp254_6");
let exp_normal: Vec<U256> = (f * g).on_stack();
let exp_square: Vec<U256> = (f * f).on_stack();
@ -55,7 +55,7 @@ fn test_mul_fp6() -> Result<()> {
Ok(())
}
fn run_mul_fp12(f: Fp12<BN254>, g: Fp12<BN254>, label: &str) -> Vec<U256> {
fn run_bn_mul_fp12(f: Fp12<BN254>, g: Fp12<BN254>, label: &str) -> Vec<U256> {
let in0: usize = 200;
let in1: usize = 212;
let out: usize = 224;
@ -81,15 +81,15 @@ fn run_mul_fp12(f: Fp12<BN254>, g: Fp12<BN254>, label: &str) -> Vec<U256> {
}
#[test]
fn test_mul_fp12() -> Result<()> {
fn test_bn_mul_fp12() -> Result<()> {
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 out_normal: Vec<U256> = run_mul_fp12(f, g, "mul_fp254_12");
let out_sparse: Vec<U256> = run_mul_fp12(f, h, "mul_fp254_12_sparse");
let out_square: Vec<U256> = run_mul_fp12(f, f, "square_fp254_12");
let out_normal: Vec<U256> = run_bn_mul_fp12(f, g, "mul_fp254_12");
let out_sparse: Vec<U256> = run_bn_mul_fp12(f, h, "mul_fp254_12_sparse");
let out_square: Vec<U256> = run_bn_mul_fp12(f, f, "square_fp254_12");
let exp_normal: Vec<U256> = (f * g).on_stack();
let exp_sparse: Vec<U256> = (f * h).on_stack();
@ -102,7 +102,7 @@ fn test_mul_fp12() -> Result<()> {
Ok(())
}
fn run_frob_fp6(f: Fp6<BN254>, n: usize) -> Vec<U256> {
fn run_bn_frob_fp6(f: Fp6<BN254>, n: usize) -> Vec<U256> {
let setup = InterpreterMemoryInitialization {
label: format!("test_frob_fp254_6_{}", n),
stack: f.on_stack(),
@ -114,18 +114,18 @@ fn run_frob_fp6(f: Fp6<BN254>, n: usize) -> Vec<U256> {
}
#[test]
fn test_frob_fp6() -> Result<()> {
fn test_bn_frob_fp6() -> Result<()> {
let mut rng = rand::thread_rng();
let f: Fp6<BN254> = rng.gen::<Fp6<BN254>>();
for n in 1..4 {
let output: Vec<U256> = run_frob_fp6(f, n);
let output: Vec<U256> = run_bn_frob_fp6(f, n);
let expected: Vec<U256> = f.frob(n).on_stack();
assert_eq!(output, expected);
}
Ok(())
}
fn run_frob_fp12(f: Fp12<BN254>, n: usize) -> Vec<U256> {
fn run_bn_frob_fp12(f: Fp12<BN254>, n: usize) -> Vec<U256> {
let ptr: usize = 200;
let setup = InterpreterMemoryInitialization {
label: format!("test_frob_fp254_12_{}", n),
@ -138,12 +138,12 @@ fn run_frob_fp12(f: Fp12<BN254>, n: usize) -> Vec<U256> {
}
#[test]
fn test_frob_fp12() -> Result<()> {
fn test_bn_frob_fp12() -> Result<()> {
let mut rng = rand::thread_rng();
let f: Fp12<BN254> = rng.gen::<Fp12<BN254>>();
for n in [1, 2, 3, 6] {
let output = run_frob_fp12(f, n);
let output = run_bn_frob_fp12(f, n);
let expected: Vec<U256> = f.frob(n).on_stack();
assert_eq!(output, expected);
}
@ -151,7 +151,7 @@ fn test_frob_fp12() -> Result<()> {
}
#[test]
fn test_inv_fp12() -> Result<()> {
fn test_bn_inv_fp12() -> Result<()> {
let ptr: usize = 200;
let inv: usize = 212;
let mut rng = rand::thread_rng();
@ -173,7 +173,7 @@ fn test_inv_fp12() -> Result<()> {
}
#[test]
fn test_final_exponent() -> Result<()> {
fn test_bn_final_exponent() -> Result<()> {
let ptr: usize = 200;
let mut rng = rand::thread_rng();
@ -281,9 +281,11 @@ fn test_miller() -> Result<()> {
}
#[test]
fn test_pairing() -> Result<()> {
let ptr: usize = 224;
fn test_bn_pairing() -> Result<()> {
let acc: usize = 200;
let out: usize = 212;
let ptr: usize = 224;
let inputs: Vec<U256> = vec![
CURVE_GENERATOR.x.val,
CURVE_GENERATOR.y.val,
@ -302,7 +304,7 @@ fn test_pairing() -> Result<()> {
U256::from(0xdeadbeefu32),
],
segment: BnPairing,
memory: vec![(ptr, inputs), (out, vec![U256::one()])],
memory: vec![(ptr, inputs), (out, vec![U256::from(acc)])],
};
let interpreter = run_interpreter_with_memory(setup).unwrap();
let output: Vec<U256> = interpreter.extract_kernel_memory(BnPairing, out..out + 12);