From 9ef17537e6a36952739d3ea909aeaf2df992988a Mon Sep 17 00:00:00 2001 From: Hossein Moghaddas Date: Tue, 9 Apr 2024 09:27:20 +0200 Subject: [PATCH] Fix CI and build for each feature (#142) * Fix build for different features with `no-std` * Fix for constraints of each feature * Fix CI * Do a `cargo fmt` * patch std needed after https://github.com/arkworks-rs/algebra/pull/811 Need to add Sync trait bounds on a few types --------- Co-authored-by: mmagician --- .github/workflows/ci.yml | 4 ++-- Cargo.toml | 1 + crypto-primitives/Cargo.toml | 3 ++- crypto-primitives/src/commitment/pedersen/mod.rs | 2 ++ crypto-primitives/src/crh/bowe_hopwood/constraints.rs | 2 ++ crypto-primitives/src/crh/bowe_hopwood/mod.rs | 2 ++ crypto-primitives/src/crh/injective_map/mod.rs | 2 ++ crypto-primitives/src/crh/mod.rs | 4 ++-- crypto-primitives/src/crh/pedersen/constraints.rs | 2 ++ crypto-primitives/src/crh/pedersen/mod.rs | 2 ++ crypto-primitives/src/crh/poseidon/constraints.rs | 2 ++ crypto-primitives/src/crh/sha256/constraints.rs | 2 ++ crypto-primitives/src/crh/sha256/mod.rs | 2 ++ .../src/encryption/elgamal/constraints.rs | 2 ++ crypto-primitives/src/merkle_tree/constraints.rs | 2 ++ crypto-primitives/src/merkle_tree/mod.rs | 10 ++++++++-- crypto-primitives/src/prf/blake2s/constraints.rs | 2 ++ crypto-primitives/src/prf/blake2s/mod.rs | 2 ++ crypto-primitives/src/prf/constraints.rs | 2 ++ crypto-primitives/src/signature/schnorr/constraints.rs | 4 +++- crypto-primitives/src/signature/schnorr/mod.rs | 2 ++ crypto-primitives/src/snark/constraints.rs | 2 ++ crypto-primitives/src/sponge/absorb.rs | 2 ++ crypto-primitives/src/sponge/constraints/absorb.rs | 2 ++ crypto-primitives/src/sponge/constraints/mod.rs | 2 ++ crypto-primitives/src/sponge/mod.rs | 3 ++- crypto-primitives/src/sponge/poseidon/constraints.rs | 2 ++ crypto-primitives/src/sponge/poseidon/grain_lfsr.rs | 2 ++ crypto-primitives/src/sponge/poseidon/mod.rs | 2 ++ crypto-primitives/src/sponge/poseidon/traits.rs | 2 ++ 30 files changed, 66 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index deec8b0e..32c9e9b7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -118,5 +118,5 @@ jobs: - name: crypto-primitives run: | - cargo build --no-default-features --features=r1cs --target aarch64-unknown-none - cargo check --examples --no-default-features --features=r1cs --target aarch64-unknown-none + cargo build --no-default-features --features=r1cs,merkle_tree,prf,encryption,signature,snark --target aarch64-unknown-none + cargo check --all --no-default-features --features=r1cs,merkle_tree,prf,encryption,signature,snark --target aarch64-unknown-none diff --git a/Cargo.toml b/Cargo.toml index 086db375..afe68c49 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,6 +49,7 @@ ark-ff = { git = "https://github.com/arkworks-rs/algebra/" } ark-ec = { git = "https://github.com/arkworks-rs/algebra/" } ark-poly = { git = "https://github.com/arkworks-rs/algebra/" } ark-serialize = { git = "https://github.com/arkworks-rs/algebra/" } +ark-std = { git = "https://github.com/arkworks-rs/std/" } ark-ed-on-bls12-377 = { git = "https://github.com/arkworks-rs/algebra/" } ark-ed-on-bls12-381 = { git = "https://github.com/arkworks-rs/algebra/" } diff --git a/crypto-primitives/Cargo.toml b/crypto-primitives/Cargo.toml index 3dc2baf5..7b31c5e5 100644 --- a/crypto-primitives/Cargo.toml +++ b/crypto-primitives/Cargo.toml @@ -33,6 +33,7 @@ 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 } +hashbrown = { version = "^0.14", default-features = false, optional = true } [features] default = ["std"] @@ -43,7 +44,7 @@ r1cs = [ "ark-r1cs-std", "tracing" ] crh = [ "sponge" ] sponge = [] commitment = ["crh"] -merkle_tree = ["crh"] +merkle_tree = ["crh", "hashbrown"] encryption = [] prf = [] snark = [] diff --git a/crypto-primitives/src/commitment/pedersen/mod.rs b/crypto-primitives/src/commitment/pedersen/mod.rs index cfcdab74..e1aa0d4b 100644 --- a/crypto-primitives/src/commitment/pedersen/mod.rs +++ b/crypto-primitives/src/commitment/pedersen/mod.rs @@ -4,6 +4,8 @@ use ark_ff::{BitIteratorLE, Field, PrimeField, ToConstraintField}; use ark_serialize::CanonicalSerialize; use ark_std::marker::PhantomData; use ark_std::rand::Rng; +#[cfg(not(feature = "std"))] +use ark_std::vec::Vec; use ark_std::UniformRand; use super::CommitmentScheme; diff --git a/crypto-primitives/src/crh/bowe_hopwood/constraints.rs b/crypto-primitives/src/crh/bowe_hopwood/constraints.rs index e5eef789..a9bf49de 100644 --- a/crypto-primitives/src/crh/bowe_hopwood/constraints.rs +++ b/crypto-primitives/src/crh/bowe_hopwood/constraints.rs @@ -10,6 +10,8 @@ use crate::crh::{ use ark_ff::Field; use ark_r1cs_std::{groups::curves::twisted_edwards::AffineVar, prelude::*}; use ark_relations::r1cs::{Namespace, SynthesisError}; +#[cfg(not(feature = "std"))] +use ark_std::vec::Vec; use crate::crh::bowe_hopwood::{TwoToOneCRH, CRH}; diff --git a/crypto-primitives/src/crh/bowe_hopwood/mod.rs b/crypto-primitives/src/crh/bowe_hopwood/mod.rs index fb2bf0ac..d284c3ef 100644 --- a/crypto-primitives/src/crh/bowe_hopwood/mod.rs +++ b/crypto-primitives/src/crh/bowe_hopwood/mod.rs @@ -21,6 +21,8 @@ use ark_ff::fields::PrimeField; use ark_serialize::{CanonicalDeserialize, CanonicalSerialize}; use ark_std::borrow::Borrow; use ark_std::cfg_chunks; +#[cfg(not(feature = "std"))] +use ark_std::vec::Vec; use ark_std::UniformRand; #[cfg(feature = "r1cs")] diff --git a/crypto-primitives/src/crh/injective_map/mod.rs b/crypto-primitives/src/crh/injective_map/mod.rs index 4927852a..289a9006 100644 --- a/crypto-primitives/src/crh/injective_map/mod.rs +++ b/crypto-primitives/src/crh/injective_map/mod.rs @@ -1,5 +1,7 @@ use crate::Error; use ark_std::rand::Rng; +#[cfg(not(feature = "std"))] +use ark_std::vec::Vec; use ark_std::{fmt::Debug, hash::Hash, marker::PhantomData}; use super::{pedersen, CRHScheme, TwoToOneCRHScheme}; diff --git a/crypto-primitives/src/crh/mod.rs b/crypto-primitives/src/crh/mod.rs index e66be16e..08cb4a6e 100644 --- a/crypto-primitives/src/crh/mod.rs +++ b/crypto-primitives/src/crh/mod.rs @@ -29,7 +29,7 @@ pub trait CRHScheme { + Default + CanonicalSerialize + CanonicalDeserialize; - type Parameters: Clone + CanonicalSerialize + CanonicalDeserialize; + type Parameters: Clone + CanonicalSerialize + CanonicalDeserialize + Sync; fn setup(r: &mut R) -> Result; fn evaluate>( @@ -50,7 +50,7 @@ pub trait TwoToOneCRHScheme { + Default + CanonicalSerialize + CanonicalDeserialize; - type Parameters: Clone + CanonicalSerialize + CanonicalDeserialize; + type Parameters: Clone + CanonicalSerialize + CanonicalDeserialize + Sync; fn setup(r: &mut R) -> Result; diff --git a/crypto-primitives/src/crh/pedersen/constraints.rs b/crypto-primitives/src/crh/pedersen/constraints.rs index 3f64e755..0d1e72ff 100644 --- a/crypto-primitives/src/crh/pedersen/constraints.rs +++ b/crypto-primitives/src/crh/pedersen/constraints.rs @@ -6,6 +6,8 @@ use ark_ec::CurveGroup; use ark_ff::Field; use ark_r1cs_std::prelude::*; use ark_relations::r1cs::{Namespace, SynthesisError}; +#[cfg(not(feature = "std"))] +use ark_std::vec::Vec; use crate::crh::pedersen::{TwoToOneCRH, CRH}; use crate::crh::{CRHSchemeGadget, TwoToOneCRHSchemeGadget}; diff --git a/crypto-primitives/src/crh/pedersen/mod.rs b/crypto-primitives/src/crh/pedersen/mod.rs index 97850b86..29d696d4 100644 --- a/crypto-primitives/src/crh/pedersen/mod.rs +++ b/crypto-primitives/src/crh/pedersen/mod.rs @@ -13,6 +13,8 @@ use ark_ff::{Field, ToConstraintField}; use ark_serialize::{CanonicalDeserialize, CanonicalSerialize}; use ark_std::borrow::Borrow; use ark_std::cfg_chunks; +#[cfg(not(feature = "std"))] +use ark_std::vec::Vec; #[cfg(feature = "r1cs")] pub mod constraints; diff --git a/crypto-primitives/src/crh/poseidon/constraints.rs b/crypto-primitives/src/crh/poseidon/constraints.rs index a0ad5001..059a6934 100644 --- a/crypto-primitives/src/crh/poseidon/constraints.rs +++ b/crypto-primitives/src/crh/poseidon/constraints.rs @@ -15,6 +15,8 @@ use ark_r1cs_std::R1CSVar; use ark_relations::r1cs::{Namespace, SynthesisError}; use ark_std::borrow::Borrow; use ark_std::marker::PhantomData; +#[cfg(not(feature = "std"))] +use ark_std::vec::Vec; #[derive(Clone)] pub struct CRHParametersVar { diff --git a/crypto-primitives/src/crh/sha256/constraints.rs b/crypto-primitives/src/crh/sha256/constraints.rs index bf40c945..c68c97fd 100644 --- a/crypto-primitives/src/crh/sha256/constraints.rs +++ b/crypto-primitives/src/crh/sha256/constraints.rs @@ -19,6 +19,8 @@ use ark_r1cs_std::{ R1CSVar, }; use ark_relations::r1cs::{ConstraintSystemRef, Namespace, SynthesisError}; +#[cfg(not(feature = "std"))] +use ark_std::vec::Vec; const STATE_LEN: usize = 8; diff --git a/crypto-primitives/src/crh/sha256/mod.rs b/crypto-primitives/src/crh/sha256/mod.rs index 8a2cb1d4..16738d88 100644 --- a/crypto-primitives/src/crh/sha256/mod.rs +++ b/crypto-primitives/src/crh/sha256/mod.rs @@ -2,6 +2,8 @@ use crate::crh::{CRHScheme, TwoToOneCRHScheme}; use crate::Error; use ark_std::rand::Rng; +#[cfg(not(feature = "std"))] +use ark_std::vec::Vec; // Re-export the RustCrypto Sha256 type and its associated traits pub use sha2::{digest, Sha256}; diff --git a/crypto-primitives/src/encryption/elgamal/constraints.rs b/crypto-primitives/src/encryption/elgamal/constraints.rs index 290492d2..c5c3c21b 100644 --- a/crypto-primitives/src/encryption/elgamal/constraints.rs +++ b/crypto-primitives/src/encryption/elgamal/constraints.rs @@ -11,6 +11,8 @@ use ark_ff::{ Zero, }; use ark_serialize::CanonicalSerialize; +#[cfg(not(feature = "std"))] +use ark_std::vec::Vec; use ark_std::{borrow::Borrow, marker::PhantomData}; pub type ConstraintF = <::BaseField as Field>::BasePrimeField; diff --git a/crypto-primitives/src/merkle_tree/constraints.rs b/crypto-primitives/src/merkle_tree/constraints.rs index 4cb764a3..aa9fbb87 100644 --- a/crypto-primitives/src/merkle_tree/constraints.rs +++ b/crypto-primitives/src/merkle_tree/constraints.rs @@ -6,6 +6,8 @@ use ark_r1cs_std::prelude::*; use ark_relations::r1cs::{Namespace, SynthesisError}; use ark_std::borrow::Borrow; use ark_std::fmt::Debug; +#[cfg(not(feature = "std"))] +use ark_std::vec::Vec; pub trait DigestVarConverter { type TargetType: Borrow; diff --git a/crypto-primitives/src/merkle_tree/mod.rs b/crypto-primitives/src/merkle_tree/mod.rs index a5fd30b0..49627eac 100644 --- a/crypto-primitives/src/merkle_tree/mod.rs +++ b/crypto-primitives/src/merkle_tree/mod.rs @@ -6,8 +6,11 @@ use crate::sponge::Absorb; use crate::{crh::CRHScheme, Error}; use ark_serialize::{CanonicalDeserialize, CanonicalSerialize}; use ark_std::borrow::Borrow; -use ark_std::collections::{BTreeSet, HashMap}; +use ark_std::collections::BTreeSet; use ark_std::hash::Hash; +#[cfg(not(feature = "std"))] +use ark_std::vec::Vec; +use hashbrown::HashMap; #[cfg(test)] mod tests; @@ -65,7 +68,9 @@ pub trait Config { + Default + CanonicalSerialize + CanonicalDeserialize - + Send; + + Send + + Sync; + // transition between leaf layer to inner layer type LeafInnerDigestConverter: DigestConverter< Self::LeafDigest, @@ -80,6 +85,7 @@ pub trait Config { + CanonicalSerialize + CanonicalDeserialize + Send + + Sync + Absorb; // Tom's Note: in the future, if we want different hash function, we can simply add more diff --git a/crypto-primitives/src/prf/blake2s/constraints.rs b/crypto-primitives/src/prf/blake2s/constraints.rs index fdfd99d7..cd9ea242 100644 --- a/crypto-primitives/src/prf/blake2s/constraints.rs +++ b/crypto-primitives/src/prf/blake2s/constraints.rs @@ -3,6 +3,8 @@ use ark_relations::r1cs::{ConstraintSystemRef, Namespace, SynthesisError}; use crate::prf::PRFGadget; use ark_r1cs_std::prelude::*; +#[cfg(not(feature = "std"))] +use ark_std::vec::Vec; use core::borrow::Borrow; diff --git a/crypto-primitives/src/prf/blake2s/mod.rs b/crypto-primitives/src/prf/blake2s/mod.rs index b2696c19..7dd10c9b 100644 --- a/crypto-primitives/src/prf/blake2s/mod.rs +++ b/crypto-primitives/src/prf/blake2s/mod.rs @@ -1,3 +1,5 @@ +#[cfg(not(feature = "std"))] +use ark_std::vec::Vec; use blake2::{Blake2s256 as B2s, Blake2sMac}; use digest::Digest; diff --git a/crypto-primitives/src/prf/constraints.rs b/crypto-primitives/src/prf/constraints.rs index 1e09571d..19fb0da6 100644 --- a/crypto-primitives/src/prf/constraints.rs +++ b/crypto-primitives/src/prf/constraints.rs @@ -5,6 +5,8 @@ use crate::prf::PRF; use ark_relations::r1cs::{Namespace, SynthesisError}; use ark_r1cs_std::prelude::*; +#[cfg(not(feature = "std"))] +use ark_std::vec::Vec; pub trait PRFGadget { type OutputVar: EqGadget diff --git a/crypto-primitives/src/signature/schnorr/constraints.rs b/crypto-primitives/src/signature/schnorr/constraints.rs index 7d19ecc1..9198465f 100644 --- a/crypto-primitives/src/signature/schnorr/constraints.rs +++ b/crypto-primitives/src/signature/schnorr/constraints.rs @@ -5,7 +5,9 @@ use ark_relations::r1cs::{Namespace, SynthesisError}; use crate::signature::SigRandomizePkGadget; -use core::{borrow::Borrow, marker::PhantomData}; +#[cfg(not(feature = "std"))] +use ark_std::vec::Vec; +use ark_std::{borrow::Borrow, marker::PhantomData}; use crate::signature::schnorr::{Parameters, PublicKey, Schnorr}; use digest::Digest; diff --git a/crypto-primitives/src/signature/schnorr/mod.rs b/crypto-primitives/src/signature/schnorr/mod.rs index c8c3cf75..bd7692f6 100644 --- a/crypto-primitives/src/signature/schnorr/mod.rs +++ b/crypto-primitives/src/signature/schnorr/mod.rs @@ -7,6 +7,8 @@ use ark_ff::{ use ark_serialize::CanonicalSerialize; use ark_std::ops::Mul; use ark_std::rand::Rng; +#[cfg(not(feature = "std"))] +use ark_std::vec::Vec; use ark_std::{hash::Hash, marker::PhantomData}; use digest::Digest; diff --git a/crypto-primitives/src/snark/constraints.rs b/crypto-primitives/src/snark/constraints.rs index ba1ef909..92e16b92 100644 --- a/crypto-primitives/src/snark/constraints.rs +++ b/crypto-primitives/src/snark/constraints.rs @@ -15,6 +15,8 @@ use ark_relations::{ }, }; use ark_snark::{CircuitSpecificSetupSNARK, UniversalSetupSNARK, SNARK}; +#[cfg(not(feature = "std"))] +use ark_std::vec::Vec; use ark_std::{borrow::Borrow, fmt, marker::PhantomData, vec::IntoIter}; /// This implements constraints for SNARK verifiers. diff --git a/crypto-primitives/src/sponge/absorb.rs b/crypto-primitives/src/sponge/absorb.rs index eb154515..60d83e85 100644 --- a/crypto-primitives/src/sponge/absorb.rs +++ b/crypto-primitives/src/sponge/absorb.rs @@ -7,6 +7,8 @@ use ark_ec::{ use ark_ff::models::{Fp, FpConfig}; use ark_ff::{BigInteger, Field, PrimeField, ToConstraintField}; use ark_serialize::CanonicalSerialize; +#[cfg(not(feature = "std"))] +use ark_std::{string::String, vec::Vec}; pub use ark_crypto_primitives_macros::*; diff --git a/crypto-primitives/src/sponge/constraints/absorb.rs b/crypto-primitives/src/sponge/constraints/absorb.rs index fa943cdf..bb3abb4f 100644 --- a/crypto-primitives/src/sponge/constraints/absorb.rs +++ b/crypto-primitives/src/sponge/constraints/absorb.rs @@ -13,6 +13,8 @@ use ark_r1cs_std::groups::curves::short_weierstrass::{ use ark_r1cs_std::groups::curves::twisted_edwards::AffineVar as TEAffineVar; use ark_r1cs_std::uint8::UInt8; use ark_relations::r1cs::SynthesisError; +#[cfg(not(feature = "std"))] +use ark_std::vec::Vec; /// An interface for objects that can be absorbed by a `CryptographicSpongeVar` whose constraint field /// is `CF`. diff --git a/crypto-primitives/src/sponge/constraints/mod.rs b/crypto-primitives/src/sponge/constraints/mod.rs index 889f733f..aa872fe5 100644 --- a/crypto-primitives/src/sponge/constraints/mod.rs +++ b/crypto-primitives/src/sponge/constraints/mod.rs @@ -9,6 +9,8 @@ use ark_r1cs_std::uint8::UInt8; use ark_r1cs_std::R1CSVar; use ark_relations::lc; use ark_relations::r1cs::{ConstraintSystemRef, LinearCombination, SynthesisError}; +#[cfg(not(feature = "std"))] +use ark_std::vec::Vec; mod absorb; pub use absorb::*; diff --git a/crypto-primitives/src/sponge/mod.rs b/crypto-primitives/src/sponge/mod.rs index e0c84744..2356f169 100644 --- a/crypto-primitives/src/sponge/mod.rs +++ b/crypto-primitives/src/sponge/mod.rs @@ -1,5 +1,6 @@ use ark_ff::PrimeField; -use ark_std::vec; +#[cfg(not(feature = "std"))] +use ark_std::vec::Vec; /// Infrastructure for the constraints counterparts. #[cfg(feature = "r1cs")] diff --git a/crypto-primitives/src/sponge/poseidon/constraints.rs b/crypto-primitives/src/sponge/poseidon/constraints.rs index 276ed0e0..fc0409ac 100644 --- a/crypto-primitives/src/sponge/poseidon/constraints.rs +++ b/crypto-primitives/src/sponge/poseidon/constraints.rs @@ -7,6 +7,8 @@ use ark_ff::PrimeField; use ark_r1cs_std::fields::fp::FpVar; use ark_r1cs_std::prelude::*; use ark_relations::r1cs::{ConstraintSystemRef, SynthesisError}; +#[cfg(not(feature = "std"))] +use ark_std::vec::Vec; #[derive(Clone)] /// the gadget for Poseidon sponge diff --git a/crypto-primitives/src/sponge/poseidon/grain_lfsr.rs b/crypto-primitives/src/sponge/poseidon/grain_lfsr.rs index 1cda4f71..d576f1a1 100644 --- a/crypto-primitives/src/sponge/poseidon/grain_lfsr.rs +++ b/crypto-primitives/src/sponge/poseidon/grain_lfsr.rs @@ -1,6 +1,8 @@ #![allow(dead_code)] use ark_ff::{BigInteger, PrimeField}; +#[cfg(not(feature = "std"))] +use ark_std::vec::Vec; pub struct PoseidonGrainLFSR { pub prime_num_bits: u64, diff --git a/crypto-primitives/src/sponge/poseidon/mod.rs b/crypto-primitives/src/sponge/poseidon/mod.rs index 26349b0b..eee18b93 100644 --- a/crypto-primitives/src/sponge/poseidon/mod.rs +++ b/crypto-primitives/src/sponge/poseidon/mod.rs @@ -5,6 +5,8 @@ use crate::sponge::{ use ark_ff::{BigInteger, PrimeField}; use ark_serialize::{CanonicalDeserialize, CanonicalSerialize}; use ark_std::any::TypeId; +#[cfg(not(feature = "std"))] +use ark_std::vec::Vec; /// constraints for Poseidon #[cfg(feature = "r1cs")] diff --git a/crypto-primitives/src/sponge/poseidon/traits.rs b/crypto-primitives/src/sponge/poseidon/traits.rs index 237f8732..c1c446e7 100644 --- a/crypto-primitives/src/sponge/poseidon/traits.rs +++ b/crypto-primitives/src/sponge/poseidon/traits.rs @@ -1,6 +1,8 @@ use crate::sponge::poseidon::grain_lfsr::PoseidonGrainLFSR; use crate::sponge::poseidon::PoseidonConfig; use ark_ff::{fields::models::*, PrimeField}; +#[cfg(not(feature = "std"))] +use ark_std::vec::Vec; /// An entry in the default Poseidon parameters pub struct PoseidonDefaultConfigEntry {