Skip to content

Commit

Permalink
Issue #114 - Publish poseidon in crate.io (#115)
Browse files Browse the repository at this point in the history
* Require hades as `dusk-hades="0.13"`

* Implement `Canon` for `PoseidonBranch`

* Publish `dusk-poseidon` `v0.18.0`
  • Loading branch information
vlopes11 authored Feb 11, 2021
1 parent d040583 commit 66a2a1e
Show file tree
Hide file tree
Showing 16 changed files with 58 additions and 45 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.18.0] - 2021-02-11

### Changed

- Change crate's name from `Poseidon252` to `dusk-poseidon`
- Implement `Canon` for `PoseidonBranch`

## [0.17.0] - 2021-02-01

### Changed
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Contributing to Poseidon252
# Contributing to Dusk-Poseidon

All of the code under this repository is licensed under the
Mozilla Public License Version 2.0.
Expand Down
13 changes: 9 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
[package]
name = "poseidon252"
version = "0.17.0"
name = "dusk-poseidon"
version = "0.18.0"
authors = [
"zer0 <[email protected]>", "vlopes11 <[email protected]>", "CPerezz <[email protected]>", "Kristoffer Ström <[email protected]>"
]
edition = "2018"
description ="Implementation of Poseidon hash algorithm over the Bls12-381 Scalar field."
categories = ["algorithms", "cryptography", "no-std", "wasm"]
keywords = ["cryptography", "zero-knowledge", "crypto"]
license = "MPL-2.0"
repository = "https://github.com/dusk-network/poseidon252"

[dependencies]
dusk-bls12_381 = {version = "0.6", default-features = false}
dusk-jubjub = {version = "0.8", default-features = false}
dusk-bytes = "0.1"
hades252 = { git = "https://github.com/dusk-network/hades252", tag = "v0.12.0", default-features = false }
dusk-hades = { version = "0.13", default-features = false }
canonical = {version = "0.5", optional = true}
canonical_derive = {version = "0.5", optional = true}
microkelvin = {version = "0.6", optional = true}
Expand All @@ -29,7 +34,7 @@ criterion = "0.3"
default = ["std"]
alloc = []
std = [
"hades252/default",
"dusk-hades/default",
"dusk-bls12_381/default",
"dusk-jubjub/std",
"dusk-plonk",
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
![Build Status](https://github.com/dusk-network/Poseidon252/workflows/Continuous%20integration/badge.svg)
[![Repository](https://img.shields.io/badge/github-poseidon252-blueviolet)](https://github.com/dusk-network/Poseidon252)
[![Documentation](https://img.shields.io/badge/docs-poseidon252-blue)](https://dusk-network.github.io/Poseidon252/index.html)
[![Documentation](https://img.shields.io/badge/docs-poseidon252-blue)](https://docs.rs/dusk-poseidon/latest/dusk_poseidon/)

# Poseidon252
# Dusk-Poseidon

Reference implementation for the Poseidon Hashing algorithm.

Expand All @@ -15,7 +15,7 @@ required to perform Poseidon Hashes.

This hashes heavily rely on the Hades permutation, which is one of the key parts that Poseidon needs in order
to work.
This library uses the reference implementation of [Hades252](https://github.com/dusk-network/hades252) which has been
This library uses the reference implementation of [Dusk-Hades](https://github.com/dusk-network/hades252) which has been
designed & build by the [Dusk-Network team](https://dusk.network/).

**The library provides the two hashing techniques of Poseidon:**
Expand Down Expand Up @@ -45,7 +45,7 @@ The module provides two sponge hash implementations:
The Merkle Level Hashing is a technique that Poseidon is optimized-by-design
to perform.
This technique allows us to perform hashes of an entire Merkle Tree using
`Hades252` as backend.
`Dusk-Hades` as backend.

The technique requires the computation of a `bitflags` element which is always
positioned as the first item of the level when we hash it, and it basically generated
Expand Down Expand Up @@ -77,7 +77,7 @@ use canonical::Canon;
use canonical_derive::Canon;
use canonical_host::MemStore;
use dusk_plonk::prelude::*;
use poseidon252::tree::{PoseidonAnnotation, PoseidonLeaf, PoseidonTree, merkle_opening};
use dusk_poseidon::tree::{PoseidonAnnotation, PoseidonLeaf, PoseidonTree, merkle_opening};
// Constant depth of the merkle tree
const DEPTH: usize = 17;
Expand Down
4 changes: 2 additions & 2 deletions src/cipher/cipher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use canonical_derive::Canon;

use dusk_bls12_381::BlsScalar;
use dusk_bytes::{DeserializableSlice, Error as BytesError, Serializable};
use dusk_hades::strategies::{ScalarStrategy, Strategy};
use dusk_jubjub::JubJubAffine;
use hades252::strategies::{ScalarStrategy, Strategy};

const MESSAGE_CAPACITY: usize = 2;
const CIPHER_SIZE: usize = MESSAGE_CAPACITY + 1;
Expand Down Expand Up @@ -81,7 +81,7 @@ impl PoseidonCipher {
pub fn initial_state(
secret: &JubJubAffine,
nonce: BlsScalar,
) -> [BlsScalar; hades252::WIDTH] {
) -> [BlsScalar; dusk_hades::WIDTH] {
[
// Domain - Maximum plaintext length of the elements of Fq, as
// defined in the paper
Expand Down
6 changes: 3 additions & 3 deletions src/cipher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

//! # Poseidon 252 Cipher
//! # Dusk-Poseidon Cipher
//!
//! Encryption/decryption implementation with Poseidon252 backend.
//! Encryption/decryption implementation with Dusk-Poseidon backend.
//!
//! This implementation is optimized for a message containing 2 scalars.
//!
Expand All @@ -24,7 +24,7 @@
//! use core::ops::Mul;
//! use dusk_bls12_381::BlsScalar;
//! use dusk_jubjub::{dhke, JubJubExtended, JubJubScalar, GENERATOR};
//! use poseidon252::cipher::PoseidonCipher;
//! use dusk_poseidon::cipher::PoseidonCipher;
//!
//! fn sender(
//! sender_secret: &JubJubScalar,
Expand Down
2 changes: 1 addition & 1 deletion src/cipher/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn sanity() {

// The hades permutation cannot be performed if the cipher is bigger than
// hades width
assert!(hades252::WIDTH >= PoseidonCipher::cipher_size());
assert!(dusk_hades::WIDTH >= PoseidonCipher::cipher_size());
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions src/cipher/zk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

use crate::cipher::PoseidonCipher;
use dusk_bls12_381::BlsScalar;
use dusk_hades::strategies::{GadgetStrategy, Strategy};
use dusk_plonk::constraint_system::ecc::Point;
use dusk_plonk::prelude::*;
use hades252::strategies::{GadgetStrategy, Strategy};

impl PoseidonCipher {
/// Returns the initial state of the encryption within a composer circuit
Expand All @@ -17,7 +17,7 @@ impl PoseidonCipher {
ks0: Variable,
ks1: Variable,
nonce: Variable,
) -> [Variable; hades252::WIDTH] {
) -> [Variable; dusk_hades::WIDTH] {
let domain = BlsScalar::from_raw([0x100000000u64, 0, 0, 0]);
let domain = composer.add_witness_to_circuit_description(domain);

Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub enum Error<E: fmt::Debug> {
#[cfg(feature = "std")]
impl<E: fmt::Debug> std_fmt::Display for Error<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std_fmt::Result {
write!(f, "Poseidon252 Error: {:?}", &self)
write!(f, "Dusk-Poseidon Error: {:?}", &self)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/perm_uses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
//! The `pad` module implements the padding algorithm on the Poseidon hash.
use dusk_bls12_381::BlsScalar;
use hades252::strategies::{ScalarStrategy, Strategy};
use dusk_hades::strategies::{ScalarStrategy, Strategy};

/// Takes in one BlsScalar and outputs 2.
/// This function is fixed.
pub fn two_outputs(message: BlsScalar) -> [BlsScalar; 2] {
const CAPACITY: BlsScalar = BlsScalar::from_raw([0, 1, 0, 0]);

let mut words = [BlsScalar::zero(); hades252::WIDTH];
let mut words = [BlsScalar::zero(); dusk_hades::WIDTH];

words[0] = CAPACITY;
words[1] = message;
Expand Down
6 changes: 3 additions & 3 deletions src/sponge/sponge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
//! Sponge hash and gadget definition
use dusk_bls12_381::BlsScalar;
use hades252::{ScalarStrategy, Strategy, WIDTH};
use dusk_hades::{ScalarStrategy, Strategy, WIDTH};

#[cfg(feature = "std")]
use dusk_plonk::prelude::*;

#[cfg(feature = "std")]
use hades252::GadgetStrategy;
use dusk_hades::GadgetStrategy;

/// The `hash` function takes an arbitrary number of Scalars and returns the
/// hash, using the `Hades` ScalarStragegy.
Expand Down Expand Up @@ -176,7 +176,7 @@ pub fn sponge_gadget(
#[cfg(feature = "std")]
mod tests {
use anyhow::Result;
use hades252::WIDTH;
use dusk_hades::WIDTH;

use super::*;

Expand Down
4 changes: 2 additions & 2 deletions src/tree/annotation/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use canonical::{Canon, Store};
use canonical_derive::Canon;
use core::borrow::Borrow;
use dusk_bls12_381::BlsScalar;
use hades252::{ScalarStrategy, Strategy};
use dusk_hades::{ScalarStrategy, Strategy};
use microkelvin::{Annotation, Cardinality};
use nstack::NStack;

Expand All @@ -36,7 +36,7 @@ impl PoseidonAnnotation {
let cardinality =
<Cardinality as Annotation<NStack<L, A, S>, S>>::from_node(node);

let mut perm = [BlsScalar::zero(); hades252::WIDTH];
let mut perm = [BlsScalar::zero(); dusk_hades::WIDTH];
let mut flag = 1;
let mut mask = 0;

Expand Down
21 changes: 11 additions & 10 deletions src/tree/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,26 @@
use super::{PoseidonLeaf, PoseidonTreeAnnotation};

use alloc::vec::Vec;
use canonical::Store;
use canonical::{Canon, Store};
use canonical_derive::Canon;
use core::iter;
use core::ops::Deref;
use dusk_bls12_381::BlsScalar;
use hades252::{ScalarStrategy, Strategy};
use dusk_hades::{ScalarStrategy, Strategy};
use microkelvin::Branch;
use nstack::NStack;

/// Represents a level of a branch on a given depth
#[derive(Debug, Default, Clone, Copy)]
#[derive(Debug, Default, Clone, Copy, Canon)]
pub struct PoseidonLevel {
level: [BlsScalar; hades252::WIDTH],
offset: usize,
level: [BlsScalar; dusk_hades::WIDTH],
offset: u64,
}

impl PoseidonLevel {
/// Represents the offset of a node for a given path produced by a branch
/// in a merkle opening
pub fn offset(&self) -> usize {
pub fn offset(&self) -> u64 {
self.offset
}
}
Expand All @@ -34,7 +35,7 @@ impl Deref for PoseidonLevel {
type Target = BlsScalar;

fn deref(&self) -> &Self::Target {
&self.level[self.offset]
&self.level[self.offset as usize]
}
}

Expand All @@ -45,7 +46,7 @@ impl AsRef<[BlsScalar]> for PoseidonLevel {
}

/// Represents a full path for a merkle opening
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Canon)]
pub struct PoseidonBranch<const DEPTH: usize> {
path: Vec<PoseidonLevel>,
}
Expand Down Expand Up @@ -104,7 +105,7 @@ where
.zip(branch.path.iter_mut())
.for_each(|(l, b)| {
depth += 1;
b.offset = l.offset() + 1;
b.offset = l.offset() as u64 + 1;

let mut flag = 1;
let mut mask = 0;
Expand Down Expand Up @@ -143,7 +144,7 @@ where

let flag = BlsScalar::one();
let level = branch.path[depth - 1].level;
let mut perm = [BlsScalar::zero(); hades252::WIDTH];
let mut perm = [BlsScalar::zero(); dusk_hades::WIDTH];

let mut h = ScalarStrategy::new();
branch.path.iter_mut().skip(depth).fold(level, |l, b| {
Expand Down
6 changes: 3 additions & 3 deletions src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

//! # Poseidon252 Merkle Tree
//! # Dusk-Poseidon Merkle Tree
//!
//! Implementation of a Merkle Tree with a Poseidon252 backend and zero-knowledge opening proof powered by PLONK.
//! Implementation of a Merkle Tree with a Dusk-Poseidon backend and zero-knowledge opening proof powered by PLONK.
//!
//! ### Example
//!
Expand All @@ -18,7 +18,7 @@
//! use canonical_derive::Canon;
//! use canonical_host::MemStore;
//! use dusk_plonk::prelude::*;
//! use poseidon252::tree::{merkle_opening, PoseidonAnnotation, PoseidonLeaf, PoseidonTree};
//! use dusk_poseidon::tree::{merkle_opening, PoseidonAnnotation, PoseidonLeaf, PoseidonTree};
//!
//! // Constant depth of the merkle tree
//! const DEPTH: usize = 17;
Expand Down
6 changes: 3 additions & 3 deletions src/tree/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use canonical_derive::Canon;
use canonical_host::MemStore;
use core::borrow::Borrow;
use dusk_bls12_381::BlsScalar;
use hades252::{ScalarStrategy, Strategy};
use dusk_hades::{ScalarStrategy, Strategy};

#[derive(Debug, Default, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Canon)]
pub struct MockLeaf {
Expand Down Expand Up @@ -147,7 +147,7 @@ fn tree_branch_leaf() {
const DEPTH: usize = 17;

let mut h = ScalarStrategy::new();
let zero = [BlsScalar::zero(); hades252::WIDTH];
let zero = [BlsScalar::zero(); dusk_hades::WIDTH];
let mut perm = zero;

[
Expand Down Expand Up @@ -202,7 +202,7 @@ fn tree_branch_depth() {
let leaf = MockLeaf::from(1);
tree.push(leaf).unwrap();

let mut perm_base = [BlsScalar::zero(); hades252::WIDTH];
let mut perm_base = [BlsScalar::zero(); dusk_hades::WIDTH];
perm_base[0] = BlsScalar::one();
perm_base[1] = leaf.poseidon_hash();

Expand Down
6 changes: 3 additions & 3 deletions src/tree/zk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// Copyright (c) DUSK NETWORK. All rights reserved.

use super::PoseidonBranch;
use dusk_hades::{GadgetStrategy, Strategy};
use dusk_plonk::prelude::*;
use hades252::{GadgetStrategy, Strategy};

/// Perform a merkle opening for a given branch and return the calculated root
pub fn merkle_opening<const DEPTH: usize>(
Expand All @@ -19,13 +19,13 @@ pub fn merkle_opening<const DEPTH: usize>(
let mut root = zero;

// Generate a permutation container
let mut perm = [zero; hades252::WIDTH];
let mut perm = [zero; dusk_hades::WIDTH];

// For every level, replace the level offset with needle,
// permutate the level and set the needle to the next level
// to the poseidon result of the permutation
branch.as_ref().iter().fold(leaf, |needle, level| {
let offset = level.offset();
let offset = level.offset() as usize;

level.as_ref().iter().enumerate().for_each(|(i, l)| {
if i != offset {
Expand Down

0 comments on commit 66a2a1e

Please sign in to comment.