mirror of
https://github.com/vacp2p/zerokit.git
synced 2026-01-10 06:08:09 -05:00
Compare commits
1 Commits
tyshko_ros
...
benchmark-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
89d4c42234 |
138
.github/workflows/ci.yml
vendored
138
.github/workflows/ci.yml
vendored
@@ -28,18 +28,18 @@ on:
|
||||
name: Tests
|
||||
|
||||
jobs:
|
||||
test:
|
||||
|
||||
multiplier:
|
||||
strategy:
|
||||
matrix:
|
||||
platform: [ubuntu-latest, macos-latest]
|
||||
crate: [multiplier, semaphore, rln, utils]
|
||||
runs-on: ${{ matrix.platform }}
|
||||
timeout-minutes: 60
|
||||
|
||||
name: test - ${{ matrix.crate }} - ${{ matrix.platform }}
|
||||
name: multiplier - ${{ matrix.platform }}
|
||||
steps:
|
||||
- name: Checkout sources
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v2
|
||||
- name: Install stable toolchain
|
||||
uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
@@ -52,8 +52,33 @@ jobs:
|
||||
- name: cargo-make test
|
||||
run: |
|
||||
cargo make test --release
|
||||
working-directory: ${{ matrix.crate }}
|
||||
working-directory: multiplier
|
||||
|
||||
semaphore:
|
||||
strategy:
|
||||
matrix:
|
||||
platform: [ubuntu-latest, macos-latest]
|
||||
runs-on: ${{ matrix.platform }}
|
||||
timeout-minutes: 60
|
||||
|
||||
name: semaphore - ${{ matrix.platform }}
|
||||
steps:
|
||||
- name: Checkout sources
|
||||
uses: actions/checkout@v2
|
||||
- name: Install stable toolchain
|
||||
uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
profile: minimal
|
||||
toolchain: stable
|
||||
override: true
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
- name: Install Dependencies
|
||||
run: make installdeps
|
||||
- name: cargo-make test
|
||||
run: |
|
||||
cargo make test --release
|
||||
working-directory: semaphore
|
||||
|
||||
rln-wasm:
|
||||
strategy:
|
||||
matrix:
|
||||
@@ -61,9 +86,9 @@ jobs:
|
||||
runs-on: ${{ matrix.platform }}
|
||||
timeout-minutes: 60
|
||||
|
||||
name: test - rln-wasm - ${{ matrix.platform }}
|
||||
name: rln-wasm - ${{ matrix.platform }}
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/checkout@v2
|
||||
- name: Install stable toolchain
|
||||
uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
@@ -80,19 +105,68 @@ jobs:
|
||||
- run: cargo make test --release
|
||||
working-directory: rln-wasm
|
||||
|
||||
rln:
|
||||
strategy:
|
||||
matrix:
|
||||
platform: [ubuntu-latest, macos-latest]
|
||||
runs-on: ${{ matrix.platform }}
|
||||
timeout-minutes: 60
|
||||
|
||||
name: rln - ${{ matrix.platform }}
|
||||
steps:
|
||||
- name: Checkout sources
|
||||
uses: actions/checkout@v2
|
||||
- name: Install stable toolchain
|
||||
uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
profile: minimal
|
||||
toolchain: stable
|
||||
override: true
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
- name: Install Dependencies
|
||||
run: make installdeps
|
||||
- name: cargo-make test
|
||||
run: |
|
||||
cargo make test --release
|
||||
working-directory: rln
|
||||
|
||||
utils:
|
||||
strategy:
|
||||
matrix:
|
||||
platform: [ubuntu-latest, macos-latest]
|
||||
runs-on: ${{ matrix.platform }}
|
||||
timeout-minutes: 60
|
||||
|
||||
name: utils - ${{ matrix.platform }}
|
||||
steps:
|
||||
- name: Checkout sources
|
||||
uses: actions/checkout@v2
|
||||
- name: Install stable toolchain
|
||||
uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
profile: minimal
|
||||
toolchain: stable
|
||||
override: true
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
- name: Install Dependencies
|
||||
run: make installdeps
|
||||
- name: cargo-make test
|
||||
run: |
|
||||
cargo make test --release
|
||||
working-directory: utils
|
||||
|
||||
lint:
|
||||
strategy:
|
||||
matrix:
|
||||
# we run lint tests only on ubuntu
|
||||
platform: [ubuntu-latest]
|
||||
crate: [multiplier, semaphore, rln, utils]
|
||||
runs-on: ${{ matrix.platform }}
|
||||
timeout-minutes: 60
|
||||
|
||||
name: lint - ${{ matrix.crate }} - ${{ matrix.platform }}
|
||||
name: lint - ${{ matrix.platform }}
|
||||
steps:
|
||||
- name: Checkout sources
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v2
|
||||
- name: Install stable toolchain
|
||||
uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
@@ -105,33 +179,27 @@ jobs:
|
||||
run: make installdeps
|
||||
- name: cargo fmt
|
||||
if: success() || failure()
|
||||
run: cargo fmt -- --check
|
||||
working-directory: ${{ matrix.crate }}
|
||||
- name: cargo clippy
|
||||
run: cargo fmt --all -- --check
|
||||
- name: multiplier - cargo clippy
|
||||
if: success() || failure()
|
||||
run: |
|
||||
cargo clippy --release -- -D warnings
|
||||
working-directory: ${{ matrix.crate }}
|
||||
working-directory: multiplier
|
||||
- name: semaphore - cargo clippy
|
||||
if: success() || failure()
|
||||
run: |
|
||||
cargo clippy --release -- -D warnings
|
||||
working-directory: semaphore
|
||||
- name: rln - cargo clippy
|
||||
if: success() || failure()
|
||||
run: |
|
||||
cargo clippy --release -- -D warnings
|
||||
working-directory: rln
|
||||
- name: utils - cargo clippy
|
||||
if: success() || failure()
|
||||
run: |
|
||||
cargo clippy --release -- -D warnings
|
||||
working-directory: utils
|
||||
# We skip clippy on rln-wasm, since wasm target is managed by cargo make
|
||||
# Currently not treating warnings as error, too noisy
|
||||
# -- -D warnings
|
||||
benchmark:
|
||||
# run only in pull requests
|
||||
if: github.event_name == 'pull_request'
|
||||
strategy:
|
||||
matrix:
|
||||
# we run benchmark tests only on ubuntu
|
||||
platform: [ubuntu-latest]
|
||||
crate: [rln, utils]
|
||||
runs-on: ${{ matrix.platform }}
|
||||
timeout-minutes: 60
|
||||
|
||||
name: benchmark - ${{ matrix.platform }} - ${{ matrix.crate }}
|
||||
steps:
|
||||
- name: Checkout sources
|
||||
uses: actions/checkout@v3
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
- uses: boa-dev/criterion-compare-action@v3
|
||||
with:
|
||||
branchName: ${{ github.base_ref }}
|
||||
cwd: ${{ matrix.crate }}
|
||||
# -- -D warnings
|
||||
8
.github/workflows/nightly-release.yml
vendored
8
.github/workflows/nightly-release.yml
vendored
@@ -16,7 +16,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout sources
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v2
|
||||
- name: Install stable toolchain
|
||||
uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
@@ -51,7 +51,7 @@ jobs:
|
||||
- aarch64-apple-darwin
|
||||
steps:
|
||||
- name: Checkout sources
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v2
|
||||
- name: Install stable toolchain
|
||||
uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
@@ -81,7 +81,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout sources
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v2
|
||||
- name: Install stable toolchain
|
||||
uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
@@ -115,7 +115,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v2
|
||||
with:
|
||||
ref: master
|
||||
- name: Download artifacts
|
||||
|
||||
2
.github/workflows/sync-labels.yml
vendored
2
.github/workflows/sync-labels.yml
vendored
@@ -9,7 +9,7 @@ jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/checkout@v2
|
||||
- uses: micnncim/action-label-syncer@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
182
Cargo.lock
generated
182
Cargo.lock
generated
@@ -125,34 +125,6 @@ dependencies = [
|
||||
"ark-std 0.4.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ark-circom"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "295bb8e275f3e211b36a822469ba88deb028ecb3d7fe8684102598a9158a7350"
|
||||
dependencies = [
|
||||
"ark-bn254 0.4.0",
|
||||
"ark-crypto-primitives 0.4.0",
|
||||
"ark-ec 0.4.1",
|
||||
"ark-ff 0.4.1",
|
||||
"ark-groth16 0.4.0",
|
||||
"ark-poly 0.4.1",
|
||||
"ark-relations 0.4.0",
|
||||
"ark-serialize 0.4.1",
|
||||
"ark-std 0.4.0",
|
||||
"byteorder",
|
||||
"cfg-if",
|
||||
"color-eyre 0.6.2",
|
||||
"criterion 0.3.6",
|
||||
"fnv",
|
||||
"hex",
|
||||
"num",
|
||||
"num-bigint",
|
||||
"num-traits",
|
||||
"thiserror",
|
||||
"wasmer",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ark-circom"
|
||||
version = "0.1.0"
|
||||
@@ -170,7 +142,7 @@ dependencies = [
|
||||
"cfg-if",
|
||||
"color-eyre 0.6.2",
|
||||
"criterion 0.3.6",
|
||||
"ethers-core 2.0.4",
|
||||
"ethers-core",
|
||||
"fnv",
|
||||
"hex",
|
||||
"num",
|
||||
@@ -197,7 +169,34 @@ dependencies = [
|
||||
"cfg-if",
|
||||
"color-eyre 0.5.11",
|
||||
"criterion 0.3.6",
|
||||
"ethers-core 2.0.4",
|
||||
"ethers-core",
|
||||
"fnv",
|
||||
"hex",
|
||||
"num",
|
||||
"num-bigint",
|
||||
"num-traits",
|
||||
"thiserror",
|
||||
"wasmer",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ark-circom"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/gakonst/ark-circom#f97ac2b2458fd14489bc70375bb45688f5ef26a8"
|
||||
dependencies = [
|
||||
"ark-bn254 0.4.0",
|
||||
"ark-crypto-primitives 0.4.0",
|
||||
"ark-ec 0.4.1",
|
||||
"ark-ff 0.4.1",
|
||||
"ark-groth16 0.4.0",
|
||||
"ark-poly 0.4.1",
|
||||
"ark-relations 0.4.0",
|
||||
"ark-serialize 0.4.1",
|
||||
"ark-std 0.4.0",
|
||||
"byteorder",
|
||||
"cfg-if",
|
||||
"color-eyre 0.6.2",
|
||||
"criterion 0.3.6",
|
||||
"fnv",
|
||||
"hex",
|
||||
"num",
|
||||
@@ -1523,34 +1522,7 @@ dependencies = [
|
||||
"rlp",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"strum 0.24.1",
|
||||
"tempfile",
|
||||
"thiserror",
|
||||
"tiny-keccak",
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ethers-core"
|
||||
version = "2.0.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "60ca2514feb98918a0a31de7e1983c29f2267ebf61b2dc5d4294f91e5b866623"
|
||||
dependencies = [
|
||||
"arrayvec",
|
||||
"bytes",
|
||||
"chrono",
|
||||
"elliptic-curve",
|
||||
"ethabi",
|
||||
"generic-array",
|
||||
"hex",
|
||||
"k256",
|
||||
"num_enum",
|
||||
"open-fastrlp",
|
||||
"rand",
|
||||
"rlp",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"strum 0.25.0",
|
||||
"strum",
|
||||
"tempfile",
|
||||
"thiserror",
|
||||
"tiny-keccak",
|
||||
@@ -2412,6 +2384,14 @@ dependencies = [
|
||||
"plotters-backend",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pmtree"
|
||||
version = "1.0.0"
|
||||
source = "git+https://github.com/Rate-Limiting-Nullifier/pmtree?rev=bf27d4273b34a7f4ec3c77cdf67fb17a5602504a#bf27d4273b34a7f4ec3c77cdf67fb17a5602504a"
|
||||
dependencies = [
|
||||
"rayon",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ppv-lite86"
|
||||
version = "0.2.17"
|
||||
@@ -2689,7 +2669,7 @@ name = "rln"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"ark-bn254 0.4.0",
|
||||
"ark-circom 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"ark-circom 0.1.0 (git+https://github.com/gakonst/ark-circom)",
|
||||
"ark-ec 0.4.1",
|
||||
"ark-ff 0.4.1",
|
||||
"ark-groth16 0.4.0",
|
||||
@@ -2698,7 +2678,6 @@ dependencies = [
|
||||
"ark-std 0.4.0",
|
||||
"cfg-if",
|
||||
"color-eyre 0.6.2",
|
||||
"criterion 0.4.0",
|
||||
"include_dir",
|
||||
"num-bigint",
|
||||
"num-traits",
|
||||
@@ -2710,8 +2689,8 @@ dependencies = [
|
||||
"sled",
|
||||
"thiserror",
|
||||
"tiny-keccak",
|
||||
"utils",
|
||||
"wasmer",
|
||||
"zerokit_utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -2722,8 +2701,6 @@ dependencies = [
|
||||
"clap_derive",
|
||||
"color-eyre 0.6.2",
|
||||
"rln",
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -2928,7 +2905,7 @@ dependencies = [
|
||||
"ark-std 0.3.0",
|
||||
"color-eyre 0.6.2",
|
||||
"enumset",
|
||||
"ethers-core 2.0.4",
|
||||
"ethers-core",
|
||||
"hex",
|
||||
"hex-literal",
|
||||
"num-bigint",
|
||||
@@ -2954,7 +2931,7 @@ dependencies = [
|
||||
"ark-relations 0.3.0",
|
||||
"ark-std 0.3.0",
|
||||
"color-eyre 0.6.2",
|
||||
"ethers-core 2.0.8",
|
||||
"ethers-core",
|
||||
"once_cell",
|
||||
"rand",
|
||||
"rand_chacha",
|
||||
@@ -3161,16 +3138,7 @@ version = "0.24.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "063e6045c0e62079840579a7e47a355ae92f60eb74daaf156fb1e84ba164e63f"
|
||||
dependencies = [
|
||||
"strum_macros 0.24.3",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "strum"
|
||||
version = "0.25.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "290d54ea6f91c969195bdbcd7442c8c2a2ba87da8bf60a7ee86a235d4bc1e125"
|
||||
dependencies = [
|
||||
"strum_macros 0.25.1",
|
||||
"strum_macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -3186,19 +3154,6 @@ dependencies = [
|
||||
"syn 1.0.109",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "strum_macros"
|
||||
version = "0.25.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6069ca09d878a33f883cc06aaa9718ede171841d3832450354410b718b097232"
|
||||
dependencies = [
|
||||
"heck",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"rustversion",
|
||||
"syn 2.0.16",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "subtle"
|
||||
version = "2.4.1"
|
||||
@@ -3269,18 +3224,18 @@ checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d"
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "1.0.39"
|
||||
version = "1.0.38"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a5ab016db510546d856297882807df8da66a16fb8c4101cb8b30054b0d5b2d9c"
|
||||
checksum = "6a9cd18aa97d5c45c6603caea1da6628790b37f7a34b6ca89522331c5180fed0"
|
||||
dependencies = [
|
||||
"thiserror-impl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror-impl"
|
||||
version = "1.0.39"
|
||||
version = "1.0.38"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5420d42e90af0c38c3290abcca25b9b3bdf379fc9f55c528f53a269d9c9a267e"
|
||||
checksum = "1fb327af4685e4d03fa8cbcf1716380da910eeb2bb8be417e7f9fd3fb164f36f"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
@@ -3498,21 +3453,29 @@ version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
|
||||
|
||||
[[package]]
|
||||
name = "utils"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"ark-bn254 0.4.0",
|
||||
"ark-ff 0.4.1",
|
||||
"color-eyre 0.6.2",
|
||||
"criterion 0.4.0",
|
||||
"hex-literal",
|
||||
"num-bigint",
|
||||
"num-traits",
|
||||
"pmtree",
|
||||
"serde",
|
||||
"sled",
|
||||
"tiny-keccak",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "uuid"
|
||||
version = "1.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4dad5567ad0cf5b760e5665964bec1b47dfd077ba8a2544b513f3556d3d239a2"
|
||||
|
||||
[[package]]
|
||||
name = "vacp2p_pmtree"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bee78790de52d6774ea318eb747a488fd5d96055bfe75ec2e7f605b15e21c664"
|
||||
dependencies = [
|
||||
"rayon",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "valuable"
|
||||
version = "0.1.0"
|
||||
@@ -4257,20 +4220,3 @@ dependencies = [
|
||||
"quote",
|
||||
"syn 2.0.16",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerokit_utils"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"ark-bn254 0.4.0",
|
||||
"ark-ff 0.4.1",
|
||||
"color-eyre 0.6.2",
|
||||
"criterion 0.4.0",
|
||||
"hex-literal",
|
||||
"num-bigint",
|
||||
"num-traits",
|
||||
"serde",
|
||||
"sled",
|
||||
"tiny-keccak",
|
||||
"vacp2p_pmtree",
|
||||
]
|
||||
|
||||
5
Makefile
5
Makefile
@@ -8,13 +8,12 @@ all: .pre-build build
|
||||
.pre-build: .fetch-submodules
|
||||
@cargo install cargo-make
|
||||
ifdef CI
|
||||
@cargo install cross --git https://github.com/cross-rs/cross.git --rev 1511a28
|
||||
@cargo install cross --git https://github.com/cross-rs/cross --branch main
|
||||
endif
|
||||
|
||||
installdeps: .pre-build
|
||||
ifeq ($(shell uname),Darwin)
|
||||
# commented due to https://github.com/orgs/Homebrew/discussions/4612
|
||||
# @brew update
|
||||
@brew update
|
||||
@brew install cmake ninja
|
||||
else ifeq ($(shell uname),Linux)
|
||||
@sudo apt-get update
|
||||
|
||||
@@ -8,6 +8,3 @@ rln = { path = "../rln" }
|
||||
clap = { version = "4.2.7", features = ["cargo", "derive", "env"]}
|
||||
clap_derive = { version = "=4.2.0" }
|
||||
color-eyre = "=0.6.2"
|
||||
# serialization
|
||||
serde_json = "1.0.48"
|
||||
serde = { version = "1.0.130", features = ["derive"] }
|
||||
|
||||
@@ -33,33 +33,4 @@ pub(crate) enum Commands {
|
||||
#[arg(short, long)]
|
||||
file: PathBuf,
|
||||
},
|
||||
SetNextLeaf {
|
||||
#[arg(short, long)]
|
||||
file: PathBuf,
|
||||
},
|
||||
DeleteLeaf {
|
||||
index: usize,
|
||||
},
|
||||
GetRoot,
|
||||
GetProof {
|
||||
index: usize,
|
||||
},
|
||||
Prove {
|
||||
#[arg(short, long)]
|
||||
input: PathBuf,
|
||||
},
|
||||
Verify {
|
||||
#[arg(short, long)]
|
||||
file: PathBuf,
|
||||
},
|
||||
GenerateProof {
|
||||
#[arg(short, long)]
|
||||
input: PathBuf,
|
||||
},
|
||||
VerifyWithRoots {
|
||||
#[arg(short, long)]
|
||||
input: PathBuf,
|
||||
#[arg(short, long)]
|
||||
roots: PathBuf,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
use color_eyre::Result;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{fs::File, io::Read, path::PathBuf};
|
||||
|
||||
pub const RLN_STATE_PATH: &str = "RLN_STATE_PATH";
|
||||
|
||||
#[derive(Default, Serialize, Deserialize)]
|
||||
pub(crate) struct Config {
|
||||
pub inner: Option<InnerConfig>,
|
||||
}
|
||||
|
||||
#[derive(Default, Serialize, Deserialize)]
|
||||
pub(crate) struct InnerConfig {
|
||||
pub file: PathBuf,
|
||||
pub tree_height: usize,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub(crate) fn load_config() -> Result<Config> {
|
||||
let path = PathBuf::from(std::env::var(RLN_STATE_PATH)?);
|
||||
|
||||
let mut file = File::open(path)?;
|
||||
|
||||
let mut contents = String::new();
|
||||
file.read_to_string(&mut contents)?;
|
||||
let state: Config = serde_json::from_str(&contents)?;
|
||||
Ok(state)
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,6 @@ use rln::public::RLN;
|
||||
use state::State;
|
||||
|
||||
mod commands;
|
||||
mod config;
|
||||
mod state;
|
||||
|
||||
#[derive(Parser)]
|
||||
@@ -20,7 +19,7 @@ struct Cli {
|
||||
fn main() -> Result<()> {
|
||||
let cli = Cli::parse();
|
||||
|
||||
let mut state = State::load_state()?;
|
||||
let mut state = State::default();
|
||||
|
||||
match &cli.command {
|
||||
Some(Commands::New {
|
||||
@@ -55,7 +54,7 @@ fn main() -> Result<()> {
|
||||
Some(Commands::SetTree { tree_height }) => {
|
||||
state
|
||||
.rln
|
||||
.ok_or(Report::msg("no RLN instance initialized"))?
|
||||
.ok_or(Report::msg("no RLN initialized"))?
|
||||
.set_tree(*tree_height)?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -63,7 +62,7 @@ fn main() -> Result<()> {
|
||||
let input_data = File::open(&file)?;
|
||||
state
|
||||
.rln
|
||||
.ok_or(Report::msg("no RLN instance initialized"))?
|
||||
.ok_or(Report::msg("no RLN initialized"))?
|
||||
.set_leaf(*index, input_data)?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -71,7 +70,7 @@ fn main() -> Result<()> {
|
||||
let input_data = File::open(&file)?;
|
||||
state
|
||||
.rln
|
||||
.ok_or(Report::msg("no RLN instance initialized"))?
|
||||
.ok_or(Report::msg("no RLN initialized"))?
|
||||
.set_leaves_from(*index, input_data)?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -79,76 +78,10 @@ fn main() -> Result<()> {
|
||||
let input_data = File::open(&file)?;
|
||||
state
|
||||
.rln
|
||||
.ok_or(Report::msg("no RLN instance initialized"))?
|
||||
.ok_or(Report::msg("no RLN initialized"))?
|
||||
.init_tree_with_leaves(input_data)?;
|
||||
Ok(())
|
||||
}
|
||||
Some(Commands::SetNextLeaf { file }) => {
|
||||
let input_data = File::open(&file)?;
|
||||
state
|
||||
.rln
|
||||
.ok_or(Report::msg("no RLN instance initialized"))?
|
||||
.set_next_leaf(input_data)?;
|
||||
Ok(())
|
||||
}
|
||||
Some(Commands::DeleteLeaf { index }) => {
|
||||
state
|
||||
.rln
|
||||
.ok_or(Report::msg("no RLN instance initialized"))?
|
||||
.delete_leaf(*index)?;
|
||||
Ok(())
|
||||
}
|
||||
Some(Commands::GetRoot) => {
|
||||
let writer = std::io::stdout();
|
||||
state
|
||||
.rln
|
||||
.ok_or(Report::msg("no RLN instance initialized"))?
|
||||
.get_root(writer)?;
|
||||
Ok(())
|
||||
}
|
||||
Some(Commands::GetProof { index }) => {
|
||||
let writer = std::io::stdout();
|
||||
state
|
||||
.rln
|
||||
.ok_or(Report::msg("no RLN instance initialized"))?
|
||||
.get_proof(*index, writer)?;
|
||||
Ok(())
|
||||
}
|
||||
Some(Commands::Prove { input }) => {
|
||||
let input_data = File::open(&input)?;
|
||||
let writer = std::io::stdout();
|
||||
state
|
||||
.rln
|
||||
.ok_or(Report::msg("no RLN instance initialized"))?
|
||||
.prove(input_data, writer)?;
|
||||
Ok(())
|
||||
}
|
||||
Some(Commands::Verify { file }) => {
|
||||
let input_data = File::open(&file)?;
|
||||
state
|
||||
.rln
|
||||
.ok_or(Report::msg("no RLN instance initialized"))?
|
||||
.verify(input_data)?;
|
||||
Ok(())
|
||||
}
|
||||
Some(Commands::GenerateProof { input }) => {
|
||||
let input_data = File::open(&input)?;
|
||||
let writer = std::io::stdout();
|
||||
state
|
||||
.rln
|
||||
.ok_or(Report::msg("no RLN instance initialized"))?
|
||||
.generate_rln_proof(input_data, writer)?;
|
||||
Ok(())
|
||||
}
|
||||
Some(Commands::VerifyWithRoots { input, roots }) => {
|
||||
let input_data = File::open(&input)?;
|
||||
let roots_data = File::open(&roots)?;
|
||||
state
|
||||
.rln
|
||||
.ok_or(Report::msg("no RLN instance initialized"))?
|
||||
.verify_with_roots(input_data, roots_data)?;
|
||||
Ok(())
|
||||
}
|
||||
None => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,23 +1,6 @@
|
||||
use color_eyre::Result;
|
||||
use rln::public::RLN;
|
||||
use std::fs::File;
|
||||
|
||||
use crate::config::{InnerConfig, Config};
|
||||
|
||||
#[derive(Default)]
|
||||
pub(crate) struct State<'a> {
|
||||
pub rln: Option<RLN<'a>>,
|
||||
}
|
||||
|
||||
impl<'a> State<'a> {
|
||||
pub(crate) fn load_state() -> Result<State<'a>> {
|
||||
let config = Config::load_config()?;
|
||||
let rln = if let Some(InnerConfig { file, tree_height }) = config.inner {
|
||||
let resources = File::open(&file)?;
|
||||
Some(RLN::new(tree_height, resources)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
Ok(State { rln })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -236,27 +236,6 @@ pub fn wasm_delete_leaf(ctx: *mut RLNWrapper, index: usize) -> Result<(), String
|
||||
call_with_error_msg!(ctx, delete_leaf, "could not delete leaf".to_string(), index)
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[wasm_bindgen(js_name = setMetadata)]
|
||||
pub fn wasm_set_metadata(ctx: *mut RLNWrapper, input: Uint8Array) -> Result<(), String> {
|
||||
call_with_error_msg!(
|
||||
ctx,
|
||||
set_metadata,
|
||||
"could not set metadata".to_string(),
|
||||
&*input.to_vec()
|
||||
)
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[wasm_bindgen(js_name = getMetadata)]
|
||||
pub fn wasm_get_metadata(ctx: *mut RLNWrapper) -> Result<Uint8Array, String> {
|
||||
call_with_output_and_error_msg!(
|
||||
ctx,
|
||||
get_metadata,
|
||||
"could not get metadata".to_string()
|
||||
)
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[wasm_bindgen(js_name = initTreeWithLeaves)]
|
||||
pub fn wasm_init_tree_with_leaves(ctx: *mut RLNWrapper, input: Uint8Array) -> Result<(), String> {
|
||||
|
||||
@@ -6,7 +6,8 @@ mod tests {
|
||||
use rln::circuit::TEST_TREE_HEIGHT;
|
||||
use rln::utils::normalize_usize;
|
||||
use rln_wasm::*;
|
||||
use wasm_bindgen::{prelude::*, JsValue};
|
||||
use wasm_bindgen::prelude::*;
|
||||
use wasm_bindgen::JsValue;
|
||||
use wasm_bindgen_test::wasm_bindgen_test;
|
||||
|
||||
#[wasm_bindgen(module = "src/utils.js")]
|
||||
@@ -108,28 +109,4 @@ mod tests {
|
||||
let is_proof_valid = wasm_verify_with_roots(rln_instance, proof_with_signal, roots);
|
||||
assert!(is_proof_valid.unwrap(), "verifying proof with roots failed");
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn test_metadata() {
|
||||
let tree_height = TEST_TREE_HEIGHT;
|
||||
let circom_path = format!("../rln/resources/tree_height_{TEST_TREE_HEIGHT}/rln.wasm");
|
||||
let zkey_path = format!("../rln/resources/tree_height_{TEST_TREE_HEIGHT}/rln_final.zkey");
|
||||
let vk_path =
|
||||
format!("../rln/resources/tree_height_{TEST_TREE_HEIGHT}/verification_key.json");
|
||||
let zkey = read_file(&zkey_path).unwrap();
|
||||
let vk = read_file(&vk_path).unwrap();
|
||||
|
||||
// Creating an instance of RLN
|
||||
let rln_instance = wasm_new(tree_height, zkey, vk).unwrap();
|
||||
|
||||
|
||||
let test_metadata = Uint8Array::new(&JsValue::from_str("test"));
|
||||
// Inserting random metadata
|
||||
wasm_set_metadata(rln_instance, test_metadata.clone()).unwrap();
|
||||
|
||||
// Getting metadata
|
||||
let metadata = wasm_get_metadata(rln_instance).unwrap();
|
||||
|
||||
assert_eq!(metadata.to_vec(), test_metadata.to_vec());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,11 +3,9 @@ name = "rln"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
license = "MIT OR Apache-2.0"
|
||||
description = "APIs to manage, compute and verify zkSNARK proofs and RLN primitives"
|
||||
|
||||
[lib]
|
||||
crate-type = ["rlib", "staticlib"]
|
||||
bench = false
|
||||
|
||||
# This flag disable cargo doctests, i.e. testing example code-snippets in documentation
|
||||
doctest = false
|
||||
@@ -22,34 +20,33 @@ ark-bn254 = { version = "=0.4.0" }
|
||||
ark-groth16 = { version = "=0.4.0", features = ["parallel"], default-features = false }
|
||||
ark-relations = { version = "=0.4.0", default-features = false, features = [ "std" ] }
|
||||
ark-serialize = { version = "=0.4.1", default-features = false }
|
||||
ark-circom = { version = "=0.1.0", default-features = false, features = ["circom-2"] }
|
||||
ark-circom = { git = "https://github.com/gakonst/ark-circom", default-features = false, features = ["circom-2"] }
|
||||
|
||||
# WASM
|
||||
wasmer = { version = "=2.3.0", default-features = false }
|
||||
wasmer = { version = "2.3.0", default-features = false }
|
||||
|
||||
# error handling
|
||||
color-eyre = "=0.6.2"
|
||||
thiserror = "=1.0.39"
|
||||
thiserror = "=1.0.38"
|
||||
|
||||
# utilities
|
||||
cfg-if = "=1.0"
|
||||
num-bigint = { version = "=0.4.3", default-features = false, features = ["rand"] }
|
||||
num-traits = "=0.2.15"
|
||||
once_cell = "=1.17.1"
|
||||
num-traits = "0.2.11"
|
||||
once_cell = "1.14.0"
|
||||
rand = "=0.8.5"
|
||||
rand_chacha = "=0.3.1"
|
||||
tiny-keccak = { version = "=2.0.2", features = ["keccak"] }
|
||||
utils = { package = "zerokit_utils", version = "=0.1.0", path = "../utils/", default-features = false }
|
||||
utils = { path = "../utils/", default-features = false }
|
||||
|
||||
# serialization
|
||||
serde_json = "=1.0.96"
|
||||
serde = { version = "=1.0.163", features = ["derive"] }
|
||||
serde_json = "1.0.48"
|
||||
serde = { version = "1.0.130", features = ["derive"] }
|
||||
|
||||
include_dir = "=0.7.3"
|
||||
|
||||
[dev-dependencies]
|
||||
sled = "=0.34.7"
|
||||
criterion = { version = "=0.4.0", features = ["html_reports"] }
|
||||
|
||||
[features]
|
||||
default = ["parallel", "wasmer/sys-default", "pmtree-ft"]
|
||||
@@ -59,7 +56,3 @@ fullmerkletree = ["default"]
|
||||
|
||||
# Note: pmtree feature is still experimental
|
||||
pmtree-ft = ["utils/pmtree-ft"]
|
||||
|
||||
[[bench]]
|
||||
name = "pmtree_benchmark"
|
||||
harness = false
|
||||
|
||||
@@ -5,7 +5,3 @@ args = ["build", "--release"]
|
||||
[tasks.test]
|
||||
command = "cargo"
|
||||
args = ["test", "--release"]
|
||||
|
||||
[tasks.bench]
|
||||
command = "cargo"
|
||||
args = ["bench"]
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
use criterion::{criterion_group, criterion_main, Criterion};
|
||||
use utils::ZerokitMerkleTree;
|
||||
|
||||
use rln::{circuit::Fr, pm_tree_adapter::PmTree};
|
||||
|
||||
pub fn pmtree_benchmark(c: &mut Criterion) {
|
||||
let mut tree = PmTree::default(2).unwrap();
|
||||
|
||||
let leaves: Vec<Fr> = (0..4).map(|s| Fr::from(s)).collect();
|
||||
|
||||
c.bench_function("Pmtree::set", |b| {
|
||||
b.iter(|| {
|
||||
tree.set(0, leaves[0]).unwrap();
|
||||
})
|
||||
});
|
||||
|
||||
c.bench_function("Pmtree:delete", |b| {
|
||||
b.iter(|| {
|
||||
tree.delete(0).unwrap();
|
||||
})
|
||||
});
|
||||
|
||||
c.bench_function("Pmtree::override_range", |b| {
|
||||
b.iter(|| {
|
||||
tree.override_range(0, leaves.clone(), [0, 1, 2, 3])
|
||||
.unwrap();
|
||||
})
|
||||
});
|
||||
|
||||
c.bench_function("Pmtree::compute_root", |b| {
|
||||
b.iter(|| {
|
||||
tree.compute_root().unwrap();
|
||||
})
|
||||
});
|
||||
|
||||
c.bench_function("Pmtree::get", |b| {
|
||||
b.iter(|| {
|
||||
tree.get(0).unwrap();
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
criterion_group!(benches, pmtree_benchmark);
|
||||
criterion_main!(benches);
|
||||
@@ -222,12 +222,6 @@ pub extern "C" fn set_leaf(ctx: *mut RLN, index: usize, input_buffer: *const Buf
|
||||
call!(ctx, set_leaf, index, input_buffer)
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn get_leaf(ctx: *mut RLN, index: usize, output_buffer: *mut Buffer) -> bool {
|
||||
call_with_output_arg!(ctx, get_leaf, output_buffer, index)
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn set_next_leaf(ctx: *mut RLN, input_buffer: *const Buffer) -> bool {
|
||||
@@ -385,28 +379,6 @@ pub extern "C" fn recover_id_secret(
|
||||
)
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////
|
||||
// Persistent metadata APIs
|
||||
////////////////////////////////////////////////////////
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn set_metadata(ctx: *mut RLN, input_buffer: *const Buffer) -> bool {
|
||||
call!(ctx, set_metadata, input_buffer)
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn get_metadata(ctx: *const RLN, output_buffer: *mut Buffer) -> bool {
|
||||
call_with_output_arg!(ctx, get_metadata, output_buffer)
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn flush(ctx: *mut RLN) -> bool {
|
||||
call!(ctx, flush)
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn hash(input_buffer: *const Buffer, output_buffer: *mut Buffer) -> bool {
|
||||
|
||||
@@ -7,15 +7,11 @@ use std::collections::HashSet;
|
||||
use std::fmt::Debug;
|
||||
use std::path::PathBuf;
|
||||
use std::str::FromStr;
|
||||
use utils::pmtree::{Database, Hasher};
|
||||
use utils::pmtree::Hasher;
|
||||
use utils::*;
|
||||
|
||||
const METADATA_KEY: [u8; 8] = *b"metadata";
|
||||
|
||||
pub struct PmTree {
|
||||
tree: pmtree::MerkleTree<SledDB, PoseidonHash>,
|
||||
// metadata that an application may use to store additional information
|
||||
metadata: Vec<u8>,
|
||||
}
|
||||
|
||||
pub struct PmTreeProof {
|
||||
@@ -62,36 +58,13 @@ impl FromStr for PmtreeConfig {
|
||||
fn from_str(s: &str) -> Result<Self> {
|
||||
let config: Value = serde_json::from_str(s)?;
|
||||
|
||||
let temporary = config["temporary"].as_bool();
|
||||
let path = config["path"].as_str();
|
||||
let path = path.map(PathBuf::from);
|
||||
let temporary = config["temporary"].as_bool();
|
||||
let cache_capacity = config["cache_capacity"].as_u64();
|
||||
let flush_every_ms = config["flush_every_ms"].as_u64();
|
||||
let mode = match config["mode"].as_str() {
|
||||
Some("HighThroughput") => Mode::HighThroughput,
|
||||
Some("LowSpace") => Mode::LowSpace,
|
||||
_ => Mode::HighThroughput,
|
||||
};
|
||||
let use_compression = config["use_compression"].as_bool();
|
||||
|
||||
if temporary.is_some()
|
||||
&& path.is_some()
|
||||
&& temporary.unwrap()
|
||||
&& path.as_ref().unwrap().exists()
|
||||
{
|
||||
return Err(Report::msg(format!(
|
||||
"Path {:?} already exists, cannot use temporary",
|
||||
path.unwrap()
|
||||
)));
|
||||
}
|
||||
|
||||
let config = pm_tree::Config::new()
|
||||
.temporary(temporary.unwrap_or(get_tmp()))
|
||||
.path(path.unwrap_or(get_tmp_path()))
|
||||
.cache_capacity(cache_capacity.unwrap_or(1024 * 1024 * 1024))
|
||||
.flush_every_ms(flush_every_ms)
|
||||
.mode(mode)
|
||||
.use_compression(use_compression.unwrap_or(false));
|
||||
.path(path.unwrap_or(get_tmp_path()));
|
||||
Ok(PmtreeConfig(config))
|
||||
}
|
||||
}
|
||||
@@ -99,15 +72,7 @@ impl FromStr for PmtreeConfig {
|
||||
impl Default for PmtreeConfig {
|
||||
fn default() -> Self {
|
||||
let tmp_path = get_tmp_path();
|
||||
PmtreeConfig(
|
||||
pm_tree::Config::new()
|
||||
.temporary(true)
|
||||
.path(tmp_path)
|
||||
.cache_capacity(150_000)
|
||||
.mode(Mode::HighThroughput)
|
||||
.use_compression(false)
|
||||
.flush_every_ms(Some(12_000)),
|
||||
)
|
||||
PmtreeConfig(pm_tree::Config::new().temporary(true).path(tmp_path))
|
||||
}
|
||||
}
|
||||
impl Debug for PmtreeConfig {
|
||||
@@ -139,14 +104,7 @@ impl ZerokitMerkleTree for PmTree {
|
||||
Err(_) => pmtree::MerkleTree::new(depth, config.0)?,
|
||||
};
|
||||
|
||||
Ok(PmTree {
|
||||
tree,
|
||||
metadata: Vec::new(),
|
||||
})
|
||||
}
|
||||
|
||||
fn close_db_connection(&mut self) -> Result<()> {
|
||||
self.tree.db.close().map_err(|e| Report::msg(e.to_string()))
|
||||
Ok(PmTree { tree })
|
||||
}
|
||||
|
||||
fn depth(&self) -> usize {
|
||||
@@ -249,25 +207,6 @@ impl ZerokitMerkleTree for PmTree {
|
||||
fn compute_root(&mut self) -> Result<FrOf<Self::Hasher>> {
|
||||
Ok(self.tree.root())
|
||||
}
|
||||
|
||||
fn set_metadata(&mut self, metadata: &[u8]) -> Result<()> {
|
||||
self.tree.db.put(METADATA_KEY, metadata.to_vec())?;
|
||||
self.metadata = metadata.to_vec();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn metadata(&self) -> Result<Vec<u8>> {
|
||||
if !self.metadata.is_empty() {
|
||||
return Ok(self.metadata.clone());
|
||||
}
|
||||
// if empty, try searching the db
|
||||
let data = self.tree.db.get(METADATA_KEY)?;
|
||||
|
||||
if data.is_none() {
|
||||
return Err(Report::msg("metadata does not exist"));
|
||||
}
|
||||
Ok(data.unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
impl ZerokitMerkleProof for PmTreeProof {
|
||||
|
||||
@@ -12,8 +12,10 @@ use ark_serialize::{CanonicalDeserialize, CanonicalSerialize, Read, Write};
|
||||
use cfg_if::cfg_if;
|
||||
use color_eyre::{Report, Result};
|
||||
use num_bigint::BigInt;
|
||||
use serde_json::{json, Value};
|
||||
use std::io::Cursor;
|
||||
use utils::{ZerokitMerkleProof, ZerokitMerkleTree};
|
||||
use std::str::FromStr;
|
||||
use utils::{Hasher, ZerokitMerkleProof, ZerokitMerkleTree};
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(not(target_arch = "wasm32"))] {
|
||||
@@ -21,9 +23,6 @@ cfg_if! {
|
||||
use std::sync::Mutex;
|
||||
use crate::circuit::{circom_from_folder, vk_from_folder, circom_from_raw, zkey_from_folder, TEST_RESOURCES_FOLDER, TEST_TREE_HEIGHT};
|
||||
use ark_circom::WitnessCalculator;
|
||||
use serde_json::{json, Value};
|
||||
use utils::{Hasher};
|
||||
use std::str::FromStr;
|
||||
} else {
|
||||
use std::marker::*;
|
||||
}
|
||||
@@ -80,17 +79,18 @@ impl RLN<'_> {
|
||||
let resources_folder = rln_config["resources_folder"]
|
||||
.as_str()
|
||||
.unwrap_or(TEST_RESOURCES_FOLDER);
|
||||
let tree_config = rln_config["tree_config"].to_string();
|
||||
let tree_config_opt = rln_config["tree_config"].as_str();
|
||||
|
||||
let witness_calculator = circom_from_folder(resources_folder)?;
|
||||
|
||||
let proving_key = zkey_from_folder(resources_folder)?;
|
||||
let verification_key = vk_from_folder(resources_folder)?;
|
||||
|
||||
let tree_config: <PoseidonTree as ZerokitMerkleTree>::Config = if tree_config.is_empty() {
|
||||
<PoseidonTree as ZerokitMerkleTree>::Config::default()
|
||||
} else {
|
||||
<PoseidonTree as ZerokitMerkleTree>::Config::from_str(&tree_config)?
|
||||
let tree_config: <PoseidonTree as ZerokitMerkleTree>::Config = match tree_config_opt {
|
||||
Some(tree_config_str) => {
|
||||
<PoseidonTree as ZerokitMerkleTree>::Config::from_str(tree_config_str)?
|
||||
}
|
||||
None => <PoseidonTree as ZerokitMerkleTree>::Config::default(),
|
||||
};
|
||||
|
||||
// We compute a default empty tree
|
||||
@@ -217,31 +217,6 @@ impl RLN<'_> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Gets a leaf value at position index in the internal Merkle tree.
|
||||
/// The leaf value is written to output_data.
|
||||
/// Input values are:
|
||||
/// - `index`: the index of the leaf
|
||||
///
|
||||
/// Example:
|
||||
/// ```
|
||||
/// use crate::protocol::*;
|
||||
/// use std::io::Cursor;
|
||||
///
|
||||
/// let id_index = 10;
|
||||
/// let mut buffer = Cursor::new(Vec::<u8>::new());
|
||||
/// rln.get_leaf(id_index, &mut buffer).unwrap();
|
||||
/// let id_commitment = deserialize_field_element(&buffer.into_inner()).unwrap();
|
||||
pub fn get_leaf<W: Write>(&self, index: usize, mut output_data: W) -> Result<()> {
|
||||
// We get the leaf at input index
|
||||
let leaf = self.tree.get(index)?;
|
||||
|
||||
// We serialize the leaf and write it to output
|
||||
let leaf_byte = fr_to_bytes_le(&leaf);
|
||||
output_data.write_all(&leaf_byte)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Sets multiple leaves starting from position index in the internal Merkle tree.
|
||||
///
|
||||
/// If n leaves are passed as input, these will be set at positions `index`, `index+1`, ..., `index+n-1` respectively.
|
||||
@@ -437,43 +412,6 @@ impl RLN<'_> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Sets some metadata that a consuming application may want to store in the RLN object.
|
||||
/// This metadata is not used by the RLN module.
|
||||
///
|
||||
/// Input values are:
|
||||
/// - `metadata`: a byte vector containing the metadata
|
||||
///
|
||||
/// Example
|
||||
///
|
||||
/// ```
|
||||
/// let metadata = b"some metadata";
|
||||
/// rln.set_metadata(metadata).unwrap();
|
||||
/// ```
|
||||
pub fn set_metadata(&mut self, metadata: &[u8]) -> Result<()> {
|
||||
self.tree.set_metadata(metadata)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns the metadata stored in the RLN object.
|
||||
///
|
||||
/// Output values are:
|
||||
/// - `output_data`: a writer receiving the serialization of the metadata
|
||||
///
|
||||
/// Example
|
||||
///
|
||||
/// ```
|
||||
/// use std::io::Cursor;
|
||||
///
|
||||
/// let mut buffer = Cursor::new(Vec::<u8>::new());
|
||||
/// rln.get_metadata(&mut buffer).unwrap();
|
||||
/// let metadata = buffer.into_inner();
|
||||
/// ```
|
||||
pub fn get_metadata<W: Write>(&self, mut output_data: W) -> Result<()> {
|
||||
let metadata = self.tree.metadata()?;
|
||||
output_data.write_all(&metadata)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns the Merkle tree root
|
||||
///
|
||||
/// Output values are:
|
||||
@@ -1129,14 +1067,6 @@ impl RLN<'_> {
|
||||
let (rln_witness, _) = deserialize_witness(serialized_witness)?;
|
||||
get_json_inputs(&rln_witness)
|
||||
}
|
||||
|
||||
/// Closes the connection to the Merkle tree database.
|
||||
/// This function should be called before the RLN object is dropped.
|
||||
/// If not called, the connection will be closed when the RLN object is dropped.
|
||||
/// This improves robustness of the tree.
|
||||
pub fn flush(&mut self) -> Result<()> {
|
||||
self.tree.close_db_connection()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
@@ -1386,8 +1316,6 @@ mod test {
|
||||
let (root_single_additions, _) = bytes_le_to_fr(&buffer.into_inner());
|
||||
|
||||
assert_eq!(root_batch_with_init, root_single_additions);
|
||||
|
||||
rln.flush();
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1901,50 +1829,4 @@ mod test {
|
||||
// We ensure that an empty value was written to output_buffer, i.e. no secret is recovered
|
||||
assert!(serialized_identity_secret_hash.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_leaf() {
|
||||
// We generate a random tree
|
||||
let tree_height = 10;
|
||||
let mut rng = thread_rng();
|
||||
let input_buffer =
|
||||
Cursor::new(json!({ "resources_folder": TEST_RESOURCES_FOLDER }).to_string());
|
||||
let mut rln = RLN::new(tree_height, input_buffer).unwrap();
|
||||
|
||||
// We generate a random leaf
|
||||
let leaf = Fr::rand(&mut rng);
|
||||
|
||||
// We generate a random index
|
||||
let index = rng.gen_range(0..rln.tree.capacity());
|
||||
|
||||
// We add the leaf to the tree
|
||||
let mut buffer = Cursor::new(fr_to_bytes_le(&leaf));
|
||||
rln.set_leaf(index, &mut buffer).unwrap();
|
||||
|
||||
// We get the leaf
|
||||
let mut output_buffer = Cursor::new(Vec::<u8>::new());
|
||||
rln.get_leaf(index, &mut output_buffer).unwrap();
|
||||
|
||||
// We ensure that the leaf is the same as the one we added
|
||||
let (received_leaf, _) = bytes_le_to_fr(output_buffer.into_inner().as_ref());
|
||||
assert_eq!(received_leaf, leaf);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_metadata() {
|
||||
let tree_height = TEST_TREE_HEIGHT;
|
||||
|
||||
let input_buffer =
|
||||
Cursor::new(json!({ "resources_folder": TEST_RESOURCES_FOLDER }).to_string());
|
||||
let mut rln = RLN::new(tree_height, input_buffer).unwrap();
|
||||
|
||||
let arbitrary_metadata: &[u8] = b"block_number:200000";
|
||||
rln.set_metadata(arbitrary_metadata).unwrap();
|
||||
|
||||
let mut buffer = Cursor::new(Vec::<u8>::new());
|
||||
rln.get_metadata(&mut buffer).unwrap();
|
||||
let received_metadata = buffer.into_inner();
|
||||
|
||||
assert_eq!(arbitrary_metadata, received_metadata);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1156,77 +1156,4 @@ mod test {
|
||||
|
||||
assert_eq!(received_hash, expected_hash);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_leaf() {
|
||||
// We create a RLN instance
|
||||
let tree_height = TEST_TREE_HEIGHT;
|
||||
let no_of_leaves = 1 << TEST_TREE_HEIGHT;
|
||||
|
||||
let mut rln_pointer = MaybeUninit::<*mut RLN>::uninit();
|
||||
let input_config = json!({ "resources_folder": TEST_RESOURCES_FOLDER }).to_string();
|
||||
let input_buffer = &Buffer::from(input_config.as_bytes());
|
||||
let success = new(tree_height, input_buffer, rln_pointer.as_mut_ptr());
|
||||
assert!(success, "RLN object creation failed");
|
||||
let rln_pointer = unsafe { &mut *rln_pointer.assume_init() };
|
||||
|
||||
// We generate a new identity tuple from an input seed
|
||||
let seed_bytes: &[u8] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||
let input_buffer = &Buffer::from(seed_bytes);
|
||||
let mut output_buffer = MaybeUninit::<Buffer>::uninit();
|
||||
let success =
|
||||
seeded_extended_key_gen(rln_pointer, input_buffer, output_buffer.as_mut_ptr());
|
||||
assert!(success, "seeded key gen call failed");
|
||||
|
||||
let output_buffer = unsafe { output_buffer.assume_init() };
|
||||
let result_data = <&[u8]>::from(&output_buffer).to_vec();
|
||||
let (_, _, _, id_commitment) = deserialize_identity_tuple(result_data);
|
||||
|
||||
// We insert the id_commitment into the tree at a random index
|
||||
let mut rng = thread_rng();
|
||||
let index = rng.gen_range(0..no_of_leaves) as usize;
|
||||
let leaf = fr_to_bytes_le(&id_commitment);
|
||||
let input_buffer = &Buffer::from(leaf.as_ref());
|
||||
let success = set_leaf(rln_pointer, index, input_buffer);
|
||||
assert!(success, "set leaf call failed");
|
||||
|
||||
// We get the leaf at the same index
|
||||
let mut output_buffer = MaybeUninit::<Buffer>::uninit();
|
||||
let success = get_leaf(rln_pointer, index, output_buffer.as_mut_ptr());
|
||||
assert!(success, "get leaf call failed");
|
||||
let output_buffer = unsafe { output_buffer.assume_init() };
|
||||
let result_data = <&[u8]>::from(&output_buffer).to_vec();
|
||||
let (received_id_commitment, _) = bytes_le_to_fr(&result_data);
|
||||
|
||||
// We check that the received id_commitment is the same as the one we inserted
|
||||
assert_eq!(received_id_commitment, id_commitment);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_metadata() {
|
||||
// We create a RLN instance
|
||||
let tree_height = TEST_TREE_HEIGHT;
|
||||
|
||||
let mut rln_pointer = MaybeUninit::<*mut RLN>::uninit();
|
||||
let input_config = json!({ "resources_folder": TEST_RESOURCES_FOLDER }).to_string();
|
||||
let input_buffer = &Buffer::from(input_config.as_bytes());
|
||||
let success = new(tree_height, input_buffer, rln_pointer.as_mut_ptr());
|
||||
assert!(success, "RLN object creation failed");
|
||||
let rln_pointer = unsafe { &mut *rln_pointer.assume_init() };
|
||||
|
||||
let seed_bytes: &[u8] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||
let input_buffer = &Buffer::from(seed_bytes);
|
||||
|
||||
let success = set_metadata(rln_pointer, input_buffer);
|
||||
assert!(success, "set_metadata call failed");
|
||||
|
||||
let mut output_buffer = MaybeUninit::<Buffer>::uninit();
|
||||
let success = get_metadata(rln_pointer, output_buffer.as_mut_ptr());
|
||||
assert!(success, "get_metadata call failed");
|
||||
|
||||
let output_buffer = unsafe { output_buffer.assume_init() };
|
||||
let result_data = <&[u8]>::from(&output_buffer).to_vec();
|
||||
|
||||
assert_eq!(result_data, seed_bytes.to_vec());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ color-eyre = "0.6.1"
|
||||
once_cell = "1.8"
|
||||
rand = "0.8.4"
|
||||
semaphore = { git = "https://github.com/worldcoin/semaphore-rs", rev = "ee658c2"}
|
||||
ethers-core = { version = "2.0.8", default-features = false }
|
||||
ethers-core = { git = "https://github.com/gakonst/ethers-rs", default-features = false }
|
||||
ruint = { version = "1.2.0", features = [ "serde", "num-bigint", "ark-ff" ] }
|
||||
serde = "1.0"
|
||||
thiserror = "1.0.0"
|
||||
|
||||
@@ -1,27 +1,24 @@
|
||||
[package]
|
||||
name = "zerokit_utils"
|
||||
name = "utils"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
license = "MIT OR Apache-2.0"
|
||||
description = "Various utilities for Zerokit"
|
||||
|
||||
[lib]
|
||||
bench = false
|
||||
|
||||
[dependencies]
|
||||
ark-ff = { version = "=0.4.1", default-features = false, features = ["asm"] }
|
||||
num-bigint = { version = "=0.4.3", default-features = false, features = ["rand"] }
|
||||
color-eyre = "=0.6.2"
|
||||
pmtree = { package = "vacp2p_pmtree", version = "=1.0.0", optional = true}
|
||||
pmtree = { git = "https://github.com/Rate-Limiting-Nullifier/pmtree", rev = "bf27d4273b34a7f4ec3c77cdf67fb17a5602504a", optional = true}
|
||||
sled = "=0.34.7"
|
||||
serde = "=1.0.163"
|
||||
serde = "1.0.44"
|
||||
|
||||
[dev-dependencies]
|
||||
ark-bn254 = "=0.4.0"
|
||||
num-traits = "=0.2.15"
|
||||
hex-literal = "=0.3.4"
|
||||
tiny-keccak = { version = "=2.0.2", features = ["keccak"] }
|
||||
criterion = { version = "=0.4.0", features = ["html_reports"] }
|
||||
num-traits = "0.2.11"
|
||||
hex-literal = "0.3.4"
|
||||
tiny-keccak = { version = "2.0.2", features = ["keccak"] }
|
||||
criterion = { version = "0.4", features = ["html_reports"] }
|
||||
|
||||
[features]
|
||||
default = ["parallel"]
|
||||
|
||||
@@ -5,7 +5,3 @@ args = ["build", "--release"]
|
||||
[tasks.test]
|
||||
command = "cargo"
|
||||
args = ["test", "--release"]
|
||||
|
||||
[tasks.bench]
|
||||
command = "cargo"
|
||||
args = ["bench"]
|
||||
@@ -1,7 +1,7 @@
|
||||
use criterion::{criterion_group, criterion_main, Criterion};
|
||||
use hex_literal::hex;
|
||||
use tiny_keccak::{Hasher as _, Keccak};
|
||||
use zerokit_utils::{
|
||||
use utils::{
|
||||
FullMerkleConfig, FullMerkleTree, Hasher, OptimalMerkleConfig, OptimalMerkleTree,
|
||||
ZerokitMerkleTree,
|
||||
};
|
||||
|
||||
@@ -29,9 +29,6 @@ pub struct FullMerkleTree<H: Hasher> {
|
||||
// The next available (i.e., never used) tree index. Equivalently, the number of leaves added to the tree
|
||||
// (deletions leave next_index unchanged)
|
||||
next_index: usize,
|
||||
|
||||
// metadata that an application may use to store additional information
|
||||
metadata: Vec<u8>,
|
||||
}
|
||||
|
||||
/// Element of a Merkle proof
|
||||
@@ -97,14 +94,9 @@ where
|
||||
cached_nodes,
|
||||
nodes,
|
||||
next_index,
|
||||
metadata: Vec::new(),
|
||||
})
|
||||
}
|
||||
|
||||
fn close_db_connection(&mut self) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Returns the depth of the tree
|
||||
fn depth(&self) -> usize {
|
||||
self.depth
|
||||
@@ -242,15 +234,6 @@ where
|
||||
fn compute_root(&mut self) -> Result<FrOf<Self::Hasher>> {
|
||||
Ok(self.root())
|
||||
}
|
||||
|
||||
fn set_metadata(&mut self, metadata: &[u8]) -> Result<()> {
|
||||
self.metadata = metadata.to_vec();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn metadata(&self) -> Result<Vec<u8>> {
|
||||
Ok(self.metadata.to_vec())
|
||||
}
|
||||
}
|
||||
|
||||
impl<H: Hasher> FullMerkleTree<H>
|
||||
|
||||
@@ -63,9 +63,6 @@ pub trait ZerokitMerkleTree {
|
||||
fn delete(&mut self, index: usize) -> Result<()>;
|
||||
fn proof(&self, index: usize) -> Result<Self::Proof>;
|
||||
fn verify(&self, leaf: &FrOf<Self::Hasher>, witness: &Self::Proof) -> Result<bool>;
|
||||
fn set_metadata(&mut self, metadata: &[u8]) -> Result<()>;
|
||||
fn metadata(&self) -> Result<Vec<u8>>;
|
||||
fn close_db_connection(&mut self) -> Result<()>;
|
||||
}
|
||||
|
||||
pub trait ZerokitMerkleProof {
|
||||
|
||||
@@ -30,9 +30,6 @@ where
|
||||
// The next available (i.e., never used) tree index. Equivalently, the number of leaves added to the tree
|
||||
// (deletions leave next_index unchanged)
|
||||
next_index: usize,
|
||||
|
||||
// metadata that an application may use to store additional information
|
||||
metadata: Vec<u8>,
|
||||
}
|
||||
|
||||
/// The Merkle proof
|
||||
@@ -79,14 +76,9 @@ where
|
||||
depth,
|
||||
nodes: HashMap::new(),
|
||||
next_index: 0,
|
||||
metadata: Vec::new(),
|
||||
})
|
||||
}
|
||||
|
||||
fn close_db_connection(&mut self) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Returns the depth of the tree
|
||||
fn depth(&self) -> usize {
|
||||
self.depth
|
||||
@@ -224,15 +216,6 @@ where
|
||||
self.recalculate_from(0)?;
|
||||
Ok(self.root())
|
||||
}
|
||||
|
||||
fn set_metadata(&mut self, metadata: &[u8]) -> Result<()> {
|
||||
self.metadata = metadata.to_vec();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn metadata(&self) -> Result<Vec<u8>> {
|
||||
Ok(self.metadata.to_vec())
|
||||
}
|
||||
}
|
||||
|
||||
impl<H: Hasher> OptimalMerkleTree<H>
|
||||
|
||||
@@ -45,15 +45,6 @@ impl Database for SledDB {
|
||||
Ok(SledDB(db))
|
||||
}
|
||||
|
||||
fn close(&mut self) -> PmtreeResult<()> {
|
||||
let _ = self.0.flush().map_err(|_| {
|
||||
PmtreeErrorKind::DatabaseError(DatabaseErrorKind::CustomError(
|
||||
"Cannot flush database".to_string(),
|
||||
))
|
||||
})?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get(&self, key: DBKey) -> PmtreeResult<Option<Value>> {
|
||||
match self.0.get(key) {
|
||||
Ok(value) => Ok(value.map(|val| val.to_vec())),
|
||||
|
||||
@@ -36,12 +36,20 @@ impl PoseidonGrainLFSR {
|
||||
assert!(is_field == 1);
|
||||
|
||||
// b0, b1 describes the field
|
||||
state[1] = is_field == 1;
|
||||
if is_field == 1 {
|
||||
state[1] = true;
|
||||
} else {
|
||||
state[1] = false;
|
||||
}
|
||||
|
||||
assert!(is_sbox_an_inverse == 0 || is_sbox_an_inverse == 1);
|
||||
|
||||
// b2, ..., b5 describes the S-BOX
|
||||
state[5] = is_sbox_an_inverse == 1;
|
||||
if is_sbox_an_inverse == 1 {
|
||||
state[5] = true;
|
||||
} else {
|
||||
state[5] = false;
|
||||
}
|
||||
|
||||
// b6, ..., b17 are the binary representation of n (prime_num_bits)
|
||||
{
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
mod test {
|
||||
use hex_literal::hex;
|
||||
use tiny_keccak::{Hasher as _, Keccak};
|
||||
use zerokit_utils::{
|
||||
use utils::{
|
||||
FullMerkleConfig, FullMerkleTree, Hasher, OptimalMerkleConfig, OptimalMerkleTree,
|
||||
ZerokitMerkleProof, ZerokitMerkleTree,
|
||||
};
|
||||
|
||||
@@ -3,7 +3,7 @@ mod test {
|
||||
use ark_bn254::Fr;
|
||||
use num_bigint::BigUint;
|
||||
use num_traits::Num;
|
||||
use zerokit_utils::poseidon_hash::Poseidon;
|
||||
use utils::poseidon_hash::Poseidon;
|
||||
|
||||
const ROUND_PARAMS: [(usize, usize, usize, usize); 8] = [
|
||||
(2, 8, 56, 0),
|
||||
|
||||
Reference in New Issue
Block a user