diff --git a/CHANGELOG.md b/CHANGELOG.md index 03a24dd..2d50616 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.13.0] - 04-11-20 +### Changed +- PoseidonLeaf pos setter for API consistency with Phoenix + +### Removed +- PoseidonCipher std::io implementations + ## [0.12.0] - 03-11-20 ### Added - Gate-featured `canonical` impl. diff --git a/Cargo.toml b/Cargo.toml index 23dc483..d758566 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "poseidon252" -version = "0.12.0" +version = "0.13.0" authors = [ "zer0 ", "vlopes11 ", "CPerezz ", "Kristoffer Ström " ] diff --git a/README.md b/README.md index e970755..c50c6b1 100644 --- a/README.md +++ b/README.md @@ -107,13 +107,13 @@ impl PoseidonLeaf for DataLeaf { } // Position on the tree - fn tree_pos(&self) -> u64 { + fn pos(&self) -> u64 { self.pos } // Method used to set the position on the tree after the `PoseidonTree::push` call - fn tree_pos_mut(&mut self) -> &mut u64 { - &mut self.pos + fn set_pos(&mut self, pos: u64) { + self.pos = pos; } } diff --git a/src/cipher/cipher.rs b/src/cipher/cipher.rs index 2b06338..9ec9f5b 100644 --- a/src/cipher/cipher.rs +++ b/src/cipher/cipher.rs @@ -16,8 +16,6 @@ use super::{ CIPHER_BYTES_SIZE, CIPHER_SIZE, ENCRYPTED_DATA_SIZE, MESSAGE_CAPACITY, }; -use std::io; - pub use super::CipherError; /// ```ignore @@ -148,7 +146,7 @@ impl PoseidonCipher { } /// Bytes consumed on serialization of the poseidon cipher - pub fn serialized_size() -> usize { + pub const fn serialized_size() -> usize { ENCRYPTED_DATA_SIZE } @@ -251,45 +249,3 @@ impl PoseidonCipher { [domain, length, ks0, ks1, nonce] } } - -impl io::Write for PoseidonCipher { - fn write(&mut self, buf: &[u8]) -> Result { - if buf.len() < ENCRYPTED_DATA_SIZE { - return Err(io::Error::from(io::ErrorKind::UnexpectedEof)); - } - - let mut bytes = [0u8; 32]; - self.cipher.iter_mut().try_fold(0usize, |mut n, x| { - n += bytes.as_mut().write(&buf[n..n + 32])?; - - // Constant time option is REALLY inflexible, so this is required - let scalar = BlsScalar::from_bytes(&bytes); - - if scalar.is_none().into() { - return Err(io::Error::from(io::ErrorKind::InvalidData)); - } - - *x = scalar.unwrap(); - - Ok(n) - }) - } - - fn flush(&mut self) -> Result<(), io::Error> { - Ok(()) - } -} - -impl io::Read for PoseidonCipher { - fn read(&mut self, buf: &mut [u8]) -> Result { - if buf.len() < ENCRYPTED_DATA_SIZE { - return Err(io::Error::from(io::ErrorKind::UnexpectedEof)); - } - - self.cipher.iter_mut().try_fold(0usize, |n, x| { - let s = (&mut x.to_bytes().as_ref()).read(&mut buf[n..n + 32])?; - - Ok(n + s) - }) - } -} diff --git a/src/cipher/mod.rs b/src/cipher/mod.rs index 8b18b67..1cbad7c 100644 --- a/src/cipher/mod.rs +++ b/src/cipher/mod.rs @@ -17,6 +17,9 @@ pub const CIPHER_SIZE: usize = MESSAGE_CAPACITY + 1; pub const CIPHER_BYTES_SIZE: usize = CIPHER_SIZE * 32; /// Bytes consumed on serialization of the poseidon cipher +/// +/// This is kept for backwards compatibility since the constant definition is +/// redundant to [`CIPHER_BYTES_SIZE`] pub const ENCRYPTED_DATA_SIZE: usize = CIPHER_SIZE * 32; /// [`PoseidonCipher`] definition diff --git a/src/cipher/tests.rs b/src/cipher/tests.rs index 2468793..f5d92a7 100644 --- a/src/cipher/tests.rs +++ b/src/cipher/tests.rs @@ -4,15 +4,12 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. -use super::{ - PoseidonCipher, CIPHER_SIZE, ENCRYPTED_DATA_SIZE, MESSAGE_CAPACITY, -}; +use super::{PoseidonCipher, CIPHER_SIZE, MESSAGE_CAPACITY}; use anyhow::Result; use dusk_plonk::jubjub::{AffinePoint, Fr, GENERATOR}; use dusk_plonk::prelude::*; use hades252::WIDTH; use rand::RngCore; -use std::io::{Read, Write}; use std::ops::Mul; fn gen() -> ([BlsScalar; MESSAGE_CAPACITY], AffinePoint, BlsScalar) { @@ -93,30 +90,6 @@ fn wrong_key_fail() { assert!(cipher.decrypt(&wrong_secret, &nonce).is_err()); } -#[test] -fn serialization() -> Result<()> { - let (message, secret, nonce) = gen(); - - let mut cipher = PoseidonCipher::encrypt(&message, &secret, &nonce); - - let mut bytes = vec![0u8; ENCRYPTED_DATA_SIZE]; - - let n = cipher.read(bytes.as_mut_slice())?; - assert_eq!(n, PoseidonCipher::serialized_size()); - - let mut deser_cipher = PoseidonCipher::default(); - let n = deser_cipher.write(bytes.as_slice())?; - assert_eq!(n, PoseidonCipher::serialized_size()); - - assert_eq!(cipher, deser_cipher); - - let decrypt = deser_cipher.decrypt(&secret, &nonce)?; - - assert_eq!(message, decrypt); - - Ok(()) -} - #[test] fn bytes() -> Result<()> { let (message, secret, nonce) = gen(); diff --git a/src/tree/mod.rs b/src/tree/mod.rs index ee26a12..b9fa9c3 100644 --- a/src/tree/mod.rs +++ b/src/tree/mod.rs @@ -41,13 +41,13 @@ where fn poseidon_hash(&self) -> BlsScalar; /// Index of the leaf structure on the merkle tree. - fn tree_pos(&self) -> u64; + fn pos(&self) -> u64; /// Index of the leaf structure on the merkle tree. /// /// This method is internally used to set the index after the data has been inserted in the /// merkle tree. - fn tree_pos_mut(&mut self) -> &mut u64; + fn set_pos(&mut self, pos: u64); } /// Represents a Merkle Tree with a given depth that will be calculated using poseidon hash @@ -117,7 +117,7 @@ where .sum(), }; - *leaf.tree_pos_mut() = size as u64; + leaf.set_pos(size as u64); self.inner .push(leaf) .map_err(|e| anyhow!("Error pushing to the tree: {:?}", e))?; @@ -210,7 +210,7 @@ where A::poseidon_walk(w, data.clone()) }) .map_err(|e| anyhow!("Error fetching the branch: {:?}", e))? - .map(|l| l.tree_pos()) + .map(|l| l.pos()) .unwrap_or(u64::max_value()) as usize; Ok(Self { tree, pos, data }) diff --git a/src/tree/tests.rs b/src/tree/tests.rs index 03250b2..10ff50a 100644 --- a/src/tree/tests.rs +++ b/src/tree/tests.rs @@ -39,12 +39,12 @@ impl PoseidonLeaf for MockLeaf { self.s } - fn tree_pos(&self) -> u64 { + fn pos(&self) -> u64 { self.pos } - fn tree_pos_mut(&mut self) -> &mut u64 { - &mut self.pos + fn set_pos(&mut self, pos: u64) { + self.pos = pos; } } @@ -102,7 +102,7 @@ fn tree_max_walk() { .map(|l| l.unwrap()) .enumerate() .for_each(|(i, leaf)| { - assert_eq!(pos + i as u64, leaf.tree_pos()); + assert_eq!(pos + i as u64, leaf.pos()); }); assert!(tree.iter_walk((max + 1) as u64).unwrap().next().is_none()); @@ -137,7 +137,7 @@ fn tree_max_walk_non_continuous() { if pos % 4 == 0 { pos += 1; } - assert_eq!(pos, leaf.tree_pos()); + assert_eq!(pos, leaf.pos()); pos += 1; });