arnaucube
arnaucube/kestoPublic Self-hosted repository
kesto/poseidon-rs-examples/src/lib.rs
poseidon-rs-examples/src/lib.rsRaw
 1#[cfg(test)]
 2mod tests {
 3    use ff::PrimeField;
 4    use poseidon_rs::{Fr, Poseidon};
 5
 6    // for parsing hex
 7    extern crate num;
 8    extern crate num_bigint;
 9    use num_bigint::BigInt;
10
11    #[test]
12    fn test_usage() {
13        let v: Fr = Fr::from_str(
14            "11043376183861534927536506085090418075369306574649619885724436265926427398571",
15        )
16        .unwrap();
17        let mut to_hash: Vec<Fr> = Vec::new();
18        to_hash.push(v);
19
20        let poseidon = Poseidon::new();
21        let h = poseidon.hash(to_hash).unwrap();
22        assert_eq!(
23            h.to_string(),
24            "Fr(0x28410c403c92a9f18d1f27b22218b3649b3be8640dc160ad53bd21cf02f98d81)"
25        );
26    }
27
28    #[test]
29    fn test_usage_hex() {
30        let b: BigInt = BigInt::parse_bytes(
31            b"186a5454a7c47c73dfc74ac32ea40a57d27eeb4e2bfc6551dd7b66686d3fd1ab", // same value than in previous test, but in hex
32            16,
33        )
34        .unwrap();
35
36        let v: Fr = Fr::from_str(&b.to_string()).unwrap();
37        let mut to_hash: Vec<Fr> = Vec::new();
38        to_hash.push(v);
39
40        let poseidon = Poseidon::new();
41        let h = poseidon.hash(to_hash).unwrap();
42        assert_eq!(
43            h.to_string(),
44            "Fr(0x28410c403c92a9f18d1f27b22218b3649b3be8640dc160ad53bd21cf02f98d81)"
45        );
46    }
47
48    #[test]
49    fn test_usage_bytes() {
50        let msg = "hello";
51        let b: BigInt = BigInt::parse_bytes(msg.as_bytes(), 10).unwrap();
52        let v: Fr = Fr::from_str(&b.to_string()).unwrap();
53
54        let mut to_hash: Vec<Fr> = Vec::new();
55        to_hash.push(v);
56
57        let poseidon = Poseidon::new();
58        let h = poseidon.hash(to_hash).unwrap();
59        assert_eq!(
60            h.to_string(),
61            "Fr(0x28410c403c92a9f18d1f27b22218b3649b3be8640dc160ad53bd21cf02f98d81)"
62        );
63    }
64}