Skip to content

Commit

Permalink
Restructure the project removing dependencies.
Browse files Browse the repository at this point in the history
Move out of the core project the following plugins:
- anemoi hash
- poseidon hash
- pow challenge generation
  • Loading branch information
mmaker committed Oct 22, 2024
1 parent d68eb56 commit 39c201d
Show file tree
Hide file tree
Showing 44 changed files with 175 additions and 146 deletions.
2 changes: 0 additions & 2 deletions .cargo/config.toml

This file was deleted.

80 changes: 10 additions & 70 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
[package]
name = "nimue"
version = "0.1.0"
authors = ["Michele Orrù <m@orru.net>"]
description = "A library for Fiat-Shamir transcripts."
edition = "2021"
license = "MIT/Apache-2.0"
[workspace]
resolver = "2"

members = [
"nimue",
"nimue-pow",
"nimue-anemoi",
"nimue-poseidon",
]


[patch.crates-io]
ark-std = { git = "https://github.com/arkworks-rs/utils" }
ark-ec = { git = "https://github.com/arkworks-rs/algebra" }
Expand All @@ -15,66 +17,4 @@ ark-serialize = { git = "https://github.com/arkworks-rs/algebra" }
ark-bls12-381 = { git = "https://github.com/arkworks-rs/algebra" }
ark-curve25519 = { git = "https://github.com/arkworks-rs/algebra" }
ark-pallas = { git = "https://github.com/arkworks-rs/algebra" }
ark-vesta = { git = "https://github.com/arkworks-rs/algebra" }

[dependencies]
zeroize = { version = "1.6.0", features = ["zeroize_derive"] }
rand = { version = "0.8.5", features = ["getrandom"] }
digest = "0.10.7"
generic-array = "0.14.7"
# used as default hasher for the prover
keccak = { version = "0.1.4"}
log = "0.4.20"
# optional dependencies
ark-ff = { version = "0.4.0", optional = true }
ark-ec = { version = "0.4.0", optional = true }
ark-serialize = { version = "0.4.2", optional = true, features = ["std"] }
# anemoi = {git = "https://github.com/anemoi-hash/anemoi-rust", optional=true}
group = { version = "0.13.0", optional = true }
ark-bls12-381 = { version = "0.4.0", optional = true }
rayon = { version = "1.10.0", optional = true }
bytemuck = "1.17.1"
# blake3 = {version = "1.5.4", optional = true}


[features]
default = ["parallel"]
parallel = ["dep:rayon"]
ark = ["dep:ark-ff", "dep:ark-ec", "dep:ark-serialize"]
group = ["dep:group"]
ark-bls12-381 = ["ark", "dep:ark-bls12-381"]
rayon = ["dep:rayon"]
asm = ["keccak/asm", "keccak/simd"]
# anemoi = ["dep:anemoi"]

[dev-dependencies]
ark-std = "0.4.0"
sha2 = "0.10.7"
blake2 = "0.10.6"
hex = "0.4.3"
# test curve25519 compatibility
curve25519-dalek = { version = "4.0.0", features = ["group"] }
ark-curve25519 = "0.4.0"
# test algebraic hashers
bls12_381 = "0.8.0"
anyhow = { version = "1.0.75", features = ["backtrace"] }
ark-pallas = { version = "0.4.0", features = ["std"] }
pallas = "0.22.0"
pasta_curves = "0.5.1"
ark-vesta = { version = "0.4.0", features = ["std"] }

[package.metadata.docs.rs]
rustdoc-args = ["--html-in-header", "doc/katex-header.html", "--cfg", "docsrs"]
features = ["ark", "group"]

[[example]]
name = "schnorr"
required-features = ["ark"]

[[example]]
name = "schnorr_algebraic_hash"
required-features = ["ark", "ark-bls112-381"]

[[example]]
name = "bulletproof"
required-features = ["ark"]
ark-vesta = { git = "https://github.com/arkworks-rs/algebra" }
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
Nimue: a Fiat-Shamir library
=========

**This library has not been externally reviewed yet and shouldn't be considered ready for deployments.**

Nimue is a hash-agnostic library that believes in random oracles.
It facilitates the writing of multi-round public coin protocols.
Built on the top of the SAFE framework and provides an API for generating the verifier's and prover's random coins.
It provides a generic API for generating the verifier's random coins and the prover randomness.
The project has the following crates:

- `nimue`: the core library, with bindings for [`group`](https://github.com/zkcrypto/group) and [arkworks](https://arkworks.rs). This crate provides the basic traits for hashes bases on **compression functions** and **sponge-based hash functions**, both via Rust's generic [`Digest`](https://docs.rs/digest/latest/digest/) API and a (more fine-grained and efficient) permutation function API
- `nimue-pow`: an extension for challenges computed via grinding / proof-of-work;
- `nimue-anemoi`: a **WORK IN PROGRESS** implementation of the [Anemoi](https://anemoi-hash.github.io/) hash function (in arkworks for now);
- `nimue-poseidon`: a **WORK IN PROGRESS** implementation of the [Poseidon](https://anemoi-hash.github.io/) hash function (in arkworks for now).

# Features

Expand All @@ -29,4 +33,5 @@ while maintaining security is crucial. We offer tools for preprocessing the Tran
- **Private randomness generation**.
It is vital to avoid providing two different challenges for the same prover message. We do our best to avoid it by tying down the prover randomness to the protocol transcript, without making the proof deterministic.

Check out the [documentation](https://docs.rs/nimue/latest/nimue/) and some [`examples/`](https://github.com/mmaker/nimue/tree/main/examples).
## More information
Check out the [documentation](https://arkworks.rs/nimue/) and some [`examples/`](https://github.com/arkworks-rs/nimue/tree/main/examples).
13 changes: 13 additions & 0 deletions nimue-anemoi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "nimue-anemoi"
version = "0.1.0"
edition = "2021"
license = "MIT/Apache-2.0"

[dependencies]
ark-ff = "^0.4.2"
nimue = { version = "0.1.0", path = "../nimue", features = ["ark"] }
zeroize = "1.8.1"
anemoi = {git = "https://github.com/mmaker/anemoi", features=["bls12_381"]}
ark-bls12-381 = "^0.4.0"
arrayvec = "0.7.6"
25 changes: 17 additions & 8 deletions src/plugins/ark/anemoi/mod.rs → nimue-anemoi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
//!
//! The main reason for this code not being deployed is that [anemoi](https://anemoi-hash.github.io/)'s Rust implementation
//! is not published as a crate and thus `nimue` cannot publish it along with a new release.

use ark_ff::Field;
use std::ops::{Index, IndexMut, Range, RangeFrom, RangeTo};
use anemoi;
use ark_ff::{Field, PrimeField};
use zeroize::Zeroize;

use super::sponge::Sponge;
use nimue::hash::sponge::Sponge;

#[derive(Clone, Zeroize)]
pub struct AnemoiState<F: Field, const R: usize, const N: usize>([F; N]);
Expand All @@ -18,7 +17,17 @@ impl<F: Field, const N: usize, const R: usize> Default for AnemoiState<F, R, N>
}
}

crate::hash::index::impl_indexing!(AnemoiState, 0, Output = F, Params = [F: Field], Constants = [R, N]);
impl<F: Field, const R: usize, const N: usize> AsRef<[F]> for AnemoiState<F, R, N> {
fn as_ref(&self) -> &[F] {
&self.0
}
}

impl<F: Field, const R: usize, const N: usize> AsMut<[F]> for AnemoiState<F, R, N> {
fn as_mut(&mut self) -> &mut [F] {
&mut self.0
}
}

pub type AnemoiBls12_381_2_1 = AnemoiState<anemoi::bls12_381::Felt, 2, 1>;
use anemoi::bls12_381::anemoi_2_1::AnemoiBls12_381_2_1 as _AnemoiBls12_381_2_1;
Expand All @@ -33,13 +42,13 @@ impl Sponge
{
type U = anemoi::bls12_381::Felt;

const CAPACITY: usize = _AnemoiBls12_381_2_1::WIDTH - _AnemoiBls12_381_2_1::RATE;
const N: usize = _AnemoiBls12_381_2_1::WIDTH;

const RATE: usize = _AnemoiBls12_381_2_1::RATE;
const R: usize = _AnemoiBls12_381_2_1::RATE;

fn new(iv: [u8; 32]) -> Self {
let mut state = Self::default();
state[RATE] = anemoi::bls12_381::Felt::from_le_bytes_mod_order(&iv);
state.as_mut()[Self::R] = anemoi::bls12_381::Felt::from_le_bytes_mod_order(&iv);
state
}

Expand Down
14 changes: 14 additions & 0 deletions nimue-poseidon/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "nimue-poseidon"
version = "0.1.0"
edition = "2021"
license = "MIT/Apache-2.0"

[dependencies]
nimue = { path = "../nimue", features = ["ark"]}
ark-ff = "^0.4.2"
zeroize = "1.8.1"
ark-bls12-381 = "^0.4.0"

[dev-dependencies]
ark-bls12-381 = "^0.4.0"
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::plugins::ark::poseidon;

mod fr_3_2;

poseidon_sponge!(PoseidonBls12381Fr3_1, fr_3_2);
19 changes: 11 additions & 8 deletions src/plugins/ark/poseidon/mod.rs → nimue-poseidon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
//! from William Lin, with contributions from Pratyush Mishra, Weikeng Chen, Yuwen Zhang, Kristian Sosnin, Merlyn, Wilson Nguyen, Hossein Moghaddas, and others.
use ark_ff::PrimeField;

use crate::hash::sponge::Sponge;
use crate::hash::Unit;
use nimue::hash::sponge::DuplexSponge;
use nimue::hash::sponge::Sponge;
use nimue::hash::Unit;

#[derive(Clone)]
pub struct PoseidonSponge<F: PrimeField, const R: usize, const N: usize> {
Expand All @@ -23,8 +24,7 @@ pub struct PoseidonSponge<F: PrimeField, const R: usize, const N: usize> {
pub state: [F; N],
}

pub type PoseidonHash<F, const R: usize, const N: usize> =
crate::hash::sponge::DuplexSponge<PoseidonSponge<F, R, N>>;
pub type PoseidonHash<F, const R: usize, const N: usize> = DuplexSponge<PoseidonSponge<F, R, N>>;

impl<F: PrimeField, const R: usize, const N: usize> AsRef<[F]> for PoseidonSponge<F, R, N> {
fn as_ref(&self) -> &[F] {
Expand Down Expand Up @@ -124,11 +124,11 @@ where
#[allow(unused)]
macro_rules! poseidon_sponge {
($name: ident, $path: tt) => {
pub type $name = crate::hash::sponge::DuplexSponge<
poseidon::PoseidonSponge<$path::Field, { $path::R }, { $path::N }>,
pub type $name = nimue::hash::sponge::DuplexSponge<
crate::PoseidonSponge<$path::Field, { $path::R }, { $path::N }>,
>;

impl Default for poseidon::PoseidonSponge<$path::Field, { $path::R }, { $path::N }> {
impl Default for crate::PoseidonSponge<$path::Field, { $path::R }, { $path::N }> {
fn default() -> Self {
let alpha = $path::ALPHA;
let full_rounds = $path::FULL_ROUNDS;
Expand All @@ -147,5 +147,8 @@ macro_rules! poseidon_sponge {
};
}

#[cfg(feature = "ark-bls12-381")]
pub mod bls12_381;

/// Unit-tests.
#[cfg(test)]
mod tests;
19 changes: 19 additions & 0 deletions nimue-pow/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "nimue-pow"
version = "0.1.0"
edition = "2021"
authors = [
"Giacomo Fenzi <giacomofenzi@outlook.com>",
"Remco Bloemen <remco@wicked.ventures>"
]

[dependencies]
nimue = { path = "../nimue" }
blake3 = "1.5.4"
keccak = { version = "0.1.4"}
bytemuck = "1.17.1"
rayon = { version = "1.10.0", optional = true }

[features]
default = ["parallel"]
parallel = ["dep:rayon"]
4 changes: 2 additions & 2 deletions src/plugins/pow/blake3.rs → nimue-pow/src/blake3.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::PowStrategy;
use blake3;

use {
blake3::{
Expand Down Expand Up @@ -144,8 +145,7 @@ impl Blake3PoW {
#[test]
fn test_pow_blake3() {
use crate::{
plugins::pow::{PoWChallenge, PoWIOPattern},
ByteIOPattern, ByteReader, ByteWriter, IOPattern,
ByteIOPattern, IOPattern, PoWIOPattern, ByteReader, ByteWriter, PoWChallenge
};

const BITS: f64 = 10.0;
Expand Down
3 changes: 1 addition & 2 deletions src/plugins/pow/keccak.rs → nimue-pow/src/keccak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ impl PowStrategy for KeccakPoW {
#[test]
fn test_pow_keccak() {
use crate::{
plugins::pow::{PoWChallenge, PoWIOPattern},
ByteIOPattern, ByteReader, ByteWriter, IOPattern,
ByteIOPattern, ByteReader, ByteWriter, IOPattern, PoWIOPattern, PoWChallenge
};

const BITS: f64 = 10.0;
Expand Down
5 changes: 3 additions & 2 deletions src/plugins/pow/mod.rs → nimue-pow/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// pub mod blake3;
pub mod blake3;
pub mod keccak;

use crate::{
use nimue::{
Arthur, ByteChallenges, ByteIOPattern, ByteReader, ByteWriter, IOPattern, Merlin, ProofError,
ProofResult,
};

/// [`IOPattern`] for proof-of-work challenges.
pub trait PoWIOPattern {
/// Adds a [`PoWChallenge`] to the [`IOPattern`].
Expand Down
64 changes: 64 additions & 0 deletions nimue/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
[package]
name = "nimue"
version = "0.1.0"
authors = ["Michele Orrù <m@orru.net>"]
description = "A library for Fiat-Shamir transcripts."
edition = "2021"
license = "MIT/Apache-2.0"

[dependencies]
zeroize = { version = "1.6.0", features = ["zeroize_derive"] }
rand = { version = "0.8.5", features = ["getrandom"] }
digest = "0.10.7"
generic-array = "0.14.7"
# used as default hasher for the prover
keccak = { version = "0.1.4"}
log = "0.4.20"
# optional dependencies
ark-ff = { version = "0.4.0", optional = true }
ark-ec = { version = "0.4.0", optional = true }
ark-serialize = { version = "0.4.2", optional = true, features = ["std"] }
group = { version = "0.13.0", optional = true }
ark-bls12-381 = { version = "0.4.0", optional = true }
rayon = { version = "1.10.0", optional = true }

[features]
default = ["parallel"]
parallel = ["dep:rayon"]
ark = ["dep:ark-ff", "dep:ark-ec", "dep:ark-serialize"]
group = ["dep:group"]
ark-bls12-381 = ["ark", "dep:ark-bls12-381"]
rayon = ["dep:rayon"]
asm = ["keccak/asm", "keccak/simd"]

[dev-dependencies]
ark-std = "0.4.0"
sha2 = "0.10.7"
blake2 = "0.10.6"
hex = "0.4.3"
# test curve25519 compatibility
curve25519-dalek = { version = "4.0.0", features = ["group"] }
ark-curve25519 = "0.4.0"
# test algebraic hashers
bls12_381 = "0.8.0"
anyhow = { version = "1.0.75", features = ["backtrace"] }
ark-pallas = { version = "0.4.0", features = ["std"] }
pallas = "0.22.0"
pasta_curves = "0.5.1"
ark-vesta = { version = "0.4.0", features = ["std"] }

[package.metadata.docs.rs]
rustdoc-args = ["--html-in-header", "../doc/katex-header.html", "--cfg", "docsrs"]
features = ["ark", "group"]

[[example]]
name = "schnorr"
required-features = ["ark"]

[[example]]
name = "schnorr_algebraic_hash"
required-features = ["ark", "ark-bls112-381"]

[[example]]
name = "bulletproof"
required-features = ["ark"]
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ fn main() {
// type H = nimue::hash::Keccak;
type H = nimue::hash::legacy::DigestBridge<blake2::Blake2s256>;
// type H = nimue::hash::legacy::DigestBridge<sha2::Sha256>;
// type H = nimue::plugins::ark::poseidon::PoseidonHash;
// type H = nimue_poseidon::PoseidonHash;

// Unit type where the hash function works over.
type U = u8;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ where
}
}


/// XXX. duplicate code
impl<'a, H, C, const N: usize> ByteChallenges for Arthur<'a, H, Fp<C, N>>
where
Expand Down
File renamed without changes.
Loading

0 comments on commit 39c201d

Please sign in to comment.