garaga
  • Garaga documentation
  • Installation
    • Python package
    • Rust Crate
    • Npm package
  • Developer setup & guides
    • Working with auto-generated Cairo Code
    • garaga-rs crate
      • Rust -> Python bindings
      • Rust -> Wasm bidings
  • Using garaga libraries in your Cairo project
    • ECDSA & Schnorr Signatures
    • Hashing functions
  • Deploy your own SNARK verifier on Starknet
    • Groth16
      • Generate and deploy your verifier contract
      • Generating calldata from a proof and using your deployed contract
        • Using Python/Garaga CLI
        • Using Rust
        • Using Typescript
    • Noir
  • Maintained Smart Contracts
    • RiscZero
    • SP1
    • Drand
  • Support
  • Updating these docs
Powered by GitBook
On this page

Was this helpful?

  1. Using garaga libraries in your Cairo project

Hashing functions

PreviousECDSA & Schnorr SignaturesNextDeploy your own SNARK verifier on Starknet

Last updated 3 months ago

Was this helpful?

Poseidon(x,y)=z for BN254 scalar field

This hash function is both compatible with (nInputs=2) and (std::hash::poseidon::hash::hash_2)

Below we show Noir and Cairo code computing the same hash.

main.nr
use std::hash::poseidon;

fn main(x: Field, y: pub Field) {
    let x1 = [x, y];
    let z = poseidon::bn254::hash_2(x1);
    assert(z == 7853200120776062878684798364095072458815029376092732009249414926327459813530);
}

#[test]
fn test_main() {
    main(1, 2);

    // Uncomment to make test fail
    // main(1, 1);
}
main.cairo
use garaga::hashes::poseidon_hash_2_bn254;

fn test_poseidon_bn254() {
    let x: u384 = u384 { limb0: 1, limb1: 0, limb2: 0, limb3: 0 };
    let y: u384 = u384 { limb0: 2, limb1: 0, limb2: 0, limb3: 0 };
    let z: u384 =
    7853200120776062878684798364095072458815029376092732009249414926327459813530_u256
    .into();

    assert_eq!(poseidon_hash_2_bn254(x, y), z);
}
Circom poseidon implementation
Noir Poseidon hash