Skip to content

Commit

Permalink
test: rust-reference-impls: Refactor reference impls into directory
Browse files Browse the repository at this point in the history
  • Loading branch information
joeykraut committed Jan 28, 2025
1 parent 53199dc commit 604158d
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 8 deletions.
36 changes: 36 additions & 0 deletions test/Merkle.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,42 @@ contract MerkleTest is TestUtils {
uint256 result = merklePoseidon.hashMerkle(input, idx, sisterLeaves);
console.log("result:", result);
}

/// @dev Helper to run the reference implementation
function runReferenceImpl(uint256 input, uint256 idx, uint256[] memory sisterLeaves) internal returns (uint256) {
string[] memory args = new string[](4);
args[0] = "./test/rust-reference-impls/target/debug/merkle";
args[1] = vm.toString(input);
args[2] = vm.toString(idx);
args[3] = arrayToString(sisterLeaves);

bytes memory res = vm.ffi(args);
string memory str = string(res);

require(
bytes(str).length > 4 && bytes(str)[0] == "R" && bytes(str)[1] == "E" && bytes(str)[2] == "S"
&& bytes(str)[3] == ":",
"Invalid output format"
);

bytes memory hexBytes = new bytes(bytes(str).length - 4);
for (uint256 i = 4; i < bytes(str).length; i++) {
hexBytes[i - 4] = bytes(str)[i];
}
return vm.parseUint(string(hexBytes));
}

function arrayToString(uint256[] memory arr) internal pure returns (string memory) {
string memory result = "[";
for (uint256 i = 0; i < arr.length; i++) {
if (i > 0) {
result = string(abi.encodePacked(result, ","));
}
result = string(abi.encodePacked(result, vm.toString(arr[i])));
}
result = string(abi.encodePacked(result, "]"));
return result;
}
}

interface MerklePoseidon {
Expand Down
4 changes: 2 additions & 2 deletions test/Poseidon.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ contract PoseidonTest is TestUtils {
compileInputs[1] = "build";
compileInputs[2] = "--quiet";
compileInputs[3] = "--manifest-path";
compileInputs[4] = "test/poseidon-reference-implementation/Cargo.toml";
compileInputs[4] = "test/rust-reference-impls/poseidon/Cargo.toml";
vm.ffi(compileInputs);

// Now run the binary directly from target/debug
string[] memory runInputs = new string[](3);
runInputs[0] = "./test/poseidon-reference-implementation/target/debug/poseidon-reference-implementation";
runInputs[0] = "./test/rust-reference-impls/target/debug/poseidon";
runInputs[1] = vm.toString(a);
runInputs[2] = vm.toString(b);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
[package]
name = "poseidon-reference-implementation"
version = "0.1.0"
edition = "2021"
[workspace]
members = ["poseidon", "merkle"]

[dependencies]
[workspace.dependencies]
renegade-constants = { package = "constants", git = "https://github.com/renegade-fi/renegade.git", default-features = false }
renegade-crypto = { git = "https://github.com/renegade-fi/renegade.git" }

num-bigint = "0.4"
9 changes: 9 additions & 0 deletions test/rust-reference-impls/merkle/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "merkle"
version = "0.1.0"
edition = "2021"

[dependencies]
renegade-constants.workspace = true
renegade-crypto.workspace = true
num-bigint.workspace = true
42 changes: 42 additions & 0 deletions test/rust-reference-impls/merkle/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use renegade_constants::Scalar;
use renegade_crypto::fields::scalar_to_biguint;
use renegade_crypto::hash::Poseidon2Sponge;

fn main() {
let args: Vec<String> = std::env::args().collect();
if args.len() < 4 {
eprintln!("Usage: {} <input> <idx> <sister_leaves>", args[0]);
std::process::exit(1);
}

let input = Scalar::from_decimal_string(&args[1]).unwrap();
let idx = args[2].parse::<u64>().unwrap();
let sister_leaves: Vec<Scalar> = args[3]
.trim_matches(|c| c == '[' || c == ']')
.split(',')
.map(|s| Scalar::from_decimal_string(s).unwrap())
.collect();

let result = hash_merkle(input, idx, &sister_leaves);
let res_biguint = scalar_to_biguint(&result);
let res_hex = format!("{res_biguint:x}");
println!("RES:0x{}", res_hex);
}

fn hash_merkle(input: Scalar, idx: u64, sister_leaves: &[Scalar]) -> Scalar {
let mut current = input;
let mut current_idx = idx;
let mut sponge = Poseidon2Sponge::new();

for sister in sister_leaves {
let inputs = if current_idx % 2 == 0 {
[current.inner(), sister.inner()]
} else {
[sister.inner(), current.inner()]
};
current = Scalar::new(sponge.hash(&inputs));
current_idx /= 2;
}

current
}
9 changes: 9 additions & 0 deletions test/rust-reference-impls/poseidon/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "poseidon"
version = "0.1.0"
edition = "2021"

[dependencies]
renegade-constants.workspace = true
renegade-crypto.workspace = true
num-bigint.workspace = true

0 comments on commit 604158d

Please sign in to comment.