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: add validations and improve as_arkworks #663

Merged
merged 1 commit into from
Oct 3, 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
9 changes: 6 additions & 3 deletions fastcrypto-zkp/src/bn254/zk_login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@ impl ZkLoginInputs {

/// Initialize JWTDetails by parsing header_base64 and iss_base64_details.
pub fn init(&mut self) -> Result<Self, FastCryptoError> {
if BigUint::from_str(&self.address_seed).is_err() {
return Err(FastCryptoError::InvalidInput);
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the proof points are validated somewhere else already

self.jwt_details = JWTDetails::new(&self.header_base64, &self.iss_base64_details)?;
Ok(self.to_owned())
}
Expand Down Expand Up @@ -420,9 +423,9 @@ impl ZkLoginProof {
/// Convert the Circom G1/G2/GT to arkworks G1/G2/GT
pub fn as_arkworks(&self) -> Result<Proof<Bn254>, FastCryptoError> {
Ok(Proof {
a: g1_affine_from_str_projective(self.a.clone())?,
b: g2_affine_from_str_projective(self.b.clone())?,
c: g1_affine_from_str_projective(self.c.clone())?,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

possibly why this takes too much time in flamegraph 🤔

Copy link
Contributor

@jonas-lj jonas-lj Oct 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean the clone? The flamegraph shows that it's the subgroup check called by the g*_from_str_projective functions (through Bn254::Fr::new) which takes almost all the time.

I don't think we can omit the subgroup check, but it's possible to batch subgroup checks by doing the check on a random linear combination of elements.

a: g1_affine_from_str_projective(&self.a)?,
b: g2_affine_from_str_projective(&self.b)?,
c: g1_affine_from_str_projective(&self.c)?,
})
}
}
Expand Down
10 changes: 5 additions & 5 deletions fastcrypto-zkp/src/bn254/zk_login_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ static GLOBAL_VERIFYING_KEY: Lazy<PreparedVerifyingKey<Bn254>> = Lazy::new(globa
/// Load a fixed verifying key from zkLogin.vkey output. This is based on a local setup and should not use in production.
fn global_pvk() -> PreparedVerifyingKey<Bn254> {
// Convert the Circom G1/G2/GT to arkworks G1/G2/GT
let vk_alpha_1 = g1_affine_from_str_projective(vec![
let vk_alpha_1 = g1_affine_from_str_projective(&vec![
"21529901943976716921335152104180790524318946701278905588288070441048877064089".to_string(),
"7775817982019986089115946956794180159548389285968353014325286374017358010641".to_string(),
"1".to_string(),
])
.unwrap();
let vk_beta_2 = g2_affine_from_str_projective(vec![
let vk_beta_2 = g2_affine_from_str_projective(&vec![
vec![
"6600437987682835329040464538375790690815756241121776438004683031791078085074"
.to_string(),
Expand All @@ -60,7 +60,7 @@ fn global_pvk() -> PreparedVerifyingKey<Bn254> {
vec!["1".to_string(), "0".to_string()],
])
.unwrap();
let vk_gamma_2 = g2_affine_from_str_projective(vec![
let vk_gamma_2 = g2_affine_from_str_projective(&vec![
vec![
"10857046999023057135944570762232829481370756359578518086990519993285655852781"
.to_string(),
Expand All @@ -76,7 +76,7 @@ fn global_pvk() -> PreparedVerifyingKey<Bn254> {
vec!["1".to_string(), "0".to_string()],
])
.unwrap();
let vk_delta_2 = g2_affine_from_str_projective(vec![
let vk_delta_2 = g2_affine_from_str_projective(&vec![
vec![
"19260309516619721648285279557078789954438346514188902804737557357941293711874"
.to_string(),
Expand Down Expand Up @@ -111,7 +111,7 @@ fn global_pvk() -> PreparedVerifyingKey<Bn254> {
"1".to_string(),
],
] {
let g1 = g1_affine_from_str_projective(e).unwrap();
let g1 = g1_affine_from_str_projective(&e).unwrap();
vk_gamma_abc_g1.push(g1);
}

Expand Down
6 changes: 3 additions & 3 deletions fastcrypto-zkp/src/circom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use fastcrypto::error::FastCryptoError;
pub type CircomG1 = Vec<String>;
pub type CircomG2 = Vec<Vec<String>>;

pub fn g1_affine_from_str_projective(s: CircomG1) -> Result<G1Affine, FastCryptoError> {
pub fn g1_affine_from_str_projective(s: &CircomG1) -> Result<G1Affine, FastCryptoError> {
if s.len() != 3 {
return Err(FastCryptoError::InvalidInput);
}
Expand All @@ -22,13 +22,13 @@ pub fn g1_affine_from_str_projective(s: CircomG1) -> Result<G1Affine, FastCrypto
.into())
}

pub fn g2_affine_from_str_projective(s: CircomG2) -> Result<G2Affine, FastCryptoError> {
pub fn g2_affine_from_str_projective(s: &CircomG2) -> Result<G2Affine, FastCryptoError> {
use ark_bn254::G2Projective;
if s.len() != 3 {
return Err(FastCryptoError::InvalidInput);
}

for x in &s {
for x in s {
if x.len() != 2 {
return Err(FastCryptoError::InvalidInput);
}
Expand Down