Skip to content

Commit

Permalink
Merge pull request #91 from dusk-network/vlopes11/leaf-note-consistency
Browse files Browse the repository at this point in the history
PoseidonLeaf pos API consistency
  • Loading branch information
vlopes11 authored Nov 4, 2020
2 parents f5906e1 + 04c00bc commit 10151b2
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 86 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "poseidon252"
version = "0.12.0"
version = "0.13.0"
authors = [
"zer0 <[email protected]>", "vlopes11 <[email protected]>", "CPerezz <[email protected]>", "Kristoffer Ström <[email protected]>"
]
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ impl PoseidonLeaf<MemStore> 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;
}
}
Expand Down
46 changes: 1 addition & 45 deletions src/cipher/cipher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ use super::{
CIPHER_BYTES_SIZE, CIPHER_SIZE, ENCRYPTED_DATA_SIZE, MESSAGE_CAPACITY,
};

use std::io;

pub use super::CipherError;

/// ```ignore
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -251,45 +249,3 @@ impl PoseidonCipher {
[domain, length, ks0, ks1, nonce]
}
}

impl io::Write for PoseidonCipher {
fn write(&mut self, buf: &[u8]) -> Result<usize, io::Error> {
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<usize, io::Error> {
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)
})
}
}
3 changes: 3 additions & 0 deletions src/cipher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 1 addition & 28 deletions src/cipher/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))?;
Expand Down Expand Up @@ -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 })
Expand Down
10 changes: 5 additions & 5 deletions src/tree/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ impl PoseidonLeaf<MemStore> 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;
}
}

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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;
});

Expand Down

0 comments on commit 10151b2

Please sign in to comment.