Skip to content

Commit

Permalink
renames and more test
Browse files Browse the repository at this point in the history
  • Loading branch information
joyqvq committed Mar 27, 2024
1 parent dcda933 commit 93949d5
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 47 deletions.
13 changes: 5 additions & 8 deletions fastcrypto-zkp/src/bn254/unit_tests/zk_login_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
8 changes: 3 additions & 5 deletions fastcrypto-zkp/src/bn254/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -43,9 +43,7 @@ pub fn gen_address_seed(
value: &str, // i.e. the sub value
aud: &str, // i.e. the client ID
) -> Result<String, FastCryptoError> {
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)
}

Expand All @@ -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())
}
Expand Down
16 changes: 7 additions & 9 deletions fastcrypto-zkp/src/bn254/zk_login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)?;
Expand Down
2 changes: 1 addition & 1 deletion fastcrypto-zkp/src/bn254/zk_login_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
2 changes: 1 addition & 1 deletion fastcrypto-zkp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ pub mod bn254;
pub mod dummy_circuits;

/// Circom-compatible deserialization of points
pub mod circom;
pub mod zk_login_utils;
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,17 @@ impl<'de> Deserialize<'de> for Bn254FrElement {
}

/// Convert Bn254FqElement type to arkworks' Fq.
fn parse_fq_field_element(s: &Bn254FqElement) -> Result<Fq, FastCryptoError> {
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<Fr, FastCryptoError> {
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
Expand All @@ -147,12 +151,8 @@ pub(crate) fn g1_affine_from_str_projective(s: &CircomG1) -> Result<G1Affine, Fa
return Err(FastCryptoError::InvalidInput);

Check warning on line 151 in fastcrypto-zkp/src/zk_login_utils.rs

View check run for this annotation

Codecov / codecov/patch

fastcrypto-zkp/src/zk_login_utils.rs#L151

Added line #L151 was not covered by tests
}

let g1: G1Affine = G1Projective::new_unchecked(
parse_fq_field_element(&s[0])?,
parse_fq_field_element(&s[1])?,
parse_fq_field_element(&s[2])?,
)
.into();
let g1: G1Affine =
G1Projective::new_unchecked((&s[0]).into(), (&s[1]).into(), (&s[2]).into()).into();

if !g1.is_on_curve() || !g1.is_in_correct_subgroup_assuming_on_curve() {
return Err(FastCryptoError::InvalidInput);
Expand All @@ -171,18 +171,9 @@ pub(crate) fn g2_affine_from_str_projective(s: &CircomG2) -> Result<G2Affine, Fa
}

let g2: G2Affine = G2Projective::new_unchecked(
Fq2::new(
parse_fq_field_element(&s[0][0])?,
parse_fq_field_element(&s[0][1])?,
),
Fq2::new(
parse_fq_field_element(&s[1][0])?,
parse_fq_field_element(&s[1][1])?,
),
Fq2::new(
parse_fq_field_element(&s[2][0])?,
parse_fq_field_element(&s[2][1])?,
),
Fq2::new((&s[0][0]).into(), (&s[0][1]).into()),
Fq2::new((&s[1][0]).into(), (&s[1][1]).into()),
Fq2::new((&s[2][0]).into(), (&s[2][1]).into()),
)
.into();

Expand All @@ -195,7 +186,7 @@ pub(crate) fn g2_affine_from_str_projective(s: &CircomG2) -> Result<G2Affine, Fa

#[cfg(test)]
mod test {
use crate::circom::Bn254FqElement;
use crate::zk_login_utils::Bn254FqElement;
use std::str::FromStr;

use super::Bn254FrElement;
Expand All @@ -205,9 +196,12 @@ mod test {
fn from_str_on_digits_only() {
// do not allow non digit results.
assert!(Bn254FrElement::from_str("10_________0").is_err());
assert!(Bn254FqElement::from_str("10_________0").is_err());
// do not allow leading zeros.
assert!(Bn254FrElement::from_str("000001").is_err());
assert!(Bn254FqElement::from_str("000001").is_err());
assert!(Bn254FrElement::from_str("garbage").is_err());
assert!(Bn254FqElement::from_str("garbage").is_err());
}
#[test]
fn unpadded_slice() {
Expand Down

0 comments on commit 93949d5

Please sign in to comment.