diff --git a/core/proofs/src/circuit_transfer.rs b/core/proofs/src/circuit_transfer.rs index 978149ca..1fb530eb 100644 --- a/core/proofs/src/circuit_transfer.rs +++ b/core/proofs/src/circuit_transfer.rs @@ -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()); diff --git a/demo/cli/proving.params b/demo/cli/proving.params index 1ed20b67..242b3caf 100644 Binary files a/demo/cli/proving.params and b/demo/cli/proving.params differ diff --git a/demo/cli/src/main.rs b/demo/cli/src/main.rs index b4aed3f2..a7404a6c 100644 --- a/demo/cli/src/main.rs +++ b/demo/cli/src/main.rs @@ -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"; @@ -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") @@ -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") @@ -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(); @@ -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( @@ -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() { @@ -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; @@ -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"); @@ -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::(&seed[..]); let decryption_key = ProofGenerationKey::::from_seed(&seed[..], &PARAMS).bdk(); @@ -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::::read(&mut &recipient_encryption_key[..], &PARAMS).unwrap(); let encrypted_balance = elgamal::Ciphertext::read(&mut &encrypted_balance_vec[..], &PARAMS as &JubjubBls12).unwrap(); @@ -523,7 +546,8 @@ fn cli() -> Result<(), String> { &recipient_account_id, &origin_key, encrypted_balance, - rng + rng, + fee ).expect("fails to generate the tx"); @@ -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; diff --git a/demo/cli/verification.params b/demo/cli/verification.params index 5295212a..74b4522e 100644 Binary files a/demo/cli/verification.params and b/demo/cli/verification.params differ diff --git a/runtime/src/conf_transfer.rs b/runtime/src/conf_transfer.rs index 3436f840..f935707e 100644 --- a/runtime/src/conf_transfer.rs +++ b/runtime/src/conf_transfer.rs @@ -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), @@ -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[..]) )); }) } diff --git a/runtime/wasm/target/wasm32-unknown-unknown/release/zero_chain_runtime_wasm.compact.wasm b/runtime/wasm/target/wasm32-unknown-unknown/release/zero_chain_runtime_wasm.compact.wasm index efb8937f..33a5101d 100644 Binary files a/runtime/wasm/target/wasm32-unknown-unknown/release/zero_chain_runtime_wasm.compact.wasm and b/runtime/wasm/target/wasm32-unknown-unknown/release/zero_chain_runtime_wasm.compact.wasm differ diff --git a/runtime/wasm/target/wasm32-unknown-unknown/release/zero_chain_runtime_wasm.wasm b/runtime/wasm/target/wasm32-unknown-unknown/release/zero_chain_runtime_wasm.wasm index 8c41d0a0..2cefb3b0 100755 Binary files a/runtime/wasm/target/wasm32-unknown-unknown/release/zero_chain_runtime_wasm.wasm and b/runtime/wasm/target/wasm32-unknown-unknown/release/zero_chain_runtime_wasm.wasm differ