Skip to content
This repository has been archived by the owner on Jan 16, 2025. It is now read-only.

Commit

Permalink
Add PublicKey and SecretKey from schnorr
Browse files Browse the repository at this point in the history
- Add `dusk_bytes::Serializable` trait to structure
- Remove manual implementation of `to_bytes` and `from_bytes`
- Remove `Error` enum
- Remove `decode` function
- Bump `dusk-jubjub` to `v0.8`
- Bump `poseidon252` to `v0.16.0`
- Bump `canonical` to `v0.5`
- Bump `canonical_derive` `v0.5`
- Update CHANGELOG to ISO 8601

Resolves: #30
See also: dusk-network/schnorr#8, dusk-network/schnorr#21
  • Loading branch information
ZER0 committed Jan 28, 2021
1 parent d5183c3 commit c280464
Show file tree
Hide file tree
Showing 13 changed files with 166 additions and 500 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dusk_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
args: --release

test_nightly_canon:
name: Nightly tests
name: Nightly tests canon
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand Down
29 changes: 27 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,35 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.4.1] - 26-11-20
## [0.5.0] - 2021-01-28

### Added

- Add `PublicKey` and `SecretKey` (removed from `schnorr`)
- Add `dusk_bytes::Serializable` trait to structure

### Removed

- Remove manual implementation of `to_bytes` and `from_bytes`
- Remove `Error` enum
- Remove `decode` function

### Changed

- Bump `dusk-jubjub` to `v0.8`
- Bump `poseidon252` to `v0.16.0`
- Bump `canonical` to `v0.5`
- Bump `canonical_derive` `v0.5`
- Update CHANGELOG to ISO 8601

## [0.4.1] - 2020-11-26

### Changed

- Use poseidon252 dependency.

## [0.4.0] - 17-11-20
## [0.4.0] - 2020-11-17

### Changed

- No-Std compatibility.
14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
[package]
name = "dusk-pki"
version = "0.4.1"
version = "0.5.0"
authors = ["zer0 <[email protected]>", "Victor Lopez <[email protected]"]
edition = "2018"

[dependencies]
rand_core = "0.5.1"
dusk-jubjub = "0.5"
poseidon252 = {git = "https://github.com/dusk-network/Poseidon252", tag = "v0.14.1", default-features = false}
hex ="^0.4"
rand_core = "0.5"
dusk-jubjub = "0.8"
poseidon252 = {git = "https://github.com/dusk-network/Poseidon252", tag = "v0.16.0", default-features = false}
subtle = "^2.2.1"
canonical = {version = "0.4", optional = true}
canonical_derive = {version = "0.4", optional = true}
canonical = {version = "0.5", optional = true}
canonical_derive = {version = "0.5", optional = true}
dusk-bytes = { version = "0.1" }

[dev-dependencies]
rand ="0.7"
Expand Down
47 changes: 0 additions & 47 deletions src/decode.rs

This file was deleted.

31 changes: 0 additions & 31 deletions src/errors.rs

This file was deleted.

68 changes: 15 additions & 53 deletions src/keys/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@
// Copyright (c) DUSK NETWORK. All rights reserved.

use super::secret::SecretKey;
use crate::{Error, JubJubAffine, JubJubExtended};
use core::convert::TryFrom;
use core::fmt;
use crate::{JubJubAffine, JubJubExtended};
use dusk_bytes::{Error, HexDebug, Serializable};
use dusk_jubjub::GENERATOR_EXTENDED;

#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "canon", derive(Canon))]
#[cfg(feature = "canon")]
use canonical::Canon;
#[cfg(feature = "canon")]
use canonical_derive::Canon;

/// Structure repesenting a [`PublicKey`]
pub struct PublicKey(JubJubExtended);
#[derive(Copy, Clone, PartialEq, HexDebug)]
#[cfg_attr(feature = "canon", derive(Canon))]
pub struct PublicKey(pub(crate) JubJubExtended);

impl From<&SecretKey> for PublicKey {
fn from(sk: &SecretKey) -> Self {
Expand Down Expand Up @@ -42,55 +45,14 @@ impl AsRef<JubJubExtended> for PublicKey {
}
}

impl PublicKey {
/// Copies `self` into a new array of 32 bytes.
pub fn to_bytes(&self) -> [u8; 32] {
JubJubAffine::from(self.0).to_bytes()
}

/// Create a new `PublicKey` from an array of 32 bytes.
pub fn from_bytes(bytes: &[u8; 32]) -> Result<Self, Error> {
match Option::<JubJubAffine>::from(JubJubAffine::from_bytes(*bytes)) {
Some(point) => Ok(PublicKey(JubJubExtended::from(point))),
_ => Err(Error::InvalidPoint),
}
}
}

impl fmt::LowerHex for PublicKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let bytes = self.to_bytes();

if f.alternate() {
write!(f, "0x")?
}

for byte in &bytes[..] {
write!(f, "{:02X}", &byte)?
}

Ok(())
}
}

impl fmt::UpperHex for PublicKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let bytes = self.to_bytes();
impl Serializable<32> for PublicKey {
type Error = Error;

if f.alternate() {
write!(f, "0x")?
}

for byte in &bytes[..] {
write!(f, "{:02X}", &byte)?
}

Ok(())
fn to_bytes(&self) -> [u8; 32] {
JubJubAffine::from(self.0).to_bytes()
}
}

impl fmt::Display for PublicKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:x}", self)
fn from_bytes(bytes: &[u8; 32]) -> Result<Self, Error> {
Ok(Self(JubJubAffine::from_bytes(bytes)?.into()))
}
}
69 changes: 16 additions & 53 deletions src/keys/secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use crate::{Error, JubJubScalar};
use core::convert::TryFrom;
use core::fmt;
use crate::JubJubScalar;
use dusk_bytes::{Error, HexDebug, Serializable};
use rand_core::{CryptoRng, RngCore};

#[cfg(feature = "canon")]
use canonical::Canon;
#[cfg(feature = "canon")]
use canonical_derive::Canon;

#[allow(non_snake_case)]
#[derive(Default, Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "canon", derive(Canon))]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, HexDebug)]
/// Structure repesenting a secret key
pub struct SecretKey(JubJubScalar);
pub struct SecretKey(pub(crate) JubJubScalar);

impl From<JubJubScalar> for SecretKey {
fn from(s: JubJubScalar) -> SecretKey {
Expand All @@ -36,65 +40,24 @@ impl AsRef<JubJubScalar> for SecretKey {
impl SecretKey {
/// This will create a random [`SecretKey`] from a scalar
/// of the Field JubJubScalar.
pub fn new<T>(rand: &mut T) -> SecretKey
pub fn random<T>(rand: &mut T) -> SecretKey
where
T: RngCore + CryptoRng,
{
let fr = JubJubScalar::random(rand);

SecretKey(fr)
}

/// Copies `self` into a new array of 32 bytes.
pub fn to_bytes(&self) -> [u8; 32] {
let mut bytes = [0u8; 32];
bytes.copy_from_slice(&self.0.to_bytes());
bytes
}

/// Create a new [`SecretKey`] from an array of 32 bytes.
pub fn from_bytes(bytes: &[u8; 32]) -> Result<Self, Error> {
match Option::from(JubJubScalar::from_bytes(bytes)) {
Some(scalar) => Ok(SecretKey(scalar)),
_ => Err(Error::InvalidScalar),
}
}
}

impl fmt::LowerHex for SecretKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let bytes = self.to_bytes();

if f.alternate() {
write!(f, "0x")?
}

for byte in &bytes[..] {
write!(f, "{:02X}", &byte)?
}

Ok(())
}
}

impl fmt::UpperHex for SecretKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let bytes = self.to_bytes();
impl Serializable<32> for SecretKey {
type Error = Error;

if f.alternate() {
write!(f, "0x")?
}

for byte in &bytes[..] {
write!(f, "{:02X}", &byte)?
}

Ok(())
fn to_bytes(&self) -> [u8; 32] {
self.0.to_bytes()
}
}

impl fmt::Display for SecretKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:x}", self)
fn from_bytes(bytes: &[u8; 32]) -> Result<Self, Error> {
Ok(Self(JubJubScalar::from_bytes(bytes)?))
}
}
Loading

0 comments on commit c280464

Please sign in to comment.