Skip to content
This repository has been archived by the owner on Aug 15, 2024. It is now read-only.

Crates.io 0.5.0 release #17

Merged
merged 34 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
305c90e
poseidon2 implemented
olesHolem Jul 17, 2023
a2b3013
small fix
olesHolem Jul 17, 2023
0bdba2d
transcript implemented
olesHolem Jul 17, 2023
47e7510
better absorbtion
olesHolem Jul 18, 2023
e18b0e1
tests
olesHolem Jul 18, 2023
ad46c57
circuit implementation
olesHolem Jul 19, 2023
067b1d3
fixes
olesHolem Jul 23, 2023
390eac2
pow_runner implemented
olesHolem Jul 24, 2023
20a97b7
changes for snark wrapper
olesHolem Aug 16, 2023
745189b
fix build by using logging crate (#6)
akash-chandrakar Aug 28, 2023
4ad3ab7
update boojum
olesHolem Aug 30, 2023
22ffac5
using custom gate
olesHolem Sep 13, 2023
5731618
poseidon2 configs changed
olesHolem Sep 13, 2023
70b27eb
Change version of log to 0.4.19
StanislavBreadless Sep 19, 2023
b88c6b8
Merge pull request #7 from matter-labs/StanislavBreadless-patch-1
shamatar Sep 19, 2023
26c015e
Update Cargo.toml
shahar4 Sep 22, 2023
4e236a5
Merge pull request #8 from matter-labs/sk-update-to-public-boojum
shahar4 Sep 22, 2023
d690325
Syncs common workflows into poseidon2 (#12)
yorik Sep 26, 2023
754ac4c
fixed poseidon2 hash
olesHolem Sep 26, 2023
128ae32
Merge remote-tracking branch 'origin/poseidon2' into poseidon2_fix
olesHolem Sep 26, 2023
b4381f5
use queue in transcript
olesHolem Sep 27, 2023
c9e09ee
change compiler version and ignore tests
olesHolem Sep 28, 2023
8cbccea
ci fix
olesHolem Sep 28, 2023
5ddc55b
fmt check turned off
olesHolem Sep 28, 2023
09b96e7
Merge pull request #13 from matter-labs/poseidon2_fix
olesHolem Sep 28, 2023
7cf3752
done
olesHolem Oct 12, 2023
c4a7884
Merge pull request #14 from matter-labs/reexports
olesHolem Oct 16, 2023
2e5e8af
add normalization
shamatar Dec 20, 2023
dc7ad03
optimized tree hasher
olesHolem Feb 12, 2024
126937e
better parameters saving
olesHolem Feb 13, 2024
ecc021a
Prepare 0.5.0 release
popzxc Jul 5, 2024
1ec2989
Merge branch 'master' into crates.io-0.5.0-branch
popzxc Aug 5, 2024
4ddbf6d
Uncomment fmt workflow
popzxc Aug 5, 2024
a8d6e69
Merge artifacts
popzxc Aug 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ jobs:
with:
# Remove default `-D warnings`.
rustflags: ""
- name: Setup rust
run: |
rustup set profile minimal
rustup toolchain install nightly-2023-08-23
rustup default nightly-2023-08-23
- run: cargo build --verbose
- run: cargo test --verbose --all

Expand Down
14 changes: 9 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
name = "rescue_poseidon"
description = "Sponge construction based Algebraic Hash Functions"
keywords = ["poseidon", "rescue", "sponge"]
license = "MIT OR Apache-2.0"
version = "0.4.1"
version = "0.5.0"
authors = ["Sait Imamoglu <si@matterlabs.dev>"]
edition = "2021"
license = "MIT OR Apache-2.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
derivative = "2"

franklin_crypto = {package = "franklin-crypto", features = ["plonk"], version = "=0.1.0" }
typemap_rev = "0.3"
lazy_static = "1.4"
franklin_crypto = {package = "franklin-crypto", features = ["plonk"], version = "=0.2.0" }
num-bigint = "0.3"
num-integer = "0.1"
num-iter = "0.1"
Expand All @@ -24,9 +27,10 @@ addchain = "0.2"
smallvec = "1.9"
blake2 = "0.10"
rayon = {version = "1.5", optional = true}
lazy_static = {version = "1.4", optional = true}
# lazy_static = {version = "1.4", optional = true}
futures = {version = "0.3", features = ["executor", "thread-pool"], optional = true}
arrayvec = "0.7"
log = "0.4.19"

[dev-dependencies]
# reference poseidon does not uses specialization so some tests will fail.
Expand All @@ -49,7 +53,7 @@ hex = "0.4"
[features]
default = []
rayon = ["dep:rayon"]
futures = ["dep:lazy_static", "dep:futures"]
futures = ["dep:futures"]

[[bench]]
name = "benches"
Expand Down
1 change: 1 addition & 0 deletions rust-toolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nightly-2024-05-07
1 change: 1 addition & 0 deletions src/circuit/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
pub(crate) mod sponge;

Check warning on line 1 in src/circuit/mod.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/rescue-poseidon/rescue-poseidon/src/circuit/mod.rs
pub(crate) mod poseidon;
pub mod poseidon2;
pub(crate) mod rescue;
pub(crate) mod rescue_prime;

Check warning on line 5 in src/circuit/mod.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/rescue-poseidon/rescue-poseidon/src/circuit/mod.rs
mod sbox;
mod matrix;
#[cfg(test)]
Expand Down
117 changes: 117 additions & 0 deletions src/circuit/poseidon2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
use super::sbox::sbox;

Check warning on line 1 in src/circuit/poseidon2.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/rescue-poseidon/rescue-poseidon/src/circuit/poseidon2.rs
use super::sponge::circuit_generic_hash_num;
use super::matrix::{matrix_vector_product, mul_by_sparse_matrix};

Check warning on line 3 in src/circuit/poseidon2.rs

View workflow job for this annotation

GitHub Actions / cargo build and test

unused import: `mul_by_sparse_matrix`

Check warning on line 3 in src/circuit/poseidon2.rs

View workflow job for this annotation

GitHub Actions / cargo build and test

unused import: `mul_by_sparse_matrix`
use crate::{DomainStrategy, poseidon::params::PoseidonParams};

Check warning on line 4 in src/circuit/poseidon2.rs

View workflow job for this annotation

GitHub Actions / cargo build and test

unused import: `poseidon::params::PoseidonParams`

Check warning on line 4 in src/circuit/poseidon2.rs

View workflow job for this annotation

GitHub Actions / cargo build and test

unused import: `poseidon::params::PoseidonParams`
use crate::poseidon2::Poseidon2Params;
use crate::traits::{HashFamily, HashParams};

Check warning on line 6 in src/circuit/poseidon2.rs

View workflow job for this annotation

GitHub Actions / cargo build and test

unused import: `HashFamily`

Check warning on line 6 in src/circuit/poseidon2.rs

View workflow job for this annotation

GitHub Actions / cargo build and test

unused import: `HashFamily`
use franklin_crypto::bellman::plonk::better_better_cs::cs::ConstraintSystem;
use franklin_crypto::bellman::{Field, SynthesisError};
use franklin_crypto::{
bellman::Engine,
plonk::circuit::{allocated_num::Num, linear_combination::LinearCombination},
};

/// Receives inputs whose length `known` prior(fixed-length).
/// Also uses custom domain strategy which basically sets value of capacity element to
/// length of input and applies a padding rule which makes input size equals to multiple of
/// rate parameter.
/// Uses pre-defined state-width=3 and rate=2.
pub fn circuit_poseidon2_hash<E: Engine, CS: ConstraintSystem<E>, const L: usize>(
cs: &mut CS,
input: &[Num<E>; L],
domain_strategy: Option<DomainStrategy>,
) -> Result<[Num<E>; 2], SynthesisError> {
const WIDTH: usize = 3;
const RATE: usize = 2;
let params = Poseidon2Params::<E, RATE, WIDTH>::default();
circuit_generic_hash_num(cs, input, &params, domain_strategy)
}

pub fn circuit_poseidon2_round_function<
E: Engine,
CS: ConstraintSystem<E>,
const RATE: usize,
const WIDTH: usize,
>(
cs: &mut CS,
params: &Poseidon2Params<E, RATE, WIDTH>,
state: &mut [LinearCombination<E>; WIDTH],
) -> Result<(), SynthesisError> {
assert!(params.number_of_full_rounds() % 2 == 0);

let half_of_full_rounds = params.number_of_full_rounds() / 2;

// Linear layer at beginning
matrix_vector_product(&params.mds_external_matrix, state)?;

// first full rounds
for round in 0..half_of_full_rounds {
let round_constants = &params.round_constants[round];

// add round constatnts
for (s, c) in state.iter_mut().zip(round_constants.iter()) {
s.add_assign_constant(*c);
}
// non linear sbox
sbox(
cs,
params.alpha(),
state,
Some(0..WIDTH),
params.custom_gate(),
)?;

// mul state by mds
matrix_vector_product(&params.mds_external_matrix, state)?;
}

let mut diag_internal_matrix_decreased = params.diag_internal_matrix.clone();
for coeff in diag_internal_matrix_decreased.iter_mut() {
coeff.sub_assign(&E::Fr::one());
}

for round in half_of_full_rounds..(params.partial_rounds + half_of_full_rounds) {
// add round constatnt
let round_constant = params.round_constants[round][0];
state[0].add_assign_constant(round_constant);

// non linear sbox
sbox(cs, params.alpha(), state, Some(0..1), params.custom_gate())?;

// mul state by internal matrix
let mut sum = state[0].clone();
for s in state.iter().skip(1) {
sum.add_assign(s);
}

for (s, coeff) in state.iter_mut().zip(diag_internal_matrix_decreased.iter()) {
s.scale(coeff);
s.add_assign(&sum);
}
}

// second full round
for round in (params.number_of_partial_rounds() + half_of_full_rounds)
..(params.number_of_partial_rounds() + params.number_of_full_rounds())
{
let round_constants = &params.round_constants[round];

// add round constatnts
for (s, c) in state.iter_mut().zip(round_constants.iter()) {
s.add_assign_constant(*c);
}
// non linear sbox
sbox(
cs,
params.alpha(),
state,
Some(0..WIDTH),
params.custom_gate(),
)?;

// mul state by mds
matrix_vector_product(&params.mds_external_matrix, state)?;
}

Ok(())
}
16 changes: 15 additions & 1 deletion src/circuit/sponge.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
common::domain_strategy::DomainStrategy,
traits::{HashFamily, HashParams},
traits::{HashFamily, HashParams}, poseidon2::Poseidon2Params,

Check warning on line 3 in src/circuit/sponge.rs

View workflow job for this annotation

GitHub Actions / cargo build and test

unused import: `poseidon2::Poseidon2Params`

Check warning on line 3 in src/circuit/sponge.rs

View workflow job for this annotation

GitHub Actions / cargo build and test

unused import: `poseidon2::Poseidon2Params`
};
use franklin_crypto::{
bellman::plonk::better_better_cs::cs::ConstraintSystem, plonk::circuit::allocated_num::Num,
Expand Down Expand Up @@ -54,7 +54,7 @@
pub struct CircuitGenericSponge<E: Engine, const RATE: usize, const WIDTH: usize> {
state: [LinearCombination<E>; WIDTH],
mode: SpongeMode<E, RATE>,
domain_strategy: DomainStrategy,

Check warning on line 57 in src/circuit/sponge.rs

View workflow job for this annotation

GitHub Actions / cargo build and test

field `domain_strategy` is never read
}

impl<'a, E: Engine, const RATE: usize, const WIDTH: usize> CircuitGenericSponge<E, RATE, WIDTH> {
Expand Down Expand Up @@ -331,6 +331,13 @@
HashFamily::RescuePrime => {
super::rescue_prime::gadget_rescue_prime_round_function(cs, params, state)
}
HashFamily::Poseidon2 => {
super::poseidon2::circuit_poseidon2_round_function(
cs,
params.try_to_poseidon2_params().unwrap(),
state
)
}
}
}

Expand All @@ -356,6 +363,13 @@
HashFamily::RescuePrime => {
super::rescue_prime::gadget_rescue_prime_round_function(cs, params, state)
}
HashFamily::Poseidon2 => {
super::poseidon2::circuit_poseidon2_round_function(
cs,
params.try_to_poseidon2_params().unwrap(),
state
)
}
};

let _ = tmp?;
Expand Down
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
mod circuit;
#![feature(allocator_api)]

pub mod circuit;
#[allow(dead_code)]
mod common;
mod sponge;
pub mod poseidon;
pub mod poseidon2;
pub mod rescue;
pub mod rescue_prime;
#[cfg(test)]
Expand All @@ -23,6 +26,7 @@ pub use rescue::{params::RescueParams, rescue_hash};
pub use rescue_prime::{params::RescuePrimeParams, rescue_prime_hash};
pub use common::domain_strategy::DomainStrategy;

pub extern crate franklin_crypto;

pub trait BigArraySerde<'de>: Sized {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
Expand Down
11 changes: 11 additions & 0 deletions src/poseidon2/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pub mod params;
pub mod poseidon2;
pub mod sponge;
pub mod transcript;
pub mod pow_runner;
#[cfg(test)]
mod tests;

pub use self::sponge::*;
pub use self::params::Poseidon2Params;
pub use self::poseidon2::*;
Loading
Loading