-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move keys from dusk_pki into phoenix
Resolves #126
- Loading branch information
1 parent
e320c7d
commit ad0a881
Showing
22 changed files
with
623 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
} | ||
} |
Oops, something went wrong.