Skip to content

Commit

Permalink
Absorb derive (#135)
Browse files Browse the repository at this point in the history
* move crypto-primitives to its own crate dir

* create new crate "macros"

* Macro derive working with tests

* rename to Absorb, clean up and format

* add test for struct with no named fields, panics now

* implement `Absorb` macro for structs with unnamed fields too

* inline variable

* re-export Absorb derive

* workspace inheritance

* minimize the diff

* add licenses to each crate

* change generic type's name to `FieldType`. `F` is often used in caller

* add tests with generic type `F`

* replace licenses with symlinks
  • Loading branch information
mmagician authored Feb 9, 2024
1 parent 38e6e65 commit ccf9d95
Show file tree
Hide file tree
Showing 64 changed files with 294 additions and 80 deletions.
87 changes: 8 additions & 79 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
[package]
name = "ark-crypto-primitives"
[workspace]
members = [
"crypto-primitives",
"macros",
]
resolver = "2"

[workspace.package]
version = "0.4.0"
authors = [ "arkworks contributors" ]
description = "A library of useful cryptographic primitives"
Expand All @@ -11,51 +17,6 @@ categories = ["cryptography"]
include = ["Cargo.toml", "src", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]
license = "MIT/Apache-2.0"
edition = "2021"
resolver = "2"

################################# Dependencies ################################

[dependencies]
ark-ff = { version = "^0.4.0", default-features = false }
ark-ec = { version = "^0.4.0", default-features = false }
ark-std = { version = "^0.4.0", default-features = false }
ark-relations = { version = "^0.4.0", default-features = false }
ark-serialize = { version = "^0.4.0", default-features = false, features = [ "derive" ] }

blake2 = { version = "0.10", default-features = false }
sha2 = { version = "0.10", default-features = false }
digest = { version = "0.10", default-features = false }

ark-r1cs-std = { version = "^0.4.0", optional = true, default-features = false }
ark-snark = { version = "^0.4.0", default-features = false }

rayon = { version = "1.0", optional = true }
derivative = { version = "2.0", features = ["use_core"] }
tracing = { version = "0.1", default-features = false, features = [ "attributes" ], optional = true }

[features]
default = ["std"]
std = [ "ark-ff/std", "ark-ec/std", "ark-std/std", "ark-relations/std" ]
print-trace = [ "ark-std/print-trace" ]
parallel = [ "std", "rayon", "ark-ec/parallel", "ark-std/parallel", "ark-ff/parallel" ]
r1cs = [ "ark-r1cs-std", "tracing" ]
crh = [ "sponge" ]
sponge = []
commitment = ["crh"]
merkle_tree = ["crh"]
encryption = []
prf = []
snark = []
signature = []

[dev-dependencies]
ark-ed-on-bls12-377 = { version = "^0.4.0", default-features = false }
ark-ed-on-bls12-381 = { version = "^0.4.0", default-features = false, features = [ "r1cs" ] }
ark-bls12-377 = { version = "^0.4.0", default-features = false, features = [ "curve", "r1cs" ] }
ark-mnt4-298 = { version = "^0.4.0", default-features = false, features = [ "curve", "r1cs" ] }
ark-mnt6-298 = { version = "^0.4.0", default-features = false, features = [ "r1cs" ] }
criterion = { version = "0.4" }


[profile.release]
opt-level = 3
Expand All @@ -82,38 +43,6 @@ incremental = true
debug-assertions = true
debug = true

################################# Benchmarks ##################################

[[bench]]
name = "pedersen_crh"
path = "benches/crh.rs"
harness = false
required-features = [ "crh" ]

[[bench]]
name = "pedersen_comm"
path = "benches/comm.rs"
harness = false
required-features = [ "commitment" ]

[[bench]]
name = "blake2s_prf"
path = "benches/prf.rs"
harness = false
required-features = [ "prf" ]

[[bench]]
name = "schnorr_sig"
path = "benches/signature.rs"
harness = false
required-features = [ "signature" ]

[[bench]]
name = "merkle_tree"
path = "benches/merkle_tree.rs"
harness = false
required-features = [ "merkle_tree" ]

[patch.crates-io]
ark-r1cs-std = { git = "https://github.com/arkworks-rs/r1cs-std/" }
ark-ff = { git = "https://github.com/arkworks-rs/algebra/" }
Expand Down
90 changes: 90 additions & 0 deletions crypto-primitives/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
[package]
name = "ark-crypto-primitives"
description.workspace = true
documentation.workspace = true
keywords.workspace = true
version.workspace = true
authors.workspace = true
homepage.workspace = true
repository.workspace = true
categories.workspace = true
include.workspace = true
license.workspace = true
edition.workspace = true

################################# Dependencies ################################

[dependencies]
ark-crypto-primitives-macros = { version = "^0.4.0", path = "../macros" }

ark-ff = { version = "^0.4.0", default-features = false }
ark-ec = { version = "^0.4.0", default-features = false }
ark-std = { version = "^0.4.0", default-features = false }
ark-relations = { version = "^0.4.0", default-features = false }
ark-serialize = { version = "^0.4.0", default-features = false, features = [ "derive" ] }

blake2 = { version = "0.10", default-features = false }
sha2 = { version = "0.10", default-features = false }
digest = { version = "0.10", default-features = false }

ark-r1cs-std = { version = "^0.4.0", optional = true, default-features = false }
ark-snark = { version = "^0.4.0", default-features = false }

rayon = { version = "1.0", optional = true }
derivative = { version = "2.0", features = ["use_core"] }
tracing = { version = "0.1", default-features = false, features = [ "attributes" ], optional = true }

[features]
default = ["std"]
std = [ "ark-ff/std", "ark-ec/std", "ark-std/std", "ark-relations/std" ]
print-trace = [ "ark-std/print-trace" ]
parallel = [ "std", "rayon", "ark-ec/parallel", "ark-std/parallel", "ark-ff/parallel" ]
r1cs = [ "ark-r1cs-std", "tracing" ]
crh = [ "sponge" ]
sponge = []
commitment = ["crh"]
merkle_tree = ["crh"]
encryption = []
prf = []
snark = []
signature = []

[dev-dependencies]
ark-ed-on-bls12-377 = { version = "^0.4.0", default-features = false }
ark-ed-on-bls12-381 = { version = "^0.4.0", default-features = false, features = [ "r1cs" ] }
ark-bls12-377 = { version = "^0.4.0", default-features = false, features = [ "curve", "r1cs" ] }
ark-mnt4-298 = { version = "^0.4.0", default-features = false, features = [ "curve", "r1cs" ] }
ark-mnt6-298 = { version = "^0.4.0", default-features = false, features = [ "r1cs" ] }
criterion = { version = "0.4" }

################################# Benchmarks ##################################

[[bench]]
name = "pedersen_crh"
path = "benches/crh.rs"
harness = false
required-features = [ "crh" ]

[[bench]]
name = "pedersen_comm"
path = "benches/comm.rs"
harness = false
required-features = [ "commitment" ]

[[bench]]
name = "blake2s_prf"
path = "benches/prf.rs"
harness = false
required-features = [ "prf" ]

[[bench]]
name = "schnorr_sig"
path = "benches/signature.rs"
harness = false
required-features = [ "signature" ]

[[bench]]
name = "merkle_tree"
path = "benches/merkle_tree.rs"
harness = false
required-features = [ "merkle_tree" ]
1 change: 1 addition & 0 deletions crypto-primitives/LICENSE-APACHE
1 change: 1 addition & 0 deletions crypto-primitives/LICENSE-MIT
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.
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.
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.
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.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
76 changes: 75 additions & 1 deletion src/sponge/absorb.rs → crypto-primitives/src/sponge/absorb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use ark_ff::{BigInteger, Field, PrimeField, ToConstraintField};
use ark_serialize::CanonicalSerialize;
use ark_std::vec::Vec;

pub use ark_crypto_primitives_macros::*;

/// An interface for objects that can be absorbed by a `CryptographicSponge`.
pub trait Absorb {
/// Converts the object into a list of bytes that can be absorbed by a `CryptographicSponge`.
Expand Down Expand Up @@ -370,8 +372,11 @@ macro_rules! collect_sponge_field_elements {

#[cfg(test)]
mod tests {
use crate::sponge::field_cast;
use crate::sponge::poseidon::{poseidon_parameters_for_test, PoseidonSponge};
use crate::sponge::test::Fr;
use crate::sponge::Absorb;
use crate::sponge::{field_cast, CryptographicSponge};
use ark_ff::PrimeField;
use ark_std::{test_rng, vec::Vec, UniformRand};

#[test]
Expand All @@ -382,4 +387,73 @@ mod tests {
field_cast::<_, Fr>(&expected, &mut actual).unwrap();
assert_eq!(actual, expected);
}

#[derive(Absorb)]
struct SubStruct {
a: u8,
b: u16,
}

#[derive(Absorb)]
struct StructWithGeneric<F: PrimeField + Absorb>(F);

#[derive(Absorb)]
struct StructWithNoNamedFields(u16);

#[derive(Absorb)]
struct TestStruct {
a: u8,
b: u16,
c: u32,
d: u64,
e: u128,
f: Fr,
g: SubStruct,
h: StructWithNoNamedFields,
i: StructWithGeneric<Fr>,
}

#[test]
fn test_absorb_derive() {
let a = TestStruct {
a: 1,
b: 2,
c: 3,
d: 4,
e: 5,
f: Fr::from(6),
g: SubStruct { a: 7, b: 8 },
h: StructWithNoNamedFields(9),
i: StructWithGeneric(Fr::from(10)),
};

let sponge_param = poseidon_parameters_for_test();
let mut sponge = PoseidonSponge::<Fr>::new(&sponge_param);

sponge.absorb(&a);
let out_derived = sponge.squeeze_bytes(32);

let mut sponge = PoseidonSponge::<Fr>::new(&sponge_param);
sponge.absorb(&a.a);
sponge.absorb(&a.b);
sponge.absorb(&a.c);
sponge.absorb(&a.d);
sponge.absorb(&a.e);
// we forgot to absorb some fields, assert that output is different
let out_manual = sponge.squeeze_bytes(32);
assert_ne!(out_derived, out_manual);

let mut sponge = PoseidonSponge::<Fr>::new(&sponge_param);
sponge.absorb(&a.a);
sponge.absorb(&a.b);
sponge.absorb(&a.c);
sponge.absorb(&a.d);
sponge.absorb(&a.e);
sponge.absorb(&a.f);
sponge.absorb(&a.g);
sponge.absorb(&a.h);
sponge.absorb(&a.i);
let out_manual = sponge.squeeze_bytes(32);
assert_eq!(out_derived, out_manual);
}
}
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 @@ -13,6 +13,8 @@ use ark_std::vec::Vec;
pub mod constraints;
#[cfg(test)]
mod tests;
#[cfg(test)]
pub(crate) use tests::poseidon_parameters_for_test;

/// default parameters traits for Poseidon
pub mod traits;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
21 changes: 21 additions & 0 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "ark-crypto-primitives-macros"
description.workspace = true
documentation.workspace = true
version.workspace = true
authors.workspace = true
homepage.workspace = true
repository.workspace = true
categories.workspace = true
include.workspace = true
license.workspace = true
edition.workspace = true
publish = false

[dependencies]
quote = { version = "1.0" }
syn = { version = "2.0" }
proc-macro2 = { version = "1.0" }

[lib]
proc-macro = true
1 change: 1 addition & 0 deletions macros/LICENSE-APACHE
1 change: 1 addition & 0 deletions macros/LICENSE-MIT
Loading

0 comments on commit ccf9d95

Please sign in to comment.