Skip to content

Commit

Permalink
Merge pull request #220 from dusk-network/mocello/remove_sync
Browse files Browse the repository at this point in the history
core: Remove `SyncAddress`
  • Loading branch information
moCello authored Jun 19, 2024
2 parents 090285f + a31c3dd commit ccc2294
Show file tree
Hide file tree
Showing 11 changed files with 17 additions and 187 deletions.
3 changes: 1 addition & 2 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Add `elgamal::encrypt` and `elgamal::decrypt`
- Add `stealth_address` and `sync_address` functions directly to note [#208]
- Add a light sync method in the `ViewKey` [#199]
- Add `stealth_address` function directly to note [#208]
- Add function `value_commitment` [#201]
- Add function `transparent_value_commitment` [#201]
- Add `owns()` and `owns_unchecked()` to `Secretkey` [#146]
Expand Down
8 changes: 0 additions & 8 deletions core/src/addresses.rs

This file was deleted.

93 changes: 0 additions & 93 deletions core/src/addresses/sync.rs

This file was deleted.

10 changes: 1 addition & 9 deletions core/src/keys/public.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, StealthAddress, SyncAddress, ViewKey};
use crate::{keys::hash, SecretKey, StealthAddress, ViewKey};

use dusk_jubjub::{JubJubAffine, JubJubExtended, JubJubScalar};

Expand Down Expand Up @@ -58,14 +58,6 @@ impl PublicKey {

StealthAddress { R, note_pk }
}

/// Generates new sync address from a given 'r'
pub fn gen_sync_address(&self, r: &JubJubScalar) -> SyncAddress {
let R = GENERATOR_EXTENDED * r;
let k = self.A * r;

SyncAddress { R, k }
}
}

impl ConstantTimeEq for PublicKey {
Expand Down
18 changes: 5 additions & 13 deletions core/src/keys/secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,31 +81,23 @@ 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: &StealthAddress) -> NoteSecretKey {
let aR = sa.R() * self.a;
pub fn gen_note_sk(&self, stealth: &StealthAddress) -> NoteSecretKey {
let aR = stealth.R() * self.a;

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 stealth = note.stealth_address();

let aR = sa.R() * self.a();
let aR = stealth.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
stealth.note_pk().as_ref() == &note_pk
}
}

Expand Down
14 changes: 3 additions & 11 deletions core/src/keys/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,14 @@ impl ViewKey {

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

let aR = sa.R() * self.a();
let aR = stealth.R() * self.a();
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
}

/// Checks `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
stealth.note_pk().as_ref() == &note_pk
}
}

Expand Down
5 changes: 2 additions & 3 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,26 @@
#![deny(missing_docs)]
#![no_std]

mod addresses;
mod encryption;
mod error;
mod keys;
mod note;
mod stealth_address;

#[cfg(feature = "alloc")]
mod transaction;

/// The number of output notes in a transaction
pub const OUTPUT_NOTES: usize = 2;

pub use addresses::stealth::StealthAddress;
pub use addresses::sync::SyncAddress;
pub use encryption::{aes, elgamal};
pub use error::Error;
pub use keys::hash;
pub use keys::public::PublicKey;
pub use keys::secret::SecretKey;
pub use keys::view::ViewKey;
pub use note::{Note, NoteType, VALUE_ENC_SIZE as NOTE_VAL_ENC_SIZE};
pub use stealth_address::StealthAddress;

#[cfg(feature = "alloc")]
/// Transaction Skeleton used by the phoenix transaction model
Expand Down
25 changes: 3 additions & 22 deletions core/src/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use core::convert::{TryFrom, TryInto};

use crate::{
encryption::elgamal, transparent_value_commitment, value_commitment, Error,
PublicKey, SecretKey, StealthAddress, SyncAddress, ViewKey,
PublicKey, SecretKey, StealthAddress, ViewKey,
};
use dusk_bls12_381::BlsScalar;
use dusk_bytes::{DeserializableSlice, Error as BytesError, Serializable};
Expand Down Expand Up @@ -77,7 +77,6 @@ pub struct Note {
pub(crate) note_type: NoteType,
pub(crate) value_commitment: JubJubAffine,
pub(crate) stealth_address: StealthAddress,
pub(crate) sync_address: SyncAddress,
pub(crate) pos: u64,
pub(crate) value_enc: [u8; VALUE_ENC_SIZE],
// the elgamal encryption of the sender_pk encrypted using the output_npk
Expand Down Expand Up @@ -105,9 +104,6 @@ impl Note {
let r = JubJubScalar::random(&mut *rng);
let stealth_address = pk.gen_stealth_address(&r);

let r_sync = JubJubScalar::random(&mut *rng);
let sync_address = pk.gen_sync_address(&r_sync);

let value_commitment = value_commitment(value, value_blinder);

// Output notes have undefined position, equals to u64's MAX value
Expand Down Expand Up @@ -152,7 +148,6 @@ impl Note {
note_type,
value_commitment,
stealth_address,
sync_address,
pos,
value_enc,
sender_enc: [sender_enc_A, sender_enc_B],
Expand Down Expand Up @@ -182,12 +177,11 @@ impl Note {

/// Creates a new transparent note
///
/// This is equivalent to [`transparent`] but taking only a stealth address,
/// sync address, and a value. This is done to be able to generate a note
/// This is equivalent to [`transparent`] but taking only a stealth address
/// and a value. This is done to be able to generate a note
/// directly for a stealth address, as opposed to a public key.
pub fn transparent_stealth(
stealth_address: StealthAddress,
sync_address: SyncAddress,
value: u64,
sender_enc: [(JubJubAffine, JubJubAffine); 2],
) -> Self {
Expand All @@ -202,7 +196,6 @@ impl Note {
note_type: NoteType::Transparent,
value_commitment,
stealth_address,
sync_address,
pos,
value_enc,
sender_enc,
Expand Down Expand Up @@ -238,7 +231,6 @@ impl Note {
note_type: NoteType::Transparent,
value_commitment: JubJubAffine::default(),
stealth_address: StealthAddress::default(),
sync_address: SyncAddress::default(),
pos: 0,
value_enc: [0; VALUE_ENC_SIZE],
sender_enc: [(JubJubAffine::default(), JubJubAffine::default()); 2],
Expand Down Expand Up @@ -319,11 +311,6 @@ impl Note {
&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 @@ -379,7 +366,6 @@ impl Note {
const SIZE: usize = 1
+ JubJubAffine::SIZE
+ StealthAddress::SIZE
+ SyncAddress::SIZE
+ u64::SIZE
+ VALUE_ENC_SIZE
+ 4 * JubJubAffine::SIZE;
Expand All @@ -400,9 +386,6 @@ impl Serializable<SIZE> for Note {
buf[start..start + StealthAddress::SIZE]
.copy_from_slice(&self.stealth_address.to_bytes());
start += StealthAddress::SIZE;
buf[start..start + SyncAddress::SIZE]
.copy_from_slice(&self.sync_address.to_bytes());
start += SyncAddress::SIZE;
buf[start..start + u64::SIZE].copy_from_slice(&self.pos.to_le_bytes());
start += u64::SIZE;
buf[start..start + VALUE_ENC_SIZE].copy_from_slice(&self.value_enc);
Expand Down Expand Up @@ -431,7 +414,6 @@ impl Serializable<SIZE> for Note {
let mut buf = &bytes[1..];
let value_commitment = JubJubAffine::from_reader(&mut buf)?;
let stealth_address = StealthAddress::from_reader(&mut buf)?;
let sync_address = SyncAddress::from_reader(&mut buf)?;
let pos = u64::from_reader(&mut buf)?;

let mut value_enc = [0u8; VALUE_ENC_SIZE];
Expand All @@ -448,7 +430,6 @@ impl Serializable<SIZE> for Note {
note_type,
value_commitment,
stealth_address,
sync_address,
pos,
value_enc,
sender_enc: [
Expand Down
10 changes: 0 additions & 10 deletions core/src/addresses/stealth.rs → core/src/stealth_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

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

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

impl From<&SyncAddress> for StealthAddress {
fn from(sa: &SyncAddress) -> Self {
StealthAddress {
note_pk: NotePublicKey::from(sa.k()),
R: *sa.R(),
}
}
}

impl Serializable<64> for StealthAddress {
type Error = Error;
/// Encode the `StealthAddress` to an array of 64 bytes
Expand Down
4 changes: 0 additions & 4 deletions core/tests/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ fn keys_consistency() {
let note = Note::transparent(&mut rng, &pk, NOTE_VALUE, sender_blinder);

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 @@ -84,9 +82,7 @@ fn keys_consistency() {
assert_ne!(vk, wrong_vk);

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
Loading

0 comments on commit ccc2294

Please sign in to comment.