Testing code

This commit is contained in:
Bogdan-Ștefan Neacșu
2022-02-25 13:43:01 +03:00
parent 20c144c236
commit 5df820db6c
4 changed files with 54 additions and 23 deletions
+41 -18
View File
@@ -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<Signature>,
@@ -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<String>,
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<Vec<Signature>, 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,
+1 -1
View File
@@ -24,7 +24,7 @@
idx: idx,
validatorUrls: validator_urls,
});
alert(response);
qrVisible = !response;
}
async function deleteCredential(idx) {
+1 -2
View File
@@ -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;
+11 -2
View File
@@ -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