From 5df820db6c3d57de7f4ff8bd39bb9567ceb3e887 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogdan-=C8=98tefan=20Neac=C8=99u?= Date: Fri, 25 Feb 2022 13:43:01 +0300 Subject: [PATCH] Testing code --- clients/tauri-client/src-tauri/src/main.rs | 59 ++++++++++++++------ clients/tauri-client/src/routes/index.svelte | 2 +- common/nymcoconut/src/lib.rs | 3 +- common/nymcoconut/src/scheme/verification.rs | 13 ++++- 4 files changed, 54 insertions(+), 23 deletions(-) diff --git a/clients/tauri-client/src-tauri/src/main.rs b/clients/tauri-client/src-tauri/src/main.rs index f09a125450..6a99030a18 100644 --- a/clients/tauri-client/src-tauri/src/main.rs +++ b/clients/tauri-client/src-tauri/src/main.rs @@ -9,9 +9,11 @@ use tokio::sync::RwLock; use url::Url; use coconut_interface::{ - self, hash_to_scalar, Attribute, Credential, Parameters, Signature, Theta, VerificationKey, + self, hash_to_scalar, Attribute, Base58, Bytable, Credential, Parameters, Signature, Theta, + VerificationKey, }; -use credentials::{obtain_aggregate_signature, obtain_aggregate_verification_key}; +use credentials::coconut::bandwidth::{obtain_signature, BandwidthVoucherAttributes}; +use credentials::obtain_aggregate_verification_key; struct State { signatures: Vec, @@ -145,13 +147,14 @@ async fn verify_credential( // the API needs to be improved but at least it should compile (in theory) let verification_key = get_aggregated_verification_key(validator_urls.clone(), state.clone()).await?; + println!("Verification key {:?}", verification_key.to_bs58()); let theta = prove_credential(idx, validator_urls, state.clone()).await?; let state = state.read().await; let public_attributes_bytes = vec![ - state.voucher_value.to_bytes().to_vec(), - state.voucher_info.to_bytes().to_vec(), + state.voucher_value.to_byte_vec(), + state.voucher_info.to_byte_vec(), ]; let credential = Credential::new( @@ -172,19 +175,24 @@ async fn get_credential( validator_urls: Vec, state: tauri::State<'_, Arc>>, ) -> Result, String> { - let guard = state.read().await; - let parsed_urls = parse_url_validators(&validator_urls)?; - let public_attributes = vec![guard.voucher_value, guard.voucher_info]; - let private_attributes = vec![guard.serial_number, guard.binding_number]; + let signature = { + let guard = state.read().await; + let parsed_urls = parse_url_validators(&validator_urls)?; + let bandwidth_credential_attributes = BandwidthVoucherAttributes { + serial_number: guard.serial_number, + binding_number: guard.binding_number, + voucher_value: guard.voucher_value, + voucher_info: guard.voucher_info, + }; - let signature = obtain_aggregate_signature( - &guard.params, - &public_attributes, - &private_attributes, - &parsed_urls, - ) - .await - .map_err(|err| format!("failed to obtain aggregate signature - {:?}", err))?; + obtain_signature( + &guard.params, + &bandwidth_credential_attributes, + &parsed_urls, + ) + .await + .map_err(|err| format!("failed to obtain aggregate signature - {:?}", err))? + }; let mut state = state.write().await; state.signatures.push(signature); @@ -192,8 +200,23 @@ async fn get_credential( } fn main() { - let public_attributes = vec![b"public_key".to_vec()]; - let private_attributes = vec![b"private_key".to_vec()]; + let params = coconut_interface::Parameters::new(4).unwrap(); + let bandwidth_credential_attributes = BandwidthVoucherAttributes { + serial_number: params.random_scalar(), + binding_number: params.random_scalar(), + voucher_value: hash_to_scalar(1024u64.to_be_bytes()), + voucher_info: hash_to_scalar("BandwidthVoucher"), + }; + let public_attributes = bandwidth_credential_attributes + .get_public_attributes() + .iter() + .map(|attr| attr.to_byte_vec()) + .collect(); + let private_attributes = bandwidth_credential_attributes + .get_private_attributes() + .iter() + .map(|attr| attr.to_byte_vec()) + .collect(); tauri::Builder::default() .manage(Arc::new(RwLock::new(State::init( public_attributes, diff --git a/clients/tauri-client/src/routes/index.svelte b/clients/tauri-client/src/routes/index.svelte index 7ef6c66784..fc9b645177 100644 --- a/clients/tauri-client/src/routes/index.svelte +++ b/clients/tauri-client/src/routes/index.svelte @@ -24,7 +24,7 @@ idx: idx, validatorUrls: validator_urls, }); - alert(response); + qrVisible = !response; } async function deleteCredential(idx) { diff --git a/common/nymcoconut/src/lib.rs b/common/nymcoconut/src/lib.rs index 491a8e8fe1..936b27005f 100644 --- a/common/nymcoconut/src/lib.rs +++ b/common/nymcoconut/src/lib.rs @@ -5,6 +5,7 @@ use std::convert::TryInto; use bls12_381::Scalar; +pub use crate::traits::Bytable; pub use elgamal::elgamal_keygen; pub use elgamal::ElGamalKeyPair; pub use elgamal::PublicKey; @@ -28,8 +29,6 @@ pub use scheme::SignatureShare; pub use traits::Base58; pub use utils::hash_to_scalar; -use crate::traits::Bytable; - pub mod elgamal; mod error; mod impls; diff --git a/common/nymcoconut/src/scheme/verification.rs b/common/nymcoconut/src/scheme/verification.rs index 843f827f0f..0efe08c690 100644 --- a/common/nymcoconut/src/scheme/verification.rs +++ b/common/nymcoconut/src/scheme/verification.rs @@ -206,10 +206,17 @@ pub fn verify_credential( public_attributes: &[Attribute], ) -> bool { if public_attributes.len() + theta.pi_v.private_attributes_len() > verification_key.beta.len() { + println!( + "Len fail: {} + {} > {}", + public_attributes.len(), + theta.pi_v.private_attributes_len(), + verification_key.beta.len() + ); return false; } if !theta.verify_proof(params, verification_key) { + println!("Proof fail"); return false; } @@ -230,12 +237,14 @@ pub fn verify_credential( theta.blinded_message + signed_public_attributes }; - check_bilinear_pairing( + let ret = check_bilinear_pairing( &theta.credential.0.to_affine(), &G2Prepared::from(kappa.to_affine()), &(theta.credential.1).to_affine(), params.prepared_miller_g2(), - ) && !bool::from(theta.credential.0.is_identity()) + ); + println!("Ret value {}", ret); + ret && !bool::from(theta.credential.0.is_identity()) } // Used in tests only