Skip to content

Commit 553e7e0

Browse files
committed
Add nonce to tx
1 parent e13ce9a commit 553e7e0

7 files changed

Lines changed: 54 additions & 19 deletions

File tree

core/primitives/src/g_epoch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use pairing::bls12_381::Bls12;
1010
use pairing::io;
1111
use parity_codec::{Encode, Decode, Input};
1212
use byteorder::{ByteOrder, LittleEndian};
13-
use std::convert::TryFrom;
13+
use core::convert::TryFrom;
1414

1515
const SIZE: usize = 32;
1616
const GEPOCH_PERSONALIZATION: &[u8; 8] = b"zcgepoch";

core/primitives/src/nonce.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use pairing::bls12_381::Bls12;
88
use jubjub::curve::{edwards, PrimeOrder, Unknown};
99
use pairing::io;
1010
use parity_codec::{Encode, Decode, Input};
11-
use std::convert::TryFrom;
11+
use core::convert::TryFrom;
1212

1313
const SIZE: usize = 32;
1414

core/proofs/src/circuit/transfer.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,8 @@ impl<'a, E: JubjubEngine> Circuit<E> for Transfer<'a, E> {
378378
params
379379
)?;
380380

381-
nonce.inputize(cs.namespace(|| "nonce"))?;
381+
g_epoch.inputize(cs.namespace(|| "inputize g_epoch"))?;
382+
nonce.inputize(cs.namespace(|| "inputize nonce"))?;
382383
}
383384

384385
Ok(())
@@ -436,6 +437,7 @@ mod tests {
436437

437438
let rvk = proof_gen_key.into_rvk(alpha, params).0.into_xy();
438439
let g_epoch = edwards::Point::rand(rng, params).mul_by_cofactor(params);
440+
let g_epoch_xy = g_epoch.into_xy();
439441
let nonce = g_epoch.mul(dec_key.0, params).into_xy();
440442

441443
let mut cs = TestConstraintSystem::<Bls12>::new();
@@ -457,10 +459,10 @@ mod tests {
457459
instance.synthesize(&mut cs).unwrap();
458460

459461
assert!(cs.is_satisfied());
460-
assert_eq!(cs.num_constraints(), 25051);
461-
assert_eq!(cs.hash(), "d429836034d9816ffd3e157e5de055450c00ad6027e1cb0b8ecb06e390a60adc");
462+
assert_eq!(cs.num_constraints(), 25053);
463+
assert_eq!(cs.hash(), "fabd4cb7d2ebbdb643eefe54b21a4c2d802544ea860c485a14532b2cd1194b4f");
462464

463-
assert_eq!(cs.num_inputs(), 21);
465+
assert_eq!(cs.num_inputs(), 23);
464466
assert_eq!(cs.get_input(0, "ONE"), Fr::one());
465467
assert_eq!(cs.get_input(1, "inputize enc_key_sender/x/input variable"), address_sender_xy.0);
466468
assert_eq!(cs.get_input(2, "inputize enc_key_sender/y/input variable"), address_sender_xy.1);
@@ -480,8 +482,10 @@ mod tests {
480482
assert_eq!(cs.get_input(16, "inputize pointr/y/input variable"), c_bal_right.1);
481483
assert_eq!(cs.get_input(17, "rvk/x/input variable"), rvk.0);
482484
assert_eq!(cs.get_input(18, "rvk/y/input variable"), rvk.1);
483-
assert_eq!(cs.get_input(19, "nonce/x/input variable"), nonce.0);
484-
assert_eq!(cs.get_input(20, "nonce/y/input variable"), nonce.1);
485+
assert_eq!(cs.get_input(19, "inputize g_epoch/x/input variable"), g_epoch_xy.0);
486+
assert_eq!(cs.get_input(20, "inputize g_epoch/y/input variable"), g_epoch_xy.1);
487+
assert_eq!(cs.get_input(21, "inputize nonce/x/input variable"), nonce.0);
488+
assert_eq!(cs.get_input(22, "inputize nonce/y/input variable"), nonce.1);
485489
}
486490

487491
#[test]

core/proofs/src/prover.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ pub struct ConfidentialProof<E: JubjubEngine> {
128128
pub enc_keys: MultiEncKeys<E>,
129129
pub multi_ciphertexts: MultiCiphertexts<E>,
130130
pub cipher_balance: Ciphertext<E>,
131+
pub nonce: edwards::Point<E, PrimeOrder>,
131132
}
132133

133134
impl<E: JubjubEngine> ConfidentialProof<E> {
@@ -157,6 +158,7 @@ impl<E: JubjubEngine> ConfidentialProof<E> {
157158
FixedGenerators::NoteCommitmentRandomness,
158159
params,
159160
);
161+
let nonce = g_epoch.mul(dec_key_sender.0, params);
160162

161163
let instance = Transfer {
162164
params: params,
@@ -175,7 +177,7 @@ impl<E: JubjubEngine> ConfidentialProof<E> {
175177
// Crate proof
176178
let proof = create_random_proof(instance, proving_key, rng)?;
177179

178-
let mut public_input = [E::Fr::zero(); 18];
180+
let mut public_input = [E::Fr::zero(); 22];
179181
let p_g = FixedGenerators::NoteCommitmentRandomness;
180182

181183
let cipher_sender = Ciphertext::encrypt(
@@ -244,8 +246,18 @@ impl<E: JubjubEngine> ConfidentialProof<E> {
244246
}
245247
{
246248
let (x, y) = rvk.0.into_xy();
247-
public_input[12] = x;
248-
public_input[13] = y;
249+
public_input[16] = x;
250+
public_input[17] = y;
251+
}
252+
{
253+
let (x, y) = g_epoch.into_xy();
254+
public_input[18] = x;
255+
public_input[19] = y;
256+
}
257+
{
258+
let (x, y) = nonce.into_xy();
259+
public_input[20] = x;
260+
public_input[21] = y;
249261
}
250262

251263
// This verification is just an error handling, not validate if it returns `true`,
@@ -261,6 +273,7 @@ impl<E: JubjubEngine> ConfidentialProof<E> {
261273
enc_keys: MultiEncKeys::new_for_confidential(enc_keys.recipient.clone()),
262274
multi_ciphertexts: MultiCiphertexts::new_for_confidential(cipher_sender, cipher_recipient, cipher_fee),
263275
cipher_balance: cipher_balance.clone(),
276+
nonce,
264277
};
265278

266279
Ok(proof)

core/proofs/src/transaction.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub struct Transaction{
2424
pub rsk: [u8; 32], // 32 bytes
2525
pub rvk: [u8; 32], // 32 bytes
2626
pub enc_balance: [u8; 64], // 32 bytes
27+
pub nonce: [u8; 32],
2728
}
2829

2930
impl Transaction {
@@ -64,6 +65,8 @@ impl Transaction {
6465
&PARAMS,
6566
).expect("Should not be faild to generate a proof.");
6667

68+
// TODO: Creating bridge_convert traits between std and no_std.
69+
6770
// Generate the re-randomized sign key
6871
let mut rsk_bytes = [0u8; 32];
6972
spending_key
@@ -118,6 +121,12 @@ impl Transaction {
118121
.write(&mut enc_balance[..])
119122
.map_err(|_| io::Error::InvalidData)?;
120123

124+
let mut nonce = [0u8; 32];
125+
proof_output
126+
.nonce
127+
.write(&mut nonce[..])
128+
.map_err(|_| io::Error::InvalidData)?;
129+
121130
let tx = Transaction {
122131
proof: proof_bytes,
123132
rvk: rvk_bytes,
@@ -128,6 +137,7 @@ impl Transaction {
128137
rsk: rsk_bytes,
129138
enc_fee,
130139
enc_balance,
140+
nonce,
131141
};
132142

133143
Ok(tx)

modules/encrypted-balances/src/lib.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44
use support::{decl_module, decl_storage, decl_event, StorageValue, StorageMap, dispatch::Result, Parameter};
55
use rstd::prelude::*;
66
use rstd::result;
7+
use rstd::convert::TryFrom;
78
use bellman_verifier::verify_proof;
89
use pairing::{
9-
bls12_381::{
10-
Bls12,
11-
Fr,
12-
},
10+
bls12_381::{Bls12,Fr},
1311
Field,
1412
};
1513
use runtime_primitives::traits::{Member, Zero, MaybeSerializeDebug, As};
1614
use jubjub::redjubjub::PublicKey;
15+
use jubjub::curve::{edwards, PrimeOrder};
1716
use zprimitives::{
1817
EncKey, Proof, PreparedVk, ElgamalCiphertext,
1918
SigVk, Nonce, GEpoch,
@@ -29,6 +28,8 @@ pub trait Trait: system::Trait {
2928

3029
/// The units in which we record encrypted balances.
3130
type EncryptedBalance: ElgamalCiphertext + Parameter + Member + Default + MaybeSerializeDebug + Codec;
31+
32+
type Nonce: TryFrom<edwards::Point<Bls12, PrimeOrder>> + Parameter + Member + Default + MaybeSerializeDebug + Codec;
3233
}
3334

3435
pub struct TypedParams {
@@ -56,7 +57,8 @@ decl_module! {
5657
address_recipient: EncKey,
5758
amount_sender: T::EncryptedBalance,
5859
amount_recipient: T::EncryptedBalance,
59-
fee_sender: T::EncryptedBalance
60+
fee_sender: T::EncryptedBalance,
61+
nonce: T::Nonce
6062
) -> Result {
6163
let rvk = ensure_signed(origin)?;
6264

@@ -86,6 +88,9 @@ decl_module! {
8688
let typed_balance_recipient = Self::rollover(&address_recipient)
8789
.map_err(|_| "Invalid ciphertext of recipient balance.")?;
8890

91+
// Veridate the provided nonce isn't included in the nonce pool.
92+
assert!(Self::nonce_pool().contains(&nonce));
93+
8994
// Verify the zk proof
9095
if !Self::validate_proof(
9196
&typed.zkproof,
@@ -99,7 +104,10 @@ decl_module! {
99104
)? {
100105
Self::deposit_event(RawEvent::InvalidZkProof());
101106
return Err("Invalid zkproof");
102-
}
107+
}
108+
109+
// Add a nonce into the nonce pool
110+
Self::nonce_pool().push(nonce);
103111

104112
// Subtracting transferred amount and fee from the sender's encrypted balances.
105113
// This function causes a storage mutation.
@@ -157,7 +165,7 @@ decl_storage! {
157165

158166
/// A nonce pool. All nonces are erasured at the time of starting each epochs.
159167
// Consider chainging Vec to BtreeMap
160-
pub NoncePool get(nonce_pool): Vec<Nonce>;
168+
pub NoncePool get(nonce_pool): Vec<T::Nonce>;
161169
}
162170
}
163171

zface/src/transaction/commands.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use zjubjub::{
1515
redjubjub::PrivateKey as zPrivateKey
1616
};
1717
use zpairing::{bls12_381::Bls12 as zBls12, PrimeField as zPrimeField, PrimeFieldRepr as zPrimeFieldRepr};
18-
use zprimitives::{PARAMS as ZPARAMS, Proof, Ciphertext as zCiphertext, EncKey, SigVerificationKey, RedjubjubSignature, SigVk};
18+
use zprimitives::{PARAMS as ZPARAMS, Proof, Ciphertext as zCiphertext, EncKey, SigVerificationKey, RedjubjubSignature, SigVk, Nonce};
1919
use zerochain_runtime::{UncheckedExtrinsic, Call, EncryptedBalancesCall, EncryptedAssetsCall};
2020
use runtime_primitives::generic::Era;
2121
use parity_codec::{Compact, Encode, Decode};

0 commit comments

Comments
 (0)