Skip to content

Update version to 0.5 #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
23 changes: 11 additions & 12 deletions merkle-tree-example/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
[package]
name = "merkle-tree-example"
version = "0.3.0"
version = "0.5.0"
authors = ["arkworks contributors"]
edition = "2018"

[dependencies]
ark-ff = { version = "^0.3.0", default-features = false }
ark-ec = { version = "^0.3.0", default-features = false }
ark-ed-on-bls12-381 = { version = "^0.3.0", features = ["r1cs"] }
ark-bls12-381 = { version = "^0.3.0", default-features = false }
ark-std = { version = "^0.3.0", default-features = false }
ark-relations = { version = "^0.3.0", default-features = false }
ark-ff = { version = "^0.5.0", default-features = false }
ark-ec = { version = "^0.5.0", default-features = false }
ark-ed-on-bls12-381 = { version = "^0.5.0", features = ["r1cs"] }
ark-bls12-381 = { version = "^0.5.0", default-features = false }
ark-std = { version = "^0.5.0", default-features = false }
ark-relations = { version = "^0.5.0", default-features = false }

ark-r1cs-std = { version = "^0.3.0", default-features = false }
ark-snark = { version = "^0.3.0", default-features = false }
ark-r1cs-std = { version = "^0.5.0", default-features = false }
ark-snark = { version = "^0.5.0", default-features = false }

ark-serialize = { version = "^0.3.0", default-features = false }

ark-crypto-primitives = { version = "^0.3.0", default-features = true, features = [ "r1cs" ] }
ark-serialize = { version = "^0.5.0", default-features = false }
ark-crypto-primitives = { version = "^0.5.0", default-features = true, features = [ "r1cs" , "crh", "merkle_tree"] }
tracing = { version = "0.1", default-features = false, features = [ "attributes" ] }
tracing-subscriber = { version = "0.2" }
16 changes: 9 additions & 7 deletions merkle-tree-example/src/common.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use ark_crypto_primitives::crh::constraints::{CRHGadget, TwoToOneCRHGadget};
use ark_crypto_primitives::crh::injective_map::constraints::{
PedersenCRHCompressorGadget, TECompressorGadget,
PedersenCRHCompressorGadget, PedersenTwoToOneCRHCompressorGadget, TECompressorGadget,
};
use ark_crypto_primitives::crh::injective_map::PedersenTwoToOneCRHCompressor;
use ark_crypto_primitives::crh::{
injective_map::{PedersenCRHCompressor, TECompressor},
pedersen,
};
use ark_crypto_primitives::crh::{CRHSchemeGadget, TwoToOneCRHSchemeGadget};
use ark_ed_on_bls12_381::{constraints::EdwardsVar, EdwardsProjective};

pub type TwoToOneHash = PedersenCRHCompressor<EdwardsProjective, TECompressor, TwoToOneWindow>;
pub type TwoToOneHash =
PedersenTwoToOneCRHCompressor<EdwardsProjective, TECompressor, TwoToOneWindow>;
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct TwoToOneWindow;

Expand All @@ -19,7 +21,6 @@ impl pedersen::Window for TwoToOneWindow {
}

pub type LeafHash = PedersenCRHCompressor<EdwardsProjective, TECompressor, LeafWindow>;

#[derive(Clone, PartialEq, Eq, Hash)]
pub struct LeafWindow;

Expand All @@ -29,7 +30,7 @@ impl pedersen::Window for LeafWindow {
const NUM_WINDOWS: usize = 144;
}

pub type TwoToOneHashGadget = PedersenCRHCompressorGadget<
pub type TwoToOneHashGadget = PedersenTwoToOneCRHCompressorGadget<
EdwardsProjective,
TECompressor,
TwoToOneWindow,
Expand All @@ -45,8 +46,9 @@ pub type LeafHashGadget = PedersenCRHCompressorGadget<
TECompressorGadget,
>;

pub type LeafHashParamsVar = <LeafHashGadget as CRHGadget<LeafHash, ConstraintF>>::ParametersVar;
pub type LeafHashParamsVar =
<LeafHashGadget as CRHSchemeGadget<LeafHash, ConstraintF>>::ParametersVar;
pub type TwoToOneHashParamsVar =
<TwoToOneHashGadget as TwoToOneCRHGadget<TwoToOneHash, ConstraintF>>::ParametersVar;
<TwoToOneHashGadget as TwoToOneCRHSchemeGadget<TwoToOneHash, ConstraintF>>::ParametersVar;

pub type ConstraintF = ark_ed_on_bls12_381::Fq;
55 changes: 41 additions & 14 deletions merkle-tree-example/src/constraints.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::common::*;
use crate::{common::*, MerkleConfig, MerkleConfigVar};
use crate::{Root, SimplePath};
use ark_crypto_primitives::crh::{TwoToOneCRH, TwoToOneCRHGadget, CRH};
use ark_crypto_primitives::crh::{CRHScheme, TwoToOneCRHScheme, TwoToOneCRHSchemeGadget};
use ark_crypto_primitives::merkle_tree::constraints::PathVar;
use ark_r1cs_std::prelude::*;
use ark_relations::r1cs::{ConstraintSynthesizer, ConstraintSystemRef, SynthesisError};
Expand All @@ -9,18 +9,18 @@ use ark_relations::r1cs::{ConstraintSynthesizer, ConstraintSystemRef, SynthesisE
// just know that these are types that you can use.)

/// The R1CS equivalent of the the Merkle tree root.
pub type RootVar = <TwoToOneHashGadget as TwoToOneCRHGadget<TwoToOneHash, ConstraintF>>::OutputVar;
pub type RootVar =
<TwoToOneHashGadget as TwoToOneCRHSchemeGadget<TwoToOneHash, ConstraintF>>::OutputVar;

/// The R1CS equivalent of the the Merkle tree path.
pub type SimplePathVar =
PathVar<crate::MerkleConfig, LeafHashGadget, TwoToOneHashGadget, ConstraintF>;
pub type SimplePathVar = PathVar<MerkleConfig, ConstraintF, MerkleConfigVar>;

////////////////////////////////////////////////////////////////////////////////

pub struct MerkleTreeVerification {
// These are constants that will be embedded into the circuit
pub leaf_crh_params: <LeafHash as CRH>::Parameters,
pub two_to_one_crh_params: <TwoToOneHash as TwoToOneCRH>::Parameters,
pub leaf_crh_params: <LeafHash as CRHScheme>::Parameters,
pub two_to_one_crh_params: <TwoToOneHash as TwoToOneCRHScheme>::Parameters,

// These are the public inputs to the circuit.
pub root: Root,
Expand Down Expand Up @@ -75,15 +75,24 @@ fn merkle_tree_constraints_correctness() {
let mut rng = ark_std::test_rng();

// First, let's sample the public parameters for the hash functions:
let leaf_crh_params = <LeafHash as CRH>::setup(&mut rng).unwrap();
let two_to_one_crh_params = <TwoToOneHash as TwoToOneCRH>::setup(&mut rng).unwrap();
let leaf_crh_params = <LeafHash as CRHScheme>::setup(&mut rng).unwrap();
let two_to_one_crh_params = <TwoToOneHash as TwoToOneCRHScheme>::setup(&mut rng).unwrap();

// Next, let's construct our tree.
// This follows the API in https://github.com/arkworks-rs/crypto-primitives/blob/6be606259eab0aec010015e2cfd45e4f134cd9bf/src/merkle_tree/mod.rs#L156
let tree = crate::SimpleMerkleTree::new(
&leaf_crh_params,
&two_to_one_crh_params,
&[1u8, 2u8, 3u8, 10u8, 9u8, 17u8, 70u8, 45u8], // the i-th entry is the i-th leaf.
&[
&[1u8][..],
&[2u8][..],
&[3u8][..],
&[10u8][..],
&[9u8][..],
&[17u8][..],
&[70u8][..],
&[45u8][..],
], // the i-th entry is the i-th leaf.
)
.unwrap();

Expand Down Expand Up @@ -136,23 +145,41 @@ fn merkle_tree_constraints_soundness() {
let mut rng = ark_std::test_rng();

// First, let's sample the public parameters for the hash functions:
let leaf_crh_params = <LeafHash as CRH>::setup(&mut rng).unwrap();
let two_to_one_crh_params = <TwoToOneHash as TwoToOneCRH>::setup(&mut rng).unwrap();
let leaf_crh_params = <LeafHash as CRHScheme>::setup(&mut rng).unwrap();
let two_to_one_crh_params = <TwoToOneHash as TwoToOneCRHScheme>::setup(&mut rng).unwrap();

// Next, let's construct our tree.
// This follows the API in https://github.com/arkworks-rs/crypto-primitives/blob/6be606259eab0aec010015e2cfd45e4f134cd9bf/src/merkle_tree/mod.rs#L156
let tree = crate::SimpleMerkleTree::new(
&leaf_crh_params,
&two_to_one_crh_params,
&[1u8, 2u8, 3u8, 10u8, 9u8, 17u8, 70u8, 45u8], // the i-th entry is the i-th leaf.
&[
&[1u8][..],
&[2u8][..],
&[3u8][..],
&[10u8][..],
&[9u8][..],
&[17u8][..],
&[70u8][..],
&[45u8][..],
], // the i-th entry is the i-th leaf.
)
.unwrap();

// We just mutate the first leaf
let second_tree = crate::SimpleMerkleTree::new(
&leaf_crh_params,
&two_to_one_crh_params,
&[4u8, 2u8, 3u8, 10u8, 9u8, 17u8, 70u8, 45u8], // the i-th entry is the i-th leaf.
&[
&[4u8][..],
&[2u8][..],
&[3u8][..],
&[10u8][..],
&[9u8][..],
&[17u8][..],
&[70u8][..],
&[45u8][..],
], // the i-th entry is the i-th leaf.
)
.unwrap();

Expand Down
46 changes: 38 additions & 8 deletions merkle-tree-example/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use ark_crypto_primitives::crh::TwoToOneCRH;
use ark_crypto_primitives::merkle_tree::{Config, MerkleTree, Path};
use ark_crypto_primitives::crh::{
CRHScheme, CRHSchemeGadget, TwoToOneCRHScheme, TwoToOneCRHSchemeGadget,
};
use ark_crypto_primitives::merkle_tree::constraints::{BytesVarDigestConverter, ConfigGadget};
use ark_crypto_primitives::merkle_tree::{ByteDigestConverter, Config, MerkleTree, Path};

pub mod common;
use ark_r1cs_std::uint8::UInt8;
use common::*;

mod constraints;
Expand All @@ -12,35 +16,61 @@ pub struct MerkleConfig;
impl Config for MerkleConfig {
// Our Merkle tree relies on two hashes: one to hash leaves, and one to hash pairs
// of internal nodes.
type Leaf = [u8];
type LeafHash = LeafHash;
type TwoToOneHash = TwoToOneHash;
type LeafDigest = <LeafHash as CRHScheme>::Output;
type LeafInnerDigestConverter = ByteDigestConverter<Self::LeafDigest>;
type InnerDigest = <TwoToOneHash as TwoToOneCRHScheme>::Output;
}

struct MerkleConfigVar;
impl ConfigGadget<MerkleConfig, ConstraintF> for MerkleConfigVar {
type Leaf = LeafVar<ConstraintF>;
type LeafDigest = <LeafHashGadget as CRHSchemeGadget<LeafHash, ConstraintF>>::OutputVar;
type LeafInnerConverter = BytesVarDigestConverter<Self::LeafDigest, ConstraintF>;
type InnerDigest =
<TwoToOneHashGadget as TwoToOneCRHSchemeGadget<TwoToOneHash, ConstraintF>>::OutputVar;
type LeafHash = LeafHashGadget;
type TwoToOneHash = TwoToOneHashGadget;
}

type LeafVar<ConstraintF> = [UInt8<ConstraintF>];

/// A Merkle tree containing account information.
pub type SimpleMerkleTree = MerkleTree<MerkleConfig>;
/// The root of the account Merkle tree.
pub type Root = <TwoToOneHash as TwoToOneCRH>::Output;
pub type Root = <TwoToOneHash as TwoToOneCRHScheme>::Output;
/// A membership proof for a given account.
pub type SimplePath = Path<MerkleConfig>;

// Run this test via `cargo test --release test_merkle_tree`.
#[test]
fn test_merkle_tree() {
use ark_crypto_primitives::crh::CRH;
use ark_crypto_primitives::crh::CRHScheme;
// Let's set up an RNG for use within tests. Note that this is *not* safe
// for any production use.
let mut rng = ark_std::test_rng();

// First, let's sample the public parameters for the hash functions:
let leaf_crh_params = <LeafHash as CRH>::setup(&mut rng).unwrap();
let two_to_one_crh_params = <TwoToOneHash as TwoToOneCRH>::setup(&mut rng).unwrap();
let leaf_crh_params = <LeafHash as CRHScheme>::setup(&mut rng).unwrap();
let two_to_one_crh_params = <TwoToOneHash as TwoToOneCRHScheme>::setup(&mut rng).unwrap();

// Next, let's construct our tree.
// This follows the API in https://github.com/arkworks-rs/crypto-primitives/blob/6be606259eab0aec010015e2cfd45e4f134cd9bf/src/merkle_tree/mod.rs#L156
let tree = SimpleMerkleTree::new(
&leaf_crh_params,
&two_to_one_crh_params,
&[1u8, 2u8, 3u8, 10u8, 9u8, 17u8, 70u8, 45u8], // the i-th entry is the i-th leaf.
&[
&[1u8][..],
&[2u8][..],
&[3u8][..],
&[10u8][..],
&[9u8][..],
&[17u8][..],
&[70u8][..],
&[45u8][..],
], // the i-th entry is the i-th leaf.
)
.unwrap();

Expand All @@ -56,7 +86,7 @@ fn test_merkle_tree() {
&leaf_crh_params,
&two_to_one_crh_params,
&root,
&[9u8], // The claimed leaf
&[9u8][..], // The claimed leaf
)
.unwrap();
assert!(result);
Expand Down
27 changes: 14 additions & 13 deletions rollup/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ark-rollup"
version = "0.3.0"
version = "0.5.0"
authors = [ "arkworks contributors" ]
description = "A SNARK-based rollup for a simple payments system"
repository = "https://github.com/arkworks-rs/r1cs-tutorial"
Expand All @@ -11,21 +11,22 @@ license = "MIT/Apache-2.0"
edition = "2018"

[dependencies]
ark-ff = { version = "^0.3.0", default-features = false }
ark-ec = { version = "^0.3.0", default-features = false }
ark-ed-on-bls12-381 = { version = "^0.3.0", features = ["r1cs"] }
ark-bls12-381 = { version = "^0.3.0" }
ark-std = { version = "^0.3.0", default-features = false }
ark-relations = { version = "^0.3.0", default-features = false, optional = true }
ark-ff = { version = "^0.5.0", default-features = false }
ark-ec = { version = "^0.5.0", default-features = false }
ark-ed-on-bls12-381 = { version = "^0.5.0", features = ["r1cs"] }
ark-bls12-381 = { version = "^0.5.0" }

ark-r1cs-std = { version = "^0.3.0", optional = true, default-features = false }
ark-snark = { version = "^0.3.0", default-features = false }
ark-groth16 = { version = "^0.3.0" }
ark-gm17 = { version = "^0.3.0" }
ark-std = { version = "^0.5.0", default-features = false }
ark-relations = { version = "^0.5.0", default-features = false, optional = true }

ark-serialize = { version = "^0.3.0", default-features = false }
ark-r1cs-std = { version = "^0.5.0", optional = true, default-features = false }
ark-snark = { version = "^0.5.0", default-features = false }
ark-groth16 = { version = "^0.5.0" }

ark-crypto-primitives = { version = "^0.3.0", default-features = true }

ark-serialize = { version = "^0.5.0", default-features = false }

ark-crypto-primitives = { version = "^0.5.0", default-features = true }
ark-simple-payments = { path = "../simple-payments", default-features = true }
blake2 = { version = "0.9" }
digest = "0.9"
Expand Down
11 changes: 4 additions & 7 deletions rollup/src/account.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::ledger::*;
use crate::ConstraintF;
use ark_ed_on_bls12_381::{constraints::EdwardsVar, EdwardsProjective};
use ark_r1cs_std::bits::{uint8::UInt8, ToBytesGadget};
use ark_r1cs_std::prelude::*;
use ark_r1cs_std::uint8::UInt8;
use ark_relations::r1cs::{Namespace, SynthesisError};
use ark_simple_payments::account::*;
use ark_simple_payments::signature::schnorr::constraints::*;
Expand Down Expand Up @@ -47,12 +47,9 @@ impl AccountInformationVar {
/// Convert the account information to bytes.
#[tracing::instrument(target = "r1cs", skip(self))]
pub fn to_bytes_le(&self) -> Vec<UInt8<crate::ConstraintF>> {
self.public_key
.to_bytes()
.unwrap()
.into_iter()
.chain(self.balance.to_bytes_le())
.collect()
let mut result = self.public_key.to_bytes_le().unwrap();
result.extend_from_slice(&self.balance.to_bytes_le());
result
}
}

Expand Down
Loading