Skip to content

Commit

Permalink
Pass tests for fee payment
Browse files Browse the repository at this point in the history
  • Loading branch information
osuketh committed Jun 6, 2019
1 parent d2a8a5f commit c20f919
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 34 deletions.
4 changes: 2 additions & 2 deletions core/proofs/src/circuit_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,8 @@ mod tests {
instance.synthesize(&mut cs).unwrap();

assert!(cs.is_satisfied());
// assert_eq!(cs.num_constraints(), 18278);
// assert_eq!(cs.hash(), "6858d345922e8a5f173dafb61264ea237b9f0fad75f51c656461cd43fdd3db34");
assert_eq!(cs.num_constraints(), 21687);
assert_eq!(cs.hash(), "006d0e0175bc1154278d7ef3f0e53514840b478ad6db2540d7910cd94a38da24");

assert_eq!(cs.num_inputs(), 19);
assert_eq!(cs.get_input(0, "ONE"), Fr::one());
Expand Down
Binary file modified demo/cli/proving.params
Binary file not shown.
81 changes: 53 additions & 28 deletions demo/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ fn cli() -> Result<(), String> {
const PROVING_KEY_PATH: &str = "demo/cli/proving.params";
const DEFAULT_AMOUNT: &str = "10";
const DEFAULT_BALANCE: &str = "100";
const DEFAULT_FEE: &str = "1";
const ALICESEED: &str = "416c696365202020202020202020202020202020202020202020202020202020";
const BOBSEED: &str = "426f622020202020202020202020202020202020202020202020202020202020";
const BOBACCOUNTID: &str = "45e66da531088b55dcb3b273ca825454d79d2d1d5c4fa2ba4a12c1fa1ccd6389";
Expand Down Expand Up @@ -121,6 +122,14 @@ fn cli() -> Result<(), String> {
.required(false)
.default_value(DEFAULT_AMOUNT)
)
.arg(Arg::with_name("fee")
.short("f")
.long("fee")
.help("The fee for the confidential transfer. (default: 1)")
.takes_value(true)
.required(false)
.default_value(DEFAULT_FEE)
)
.arg(Arg::with_name("balance")
.short("b")
.long("balance")
Expand Down Expand Up @@ -188,6 +197,14 @@ fn cli() -> Result<(), String> {
.required(false)
.default_value(BOBACCOUNTID)
)
.arg(Arg::with_name("fee")
.short("f")
.long("fee")
.help("The fee for the confidential transfer. (default: 1)")
.takes_value(true)
.required(false)
.default_value(DEFAULT_FEE)
)
// .arg(Arg::with_name("url")
// .short("u")
// .long("url")
Expand Down Expand Up @@ -342,6 +359,8 @@ fn cli() -> Result<(), String> {

let amount_str = sub_matches.value_of("amount").unwrap();
let amount: u32 = amount_str.parse().unwrap();
let fee_str = sub_matches.value_of("fee").unwrap();
let fee: u32 = fee_str.parse().unwrap();

let balance_str = sub_matches.value_of("balance").unwrap();
let balance: u32 = balance_str.parse().unwrap();
Expand All @@ -364,7 +383,6 @@ fn cli() -> Result<(), String> {
let ciphertext_balance_v = hex::decode(ciphertext_balance_a).unwrap();
let ciphertext_balance = elgamal::Ciphertext::read(&mut &ciphertext_balance_v[..], &PARAMS as &JubjubBls12).unwrap();

let fee = 1; // tmp fee is fixed value 1.
let remaining_balance = balance - amount - fee;

let tx = Transaction::gen_tx(
Expand All @@ -380,38 +398,40 @@ fn cli() -> Result<(), String> {
fee
).expect("fails to generate the tx");

// println!(
// "
// \nEncrypted fee by sender: 0x{}
// \nzkProof: 0x{}
// \nEncrypted amount by sender: 0x{}
// \nEncrypted amount by recipient: 0x{}
// ",
// HexDisplay::from(&tx.enc_fee as &AsBytesRef),
// HexDisplay::from(&&tx.proof[..] as &AsBytesRef),
// HexDisplay::from(&tx.enc_val_sender as &AsBytesRef),
// HexDisplay::from(&tx.enc_val_recipient as &AsBytesRef),
// );
println!(
"
\nzkProof(Alice): 0x{}
\naddress_sender(Alice): 0x{}
\naddress_recipient(Alice): 0x{}
\nvalue_sender(Alice): 0x{}
\nvalue_recipient(Alice): 0x{}
\nbalance_sender(Alice): 0x{}
\nrvk(Alice): 0x{}
\nrsk(Alice): 0x{}
\nEncrypted fee by sender: 0x{}
\nzkProof: 0x{}
\nEncrypted amount by sender: 0x{}
\nEncrypted amount by recipient: 0x{}
",
HexDisplay::from(&tx.enc_fee as &AsBytesRef),
HexDisplay::from(&&tx.proof[..] as &AsBytesRef),
HexDisplay::from(&tx.address_sender as &AsBytesRef),
HexDisplay::from(&tx.address_recipient as &AsBytesRef),
HexDisplay::from(&tx.enc_val_sender as &AsBytesRef),
HexDisplay::from(&tx.enc_val_recipient as &AsBytesRef),
HexDisplay::from(&tx.enc_bal_sender as &AsBytesRef),
HexDisplay::from(&tx.rvk as &AsBytesRef),
HexDisplay::from(&tx.rsk as &AsBytesRef),
HexDisplay::from(&tx.enc_fee as &AsBytesRef),
);
// println!(
// "
// \nzkProof(Alice): 0x{}
// \naddress_sender(Alice): 0x{}
// \naddress_recipient(Alice): 0x{}
// \nvalue_sender(Alice): 0x{}
// \nvalue_recipient(Alice): 0x{}
// \nbalance_sender(Alice): 0x{}
// \nrvk(Alice): 0x{}
// \nrsk(Alice): 0x{}
// ",
// HexDisplay::from(&&tx.proof[..] as &AsBytesRef),
// HexDisplay::from(&tx.address_sender as &AsBytesRef),
// HexDisplay::from(&tx.address_recipient as &AsBytesRef),
// HexDisplay::from(&tx.enc_val_sender as &AsBytesRef),
// HexDisplay::from(&tx.enc_val_recipient as &AsBytesRef),
// HexDisplay::from(&tx.enc_bal_sender as &AsBytesRef),
// HexDisplay::from(&tx.rvk as &AsBytesRef),
// HexDisplay::from(&tx.rsk as &AsBytesRef),
// );

if let Some(value) = sub_matches.value_of("is-submitting") {
match value.parse() {
Expand All @@ -434,6 +454,7 @@ fn cli() -> Result<(), String> {
zCiphertext::from_slice(&tx.enc_val_sender[..]),
zCiphertext::from_slice(&tx.enc_val_recipient[..]),
sig_vk,
zCiphertext::from_slice(&tx.enc_fee[..]),
));

let era = Era::Immortal;
Expand Down Expand Up @@ -466,7 +487,7 @@ fn cli() -> Result<(), String> {
// if url_str.len() != 0 {
// url = Url::Custom(url_str);
// }

println!("Computing zk proof...");
let api = Api::init(Url::Local);

let rng = &mut OsRng::new().expect("should be able to construct RNG");
Expand Down Expand Up @@ -498,6 +519,8 @@ fn cli() -> Result<(), String> {
let seed = hex::decode(sub_matches.value_of("sender-seed").unwrap()).unwrap();
let amount_str = sub_matches.value_of("amount").unwrap();
let amount: u32 = amount_str.parse().unwrap();
let fee_str = sub_matches.value_of("fee").unwrap();
let fee: u32 = fee_str.parse().unwrap();

let origin_key = bytes_to_uniform_fs::<Bls12>(&seed[..]);
let decryption_key = ProofGenerationKey::<Bls12>::from_seed(&seed[..], &PARAMS).bdk();
Expand All @@ -508,7 +531,7 @@ fn cli() -> Result<(), String> {
let recipient_encryption_key = hex::decode(sub_matches.value_of("recipient-encryption-key").unwrap()).unwrap();

let (decrypted_balance, encrypted_balance_vec, _) = get_balance_from_decryption_key(&decrypted_key[..] ,api.clone());
let remaining_balance = decrypted_balance - amount;
let remaining_balance = decrypted_balance - amount - fee;

let recipient_account_id = EncryptionKey::<Bls12>::read(&mut &recipient_encryption_key[..], &PARAMS).unwrap();
let encrypted_balance = elgamal::Ciphertext::read(&mut &encrypted_balance_vec[..], &PARAMS as &JubjubBls12).unwrap();
Expand All @@ -523,7 +546,8 @@ fn cli() -> Result<(), String> {
&recipient_account_id,
&origin_key,
encrypted_balance,
rng
rng,
fee
).expect("fails to generate the tx");


Expand All @@ -546,6 +570,7 @@ fn cli() -> Result<(), String> {
zCiphertext::from_slice(&tx.enc_val_sender[..]),
zCiphertext::from_slice(&tx.enc_val_recipient[..]),
sig_vk,
zCiphertext::from_slice(&tx.enc_fee[..]),
));

let era = Era::Immortal;
Expand Down
Binary file modified demo/cli/verification.params
Binary file not shown.
9 changes: 5 additions & 4 deletions runtime/src/conf_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,13 @@ mod tests {
#[test]
fn test_call_function() {
with_externalities(&mut new_test_ext(), || {
let proof: [u8; 192] = hex!("a1200c59de33436d49519ef18ef97fbae1b7187b5fdc2abcb7e323b526fd79947f72698d94c78efe8e4a72eb147078abb9d7d353bfc5a89cba4f1e4ad22224bfe97f8b26b04d45f3986ce82692bc56324391c1ff1363cdc05795add569306ec7052132f40f7491446f7a58e9078f63ea1200a8c633ae43b52e14ee028ba26953bc011c8f7a3766bc481b0d31aee56bb0a5b69897f99a2da23cd43d69bd32e44895303083179202608ac24fcff1f532271642ad3d24959c0a0e00b15dba81fcfc");
let proof: [u8; 192] = hex!("a7e763cbdc1d4b78e70534894d9dbef78ac259ab0cd602e65d31459dd03432c5e14dbef9484a9ab36d9db17ad531b50aa8d051dc885599fbefcd1992437ee3453ef66d5921b9082c5ac93ddf7370dac444050147a71849cc1d16d4208984335d1567bb676a30974e8ae228741adbf6ac50d3c35ee14e835762bc4868e6f22d7b69ccbbbc5cfc3fbe49968c1873a99ffcacb71b1139806166e5c491ff9addbcbabc9df058371ef989219ba20c6a718317b4586bbf1d429d4bf4dab47e130bd23f");
let pkd_addr_alice: [u8; 32] = hex!("fd0c0c0183770c99559bf64df4fe23f77ced9b8b4d02826a282bcd125117dcc2");
let pkd_addr_bob: [u8; 32] = hex!("45e66da531088b55dcb3b273ca825454d79d2d1d5c4fa2ba4a12c1fa1ccd6389");
let enc10_by_alice: [u8; 64] = hex!("5bdecb08dbc3a38be4217c939c30768d990e789431aeee4832cfca84bf04c650eb4ccfaac7dbb7c20dfcf8eea5fe184bacaf249c3e40920d2855013fce9d876d");
let enc10_by_bob: [u8; 64] = hex!("c8cd8d37f214f5f000f47e899cd1839a96b42bd98cc3abe42e7261ed083e6d1ceb4ccfaac7dbb7c20dfcf8eea5fe184bacaf249c3e40920d2855013fce9d876d");
let enc10_by_alice: [u8; 64] = hex!("087d5aa97ed351a81cea9e7bb46c83bb4a889bc696f623e7812fc59509cc3a6c997173e746fe32c12a70584cdf9dce783cf3daf44c17d40142f2c460324355aa");
let enc10_by_bob: [u8; 64] = hex!("88c851325af572216ececdc2e120bfa972ed9e6b901ee45e31288abd84c3b6be997173e746fe32c12a70584cdf9dce783cf3daf44c17d40142f2c460324355aa");
let rvk: [u8; 32] = hex!("f539db3c0075f6394ff8698c95ca47921669c77bb2b23b366f42a39b05a88c96");
let enc1_by_alice: [u8; 64] = hex!("55a75030bd77f5b7914b55575c154f61a721e05df076546d815e877d71ac6dcc997173e746fe32c12a70584cdf9dce783cf3daf44c17d40142f2c460324355aa");

assert_ok!(ConfTransfer::confidential_transfer(
Origin::signed(1),
Expand All @@ -356,7 +357,7 @@ mod tests {
Ciphertext::from_slice(&enc10_by_alice[..]),
Ciphertext::from_slice(&enc10_by_bob[..]),
SigVerificationKey::from_slice(&rvk),
None // fee
Ciphertext::from_slice(&enc1_by_alice[..])
));
})
}
Expand Down
Binary file not shown.
Binary file not shown.

0 comments on commit c20f919

Please sign in to comment.