Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add owns and owns_unchecked to SecretKey #216

Merged
merged 2 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add a light sync method in the `ViewKey` [#199]
- Add function `value_commitment` [#201]
- Add function `transparent_value_commitment` [#201]
- Add `owns()` and `owns_unchecked()` to `Secretkey` [#146]

### Changed

Expand Down Expand Up @@ -345,6 +346,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Canonical implementation shielded by feature.

<!-- ISSUES -->
[#146]: https://github.com/dusk-network/phoenix/issues/146
[#208]: https://github.com/dusk-network/phoenix/issues/208
[#201]: https://github.com/dusk-network/phoenix/issues/201
[#199]: https://github.com/dusk-network/phoenix/issues/199
Expand Down
25 changes: 23 additions & 2 deletions core/src/keys/secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use crate::{keys::hash, StealthAddress};
use crate::{keys::hash, Note, StealthAddress};

use dusk_jubjub::JubJubScalar;
use dusk_jubjub::{JubJubScalar, GENERATOR_EXTENDED};
use ff::Field;
use jubjub_schnorr::SecretKey as NoteSecretKey;
use zeroize::Zeroize;
Expand Down Expand Up @@ -86,6 +86,27 @@ impl SecretKey {

NoteSecretKey::from(hash(&aR) + self.b)
}

/// Checks if `note_pk ?= (H(R · a) + b) · G`
pub fn owns(&self, note: &Note) -> bool {
let sa = note.stealth_address();

let aR = sa.R() * self.a();
let hash_aR = hash(&aR);
let note_sk = hash_aR + self.b();

let note_pk = GENERATOR_EXTENDED * note_sk;

sa.note_pk().as_ref() == &note_pk
}

/// Checks if `k_sync ?= R_sync · a`
pub fn owns_unchecked(&self, note: &Note) -> bool {
let sa = note.sync_address();
let aR = sa.R() * self.a();

sa.k() == &aR
}
}

impl ConstantTimeEq for SecretKey {
Expand Down
6 changes: 3 additions & 3 deletions core/src/keys/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ impl ViewKey {
let sa = note.stealth_address();

let aR = sa.R() * self.a();
let aR = hash(&aR);
let aR = GENERATOR_EXTENDED * aR;
let note_pk = aR + self.B();
let hash_aR = hash(&aR);
let hash_aR_G = GENERATOR_EXTENDED * hash_aR;
let note_pk = hash_aR_G + self.B();

sa.note_pk().as_ref() == &note_pk
}
Expand Down
4 changes: 4 additions & 0 deletions core/tests/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ fn keys_consistency() {

assert!(vk.owns(&note));
assert!(vk.owns_unchecked(&note));
assert!(sk.owns(&note));
assert!(sk.owns_unchecked(&note));

let wrong_sk = SecretKey::random(&mut rng);
let wrong_vk = ViewKey::from(&wrong_sk);
Expand All @@ -79,6 +81,8 @@ fn keys_consistency() {

assert!(!wrong_vk.owns(&note));
assert!(!wrong_vk.owns_unchecked(&note));
assert!(!wrong_sk.owns(&note));
assert!(!wrong_sk.owns_unchecked(&note));

let sa = pk.gen_stealth_address(&r);

Expand Down