mirror of
https://github.com/vacp2p/semaphore-rs.git
synced 2025-02-24 01:28:28 +00:00
Implement conversions
This commit is contained in:
parent
adde0a558d
commit
a7108109fc
@ -53,6 +53,8 @@ ethers-core = { git = "https://github.com/gakonst/ethers-rs", default-features =
|
||||
|
||||
[dev-dependencies]
|
||||
pretty_assertions = "1.0"
|
||||
rand_chacha = "0.3.1"
|
||||
rand_core = "0.6.3"
|
||||
serde_json = "1.0.79"
|
||||
tempfile = "3.0"
|
||||
tiny-keccak = "2.0.2"
|
||||
|
@ -16,13 +16,12 @@ pub mod mimc_hash;
|
||||
#[cfg(feature = "mimc")]
|
||||
pub mod mimc_tree;
|
||||
|
||||
use ark_bn254::{FrParameters, Parameters};
|
||||
use ark_bn254::{Fr, FrParameters, Parameters};
|
||||
use ark_ec::bn::Bn;
|
||||
use ark_ff::Fp256;
|
||||
|
||||
pub use crate::posseidon_hash::posseidon_hash;
|
||||
|
||||
pub type Field = Fp256<FrParameters>;
|
||||
pub type Field = Fr;
|
||||
pub type Groth16Proof = ark_groth16::Proof<Bn<Parameters>>;
|
||||
pub type EthereumGroth16Proof = ark_circom::ethereum::Proof;
|
||||
|
||||
|
@ -3,7 +3,7 @@ use crate::{
|
||||
merkle_tree::{self, Hasher, MerkleTree},
|
||||
posseidon_hash, Field,
|
||||
};
|
||||
use ark_ff::PrimeField;
|
||||
use ark_ff::{PrimeField, ToBytes};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[allow(dead_code)]
|
||||
@ -33,7 +33,12 @@ impl From<Hash> for Field {
|
||||
#[allow(clippy::fallible_impl_from)] // TODO
|
||||
impl From<Field> for Hash {
|
||||
fn from(n: Field) -> Self {
|
||||
todo!()
|
||||
let mut bytes = [0_u8; 32];
|
||||
n.into_repr()
|
||||
.write(&mut bytes[..])
|
||||
.expect("write should succeed");
|
||||
bytes.reverse(); // Convert to big endian
|
||||
Self(bytes)
|
||||
}
|
||||
}
|
||||
|
||||
@ -48,7 +53,22 @@ impl Hasher for PoseidonHash {
|
||||
#[cfg(test)]
|
||||
pub mod test {
|
||||
use super::*;
|
||||
use ark_ff::UniformRand;
|
||||
use hex_literal::hex;
|
||||
use rand_chacha::ChaChaRng;
|
||||
use rand_core::SeedableRng;
|
||||
|
||||
#[test]
|
||||
fn test_ark_hash_ark_roundtrip() {
|
||||
use ark_ff::One;
|
||||
let mut rng = ChaChaRng::seed_from_u64(123);
|
||||
for i in 0..1000 {
|
||||
let n = Field::rand(&mut rng);
|
||||
let n = Field::one();
|
||||
let m = Hash::from(n).into();
|
||||
assert_eq!(n, m);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tree_4() {
|
||||
|
@ -3,7 +3,8 @@ use crate::{
|
||||
merkle_tree::{self, Hasher, MerkleTree},
|
||||
Field,
|
||||
};
|
||||
use ff::{PrimeField, PrimeFieldRepr};
|
||||
use ark_ff::{BigInteger256, PrimeField as _};
|
||||
use ff::{PrimeField as _, PrimeFieldRepr as _};
|
||||
use once_cell::sync::Lazy;
|
||||
use poseidon_rs::{Fr, FrRepr, Poseidon};
|
||||
use serde::{Deserialize, Serialize};
|
||||
@ -11,11 +12,11 @@ use serde::{Deserialize, Serialize};
|
||||
static POSEIDON: Lazy<Poseidon> = Lazy::new(Poseidon::new);
|
||||
|
||||
fn ark_to_posseidon(n: Field) -> Fr {
|
||||
todo!()
|
||||
Fr::from_repr(FrRepr(n.into_repr().0)).expect("n is a valid field element")
|
||||
}
|
||||
|
||||
fn posseidon_to_ark(n: Fr) -> Field {
|
||||
todo!()
|
||||
Field::from_repr(BigInteger256(n.into_repr().0)).expect("n is a valid field element")
|
||||
}
|
||||
|
||||
pub fn posseidon_hash(input: &[Field]) -> Field {
|
||||
@ -33,14 +34,26 @@ pub fn posseidon_hash(input: &[Field]) -> Field {
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use ff::{Field, PrimeField, PrimeFieldRepr};
|
||||
use super::{ark_to_posseidon, posseidon_to_ark};
|
||||
use crate::Field;
|
||||
use ark_ff::{Field as _, UniformRand};
|
||||
use ff::{Field as _, PrimeField, PrimeFieldRepr};
|
||||
use poseidon_rs::Fr;
|
||||
use rand_chacha::ChaChaRng;
|
||||
use rand_core::SeedableRng;
|
||||
|
||||
#[test]
|
||||
fn test_modulus_identical() {
|
||||
let mut modulus = [0_u8; 32];
|
||||
let writer = Fr::char().write_be(&mut modulus[..]).unwrap();
|
||||
assert_eq!(Fr::char().0, Field::characteristic());
|
||||
}
|
||||
|
||||
todo!()
|
||||
#[test]
|
||||
fn test_ark_pos_ark_roundtrip() {
|
||||
let mut rng = ChaChaRng::seed_from_u64(123);
|
||||
for i in 0..1000 {
|
||||
let n = Field::rand(&mut rng);
|
||||
let m = posseidon_to_ark(ark_to_posseidon(n));
|
||||
assert_eq!(n, m)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user