fp12 sq works

This commit is contained in:
Dmitry Vagner 2022-11-15 13:34:47 -08:00
parent 77820b0f23
commit 3dc66a259f
4 changed files with 66 additions and 11 deletions

View File

@ -677,4 +677,24 @@
// stack 9b , a, 9a - b
ADDFP254
// stack: 9b + a, 9a - b
%endmacro
%endmacro
%macro sh
// stack: f0 , f0_, f1, f1_, f2 , f2_
SWAP2
// stack: f1 , f0_, g0 , f1_, f2 , f2_
SWAP4
// stack: f2 , f0_, g0 , f1_, g1 , f2_
SWAP1
// stack: f0_, f2 , g0 , f1_, g1 , f2_
SWAP3
// stack: f1_, f2 , g0 , g0_, g1 , f2_
SWAP5
// stack: f2_, f2 , g0 , g0_, g1 , g1_
SWAP1
// stack: f2 , f2_, g0 , g0_, g1 , g1_
%i9
// stack: g2_, g2 , g0 , g0_, g1 , g1_
SWAP1
// stack: g2 , g2_, g0 , g0_, g1 , g1_
%endmacro

View File

@ -23,7 +23,7 @@ global test_mul_fp12:
// stack: ret_stack, inB, out, inA, out
SWAP3
// stack: inA, inB, out, ret_stack, out
%jump(mul_fp12_sparse)
%jump(square_fp12_test)
ret_stack:
// stack: out
DUP1 %offset_fp6
@ -36,6 +36,11 @@ ret_stack:
// stack: h, h', out
%jump(0xdeadbeef)
square_fp12_test:
POP
%jump(square_fp12)
///////////////////////////////////////
///// GENERAL FP12 MULTIPLICATION /////
///////////////////////////////////////
@ -360,7 +365,7 @@ global mul_fp12_sparse:
/// swap | 2 | 16 | 32
/// add | 1 | 16 | 16
/// mul | 1 | 157 | 157
/// sq | 2 | |
/// sq | 2 | 101 | 202
/// dbl | 1 | 13 | 13
///
/// lone stack operations:
@ -391,7 +396,7 @@ global mul_fp12_sparse:
global square_fp12:
// stack: inp, out
DUP1 %offset_fp6
DUP1
// stack: inp, inp, out
%load_fp6
// stack: f, inp, out
@ -427,17 +432,19 @@ post_mul:
%jump(square_fp6)
post_sq1:
// stack: f'f', inp, f, post_sq2, out
%sh
// stack: sh(f'f'), inp, f, post_sq2, out
%swap_fp6_hole
// stack: f, inp, f'f', post_sq2, out
// stack: f, inp, sh(f'f'), post_sq2, out
SWAP6 SWAP13 SWAP6
// stack: f, post_sq2, f'f', inp, out
// stack: f, post_sq2, sh(f'f'), inp, out
%jump(square_fp6)
post_sq2:
// stack: ff , f'f', inp, out
// stack: ff , sh(f'f'), inp, out
%add_fp6
// stack: ff + f'f', inp, out
// stack: ff + sh(f'f'), inp, out
DUP8
// stack: out, ff + f'f', inp, out
// stack: out, ff + sh(f'f'), inp, out
%store_fp6
// stack: inp, out
%pop2

View File

@ -58,7 +58,6 @@
/// e2 = c0d2 + c1d1 + c2d0 - (c0_d2_ + c1_d1_ + c2_d0_)
/// e2_ = c0d2_ + c0_d2 + c1d1_ + c1_d1 + c2d0_ + c2_d0
// cost: 157
global mul_fp6:
// e2
@ -299,6 +298,7 @@ global mul_fp6:
/// e2 = 2(c0c2 - c0_c2_) + (c1^2 - c1_^2)
/// e2_ = 2(c0_c2 + c2c0_) + 2c1c1_
// cost: 101
global square_fp6:
/// e0 = (c0^2 - c0_^2) + x0
/// e0_ = 2c0c0_ + x0_

View File

@ -223,7 +223,7 @@ fn test_fp12() -> Result<()> {
Ok(())
}
#[test]
// #[test]
fn test_fp12_sparse() -> Result<()> {
let in1 = 64;
let in2 = 76;
@ -251,3 +251,31 @@ fn test_fp12_sparse() -> Result<()> {
Ok(())
}
#[test]
fn test_fp12_square() -> Result<()> {
let in1 = 64;
let in2 = 76;
let out = 88;
let f0 = gen_fp6();
let f1 = gen_fp6();
let initial_offset = KERNEL.global_labels["test_mul_fp12"];
let initial_stack: Vec<U256> = make_initial_stack(in1, in2, out, f0, f1, f0, f1);
let final_stack: Vec<U256> = run_interpreter(initial_offset, initial_stack)?
.stack()
.to_vec();
let mut output: Vec<u32> = mul_fp12([f0, f1], [f0, f1])
.into_iter()
.flatten()
.flatten()
.collect();
output.extend(vec![out]);
let expected = as_stack(output);
assert_eq!(final_stack, expected);
Ok(())
}