From 0cf629ef0d6d5dbca2b95e6618e47d9f92b999db Mon Sep 17 00:00:00 2001 From: CPerezz Date: Wed, 9 Jun 2021 21:53:47 +0200 Subject: [PATCH 1/7] Change `JubJubScalar` for `BlsScalar` for `nonce`s Resolves: #84 --- CHANGELOG.md | 4 ++++ src/convert.rs | 2 +- src/crossover.rs | 10 +++++----- src/message.rs | 10 +++++----- src/note.rs | 15 +++++++-------- tests/note_test.rs | 4 ++-- 6 files changed, 24 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ccf6d46..49bc1d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### 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 diff --git a/src/convert.rs b/src/convert.rs index 8d11314..d4f1c04 100644 --- a/src/convert.rs +++ b/src/convert.rs @@ -79,7 +79,7 @@ impl From 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) diff --git a/src/crossover.rs b/src/crossover.rs index 6ee973e..63c1e45 100644 --- a/src/crossover.rs +++ b/src/crossover.rs @@ -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; @@ -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, } @@ -53,7 +53,7 @@ impl Serializable<{ 64 + PoseidonCipher::SIZE }> for Crossover { fn from_bytes(bytes: &[u8; Self::SIZE]) -> Result { 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..])?; @@ -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 @@ -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 } diff --git a/src/message.rs b/src/message.rs index e67c74a..f248c92 100644 --- a/src/message.rs +++ b/src/message.rs @@ -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, } @@ -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( @@ -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 } @@ -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)?; diff --git a/src/note.rs b/src/note.rs index 62214a8..5a19b88 100644 --- a/src/note.rs +++ b/src/note.rs @@ -61,7 +61,7 @@ impl TryFrom 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, @@ -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) } @@ -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, @@ -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(); @@ -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], @@ -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 } @@ -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]); diff --git a/tests/note_test.rs b/tests/note_test.rs index bbfc547..5651f74 100644 --- a/tests/note_test.rs +++ b/tests/note_test.rs @@ -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}; @@ -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( From f776c21004409dd8f58c4c753474c7f7da44e569 Mon Sep 17 00:00:00 2001 From: CPerezz Date: Wed, 9 Jun 2021 22:13:04 +0200 Subject: [PATCH 2/7] Add `dusk-bytes::BadLength` impl for crate Error Resolves: #88 --- CHANGELOG.md | 4 ++++ src/error.rs | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ccf6d46..3eba1e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Add `dusk-bytes::BadLength` impl for crate Error [#88](https://github.com/dusk-network/phoenix-core/issues/88) + ## [0.11.0] - 2021-06-09 ### Added diff --git a/src/error.rs b/src/error.rs index 61c2779..a8cb392 100644 --- a/src/error.rs +++ b/src/error.rs @@ -4,6 +4,7 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. +use dusk_bytes::BadLength; use dusk_poseidon::Error as PoseidonError; use core::fmt; @@ -32,6 +33,8 @@ pub enum Error { InvalidCommitment, /// Invalid Nonce InvalidNonce, + /// Dusk-bytes BadLenght error + BadLenght(usize, usize), } impl From for Error { @@ -45,3 +48,9 @@ impl fmt::Display for Error { write!(f, "Phoenix-Core Error: {:?}", &self) } } + +impl BadLength for Error { + fn bad_length(found: usize, expected: usize) -> Self { + Error::BadLenght(found, expected) + } +} From 9fc0132a07a51e83b2776b3161c51f5b7de0cbe5 Mon Sep 17 00:00:00 2001 From: CPerezz Date: Thu, 10 Jun 2021 01:51:49 +0200 Subject: [PATCH 3/7] Add From impl for dusk-bytes::Error This was needed so that we can use `?` inside of Serializable impls without needing to map errors on libs that depend on structures of this one. Resolves: #92 --- src/error.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/error.rs b/src/error.rs index a8cb392..6d02994 100644 --- a/src/error.rs +++ b/src/error.rs @@ -4,7 +4,7 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. -use dusk_bytes::BadLength; +use dusk_bytes::{BadLength, Error as DuskBytesError, InvalidChar}; use dusk_poseidon::Error as PoseidonError; use core::fmt; @@ -33,8 +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 for Error { @@ -49,8 +53,29 @@ impl fmt::Display for Error { } } +impl From 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) + } +} From 278875ed52be9de8ec9c0f90834e2c6b5e8659c0 Mon Sep 17 00:00:00 2001 From: CPerezz Date: Thu, 10 Jun 2021 01:52:49 +0200 Subject: [PATCH 4/7] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1b6c11..7536c9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Add `dusk-bytes::BadLength` impl for crate Error [#88](https://github.com/dusk-network/phoenix-core/issues/88) - +- Add `From` 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) From da4b6651a21b324a74666d27322485eeedac9af0 Mon Sep 17 00:00:00 2001 From: CPerezz Date: Mon, 5 Jul 2021 19:08:00 +0200 Subject: [PATCH 5/7] Update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7536c9d..22f31b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ 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) From 5e19e2d11c5cf0de922b060660e453c9f50dd57c Mon Sep 17 00:00:00 2001 From: CPerezz Date: Mon, 5 Jul 2021 19:08:40 +0200 Subject: [PATCH 6/7] Remove rc versions freom poseidon & pki --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9e12b89..1718b2c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } From 44c2b61d14399f44ba9e84f15d1bbb61efd20bbe Mon Sep 17 00:00:00 2001 From: CPerezz Date: Mon, 5 Jul 2021 19:09:20 +0200 Subject: [PATCH 7/7] Release 0.12.0 --- Cargo.toml | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1718b2c..6b3dae1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "phoenix-core" -version = "0.11.0" +version = "0.12.0" authors = ["zer0 ", "Victor Lopez