Skip to content

Commit

Permalink
add groth16 circuits with line precomputation and remove mpcheck for …
Browse files Browse the repository at this point in the history
…3 pairs + fix Ed25519 params
  • Loading branch information
feltroidprime committed Aug 1, 2024
1 parent 3ea0325 commit f85925a
Show file tree
Hide file tree
Showing 28 changed files with 55,030 additions and 57,597 deletions.
10 changes: 5 additions & 5 deletions hydra/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
BLS12_381_ID = 1
SECP256K1_ID = 2
SECP256R1_ID = 3
X25519_ID = 4
ED25519_ID = 4


class CurveID(Enum):
BN254 = 0
BLS12_381 = 1
SECP256K1 = 2
SECP256R1 = 3
X25519 = 4
ED25519 = 4

@staticmethod
def find_value_in_string(s: str) -> int | None:
Expand Down Expand Up @@ -318,9 +318,9 @@ def NAF(x):
Gx=0x6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296,
Gy=0x4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5,
),
X25519_ID: TwistedEdwardsCurve(
cairo_zero_namespace_name="x25519", # See https://neuromancer.sk/std/other/Ed25519
id=X25519_ID,
ED25519_ID: TwistedEdwardsCurve(
cairo_zero_namespace_name="ED25519", # See https://neuromancer.sk/std/other/Ed25519
id=ED25519_ID,
p=0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFED,
n=0x1000000000000000000000000000000014DEF9DEA2F79CD65812631A5CF5D3ED,
h=8,
Expand Down
53 changes: 28 additions & 25 deletions hydra/hints/ecip.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,33 +550,36 @@ def print_ff(ff: FF):
if __name__ == "__main__":
import random

from hydra.definitions import STARK
from hydra.hints.io import int_to_u384, int_array_to_u384_array

random.seed(0)

# def build_cairo1_tests_derive_ec_point_from_X(x: int, curve_id: CurveID, idx: int):
# x_f, y, roots = derive_ec_point_from_X(x, curve_id)

# code = f"""
# #[test]
# fn derive_ec_point_from_X_{CurveID(curve_id).name}_{idx}() {{
# let x: felt252 = {x%STARK};
# let y: u384 = {int_to_u384(y)};
# let grhs_roots:Array<u384> = {int_array_to_u384_array(roots)};
# let result = derive_ec_point_from_X(x, y, grhs_roots, {curve_id.value});
# assert!(result.x == {int_to_u384(x_f)});
# assert!(result.y == y);
# }}
# """
# return code

# codes = "\n".join(
# [
# build_cairo1_tests_derive_ec_point_from_X(x, curve_id, idx)
# for idx, x in enumerate([random.randint(0, STARK - 1) for _ in range(2)])
# for curve_id in CurveID
# ]
# )

# print(codes)
def build_cairo1_tests_derive_ec_point_from_X(x: int, curve_id: CurveID, idx: int):
x_f, y, roots = derive_ec_point_from_X(x, curve_id)

code = f"""
#[test]
fn derive_ec_point_from_X_{CurveID(curve_id).name}_{idx}() {{
let x: felt252 = {x%STARK};
let y: u384 = {int_to_u384(y)};
let grhs_roots:Array<u384> = {int_array_to_u384_array(roots)};
let result = derive_ec_point_from_X(x, y, grhs_roots, {curve_id.value});
assert!(result.x == {int_to_u384(x_f)});
assert!(result.y == y);
}}
"""
return code

codes = "\n".join(
[
build_cairo1_tests_derive_ec_point_from_X(x, curve_id, idx)
for idx, x in enumerate([random.randint(0, STARK - 1) for _ in range(2)])
for curve_id in CurveID
]
)

print(codes)

# average_n_roots = 0
# max_n_roots = 0
Expand Down
7 changes: 5 additions & 2 deletions hydra/hints/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ def bigint_split(
return coeffs[::-1]


def int_to_u384(x: int | PyFelt) -> str:
def int_to_u384(x: int | PyFelt, as_hex=True) -> str:
limbs = bigint_split(x, 4, 2**96)
return f"u384{{limb0:{hex(limbs[0])}, limb1:{hex(limbs[1])}, limb2:{hex(limbs[2])}, limb3:{hex(limbs[3])}}}"
if as_hex:
return f"u384{{limb0:{hex(limbs[0])}, limb1:{hex(limbs[1])}, limb2:{hex(limbs[2])}, limb3:{hex(limbs[3])}}}"
else:
return f"u384{{limb0:{limbs[0]}, limb1:{limbs[1]}, limb2:{limbs[2]}, limb3:{limbs[3]}}}"


def int_to_u256(x: int | PyFelt) -> str:
Expand Down
12 changes: 6 additions & 6 deletions hydra/precompiled_circuits/all_circuits.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,17 +255,17 @@ class CircuitID(Enum):
},
CircuitID.MP_CHECK_BIT0_LOOP: {
"class": MPCheckBit0Loop,
"params": [{"n_pairs": k} for k in [2, 3]],
"params": [{"n_pairs": k} for k in [2]],
"filename": "multi_pairing_check",
},
CircuitID.MP_CHECK_BIT00_LOOP: {
"class": MPCheckBit00Loop,
"params": [{"n_pairs": k} for k in [2, 3]],
"params": [{"n_pairs": k} for k in [2]],
"filename": "multi_pairing_check",
},
CircuitID.MP_CHECK_BIT1_LOOP: {
"class": MPCheckBit1Loop,
"params": [{"n_pairs": k} for k in [2, 3]],
"params": [{"n_pairs": k} for k in [2]],
"filename": "multi_pairing_check",
},
CircuitID.MP_CHECK_PREPARE_PAIRS: {
Expand All @@ -280,17 +280,17 @@ class CircuitID(Enum):
},
CircuitID.MP_CHECK_INIT_BIT: {
"class": MPCheckInitBit,
"params": [{"n_pairs": k} for k in [2, 3]],
"params": [{"n_pairs": k} for k in [2]],
"filename": "multi_pairing_check",
},
CircuitID.MP_CHECK_FINALIZE_BN: {
"class": MPCheckFinalizeBN,
"params": [{"n_pairs": k} for k in [2, 3]],
"params": [{"n_pairs": k} for k in [2]],
"filename": "multi_pairing_check",
},
CircuitID.MP_CHECK_FINALIZE_BLS: {
"class": MPCheckFinalizeBLS,
"params": [{"n_pairs": k} for k in [2, 3]],
"params": [{"n_pairs": k} for k in [2]],
"filename": "multi_pairing_check",
},
CircuitID.FP12_MUL_ASSERT_ONE: {
Expand Down
15 changes: 8 additions & 7 deletions hydra/precompiled_circuits/compilable_circuits/base.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from abc import ABC, abstractmethod
from hydra.modulo_circuit import ModuloCircuit, PyFelt, ModuloCircuitElement
from hydra.definitions import get_base_field, CurveID
from hydra.modulo_circuit_structs import Cairo1SerializableStruct
import re
from enum import Enum
import subprocess
from abc import ABC, abstractmethod
from concurrent.futures import ProcessPoolExecutor
from enum import Enum

from hydra.definitions import CurveID, get_base_field
from hydra.hints.io import int_array_to_u384_array
from hydra.modulo_circuit import ModuloCircuit, ModuloCircuitElement, PyFelt
from hydra.modulo_circuit_structs import Cairo1SerializableStruct


class BaseModuloCircuit(ABC):
Expand Down Expand Up @@ -96,7 +97,7 @@ def compilation_mode_to_file_header(mode: int) -> str:
};
use core::circuit::CircuitElement as CE;
use core::circuit::CircuitInput as CI;
use garaga::definitions::{get_a, get_b, get_p, get_g, get_min_one, G1Point, G2Point, E12D, E12DMulQuotient, G1G2Pair, BNProcessedPair, BLSProcessedPair, MillerLoopResultScalingFactor};
use garaga::definitions::{get_a, get_b, get_p, get_g, get_min_one, G1Point, G2Point, E12D, E12DMulQuotient, G1G2Pair, BNProcessedPair, BLSProcessedPair, MillerLoopResultScalingFactor, G2Line};
use garaga::ec_ops::{SlopeInterceptOutput, FunctionFeltEvaluations, FunctionFelt};
use core::option::Option;\n
"""
Expand All @@ -113,7 +114,7 @@ def cairo1_tests_header() -> str:
circuit_mul, circuit_inverse, EvalCircuitResult, EvalCircuitTrait, u384,
CircuitOutputsTrait, CircuitModulus, AddInputResultTrait, CircuitInputs
};
use garaga::definitions::{G1Point, G2Point, E12D, E12DMulQuotient, G1G2Pair, BNProcessedPair, BLSProcessedPair, MillerLoopResultScalingFactor};
use garaga::definitions::{G1Point, G2Point, E12D, E12DMulQuotient, G1G2Pair, BNProcessedPair, BLSProcessedPair, MillerLoopResultScalingFactor, G2Line};
use garaga::ec_ops::{SlopeInterceptOutput, FunctionFeltEvaluations, FunctionFelt};
"""

Expand Down
Loading

0 comments on commit f85925a

Please sign in to comment.