Skip to content

Commit

Permalink
Merge pull request #55 from dusk-network/vlopes11/45-cipher-from-to-b…
Browse files Browse the repository at this point in the history
…ytes

Implements #45 - From/to bytes for PoseidonCipher
  • Loading branch information
CPerezz authored Sep 7, 2020
2 parents 51fcc8f + e3a2311 commit 9044dfa
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.6.4] - 07-09-20
### Added
- `PoseidonCipher` from/to bytes.

## [0.6.3] - 01-09-20
### Added
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.6.3"
version = "0.6.4"
authors = [
"zer0 <[email protected]>", "vlopes11 <[email protected]>", "CPerezz <[email protected]>", "Kristoffer Ström <[email protected]>"
]
Expand Down
42 changes: 41 additions & 1 deletion src/cipher/cipher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use dusk_plonk::jubjub::AffinePoint;
use dusk_plonk::prelude::*;
use hades252::{ScalarStrategy, Strategy, WIDTH};

use super::{CIPHER_SIZE, ENCRYPTED_DATA_SIZE, MESSAGE_CAPACITY};
use super::{
CIPHER_BYTES_SIZE, CIPHER_SIZE, ENCRYPTED_DATA_SIZE, MESSAGE_CAPACITY,
};

use std::io;

Expand Down Expand Up @@ -93,6 +95,44 @@ impl PoseidonCipher {
Self { cipher }
}

/// Convert the instance to a bytes representation
pub fn to_bytes(&self) -> [u8; CIPHER_BYTES_SIZE] {
let mut bytes = [0u8; CIPHER_BYTES_SIZE];

self.cipher.iter().enumerate().for_each(|(i, c)| {
let n = i * 32;
bytes[n..n + 32].copy_from_slice(&c.to_bytes());
});

bytes
}

/// Create an instance from a previous `PoseidonCipher::to_bytes` function
pub fn from_bytes(bytes: &[u8; CIPHER_BYTES_SIZE]) -> Option<Self> {
let mut cipher: [Option<BlsScalar>; CIPHER_SIZE] = [None; CIPHER_SIZE];
let mut b = [0u8; 32];

cipher.iter_mut().enumerate().for_each(|(i, c)| {
let n = i * 32;
b.copy_from_slice(&bytes[n..n + 32]);

let s = BlsScalar::from_bytes(&b);
if s.is_some().into() {
c.replace(s.unwrap());
}
});

let mut scalars = [BlsScalar::zero(); CIPHER_SIZE];
for (c, s) in cipher.iter().zip(scalars.iter_mut()) {
match c {
Some(c) => *s = *c,
None => return None,
}
}

Some(PoseidonCipher::new(scalars))
}

/// Maximum number of scalars allowed per message
pub fn capacity() -> usize {
MESSAGE_CAPACITY
Expand Down
3 changes: 3 additions & 0 deletions src/cipher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ pub const MESSAGE_CAPACITY: usize = 2;
/// Number of scalars used in a cipher
pub const CIPHER_SIZE: usize = MESSAGE_CAPACITY + 1;

/// Number of bytes used by from/to bytes `PoseidonCipher` function
pub const CIPHER_BYTES_SIZE: usize = CIPHER_SIZE * 32;

/// Bytes consumed on serialization of the poseidon cipher
pub const ENCRYPTED_DATA_SIZE: usize = CIPHER_SIZE * 32;

Expand Down
18 changes: 18 additions & 0 deletions src/cipher/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,21 @@ fn serialization() -> Result<()> {

Ok(())
}

#[test]
fn bytes() -> Result<()> {
let (message, secret, nonce) = gen();

let cipher = PoseidonCipher::encrypt(&message, &secret, &nonce);

let bytes = cipher.to_bytes();
let restored_cipher = PoseidonCipher::from_bytes(&bytes).unwrap();

assert_eq!(cipher, restored_cipher);

let decrypt = restored_cipher.decrypt(&secret, &nonce)?;

assert_eq!(message, decrypt);

Ok(())
}

0 comments on commit 9044dfa

Please sign in to comment.