Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update verifier logic #615

Merged
merged 13 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
662 changes: 626 additions & 36 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions fastcrypto-zkp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ serde = { version = "1.0.152", features = ["derive"] }
serde_json = "1.0.93"
once_cell = "1.16"
poseidon-ark = { git = "https://github.com/MystenLabs/poseidon-ark.git", rev = "39844046ded0c4aa7a247e8545e131b59a330a9c" }
reqwest = "0.11.18"
bcs = "0.1.4"

[dev-dependencies]
ark-bls12-377 = "0.4.0"
Expand All @@ -45,3 +47,4 @@ criterion = "0.4.0"
hex = "0.4.3"
proptest = "1.1.0"
num-bigint = { version = "0.4", default-features = false, features = ["rand"] }
tokio = { version = "1.24.1", features = ["sync", "rt", "macros"] }
5 changes: 5 additions & 0 deletions fastcrypto-zkp/src/bn254/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ pub mod poseidon;
/// Zk login structs and utilities
pub mod zk_login;

/// Zk login entrypoints
pub mod zk_login_api;

/// Zk login utils
pub mod utils;
/// A field element in the BN254 construction. Thin wrapper around `api::Bn254Fr`.
#[derive(Debug, From)]
pub struct FieldElement(pub(crate) api::Bn254Fr);
Expand Down
69 changes: 46 additions & 23 deletions fastcrypto-zkp/src/bn254/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,34 @@ impl PoseidonWrapper {
.map_err(|_| FastCryptoError::InvalidInput)
}
}

/// Calculate the poseidon hash of the field element inputs. If the input
/// length is <= 16, calculate H(inputs), if it is <= 32, calculate H(H(inputs[0..16]), H(inputs[16..32])), otherwise return an error.
pub fn to_poseidon_hash(inputs: Vec<Fr>) -> Result<Fr, FastCryptoError> {
if inputs.len() <= 16 {
let mut poseidon1: PoseidonWrapper = PoseidonWrapper::new();
poseidon1.hash(inputs)
} else if inputs.len() <= 32 {
let mut poseidon1: PoseidonWrapper = PoseidonWrapper::new();
let hash1 = poseidon1.hash(inputs[0..16].to_vec())?;

let mut poseidon2 = PoseidonWrapper::new();
let hash2 = poseidon2.hash(inputs[16..].to_vec())?;

let mut poseidon3 = PoseidonWrapper::new();
poseidon3.hash([hash1, hash2].to_vec())
} else {
Err(FastCryptoError::GeneralError(format!(
"Yet to implement: Unable to hash a vector of length {}",
inputs.len()
)))
}
}

#[cfg(test)]
mod test {
use super::PoseidonWrapper;
use crate::bn254::zk_login::Bn254Fr;
use crate::bn254::zk_login::{calculate_merklized_hash, to_poseidon_hash};
use crate::bn254::{poseidon::to_poseidon_hash, zk_login::Bn254Fr};
use ark_bn254::Fr;
use std::str::FromStr;

Expand All @@ -54,13 +77,6 @@ mod test {

#[test]
fn poseidon_test() {
// TODO (joyqvq): add more test vectors here from circom.js
// Test vector generated from circom.js
// Poseidon([134696963602902907403122104327765350261n,
// 17932473587154777519561053972421347139n,
// 10000,
// 50683480294434968413708503290439057629605340925620961559740848568164438166n])
// = 2272550810841985018139126931041192927190568084082399473943239080305281957330n
let mut poseidon = PoseidonWrapper::new();
let input1 = Fr::from_str("134696963602902907403122104327765350261").unwrap();
let input2 = Fr::from_str("17932473587154777519561053972421347139").unwrap();
Expand All @@ -79,43 +95,50 @@ mod test {
);
}
#[test]
fn test_merklized_hash() {
let masked_content = b"eyJhbGciOiJSUzI1NiIsImtpZCI6ImM5YWZkYTM2ODJlYmYwOWViMzA1NWMxYzRiZDM5Yjc1MWZiZjgxOTUiLCJ0eXAiOiJKV1QifQ.=yJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20iLC===========================================================================================================CJhdWQiOiI1NzU1MTkyMDQyMzctbXNvcDllcDQ1dTJ1bzk4aGFwcW1uZ3Y4ZDg0cWRjOGsuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLC==========================================================================================================================================================================================================================================================================================================\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x14\xd8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
fn test_to_poseidon_hash() {
assert_eq!(
calculate_merklized_hash(masked_content).unwrap(),
"14900420995580824499222150327925943524564997104405553289134597516335134742309"
);

assert_eq!(
to_poseidon_hash(to_bigint_arr(vec![1])).unwrap(),
to_poseidon_hash(to_bigint_arr(vec![1]))
.unwrap()
.to_string(),
"18586133768512220936620570745912940619677854269274689475585506675881198879027"
);
assert_eq!(
to_poseidon_hash(to_bigint_arr(vec![1, 2])).unwrap(),
to_poseidon_hash(to_bigint_arr(vec![1, 2]))
.unwrap()
.to_string(),
"7853200120776062878684798364095072458815029376092732009249414926327459813530"
);
assert_eq!(
to_poseidon_hash(to_bigint_arr(vec![
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
]))
.unwrap(),
.unwrap()
.to_string(),
"4203130618016961831408770638653325366880478848856764494148034853759773445968"
);
assert_eq!(
to_poseidon_hash(to_bigint_arr(vec![
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
]))
.unwrap(),
"13895998335546007571506436905298853781676311844723695580596383169075721618652"
.unwrap()
.to_string(),
"9989051620750914585850546081941653841776809718687451684622678807385399211877"
);
assert_eq!(
to_poseidon_hash(to_bigint_arr(vec![
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
23, 24, 25, 26, 27, 28, 29
]))
.unwrap(),
"14023706212980258922092162104379517008998397500440232747089120702484714603058"
.unwrap()
.to_string(),
"4123755143677678663754455867798672266093104048057302051129414708339780424023"
);

assert!(to_poseidon_hash(to_bigint_arr(vec![
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31, 32
]))
.is_err());
}

#[test]
Expand Down
Loading