Skip to content

Commit

Permalink
Merge branch 'main' of github.com:luiz-lvj/garaga into npm_package
Browse files Browse the repository at this point in the history
  • Loading branch information
luiz-lvj committed Oct 29, 2024
2 parents a94b474 + 017e209 commit 22f8260
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 94 deletions.
52 changes: 52 additions & 0 deletions .github/scripts/verify_contract.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import ast
import asyncio
import sys

from starknet_py.net.full_node_client import FullNodeClient


async def verify_contract():
try:
with open(
"hydra/garaga/starknet/groth16_contract_generator/generator.py", "r"
) as f:
tree = ast.parse(f.read())
for node in ast.walk(tree):
if isinstance(node, ast.Assign) and len(node.targets) == 1:
if getattr(node.targets[0], "id", None) == "ECIP_OPS_CLASS_HASH":
class_hash = hex(node.value.value)
break
print(f"Using class hash: {class_hash}")
except Exception as e:
print(f"Error parsing generator.py: {str(e)}", file=sys.stderr)
sys.exit(1)

print("\nVerifying on Sepolia...")
sepolia = FullNodeClient("https://free-rpc.nethermind.io/sepolia-juno")
try:
sepolia_result = await sepolia.get_class_by_hash(class_hash)
if not sepolia_result:
print("Error: Contract not declared on Sepolia", file=sys.stderr)
sys.exit(1)
print("✓ Contract verified on Sepolia")
except Exception as e:
print(f"Error checking Sepolia: {str(e)}", file=sys.stderr)
sys.exit(1)

print("\nVerifying on Mainnet...")
mainnet = FullNodeClient("https://free-rpc.nethermind.io/mainnet-juno")
try:
mainnet_result = await mainnet.get_class_by_hash(class_hash)
if not mainnet_result:
print("Error: Contract not declared on Mainnet", file=sys.stderr)
sys.exit(1)
print("✓ Contract verified on Mainnet")
except Exception as e:
print(f"Error checking Mainnet: {str(e)}", file=sys.stderr)
sys.exit(1)

print("\n✓ Contract verified on both networks")


if __name__ == "__main__":
asyncio.run(verify_contract())
20 changes: 11 additions & 9 deletions .github/workflows/maturin.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
# This file is autogenerated by maturin v1.7.0
# To update, run
#
# maturin generate-ci github
#
name: CI

on:
push:
branches:
Expand Down Expand Up @@ -125,7 +119,6 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
# Workaround to ensure python 3.10 is available on x86_64
python-version: ${{ matrix.platform.target == 'x86_64' && '3.10' || '3.x' }}
- name: Build wheels
uses: PyO3/maturin-action@v1
Expand Down Expand Up @@ -157,10 +150,19 @@ jobs:
release:
name: Release
runs-on: ubuntu-latest
# Note this will only run when a new tag is pushed
if: "startsWith(github.ref, 'refs/tags/')"
if: "startsWith(github.ref, 'refs/tags/')" # Only run on tag pushes
needs: [linux, musllinux, windows, macos, sdist]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.9'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install "starknet-py>=0.24.0"
- name: Verify Contract Declaration
run: python .github/scripts/verify_contract.py
- uses: actions/download-artifact@v4
- name: Publish to PyPI
uses: PyO3/maturin-action@v1
Expand Down
13 changes: 11 additions & 2 deletions .github/workflows/wasm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,19 @@ jobs:
release:
name: Release
runs-on: ubuntu-latest
# Note this will only run when a new tag is pushed
if: "startsWith(github.ref, 'refs/tags/')"
if: "startsWith(github.ref, 'refs/tags/')" # Only run on tag pushes
needs: [build, test, test-integration]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.9'
- name: Install Python dependencies
run: |
python -m pip install --upgrade pip
python -m pip install "starknet-py>=0.24.0"
- name: Verify Contract Declaration
run: python .github/scripts/verify_contract.py
- uses: actions/setup-node@v4
with:
node-version: 'lts/*'
Expand Down
24 changes: 7 additions & 17 deletions hydra/garaga/starknet/groth16_contract_generator/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def gen_groth16_verifier(
fn verify_groth16_proof_{curve_id.name.lower()}(
ref self: TContractState,
full_proof_with_hints: Span<felt252>,
) -> bool;
) -> Option<Span<u256>>;
}}
#[starknet::contract]
Expand All @@ -88,9 +88,11 @@ def gen_groth16_verifier(
fn verify_groth16_proof_{curve_id.name.lower()}(
ref self: ContractState,
full_proof_with_hints: Span<felt252>,
) -> bool {{
) -> Option<Span<u256>> {{
// DO NOT EDIT THIS FUNCTION UNLESS YOU KNOW WHAT YOU ARE DOING.
// ONLY EDIT THE process_public_inputs FUNCTION BELOW.
// This function returns an Option for the public inputs if the proof is valid.
// If the proof is invalid, the execution will either fail or return None.
// Read the documentation to learn how to generate the full_proof_with_hints array given a proof and a verifying key.
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;
Expand Down Expand Up @@ -140,24 +142,12 @@ def gen_groth16_verifier(
small_Q
);
if check == true {{
self
.process_public_inputs(
starknet::get_caller_address(), groth16_proof.public_inputs
);
return true;
return Option::Some(groth16_proof.public_inputs);
}} else {{
return false;
return Option::None;
}}
}}
}}
#[generate_trait]
impl InternalFunctions of InternalFunctionsTrait {{
fn process_public_inputs(
ref self: ContractState, user: ContractAddress, public_inputs: Span<u256>,
) {{ // Process the public inputs with respect to the caller address (user).
// Update the storage, emit events, call other contracts, etc.
}}
}}
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def gen_risc0_groth16_verifier(
fn verify_groth16_proof_{curve_id.name.lower()}(
ref self: TContractState,
full_proof_with_hints: Span<felt252>,
) -> bool;
) -> Option<Span<u8>>;
}}
#[starknet::contract]
Expand All @@ -76,7 +76,6 @@ def gen_risc0_groth16_verifier(
use super::{{N_FREE_PUBLIC_INPUTS, vk, ic, precomputed_lines, T}};
const ECIP_OPS_CLASS_HASH: felt252 = {hex(ecip_class_hash)};
use starknet::ContractAddress;
#[storage]
struct Storage {{}}
Expand All @@ -86,9 +85,12 @@ def gen_risc0_groth16_verifier(
fn verify_groth16_proof_{curve_id.name.lower()}(
ref self: ContractState,
full_proof_with_hints: Span<felt252>,
) -> bool {{
) -> Option<Span<u8>> {{
// DO NOT EDIT THIS FUNCTION UNLESS YOU KNOW WHAT YOU ARE DOING.
// ONLY EDIT THE process_public_inputs FUNCTION BELOW.
// This function returns an Option for the public inputs if the proof is valid.
// If the proof is invalid, the execution will either fail or return None.
// Read the documentation to learn how to generate the full_proof_with_hints array given a proof and a verifying key.
let fph = deserialize_full_proof_with_hints_risc0(full_proof_with_hints);
let groth16_proof = fph.groth16_proof;
Expand Down Expand Up @@ -143,24 +145,13 @@ def gen_risc0_groth16_verifier(
small_Q
);
if check == true {{
self.process_public_inputs(starknet::get_caller_address(), journal);
return true;
return Option::Some(journal);
}} else {{
return false;
return Option::None;
}}
}}
}}
#[generate_trait]
impl InternalFunctions of InternalFunctionsTrait {{
fn process_public_inputs(
ref self: ContractState, user: ContractAddress, public_inputs: Span<u8>,
) {{ // Process the public inputs with respect to the caller address (user).
// Update the storage, emit events, call other contracts, etc.
}}
}}
}}
"""

create_directory(output_folder_path)
Expand Down
25 changes: 8 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 @@ -4,7 +4,7 @@ use super::groth16_verifier_constants::{N_PUBLIC_INPUTS, vk, ic, precomputed_lin
trait IGroth16VerifierBLS12_381<TContractState> {
fn verify_groth16_proof_bls12_381(
ref self: TContractState, full_proof_with_hints: Span<felt252>,
) -> bool;
) -> Option<Span<u256>>;
}

#[starknet::contract]
Expand All @@ -27,9 +27,12 @@ mod Groth16VerifierBLS12_381 {
impl IGroth16VerifierBLS12_381 of super::IGroth16VerifierBLS12_381<ContractState> {
fn verify_groth16_proof_bls12_381(
ref self: ContractState, full_proof_with_hints: Span<felt252>,
) -> bool {
) -> Option<Span<u256>> {
// DO NOT EDIT THIS FUNCTION UNLESS YOU KNOW WHAT YOU ARE DOING.
// ONLY EDIT THE process_public_inputs FUNCTION BELOW.
// This function returns an Option for the public inputs if the proof is valid.
// If the proof is invalid, the execution will either fail or return None.
// Read the documentation to learn how to generate the full_proof_with_hints array given
// a proof and a verifying key.
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;
Expand Down Expand Up @@ -79,23 +82,11 @@ mod Groth16VerifierBLS12_381 {
small_Q
);
if check == true {
self
.process_public_inputs(
starknet::get_caller_address(), groth16_proof.public_inputs
);
return true;
return Option::Some(groth16_proof.public_inputs);
} else {
return false;
return Option::None;
}
}
}
#[generate_trait]
impl InternalFunctions of InternalFunctionsTrait {
fn process_public_inputs(
ref self: ContractState, user: ContractAddress, public_inputs: Span<u256>,
) { // Process the public inputs with respect to the caller address (user).
// Update the storage, emit events, call other contracts, etc.
}
}
}

25 changes: 8 additions & 17 deletions src/contracts/groth16_example_bn254/src/groth16_verifier.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::groth16_verifier_constants::{N_PUBLIC_INPUTS, vk, ic, precomputed_lin
trait IGroth16VerifierBN254<TContractState> {
fn verify_groth16_proof_bn254(
ref self: TContractState, full_proof_with_hints: Span<felt252>,
) -> bool;
) -> Option<Span<u256>>;
}

#[starknet::contract]
Expand All @@ -27,9 +27,12 @@ mod Groth16VerifierBN254 {
impl IGroth16VerifierBN254 of super::IGroth16VerifierBN254<ContractState> {
fn verify_groth16_proof_bn254(
ref self: ContractState, full_proof_with_hints: Span<felt252>,
) -> bool {
) -> Option<Span<u256>> {
// DO NOT EDIT THIS FUNCTION UNLESS YOU KNOW WHAT YOU ARE DOING.
// ONLY EDIT THE process_public_inputs FUNCTION BELOW.
// This function returns an Option for the public inputs if the proof is valid.
// If the proof is invalid, the execution will either fail or return None.
// Read the documentation to learn how to generate the full_proof_with_hints array given
// a proof and a verifying key.
let fph = deserialize_full_proof_with_hints_bn254(full_proof_with_hints);
let groth16_proof = fph.groth16_proof;
let mpcheck_hint = fph.mpcheck_hint;
Expand Down Expand Up @@ -79,23 +82,11 @@ mod Groth16VerifierBN254 {
small_Q
);
if check == true {
self
.process_public_inputs(
starknet::get_caller_address(), groth16_proof.public_inputs
);
return true;
return Option::Some(groth16_proof.public_inputs);
} else {
return false;
return Option::None;
}
}
}
#[generate_trait]
impl InternalFunctions of InternalFunctionsTrait {
fn process_public_inputs(
ref self: ContractState, user: ContractAddress, public_inputs: Span<u256>,
) { // Process the public inputs with respect to the caller address (user).
// Update the storage, emit events, call other contracts, etc.
}
}
}

24 changes: 9 additions & 15 deletions src/contracts/risc0_verifier_bn254/src/groth16_verifier.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::groth16_verifier_constants::{N_FREE_PUBLIC_INPUTS, vk, ic, precompute
trait IRisc0Groth16VerifierBN254<TContractState> {
fn verify_groth16_proof_bn254(
ref self: TContractState, full_proof_with_hints: Span<felt252>,
) -> bool;
) -> Option<Span<u8>>;
}

#[starknet::contract]
Expand All @@ -19,7 +19,6 @@ mod Risc0Groth16VerifierBN254 {

const ECIP_OPS_CLASS_HASH: felt252 =
0x2672f1f079ccbafe1be4a20a76421b509fcfb406cbf6818563ed812edaeb3a3;
use starknet::ContractAddress;

#[storage]
struct Storage {}
Expand All @@ -28,9 +27,13 @@ mod Risc0Groth16VerifierBN254 {
impl IRisc0Groth16VerifierBN254 of super::IRisc0Groth16VerifierBN254<ContractState> {
fn verify_groth16_proof_bn254(
ref self: ContractState, full_proof_with_hints: Span<felt252>,
) -> bool {
) -> Option<Span<u8>> {
// DO NOT EDIT THIS FUNCTION UNLESS YOU KNOW WHAT YOU ARE DOING.
// ONLY EDIT THE process_public_inputs FUNCTION BELOW.
// This function returns an Option for the public inputs if the proof is valid.
// If the proof is invalid, the execution will either fail or return None.
// Read the documentation to learn how to generate the full_proof_with_hints array given
// a proof and a verifying key.

let fph = deserialize_full_proof_with_hints_risc0(full_proof_with_hints);

let groth16_proof = fph.groth16_proof;
Expand Down Expand Up @@ -85,20 +88,11 @@ mod Risc0Groth16VerifierBN254 {
small_Q
);
if check == true {
self.process_public_inputs(starknet::get_caller_address(), journal);
return true;
return Option::Some(journal);
} else {
return false;
return Option::None;
}
}
}
#[generate_trait]
impl InternalFunctions of InternalFunctionsTrait {
fn process_public_inputs(
ref self: ContractState, user: ContractAddress, public_inputs: Span<u8>,
) { // Process the public inputs with respect to the caller address (user).
// Update the storage, emit events, call other contracts, etc.
}
}
}

0 comments on commit 22f8260

Please sign in to comment.