Skip to content

Commit

Permalink
Move keys from dusk_pki into phoenix
Browse files Browse the repository at this point in the history
Resolves #126
  • Loading branch information
Neotamandua committed Nov 2, 2023
1 parent e320c7d commit ad0a881
Show file tree
Hide file tree
Showing 22 changed files with 623 additions and 81 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

[0.21.0] - 2023-10-12
### Added

- Move `PublicSpendKey` (now named `PublicKey`), `SecretSpendKey` (now named `SecretKey`), `SteathAddress`, `ViewKey` from dusk_pki [#126]

## [0.21.0] - 2023-10-12

### Changed

Expand Down Expand Up @@ -213,6 +217,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.

[#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
[#114]: https://github.com/dusk-network/phoenix-core/issues/114
Expand Down
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ dusk-bls12_381 = { version = "0.12", default-features = false }
dusk-bls12_381-sign = { version = "0.5", default-features = false }
dusk-jubjub = { version = "0.13", default-features = false }
dusk-poseidon = { version = "0.31", default-features = false }
dusk-pki = { version = "0.13", default-features = false }
dusk-schnorr = { version = "0.15.0", default-features = false }
subtle = { version = "^2.2.1", default-features = false }
rkyv = { version = "0.7", optional = true, default-features = false }
bytecheck = { version = "0.6", optional = true, default-features = false }
ff = { version = "0.13", default-features = false }
Expand All @@ -31,7 +32,7 @@ alloc = []
rkyv-impl = [
"dusk-poseidon/rkyv-impl",
"dusk-jubjub/rkyv-impl",
"dusk-pki/rkyv-impl",
"dusk-schnorr/rkyv-impl",
"dusk-bls12_381/rkyv-impl",
"dusk-bls12_381-sign/rkyv-impl",
"rkyv",
Expand Down
4 changes: 2 additions & 2 deletions src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
// Copyright (c) DUSK NETWORK. All rights reserved.

use crate::note::TRANSPARENT_BLINDER;
use crate::{BlsScalar, JubJubScalar};
use crate::{Crossover, Error, Fee, Note, NoteType, Remainder};

use core::convert::TryFrom;
use dusk_jubjub::{GENERATOR_EXTENDED, GENERATOR_NUMS_EXTENDED};
use dusk_bls12_381::BlsScalar;
use dusk_jubjub::{JubJubScalar, GENERATOR_EXTENDED, GENERATOR_NUMS_EXTENDED};
use dusk_poseidon::cipher::PoseidonCipher;

impl From<(Fee, Crossover)> for Note {
Expand Down
5 changes: 2 additions & 3 deletions src/crossover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@

//! Fee module contains the logic related to `Crossover` structure

use crate::{BlsScalar, JubJubExtended};

use dusk_bls12_381::BlsScalar;
use dusk_bytes::{DeserializableSlice, Error as BytesError, Serializable};
use dusk_jubjub::JubJubAffine;
use dusk_jubjub::{JubJubAffine, JubJubExtended};
use dusk_poseidon::cipher::PoseidonCipher;
use dusk_poseidon::sponge;

Expand Down
10 changes: 5 additions & 5 deletions src/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

//! Fee module contains the logic related to `Fee` and `Remainder` structure

use crate::{Ownable, PublicKey, StealthAddress};
use dusk_bls12_381::BlsScalar;
use dusk_bytes::{DeserializableSlice, Error as BytesError, Serializable};
use dusk_pki::{Ownable, PublicSpendKey, StealthAddress};
use dusk_jubjub::JubJubScalar;
use dusk_poseidon::sponge::hash;
use rand_core::{CryptoRng, RngCore};

Expand All @@ -16,8 +18,6 @@ use rkyv::{Archive, Deserialize, Serialize};

use core::cmp;

use crate::{BlsScalar, JubJubScalar};

mod remainder;
pub use remainder::Remainder;

Expand Down Expand Up @@ -50,7 +50,7 @@ impl Fee {
rng: &mut R,
gas_limit: u64,
gas_price: u64,
psk: &PublicSpendKey,
psk: &PublicKey,
) -> Self {
let r = JubJubScalar::random(rng);

Expand All @@ -62,7 +62,7 @@ impl Fee {
gas_limit: u64,
gas_price: u64,
r: &JubJubScalar,
psk: &PublicSpendKey,
psk: &PublicKey,
) -> Self {
let stealth_address = psk.gen_stealth_address(r);

Expand Down
6 changes: 2 additions & 4 deletions src/fee/remainder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@

//! Remainder module contains the logic related to `Remainder` structure

use dusk_pki::Ownable;
use dusk_pki::StealthAddress;
use crate::{Ownable, StealthAddress};

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

use dusk_bls12_381::BlsScalar;
use dusk_poseidon::sponge::hash;

use crate::BlsScalar;

/// The Remainder structure.
#[derive(Clone, Copy, Debug)]
#[cfg_attr(
Expand Down
10 changes: 10 additions & 0 deletions src/keys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) DUSK NETWORK. All rights reserved.

pub mod public;
pub mod secret;
pub mod stealth;
pub mod view;
109 changes: 109 additions & 0 deletions src/keys/public.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use dusk_schnorr::NotePublicKey;

use crate::{permutation, StealthAddress};

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

use super::secret::SecretKey;

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

use dusk_bytes::{DeserializableSlice, Error, HexDebug, 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)]
#[cfg_attr(
feature = "rkyv-impl",
derive(Archive, Serialize, Deserialize),
archive_attr(derive(bytecheck::CheckBytes))
)]
pub struct PublicKey {
A: JubJubExtended,
B: JubJubExtended,
}

impl PublicKey {
/// This method is used to construct a new `PublicKey` from the given
/// public pair of `a·G` and `b·G`
pub fn new(A: JubJubExtended, B: JubJubExtended) -> Self {
Self { A, B }
}

/// Gets `A` (`a·G`)
pub fn A(&self) -> &JubJubExtended {
&self.A
}

/// Gets `B` (`b·G`)
pub fn B(&self) -> &JubJubExtended {
&self.B
}

/// Generates new `PKr = H(A · r) · G + B` from a given `r`
pub fn gen_stealth_address(&self, r: &JubJubScalar) -> StealthAddress {
let G = GENERATOR_EXTENDED;
let R = G * r;

let rA = self.A * r;
let rA = permutation::hash(&rA);
let rA = G * rA;

let pk_r = rA + self.B;
let pk_r = NotePublicKey::from(pk_r);

StealthAddress { R, pk_r }
}
}

impl ConstantTimeEq for PublicKey {
fn ct_eq(&self, other: &Self) -> Choice {
self.A.ct_eq(&other.A) & self.B.ct_eq(&other.B)
}
}

impl PartialEq for PublicKey {
fn eq(&self, other: &Self) -> bool {
self.ct_eq(other).into()
}
}

impl Eq for PublicKey {}

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

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

impl Serializable<64> for PublicKey {
type Error = Error;

fn to_bytes(&self) -> [u8; Self::SIZE] {
let mut bytes = [0u8; Self::SIZE];
bytes[..32].copy_from_slice(&JubJubAffine::from(self.A).to_bytes());
bytes[32..].copy_from_slice(&JubJubAffine::from(self.B).to_bytes());
bytes
}

fn from_bytes(bytes: &[u8; Self::SIZE]) -> Result<Self, Self::Error> {
let A = JubJubExtended::from(JubJubAffine::from_slice(&bytes[..32])?);
let B = JubJubExtended::from(JubJubAffine::from_slice(&bytes[32..])?);

Ok(Self { A, B })
}
}
112 changes: 112 additions & 0 deletions src/keys/secret.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use super::public::PublicKey;
use super::stealth::StealthAddress;
use crate::{permutation, ViewKey};
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 rand_core::{CryptoRng, RngCore};
use subtle::{Choice, ConstantTimeEq};

/// Secret pair of `a` and `b` defining a [`SecretKey`]
#[derive(Clone, Copy, Eq, HexDebug)]
#[cfg_attr(
feature = "rkyv-impl",
derive(Archive, Serialize, Deserialize),
archive_attr(derive(bytecheck::CheckBytes))
)]
pub struct SecretKey {
a: JubJubScalar,
b: JubJubScalar,
}

impl SecretKey {
/// This method is used to construct a new `SecretKey` from the given
/// secret pair of `a` and `b`.
pub fn new(a: JubJubScalar, b: JubJubScalar) -> Self {
Self { a, b }
}

/// Gets `a`
pub fn a(&self) -> &JubJubScalar {
&self.a
}

/// Gets `b`
pub fn b(&self) -> &JubJubScalar {
&self.b
}

/// Deterministically create a new [`SecretKey`] from a random number
/// generator
pub fn random<R: RngCore + CryptoRng>(rng: &mut R) -> Self {
let a = JubJubScalar::random(rng);
let b = JubJubScalar::random(rng);

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 = permutation::hash(&aR);

(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 {
fn ct_eq(&self, other: &Self) -> Choice {
self.a.ct_eq(&other.a) & self.b.ct_eq(&other.b)
}
}

impl PartialEq for SecretKey {
fn eq(&self, other: &Self) -> bool {
self.ct_eq(other).into()
}
}

impl Serializable<64> for SecretKey {
type Error = Error;

fn to_bytes(&self) -> [u8; 64] {
let mut bytes = [0u8; 64];
bytes[..32].copy_from_slice(&self.a.to_bytes());
bytes[32..].copy_from_slice(&self.b.to_bytes());
bytes
}

fn from_bytes(buf: &[u8; 64]) -> Result<Self, Self::Error> {
let a = JubJubScalar::from_slice(&buf[..32])?;
let b = JubJubScalar::from_slice(&buf[32..])?;

Ok(Self { a, b })
}
}
Loading

0 comments on commit ad0a881

Please sign in to comment.