Skip to content

Commit

Permalink
Rename sk_r and pk_r to note_..
Browse files Browse the repository at this point in the history
Resolves #156
  • Loading branch information
moCello committed Apr 19, 2024
1 parent 0daaba6 commit d432b25
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 126 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- Use AES-GCM from the `Encryption` module throughout the code, instead of `PoseidonCipher`.
- Rename `SecretKey::sk_r` to `SecretKey::gen_note_sk` [#156]
- Rename `StealthAddress::pk_r` to `StealthAddress::note_pk` [#156]

### Removed

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

<!-- ISSUES -->
[#156]: https://github.com/dusk-network/phoenix-core/issues/156
[#152]: https://github.com/dusk-network/phoenix-core/issues/152
[#136]: https://github.com/dusk-network/phoenix-core/issues/136
[#126]: https://github.com/dusk-network/phoenix-core/issues/126
Expand Down
17 changes: 9 additions & 8 deletions src/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,21 @@ impl Fee {
rng: &mut R,
gas_limit: u64,
gas_price: u64,
psk: &PublicKey,
pk: &PublicKey,
) -> Self {
let r = JubJubScalar::random(&mut *rng);

Self::deterministic(gas_limit, gas_price, &r, psk)
Self::deterministic(gas_limit, gas_price, &r, pk)
}

/// Create a new Fee without inner randomness
pub fn deterministic(
gas_limit: u64,
gas_price: u64,
r: &JubJubScalar,
psk: &PublicKey,
pk: &PublicKey,
) -> Self {
let stealth_address = psk.gen_stealth_address(r);
let stealth_address = pk.gen_stealth_address(r);

Fee {
gas_limit,
Expand All @@ -74,15 +74,16 @@ impl Fee {
}
}

/// Return a hash represented by `H(gas_limit, gas_price, H([pskr]))`
/// Return a hash represented by `H(gas_limit, gas_price, H([note_pk]))`
pub fn hash(&self) -> BlsScalar {
let pk_r = self.stealth_address().pk_r().as_ref().to_hash_inputs();
let note_pk =
self.stealth_address().note_pk().as_ref().to_hash_inputs();

hash(&[
BlsScalar::from(self.gas_limit),
BlsScalar::from(self.gas_price),
pk_r[0],
pk_r[1],
note_pk[0],
note_pk[1],
])
}

Expand Down
7 changes: 4 additions & 3 deletions src/fee/remainder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ impl PartialEq for Remainder {
impl Eq for Remainder {}

impl Remainder {
/// Return a hash represented by `H(gas, H([pskr]))`
/// Return a hash represented by `H(gas, H([note_pk]))`
pub fn hash(&self) -> BlsScalar {
let pk_r = self.stealth_address().pk_r().as_ref().to_hash_inputs();
let note_pk =
self.stealth_address().note_pk().as_ref().to_hash_inputs();

hash(&[BlsScalar::from(self.gas_changes), pk_r[0], pk_r[1]])
hash(&[BlsScalar::from(self.gas_changes), note_pk[0], note_pk[1]])
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/keys/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ impl PublicKey {
let rA = hash(&rA);
let rA = G * rA;

let pk_r = rA + self.B;
let pk_r = pk_r.into();
let note_pk = rA + self.B;
let note_pk = note_pk.into();

StealthAddress { R, pk_r }
StealthAddress { R, note_pk }
}
}

Expand Down
13 changes: 6 additions & 7 deletions src/keys/secret.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, StealthAddress};
use crate::{keys::hash, Ownable};
use dusk_jubjub::JubJubScalar;
use ff::Field;
use jubjub_schnorr::SecretKey as NoteSecretKey;
Expand Down Expand Up @@ -54,13 +54,12 @@ impl SecretKey {
SecretKey::new(a, b)
}

/// Generates a [`NoteSecretKey`] using the [`StealthAddress`] given.
/// With the formula: `sk_r = H(a · R) + b`
pub fn sk_r(&self, sa: &StealthAddress) -> NoteSecretKey {
let aR = sa.R() * self.a;
let aR = hash(&aR);
/// 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;

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

Expand Down
37 changes: 21 additions & 16 deletions src/keys/stealth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rkyv::{Archive, Deserialize, Serialize};

/// To obfuscate the identity of the participants, we utilizes a Stealth Address
/// system.
/// A `StealthAddress` is composed by a one-time public key (`pk_r`, the actual
/// A `StealthAddress` is composed by a one-time note-public-key (the actual
/// address) and a random point `R`.
#[derive(Default, Debug, Clone, Copy)]
#[cfg_attr(
Expand All @@ -26,7 +26,7 @@ use rkyv::{Archive, Deserialize, Serialize};
)]
pub struct StealthAddress {
pub(crate) R: JubJubExtended,
pub(crate) pk_r: NotePublicKey,
pub(crate) note_pk: NotePublicKey,
}

/// The trait `Ownable` is required by any type that wants to prove its
Expand All @@ -48,30 +48,30 @@ impl StealthAddress {
/// For additional information, check [PublicKey::from_raw_unchecked].
pub const fn from_raw_unchecked(
R: JubJubExtended,
pk_r: NotePublicKey,
note_pk: NotePublicKey,
) -> Self {
Self { R, pk_r }
Self { R, note_pk }
}

/// Gets the random point `R`
pub const fn R(&self) -> &JubJubExtended {
&self.R
}

/// Gets the `pk_r`
pub const fn pk_r(&self) -> &NotePublicKey {
&self.pk_r
/// Gets the `note_pk`
pub const fn note_pk(&self) -> &NotePublicKey {
&self.note_pk
}

/// Gets the underline `JubJubExtended` point of `pk_r`
/// Gets the underline `JubJubExtended` point of `note_pk`
pub fn address(&self) -> &JubJubExtended {
self.pk_r.as_ref()
self.note_pk.as_ref()
}
}

impl ConstantTimeEq for StealthAddress {
fn ct_eq(&self, other: &Self) -> Choice {
self.pk_r.as_ref().ct_eq(other.pk_r.as_ref()) & self.R.ct_eq(&other.R)
self.address().ct_eq(other.address()) & self.R.ct_eq(&other.R)
}
}

Expand All @@ -81,6 +81,12 @@ impl PartialEq for StealthAddress {
}
}

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

impl Ownable for StealthAddress {
fn stealth_address(&self) -> &StealthAddress {
self
Expand All @@ -93,19 +99,18 @@ impl Serializable<64> for StealthAddress {
fn to_bytes(&self) -> [u8; Self::SIZE] {
let mut bytes = [0u8; Self::SIZE];
bytes[..32].copy_from_slice(&JubJubAffine::from(self.R).to_bytes());
bytes[32..].copy_from_slice(
&JubJubAffine::from(self.pk_r.as_ref()).to_bytes(),
);
bytes[32..]
.copy_from_slice(&JubJubAffine::from(self.address()).to_bytes());
bytes
}

/// Decode the `StealthAddress` from an array of 64 bytes
fn from_bytes(bytes: &[u8; Self::SIZE]) -> Result<Self, Error> {
let R = JubJubExtended::from(JubJubAffine::from_slice(&bytes[..32])?);
let pk_r =
let note_pk =
JubJubExtended::from(JubJubAffine::from_slice(&bytes[32..])?);
let pk_r = NotePublicKey::from_raw_unchecked(pk_r);
let note_pk = NotePublicKey::from_raw_unchecked(note_pk);

Ok(StealthAddress { R, pk_r })
Ok(StealthAddress { R, note_pk })
}
}
4 changes: 2 additions & 2 deletions src/keys/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ impl ViewKey {
let aR = sa.R() * self.a();
let aR = hash(&aR);
let aR = GENERATOR_EXTENDED * aR;
let pk_r = aR + self.B();
let note_pk = aR + self.B();

sa.address() == &pk_r
sa.address() == &note_pk
}
}

Expand Down
27 changes: 14 additions & 13 deletions src/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ impl Note {
pub fn new<R: RngCore + CryptoRng>(
rng: &mut R,
note_type: NoteType,
psk: &PublicKey,
pk: &PublicKey,
value: u64,
blinding_factor: JubJubScalar,
) -> Self {
let r = JubJubScalar::random(&mut *rng);
let stealth_address = psk.gen_stealth_address(&r);
let stealth_address = pk.gen_stealth_address(&r);

let value_commitment = JubJubScalar::from(value);
let value_commitment = (GENERATOR_EXTENDED * value_commitment)
Expand All @@ -117,7 +117,7 @@ impl Note {
encryption
}
NoteType::Obfuscated => {
let shared_secret = dhke(&r, psk.A());
let shared_secret = dhke(&r, pk.A());
let blinding_factor = BlsScalar::from(blinding_factor);

let mut plaintext = value.to_bytes().to_vec();
Expand All @@ -144,10 +144,10 @@ impl Note {
/// notes, so this can be trivially treated as a constant.
pub fn transparent<R: RngCore + CryptoRng>(
rng: &mut R,
psk: &PublicKey,
pk: &PublicKey,
value: u64,
) -> Self {
Self::new(rng, NoteType::Transparent, psk, value, TRANSPARENT_BLINDER)
Self::new(rng, NoteType::Transparent, pk, value, TRANSPARENT_BLINDER)
}

/// Creates a new transparent note
Expand Down Expand Up @@ -185,11 +185,11 @@ impl Note {
/// knowledge of the value commitment of this note.
pub fn obfuscated<R: RngCore + CryptoRng>(
rng: &mut R,
psk: &PublicKey,
pk: &PublicKey,
value: u64,
blinding_factor: JubJubScalar,
) -> Self {
Self::new(rng, NoteType::Obfuscated, psk, value, blinding_factor)
Self::new(rng, NoteType::Obfuscated, pk, value, blinding_factor)
}

fn decrypt_data(
Expand Down Expand Up @@ -219,10 +219,10 @@ impl Note {

/// Create a unique nullifier for the note
///
/// This nullifier is represeted as `H(sk_r · G', pos)`
/// This nullifier is represeted as `H(note_sk · G', pos)`
pub fn gen_nullifier(&self, sk: &SecretKey) -> BlsScalar {
let sk_r = sk.sk_r(&self.stealth_address);
let pk_prime = GENERATOR_NUMS_EXTENDED * sk_r.as_ref();
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();

let pos = BlsScalar::from(self.pos);
Expand All @@ -233,14 +233,15 @@ impl Note {
/// Return the internal representation of scalars to be hashed
pub fn hash_inputs(&self) -> [BlsScalar; 6] {
let value_commitment = self.value_commitment().to_hash_inputs();
let pk_r = self.stealth_address().pk_r().as_ref().to_hash_inputs();
let note_pk =
self.stealth_address().note_pk().as_ref().to_hash_inputs();

[
BlsScalar::from(self.note_type as u64),
value_commitment[0],
value_commitment[1],
pk_r[0],
pk_r[1],
note_pk[0],
note_pk[1],
BlsScalar::from(self.pos),
]
}
Expand Down
8 changes: 4 additions & 4 deletions tests/crossover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ use rand_core::OsRng;
fn crossover_hash() -> Result<(), Error> {
let mut rng = OsRng;

let ssk = SecretKey::random(&mut rng);
let psk = PublicKey::from(ssk);
let sk = SecretKey::random(&mut rng);
let pk = PublicKey::from(sk);

let value = 25;
let blinding_factor = JubJubScalar::random(&mut rng);
let note = Note::obfuscated(&mut rng, &psk, value, blinding_factor);
let note = Note::obfuscated(&mut rng, &pk, value, blinding_factor);

let value = 25;
let blinding_factor = JubJubScalar::random(&mut rng);
let note_p = Note::obfuscated(&mut rng, &psk, value, blinding_factor);
let note_p = Note::obfuscated(&mut rng, &pk, value, blinding_factor);

let (_, crossover) = note.try_into()?;
let (_, crossover_p) = note_p.try_into()?;
Expand Down
Loading

0 comments on commit d432b25

Please sign in to comment.