From bff7f33916838b74581a692f7de7e5513d6151df Mon Sep 17 00:00:00 2001 From: arnaucube Date: Thu, 27 Aug 2020 15:05:36 +0200 Subject: [PATCH] Add poseidon-rs Fr from string & hex examples --- poseidon-rs-examples/.gitignore | 2 ++ poseidon-rs-examples/Cargo.toml | 12 +++++++++ poseidon-rs-examples/src/lib.rs | 47 +++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 poseidon-rs-examples/.gitignore create mode 100644 poseidon-rs-examples/Cargo.toml create mode 100644 poseidon-rs-examples/src/lib.rs diff --git a/poseidon-rs-examples/.gitignore b/poseidon-rs-examples/.gitignore new file mode 100644 index 0000000..96ef6c0 --- /dev/null +++ b/poseidon-rs-examples/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/poseidon-rs-examples/Cargo.toml b/poseidon-rs-examples/Cargo.toml new file mode 100644 index 0000000..22f73be --- /dev/null +++ b/poseidon-rs-examples/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "poseidon-rs-examples" +version = "0.0.1" +authors = ["arnaucube "] +edition = "2018" + +[dependencies] +ff = {package="ff_ce" , version="0.11", features = ["derive"]} +poseidon-rs = "0.0.4" +rustc-hex = "1.0.0" +num = "0.2.0" +num-bigint = "0.2.2" diff --git a/poseidon-rs-examples/src/lib.rs b/poseidon-rs-examples/src/lib.rs new file mode 100644 index 0000000..358bae7 --- /dev/null +++ b/poseidon-rs-examples/src/lib.rs @@ -0,0 +1,47 @@ +#[cfg(test)] +mod tests { + use ff::PrimeField; + use poseidon_rs::{Fr, Poseidon}; + + // for parsing hex + extern crate num; + extern crate num_bigint; + use num_bigint::BigInt; + + #[test] + fn test_usage() { + let v: Fr = Fr::from_str( + "11043376183861534927536506085090418075369306574649619885724436265926427398571", + ) + .unwrap(); + let mut to_hash: Vec = Vec::new(); + to_hash.push(v); + + let poseidon = Poseidon::new(); + let h = poseidon.hash(to_hash).unwrap(); + assert_eq!( + h.to_string(), + "Fr(0x28410c403c92a9f18d1f27b22218b3649b3be8640dc160ad53bd21cf02f98d81)" + ); + } + + #[test] + fn test_usage_hex() { + let b: BigInt = BigInt::parse_bytes( + b"186a5454a7c47c73dfc74ac32ea40a57d27eeb4e2bfc6551dd7b66686d3fd1ab", // same value than in previous test, but in hex + 16, + ) + .unwrap(); + + let v: Fr = Fr::from_str(&b.to_string()).unwrap(); + let mut to_hash: Vec = Vec::new(); + to_hash.push(v); + + let poseidon = Poseidon::new(); + let h = poseidon.hash(to_hash).unwrap(); + assert_eq!( + h.to_string(), + "Fr(0x28410c403c92a9f18d1f27b22218b3649b3be8640dc160ad53bd21cf02f98d81)" + ); + } +}