Skip to content

Commit f8e3383

Browse files
Added the support for GR1CS and added rescue hash function (#161)
* rescue works * rescue added without tests * fmt * work * work * work * ci fixed * update dependencies in Cargo.toml * Tweak * patch * work * work * work * Tweak * Tweak * Tweak * Tweak * work * Fix * Fix * Fix * Work * Work * Work * Fixes * Update cargo.toml * Fix * Fix * Fix test * Fmt * r1cs and gr1cs features replced by constraints feature * Rename predicate "XXX" to "Deg5Mul" in constraints * Rescue impl added * use constant RESCUE_PREDICATE * Make RESCUE_PREDICATE public * Test something * Fix arc application in RescueSponge to use correct round index * Update RESCUE_PREDICATE to use correct value "Deg5-Mul" * fmt * Apply suggestions from code review * fixes * Fix CI * Fix patches * Fix no-std * Fix no-std * Fix patch --------- Co-authored-by: Pratyush Mishra <[email protected]> Co-authored-by: Pratyush Mishra <[email protected]>
1 parent 5f41c00 commit f8e3383

File tree

51 files changed

+1086
-194
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1086
-194
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
steps:
1616

1717
- name: Checkout
18-
uses: actions/checkout@v1
18+
uses: actions/checkout@v4
1919
- name: Install Rust
2020
uses: actions-rs/toolchain@v1
2121
with:
@@ -42,7 +42,7 @@ jobs:
4242
- nightly
4343
steps:
4444
- name: Checkout
45-
uses: actions/checkout@v2
45+
uses: actions/checkout@v4
4646

4747
- name: Install Rust (${{ matrix.rust }})
4848
uses: actions-rs/toolchain@v1
@@ -92,7 +92,7 @@ jobs:
9292
runs-on: ubuntu-latest
9393
steps:
9494
- name: Checkout
95-
uses: actions/checkout@v2
95+
uses: actions/checkout@v4
9696

9797
- name: Install Rust
9898
uses: actions-rs/toolchain@v1
@@ -118,5 +118,5 @@ jobs:
118118

119119
- name: crypto-primitives
120120
run: |
121-
cargo build --no-default-features --features=r1cs,merkle_tree,prf,encryption,signature,snark --target aarch64-unknown-none
122-
cargo check --all --no-default-features --features=r1cs,merkle_tree,prf,encryption,signature,snark --target aarch64-unknown-none
121+
cargo build --no-default-features --features=constraints,merkle_tree,prf,encryption,signature,snark --target aarch64-unknown-none
122+
cargo check --all --no-default-features --features=constraints,merkle_tree,prf,encryption,signature,snark --target aarch64-unknown-none

Cargo.toml

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
[workspace]
2-
members = [
3-
"crypto-primitives",
4-
"macros",
5-
]
2+
members = ["crypto-primitives", "macros"]
63
resolver = "2"
74

85
[workspace.package]
@@ -12,7 +9,7 @@ description = "A library of useful cryptographic primitives"
129
homepage = "https://arkworks.rs"
1310
repository = "https://github.com/arkworks-rs/crypto-primitives"
1411
documentation = "https://docs.rs/ark-crypto-primitives/"
15-
keywords = [ "r1cs", "pedersen", "blake2s", "snark", "schnorr" ]
12+
keywords = ["gr1cs", "pedersen", "blake2s", "snark", "schnorr"]
1613
categories = ["cryptography"]
1714
include = ["Cargo.toml", "src", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]
1815
license = "MIT/Apache-2.0"
@@ -43,16 +40,21 @@ incremental = true
4340
debug-assertions = true
4441
debug = true
4542

46-
# [patch.crates-io]
47-
# ark-r1cs-std = { git = "https://github.com/arkworks-rs/r1cs-std/" }
48-
# ark-ff = { git = "https://github.com/arkworks-rs/algebra/" }
49-
# ark-ec = { git = "https://github.com/arkworks-rs/algebra/" }
50-
# ark-poly = { git = "https://github.com/arkworks-rs/algebra/" }
51-
# ark-serialize = { git = "https://github.com/arkworks-rs/algebra/" }
52-
# ark-std = { git = "https://github.com/arkworks-rs/std/" }
53-
54-
# ark-ed-on-bls12-377 = { git = "https://github.com/arkworks-rs/algebra/" }
55-
# ark-ed-on-bls12-381 = { git = "https://github.com/arkworks-rs/algebra/" }
56-
# ark-bls12-377 = { git = "https://github.com/arkworks-rs/algebra/" }
57-
# ark-mnt4-298 = { git = "https://github.com/arkworks-rs/algebra/" }
58-
# ark-mnt6-298 = { git = "https://github.com/arkworks-rs/algebra/" }
43+
[patch.crates-io]
44+
ark-relations = { git = "https://github.com/arkworks-rs/snark" }
45+
ark-snark = { git = "https://github.com/arkworks-rs/snark" }
46+
ark-r1cs-std = { git = "https://github.com/arkworks-rs/r1cs-std" }
47+
# ark-relations = { path = "../snark/relations", default-features = true }
48+
# ark-r1cs-std = { path = "../r1cs-std", default-features = true }
49+
50+
ark-ff = { git = "https://github.com/arkworks-rs/algebra.git" }
51+
ark-serialize = { git = "https://github.com/arkworks-rs/algebra.git" }
52+
ark-ec = { git = "https://github.com/arkworks-rs/algebra.git" }
53+
ark-poly = { git = "https://github.com/arkworks-rs/algebra.git" }
54+
55+
# [patch."https://github.com/arkworks-rs/snark"]
56+
# ark-snark = { path = "../snark/snark", default-features = true }
57+
# ark-relations = { path = "../snark/relations", default-features = true }
58+
59+
# [patch."https://github.com/arkworks-rs/r1cs-std"]
60+
# ark-r1cs-std = { path = "../r1cs-std", default-features = true }

crypto-primitives/Cargo.toml

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,80 +20,89 @@ ark-crypto-primitives-macros = { version = "0.5.0", path = "../macros" }
2020
ark-ff = { version = "0.5.0", default-features = false }
2121
ark-ec = { version = "0.5.0", default-features = false }
2222
ark-std = { version = "0.5.0", default-features = false }
23-
ark-relations = { version = "0.5.0", default-features = false }
2423
ark-serialize = { version = "0.5.0", default-features = false, features = [ "derive" ] }
24+
ark-relations = { git = "https://github.com/arkworks-rs/snark.git", default-features = false }
25+
ark-r1cs-std = { git = "https://github.com/arkworks-rs/r1cs-std", default-features = false, optional = true }
26+
ark-snark = { git = "https://github.com/arkworks-rs/snark.git", default-features = false }
2527

2628
blake2 = { version = "0.10", default-features = false }
2729
sha2 = { version = "0.10", default-features = false }
2830
digest = { version = "0.10", default-features = false }
2931
merlin = { version = "3.0.0", default-features = false, optional = true }
32+
num-bigint = { version = "0.4.4", default-features = false }
3033

31-
ark-r1cs-std = { version = "0.5.0", optional = true, default-features = false }
32-
ark-snark = { version = "0.5.0", default-features = false }
3334

3435
rayon = { version = "1.0", optional = true }
3536
derivative = { version = "2.0", features = ["use_core"] }
3637
tracing = { version = "0.1", default-features = false, features = [ "attributes" ], optional = true }
37-
hashbrown = { version = "0.14", default-features = false, features = ["inline-more", "allocator-api2"], optional = true }
38+
hashbrown = { version = "0.15", default-features = false, features = [ "inline-more" ], optional = true }
3839

3940
[features]
4041
default = ["std"]
41-
std = [ "ark-ff/std", "ark-ec/std", "ark-std/std", "ark-relations/std" ]
42-
print-trace = [ "ark-std/print-trace" ]
43-
parallel = [ "std", "rayon", "ark-ec/parallel", "ark-std/parallel", "ark-ff/parallel" ]
44-
r1cs = [ "ark-r1cs-std", "tracing" ]
45-
crh = [ "sponge" ]
46-
sponge = [ "merlin" ]
47-
commitment = [ "crh" ]
42+
std = ["ark-ff/std", "ark-ec/std", "ark-std/std", "ark-relations/std", "ark-r1cs-std?/std" ]
43+
print-trace = ["ark-std/print-trace"]
44+
parallel = [
45+
"std",
46+
"rayon",
47+
"ark-ec/parallel",
48+
"ark-std/parallel",
49+
"ark-ff/parallel",
50+
"ark-relations/parallel"
51+
]
52+
constraints = ["ark-r1cs-std", "tracing"]
53+
crh = ["sponge"]
54+
sponge = ["merlin"]
55+
commitment = ["crh"]
4856
merkle_tree = ["crh", "hashbrown"]
4957
encryption = []
5058
prf = []
5159
snark = []
5260
signature = []
53-
asm = [ "ark-ff/asm" ]
61+
asm = ["ark-ff/asm"]
5462

5563
[target.'cfg(all(target_has_atomic = "8", target_has_atomic = "16", target_has_atomic = "32", target_has_atomic = "64", target_has_atomic = "ptr"))'.dependencies]
56-
ahash = { version = "0.8", default-features = false}
64+
ahash = { version = "0.8", default-features = false }
5765

5866
[target.'cfg(not(all(target_has_atomic = "8", target_has_atomic = "16", target_has_atomic = "32", target_has_atomic = "64", target_has_atomic = "ptr")))'.dependencies]
5967
fnv = { version = "1.0", default-features = false }
6068

6169
[dev-dependencies]
62-
ark-ed-on-bls12-377 = { version = "0.5.0", default-features = false }
63-
ark-ed-on-bls12-381 = { version = "0.5.0", default-features = false, features = [ "r1cs" ] }
64-
ark-bls12-377 = { version = "0.5.0", default-features = false, features = [ "curve", "r1cs" ] }
65-
ark-mnt4-298 = { version = "0.5.0", default-features = false, features = [ "curve", "r1cs" ] }
66-
ark-mnt6-298 = { version = "0.5.0", default-features = false, features = [ "r1cs" ] }
67-
criterion = { version = "0.5" }
70+
ark-ed-on-bls12-377 = { git = "https://github.com/arkworks-rs/algebra", default-features = false, features = [ "r1cs" ] }
71+
ark-ed-on-bls12-381 = { git = "https://github.com/arkworks-rs/algebra", default-features = false, features = [ "r1cs" ] }
72+
ark-bls12-377 = { git = "https://github.com/arkworks-rs/algebra", default-features = false, features = [ "curve", "r1cs" ] }
73+
ark-bls12-381 = { git = "https://github.com/arkworks-rs/algebra", default-features = false, features = [ "curve" ] }
74+
ark-mnt4-298 = { git = "https://github.com/arkworks-rs/algebra", default-features = false, features = [ "curve", "r1cs" ] }
75+
ark-mnt6-298 = { git = "https://github.com/arkworks-rs/algebra", default-features = false, features = [ "r1cs" ] }
76+
criterion = { version = "0.6" }
6877

6978
################################# Benchmarks ##################################
7079

7180
[[bench]]
7281
name = "pedersen_crh"
7382
path = "benches/crh.rs"
7483
harness = false
75-
required-features = [ "crh" ]
84+
required-features = ["crh"]
7685

7786
[[bench]]
7887
name = "pedersen_comm"
7988
path = "benches/comm.rs"
8089
harness = false
81-
required-features = [ "commitment" ]
90+
required-features = ["commitment"]
8291

8392
[[bench]]
8493
name = "blake2s_prf"
8594
path = "benches/prf.rs"
8695
harness = false
87-
required-features = [ "prf" ]
96+
required-features = ["prf"]
8897

8998
[[bench]]
9099
name = "schnorr_sig"
91100
path = "benches/signature.rs"
92101
harness = false
93-
required-features = [ "signature" ]
102+
required-features = ["signature"]
94103

95104
[[bench]]
96105
name = "merkle_tree"
97106
path = "benches/merkle_tree.rs"
98107
harness = false
99-
required-features = [ "merkle_tree" ]
108+
required-features = ["merkle_tree"]

crypto-primitives/src/commitment/blake2s/constraints.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
};
55
use ark_ff::{Field, PrimeField};
66
use ark_r1cs_std::prelude::*;
7-
use ark_relations::r1cs::{Namespace, SynthesisError};
7+
use ark_relations::gr1cs::{Namespace, SynthesisError};
88
use ark_std::borrow::Borrow;
99

1010
#[derive(Clone)]
@@ -20,7 +20,7 @@ impl<F: PrimeField> CommitmentGadget<blake2s::Commitment, F> for CommGadget {
2020
type ParametersVar = ParametersVar;
2121
type RandomnessVar = RandomnessVar<F>;
2222

23-
#[tracing::instrument(target = "r1cs", skip(input, r))]
23+
#[tracing::instrument(target = "gr1cs", skip(input, r))]
2424
fn commit(
2525
_: &Self::ParametersVar,
2626
input: &[UInt8<F>],
@@ -40,7 +40,7 @@ impl<F: PrimeField> CommitmentGadget<blake2s::Commitment, F> for CommGadget {
4040
}
4141

4242
impl<ConstraintF: Field> AllocVar<(), ConstraintF> for ParametersVar {
43-
#[tracing::instrument(target = "r1cs", skip(_cs, _f))]
43+
#[tracing::instrument(target = "gr1cs", skip(_cs, _f))]
4444
fn new_variable<T: Borrow<()>>(
4545
_cs: impl Into<Namespace<ConstraintF>>,
4646
_f: impl FnOnce() -> Result<T, SynthesisError>,
@@ -51,7 +51,7 @@ impl<ConstraintF: Field> AllocVar<(), ConstraintF> for ParametersVar {
5151
}
5252

5353
impl<ConstraintF: PrimeField> AllocVar<[u8; 32], ConstraintF> for RandomnessVar<ConstraintF> {
54-
#[tracing::instrument(target = "r1cs", skip(cs, f))]
54+
#[tracing::instrument(target = "gr1cs", skip(cs, f))]
5555
fn new_variable<T: Borrow<[u8; 32]>>(
5656
cs: impl Into<Namespace<ConstraintF>>,
5757
f: impl FnOnce() -> Result<T, SynthesisError>,
@@ -77,7 +77,7 @@ mod test {
7777
};
7878
use ark_ed_on_bls12_381::Fq as Fr;
7979
use ark_r1cs_std::prelude::*;
80-
use ark_relations::r1cs::ConstraintSystem;
80+
use ark_relations::gr1cs::ConstraintSystem;
8181
use ark_std::rand::Rng;
8282

8383
#[test]

crypto-primitives/src/commitment/blake2s/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use digest::Digest;
55

66
pub struct Commitment;
77

8-
#[cfg(feature = "r1cs")]
8+
#[cfg(feature = "constraints")]
99
pub mod constraints;
1010

1111
impl CommitmentScheme for Commitment {

crypto-primitives/src/commitment/constraints.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use crate::commitment::CommitmentScheme;
22
use ark_ff::Field;
33
use ark_r1cs_std::prelude::*;
4-
use ark_relations::r1cs::SynthesisError;
4+
use ark_relations::gr1cs::SynthesisError;
55
use ark_std::fmt::Debug;
66

77
pub trait CommitmentGadget<C: CommitmentScheme, ConstraintF: Field> {
88
type OutputVar: EqGadget<ConstraintF>
99
+ ToBytesGadget<ConstraintF>
1010
+ AllocVar<C::Output, ConstraintF>
11-
+ R1CSVar<ConstraintF>
11+
+ GR1CSVar<ConstraintF>
1212
+ Clone
1313
+ Sized
1414
+ Debug;

crypto-primitives/src/commitment/injective_map/constraints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use ark_r1cs_std::{
1212
groups::{CurveVar, GroupOpsBounds},
1313
uint8::UInt8,
1414
};
15-
use ark_relations::r1cs::SynthesisError;
15+
use ark_relations::gr1cs::SynthesisError;
1616
use ark_std::marker::PhantomData;
1717

1818
type ConstraintF<C> = <<C as CurveGroup>::BaseField as Field>::BasePrimeField;

crypto-primitives/src/commitment/injective_map/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
use ark_ec::CurveGroup;
77
use ark_std::{marker::PhantomData, rand::Rng};
88

9-
#[cfg(feature = "r1cs")]
9+
#[cfg(feature = "constraints")]
1010
pub mod constraints;
1111

1212
pub struct PedersenCommCompressor<C: CurveGroup, I: InjectiveMap<C>, W: pedersen::Window> {

crypto-primitives/src/commitment/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ pub mod blake2s;
77
pub mod injective_map;
88
pub mod pedersen;
99

10-
#[cfg(feature = "r1cs")]
10+
#[cfg(feature = "constraints")]
1111
pub mod constraints;
12-
#[cfg(feature = "r1cs")]
12+
#[cfg(feature = "constraints")]
1313
pub use constraints::*;
1414

1515
pub trait CommitmentScheme {

crypto-primitives/src/commitment/pedersen/constraints.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use ark_ff::{
88
Zero,
99
};
1010
use ark_r1cs_std::prelude::*;
11-
use ark_relations::r1cs::{Namespace, SynthesisError};
11+
use ark_relations::gr1cs::{Namespace, SynthesisError};
1212
use ark_serialize::CanonicalSerialize;
1313
use ark_std::{borrow::Borrow, iter, marker::PhantomData};
1414

@@ -151,7 +151,7 @@ mod test {
151151
crh::pedersen,
152152
};
153153
use ark_r1cs_std::prelude::*;
154-
use ark_relations::r1cs::ConstraintSystem;
154+
use ark_relations::gr1cs::ConstraintSystem;
155155

156156
/// Checks that the primitive Pedersen commitment matches the gadget version
157157
#[test]
@@ -199,7 +199,6 @@ mod test {
199199
let result_var =
200200
TestCOMMGadget::commit(&parameters_var, &input_var, &randomness_var).unwrap();
201201

202-
let primitive_result = primitive_result;
203202
assert_eq!(primitive_result, result_var.value().unwrap());
204203
assert!(cs.is_satisfied().unwrap());
205204
}

0 commit comments

Comments
 (0)