diff --git a/Makefile b/Makefile index f741106..6a1c8b8 100644 --- a/Makefile +++ b/Makefile @@ -33,7 +33,7 @@ pythontests: cargo +nightly update cargo +nightly build --release python py/libbolt.py - python py/tests.py + python py/libbolt_tests.py cpptests: @cargo +nightly build --release diff --git a/go/libbolt.go b/go/libbolt.go index 6d48d73..aa730c9 100644 --- a/go/libbolt.go +++ b/go/libbolt.go @@ -32,7 +32,7 @@ type setupResp struct { type ChannelState struct { R int `json:"R"` - TxFee int `json:"tx_fee"` + TxFee int64 `json:"tx_fee"` Cp interface{} `json:"cp"` Name string `json:"name"` PayInit bool `json:"pay_init"` @@ -55,8 +55,8 @@ type CustState struct { Name string `json:"name"` PkC string `json:"pk_c"` SkC string `json:"sk_c"` - CustBalance int `json:"cust_balance"` - MerchBalance int `json:"merch_balance"` + CustBalance int64 `json:"cust_balance"` + MerchBalance int64 `json:"merch_balance"` Wpk string `json:"wpk"` Wsk string `json:"wsk"` OldKP *KP `json:"old_kp,omitempty"` @@ -80,8 +80,8 @@ type Commitment struct { type Wallet struct { Pkc []string `json:"pkc"` Wpk []string `json:"wpk"` - Bc int `json:"bc"` - Bm int `json:"bm"` + Bc int64 `json:"bc"` + Bm int64 `json:"bm"` Close []string `json:"close"` } @@ -193,7 +193,7 @@ func BidirectionalInitCustomer(channelToken ChannelToken, balanceCustomer int, b if err != nil { return ChannelToken{}, CustState{}, err } - resp := C.GoString(C.ffishim_bidirectional_init_customer(C.CString(string(serChannelToken)), C.int(balanceCustomer), C.int(balanceMerchant), C.CString(nameCustomer))) + resp := C.GoString(C.ffishim_bidirectional_init_customer(C.CString(string(serChannelToken)), C.longlong(balanceCustomer), C.longlong(balanceMerchant), C.CString(nameCustomer))) r, err := processCResponse(resp) if err != nil { return ChannelToken{}, CustState{}, err @@ -256,7 +256,7 @@ func BidirectionalEstablishMerchantIssueCloseToken(channelState ChannelState, co if err != nil { return Signature{}, err } - resp := C.GoString(C.ffishim_bidirectional_establish_merchant_issue_close_token(C.CString(string(serChannelState)), C.CString(string(serCom)), C.CString(string(serComProof)), C.CString(pkc), C.int(initCustBal), C.int(initMerchBal), C.CString(string(serMerchState)))) + resp := C.GoString(C.ffishim_bidirectional_establish_merchant_issue_close_token(C.CString(string(serChannelState)), C.CString(string(serCom)), C.CString(string(serComProof)), C.CString(pkc), C.longlong(initCustBal), C.longlong(initMerchBal), C.CString(string(serMerchState)))) r, err := processCResponse(resp) if err != nil { return Signature{}, err @@ -350,7 +350,7 @@ func BidirectionalPayGeneratePaymentProof(channelState ChannelState, custState C if err != nil { return "", CustState{}, err } - resp := C.GoString(C.ffishim_bidirectional_pay_generate_payment_proof(C.CString(string(serChannelState)), C.CString(string(serCustState)), C.int(amount))) + resp := C.GoString(C.ffishim_bidirectional_pay_generate_payment_proof(C.CString(string(serChannelState)), C.CString(string(serCustState)), C.longlong(amount))) r, err := processCResponse(resp) if err != nil { return "", CustState{}, err diff --git a/include/libbolt.h b/include/libbolt.h index ccf6316..50fa513 100644 --- a/include/libbolt.h +++ b/include/libbolt.h @@ -18,17 +18,17 @@ extern "C" { // channel init char* ffishim_bidirectional_channel_setup(const char *channel_name, unsigned int third_party_support); char* ffishim_bidirectional_init_merchant(const char *ser_channel_state, const char *name_ptr); -char* ffishim_bidirectional_init_customer(const char *ser_channel_token, int balance_customer, int balance_merchant, const char *name_ptr); +char* ffishim_bidirectional_init_customer(const char *ser_channel_token, long long int balance_customer, long long int balance_merchant, const char *name_ptr); // channel establish protocol routines char* ffishim_bidirectional_establish_customer_generate_proof(const char *ser_channel_token, const char *ser_customer_wallet); -char* ffishim_bidirectional_establish_merchant_issue_close_token(const char *ser_channel_state, const char *ser_com, const char *ser_com_proof, const char *ser_pk_c, int init_cust_bal, int init_merch_bal, const char *ser_merch_state); +char* ffishim_bidirectional_establish_merchant_issue_close_token(const char *ser_channel_state, const char *ser_com, const char *ser_com_proof, const char *ser_pk_c, long long int init_cust_bal, long long int init_merch_bal, const char *ser_merch_state); char* ffishim_bidirectional_establish_merchant_issue_pay_token(const char *ser_channel_state, const char *ser_com, const char *ser_merch_state); char* ffishim_bidirectional_verify_close_token(const char *ser_channel_state, const char *ser_customer_wallet, const char *ser_close_token); char* ffishim_bidirectional_establish_customer_final(const char *ser_channel_state, const char *ser_customer_wallet, const char *ser_pay_token); // channel pay protocol routines -char* ffishim_bidirectional_pay_generate_payment_proof(const char *ser_channel_state, const char *ser_customer_wallet, int amount); +char* ffishim_bidirectional_pay_generate_payment_proof(const char *ser_channel_state, const char *ser_customer_wallet, long long int amount); char* ffishim_bidirectional_pay_verify_payment_proof(const char *ser_channel_state, const char *ser_pay_proof, const char *ser_merch_state); char* ffishim_bidirectional_pay_generate_revoke_token(const char *ser_channel_state, const char *ser_cust_state, const char *ser_new_cust_state, const char *ser_close_token); char* ffishim_bidirectional_pay_verify_revoke_token(const char *ser_revoke_token, const char *ser_merch_state); diff --git a/src/ccs08.rs b/src/ccs08.rs index dc079aa..464c624 100644 --- a/src/ccs08.rs +++ b/src/ccs08.rs @@ -30,11 +30,11 @@ pub struct ParamsUL { // u determines the amount of signatures we need in the public params. // Each signature can be compressed to just 1 field element of 256 bits. // Then the parameters have minimum size equal to 256*u bits. - u: i32, + u: i64, // l determines how many pairings we need to compute, then in order to improve // verifier`s performance we want to minize it. // Namely, we have 2*l pairings for the prover and 3*l for the verifier. - l: i32, + l: i64, } /** @@ -51,7 +51,7 @@ pub struct SecretParamsUL { #[derive(Clone)] pub struct ProofULState { - pub decx: Vec, + pub decx: Vec, pub proofStates: Vec>, pub V: Vec>, pub D: E::G1, @@ -118,8 +118,8 @@ This must be computed in a trusted setup. #[serde(bound(deserialize = "::Fr: serde::Deserialize<'de>, ::G1: serde::Deserialize<'de>, ::G2: serde::Deserialize<'de>"))] pub struct RPPublicParams { pub p: ParamsUL, - pub a: i32, - pub b: i32, + pub a: i64, + pub b: i64, } /** @@ -140,7 +140,7 @@ impl SecretParamsUL { The value of u should be roughly b/log(b), but we can choose smaller values in order to get smaller parameters, at the cost of having worse performance. */ - pub fn setup_ul(rng: &mut R, u: i32, l: i32, csParams: CSMultiParams) -> Self { + pub fn setup_ul(rng: &mut R, u: i64, l: i64, csParams: CSMultiParams) -> Self { let mpk = setup(rng); let kp = BlindKeyPair::::generate(rng, &mpk, 1); @@ -220,7 +220,7 @@ impl ParamsUL { /** prove_ul method is used to produce the ZKRP proof that secret x belongs to the interval [0,U^L). */ - pub fn prove_ul(&self, rng: &mut R, x: i32, r: E::Fr, C: Commitment, k: usize, otherM: Vec) -> ProofUL { + pub fn prove_ul(&self, rng: &mut R, x: i64, r: E::Fr, C: Commitment, k: usize, otherM: Vec) -> ProofUL { let proofUlState = self.prove_ul_commitment(rng, x, k, None, None); // Fiat-Shamir heuristic @@ -233,8 +233,8 @@ impl ParamsUL { self.prove_ul_response(r, C, &proofUlState, c, k, otherM) } - pub fn prove_ul_commitment(&self, rng: &mut R, x: i32, k: usize, sOptional: Option>, mOptional: Option) -> ProofULState { - if x > self.u.pow(self.l as u32) || x < 0 { + pub fn prove_ul_commitment(&self, rng: &mut R, x: i64, k: usize, sOptional: Option>, mOptional: Option) -> ProofULState { + if x > self.u.pow(self.l as u32) - 1 || x < 0 { panic!("x is not within the range."); } let decx = decompose(x, self.u, self.l); @@ -340,7 +340,7 @@ fn hash(a: Vec, D: Vec) -> E::Fr { Decompose receives as input an integer x and outputs an array of integers such that x = sum(xi.u^i), i.e. it returns the decomposition of x into base u. */ -fn decompose(x: i32, u: i32, l: i32) -> Vec { +fn decompose(x: i64, u: i64, l: i64) -> Vec { let mut result = Vec::with_capacity(l as usize); let mut decomposer = x.clone(); for _i in 0..l { @@ -354,7 +354,7 @@ impl RPSecretParams { /** Setup receives integers a and b, and configures the parameters for the rangeproof scheme. */ - pub fn setup(rng: &mut R, a: i32, b: i32, csParams: CSMultiParams) -> Self { + pub fn setup(rng: &mut R, a: i64, b: i64, csParams: CSMultiParams) -> Self { // Compute optimal values for u and l if a > b { panic!("a must be less than or equal to b"); @@ -363,9 +363,9 @@ impl RPSecretParams { let logb = (b as f32).log2(); let loglogb = logb.log2(); if loglogb > 0.0 { - let mut u = (logb / loglogb) as i32; + let mut u = (logb / loglogb) as i64; u = 57; //TODO: optimize u? - let l = (b as f32).log(u as f32).ceil() as i32; + let l = (b as f64).log(u as f64).ceil() as i64; let secParamsOut = SecretParamsUL::::setup_ul(rng, u, l, csParams.clone()); let pubParams = RPPublicParams { p: secParamsOut.pubParams.clone(), a, b }; @@ -398,7 +398,7 @@ impl RPPublicParams { /** Prove method is responsible for generating the zero knowledge range proof. */ - pub fn prove(&self, rng: &mut R, x: i32, C: Commitment, r: E::Fr, k: usize, otherM: Vec) -> RangeProof { + pub fn prove(&self, rng: &mut R, x: i64, C: Commitment, r: E::Fr, k: usize, otherM: Vec) -> RangeProof { let rpState = self.prove_commitment(rng, x, C, k, None, None); let mut a = Vec::::with_capacity(self.p.l as usize); @@ -411,7 +411,7 @@ impl RPPublicParams { self.prove_response(r, &rpState, ch, k, otherM) } - pub fn prove_commitment(&self, rng: &mut R, x: i32, C: Commitment, k: usize, sOptional: Option>, mOptional: Option) -> RangeProofState { + pub fn prove_commitment(&self, rng: &mut R, x: i64, C: Commitment, k: usize, sOptional: Option>, mOptional: Option) -> RangeProofState { if x > self.b || x < self.a { panic!("x is not within the range."); } @@ -646,14 +646,14 @@ mod tests { let vec1 = decompose(25, 3, 5); let mut result = 0; for i in 0..5 { - result += vec1[i] * 3i32.pow(i as u32); + result += vec1[i] * 3i64.pow(i as u32); } assert_eq!(result, 25); let vec1 = decompose(143225, 6, 7); let mut result = 0; for i in 0..7 { - result += vec1[i] * 6i32.pow(i as u32); + result += vec1[i] * 6i64.pow(i as u32); } assert_eq!(result, 143225); } diff --git a/src/channels.rs b/src/channels.rs index ea9a45c..3858578 100644 --- a/src/channels.rs +++ b/src/channels.rs @@ -85,7 +85,7 @@ pub struct ChannelParams { ))] pub struct ChannelState { R: i32, - tx_fee: i32, + tx_fee: i64, pub cp: Option>, pub name: String, pub pay_init: bool, @@ -184,12 +184,12 @@ impl ChannelState { // load external params } - pub fn set_channel_fee(&mut self, fee: i32) { + pub fn set_channel_fee(&mut self, fee: i64) { self.tx_fee = fee; } - pub fn get_channel_fee(&self) -> i32 { - return self.tx_fee as i32; + pub fn get_channel_fee(&self) -> i64 { + return self.tx_fee as i64; } } @@ -215,9 +215,9 @@ pub struct CustomerState { pub name: String, pub pk_c: secp256k1::PublicKey, sk_c: secp256k1::SecretKey, - pub cust_balance: i32, + pub cust_balance: i64, // - pub merch_balance: i32, + pub merch_balance: i64, pub wpk: secp256k1::PublicKey, // keypair bound to the wallet wsk: secp256k1::SecretKey, @@ -235,7 +235,7 @@ pub struct CustomerState { } impl CustomerState { - pub fn new(csprng: &mut R, channel_token: &mut ChannelToken, cust_bal: i32, merch_bal: i32, name: String) -> Self { + pub fn new(csprng: &mut R, channel_token: &mut ChannelToken, cust_bal: i64, merch_bal: i64, name: String) -> Self { let mut kp = secp256k1::Secp256k1::new(); kp.randomize(csprng); @@ -358,7 +358,7 @@ impl CustomerState { } // for channel pay - pub fn generate_payment(&self, csprng: &mut R, channel: &ChannelState, amount: i32) -> (NIZKProof, Commitment, secp256k1::PublicKey, CustomerState) { + pub fn generate_payment(&self, csprng: &mut R, channel: &ChannelState, amount: i64) -> (NIZKProof, Commitment, secp256k1::PublicKey, CustomerState) { // 1 - chooose new wpk/wsk pair let mut kp = secp256k1::Secp256k1::new(); kp.randomize(csprng); @@ -559,7 +559,7 @@ impl MerchantState { return self.keypair.sign_blind(csprng, &cp.pub_params.mpk, pay_com); } - pub fn verify_proof(&self, csprng: &mut R, channel: &ChannelState, com: &Commitment, com_proof: &CommitmentProof, pkc: &E::Fr, cust_balance: i32, merch_balance: i32) -> ResultBoltType<(Signature, Signature)> { + pub fn verify_proof(&self, csprng: &mut R, channel: &ChannelState, com: &Commitment, com_proof: &CommitmentProof, pkc: &E::Fr, cust_balance: i64, merch_balance: i64) -> ResultBoltType<(Signature, Signature)> { let is_valid = nizk::verify_opening(&self.comParams, &com.c, &com_proof, &pkc, cust_balance, merch_balance); let cp = channel.cp.as_ref().unwrap(); if is_valid { @@ -581,7 +581,7 @@ impl MerchantState { return self.pay_tokens.get(&wpk_str).unwrap().clone(); } - pub fn verify_payment(&mut self, csprng: &mut R, channel: &ChannelState, proof: &NIZKProof, com: &Commitment, wpk: &secp256k1::PublicKey, amount: i32) -> ResultBoltType> { + pub fn verify_payment(&mut self, csprng: &mut R, channel: &ChannelState, proof: &NIZKProof, com: &Commitment, wpk: &secp256k1::PublicKey, amount: i64) -> ResultBoltType> { let cp = channel.cp.as_ref().unwrap(); let pay_proof = proof.clone(); let prev_wpk = hash_pubkey_to_fr::(&wpk); diff --git a/src/ffishim.rs b/src/ffishim.rs index 4bedd5a..f8aaf3d 100644 --- a/src/ffishim.rs +++ b/src/ffishim.rs @@ -120,7 +120,7 @@ pub mod ffishim { } #[no_mangle] - pub extern fn ffishim_bidirectional_init_customer(ser_channel_token: *mut c_char, balance_customer: i32, balance_merchant: i32, name_ptr: *const c_char) -> *mut c_char { + pub extern fn ffishim_bidirectional_init_customer(ser_channel_token: *mut c_char, balance_customer: i64, balance_merchant: i64, name_ptr: *const c_char) -> *mut c_char { let rng = &mut rand::thread_rng(); // Deserialize the channel token let channel_token_result: ResultSerdeType> = deserialize_result_object(ser_channel_token); @@ -162,7 +162,7 @@ pub mod ffishim { } #[no_mangle] - pub extern fn ffishim_bidirectional_establish_merchant_issue_close_token(ser_channel_state: *mut c_char, ser_com: *mut c_char, ser_com_proof: *mut c_char, ser_pk_c: *mut c_char, init_cust_bal: i32, init_merch_bal: i32, ser_merch_state: *mut c_char) -> *mut c_char { + pub extern fn ffishim_bidirectional_establish_merchant_issue_close_token(ser_channel_state: *mut c_char, ser_com: *mut c_char, ser_com_proof: *mut c_char, ser_pk_c: *mut c_char, init_cust_bal: i64, init_merch_bal: i64, ser_merch_state: *mut c_char) -> *mut c_char { let rng = &mut rand::thread_rng(); // Deserialize the channel state let channel_state_result: ResultSerdeType> = deserialize_result_object(ser_channel_state); @@ -266,7 +266,7 @@ pub mod ffishim { // PAY #[no_mangle] - pub extern fn ffishim_bidirectional_pay_generate_payment_proof(ser_channel_state: *mut c_char, ser_customer_state: *mut c_char, amount: i32) -> *mut c_char { + pub extern fn ffishim_bidirectional_pay_generate_payment_proof(ser_channel_state: *mut c_char, ser_customer_state: *mut c_char, amount: i64) -> *mut c_char { let rng = &mut rand::thread_rng(); // Deserialize the channel state let channel_state_result: ResultSerdeType> = deserialize_result_object(ser_channel_state); @@ -277,6 +277,7 @@ pub mod ffishim { let cust_state = handle_errors!(cust_state_result); // Generate the payment proof + println!("{}", amount); let (payment, new_cust_state) = bidirectional::generate_payment_proof(rng, &channel_state, &cust_state, amount); // Serialize the results and return to caller let ser = ["{\'payment\':\'", serde_json::to_string(&payment).unwrap().as_str(), diff --git a/src/lib.rs b/src/lib.rs index d74e58d..0befdc9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,6 +39,16 @@ extern crate libc; extern crate rand_xorshift; extern crate core; +pub mod sym; +pub mod cl; +pub mod ccs08; +pub mod ped92; +pub mod channels; +pub mod nizk; +pub mod util; +pub mod wallet; +pub mod ffishim; + use std::fmt; use std::str; use bincode::SizeLimit::Infinite; @@ -52,16 +62,6 @@ use ff::{Rand, Field}; use serde::{Serialize, Deserialize}; use serde::de::{Deserializer, Unexpected, Error}; -pub mod sym; -pub mod cl; -pub mod ccs08; -pub mod ped92; -pub mod channels; -pub mod nizk; -pub mod util; -pub mod wallet; -pub mod ffishim; - ////////////////////////////////// Utilities ////////////////////////////////// struct HexSlice<'a>(&'a [u8]); @@ -163,7 +163,7 @@ pub mod bidirectional { proof: NIZKProof, com: Commitment, wpk: secp256k1::PublicKey, - amount: i32, + amount: i64, } #[derive(Clone, Serialize, Deserialize)] @@ -196,7 +196,7 @@ pub mod bidirectional { /// and wallet commitment. /// pub fn init_customer<'a, R: Rng, E: Engine>(csprng: &mut R, channel_token: &mut ChannelToken, - b0_cust: i32, b0_merch: i32, name: &'a str) -> CustomerState { + b0_cust: i64, b0_merch: i64, name: &'a str) -> CustomerState { assert!(b0_cust >= 0); assert!(b0_merch >= 0); @@ -221,7 +221,7 @@ pub mod bidirectional { /// pub fn establish_merchant_issue_close_token(csprng: &mut R, channel_state: &ChannelState, com: &Commitment, com_proof: &CommitmentProof, - pkc: &E::Fr, init_cust_balance: i32, init_merch_balance: i32, + pkc: &E::Fr, init_cust_balance: i64, init_merch_balance: i64, merch_state: &MerchantState) -> BoltResult> { // verifies proof of committed values and derives blind signature on the committed values to the customer's initial wallet match merch_state.verify_proof(csprng, channel_state, com, com_proof, pkc, init_cust_balance, init_merch_balance) { @@ -270,7 +270,7 @@ pub mod bidirectional { /// PoK of the committed values in new wallet and PoK of old wallet. Return new channel token, /// new wallet (minus blind signature and refund token) and payment proof. /// - pub fn generate_payment_proof(csprng: &mut R, channel_state: &ChannelState, cust_state: &CustomerState, amount: i32) -> (Payment, CustomerState) { + pub fn generate_payment_proof(csprng: &mut R, channel_state: &ChannelState, cust_state: &CustomerState, amount: i64) -> (Payment, CustomerState) { let tx_fee = channel_state.get_channel_fee(); let payment_amount = match tx_fee > 0 { true => amount + tx_fee, @@ -305,7 +305,7 @@ pub mod bidirectional { /// /// Verify third party payment proof from two bi-directional channel payments with intermediary /// -// pub fn verify_third_party_payment(pp: &PublicParams, fee: i32, proof1: &BalanceProof, proof2: &BalanceProof) -> bool { +// pub fn verify_third_party_payment(pp: &PublicParams, fee: i64, proof1: &BalanceProof, proof2: &BalanceProof) -> bool { // if proof1.third_party && proof2.third_party { // let vcom1 = &proof1.proof_vcom.as_ref().unwrap(); // let vcom2 = &proof2.proof_vcom.as_ref().unwrap(); @@ -528,7 +528,7 @@ mod tests { use util::hash_pubkey_to_fr; fn setup_new_channel_helper(channel_state: &mut bidirectional::ChannelState, - init_cust_bal: i32, init_merch_bal: i32) + init_cust_bal: i64, init_merch_bal: i64) -> (bidirectional::ChannelToken, bidirectional::MerchantState, bidirectional::CustomerState, bidirectional::ChannelState) { let mut rng = &mut rand::thread_rng(); let merch_name = "Bob"; @@ -550,8 +550,8 @@ mod tests { fn execute_establish_protocol_helper(channel_state: &mut bidirectional::ChannelState, channel_token: &mut bidirectional::ChannelToken, - cust_balance: i32, - merch_balance: i32, + cust_balance: i64, + merch_balance: i64, merch_state: &mut bidirectional::MerchantState, cust_state: &mut bidirectional::CustomerState) { let mut rng = &mut rand::thread_rng(); @@ -584,7 +584,7 @@ mod tests { channel_token: &mut bidirectional::ChannelToken, merch_state: &mut bidirectional::MerchantState, cust_state: &mut bidirectional::CustomerState, - payment_increment: i32) { + payment_increment: i64) { let mut rng = &mut rand::thread_rng(); let (payment, new_cust_state) = bidirectional::generate_payment_proof(rng, channel_state, &cust_state, payment_increment); @@ -823,7 +823,7 @@ mod tests { // merch2_data: &mut bidirectional::InitMerchantData, // cust1_keys: &cl::KeyPairD, cust1_data: &mut bidirectional::InitCustomerData, // cust2_keys: &cl::KeyPairD, cust2_data: &mut bidirectional::InitCustomerData, -// payment_increment: i32) { +// payment_increment: i64) { // // let's test the pay protocol // bidirectional::pay_by_customer_phase1_precompute(&pp, &cust1_data.channel_token, &merch_keys.pk, &mut cust1_data.csk); // bidirectional::pay_by_customer_phase1_precompute(&pp, &cust2_data.channel_token, &merch_keys.pk, &mut cust2_data.csk); diff --git a/src/nizk.rs b/src/nizk.rs index 12ddc9f..6d266ec 100644 --- a/src/nizk.rs +++ b/src/nizk.rs @@ -72,8 +72,8 @@ impl NIZKSecretParams { let mpk = setup(rng); let keypair = BlindKeyPair::::generate(rng, &mpk, messageLength); let comParams = keypair.generate_cs_multi_params(&mpk); - let u = 57; //TODO: optimize u? - let l = (std::i16::MAX as f32).log(u as f32).floor() as i32; + let u = 128; //TODO: make u and l configurable + let l = 9; let rpParams = SecretParamsUL::setup_ul(rng, u, l, comParams.clone()); let pubParams = NIZKPublicParams { mpk, pk: keypair.public.clone(), comParams, rpParams: rpParams.pubParams.clone() }; @@ -203,12 +203,12 @@ impl NIZKPublicParams { /// /// Verify PoK for the opening of a commitment during the establishment protocol /// -pub fn verify_opening(com_params: &CSMultiParams, com: &E::G1, proof: &CommitmentProof, pkc: &E::Fr, init_cust: i32, init_merch: i32) -> bool { +pub fn verify_opening(com_params: &CSMultiParams, com: &E::G1, proof: &CommitmentProof, pkc: &E::Fr, init_cust: i64, init_merch: i64) -> bool { let xvec: Vec = vec![proof.T.clone(), com.clone()]; let challenge = util::hash_g1_to_fr::(&xvec); // compute the - let com_equal = proof.verify_proof(com_params, com, &challenge, Some(vec!{None, Some(pkc.clone()), None, Some(util::convert_int_to_fr::(init_cust)), Some(util::convert_int_to_fr::(init_merch))})); + let com_equal = proof.verify_proof(com_params, com, &challenge, Some(vec!{None, Some(pkc.clone()), None, Some(util::convert_int_to_fr::(init_cust as i64)), Some(util::convert_int_to_fr::(init_merch as i64))})); return com_equal; } @@ -228,13 +228,13 @@ mod tests { let pkc = Fr::rand(rng); let wpk = Fr::rand(rng); let wpkprime = Fr::rand(rng); - let bc = rng.gen_range(100, 1000); + let bc = rng.gen_range(100, 1000) as i64; let mut bc2 = bc.clone(); - let bm = rng.gen_range(100, 1000); + let bm = rng.gen_range(100, 1000) as i64; let mut bm2 = bm.clone(); - let epsilon = &rng.gen_range(1, 100); - bc2 -= epsilon; - bm2 += epsilon; + let epsilon = rng.gen_range(1, 100) as i64; + bc2 = (bc2 as i64 - epsilon) as i64; + bm2 = (bm2 as i64 + epsilon) as i64; let r = Fr::rand(rng); let rprime = Fr::rand(rng); @@ -248,7 +248,7 @@ mod tests { let proof = secParams.pubParams.prove(rng, wallet1, wallet2, commitment2.clone(), rprime, &paymentToken); - let fr = convert_int_to_fr::(*epsilon); + let fr = convert_int_to_fr::(epsilon); assert_eq!(secParams.verify(proof, fr, &commitment2, wpk), true); } @@ -258,13 +258,13 @@ mod tests { let pkc = Fr::rand(rng); let wpk = Fr::rand(rng); let wpkprime = Fr::rand(rng); - let bc = rng.gen_range(100, 1000); + let bc = rng.gen_range(100, 1000) as i64; let mut bc2 = bc.clone(); - let bm = rng.gen_range(100, 1000); + let bm = rng.gen_range(100, 1000) as i64; let mut bm2 = bm.clone(); - let epsilon = &rng.gen_range(-100, -1); - bc2 -= epsilon; - bm2 += epsilon; + let epsilon = rng.gen_range(-100, -1) as i64; + bc2 = (bc2 as i64 - epsilon) as i64; + bm2 = (bm2 as i64 + epsilon) as i64; let r = Fr::rand(rng); let rprime = Fr::rand(rng); @@ -278,7 +278,7 @@ mod tests { let proof = secParams.pubParams.prove(rng, wallet1, wallet2, commitment2.clone(), rprime, &paymentToken); - let fr = convert_int_to_fr::(*epsilon); + let fr = convert_int_to_fr::(epsilon); assert_eq!(secParams.verify(proof, fr, &commitment2, wpk), true); } @@ -288,13 +288,13 @@ mod tests { let pkc = Fr::rand(rng); let wpk = Fr::rand(rng); let wpkprime = Fr::rand(rng); - let bc = rng.gen_range(100, 1000); + let bc = rng.gen_range(100, 1000) as i64; let mut bc2 = bc.clone(); - let bm = rng.gen_range(100, 1000); + let bm = rng.gen_range(100, 1000) as i64; let mut bm2 = bm.clone(); - let epsilon = &rng.gen_range(1, 100); - bc2 -= epsilon; - bm2 += epsilon; + let epsilon = rng.gen_range(1, 100) as i64; + bc2 = (bc2 as i64 - epsilon) as i64; + bm2 = (bm2 as i64 + epsilon) as i64; let r = Fr::rand(rng); let rprime = Fr::rand(rng); @@ -329,13 +329,13 @@ mod tests { let pkc = Fr::rand(rng); let wpk = Fr::rand(rng); let wpkprime = Fr::rand(rng); - let bc = rng.gen_range(100, 1000); + let bc = rng.gen_range(100, 1000) as i64; let mut bc2 = bc.clone(); - let bm = rng.gen_range(100, 1000); + let bm = rng.gen_range(100, 1000) as i64; let mut bm2 = bm.clone(); - let epsilon = &rng.gen_range(1, 100); - bc2 -= epsilon; - bm2 += epsilon; + let epsilon = rng.gen_range(1, 100) as i64; + bc2 = (bc2 as i64 - epsilon) as i64; + bm2 = (bm2 as i64 + epsilon) as i64; let r = Fr::rand(rng); let rprime = Fr::rand(rng); @@ -370,8 +370,8 @@ mod tests { let wpk = Fr::rand(rng); let t = Fr::rand(rng); - let bc = rng.gen_range(100, 1000); - let bm = rng.gen_range(100, 1000); + let bc = rng.gen_range(100, 1000) as i64; + let bm = rng.gen_range(100, 1000) as i64; let wallet = Wallet:: { pkc: pkc, wpk: wpk, bc: bc, bm: bm, close: None }; let secParams = NIZKSecretParams::::setup(rng, 4); @@ -390,9 +390,9 @@ mod tests { let wpk = Fr::rand(rng); let t = Fr::rand(rng); - let bc = rng.gen_range(100, 1000); - let bc2 = rng.gen_range(100, 1000); - let bm = rng.gen_range(100, 1000); + let bc = rng.gen_range(100, 1000) as i64; + let bc2 = rng.gen_range(100, 1000) as i64; + let bm = rng.gen_range(100, 1000) as i64; let wallet1 = Wallet:: { pkc: pkc, wpk: wpk, bc: bc, bm: bm, close: None }; let wallet2 = Wallet:: { pkc: pkc, wpk: wpk, bc: bc2, bm: bm, close: None }; @@ -416,8 +416,8 @@ mod tests { let mpk = setup(&mut rng); let blindkeypair = BlindKeyPair::::generate(&mut rng, &mpk, l); let comParams = blindkeypair.generate_cs_multi_params(&mpk); - let u = 57; //TODO: optimize u? - let l = (std::i16::MAX as f32).log(u as f32).floor() as i32; + let u = 256; //TODO: optimize u? + let l = 8; let rpParams = ccs08::SecretParamsUL::setup_ul(rng, u, l, comParams.clone()); let nizk_params = NIZKPublicParams { mpk: mpk, pk: blindkeypair.public, comParams: comParams, rpParams: rpParams.pubParams.clone() }; diff --git a/src/util.rs b/src/util.rs index e9d3fc9..e06546a 100644 --- a/src/util.rs +++ b/src/util.rs @@ -77,7 +77,7 @@ pub fn hash_pubkey_to_fr(wpk: &secp256k1::PublicKey) -> E::Fr { return result.unwrap(); } -pub fn convert_int_to_fr(value: i32) -> E::Fr { +pub fn convert_int_to_fr(value: i64) -> E::Fr { if value > 0 { return E::Fr::from_str(value.to_string().as_str()).unwrap(); } else { diff --git a/src/wallet.rs b/src/wallet.rs index 8ff8db3..98e6b61 100644 --- a/src/wallet.rs +++ b/src/wallet.rs @@ -12,8 +12,8 @@ use std::fmt; pub struct Wallet { pub pkc: E::Fr, pub wpk: E::Fr, - pub bc: i32, - pub bm: i32, + pub bc: i64, + pub bm: i64, pub close: Option, }