More updates and clean up
This commit is contained in:
parent
628c167748
commit
0421d00f5e
|
@ -1,24 +1,33 @@
|
|||
extern crate bn;
|
||||
extern crate rand;
|
||||
extern crate rand_core;
|
||||
extern crate bolt;
|
||||
extern crate bincode;
|
||||
extern crate ff;
|
||||
extern crate pairing;
|
||||
extern crate time;
|
||||
extern crate secp256k1;
|
||||
//extern crate serde_derive;
|
||||
//extern crate serde;
|
||||
|
||||
//use bolt::unidirectional;
|
||||
use bolt::bidirectional;
|
||||
use time::PreciseTime;
|
||||
use std::time::Instant;
|
||||
use pairing::bls12_381::{Bls12};
|
||||
|
||||
macro_rules! measure {
|
||||
macro_rules! measure_one_arg {
|
||||
($x: expr) => {
|
||||
{
|
||||
let s = PreciseTime::now();
|
||||
let s = Instant::now();
|
||||
let res = $x;
|
||||
let e = PreciseTime::now();
|
||||
(res, s.to(e))
|
||||
let e = s.elapsed();
|
||||
(res, e.as_millis())
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! measure_two_arg {
|
||||
($x: expr) => {
|
||||
{
|
||||
let s = Instant::now();
|
||||
let (res1, res2) = $x;
|
||||
let e = s.elapsed();
|
||||
(res1, res2, e.as_millis())
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -27,157 +36,63 @@ macro_rules! measure {
|
|||
macro_rules! measure_ret_mut {
|
||||
($x: expr) => {
|
||||
{
|
||||
let s = PreciseTime::now();
|
||||
let s = Instant::now();
|
||||
let mut handle = $x;
|
||||
let e = PreciseTime::now();
|
||||
(handle, s.to(e))
|
||||
let e = s.elapsed();
|
||||
(handle, s.as_millis())
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("Hello world!");
|
||||
}
|
||||
println!("******************************************");
|
||||
let mut channel_state = bidirectional::ChannelState::<Bls12>::new(String::from("Channel A -> B"), false);
|
||||
let mut rng = &mut rand::thread_rng();
|
||||
|
||||
//fn main() {
|
||||
// println!("******************************************");
|
||||
// // libbolt tests below
|
||||
// println!("Testing the channel setup...");
|
||||
//
|
||||
// //println!("[1a] libbolt - setup bidirectional scheme params");
|
||||
// let (pp, setup_time1) = measure!(bidirectional::setup(false));
|
||||
//
|
||||
// //println!("[1b] libbolt - generate the initial channel state");
|
||||
// let mut channel = bidirectional::ChannelState::new(String::from("My New Channel A"), false);
|
||||
//
|
||||
// println!("Setup time: {}", setup_time1);
|
||||
//
|
||||
// //let msg = "Open Channel ID: ";
|
||||
// //libbolt::debug_elem_in_hex(msg, &channel.cid);
|
||||
//
|
||||
// let b0_cust = 50;
|
||||
// let b0_merch = 50;
|
||||
//
|
||||
// // generate long-lived keypair for merchant -- used to identify
|
||||
// // it to all customers
|
||||
// //println!("[2] libbolt - generate long-lived key pair for merchant");
|
||||
// let (merch_keypair, _) = measure!(bidirectional::keygen(&pp));
|
||||
//
|
||||
// // customer generates an ephemeral keypair for use on a single channel
|
||||
// println!("[3] libbolt - generate ephemeral key pair for customer (use with one channel)");
|
||||
// let (cust_keypair, _) = measure!(bidirectional::keygen(&pp));
|
||||
//
|
||||
// // each party executes the init algorithm on the agreed initial challenge balance
|
||||
// // in order to derive the channel tokens
|
||||
// println!("[5a] libbolt - initialize on the merchant side with balance {}", b0_merch);
|
||||
// let (mut merch_data, initm_time) = measure_ret_mut!(bidirectional::init_merchant(&pp, b0_merch, &merch_keypair));
|
||||
// println!(">> TIME for init_merchant: {}", initm_time);
|
||||
//
|
||||
// println!("[5b] libbolt - initialize on the customer side with balance {}", b0_cust);
|
||||
// let cm_csp = bidirectional::generate_commit_setup(&pp, &merch_keypair.pk);
|
||||
// let (mut cust_data, initc_time) = measure_ret_mut!(bidirectional::init_customer(&pp, &mut channel, b0_cust, b0_merch, &cm_csp, &cust_keypair));
|
||||
// println!(">> TIME for init_customer: {}", initc_time);
|
||||
// println!("******************************************");
|
||||
// // libbolt tests below
|
||||
// println!("Testing the establish protocol...");
|
||||
//
|
||||
// println!("[6a] libbolt - entering the establish protocol for the channel");
|
||||
// let (proof1, est_cust_time1) = measure!(bidirectional::establish_customer_phase1(&pp, &cust_data, &merch_data.bases));
|
||||
// println!(">> TIME for establish_customer_phase1: {}", est_cust_time1);
|
||||
//
|
||||
// println!("[6b] libbolt - obtain the wallet signature from the merchant");
|
||||
// let (wallet_sig, est_merch_time2) = measure!(bidirectional::establish_merchant_phase2(&pp, &mut channel, &merch_data, &proof1));
|
||||
// println!(">> TIME for establish_merchant_phase2: {}", est_merch_time2);
|
||||
//
|
||||
// println!("[6c] libbolt - complete channel establishment");
|
||||
// assert!(bidirectional::establish_customer_final(&pp, &merch_keypair.pk, &mut cust_data.csk, wallet_sig));
|
||||
//
|
||||
// assert!(channel.channel_established);
|
||||
//
|
||||
// println!("Channel has been established!");
|
||||
// println!("******************************************");
|
||||
//
|
||||
// println!("Testing the pay protocol...");
|
||||
// // let's test the pay protocol
|
||||
// bidirectional::pay_by_customer_phase1_precompute(&pp, &cust_data.channel_token, &merch_keypair.pk, &mut cust_data.csk);
|
||||
// let s = PreciseTime::now();
|
||||
// let (t_c, new_wallet, pay_proof) = bidirectional::pay_by_customer_phase1(&pp, &channel, &cust_data.channel_token, // channel token
|
||||
// &merch_keypair.pk, // merchant pub key
|
||||
// &cust_data.csk, // wallet
|
||||
// 5); // balance increment
|
||||
// let e = PreciseTime::now();
|
||||
// println!(">> TIME for pay_by_customer_phase1: {}", s.to(e));
|
||||
//
|
||||
// // get the refund token (rt_w)
|
||||
// let (rt_w, pay_merch_time1) = measure!(bidirectional::pay_by_merchant_phase1(&pp, &mut channel, &pay_proof, &merch_data));
|
||||
// println!(">> TIME for pay_by_merchant_phase1: {}", pay_merch_time1);
|
||||
//
|
||||
// // get the revocation token (rv_w) on the old public key (wpk)
|
||||
// let (rv_w, pay_cust_time2) = measure!(bidirectional::pay_by_customer_phase2(&pp, &cust_data.csk, &new_wallet, &merch_keypair.pk, &rt_w));
|
||||
// println!(">> TIME for pay_by_customer_phase2: {}", pay_cust_time2);
|
||||
//
|
||||
// // get the new wallet sig (new_wallet_sig) on the new wallet
|
||||
// let (new_wallet_sig, pay_merch_time2) = measure!(bidirectional::pay_by_merchant_phase2(&pp, &mut channel, &pay_proof, &mut merch_data, &rv_w));
|
||||
// println!(">> TIME for pay_by_merchant_phase2: {}", pay_merch_time2);
|
||||
//
|
||||
// assert!(bidirectional::pay_by_customer_final(&pp, &merch_keypair.pk, &mut cust_data, t_c, new_wallet, rt_w, new_wallet_sig));
|
||||
//
|
||||
// {
|
||||
// // scope localizes the immutable borrow here (for debug purposes only)
|
||||
// let cust_wallet = &cust_data.csk;
|
||||
// let merch_wallet = &merch_data.csk;
|
||||
// println!("Customer balance: {}", cust_wallet.balance);
|
||||
// println!("Merchant balance: {}", merch_wallet.balance);
|
||||
// }
|
||||
//
|
||||
// bidirectional::pay_by_customer_phase1_precompute(&pp, &cust_data.channel_token, &merch_keypair.pk, &mut cust_data.csk);
|
||||
// let (t_c1, new_wallet1, pay_proof1) = bidirectional::pay_by_customer_phase1(&pp, &channel, &cust_data.channel_token, // channel token
|
||||
// &merch_keypair.pk, // merchant pub key
|
||||
// &cust_data.csk, // wallet
|
||||
// -10); // balance increment
|
||||
//
|
||||
// // get the refund token (rt_w)
|
||||
// let rt_w1 = bidirectional::pay_by_merchant_phase1(&pp, &mut channel, &pay_proof1, &merch_data);
|
||||
//
|
||||
// // get the revocation token (rv_w) on the old public key (wpk)
|
||||
// let rv_w1 = bidirectional::pay_by_customer_phase2(&pp, &cust_data.csk, &new_wallet1, &merch_keypair.pk, &rt_w1);
|
||||
//
|
||||
// // get the new wallet sig (new_wallet_sig) on the new wallet
|
||||
// let new_wallet_sig1 = bidirectional::pay_by_merchant_phase2(&pp, &mut channel, &pay_proof1, &mut merch_data, &rv_w1);
|
||||
//
|
||||
// assert!(bidirectional::pay_by_customer_final(&pp, &merch_keypair.pk, &mut cust_data, t_c1, new_wallet1, rt_w1, new_wallet_sig1));
|
||||
//
|
||||
// {
|
||||
// let cust_wallet = &cust_data.csk;
|
||||
// let merch_wallet = &merch_data.csk;
|
||||
// println!("Updated balances...");
|
||||
// println!("Customer balance: {}", cust_wallet.balance);
|
||||
// println!("Merchant balance: {}", merch_wallet.balance);
|
||||
// let updated_cust_bal = b0_cust + 5;
|
||||
// let updated_merch_bal = b0_merch - 5;
|
||||
// assert_eq!(updated_cust_bal, cust_wallet.balance);
|
||||
// assert_eq!(updated_merch_bal, merch_wallet.balance);
|
||||
// }
|
||||
// println!("Pay protocol complete!");
|
||||
//
|
||||
// println!("******************************************");
|
||||
// println!("Testing the dispute algorithms...");
|
||||
//
|
||||
// {
|
||||
// let cust_wallet = &cust_data.csk;
|
||||
// // get channel closure message
|
||||
// let rc_c = bidirectional::customer_refund(&pp, &channel, &merch_keypair.pk, &cust_wallet);
|
||||
// println!("Obtained the channel closure message: {}", rc_c.message.msgtype);
|
||||
//
|
||||
// let channel_token = &cust_data.channel_token;
|
||||
// let rc_m = bidirectional::merchant_refute(&pp, &mut channel, &channel_token, &merch_data, &rc_c, &rv_w1.signature);
|
||||
// println!("Merchant has refuted the refund request!");
|
||||
//
|
||||
// let (new_b0_cust, new_b0_merch) = bidirectional::resolve(&pp, &cust_data, &merch_data,
|
||||
// Some(rc_c), Some(rc_m));
|
||||
// println!("Resolved! Customer = {}, Merchant = {}", new_b0_cust, new_b0_merch);
|
||||
// }
|
||||
//
|
||||
// // TODO: add tests for customer/merchant cheating scenarios
|
||||
// println!("******************************************");
|
||||
//}
|
||||
channel_state.setup(&mut rng); // or load_setup params
|
||||
|
||||
let b0_customer = 150;
|
||||
let b0_merchant = 10;
|
||||
let pay_inc = 20;
|
||||
|
||||
let (mut channel_token, mut merch_wallet) = bidirectional::init_merchant(rng, &mut channel_state, "Merchant Bob");
|
||||
// initialize the balance for merch_wallet
|
||||
merch_wallet.init_balance(b0_merchant);
|
||||
|
||||
let mut cust_wallet = bidirectional::init_customer(rng, &mut channel_state, &mut channel_token, b0_customer, b0_merchant, "Alice");
|
||||
|
||||
println!("{}", cust_wallet);
|
||||
|
||||
// lets establish the channel
|
||||
let (com, com_proof, est_time) = measure_two_arg!(bidirectional::establish_customer_generate_proof(rng, &mut channel_token, &mut cust_wallet));
|
||||
println!(">> Time to generate proof for establish: {} ms", est_time);
|
||||
|
||||
// obtain close token for closing out channel
|
||||
let close_token = bidirectional::establish_merchant_issue_close_token(rng, &channel_state, &com, &com_proof, &merch_wallet);
|
||||
assert!(cust_wallet.verify_close_token(&channel_state, &close_token));
|
||||
|
||||
// wait for funding tx to be confirmed, etc
|
||||
|
||||
// obtain payment token for pay protocol
|
||||
let pay_token = bidirectional::establish_merchant_issue_pay_token(rng, &channel_state, &com, &merch_wallet);
|
||||
//assert!(cust_wallet.verify_pay_token(&channel_state, &pay_token));
|
||||
|
||||
assert!(bidirectional::establish_customer_final(&mut channel_state, &mut cust_wallet, &pay_token));
|
||||
println!("Channel established!");
|
||||
|
||||
let (payment, new_cust_wallet, pay_time) = measure_two_arg!(bidirectional::generate_payment_proof(rng, &channel_state, &cust_wallet, pay_inc));
|
||||
println!(">> Time to generate payment proof: {} ms", pay_time);
|
||||
|
||||
let (new_close_token, verify_time) = measure_one_arg!(bidirectional::verify_payment_proof(rng, &channel_state, &payment, &mut merch_wallet));
|
||||
println!(">> Time to verify payment proof: {} ms", verify_time);
|
||||
|
||||
let revoke_token = bidirectional::generate_revoke_token(&channel_state, &mut cust_wallet, new_cust_wallet, &new_close_token);
|
||||
|
||||
// send revoke token and get pay-token in response
|
||||
let new_pay_token = bidirectional::verify_revoke_token(&revoke_token, &mut merch_wallet);
|
||||
|
||||
// verify the pay token and update internal state
|
||||
assert!(cust_wallet.verify_pay_token(&channel_state, &new_pay_token));
|
||||
|
||||
println!("******************************************");
|
||||
}
|
||||
|
|
|
@ -560,11 +560,7 @@ impl<E: Engine> MerchantWallet<E> {
|
|||
let cp = channel.cp.as_ref().unwrap();
|
||||
let pay_proof = proof.clone();
|
||||
let prev_wpk = hash_pubkey_to_fr::<E>(&wpk);
|
||||
let epsilon = E::Fr::from_str(&amount.to_string()).unwrap();
|
||||
if (amount < 0) {
|
||||
// TODO: how to handle negative payment increments?
|
||||
let epsilon = epsilon.inverse();
|
||||
}
|
||||
let epsilon = util::convert_int_to_fr::<E>(amount); // E::Fr::from_str(&amount.to_string()).unwrap();
|
||||
|
||||
if cp.pub_params.verify(pay_proof, epsilon, com, prev_wpk) {
|
||||
// 1 - proceed with generating close and pay token
|
||||
|
|
|
@ -76,9 +76,6 @@ pub mod util;
|
|||
pub mod wallet;
|
||||
pub mod ffishim;
|
||||
|
||||
const E_MIN: i32 = 1;
|
||||
const E_MAX: i32 = 255; // TODO: should be 2^32 - 1
|
||||
|
||||
pub fn debug_elem_in_hex(prefix: &str, r: &Fr) {
|
||||
let encoded: Vec<u8> = encode(&r, Infinite).unwrap();
|
||||
print!("{} (hex) = 0x", prefix);
|
||||
|
@ -204,8 +201,6 @@ pub mod bidirectional {
|
|||
use cl; // for blind signature
|
||||
use secp256k1; // for on-chain keys
|
||||
use HashMap;
|
||||
use E_MIN;
|
||||
use E_MAX;
|
||||
use sodiumoxide::crypto::hash::sha512;
|
||||
use sha2::Sha512;
|
||||
|
||||
|
@ -722,7 +717,6 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn bidirectional_payment_negative_payment_works() {
|
||||
// just bidirectional case (w/o third party)
|
||||
let total_owed = -20;
|
||||
|
|
|
@ -50,9 +50,14 @@ pub fn fmt_bytes_to_int(bytearray: [u8; 64]) -> String {
|
|||
// Decide if you want upper- or lowercase results,
|
||||
// padding the values to two characters, spaces
|
||||
// between bytes, etc.
|
||||
result = result + &format!("{}", *byte as u8);
|
||||
let s = format!("{}", *byte as u8);
|
||||
result = result + &s;
|
||||
}
|
||||
result.to_string()
|
||||
let s = match result.starts_with('0') {
|
||||
true => result[1..].to_string(),
|
||||
false => result.to_string()
|
||||
};
|
||||
return s;
|
||||
}
|
||||
|
||||
pub fn hash_to_fr<E: Engine>(mut byteVec: Vec<u8>) -> E::Fr {
|
||||
|
|
Loading…
Reference in New Issue