Skip to content

Commit

Permalink
Merge pull request #207 from dusk-network/rm-ownable
Browse files Browse the repository at this point in the history
Remove `Ownable` trait
  • Loading branch information
Eduardo Leegwater Simões authored Jun 13, 2024
2 parents ac886f4 + 31c8dfa commit 7a7a12c
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 90 deletions.
4 changes: 2 additions & 2 deletions circuits/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use rand::{CryptoRng, RngCore, SeedableRng};
extern crate alloc;
use alloc::vec::Vec;

use phoenix_core::{Error as PhoenixError, Note, Ownable, SecretKey, ViewKey};
use phoenix_core::{Error as PhoenixError, Note, SecretKey, ViewKey};

use crate::{recipient, recipient::RecipientParameters};

Expand Down Expand Up @@ -61,7 +61,7 @@ impl<const H: usize> TxInputNote<H> {
sk: &SecretKey,
payload_hash: BlsScalar,
) -> Result<crate::transaction::TxInputNote<H>, PhoenixError> {
let note_sk = sk.gen_note_sk(note);
let note_sk = sk.gen_note_sk(note.stealth_address());
let note_pk_p =
JubJubAffine::from(GENERATOR_NUMS_EXTENDED * note_sk.as_ref());

Expand Down
4 changes: 4 additions & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Add `stealth_address` and `sync_address` functions directly to note
- Add a light sync method in the `ViewKey` [#199]
- Add function `value_commitment` [#201]
- Add function `transparent_value_commitment` [#201]

### Changed

- Change `owns` and `owns_unchecked` to take `&Note`
- Change `gen_note_sk` to take `&StealthAddress`
- Rename `crossover` to `deposit` [#190]
- Turn the value-commitment an `JubJubAffine` point [#201]
- Expose `NOTE_ENCRYPTION_SIZE` [#201]
- Make `alloc` a `default` feature [#201]

### Removed

- Remove `Ownable` trait
- Remove `"getrandom"` feature from `aes-gcm` dependency [#195]

## [0.28.1] - 2024-05-23
Expand Down
15 changes: 0 additions & 15 deletions core/src/addresses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,3 @@

pub mod stealth;
pub mod sync;

// /// Hashes a JubJub's ExtendedPoint into a JubJub's Scalar using the JubJub's
// /// hash to scalar function
// pub fn hash(p: &JubJubExtended) -> JubJubScalar {
// JubJubScalar::hash_to_scalar(&JubJubAffine::from(p).to_bytes())
// }

/// The trait `Ownable` is required by any type that wants to prove its
/// ownership.
pub trait Ownable {
/// Returns the associated `SyncAddress`
fn sync_address(&self) -> sync::SyncAddress;
/// Returns the associated `StealthAddress`
fn stealth_address(&self) -> stealth::StealthAddress;
}
22 changes: 1 addition & 21 deletions core/src/addresses/stealth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use crate::{Ownable, SyncAddress};
use crate::SyncAddress;
use dusk_jubjub::{JubJubAffine, JubJubExtended};
use jubjub_schnorr::PublicKey as NotePublicKey;

Expand Down Expand Up @@ -71,26 +71,6 @@ impl PartialEq for StealthAddress {
}
}

impl Ownable for StealthAddress {
fn stealth_address(&self) -> StealthAddress {
*self
}

fn sync_address(&self) -> SyncAddress {
self.into()
}
}

impl Ownable for &StealthAddress {
fn stealth_address(&self) -> StealthAddress {
**self
}

fn sync_address(&self) -> SyncAddress {
SyncAddress::from(*self)
}
}

impl From<&SyncAddress> for StealthAddress {
fn from(sa: &SyncAddress) -> Self {
StealthAddress {
Expand Down
11 changes: 1 addition & 10 deletions core/src/addresses/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use crate::{Ownable, StealthAddress};
use crate::StealthAddress;
use dusk_jubjub::{JubJubAffine, JubJubExtended};
use jubjub_schnorr::PublicKey as NotePublicKey;

Expand Down Expand Up @@ -62,15 +62,6 @@ impl PartialEq for SyncAddress {
}
}

impl Ownable for SyncAddress {
fn stealth_address(&self) -> StealthAddress {
self.into()
}
fn sync_address(&self) -> Self {
*self
}
}

impl From<&StealthAddress> for SyncAddress {
fn from(sa: &StealthAddress) -> Self {
SyncAddress {
Expand Down
7 changes: 4 additions & 3 deletions core/src/keys/secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

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

use dusk_jubjub::JubJubScalar;
use ff::Field;
use jubjub_schnorr::SecretKey as NoteSecretKey;
Expand Down Expand Up @@ -80,8 +81,8 @@ impl SecretKey {

/// Generates a [`NoteSecretKey`] using the `R` of the given
/// [`StealthAddress`]. With the formula: `note_sk = H(a · R) + b`
pub fn gen_note_sk(&self, sa: impl Ownable) -> NoteSecretKey {
let aR = sa.stealth_address().R() * self.a;
pub fn gen_note_sk(&self, sa: &StealthAddress) -> NoteSecretKey {
let aR = sa.R() * self.a;

NoteSecretKey::from(hash(&aR) + self.b)
}
Expand Down
10 changes: 5 additions & 5 deletions core/src/keys/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

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

use dusk_bytes::{DeserializableSlice, Error, Serializable};
use dusk_jubjub::{
Expand Down Expand Up @@ -63,8 +63,8 @@ impl ViewKey {
}

/// Checks `note_pk = H(R · a) · G + B`
pub fn owns(&self, owner: &impl crate::Ownable) -> bool {
let sa = owner.stealth_address();
pub fn owns(&self, note: &Note) -> bool {
let sa = note.stealth_address();

let aR = sa.R() * self.a();
let aR = hash(&aR);
Expand All @@ -75,8 +75,8 @@ impl ViewKey {
}

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

sa.k() == &aR
Expand Down
1 change: 0 additions & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ mod transaction;

pub use addresses::stealth::StealthAddress;
pub use addresses::sync::SyncAddress;
pub use addresses::Ownable;
pub use encryption::aes;
pub use error::Error;
pub use keys::hash;
Expand Down
34 changes: 12 additions & 22 deletions core/src/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use core::convert::{TryFrom, TryInto};

use crate::{
transparent_value_commitment, value_commitment, Error, Ownable, PublicKey,
transparent_value_commitment, value_commitment, Error, PublicKey,
SecretKey, StealthAddress, SyncAddress, ViewKey,
};
use dusk_bls12_381::BlsScalar;
Expand Down Expand Up @@ -235,7 +235,7 @@ impl Note {
///
/// This nullifier is represeted as `H(note_sk · G', pos)`
pub fn gen_nullifier(&self, sk: &SecretKey) -> BlsScalar {
let note_sk = sk.gen_note_sk(self.stealth_address);
let note_sk = sk.gen_note_sk(&self.stealth_address);
let pk_prime = GENERATOR_NUMS_EXTENDED * note_sk.as_ref();
let pk_prime = pk_prime.to_hash_inputs();

Expand Down Expand Up @@ -275,6 +275,16 @@ impl Note {
&self.pos
}

/// Returns the the stealth address associated with the note.
pub const fn stealth_address(&self) -> &StealthAddress {
&self.stealth_address
}

/// Returns the sync address associated with the note.
pub const fn sync_address(&self) -> &SyncAddress {
&self.sync_address
}

/// Set the position of the note on the tree.
/// This, naturally, won't reflect immediatelly on the data storage
pub fn set_pos(&mut self, pos: u64) {
Expand Down Expand Up @@ -327,26 +337,6 @@ impl Note {
}
}

impl Ownable for Note {
fn stealth_address(&self) -> StealthAddress {
self.stealth_address
}

fn sync_address(&self) -> SyncAddress {
self.sync_address
}
}

impl Ownable for &Note {
fn stealth_address(&self) -> StealthAddress {
self.stealth_address
}

fn sync_address(&self) -> SyncAddress {
self.sync_address
}
}

// Serialize into 169 + ENCRYPTION_SIZE bytes, where 169 is the size of all the
// note elements without the encryption. ENCRYPTION_SIZE = PLAINTEXT_SIZE +
// ENCRYPTION_EXTRA_SIZE
Expand Down
18 changes: 10 additions & 8 deletions core/tests/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use dusk_bytes::{DeserializableSlice, Serializable};
use dusk_jubjub::JubJubScalar;
use ff::Field;
use phoenix_core::{PublicKey, SecretKey, ViewKey};
use phoenix_core::{Note, PublicKey, SecretKey, ViewKey};
use rand::rngs::StdRng;
use rand::SeedableRng;
use zeroize::Zeroize;
Expand Down Expand Up @@ -56,29 +56,31 @@ fn keys_encoding() {
fn keys_consistency() {
use dusk_jubjub::{JubJubScalar, GENERATOR_EXTENDED};

const NOTE_VALUE: u64 = 42;

let mut rng = StdRng::seed_from_u64(0xc0b);

let r = JubJubScalar::random(&mut rng);
let r_sync = JubJubScalar::random(&mut rng);

let sk = SecretKey::random(&mut rng);
let pk = PublicKey::from(&sk);
let vk = ViewKey::from(&sk);

let sa = pk.gen_stealth_address(&r);
let sync_address = pk.gen_sync_address(&r_sync);
let note = Note::transparent(&mut rng, &pk, NOTE_VALUE);

assert!(vk.owns(&sa));
assert!(vk.owns_unchecked(&sync_address));
assert!(vk.owns(&note));
assert!(vk.owns_unchecked(&note));

let wrong_sk = SecretKey::random(&mut rng);
let wrong_vk = ViewKey::from(&wrong_sk);

assert_ne!(sk, wrong_sk);
assert_ne!(vk, wrong_vk);

assert!(!wrong_vk.owns(&sa));
assert!(!wrong_vk.owns_unchecked(&sync_address));
assert!(!wrong_vk.owns(&note));
assert!(!wrong_vk.owns_unchecked(&note));

let sa = pk.gen_stealth_address(&r);

let note_sk = sk.gen_note_sk(&sa);
let wrong_note_sk = wrong_sk.gen_note_sk(&sa);
Expand Down
5 changes: 2 additions & 3 deletions core/tests/note_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
use dusk_jubjub::JubJubScalar;
use ff::Field;
use phoenix_core::{
value_commitment, Error, Note, NoteType, Ownable, PublicKey, SecretKey,
ViewKey,
value_commitment, Error, Note, NoteType, PublicKey, SecretKey, ViewKey,
};
use rand::rngs::StdRng;
use rand::SeedableRng;
Expand Down Expand Up @@ -48,7 +47,7 @@ fn transparent_stealth_note() -> Result<(), Error> {

assert_eq!(note.note_type(), NoteType::Transparent);
assert_eq!(value, note.value(None)?);
assert_eq!(sa, note.stealth_address());
assert_eq!(sa, *note.stealth_address());

Ok(())
}
Expand Down

0 comments on commit 7a7a12c

Please sign in to comment.