Skip to content

Commit

Permalink
Adds poseidon_hash_bn254(x, y) in garaga_rs with Js and Python bindin…
Browse files Browse the repository at this point in the history
…gs. (#307)
  • Loading branch information
feltroidprime authored Feb 21, 2025
1 parent d23e117 commit 939e3ff
Show file tree
Hide file tree
Showing 17 changed files with 815 additions and 19 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ ci-cairo:
ci-wasm:
./tools/make/ci_wasm.sh

wasm:
./tools/make/wasm.sh
clean:
rm -rf build/compiled_cairo_files
mkdir -p build
Expand Down
30 changes: 21 additions & 9 deletions hydra/garaga/precompiled_circuits/poseidon_bn254.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from garaga.definitions import CurveID, get_base_field
from garaga.modulo_circuit import ModuloCircuit
from garaga.modulo_circuit import ModuloCircuit, ModuloCircuitElement
from garaga.modulo_circuit_structs import u384

# Global circuit definition
Expand Down Expand Up @@ -73,8 +73,19 @@ def mix_s(t, S, r, state):
return result


def poseidon_hash(x, y):
def poseidon_hash(
x: ModuloCircuitElement, y: ModuloCircuitElement
) -> ModuloCircuitElement:
"""Computes the Poseidon hash for two inputs x and y."""
if isinstance(x, int):
x = circuit.write_element(x)
if isinstance(y, int):
y = circuit.write_element(y)
if not isinstance(x, ModuloCircuitElement) or not isinstance(
y, ModuloCircuitElement
):
raise ValueError("x and y must be ModuloCircuitElement instances")

t = 3 # 2 inputs + 1 state
n_rounds_f = 8 # Full rounds
n_rounds_p = 57 # Partial rounds
Expand Down Expand Up @@ -532,12 +543,13 @@ def poseidon_hash(x, y):
return mix_last(t, POSEIDON_M, 0, state)


# Compute the Poseidon hash
z = poseidon_hash(x, y)
if __name__ == "__main__":
# Compute the Poseidon hash
z = poseidon_hash(x, y)

# Define the output of the circuit
circuit.extend_struct_output(u384("z", [z]))
# Define the output of the circuit
circuit.extend_struct_output(u384("z", [z]))

# Compile and print the compiled circuit
compiled_code, function_name = circuit.compile_circuit()
print(compiled_code)
# Compile and print the compiled circuit
compiled_code, function_name = circuit.compile_circuit()
print(compiled_code)
43 changes: 43 additions & 0 deletions tests/hydra/test_poseidon_hash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import time

import pytest

from garaga import garaga_rs
from garaga.precompiled_circuits.poseidon_bn254 import poseidon_hash


def test_poseidon_hash_bn254():
# Test with valid inputs
x = 1
y = 2
expected = 0x115CC0F5E7D690413DF64C6B9662E9CF2A3617F2743245519E19607A4417189A

tr0 = time.time()
for _ in range(20):
result = garaga_rs.poseidon_hash_bn254(x, y)
tr1 = time.time()
print(f"Garaga RS time: {tr1 - tr0} seconds")
assert result == expected

tp0 = time.time()
for _ in range(20):
result = poseidon_hash(x, y)
tp1 = time.time()

print(f"Garaga Python time: {tp1 - tp0} seconds")
assert result.value == expected

# Test different inputs give different results
result2 = garaga_rs.poseidon_hash_bn254(2, 1)
assert result != result2

# Test with large numbers
x_large = 2**255 - 1
y_large = 2**255 - 2
result = garaga_rs.poseidon_hash_bn254(x_large, y_large)
assert isinstance(result, int)
assert len(hex(result)[2:]) <= 64 # Result should be within field size

# Test invalid inputs
with pytest.raises(OverflowError):
garaga_rs.poseidon_hash_bn254(-1, 1) # Negative numbers not allowed
7 changes: 7 additions & 0 deletions tools/garaga_rs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tools/garaga_rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ hex = "0.4"
sha2 = "0.10"
sha3 = "0.10"
js-sys = "0.3"
lazy_static = "1.4"
starknet-types-core = { version = "0.1.7", default-features = false, features = ["curve"] }
lambdaworks-crypto = { git = "https://github.com/lambdaclass/lambdaworks.git" }
lambdaworks-math = { git = "https://github.com/lambdaclass/lambdaworks.git" }
2 changes: 2 additions & 0 deletions tools/garaga_rs/src/crypto/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[allow(dead_code)]
pub mod poseidon_bn254;
Loading

0 comments on commit 939e3ff

Please sign in to comment.