Skip to content

Commit

Permalink
Merge pull request #91 from dusk-network/release-0.12
Browse files Browse the repository at this point in the history
Release 0.12
  • Loading branch information
CPerezz authored Jul 5, 2021
2 parents a8d59f5 + 44c2b61 commit 9dea335
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 36 deletions.
10 changes: 10 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]

## [0.12.0] - 2021-07-05

### Added

- Add `dusk-bytes::BadLength` impl for crate Error [#88](https://github.com/dusk-network/phoenix-core/issues/88)
- Add `From<Error>` impl for `dusk-bytes::Error` [#92](https://github.com/dusk-network/phoenix-core/issues/92)
### Changed

- Change `JubJubScalar` for `BlsScalar` for all `nonce` attributes. [#84](https://github.com/dusk-network/phoenix-core/issues/84)

## [0.11.0] - 2021-06-09

### Added
Expand Down
18 changes: 3 additions & 15 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "phoenix-core"
version = "0.11.0"
version = "0.12.0"
authors = ["zer0 <[email protected]>", "Victor Lopez <[email protected]"]
edition = "2018"
repository = "https://github.com/dusk-network/phoenix-core"
Expand All @@ -13,8 +13,8 @@ rand_core = { version = "0.6", default-features = false }
dusk-bytes = "0.1"
dusk-bls12_381 = { version = "0.8", default-features = false }
dusk-jubjub = { version = "0.10", default-features = false }
dusk-poseidon = { version = "0.21.0-rc", default-features = false }
dusk-pki = { version = "0.7.0-rc", default-features = false }
dusk-poseidon = { version = "0.21", default-features = false }
dusk-pki = { version = "0.7", default-features = false }
canonical = { version = "0.6", optional = true }
canonical_derive = { version = "0.6", optional = true }

Expand All @@ -28,15 +28,3 @@ canon = [
"dusk-poseidon/canon",
"dusk-pki/canon"
]

[profile.release]
opt-level = 3
incremental = false
codegen-units = 1
debug = false
lto = true
overflow-checks = false
debug-assertions = false

[profile.dev]
overflow-checks = false
2 changes: 1 addition & 1 deletion src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl From<Remainder> for Note {

let stealth_address = remainder.stealth_address;
let value = remainder.gas_changes;
let nonce = JubJubScalar::zero();
let nonce = BlsScalar::zero();

let value_commitment = JubJubScalar::from(value);
let value_commitment = (GENERATOR_EXTENDED * value_commitment)
Expand Down
10 changes: 5 additions & 5 deletions src/crossover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

//! Fee module contains the logic related to `Crossover` structure
use crate::{BlsScalar, JubJubExtended, JubJubScalar};
use crate::{BlsScalar, JubJubExtended};

#[cfg(feature = "canon")]
use canonical_derive::Canon;
Expand All @@ -21,7 +21,7 @@ use dusk_poseidon::sponge;
#[cfg_attr(feature = "canon", derive(Canon))]
pub struct Crossover {
pub(crate) value_commitment: JubJubExtended,
pub(crate) nonce: JubJubScalar,
pub(crate) nonce: BlsScalar,
pub(crate) encrypted_data: PoseidonCipher,
}

Expand Down Expand Up @@ -53,7 +53,7 @@ impl Serializable<{ 64 + PoseidonCipher::SIZE }> for Crossover {
fn from_bytes(bytes: &[u8; Self::SIZE]) -> Result<Self, Self::Error> {
let value_commitment =
JubJubExtended::from(JubJubAffine::from_slice(&bytes[..32])?);
let nonce = JubJubScalar::from_slice(&bytes[32..])?;
let nonce = BlsScalar::from_slice(&bytes[32..])?;

let encrypted_data = PoseidonCipher::from_slice(&bytes[64..])?;

Expand Down Expand Up @@ -81,7 +81,7 @@ impl Crossover {
let mut inputs = [BlsScalar::zero(); 3 + PoseidonCipher::cipher_size()];

inputs[..2].copy_from_slice(&self.value_commitment().to_hash_inputs());
inputs[2] = self.nonce.into();
inputs[2] = self.nonce;
inputs[3..].copy_from_slice(self.encrypted_data.cipher());

inputs
Expand All @@ -93,7 +93,7 @@ impl Crossover {
}

/// Returns the Nonce used for the encrypt / decrypt of data for this note
pub const fn nonce(&self) -> &JubJubScalar {
pub const fn nonce(&self) -> &BlsScalar {
&self.nonce
}

Expand Down
34 changes: 34 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use dusk_bytes::{BadLength, Error as DuskBytesError, InvalidChar};
use dusk_poseidon::Error as PoseidonError;

use core::fmt;
Expand Down Expand Up @@ -32,6 +33,12 @@ pub enum Error {
InvalidCommitment,
/// Invalid Nonce
InvalidNonce,
/// Dusk-bytes InvalidData error
InvalidData,
/// Dusk-bytes BadLenght error
BadLenght(usize, usize),
/// Dusk-bytes InvalidChar error
InvalidChar(char, usize),
}

impl From<PoseidonError> for Error {
Expand All @@ -45,3 +52,30 @@ impl fmt::Display for Error {
write!(f, "Phoenix-Core Error: {:?}", &self)
}
}

impl From<Error> for DuskBytesError {
fn from(err: Error) -> Self {
match err {
Error::InvalidData => DuskBytesError::InvalidData,
Error::BadLenght(found, expected) => {
DuskBytesError::BadLength { found, expected }
}
Error::InvalidChar(ch, index) => {
DuskBytesError::InvalidChar { ch, index }
}
_ => unreachable!(),
}
}
}

impl BadLength for Error {
fn bad_length(found: usize, expected: usize) -> Self {
Error::BadLenght(found, expected)
}
}

impl InvalidChar for Error {
fn invalid_char(ch: char, index: usize) -> Self {
Error::InvalidChar(ch, index)
}
}
10 changes: 5 additions & 5 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use rand_core::{CryptoRng, RngCore};
#[cfg_attr(feature = "canon", derive(Canon))]
pub struct Message {
value_commitment: JubJubExtended,
nonce: JubJubScalar,
nonce: BlsScalar,
encrypted_data: PoseidonCipher,
}

Expand All @@ -35,7 +35,7 @@ impl Message {
psk: &PublicSpendKey,
value: u64,
) -> Self {
let nonce = JubJubScalar::random(rng);
let nonce = BlsScalar::random(rng);
let blinding_factor = JubJubScalar::random(rng);

let note = Note::deterministic(
Expand Down Expand Up @@ -92,7 +92,7 @@ impl Message {
}

/// Nonce used for the encryption of the value and blinding factor
pub const fn nonce(&self) -> &JubJubScalar {
pub const fn nonce(&self) -> &BlsScalar {
&self.nonce
}

Expand Down Expand Up @@ -157,9 +157,9 @@ impl
.into();
bytes = &bytes[JubJubAffine::SIZE..];

let nonce = JubJubScalar::from_slice(&bytes[..JubJubScalar::SIZE])
let nonce = BlsScalar::from_slice(&bytes[..BlsScalar::SIZE])
.map_err(|_| Error::InvalidNonce)?;
bytes = &bytes[JubJubScalar::SIZE..];
bytes = &bytes[BlsScalar::SIZE..];

let encrypted_data = PoseidonCipher::from_slice(bytes)
.map_err(|_| Error::InvalidCipher)?;
Expand Down
15 changes: 7 additions & 8 deletions src/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl TryFrom<i32> for NoteType {
pub struct Note {
pub(crate) note_type: NoteType,
pub(crate) value_commitment: JubJubExtended,
pub(crate) nonce: JubJubScalar,
pub(crate) nonce: BlsScalar,
pub(crate) stealth_address: StealthAddress,
pub(crate) pos: u64,
pub(crate) encrypted_data: PoseidonCipher,
Expand All @@ -85,7 +85,7 @@ impl Note {
blinding_factor: JubJubScalar,
) -> Self {
let r = JubJubScalar::random(rng);
let nonce = JubJubScalar::random(rng);
let nonce = BlsScalar::random(rng);

Self::deterministic(note_type, &r, nonce, psk, value, blinding_factor)
}
Expand Down Expand Up @@ -122,7 +122,7 @@ impl Note {
pub fn deterministic(
note_type: NoteType,
r: &JubJubScalar,
nonce: JubJubScalar,
nonce: BlsScalar,
psk: &PublicSpendKey,
value: u64,
blinding_factor: JubJubScalar,
Expand Down Expand Up @@ -175,11 +175,10 @@ impl Note {
) -> Result<(u64, JubJubScalar), BytesError> {
let R = self.stealth_address.R();
let shared_secret = dhke(vk.a(), R);
let nonce = BlsScalar::from(self.nonce);

let data = self
.encrypted_data
.decrypt(&shared_secret, &nonce)
.decrypt(&shared_secret, &self.nonce)
.map_err(|_| BytesError::InvalidData)?;

let value = data[0].reduce();
Expand Down Expand Up @@ -213,7 +212,7 @@ impl Note {
BlsScalar::from(self.note_type as u64),
value_commitment[0],
value_commitment[1],
BlsScalar::from(self.nonce),
self.nonce,
pk_r[0],
pk_r[1],
R[0],
Expand Down Expand Up @@ -248,7 +247,7 @@ impl Note {
}

/// Nonce used for the encrypt / decrypt of data for this note
pub const fn nonce(&self) -> &JubJubScalar {
pub const fn nonce(&self) -> &BlsScalar {
&self.nonce
}

Expand Down Expand Up @@ -332,7 +331,7 @@ impl Serializable<{ 137 + PoseidonCipher::SIZE }> for Note {
bytes[0].try_into().map_err(|_| BytesError::InvalidData)?;
let value_commitment =
JubJubExtended::from(JubJubAffine::from_slice(&bytes[1..33])?);
let nonce = JubJubScalar::from_slice(&bytes[33..65])?;
let nonce = BlsScalar::from_slice(&bytes[33..65])?;
let stealth_address = StealthAddress::from_slice(&bytes[65..129])?;

one_u64.copy_from_slice(&bytes[129..137]);
Expand Down
4 changes: 2 additions & 2 deletions tests/note_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Copyright (c) DUSK NETWORK. All rights reserved.

use core::convert::TryInto;

use dusk_bls12_381::BlsScalar;
use dusk_jubjub::{JubJubScalar, GENERATOR_EXTENDED, GENERATOR_NUMS_EXTENDED};
use dusk_pki::{Ownable, SecretSpendKey};
use phoenix_core::{Crossover, Error, Fee, Note, NoteType};
Expand Down Expand Up @@ -55,7 +55,7 @@ fn obfuscated_deterministic_note() -> Result<(), Error> {
let value = 25;

let r = JubJubScalar::random(rng);
let nonce = JubJubScalar::random(rng);
let nonce = BlsScalar::random(rng);
let blinding_factor = JubJubScalar::random(rng);

let note = Note::deterministic(
Expand Down

0 comments on commit 9dea335

Please sign in to comment.