Skip to content

Commit 835321d

Browse files
committed
derive rho from random value and not secret key and change registration
1 parent 6d09f67 commit 835321d

18 files changed

Lines changed: 915 additions & 254 deletions

.github/workflows/dart-bp-tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ jobs:
4949
run: cargo test --release --package polymesh-dart-bp
5050

5151
- name: Run tests with ignore_prover_input_sanitation feature
52-
run: cargo test --release --package polymesh-dart-bp --features ignore_prover_input_sanitation input_sanitation_disabled
52+
run: cargo test --package polymesh-dart-bp --features ignore_prover_input_sanitation input_sanitation_disabled
5353

5454
test-wasm:
5555
name: Build dart-bp for WASM target
@@ -76,4 +76,4 @@ jobs:
7676
${{ runner.os }}-cargo-
7777
7878
- name: Build for WASM
79-
run: cargo build --package polymesh-dart-bp --target wasm32-unknown-unknown --no-default-features
79+
run: cargo build --package polymesh-dart-bp --target wasm32-unknown-unknown --no-default-features

dart-bp/benches/affirmation_proofs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ fn create_account_and_tree<
141141
let asset_id = 1;
142142
let id = PallasFr::rand(rng);
143143
let poseidon_config = get_poseidon2_params_for_2_1_hashing().unwrap();
144-
let mut account =
144+
let (mut account, _) =
145145
AccountState::new(rng, id, sk.0, sk_enc.0, asset_id, 0, poseidon_config).unwrap();
146146
account.balance = 200;
147147

dart-bp/benches/fee_account_proofs.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,7 @@ fn bench_fee_account_topup_verification(c: &mut Criterion) {
110110
);
111111

112112
let nonce = b"test-nonce";
113-
let updated_account = account
114-
.get_state_for_topup(&mut setup_rng, increase_bal_by)
115-
.unwrap();
113+
let updated_account = account.get_state_for_topup(increase_bal_by).unwrap();
116114
let updated_account_comm = updated_account.commit(account_comm_key.clone()).unwrap();
117115
let path = account_tree.get_path_to_leaf_for_proof(0, 0).unwrap();
118116
let root = account_tree.root_node();
@@ -176,9 +174,7 @@ fn bench_fee_account_topup_verification_with_rmc(c: &mut Criterion) {
176174
);
177175

178176
let nonce = b"test-nonce";
179-
let updated_account = account
180-
.get_state_for_topup(&mut setup_rng, increase_bal_by)
181-
.unwrap();
177+
let updated_account = account.get_state_for_topup(increase_bal_by).unwrap();
182178
let updated_account_comm = updated_account.commit(account_comm_key.clone()).unwrap();
183179
let path = account_tree.get_path_to_leaf_for_proof(0, 0).unwrap();
184180
let root = account_tree.root_node();
@@ -247,9 +243,7 @@ fn bench_fee_payment_verification(c: &mut Criterion) {
247243

248244
// Or could be hash(a_txn_id, a_payee_id)
249245
let nonce = b"a_txn_id,a_payee_id";
250-
let updated_account = account
251-
.get_state_for_payment(&mut setup_rng, fee_amount)
252-
.unwrap();
246+
let updated_account = account.get_state_for_payment(fee_amount).unwrap();
253247
let updated_account_comm = updated_account.commit(account_comm_key.clone()).unwrap();
254248
let path = account_tree.get_path_to_leaf_for_proof(0, 0).unwrap();
255249
let root = account_tree.root_node();
@@ -312,9 +306,7 @@ fn bench_fee_payment_verification_with_rmc(c: &mut Criterion) {
312306

313307
// Or could be hash(a_txn_id, a_payee_id)
314308
let nonce = b"a_txn_id,a_payee_id";
315-
let updated_account = account
316-
.get_state_for_payment(&mut setup_rng, fee_amount)
317-
.unwrap();
309+
let updated_account = account.get_state_for_payment(fee_amount).unwrap();
318310
let updated_account_comm = updated_account.commit(account_comm_key.clone()).unwrap();
319311
let path = account_tree.get_path_to_leaf_for_proof(0, 0).unwrap();
320312
let root = account_tree.root_node();

dart-bp/benches/settlement_proofs.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ fn new_account<R: CryptoRngCore>(
4545
id: PallasFr,
4646
) -> AccountState<PallasA> {
4747
let poseidon_config = get_poseidon2_params_for_2_1_hashing().unwrap();
48-
AccountState::new(rng, id, sk.0, sk_enc.0, asset_id, 0, poseidon_config).unwrap()
48+
AccountState::new(rng, id, sk.0, sk_enc.0, asset_id, 0, poseidon_config)
49+
.unwrap()
50+
.0
4951
}
5052

5153
/// Create shared setup params

dart-bp/src/account/mint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ mod tests {
650650
let (sk_enc, _) = keygen_enc(&mut rng, account_comm_key.sk_enc_gen());
651651

652652
let id = PallasFr::rand(&mut rng);
653-
let (account, _, _) = new_account(&mut rng, asset_id, sk_i, sk_enc, id.clone());
653+
let (account, _, _, _) = new_account(&mut rng, asset_id, sk_i, sk_enc, id.clone());
654654

655655
let account_tree = get_tree_with_account_comm::<L, _>(
656656
&account,

dart-bp/src/account/pob.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ mod tests {
911911
let (sk_enc, _) = keygen_enc(&mut rng, enc_key_gen);
912912
// Account exists with some balance and pending txns
913913
let id = PallasFr::rand(&mut rng);
914-
let (mut account, _, _) = new_account(&mut rng, asset_id, sk, sk_enc, id.clone());
914+
let (mut account, _, _, _) = new_account(&mut rng, asset_id, sk, sk_enc, id.clone());
915915
account.balance = 1000;
916916
account.counter = 7;
917917
let account_comm = account.commit(account_comm_key.clone()).unwrap();
@@ -959,7 +959,7 @@ mod tests {
959959

960960
// Account exists with some balance and pending txns
961961
let id = PallasFr::rand(&mut rng);
962-
let (mut account, _, _) = new_account(&mut rng, asset_id, sk, sk_e, id.clone());
962+
let (mut account, _, _, _) = new_account(&mut rng, asset_id, sk, sk_e, id.clone());
963963
account.balance = 1000000;
964964
account.counter = num_pending_txns;
965965
let account_comm = account.commit(account_comm_key.clone()).unwrap();
@@ -1082,7 +1082,7 @@ mod tests {
10821082
let num_pending_txns = 20;
10831083

10841084
let id = PallasFr::rand(&mut rng);
1085-
let (mut account, _, _) = new_account(&mut rng, asset_id, sk, sk_e, id.clone());
1085+
let (mut account, _, _, _) = new_account(&mut rng, asset_id, sk, sk_e, id.clone());
10861086
account.balance = 1000000;
10871087
account.counter = num_pending_txns;
10881088
let account_comm = account.commit(account_comm_key.clone()).unwrap();
@@ -1213,7 +1213,7 @@ mod tests {
12131213
let num_pending_txns = 10;
12141214

12151215
let id = PallasFr::rand(&mut rng);
1216-
let (mut account, _, _) = new_account(&mut rng, asset_id, sk, sk_e, id);
1216+
let (mut account, _, _, _) = new_account(&mut rng, asset_id, sk, sk_e, id);
12171217
account.balance = 1000000;
12181218
account.counter = num_pending_txns;
12191219
let account_comm = account.commit(account_comm_key.clone()).unwrap();
@@ -1322,7 +1322,7 @@ mod tests {
13221322
let num_pending_txns = 12;
13231323

13241324
let id = PallasFr::rand(&mut rng);
1325-
let (mut account, _, _) = new_account(&mut rng, asset_id, sk, sk_e, id);
1325+
let (mut account, _, _, _) = new_account(&mut rng, asset_id, sk, sk_e, id);
13261326
account.balance = 1000000;
13271327
account.counter = num_pending_txns;
13281328
let account_comm = account.commit(account_comm_key.clone()).unwrap();
@@ -1455,7 +1455,7 @@ mod tests {
14551455
let num_pending_txns = 6;
14561456

14571457
let id = PallasFr::rand(&mut rng);
1458-
let (mut account, _, _) = new_account(&mut rng, asset_id, sk, sk_e, id);
1458+
let (mut account, _, _, _) = new_account(&mut rng, asset_id, sk, sk_e, id);
14591459
account.balance = 1000000;
14601460
account.counter = num_pending_txns;
14611461
let account_comm = account.commit(account_comm_key.clone()).unwrap();

dart-bp/src/account/state.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ where
315315
{
316316
/// `sk_aff` is the affirmation secret key
317317
/// `sk_enc` is the encryption secret key
318+
/// Returns `(AccountState, rho_randomness)` where `rho_randomness` is needed for the registration proof.
318319
pub fn new<R: CryptoRngCore>(
319320
rng: &mut R,
320321
id: G::ScalarField, // User can hash its string ID onto the field
@@ -323,36 +324,43 @@ where
323324
asset_id: AssetId,
324325
counter: NullifierSkGenCounter,
325326
poseidon_config: Poseidon2Params<G::ScalarField>,
326-
) -> Result<Self> {
327+
) -> Result<(Self, G::ScalarField)> {
327328
let randomness = G::ScalarField::rand(rng);
328-
Self::new_given_randomness(
329+
let rho_randomness = G::ScalarField::rand(rng);
330+
let account = Self::new_given_randomness(
329331
id,
330332
sk_aff,
331333
sk_enc,
332334
asset_id,
333335
counter,
334336
randomness,
337+
rho_randomness,
335338
poseidon_config,
336-
)
339+
)?;
340+
Ok((account, rho_randomness))
337341
}
338342

343+
/// `rho_randomness` is the random value used to derive `rho` via Poseidon hash. It is NOT
344+
/// stored in `AccountState` and must be retained by the caller for creating the registration proof.
339345
pub fn new_given_randomness(
340346
id: G::ScalarField, // User can hash its string ID onto the field
341347
sk_aff: G::ScalarField,
342348
sk_enc: G::ScalarField,
343349
asset_id: AssetId,
344350
counter: NullifierSkGenCounter,
345351
randomness: G::ScalarField,
352+
rho_randomness: G::ScalarField,
346353
poseidon_config: Poseidon2Params<G::ScalarField>,
347354
) -> Result<Self> {
348355
if asset_id > MAX_ASSET_ID {
349356
return Err(Error::AssetIdTooLarge(asset_id));
350357
}
351358
let sk_enc_inv = sk_enc.inverse().ok_or(Error::InvertingZero)?;
352359
let combined = Self::concat_asset_id_counter(asset_id, counter);
353-
let rho = Poseidon_hash_2_simple::<G::ScalarField>(sk_aff, combined, poseidon_config)?;
354-
let current_rho = rho;
355-
let current_randomness = randomness;
360+
let rho =
361+
Poseidon_hash_2_simple::<G::ScalarField>(rho_randomness, combined, poseidon_config)?;
362+
let current_rho = rho.square();
363+
let current_randomness = randomness.square();
356364

357365
Ok(Self {
358366
id,
@@ -520,8 +528,8 @@ where
520528
/// Set rho and commitment randomness to new values. Used as each update to the account state
521529
/// needs these refreshed.
522530
pub fn refresh_randomness_for_state_change(&mut self) {
523-
self.current_rho = self.current_rho * self.rho;
524-
self.current_randomness = self.current_randomness * self.randomness;
531+
self.current_rho *= self.rho;
532+
self.current_randomness *= self.randomness;
525533
}
526534

527535
pub fn nullifier(&self, comm_key: &impl AccountCommitmentKeyTrait<G>) -> G {

dart-bp/src/account/state_transition.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ mod tests {
943943

944944
// Create Carol's account
945945
let carol_id = PallasFr::rand(&mut rng);
946-
let (mut carol_account, _, _) = new_account(
946+
let (mut carol_account, _, _, _) = new_account(
947947
&mut rng,
948948
asset_id,
949949
sk_carol.clone(),
@@ -1170,7 +1170,7 @@ mod tests {
11701170

11711171
// Create Alice's account
11721172
let alice_id = PallasFr::rand(&mut rng);
1173-
let (mut alice_account, _, _) =
1173+
let (mut alice_account, _, _, _) =
11741174
new_account(&mut rng, asset_id, sk_alice, sk_alice_e, alice_id);
11751175
alice_account.balance = 1000;
11761176

@@ -1387,7 +1387,7 @@ mod tests {
13871387

13881388
// Create Alice's account
13891389
let alice_id = PallasFr::rand(&mut rng);
1390-
let (mut alice_account, _, _) =
1390+
let (mut alice_account, _, _, _) =
13911391
new_account(&mut rng, asset_id, sk_alice, sk_alice_e, alice_id);
13921392
alice_account.balance = 1000;
13931393

@@ -1518,7 +1518,7 @@ mod tests {
15181518

15191519
// Create Alice's account
15201520
let alice_id = PallasFr::rand(&mut rng);
1521-
let (mut alice_account, _, _) =
1521+
let (mut alice_account, _, _, _) =
15221522
new_account(&mut rng, asset_id, sk_alice, sk_alice_e, alice_id);
15231523
alice_account.balance = 1000;
15241524

@@ -1677,7 +1677,7 @@ mod tests {
16771677

16781678
// Create Alice's accounts for both assets
16791679
let alice_id = PallasFr::rand(&mut rng);
1680-
let (mut alice_account_asset1, _, _) = new_account(
1680+
let (mut alice_account_asset1, _, _, _) = new_account(
16811681
&mut rng,
16821682
asset_id_1,
16831683
sk_alice.clone(),
@@ -1686,7 +1686,7 @@ mod tests {
16861686
);
16871687
alice_account_asset1.balance = 1000;
16881688

1689-
let (mut alice_account_asset2, _, _) =
1689+
let (mut alice_account_asset2, _, _, _) =
16901690
new_account(&mut rng, asset_id_2, sk_alice, sk_alice_e, alice_id);
16911691
alice_account_asset2.balance = 2000;
16921692

@@ -2019,7 +2019,7 @@ mod tests {
20192019
.unwrap();
20202020

20212021
let alice_id = PallasFr::rand(&mut rng);
2022-
let (mut alice_account, _, _) =
2022+
let (mut alice_account, _, _, _) =
20232023
new_account(&mut rng, asset_id, sk_alice, sk_alice_e, alice_id);
20242024
alice_account.balance = 1000;
20252025

0 commit comments

Comments
 (0)