Skip to content

Commit 939e3ff

Browse files
Adds poseidon_hash_bn254(x, y) in garaga_rs with Js and Python bindings. (#307)
1 parent d23e117 commit 939e3ff

File tree

17 files changed

+815
-19
lines changed

17 files changed

+815
-19
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ ci-cairo:
2727
ci-wasm:
2828
./tools/make/ci_wasm.sh
2929

30+
wasm:
31+
./tools/make/wasm.sh
3032
clean:
3133
rm -rf build/compiled_cairo_files
3234
mkdir -p build

hydra/garaga/precompiled_circuits/poseidon_bn254.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from garaga.definitions import CurveID, get_base_field
2-
from garaga.modulo_circuit import ModuloCircuit
2+
from garaga.modulo_circuit import ModuloCircuit, ModuloCircuitElement
33
from garaga.modulo_circuit_structs import u384
44

55
# Global circuit definition
@@ -73,8 +73,19 @@ def mix_s(t, S, r, state):
7373
return result
7474

7575

76-
def poseidon_hash(x, y):
76+
def poseidon_hash(
77+
x: ModuloCircuitElement, y: ModuloCircuitElement
78+
) -> ModuloCircuitElement:
7779
"""Computes the Poseidon hash for two inputs x and y."""
80+
if isinstance(x, int):
81+
x = circuit.write_element(x)
82+
if isinstance(y, int):
83+
y = circuit.write_element(y)
84+
if not isinstance(x, ModuloCircuitElement) or not isinstance(
85+
y, ModuloCircuitElement
86+
):
87+
raise ValueError("x and y must be ModuloCircuitElement instances")
88+
7889
t = 3 # 2 inputs + 1 state
7990
n_rounds_f = 8 # Full rounds
8091
n_rounds_p = 57 # Partial rounds
@@ -532,12 +543,13 @@ def poseidon_hash(x, y):
532543
return mix_last(t, POSEIDON_M, 0, state)
533544

534545

535-
# Compute the Poseidon hash
536-
z = poseidon_hash(x, y)
546+
if __name__ == "__main__":
547+
# Compute the Poseidon hash
548+
z = poseidon_hash(x, y)
537549

538-
# Define the output of the circuit
539-
circuit.extend_struct_output(u384("z", [z]))
550+
# Define the output of the circuit
551+
circuit.extend_struct_output(u384("z", [z]))
540552

541-
# Compile and print the compiled circuit
542-
compiled_code, function_name = circuit.compile_circuit()
543-
print(compiled_code)
553+
# Compile and print the compiled circuit
554+
compiled_code, function_name = circuit.compile_circuit()
555+
print(compiled_code)

tests/hydra/test_poseidon_hash.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import time
2+
3+
import pytest
4+
5+
from garaga import garaga_rs
6+
from garaga.precompiled_circuits.poseidon_bn254 import poseidon_hash
7+
8+
9+
def test_poseidon_hash_bn254():
10+
# Test with valid inputs
11+
x = 1
12+
y = 2
13+
expected = 0x115CC0F5E7D690413DF64C6B9662E9CF2A3617F2743245519E19607A4417189A
14+
15+
tr0 = time.time()
16+
for _ in range(20):
17+
result = garaga_rs.poseidon_hash_bn254(x, y)
18+
tr1 = time.time()
19+
print(f"Garaga RS time: {tr1 - tr0} seconds")
20+
assert result == expected
21+
22+
tp0 = time.time()
23+
for _ in range(20):
24+
result = poseidon_hash(x, y)
25+
tp1 = time.time()
26+
27+
print(f"Garaga Python time: {tp1 - tp0} seconds")
28+
assert result.value == expected
29+
30+
# Test different inputs give different results
31+
result2 = garaga_rs.poseidon_hash_bn254(2, 1)
32+
assert result != result2
33+
34+
# Test with large numbers
35+
x_large = 2**255 - 1
36+
y_large = 2**255 - 2
37+
result = garaga_rs.poseidon_hash_bn254(x_large, y_large)
38+
assert isinstance(result, int)
39+
assert len(hex(result)[2:]) <= 64 # Result should be within field size
40+
41+
# Test invalid inputs
42+
with pytest.raises(OverflowError):
43+
garaga_rs.poseidon_hash_bn254(-1, 1) # Negative numbers not allowed

tools/garaga_rs/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/garaga_rs/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ hex = "0.4"
4545
sha2 = "0.10"
4646
sha3 = "0.10"
4747
js-sys = "0.3"
48+
lazy_static = "1.4"
4849
starknet-types-core = { version = "0.1.7", default-features = false, features = ["curve"] }
4950
lambdaworks-crypto = { git = "https://github.com/lambdaclass/lambdaworks.git" }
5051
lambdaworks-math = { git = "https://github.com/lambdaclass/lambdaworks.git" }

tools/garaga_rs/src/crypto/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#[allow(dead_code)]
2+
pub mod poseidon_bn254;

0 commit comments

Comments
 (0)