From 93949d51abb1067752e06b9844e5ca61662f3fa4 Mon Sep 17 00:00:00 2001 From: Joy Wang <108701016+joyqvq@users.noreply.github.com> Date: Wed, 27 Mar 2024 11:04:53 -0400 Subject: [PATCH] renames and more test --- .../src/bn254/unit_tests/zk_login_tests.rs | 13 +++--- fastcrypto-zkp/src/bn254/utils.rs | 8 ++-- fastcrypto-zkp/src/bn254/zk_login.rs | 16 ++++---- fastcrypto-zkp/src/bn254/zk_login_api.rs | 2 +- fastcrypto-zkp/src/lib.rs | 2 +- .../src/{circom.rs => zk_login_utils.rs} | 40 ++++++++----------- 6 files changed, 34 insertions(+), 47 deletions(-) rename fastcrypto-zkp/src/{circom.rs => zk_login_utils.rs} (89%) diff --git a/fastcrypto-zkp/src/bn254/unit_tests/zk_login_tests.rs b/fastcrypto-zkp/src/bn254/unit_tests/zk_login_tests.rs index d2336fc46f..6bed4fceb4 100644 --- a/fastcrypto-zkp/src/bn254/unit_tests/zk_login_tests.rs +++ b/fastcrypto-zkp/src/bn254/unit_tests/zk_login_tests.rs @@ -11,7 +11,7 @@ use crate::bn254::zk_login::big_int_array_to_bits; use crate::bn254::zk_login::bitarray_to_bytearray; use crate::bn254::zk_login::{ base64_to_bitarray, convert_base, decode_base64_url, hash_ascii_str_to_field, hash_to_field, - parse_fr_field_element, parse_jwks, trim, verify_extended_claim, Claim, JWTDetails, JwkId, + parse_jwks, trim, verify_extended_claim, Claim, JWTDetails, JwkId, }; use crate::bn254::zk_login::{fetch_jwks, OIDCProvider}; use crate::bn254::zk_login_api::ZkLoginEnv; @@ -20,7 +20,7 @@ use crate::bn254::{ zk_login::{ZkLoginInputs, JWK}, zk_login_api::verify_zk_login, }; -use crate::circom::Bn254FrElement; +use crate::zk_login_utils::Bn254FrElement; use ark_bn254::Fr; use ark_std::rand::rngs::StdRng; use ark_std::rand::SeedableRng; @@ -495,12 +495,9 @@ fn test_verify_zk_login() { let aud = "575519204237-msop9ep45u2uo98hapqmngv8d84qdc8k.apps.googleusercontent.com"; let salt = "6588741469050502421550140105345050859"; let iss = "https://accounts.google.com"; - let salt_hash = poseidon_zk_login(vec![parse_fr_field_element( - &Bn254FrElement::from_str(salt).unwrap(), - ) - .unwrap()]) - .unwrap() - .to_string(); + let salt_hash = poseidon_zk_login(vec![(&Bn254FrElement::from_str(salt).unwrap()).into()]) + .unwrap() + .to_string(); assert!(verify_zk_login_id(&address, name, value, aud, iss, &salt_hash).is_ok()); let address_seed = gen_address_seed_with_salt_hash(&salt_hash, name, value, aud).unwrap(); diff --git a/fastcrypto-zkp/src/bn254/utils.rs b/fastcrypto-zkp/src/bn254/utils.rs index 5bd8d9a132..ec25456339 100644 --- a/fastcrypto-zkp/src/bn254/utils.rs +++ b/fastcrypto-zkp/src/bn254/utils.rs @@ -4,7 +4,7 @@ use crate::bn254::poseidon::poseidon_zk_login; use crate::bn254::zk_login::{OIDCProvider, ZkLoginInputsReader}; use crate::bn254::zk_login_api::Bn254Fr; -use crate::circom::{parse_fr_field_element, Bn254FrElement}; +use crate::zk_login_utils::Bn254FrElement; use fastcrypto::error::FastCryptoError; use fastcrypto::hash::{Blake2b256, HashFunction}; use fastcrypto::rsa::Base64UrlUnpadded; @@ -43,9 +43,7 @@ pub fn gen_address_seed( value: &str, // i.e. the sub value aud: &str, // i.e. the client ID ) -> Result { - let salt_hash = poseidon_zk_login(vec![parse_fr_field_element(&Bn254FrElement::from_str( - salt, - )?)?])?; + let salt_hash = poseidon_zk_login(vec![(&Bn254FrElement::from_str(salt)?).into()])?; gen_address_seed_with_salt_hash(&salt_hash.to_string(), name, value, aud) } @@ -60,7 +58,7 @@ pub(crate) fn gen_address_seed_with_salt_hash( hash_ascii_str_to_field(name, MAX_KEY_CLAIM_NAME_LENGTH)?, hash_ascii_str_to_field(value, MAX_KEY_CLAIM_VALUE_LENGTH)?, hash_ascii_str_to_field(aud, MAX_AUD_VALUE_LENGTH)?, - parse_fr_field_element(&Bn254FrElement::from_str(salt_hash)?)?, + (&Bn254FrElement::from_str(salt_hash)?).into(), ])? .to_string()) } diff --git a/fastcrypto-zkp/src/bn254/zk_login.rs b/fastcrypto-zkp/src/bn254/zk_login.rs index e4539c3c8e..b2b6c677fb 100644 --- a/fastcrypto-zkp/src/bn254/zk_login.rs +++ b/fastcrypto-zkp/src/bn254/zk_login.rs @@ -7,9 +7,9 @@ use serde_json::Value; use super::utils::split_to_two_frs; use crate::bn254::poseidon::poseidon_zk_login; -use crate::circom::{ - g1_affine_from_str_projective, g2_affine_from_str_projective, parse_fr_field_element, - Bn254FrElement, CircomG1, CircomG2, +use crate::zk_login_utils::{ + g1_affine_from_str_projective, g2_affine_from_str_projective, Bn254FrElement, CircomG1, + CircomG2, }; pub use ark_bn254::{Bn254, Fr as Bn254Fr}; pub use ark_ff::ToConstraintField; @@ -379,14 +379,12 @@ impl ZkLoginInputs { return Err(FastCryptoError::GeneralError("Header too long".to_string())); } - let addr_seed = parse_fr_field_element(&self.address_seed)?; + let addr_seed = (&self.address_seed).into(); let (first, second) = split_to_two_frs(eph_pk_bytes)?; - let max_epoch_f = - parse_fr_field_element(&Bn254FrElement::from_str(&max_epoch.to_string())?)?; - let index_mod_4_f = parse_fr_field_element(&Bn254FrElement::from_str( - &self.iss_base64_details.index_mod_4.to_string(), - )?)?; + let max_epoch_f = (&Bn254FrElement::from_str(&max_epoch.to_string())?).into(); + let index_mod_4_f = + (&Bn254FrElement::from_str(&self.iss_base64_details.index_mod_4.to_string())?).into(); let iss_base64_f = hash_ascii_str_to_field(&self.iss_base64_details.value, MAX_ISS_LEN_B64)?; diff --git a/fastcrypto-zkp/src/bn254/zk_login_api.rs b/fastcrypto-zkp/src/bn254/zk_login_api.rs index a004a8902f..c2e63addf6 100644 --- a/fastcrypto-zkp/src/bn254/zk_login_api.rs +++ b/fastcrypto-zkp/src/bn254/zk_login_api.rs @@ -8,7 +8,7 @@ use fastcrypto::rsa::{Base64UrlUnpadded, Encoding}; use super::zk_login::{JwkId, ZkLoginInputs, JWK}; use crate::bn254::utils::{gen_address_seed_with_salt_hash, get_zk_login_address}; -use crate::circom::{ +use crate::zk_login_utils::{ g1_affine_from_str_projective, g2_affine_from_str_projective, Bn254FqElement, Bn254FrElement, }; pub use ark_bn254::{Bn254, Fr as Bn254Fr}; diff --git a/fastcrypto-zkp/src/lib.rs b/fastcrypto-zkp/src/lib.rs index 13878899eb..4631dd3b32 100644 --- a/fastcrypto-zkp/src/lib.rs +++ b/fastcrypto-zkp/src/lib.rs @@ -24,4 +24,4 @@ pub mod bn254; pub mod dummy_circuits; /// Circom-compatible deserialization of points -pub mod circom; +pub mod zk_login_utils; diff --git a/fastcrypto-zkp/src/circom.rs b/fastcrypto-zkp/src/zk_login_utils.rs similarity index 89% rename from fastcrypto-zkp/src/circom.rs rename to fastcrypto-zkp/src/zk_login_utils.rs index 66bac4f428..75e0918e5b 100644 --- a/fastcrypto-zkp/src/circom.rs +++ b/fastcrypto-zkp/src/zk_login_utils.rs @@ -130,13 +130,17 @@ impl<'de> Deserialize<'de> for Bn254FrElement { } /// Convert Bn254FqElement type to arkworks' Fq. -fn parse_fq_field_element(s: &Bn254FqElement) -> Result { - Ok(Fq::from_be_bytes_mod_order(&s.0)) +impl From<&Bn254FqElement> for Fq { + fn from(f: &Bn254FqElement) -> Self { + Fq::from_be_bytes_mod_order(&f.0) + } } /// Convert Bn254FrElement type to arkworks' Fr. -pub fn parse_fr_field_element(s: &Bn254FrElement) -> Result { - Ok(Fr::from_be_bytes_mod_order(&s.0)) +impl From<&Bn254FrElement> for Fr { + fn from(f: &Bn254FrElement) -> Self { + Fr::from_be_bytes_mod_order(&f.0) + } } /// Deserialize a G1 projective point in BN254 serialized as a vector of three strings into an affine @@ -147,12 +151,8 @@ pub(crate) fn g1_affine_from_str_projective(s: &CircomG1) -> Result Result Result