Skip to content

Commit

Permalink
Use From trait to generate keys
Browse files Browse the repository at this point in the history
Resolves #136
  • Loading branch information
moCello committed Dec 7, 2023
1 parent b624948 commit 3b7c596
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 92 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## Removed

- Remove `HexDebug` trait for keys [#136]
- Remove `public_key` and `view_key` methods from `SecretKey` in favor of the `From` trait [#136]
- Remove `public_key` method from `ViewKey` in favor of the `From` trait [#136]

## Added

- Derive `Debug` trait for keys [#136]

## [0.22.0] - 2023-11-22

### Added
Expand Down Expand Up @@ -219,6 +229,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Removal of anyhow error implementation.
- Canonical implementation shielded by feature.

[#136]: https://github.com/dusk-network/phoenix-core/issues/136
[#126]: https://github.com/dusk-network/phoenix-core/issues/126
[#119]: https://github.com/dusk-network/phoenix-core/issues/119
[#116]: https://github.com/dusk-network/phoenix-core/issues/116
Expand Down
31 changes: 24 additions & 7 deletions src/keys/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use crate::{permutation, SecretKey, StealthAddress};
use crate::{permutation, SecretKey, StealthAddress, ViewKey};

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

#[cfg(feature = "rkyv-impl")]
use rkyv::{Archive, Deserialize, Serialize};

use dusk_bytes::{DeserializableSlice, Error, HexDebug, Serializable};
use dusk_bytes::{DeserializableSlice, Error, Serializable};
use dusk_jubjub::GENERATOR_EXTENDED;
use subtle::{Choice, ConstantTimeEq};

/// Public pair of `a·G` and `b·G` defining a [`PublicKey`]
#[derive(HexDebug, Clone, Copy)]
#[derive(Debug, Clone, Copy)]
#[cfg_attr(
feature = "rkyv-impl",
derive(Archive, Serialize, Deserialize),
Expand Down Expand Up @@ -75,14 +75,31 @@ impl PartialEq for PublicKey {
impl Eq for PublicKey {}

impl From<SecretKey> for PublicKey {
fn from(secret: SecretKey) -> Self {
secret.public_key()
fn from(sk: SecretKey) -> Self {
Self::from(&sk)
}
}

impl From<&SecretKey> for PublicKey {
fn from(secret: &SecretKey) -> Self {
secret.public_key()
fn from(sk: &SecretKey) -> Self {
let A = GENERATOR_EXTENDED * sk.a();
let B = GENERATOR_EXTENDED * sk.b();

PublicKey::new(A, B)
}
}

impl From<ViewKey> for PublicKey {
fn from(vk: ViewKey) -> Self {
Self::from(&vk)
}
}

impl From<&ViewKey> for PublicKey {
fn from(vk: &ViewKey) -> Self {
let A = GENERATOR_EXTENDED * vk.a();

PublicKey::new(A, *vk.B())
}
}

Expand Down
22 changes: 3 additions & 19 deletions src/keys/secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use crate::{permutation, PublicKey, StealthAddress, ViewKey};
use crate::{permutation, StealthAddress};
use dusk_jubjub::JubJubScalar;
use dusk_schnorr::NoteSecretKey;

#[cfg(feature = "rkyv-impl")]
use rkyv::{Archive, Deserialize, Serialize};

use dusk_bytes::{DeserializableSlice, Error, HexDebug, Serializable};
use dusk_jubjub::GENERATOR_EXTENDED;
use dusk_bytes::{DeserializableSlice, Error, Serializable};
use rand_core::{CryptoRng, RngCore};
use subtle::{Choice, ConstantTimeEq};

/// Secret pair of `a` and `b` defining a [`SecretKey`]
#[derive(Clone, Copy, Eq, HexDebug)]
#[derive(Clone, Copy, Eq, Debug)]
#[cfg_attr(
feature = "rkyv-impl",
derive(Archive, Serialize, Deserialize),
Expand Down Expand Up @@ -62,21 +61,6 @@ impl SecretKey {

(aR + self.b).into()
}

/// Derive the secret to deterministically construct a [`PublicKey`]
pub fn public_key(&self) -> PublicKey {
let A = GENERATOR_EXTENDED * self.a;
let B = GENERATOR_EXTENDED * self.b;

PublicKey::new(A, B)
}

/// Derive the secret to deterministically construct a [`ViewKey`]
pub fn view_key(&self) -> ViewKey {
let B = GENERATOR_EXTENDED * self.b;

ViewKey::new(self.a, B)
}
}

impl ConstantTimeEq for SecretKey {
Expand Down
6 changes: 3 additions & 3 deletions src/keys/stealth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use dusk_jubjub::{JubJubAffine, JubJubExtended};
use dusk_schnorr::NotePublicKey;

use dusk_bytes::{DeserializableSlice, Error, HexDebug, Serializable};
use dusk_bytes::{DeserializableSlice, Error, Serializable};

use subtle::{Choice, ConstantTimeEq};

Expand All @@ -17,8 +17,8 @@ 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
// address) and a random point `R`.
#[derive(Default, HexDebug, Clone, Copy)]
/// address) and a random point `R`.
#[derive(Default, Debug, Clone, Copy)]
#[cfg_attr(
feature = "rkyv-impl",
derive(Archive, Serialize, Deserialize),
Expand Down
23 changes: 9 additions & 14 deletions src/keys/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

use crate::keys::stealth;

use crate::{permutation, PublicKey, SecretKey};
use crate::{permutation, SecretKey};

use dusk_bytes::{DeserializableSlice, Error, HexDebug, Serializable};
use dusk_bytes::{DeserializableSlice, Error, Serializable};
use dusk_jubjub::{
JubJubAffine, JubJubExtended, JubJubScalar, GENERATOR_EXTENDED,
};
Expand All @@ -21,7 +21,7 @@ use rkyv::{Archive, Deserialize, Serialize};
///
/// The notes are encrypted against secret a, so this is used to decrypt the
/// blinding factor and value
#[derive(Clone, Copy, HexDebug)]
#[derive(Clone, Copy, Debug)]
#[cfg_attr(
feature = "rkyv-impl",
derive(Archive, Serialize, Deserialize),
Expand Down Expand Up @@ -54,13 +54,6 @@ impl ViewKey {
Self { a, B }
}

/// Derive the secret to deterministically construct a [`PublicKey`]
pub fn public_key(&self) -> PublicKey {
let A = GENERATOR_EXTENDED * self.a;

PublicKey::new(A, self.B)
}

/// Gets `a`
pub fn a(&self) -> &JubJubScalar {
&self.a
Expand All @@ -85,14 +78,16 @@ impl ViewKey {
}

impl From<SecretKey> for ViewKey {
fn from(secret: SecretKey) -> Self {
secret.view_key()
fn from(sk: SecretKey) -> Self {
Self::from(&sk)
}
}

impl From<&SecretKey> for ViewKey {
fn from(secret: &SecretKey) -> Self {
secret.view_key()
fn from(sk: &SecretKey) -> Self {
let B = GENERATOR_EXTENDED * sk.b();

ViewKey::new(*sk.a(), B)
}
}

Expand Down
6 changes: 3 additions & 3 deletions tests/crossover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
use core::convert::TryInto;

use dusk_jubjub::JubJubScalar;
use phoenix_core::{Error, Message, Note, SecretKey};
use phoenix_core::{Error, Message, Note, PublicKey, SecretKey};
use rand_core::OsRng;

#[test]
fn crossover_hash() -> Result<(), Error> {
let rng = &mut OsRng;

let ssk = SecretKey::random(rng);
let psk = ssk.public_key();
let psk = PublicKey::from(ssk);

let value = 25;
let blinding_factor = JubJubScalar::random(rng);
Expand All @@ -41,7 +41,7 @@ fn message_hash() -> Result<(), Error> {
let rng = &mut OsRng;

let ssk = SecretKey::random(rng);
let psk = ssk.public_key();
let psk = PublicKey::from(ssk);
let value = 25;

let r = JubJubScalar::random(rng);
Expand Down
24 changes: 9 additions & 15 deletions tests/keys.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 dusk_bytes::{DeserializableSlice, ParseHexStr, Serializable};
use dusk_bytes::{DeserializableSlice, Serializable};
use phoenix_core::{PublicKey, SecretKey, ViewKey};
use rand_core::OsRng;

Expand All @@ -20,17 +20,11 @@ fn ssk_from_bytes() {
#[test]
fn keys_encoding() {
let ssk = SecretKey::random(&mut OsRng);
let vk = ssk.view_key();
let psk = ssk.public_key();

assert_eq!(
vk,
ViewKey::from_hex_str(format!("{:x}", vk).as_str()).unwrap()
);
assert_eq!(
psk,
PublicKey::from_hex_str(format!("{:x}", psk).as_str()).unwrap()
);
let vk = ViewKey::from(ssk);
let psk = PublicKey::from(ssk);

assert_eq!(vk, ViewKey::from_bytes(&vk.to_bytes()).unwrap());
assert_eq!(psk, PublicKey::from_bytes(&psk.to_bytes()).unwrap());
}

#[test]
Expand All @@ -39,14 +33,14 @@ fn keys_consistency() {

let r = JubJubScalar::random(&mut OsRng);
let ssk = SecretKey::random(&mut OsRng);
let psk = ssk.public_key();
let vk = ssk.view_key();
let psk = PublicKey::from(ssk);
let vk = ViewKey::from(ssk);
let sa = psk.gen_stealth_address(&r);

assert!(vk.owns(&sa));

let wrong_ssk = SecretKey::random(&mut OsRng);
let wrong_vk = wrong_ssk.view_key();
let wrong_vk = ViewKey::from(wrong_ssk);

assert_ne!(ssk, wrong_ssk);
assert_ne!(vk, wrong_vk);
Expand Down
8 changes: 4 additions & 4 deletions tests/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
use dusk_bytes::Serializable;
use dusk_jubjub::JubJubScalar;
use dusk_jubjub::{GENERATOR_EXTENDED, GENERATOR_NUMS_EXTENDED};
use phoenix_core::{Message, SecretKey};
use phoenix_core::{Message, PublicKey, SecretKey};
use rand_core::OsRng;

#[test]
fn message_consistency() {
let rng = &mut OsRng;

let ssk = SecretKey::random(rng);
let psk = ssk.public_key();
let psk_wrong = SecretKey::random(rng).public_key();
let psk = PublicKey::from(ssk);
let psk_wrong = PublicKey::from(SecretKey::random(rng));

let r = JubJubScalar::random(rng);
let r_wrong = JubJubScalar::random(rng);
Expand All @@ -41,7 +41,7 @@ fn message_bytes() {
let rng = &mut OsRng;

let ssk = SecretKey::random(rng);
let psk = ssk.public_key();
let psk = PublicKey::from(ssk);

let r = JubJubScalar::random(rng);
let value = 106;
Expand Down
Loading

0 comments on commit 3b7c596

Please sign in to comment.