Garaga

repository·main·Indexed 42 days ago

https://github.com/keep-starknet-strange/garaga

A high-performance elliptic curve and SNARK verification library for Starknet and Cairo. Garaga provides tools for efficient verifiers for curves including BN254, BLS12-381, SECP256K1, SECP256R1, ED25519, and GRUMPKIN. It supports ZK proof verification (Groth16, Honk, Noir, RISC Zero, SP1), signature verification (ECDSA, Schnorr, EdDSA), and cryptographic operations via Python, Cairo, Rust (garaga_rs v1.1.0), and npm packages.

Tokens
70.7K
Snippets
235
Records
299
Agent score
53%

What's inside Garaga

  1. What is Garaga?

    main

    Garaga is a suite of elliptic curve tooling and SNARKs verification for Cairo and Starknet. It provides efficient elliptic curve operations on Starknet by using a dedicated builtin for emulated modular arithmetic and non-deterministic techniques for extension field multiplication, pairings, and multi-scalar multiplication (MSM).

    Supported features include:

    • Scalar & Multi-scalar multiplication (MSM): For any Weierstrass curve (e.g., BN254, BLS12_381, SECP256/R1, ED25519) by specifying curve parameters.
    • Pairing operations: For BN254 and BLS12_381.
    • Groth16 smart contract verifier generators: For BN254 and BLS12_381.
    • Noir smart contract verifier generators: For ultra keccak honk flavour.
  2. Overview of Garaga capabilities

    main

    Garaga provides high-performance cryptographic tooling for Starknet, including:

    • ZK Proof Verification: Groth16, Honk, Noir, RISC Zero, and SP1.
    • Signature Verification: ECDSA, Schnorr, and EdDSA.
    • Elliptic Curve Operations: Multi-scalar multiplication (MSM) and pairing operations for curves like BN254 and BLS12-381.
    • Other Cryptography: Hashing (SHA-512, Poseidon), drand verification, and time-lock encryption.

    Supported curves include: BN254, BLS12-381, SECP256K1, SECP256R1, ED25519, and GRUMPKIN.

  3. Overview of Corelib Imports

    main

    The corelib_imports package provides controlled access to non-public functions, structs, and traits from the Cairo Corelib library via selective re-exports. It is designed to expose performance-critical operations for cryptographic computations, such as optimized bounded integer arithmetic and circuit operations, which are otherwise inaccessible due to Cairo's visibility rules.

    To access specific internal APIs, this package utilizes feature flags (e.g., #[feature("bounded-int-utils")]).

  4. How Time-Lock Encryption (tlock) works with drand

    main

    Time-Lock Encryption (tlock) allows you to encrypt a 16-byte message for a future drand round. The message remains undecryptable until drand publishes the signature for that specific round.

    Workflow:

    1. Off-chain Encryption: Choose a future drand round $N$. Encrypt a 16-byte message using drand's public key and round $N$. This produces a CipherText consisting of a G2Point ($U$) and two 16-byte arrays ($V$ and $W$).
    2. On-chain Decryption: Once drand publishes the signature for round $N$, the signature is used to recover the key via a BLS pairing check ($e(signature, U)$). The original message is then recovered via XOR decryption.

    Important Constraint: tlock only encrypts a 16-byte (128-bit) message. To encrypt larger payloads, you must encrypt a symmetric key using tlock and then use that key to encrypt your larger data off-chain.

  5. How Garaga achieves performance via precomputed hints

    main

    To make complex operations like MSM or signature verification efficient on-chain, Garaga uses a hybrid off-chain/on-chain model:

    1. Off-chain (SDK): Use the Garaga SDK (Python, Rust, or TypeScript) to perform the heavy computation and generate verification hints.
    2. Calldata: Pass the results and the generated hints as calldata to the Starknet transaction.
    3. On-chain (Cairo): The Cairo library deserializes the calldata and verifies that the provided hints are correct, ensuring the computation was performed accurately without re-running the entire expensive operation.
  6. How to use Garaga maintained smart contracts

    main

    Garaga maintains specific smart contracts that are declared on both Starknet Sepolia and Mainnet.

    Key usage rules:

    • Use Library Syscalls: These contracts are declared but not deployed. You must interact with them using library syscalls via their class hashes. Do not attempt to use a contract address, as they do not exist unless you deploy them yourself.
    • Version Matching is Critical: The SDK generates calldata (hints) that are specific to the contract version. You must use the same Garaga SDK version that corresponds to the release of the class hashes you are using. Mismatched versions will cause verification to fail.
    • Source Code: The source code for these contracts is located in the src/contracts directory of the main Garaga repository.
  7. Understand the RISC Zero zkVM project structure

    main

    The template follows a standard directory structure for zkVM applications, separating the Host code (which manages the execution and proof generation) from the Guest code (the code that runs inside the zkVM).

    project_name
    ├── Cargo.toml
    ├── host
    │   ├── Cargo.toml
    │   └── src
    │       └── main.rs                    <-- [Host code goes here]
    └── methods
        ├── Cargo.toml
        ├── build.rs
        ├── guest
        │   ├── Cargo.toml
        │   └── src
        │       └── method_name.rs         <-- [Guest code goes here]
        └── src
            └── lib.rs
  8. How Garaga's architecture works

    main

    Garaga is composed of a Pythonic backend with Rust bindings and CairoZero / Cairo libraries.

    • Pythonic Backend: Defines emulated modular arithmetic circuits (compilable to Cairo or Cairo1), handles witness generation for non-deterministic computations, generates smart contracts for specific proof systems/curves, and generates calldata from proofs.
    • CairoZero / Starknet Interfaces: Composes and calls the circuits and implements additional logic like the Fiat-Shamir heuristic and SNARKs verifier algorithms.
  9. How ECDSA, Schnorr, and EdDSA signatures work in Garaga

    main

    Garaga supports ECDSA, Schnorr, and EdDSA signature schemes. All three follow a pattern where a Cairo struct contains the signature data and hints for efficient verification.

    Key Concepts:

    • Curve Support: All schemes work with all supported elliptic curves in Garaga, except EdDSA which is specific to Ed25519.
    • Verification Pattern: Verification functions take the public key as a separate parameter. This allows you to either hardcode the public key in your contract or provide it dynamically at runtime.
    • Hashing Responsibility: The ECDSA and Schnorr verification functions assume the message hash has already been correctly computed by the caller. They verify the signature equation but do not hash the message themselves.
    • Public Key Handling: When using the SDK to generate calldata, you can control whether the public key is included using the prepend_public_key parameter:
      • prepend_public_key=True (default): The public key is included in the serialized calldata. This is useful for dynamic public keys.
      • prepend_public_key=False: Only the signature is serialized. This is useful when the public key is hardcoded in your contract.
  10. Ensure Version Compatibility across Garaga packages

    main

    All Garaga packages (Python, Cairo, Rust, and npm) are released with matching version numbers. To guarantee compatibility when using multiple Garaga components in a single project, you must ensure they all use the same version number.

    Example for version 1.1.0:

    • Python: pip install garaga==1.1.0
    • Cairo: garaga = "1.1.0" in Scarb.toml
    • Rust: tag = "v1.1.0" in Cargo.toml
    • npm: npm install garaga@1.1.0
  11. Maintain version compatibility across Garaga toolchain

    main

    To ensure calldata is correctly formatted for on-chain verification, you must match SDK versions across your toolchain. All packages (Python, Rust, npm, Cairo) are released with synchronized version numbers.

    • For generated contracts (Groth16, Noir): Use the exact SDK version that was used to generate the verifier contract.
    • For maintained contracts (RISC Zero, SP1, drand): Use the SDK version that matches the Garaga release where those specific contract class hashes were declared.

    Example: If using version 1.1.0, ensure pip install garaga==1.1.0 and garaga = "1.1.0" in Scarb.toml are used together.