Skip to content

Commit

Permalink
Release 0.13.3 (keep-starknet-strange#195)
Browse files Browse the repository at this point in the history
Co-authored-by: Rodrigo Ferreira
  • Loading branch information
feltroidprime authored Sep 13, 2024
1 parent 2fb2964 commit c80fb9b
Show file tree
Hide file tree
Showing 51 changed files with 1,470 additions and 219 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/wasm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Build and check for changes in generated code
working-directory: tools/npm/garaga_rs
working-directory: tools/npm/garaga_ts
run: |
docker compose up --build --exit-code-from app
git status --porcelain
Expand All @@ -40,7 +40,7 @@ jobs:
uses: actions/upload-artifact@v4
with:
name: npm-package
path: tools/npm/garaga_rs/*.tgz
path: tools/npm/garaga_ts/*.tgz
if-no-files-found: error

test-integration:
Expand All @@ -63,11 +63,11 @@ jobs:
uses: actions/download-artifact@v4
with:
name: npm-package
path: tools/npm/garaga_rs
path: tools/npm/garaga_ts
- name: Build and tests integration test packages
working-directory: tools/npm/integration-test-suite
run: |
cp ../garaga_rs/garaga_rs-*.tgz garaga_rs.tgz
cp ../garaga_ts/garaga-*.tgz garaga.tgz
npm i
npm run build
npx puppeteer browsers install
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ Scarb.lock
tests/contracts_e2e/devnet/*

!hydra/garaga/starknet/groth16_contract_generator/examples/*.json


!tools/npm/garaga_rs/*.json
4 changes: 2 additions & 2 deletions hydra/garaga/modulo_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -1066,9 +1066,9 @@ def compile_circuit_cairo_1(
signature_input = "mut input: Array<u384>"

if self.generic_circuit:
code = f"fn {function_name}({signature_input}, curve_index:usize)->{signature_output} {{\n"
code = f"#[inline(always)]\nfn {function_name}({signature_input}, curve_index:usize)->{signature_output} {{\n"
else:
code = f"fn {function_name}({signature_input})->{signature_output} {{\n"
code = f"#[inline(always)]\nfn {function_name}({signature_input})->{signature_output} {{\n"

# Define the input for the circuit.
code, offset_to_reference_map, start_index = self.write_cairo1_input_stack(
Expand Down
42 changes: 37 additions & 5 deletions hydra/garaga/modulo_circuit_structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,14 @@ def extract_from_circuit_output(
code += "};"
return code

@property
def struct_name(self) -> str:
p = self.elmts[0].p
if p.bit_length() <= 288:
return "E12DMulQuotient<u288>"
else:
return "E12DMulQuotient<u384>"

def serialize(self, raw: bool = False, is_option: bool = False) -> str:
if self.elmts is None:
raw_struct = "Option::None"
Expand All @@ -979,8 +987,17 @@ def serialize(self, raw: bool = False, is_option: bool = False) -> str:
else:
return f"let {self.name}:Option<{self.__class__.__name__}> = {raw_struct};\n"
else:
assert len(self.elmts) == 11, f"Expected 11 elements, got {len(self.elmts)}"
raw_struct = f"{self.__class__.__name__}{{{','.join([f'w{i}: {int_to_u384(self.elmts[i].value)}' for i in range(len(self))])}}}"
assert len(self.elmts) == 11
bits: int = self.elmts[0].p.bit_length()
if bits <= 288:
curve_id = 0
else:
curve_id = 1

raw_struct = (
f"{self.__class__.__name__}{{"
+ f"{','.join([f'w{i}: {int_to_u2XX(self.elmts[i].value, curve_id=curve_id)}' for i in range(len(self))])}}}"
)
if is_option:
raw_struct = f"Option::Some({raw_struct})"
if raw:
Expand All @@ -989,12 +1006,27 @@ def serialize(self, raw: bool = False, is_option: bool = False) -> str:
return f"let {self.name} = {raw_struct};\n"

def _serialize_to_calldata(self) -> list[int]:
return io.bigint_split_array(self.elmts, prepend_length=False)
bits: int = self.bits
if bits <= 288:
return io.bigint_split_array(self.elmts, n_limbs=3, prepend_length=False)
elif bits <= 384:
return io.bigint_split_array(self.elmts, n_limbs=4, prepend_length=False)
else:
raise ValueError(f"Unsupported bit length for E12D: {bits}")

def dump_to_circuit_input(self) -> str:
bits: int = self.elmts[0].p.bit_length()
code = ""
for i in range(len(self)):
code += f"circuit_inputs = circuit_inputs.next_2({self.name}.w{i});\n"
if bits <= 288:
for i in range(len(self)):
code += (
f"circuit_inputs = circuit_inputs.next_u288({self.name}.w{i});\n"
)
elif bits <= 384:
for i in range(len(self)):
code += f"circuit_inputs = circuit_inputs.next_2({self.name}.w{i});\n"
else:
raise ValueError(f"Unsupported bit length: {bits}")
return code

def __len__(self) -> int:
Expand Down
19 changes: 5 additions & 14 deletions hydra/garaga/starknet/groth16_contract_generator/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from garaga.starknet.cli.utils import create_directory
from garaga.starknet.groth16_contract_generator.parsing_utils import Groth16VerifyingKey

ECIP_OPS_CLASS_HASH = 0x3B0507836FC39065C529306331041BB8460D6802974F52463AC761E458983E7
ECIP_OPS_CLASS_HASH = 0x7918F484291EB154E13D0E43BA6403E62DC1F5FBB3A191D868E2E37359F8713


def precompute_lines_from_vk(vk: Groth16VerifyingKey) -> StructArray:
Expand Down Expand Up @@ -71,9 +71,10 @@ def gen_groth16_verifier(
#[starknet::contract]
mod Groth16Verifier{curve_id.name} {{
use starknet::SyscallResultTrait;
use garaga::definitions::{{G1Point, G1G2Pair, E12DMulQuotient}};
use garaga::groth16::{{multi_pairing_check_{curve_id.name.lower()}_3P_2F_with_extra_miller_loop_result, Groth16Proof, MPCheckHint{curve_id.name}}};
use garaga::definitions::{{G1Point, G1G2Pair}};
use garaga::groth16::{{multi_pairing_check_{curve_id.name.lower()}_3P_2F_with_extra_miller_loop_result}};
use garaga::ec_ops::{{G1PointTrait, G2PointTrait, ec_safe_add}};
use garaga::utils::calldata::{{deserialize_full_proof_with_hints_{curve_id.name.lower()}}};
use super::{{N_PUBLIC_INPUTS, vk, ic, precomputed_lines}};
const ECIP_OPS_CLASS_HASH: felt252 = {hex(ecip_class_hash)};
Expand All @@ -82,14 +83,6 @@ def gen_groth16_verifier(
#[storage]
struct Storage {{}}
#[derive(Drop, Serde)]
struct FullProofWithHints {{
groth16_proof: Groth16Proof,
mpcheck_hint: MPCheckHint{curve_id.name},
small_Q: E12DMulQuotient,
msm_hint: Array<felt252>,
}}
#[abi(embed_v0)]
impl IGroth16Verifier{curve_id.name} of super::IGroth16Verifier{curve_id.name}<ContractState> {{
fn verify_groth16_proof_{curve_id.name.lower()}(
Expand All @@ -98,9 +91,7 @@ def gen_groth16_verifier(
) -> bool {{
// DO NOT EDIT THIS FUNCTION UNLESS YOU KNOW WHAT YOU ARE DOING.
// ONLY EDIT THE process_public_inputs FUNCTION BELOW.
let mut full_proof_with_hints = full_proof_with_hints;
let fph = Serde::<FullProofWithHints>::deserialize(ref full_proof_with_hints)
.expect('unwr_full_proof_with_hints');
let fph = deserialize_full_proof_with_hints_{curve_id.name.lower()}(full_proof_with_hints);
let groth16_proof = fph.groth16_proof;
let mpcheck_hint = fph.mpcheck_hint;
let small_Q = fph.small_Q;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ def gen_risc0_groth16_verifier(
#[starknet::contract]
mod Risc0Groth16Verifier{curve_id.name} {{
use starknet::SyscallResultTrait;
use garaga::definitions::{{G1Point, G1G2Pair, E12DMulQuotient}};
use garaga::groth16::{{multi_pairing_check_{curve_id.name.lower()}_3P_2F_with_extra_miller_loop_result, Groth16ProofRaw, MPCheckHint{curve_id.name}}};
use garaga::definitions::{{G1Point, G1G2Pair}};
use garaga::groth16::{{multi_pairing_check_{curve_id.name.lower()}_3P_2F_with_extra_miller_loop_result}};
use garaga::ec_ops::{{G1PointTrait, G2PointTrait, ec_safe_add}};
use garaga::risc0_utils::compute_receipt_claim;
use garaga::utils::calldata::{{FullProofWithHintsRisc0, deserialize_full_proof_with_hints_risc0}};
use super::{{N_FREE_PUBLIC_INPUTS, vk, ic, precomputed_lines, T}};
const ECIP_OPS_CLASS_HASH: felt252 = {hex(ecip_class_hash)};
Expand All @@ -80,17 +81,6 @@ def gen_risc0_groth16_verifier(
#[storage]
struct Storage {{}}
#[derive(Serde, Drop)]
struct FullProofWithHints {{
groth16_proof: Groth16ProofRaw,
image_id: Span<u32>,
journal_digest: Span<u32>,
mpcheck_hint: MPCheckHintBN254,
small_Q: E12DMulQuotient,
msm_hint: Array<felt252>,
}}
#[abi(embed_v0)]
impl IRisc0Groth16Verifier{curve_id.name} of super::IRisc0Groth16Verifier{curve_id.name}<ContractState> {{
fn verify_groth16_proof_{curve_id.name.lower()}(
Expand All @@ -99,8 +89,7 @@ def gen_risc0_groth16_verifier(
) -> bool {{
// DO NOT EDIT THIS FUNCTION UNLESS YOU KNOW WHAT YOU ARE DOING.
// ONLY EDIT THE process_public_inputs FUNCTION BELOW.
let mut full_proof_with_hints = full_proof_with_hints;
let fph = Serde::<FullProofWithHints>::deserialize(ref full_proof_with_hints).unwrap();
let fph = deserialize_full_proof_with_hints_risc0(full_proof_with_hints);
let groth16_proof = fph.groth16_proof;
let image_id = fph.image_id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ def write_all_tests():
multi_pairing_check_bn254_2P_2F,
multi_pairing_check_bls12_381_2P_2F,
u384,
E12DMulQuotient,
MPCheckHintBN254,
MPCheckHintBLS12_381,
u288,
};
use garaga::groth16::{
E12DMulQuotient,
multi_pairing_check_bn254_3P_2F_with_extra_miller_loop_result,
multi_pairing_check_bls12_381_3P_2F_with_extra_miller_loop_result,
};
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "maturin"

[project]
name = "garaga"
version = "0.13.2.3"
version = "0.13.3"
requires-python = ">=3.10,<3.11"
dependencies = [
"fastecdsa",
Expand Down
22 changes: 5 additions & 17 deletions src/contracts/groth16_example_bls12_381/src/groth16_verifier.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,27 @@ trait IGroth16VerifierBLS12_381<TContractState> {
#[starknet::contract]
mod Groth16VerifierBLS12_381 {
use starknet::SyscallResultTrait;
use garaga::definitions::{G1Point, G1G2Pair, E12DMulQuotient};
use garaga::groth16::{
multi_pairing_check_bls12_381_3P_2F_with_extra_miller_loop_result, Groth16Proof,
MPCheckHintBLS12_381
};
use garaga::definitions::{G1Point, G1G2Pair};
use garaga::groth16::{multi_pairing_check_bls12_381_3P_2F_with_extra_miller_loop_result};
use garaga::ec_ops::{G1PointTrait, G2PointTrait, ec_safe_add};
use garaga::utils::calldata::{deserialize_full_proof_with_hints_bls12_381};
use super::{N_PUBLIC_INPUTS, vk, ic, precomputed_lines};

const ECIP_OPS_CLASS_HASH: felt252 =
0x3b0507836fc39065c529306331041bb8460d6802974f52463ac761e458983e7;
0x7918f484291eb154e13d0e43ba6403e62dc1f5fbb3a191d868e2e37359f8713;
use starknet::ContractAddress;

#[storage]
struct Storage {}

#[derive(Drop, Serde)]
struct FullProofWithHints {
groth16_proof: Groth16Proof,
mpcheck_hint: MPCheckHintBLS12_381,
small_Q: E12DMulQuotient,
msm_hint: Array<felt252>,
}

#[abi(embed_v0)]
impl IGroth16VerifierBLS12_381 of super::IGroth16VerifierBLS12_381<ContractState> {
fn verify_groth16_proof_bls12_381(
ref self: ContractState, full_proof_with_hints: Span<felt252>,
) -> bool {
// DO NOT EDIT THIS FUNCTION UNLESS YOU KNOW WHAT YOU ARE DOING.
// ONLY EDIT THE process_public_inputs FUNCTION BELOW.
let mut full_proof_with_hints = full_proof_with_hints;
let fph = Serde::<FullProofWithHints>::deserialize(ref full_proof_with_hints)
.expect('unwr_full_proof_with_hints');
let fph = deserialize_full_proof_with_hints_bls12_381(full_proof_with_hints);
let groth16_proof = fph.groth16_proof;
let mpcheck_hint = fph.mpcheck_hint;
let small_Q = fph.small_Q;
Expand Down
22 changes: 5 additions & 17 deletions src/contracts/groth16_example_bn254/src/groth16_verifier.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,27 @@ trait IGroth16VerifierBN254<TContractState> {
#[starknet::contract]
mod Groth16VerifierBN254 {
use starknet::SyscallResultTrait;
use garaga::definitions::{G1Point, G1G2Pair, E12DMulQuotient};
use garaga::groth16::{
multi_pairing_check_bn254_3P_2F_with_extra_miller_loop_result, Groth16Proof,
MPCheckHintBN254
};
use garaga::definitions::{G1Point, G1G2Pair};
use garaga::groth16::{multi_pairing_check_bn254_3P_2F_with_extra_miller_loop_result};
use garaga::ec_ops::{G1PointTrait, G2PointTrait, ec_safe_add};
use garaga::utils::calldata::{deserialize_full_proof_with_hints_bn254};
use super::{N_PUBLIC_INPUTS, vk, ic, precomputed_lines};

const ECIP_OPS_CLASS_HASH: felt252 =
0x3b0507836fc39065c529306331041bb8460d6802974f52463ac761e458983e7;
0x7918f484291eb154e13d0e43ba6403e62dc1f5fbb3a191d868e2e37359f8713;
use starknet::ContractAddress;

#[storage]
struct Storage {}

#[derive(Drop, Serde)]
struct FullProofWithHints {
groth16_proof: Groth16Proof,
mpcheck_hint: MPCheckHintBN254,
small_Q: E12DMulQuotient,
msm_hint: Array<felt252>,
}

#[abi(embed_v0)]
impl IGroth16VerifierBN254 of super::IGroth16VerifierBN254<ContractState> {
fn verify_groth16_proof_bn254(
ref self: ContractState, full_proof_with_hints: Span<felt252>,
) -> bool {
// DO NOT EDIT THIS FUNCTION UNLESS YOU KNOW WHAT YOU ARE DOING.
// ONLY EDIT THE process_public_inputs FUNCTION BELOW.
let mut full_proof_with_hints = full_proof_with_hints;
let fph = Serde::<FullProofWithHints>::deserialize(ref full_proof_with_hints)
.expect('unwr_full_proof_with_hints');
let fph = deserialize_full_proof_with_hints_bn254(full_proof_with_hints);
let groth16_proof = fph.groth16_proof;
let mpcheck_hint = fph.mpcheck_hint;
let small_Q = fph.small_Q;
Expand Down
24 changes: 5 additions & 19 deletions src/contracts/risc0_verifier_bn254/src/groth16_verifier.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,28 @@ trait IRisc0Groth16VerifierBN254<TContractState> {
#[starknet::contract]
mod Risc0Groth16VerifierBN254 {
use starknet::SyscallResultTrait;
use garaga::definitions::{G1Point, G1G2Pair, E12DMulQuotient};
use garaga::groth16::{
multi_pairing_check_bn254_3P_2F_with_extra_miller_loop_result, Groth16ProofRaw,
MPCheckHintBN254
};
use garaga::definitions::{G1Point, G1G2Pair};
use garaga::groth16::{multi_pairing_check_bn254_3P_2F_with_extra_miller_loop_result};
use garaga::ec_ops::{G1PointTrait, G2PointTrait, ec_safe_add};
use garaga::risc0_utils::compute_receipt_claim;
use garaga::utils::calldata::{FullProofWithHintsRisc0, deserialize_full_proof_with_hints_risc0};
use super::{N_FREE_PUBLIC_INPUTS, vk, ic, precomputed_lines, T};

const ECIP_OPS_CLASS_HASH: felt252 =
0x3b0507836fc39065c529306331041bb8460d6802974f52463ac761e458983e7;
0x7918f484291eb154e13d0e43ba6403e62dc1f5fbb3a191d868e2e37359f8713;
use starknet::ContractAddress;

#[storage]
struct Storage {}


#[derive(Serde, Drop)]
struct FullProofWithHints {
groth16_proof: Groth16ProofRaw,
image_id: Span<u32>,
journal_digest: Span<u32>,
mpcheck_hint: MPCheckHintBN254,
small_Q: E12DMulQuotient,
msm_hint: Array<felt252>,
}

#[abi(embed_v0)]
impl IRisc0Groth16VerifierBN254 of super::IRisc0Groth16VerifierBN254<ContractState> {
fn verify_groth16_proof_bn254(
ref self: ContractState, full_proof_with_hints: Span<felt252>,
) -> bool {
// DO NOT EDIT THIS FUNCTION UNLESS YOU KNOW WHAT YOU ARE DOING.
// ONLY EDIT THE process_public_inputs FUNCTION BELOW.
let mut full_proof_with_hints = full_proof_with_hints;
let fph = Serde::<FullProofWithHints>::deserialize(ref full_proof_with_hints).unwrap();
let fph = deserialize_full_proof_with_hints_risc0(full_proof_with_hints);

let groth16_proof = fph.groth16_proof;
let image_id = fph.image_id;
Expand Down
3 changes: 2 additions & 1 deletion src/src/basic_field_ops.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn neg_mod_p(a: u384, p: u384) -> u384 {

return outputs.get_output(neg);
}

#[inline(always)]
fn compute_yInvXnegOverY_BN254(x: u384, y: u384) -> (u384, u384) {
let in1 = CircuitElement::<CircuitInput<0>> {};
let in2 = CircuitElement::<CircuitInput<1>> {};
Expand All @@ -53,6 +53,7 @@ fn compute_yInvXnegOverY_BN254(x: u384, y: u384) -> (u384, u384) {
return (outputs.get_output(yInv), outputs.get_output(xNegOverY));
}

#[inline(always)]
fn compute_yInvXnegOverY_BLS12_381(x: u384, y: u384) -> (u384, u384) {
let in1 = CircuitElement::<CircuitInput<0>> {};
let in2 = CircuitElement::<CircuitInput<1>> {};
Expand Down
1 change: 1 addition & 0 deletions src/src/circuits/dummy.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use garaga::definitions::{
use garaga::ec_ops::{SlopeInterceptOutput, FunctionFeltEvaluations, FunctionFelt};
use core::option::Option;

#[inline(always)]
fn run_DUMMY_circuit(mut input: Array<u384>, curve_index: usize) -> Array<u384> {
// INPUT stack
let (in0, in1) = (CE::<CI<0>> {}, CE::<CI<1>> {});
Expand Down
Loading

0 comments on commit c80fb9b

Please sign in to comment.