Skip to content

Commit

Permalink
try e2_mul
Browse files Browse the repository at this point in the history
  • Loading branch information
feltroidprime committed Feb 4, 2023
1 parent 725ca17 commit 4402029
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
10 changes: 1 addition & 9 deletions src/fq.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -171,28 +171,23 @@ namespace fq_bigint3 {
p = 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47
sum = ids.sum.d0 + ids.sum.d1*2**86 + ids.sum.d2*2**172
ids.needs_reduction = 1 if sum>=p else 0
print(ids.needs_reduction)
%}

if (sum.d2 == P2) {
if (sum.d1 == P1) {
if (needs_reduction != 0) {
%{ print('case 0') %}

assert [range_check_ptr] = sum.d0 - P0;
let range_check_ptr = range_check_ptr + 1;
let res = sub(sum, P);
return res;
} else {
%{ print('case 1') %}

assert [range_check_ptr] = P0 - sum.d0 - 1;
let range_check_ptr = range_check_ptr + 1;
return sum;
}
} else {
if (needs_reduction != 0) {
%{ print('case 2') %}

assert [range_check_ptr] = sum.d1 - P1;
let range_check_ptr = range_check_ptr + 1;
let res = sub(sum, P);
Expand All @@ -210,13 +205,10 @@ namespace fq_bigint3 {
let range_check_ptr = range_check_ptr + 1;
let res = sub(sum, P);
%{ print('case 4') %}

return res;
} else {
assert [range_check_ptr] = P2 - sum.d2 - 1;
let range_check_ptr = range_check_ptr + 1;
%{ print('case 5') %}

return sum;
}
Expand Down
1 change: 1 addition & 0 deletions src/towers/e2.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ namespace e2 {
return res;
}
func mul{range_check_ptr}(x: E2, y: E2) -> E2 {
alloc_locals;
// var a, b, c fp.Element
// a.Add(&x.A0, &x.A1)
// b.Add(&y.A0, &y.A1)
Expand Down
35 changes: 35 additions & 0 deletions tests/protostar_tests/test_e2.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,38 @@ func test_add{
assert res.a1.d2 = z_gnark.a1.d2;
return ();
}

@external
func test_mul{
syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr, bitwise_ptr: BitwiseBuiltin*
}() {
alloc_locals;
__setup__();

local x: E2;
local y: E2;
local z_gnark: E2;
%{
from starkware.cairo.common.cairo_secp.secp_utils import split
inputs=[random.randint(0, P-1) for i in range(4)]
fill_e2('x', inputs[0], inputs[1])
fill_e2('y', inputs[2], inputs[3])
cmd = ['./tools/parser_go/main', 'e2', 'mul'] + [str(x) for x in inputs]
out = subprocess.run(cmd, stdout=subprocess.PIPE).stdout.decode('utf-8')
print('out', out)
fp_elements = parse_fp_elements(out)
print('fp_elements', fp_elements)
fill_e2('z_gnark', fp_elements[0], fp_elements[1])
%}
let res = e2.mul(x, y);
assert res.a0.d0 = z_gnark.a0.d0;
assert res.a0.d1 = z_gnark.a0.d1;
assert res.a0.d2 = z_gnark.a0.d2;
assert res.a1.d0 = z_gnark.a1.d0;
assert res.a1.d1 = z_gnark.a1.d1;
assert res.a1.d2 = z_gnark.a1.d2;
return ();
}

0 comments on commit 4402029

Please sign in to comment.