mirror of
https://github.com/vacp2p/zerokit.git
synced 2026-01-09 13:47:58 -05:00
Compare commits
54 Commits
v0.6.0
...
test-tree-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b5afb847f1 | ||
|
|
a7d58926e4 | ||
|
|
e160ac2524 | ||
|
|
eb8eedfdb4 | ||
|
|
c78f1f1534 | ||
|
|
4d62a4d60d | ||
|
|
57b694db5d | ||
|
|
fd568c17b3 | ||
|
|
cf845c6a74 | ||
|
|
fe566b3314 | ||
|
|
0b00c639a0 | ||
|
|
7c801a804e | ||
|
|
9da80dd807 | ||
|
|
bcbd6a97af | ||
|
|
6965cf2852 | ||
|
|
578e0507b3 | ||
|
|
bf1e184da9 | ||
|
|
4473688efa | ||
|
|
c80569d518 | ||
|
|
fd99b6af74 | ||
|
|
65f53e3da3 | ||
|
|
042f8a9739 | ||
|
|
baf474e747 | ||
|
|
dc0b31752c | ||
|
|
36013bf4ba | ||
|
|
211b2d4830 | ||
|
|
5f4bcb74ce | ||
|
|
de5fd36add | ||
|
|
19c0f551c8 | ||
|
|
4133f1f8c3 | ||
|
|
149096f7a6 | ||
|
|
7023e85fce | ||
|
|
a4cafa6adc | ||
|
|
4077357e3f | ||
|
|
84d9799d09 | ||
|
|
c576af8e62 | ||
|
|
81470b9678 | ||
|
|
9d4198c205 | ||
|
|
c60e0c33fc | ||
|
|
ba467d370c | ||
|
|
ffd5851d7d | ||
|
|
759d312680 | ||
|
|
fb0ffd74a3 | ||
|
|
9d8372be39 | ||
|
|
de9c0d5072 | ||
|
|
5c60ec7cce | ||
|
|
8793965650 | ||
|
|
1930ca1610 | ||
|
|
4b4169d7a7 | ||
|
|
8a3e33be41 | ||
|
|
7bb2444ba4 | ||
|
|
00f8d039a8 | ||
|
|
e39f156fff | ||
|
|
8b04930583 |
5
.github/labels.yml
vendored
5
.github/labels.yml
vendored
@@ -90,11 +90,6 @@
|
||||
description: go-waku-productionization track (Waku Product)
|
||||
color: 9DEA79
|
||||
|
||||
# Tracks within zk-WASM project
|
||||
- name: track:kickoff
|
||||
description: Kickoff track (zk-WASM)
|
||||
color: 06B6C8
|
||||
|
||||
# Tracks within RAD project
|
||||
- name: track:waku-specs
|
||||
description: Waku specs track (RAD)
|
||||
|
||||
189
.github/workflows/ci.yml
vendored
189
.github/workflows/ci.yml
vendored
@@ -9,7 +9,9 @@ on:
|
||||
- "!rln/src/**"
|
||||
- "!rln/resources/**"
|
||||
- "!utils/src/**"
|
||||
- "!rln-wasm-utils/**"
|
||||
pull_request:
|
||||
types: [opened, synchronize, reopened, ready_for_review]
|
||||
paths-ignore:
|
||||
- "**.md"
|
||||
- "!.github/workflows/*.yml"
|
||||
@@ -17,140 +19,191 @@ on:
|
||||
- "!rln/src/**"
|
||||
- "!rln/resources/**"
|
||||
- "!utils/src/**"
|
||||
- "!rln-wasm-utils/**"
|
||||
|
||||
name: Tests
|
||||
name: CI
|
||||
|
||||
jobs:
|
||||
utils-test:
|
||||
# skip tests on draft PRs
|
||||
if: github.event_name == 'push' || (github.event_name == 'pull_request' && !github.event.pull_request.draft)
|
||||
strategy:
|
||||
matrix:
|
||||
platform: [ ubuntu-latest, macos-latest ]
|
||||
crate: [ utils ]
|
||||
platform: [ubuntu-latest, macos-latest]
|
||||
crate: [utils]
|
||||
runs-on: ${{ matrix.platform }}
|
||||
timeout-minutes: 60
|
||||
|
||||
name: test - ${{ matrix.crate }} - ${{ matrix.platform }}
|
||||
name: Test - ${{ matrix.crate }} - ${{ matrix.platform }}
|
||||
steps:
|
||||
- name: Checkout sources
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v4
|
||||
- name: Install stable toolchain
|
||||
uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
profile: minimal
|
||||
toolchain: stable
|
||||
override: true
|
||||
uses: dtolnay/rust-toolchain@stable
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
- name: Install dependencies
|
||||
run: make installdeps
|
||||
- name: cargo-make test
|
||||
- name: Test utils
|
||||
run: |
|
||||
cargo make test --release
|
||||
working-directory: ${{ matrix.crate }}
|
||||
|
||||
rln-test:
|
||||
# skip tests on draft PRs
|
||||
if: github.event_name == 'push' || (github.event_name == 'pull_request' && !github.event.pull_request.draft)
|
||||
strategy:
|
||||
matrix:
|
||||
platform: [ ubuntu-latest, macos-latest ]
|
||||
crate: [ rln ]
|
||||
feature: [ "default", "arkzkey", "stateless" ]
|
||||
platform: [ubuntu-latest, macos-latest]
|
||||
crate: [rln]
|
||||
feature: ["default", "stateless"]
|
||||
runs-on: ${{ matrix.platform }}
|
||||
timeout-minutes: 60
|
||||
|
||||
name: test - ${{ matrix.crate }} - ${{ matrix.platform }} - ${{ matrix.feature }}
|
||||
name: Test - ${{ matrix.crate }} - ${{ matrix.platform }} - ${{ matrix.feature }}
|
||||
steps:
|
||||
- name: Checkout sources
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v4
|
||||
- name: Install stable toolchain
|
||||
uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
profile: minimal
|
||||
toolchain: stable
|
||||
override: true
|
||||
uses: dtolnay/rust-toolchain@stable
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
- name: Install dependencies
|
||||
run: make installdeps
|
||||
- name: cargo-make test
|
||||
- name: Test rln
|
||||
run: |
|
||||
cargo make test_${{ matrix.feature }} --release
|
||||
if [ ${{ matrix.feature }} == default ]; then
|
||||
cargo make test --release
|
||||
else
|
||||
cargo make test_${{ matrix.feature }} --release
|
||||
fi
|
||||
working-directory: ${{ matrix.crate }}
|
||||
|
||||
rln-wasm:
|
||||
rln-wasm-test:
|
||||
# skip tests on draft PRs
|
||||
if: github.event_name == 'push' || (github.event_name == 'pull_request' && !github.event.pull_request.draft)
|
||||
strategy:
|
||||
matrix:
|
||||
platform: [ ubuntu-latest, macos-latest ]
|
||||
platform: [ubuntu-latest, macos-latest]
|
||||
crate: [rln-wasm]
|
||||
feature: ["default"]
|
||||
runs-on: ${{ matrix.platform }}
|
||||
timeout-minutes: 60
|
||||
|
||||
name: test - rln-wasm - ${{ matrix.platform }}
|
||||
name: Test - ${{ matrix.crate }} - ${{ matrix.platform }} - ${{ matrix.feature }}
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/checkout@v4
|
||||
- name: Install stable toolchain
|
||||
uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
profile: minimal
|
||||
toolchain: stable
|
||||
override: true
|
||||
uses: dtolnay/rust-toolchain@stable
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
- name: Install Dependencies
|
||||
- name: Install dependencies
|
||||
run: make installdeps
|
||||
- name: Install wasm-pack
|
||||
uses: jetli/wasm-pack-action@v0.3.0
|
||||
- run: cargo make build
|
||||
working-directory: rln-wasm
|
||||
- run: cargo make test --release
|
||||
working-directory: rln-wasm
|
||||
- name: Build rln-wasm
|
||||
run: cargo make build
|
||||
working-directory: ${{ matrix.crate }}
|
||||
- name: Test rln-wasm on node
|
||||
run: cargo make test --release
|
||||
working-directory: ${{ matrix.crate }}
|
||||
- name: Test rln-wasm on browser
|
||||
run: cargo make test_browser --release
|
||||
working-directory: ${{ matrix.crate }}
|
||||
|
||||
rln-wasm-parallel-test:
|
||||
# skip tests on draft PRs
|
||||
if: github.event_name == 'push' || (github.event_name == 'pull_request' && !github.event.pull_request.draft)
|
||||
strategy:
|
||||
matrix:
|
||||
platform: [ubuntu-latest, macos-latest]
|
||||
crate: [rln-wasm]
|
||||
feature: ["parallel"]
|
||||
runs-on: ${{ matrix.platform }}
|
||||
timeout-minutes: 60
|
||||
|
||||
name: Test - ${{ matrix.crate }} - ${{ matrix.platform }} - ${{ matrix.feature }}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Install nightly toolchain
|
||||
uses: dtolnay/rust-toolchain@nightly
|
||||
with:
|
||||
components: rust-src
|
||||
targets: wasm32-unknown-unknown
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
- name: Install dependencies
|
||||
run: make installdeps
|
||||
- name: Build rln-wasm in parallel mode
|
||||
run: cargo make build_parallel
|
||||
working-directory: ${{ matrix.crate }}
|
||||
- name: Test rln-wasm in parallel mode on browser
|
||||
run: cargo make test_parallel --release
|
||||
working-directory: ${{ matrix.crate }}
|
||||
|
||||
rln-wasm-utils-test:
|
||||
# skip tests on draft PRs
|
||||
if: github.event_name == 'push' || (github.event_name == 'pull_request' && !github.event.pull_request.draft)
|
||||
strategy:
|
||||
matrix:
|
||||
platform: [ubuntu-latest, macos-latest]
|
||||
crate: [rln-wasm-utils]
|
||||
runs-on: ${{ matrix.platform }}
|
||||
timeout-minutes: 60
|
||||
|
||||
name: Test - ${{ matrix.crate }} - ${{ matrix.platform }}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Install stable toolchain
|
||||
uses: dtolnay/rust-toolchain@stable
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
- name: Install dependencies
|
||||
run: make installdeps
|
||||
- name: Test rln-wasm-utils
|
||||
run: cargo make test --release
|
||||
working-directory: ${{ matrix.crate }}
|
||||
|
||||
lint:
|
||||
# run on both ready and draft PRs
|
||||
if: github.event_name == 'push' || (github.event_name == 'pull_request' && !github.event.pull_request.draft)
|
||||
strategy:
|
||||
matrix:
|
||||
# we run lint tests only on ubuntu
|
||||
platform: [ ubuntu-latest ]
|
||||
crate: [ rln, utils ]
|
||||
platform: [ubuntu-latest]
|
||||
crate: [rln, rln-wasm, rln-wasm-utils, utils]
|
||||
runs-on: ${{ matrix.platform }}
|
||||
timeout-minutes: 60
|
||||
|
||||
name: lint - ${{ matrix.crate }} - ${{ matrix.platform }}
|
||||
name: Lint - ${{ matrix.crate }} - ${{ matrix.platform }}
|
||||
steps:
|
||||
- name: Checkout sources
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v4
|
||||
- name: Install stable toolchain
|
||||
uses: actions-rs/toolchain@v1
|
||||
uses: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
profile: minimal
|
||||
toolchain: stable
|
||||
override: true
|
||||
components: rustfmt, clippy
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
- name: Install Dependencies
|
||||
- name: Install dependencies
|
||||
run: make installdeps
|
||||
- name: cargo fmt
|
||||
- name: Check formatting
|
||||
if: success() || failure()
|
||||
run: cargo fmt -- --check
|
||||
working-directory: ${{ matrix.crate }}
|
||||
- name: cargo clippy
|
||||
- name: Check clippy
|
||||
if: success() || failure()
|
||||
run: |
|
||||
cargo clippy --release -- -D warnings
|
||||
cargo clippy --all-targets --release -- -D warnings
|
||||
working-directory: ${{ matrix.crate }}
|
||||
# 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-utils:
|
||||
# run only in pull requests
|
||||
if: github.event_name == 'pull_request'
|
||||
# run only on ready pull requests
|
||||
if: github.event_name == 'pull_request' && !github.event.pull_request.draft
|
||||
strategy:
|
||||
matrix:
|
||||
# we run benchmark tests only on ubuntu
|
||||
platform: [ ubuntu-latest ]
|
||||
crate: [ utils ]
|
||||
platform: [ubuntu-latest]
|
||||
crate: [utils]
|
||||
runs-on: ${{ matrix.platform }}
|
||||
timeout-minutes: 60
|
||||
|
||||
name: benchmark - ${{ matrix.platform }} - ${{ matrix.crate }}
|
||||
name: Benchmark - ${{ matrix.crate }} - ${{ matrix.platform }}
|
||||
steps:
|
||||
- name: Checkout sources
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v4
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
- uses: boa-dev/criterion-compare-action@v3
|
||||
with:
|
||||
@@ -158,24 +211,24 @@ jobs:
|
||||
cwd: ${{ matrix.crate }}
|
||||
|
||||
benchmark-rln:
|
||||
# run only in pull requests
|
||||
if: github.event_name == 'pull_request'
|
||||
# run only on ready pull requests
|
||||
if: github.event_name == 'pull_request' && !github.event.pull_request.draft
|
||||
strategy:
|
||||
matrix:
|
||||
# we run benchmark tests only on ubuntu
|
||||
platform: [ ubuntu-latest ]
|
||||
crate: [ rln ]
|
||||
feature: [ "default", "arkzkey" ]
|
||||
platform: [ubuntu-latest]
|
||||
crate: [rln]
|
||||
feature: ["default"]
|
||||
runs-on: ${{ matrix.platform }}
|
||||
timeout-minutes: 60
|
||||
|
||||
name: benchmark - ${{ matrix.platform }} - ${{ matrix.crate }} - ${{ matrix.feature }}
|
||||
name: Benchmark - ${{ matrix.crate }} - ${{ matrix.platform }} - ${{ matrix.feature }}
|
||||
steps:
|
||||
- name: Checkout sources
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v4
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
- uses: boa-dev/criterion-compare-action@v3
|
||||
with:
|
||||
branchName: ${{ github.base_ref }}
|
||||
cwd: ${{ matrix.crate }}
|
||||
features: ${{ matrix.feature }}
|
||||
features: ${{ matrix.feature }}
|
||||
|
||||
165
.github/workflows/nightly-release.yml
vendored
165
.github/workflows/nightly-release.yml
vendored
@@ -6,38 +6,38 @@ on:
|
||||
|
||||
jobs:
|
||||
linux:
|
||||
name: Linux build
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
feature: [ "default", "arkzkey", "stateless" ]
|
||||
feature:
|
||||
- "stateless"
|
||||
- "stateless,parallel"
|
||||
- "pmtree-ft"
|
||||
- "pmtree-ft,parallel"
|
||||
- "fullmerkletree"
|
||||
- "fullmerkletree,parallel"
|
||||
- "optimalmerkletree"
|
||||
- "optimalmerkletree,parallel"
|
||||
target:
|
||||
- x86_64-unknown-linux-gnu
|
||||
- aarch64-unknown-linux-gnu
|
||||
- i686-unknown-linux-gnu
|
||||
include:
|
||||
- feature: stateless
|
||||
cargo_args: --exclude rln-cli
|
||||
name: Linux build
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout sources
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v4
|
||||
- name: Install stable toolchain
|
||||
uses: actions-rs/toolchain@v1
|
||||
uses: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
profile: minimal
|
||||
toolchain: stable
|
||||
override: true
|
||||
target: ${{ matrix.target }}
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
- name: Install dependencies
|
||||
run: make installdeps
|
||||
- name: cross build
|
||||
- name: Cross build
|
||||
run: |
|
||||
cross build --release --target ${{ matrix.target }} --features ${{ matrix.feature }} --workspace --exclude rln-wasm ${{ matrix.cargo_args }}
|
||||
cross build --release --target ${{ matrix.target }} --no-default-features --features ${{ matrix.feature }} --workspace
|
||||
mkdir release
|
||||
cp target/${{ matrix.target }}/release/librln* release/
|
||||
tar -czvf ${{ matrix.target }}-${{ matrix.feature }}-rln.tar.gz release/
|
||||
|
||||
- name: Upload archive artifact
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
@@ -50,33 +50,34 @@ jobs:
|
||||
runs-on: macos-latest
|
||||
strategy:
|
||||
matrix:
|
||||
feature: [ "default", "arkzkey", "stateless" ]
|
||||
feature:
|
||||
- "stateless"
|
||||
- "stateless,parallel"
|
||||
- "pmtree-ft"
|
||||
- "pmtree-ft,parallel"
|
||||
- "fullmerkletree"
|
||||
- "fullmerkletree,parallel"
|
||||
- "optimalmerkletree"
|
||||
- "optimalmerkletree,parallel"
|
||||
target:
|
||||
- x86_64-apple-darwin
|
||||
- aarch64-apple-darwin
|
||||
include:
|
||||
- feature: stateless
|
||||
cargo_args: --exclude rln-cli
|
||||
steps:
|
||||
- name: Checkout sources
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v4
|
||||
- name: Install stable toolchain
|
||||
uses: actions-rs/toolchain@v1
|
||||
uses: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
profile: minimal
|
||||
toolchain: stable
|
||||
override: true
|
||||
target: ${{ matrix.target }}
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
- name: Install dependencies
|
||||
run: make installdeps
|
||||
- name: cross build
|
||||
- name: Cross build
|
||||
run: |
|
||||
cross build --release --target ${{ matrix.target }} --features ${{ matrix.feature }} --workspace --exclude rln-wasm ${{ matrix.cargo_args }}
|
||||
cross build --release --target ${{ matrix.target }} --no-default-features --features ${{ matrix.feature }} --workspace
|
||||
mkdir release
|
||||
cp target/${{ matrix.target }}/release/librln* release/
|
||||
tar -czvf ${{ matrix.target }}-${{ matrix.feature }}-rln.tar.gz release/
|
||||
|
||||
- name: Upload archive artifact
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
@@ -84,50 +85,120 @@ jobs:
|
||||
path: ${{ matrix.target }}-${{ matrix.feature }}-rln.tar.gz
|
||||
retention-days: 2
|
||||
|
||||
browser-rln-wasm:
|
||||
name: Browser build (RLN WASM)
|
||||
rln-wasm:
|
||||
name: Build rln-wasm
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
feature:
|
||||
- "default"
|
||||
- "parallel"
|
||||
steps:
|
||||
- name: Checkout sources
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v4
|
||||
- name: Install stable toolchain
|
||||
uses: actions-rs/toolchain@v1
|
||||
uses: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
profile: minimal
|
||||
toolchain: stable
|
||||
override: true
|
||||
targets: wasm32-unknown-unknown
|
||||
- name: Install nightly toolchain
|
||||
uses: dtolnay/rust-toolchain@nightly
|
||||
with:
|
||||
targets: wasm32-unknown-unknown
|
||||
components: rust-src
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
with:
|
||||
key: rln-wasm-${{ matrix.feature }}
|
||||
- name: Install dependencies
|
||||
run: make installdeps
|
||||
- name: Install wasm-pack
|
||||
uses: jetli/wasm-pack-action@v0.3.0
|
||||
- name: cross make build
|
||||
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
|
||||
- name: Install binaryen
|
||||
run: |
|
||||
cross make build
|
||||
mkdir release
|
||||
cp pkg/** release/
|
||||
tar -czvf browser-rln-wasm.tar.gz release/
|
||||
working-directory: rln-wasm
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y binaryen
|
||||
- name: Build rln-wasm package
|
||||
run: |
|
||||
if [[ ${{ matrix.feature }} == *parallel* ]]; then
|
||||
env RUSTFLAGS="-C target-feature=+atomics,+bulk-memory,+mutable-globals" \
|
||||
rustup run nightly wasm-pack build --release --target web --scope waku \
|
||||
--features ${{ matrix.feature }} -Z build-std=panic_abort,std
|
||||
else
|
||||
wasm-pack build --release --target web --scope waku
|
||||
fi
|
||||
|
||||
sed -i.bak 's/rln-wasm/zerokit-rln-wasm/g' pkg/package.json && rm pkg/package.json.bak
|
||||
|
||||
wasm-opt pkg/rln_wasm_bg.wasm -Oz --strip-debug --strip-dwarf \
|
||||
--remove-unused-module-elements --vacuum -o pkg/rln_wasm_bg.wasm
|
||||
|
||||
mkdir release
|
||||
cp -r pkg/* release/
|
||||
tar -czvf rln-wasm-${{ matrix.feature }}.tar.gz release/
|
||||
working-directory: rln-wasm
|
||||
- name: Upload archive artifact
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: browser-rln-wasm-archive
|
||||
path: rln-wasm/browser-rln-wasm.tar.gz
|
||||
name: rln-wasm-${{ matrix.feature }}-archive
|
||||
path: rln-wasm/rln-wasm-${{ matrix.feature }}.tar.gz
|
||||
retention-days: 2
|
||||
|
||||
rln-wasm-utils:
|
||||
name: Build rln-wasm-utils
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout sources
|
||||
uses: actions/checkout@v4
|
||||
- name: Install stable toolchain
|
||||
uses: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
targets: wasm32-unknown-unknown
|
||||
- name: Install nightly toolchain
|
||||
uses: dtolnay/rust-toolchain@nightly
|
||||
with:
|
||||
targets: wasm32-unknown-unknown
|
||||
components: rust-src
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
with:
|
||||
key: rln-wasm-utils
|
||||
- name: Install dependencies
|
||||
run: make installdeps
|
||||
- name: Install wasm-pack
|
||||
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
|
||||
- name: Install binaryen
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y binaryen
|
||||
- name: Build rln-wasm-utils package
|
||||
run: |
|
||||
wasm-pack build --release --target web --scope waku
|
||||
|
||||
sed -i.bak 's/rln-wasm-utils/zerokit-rln-wasm-utils/g' pkg/package.json && rm pkg/package.json.bak
|
||||
|
||||
wasm-opt pkg/rln_wasm_utils_bg.wasm -Oz --strip-debug --strip-dwarf \
|
||||
--remove-unused-module-elements --vacuum -o pkg/rln_wasm_utils_bg.wasm
|
||||
|
||||
mkdir release
|
||||
cp -r pkg/* release/
|
||||
tar -czvf rln-wasm-utils.tar.gz release/
|
||||
working-directory: rln-wasm-utils
|
||||
- name: Upload archive artifact
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: rln-wasm-utils-archive
|
||||
path: rln-wasm-utils/rln-wasm-utils.tar.gz
|
||||
retention-days: 2
|
||||
|
||||
prepare-prerelease:
|
||||
name: Prepare pre-release
|
||||
needs: [ linux, macos, browser-rln-wasm ]
|
||||
needs: [linux, macos, rln-wasm, rln-wasm-utils]
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
ref: master
|
||||
- name: Download artifacts
|
||||
uses: actions/download-artifact@v4
|
||||
|
||||
- name: Delete tag
|
||||
uses: dev-drprasad/delete-tag-and-release@v0.2.1
|
||||
with:
|
||||
@@ -135,7 +206,6 @@ jobs:
|
||||
tag_name: nightly
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
- name: Create prerelease
|
||||
run: |
|
||||
start_tag=$(gh release list -L 2 --exclude-drafts | grep -v nightly | cut -d$'\t' -f3 | sed -n '1p')
|
||||
@@ -147,7 +217,6 @@ jobs:
|
||||
*-archive/*.tar.gz \
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
- name: Delete artifacts
|
||||
uses: geekyeggo/delete-artifact@v5
|
||||
with:
|
||||
|
||||
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@v4
|
||||
- uses: micnncim/action-label-syncer@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
15
.gitignore
vendored
15
.gitignore
vendored
@@ -1,14 +1,17 @@
|
||||
# Common files to ignore in Rust projects
|
||||
.DS_Store
|
||||
.idea
|
||||
*.log
|
||||
tmp/
|
||||
rln/pmtree_db
|
||||
|
||||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
debug/
|
||||
target/
|
||||
wabt/
|
||||
# Generated by Cargo will have compiled files and executables
|
||||
/target
|
||||
|
||||
# Generated by rln-cli
|
||||
rln-cli/database
|
||||
|
||||
# Generated by Nix
|
||||
result
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
# CHANGE LOG
|
||||
|
||||
## 2023-02-28 v0.2
|
||||
|
||||
This release contains:
|
||||
@@ -10,7 +12,6 @@ This release contains:
|
||||
- Dual License under Apache 2.0 and MIT
|
||||
- RLN compiles as a static library, which can be consumed through a C FFI
|
||||
|
||||
|
||||
## 2022-09-19 v0.1
|
||||
|
||||
Initial beta release.
|
||||
|
||||
205
CONTRIBUTING.md
Normal file
205
CONTRIBUTING.md
Normal file
@@ -0,0 +1,205 @@
|
||||
# Contributing to Zerokit
|
||||
|
||||
Thank you for your interest in contributing to Zerokit!
|
||||
This guide will discuss how the Zerokit team handles [Commits](#commits),
|
||||
[Pull Requests](#pull-requests) and [Merging](#merging).
|
||||
|
||||
**Note:** We won't force external contributors to follow this verbatim.
|
||||
Following these guidelines definitely helps us in accepting your contributions.
|
||||
|
||||
## Getting Started
|
||||
|
||||
1. Fork the repository
|
||||
2. Create a feature branch: `git checkout -b fix/your-bug-fix` or `git checkout -b feat/your-feature-name`
|
||||
3. Make your changes following our guidelines
|
||||
4. Ensure relevant tests pass (see [testing guidelines](#building-and-testing))
|
||||
5. Commit your changes (signed commits are highly encouraged - see [commit guidelines](#commits))
|
||||
6. Push and create a Pull Request
|
||||
|
||||
## Development Setup
|
||||
|
||||
### Prerequisites
|
||||
|
||||
Install the required dependencies:
|
||||
|
||||
```bash
|
||||
make installdeps
|
||||
```
|
||||
|
||||
Or use Nix:
|
||||
|
||||
```bash
|
||||
nix develop
|
||||
```
|
||||
|
||||
### Building and Testing
|
||||
|
||||
```bash
|
||||
# Build all crates
|
||||
make build
|
||||
|
||||
# Run standard tests
|
||||
make test
|
||||
|
||||
# Module-specific testing
|
||||
cd rln && cargo make test_stateless # Test stateless features
|
||||
cd rln-wasm && cargo make test_browser # Test in browser headless mode
|
||||
cd rln-wasm && cargo make test_parallel # Test parallel features
|
||||
```
|
||||
|
||||
Choose the appropriate test commands based on your changes:
|
||||
|
||||
- Core RLN changes: `make test`
|
||||
- Stateless features: `cargo make test_stateless`
|
||||
- WASM/browser features: `cargo make test_browser`
|
||||
- Parallel computation: `cargo make test_parallel`
|
||||
|
||||
### Tools
|
||||
|
||||
We recommend using the [markdownlint extension](https://marketplace.visualstudio.com/items?itemName=DavidAnson.vscode-markdownlint)
|
||||
for VS Code to maintain consistent documentation formatting.
|
||||
|
||||
## Commits
|
||||
|
||||
We want to keep our commits small and focused.
|
||||
This allows for easily reviewing individual commits and/or
|
||||
splitting up pull requests when they grow too big.
|
||||
Additionally, this allows us to merge smaller changes quicker and release more often.
|
||||
|
||||
**All commits must be GPG signed.**
|
||||
This ensures the authenticity and integrity of contributions.
|
||||
|
||||
### Conventional Commits
|
||||
|
||||
When making the commit, write the commit message
|
||||
following the [Conventional Commits (v1.0.0)](https://www.conventionalcommits.org/en/v1.0.0/) specification.
|
||||
Following this convention allows us to provide an automated release process
|
||||
that also generates a detailed Changelog.
|
||||
|
||||
As described by the specification, our commit messages should be written as:
|
||||
|
||||
```markdown
|
||||
<type>[optional scope]: <description>
|
||||
|
||||
[optional body]
|
||||
|
||||
[optional footer(s)]
|
||||
```
|
||||
|
||||
Some examples of this pattern include:
|
||||
|
||||
```markdown
|
||||
feat(rln): add parallel witness calculation support
|
||||
```
|
||||
|
||||
```markdown
|
||||
fix(rln-wasm): resolve memory leak in browser threading
|
||||
```
|
||||
|
||||
```markdown
|
||||
docs: update RLN protocol flow documentation
|
||||
```
|
||||
|
||||
#### Scopes
|
||||
|
||||
Use scopes to improve the Changelog:
|
||||
|
||||
- `rln` - Core RLN implementation
|
||||
- `rln-cli` - Command-line interface
|
||||
- `rln-wasm` - WebAssembly bindings
|
||||
- `rln-wasm-utils` - WebAssembly utilities
|
||||
- `utils` - Cryptographic utilities (Merkle trees, Poseidon hash)
|
||||
- `ci` - Continuous integration
|
||||
|
||||
#### Breaking Changes
|
||||
|
||||
Mark breaking changes by adding `!` after the type:
|
||||
|
||||
```markdown
|
||||
feat(rln)!: change proof generation API
|
||||
```
|
||||
|
||||
## Pull Requests
|
||||
|
||||
Before creating a pull request, search for related issues.
|
||||
If none exist, create an issue describing the problem you're solving.
|
||||
|
||||
### CI Flow
|
||||
|
||||
Our continuous integration automatically runs when you create a Pull Request:
|
||||
|
||||
- **Build verification**: All crates compile successfully
|
||||
- **Test execution**: Comprehensive testing across all modules and feature combinations
|
||||
- **Code formatting**: `cargo fmt` compliance
|
||||
- **Linting**: `cargo clippy` checks
|
||||
- **Cross-platform builds**: Testing on multiple platforms
|
||||
|
||||
Ensure the following commands pass before submitting:
|
||||
|
||||
```bash
|
||||
# Format code
|
||||
cargo fmt --all
|
||||
|
||||
# Check for common mistakes
|
||||
cargo clippy --all-targets
|
||||
|
||||
# Run all tests
|
||||
make test
|
||||
```
|
||||
|
||||
### Adding Tests
|
||||
|
||||
Include tests for new functionality:
|
||||
|
||||
- **Unit tests** for specific functions
|
||||
- **Integration tests** for broader functionality
|
||||
- **WASM tests** for browser compatibility
|
||||
|
||||
### Typos and Small Changes
|
||||
|
||||
For minor fixes like typos, please report them as issues instead of opening PRs.
|
||||
This helps us manage resources effectively and ensures meaningful contributions.
|
||||
|
||||
## Merging
|
||||
|
||||
We use "squash merging" for all pull requests.
|
||||
This combines all commits into one commit, so keep pull requests small and focused.
|
||||
|
||||
### Requirements
|
||||
|
||||
- CI checks must pass
|
||||
- At least one maintainer review and approval
|
||||
- All review feedback addressed
|
||||
|
||||
### Squash Guidelines
|
||||
|
||||
When squashing, update the commit title to be a proper Conventional Commit and
|
||||
include any other relevant commits in the body:
|
||||
|
||||
```markdown
|
||||
feat(rln): implement parallel witness calculation (#123)
|
||||
|
||||
fix(tests): resolve memory leak in test suite
|
||||
chore(ci): update rust toolchain version
|
||||
```
|
||||
|
||||
## Roadmap Alignment
|
||||
|
||||
Please refer to our [project roadmap](https://roadmap.vac.dev/) for current development priorities.
|
||||
Consider how your changes align with these strategic goals, when contributing.
|
||||
|
||||
## Getting Help
|
||||
|
||||
- **Issues**: Create a GitHub issue for bugs or feature requests
|
||||
- **Discussions**: Use GitHub Discussions for questions
|
||||
- **Documentation**: Check existing docs and unit tests for examples
|
||||
|
||||
## License
|
||||
|
||||
By contributing to Zerokit, you agree that your contributions will be licensed under both MIT and
|
||||
Apache 2.0 licenses, consistent with the project's dual licensing.
|
||||
|
||||
## Additional Resources
|
||||
|
||||
- [Conventional Commits Guide](https://www.conventionalcommits.org/en/v1.0.0/)
|
||||
- [Project GitHub Repository](https://github.com/vacp2p/zerokit)
|
||||
3137
Cargo.lock
generated
3137
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
||||
[workspace]
|
||||
members = ["rln", "rln-cli", "rln-wasm", "utils"]
|
||||
default-members = ["rln", "rln-cli", "utils"]
|
||||
members = ["rln", "utils"]
|
||||
exclude = ["rln-cli", "rln-wasm", "rln-wasm-utils"]
|
||||
resolver = "2"
|
||||
|
||||
# Compilation profile for any non-workspace member.
|
||||
@@ -8,7 +8,3 @@ resolver = "2"
|
||||
# while having neglible impact on incremental build times.
|
||||
[profile.dev.package."*"]
|
||||
opt-level = 3
|
||||
|
||||
[profile.release.package."rln-wasm"]
|
||||
# Tell `rustc` to optimize for small code size.
|
||||
opt-level = "s"
|
||||
|
||||
31
Makefile
31
Makefile
@@ -1,6 +1,6 @@
|
||||
.PHONY: all installdeps build test clean
|
||||
.PHONY: all installdeps build test bench clean
|
||||
|
||||
all: .pre-build build
|
||||
all: installdeps build
|
||||
|
||||
.fetch-submodules:
|
||||
@git submodule update --init --recursive
|
||||
@@ -13,25 +13,26 @@ endif
|
||||
|
||||
installdeps: .pre-build
|
||||
ifeq ($(shell uname),Darwin)
|
||||
# commented due to https://github.com/orgs/Homebrew/discussions/4612
|
||||
# @brew update
|
||||
@brew install cmake ninja
|
||||
@brew install ninja binaryen
|
||||
else ifeq ($(shell uname),Linux)
|
||||
@sudo apt-get update
|
||||
@sudo apt-get install -y cmake ninja-build
|
||||
@if [ -f /etc/os-release ] && grep -q "ID=nixos" /etc/os-release; then \
|
||||
echo "Detected NixOS, skipping apt-get installation."; \
|
||||
else \
|
||||
sudo apt-get install -y cmake ninja-build binaryen; \
|
||||
fi
|
||||
endif
|
||||
@git -C "wabt" pull || git clone --recursive https://github.com/WebAssembly/wabt.git "wabt"
|
||||
@cd wabt && mkdir -p build && cd build && cmake .. -GNinja && ninja && sudo ninja install
|
||||
@which wasm-pack || cargo install wasm-pack
|
||||
# nvm already checks if it's installed, and no-ops if it is
|
||||
@curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
|
||||
@. ${HOME}/.nvm/nvm.sh && nvm install 18.20.2 && nvm use 18.20.2;
|
||||
@which wasm-pack > /dev/null && wasm-pack --version | grep -q "0.13.1" || cargo install wasm-pack --version=0.13.1
|
||||
@test -s "$$HOME/.nvm/nvm.sh" || curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.2/install.sh | bash
|
||||
@bash -c '. "$$HOME/.nvm/nvm.sh"; [ "$$(node -v 2>/dev/null)" = "v22.14.0" ] || nvm install 22.14.0; nvm use 22.14.0; nvm alias default 22.14.0'
|
||||
|
||||
build: .pre-build
|
||||
build: installdeps
|
||||
@cargo make build
|
||||
|
||||
test: .pre-build
|
||||
test: build
|
||||
@cargo make test
|
||||
|
||||
bench: build
|
||||
@cargo make bench
|
||||
|
||||
clean:
|
||||
@cargo clean
|
||||
|
||||
81
README.md
81
README.md
@@ -1,45 +1,86 @@
|
||||
# Zerokit
|
||||
|
||||
A set of Zero Knowledge modules, written in Rust and designed to be used in other system programming environments.
|
||||
[](https://crates.io/crates/rln)
|
||||
[](https://github.com/vacp2p/zerokit/actions)
|
||||
[](https://opensource.org/licenses/MIT)
|
||||
[](https://opensource.org/licenses/Apache-2.0)
|
||||
|
||||
## Initial scope
|
||||
A collection of Zero Knowledge modules written in Rust and designed to be used in other system programming environments.
|
||||
|
||||
Focus on RLN and being able to use [Circom](https://iden3.io/circom) based
|
||||
version through ark-circom, as opposed to the native one that currently exists
|
||||
in Rust.
|
||||
## Overview
|
||||
|
||||
## Acknowledgements
|
||||
Zerokit provides zero-knowledge cryptographic primitives with a focus on performance, security, and usability.
|
||||
The current focus is on Rate-Limiting Nullifier [RLN](https://github.com/Rate-Limiting-Nullifier) implementation.
|
||||
|
||||
- Uses [ark-circom](https://github.com/gakonst/ark-circom), Rust wrapper around Circom.
|
||||
Current implementation is based on the following
|
||||
[specification](https://github.com/vacp2p/rfc-index/blob/main/vac/raw/rln-v2.md)
|
||||
and focused on RLNv2 which allows to set a rate limit for the number of messages that can be sent by a user.
|
||||
|
||||
- Inspired by Applied ZKP group work, e.g. [zk-kit](https://github.com/appliedzkp/zk-kit).
|
||||
## Features
|
||||
|
||||
- [RLN library](https://github.com/kilic/rln) written in Rust based on Bellman.
|
||||
- **RLN Implementation**: Efficient Rate-Limiting Nullifier using zkSNARKs
|
||||
- **Circom Compatibility**: Uses Circom-based circuits for RLN
|
||||
- **Cross-Platform**: Support for multiple architectures (see compatibility note below)
|
||||
- **FFI-Friendly**: Easy to integrate with other languages
|
||||
|
||||
- [semaphore-rs](https://github.com/worldcoin/semaphore-rs) written in Rust based on ark-circom.
|
||||
## Architecture
|
||||
|
||||
## Users
|
||||
|
||||
Zerokit is used by -
|
||||
|
||||
- [nwaku](https://github.com/waku-org/nwaku)
|
||||
- [js-rln](https://github.com/waku-org/js-rln)
|
||||
Zerokit currently focuses on RLN (Rate-Limiting Nullifier) implementation using [Circom](https://iden3.io/circom)
|
||||
circuits through ark-circom, providing an alternative to existing native Rust implementations.
|
||||
|
||||
## Build and Test
|
||||
|
||||
To install missing dependencies, run the following commands from the root folder
|
||||
### Install Dependencies
|
||||
|
||||
```bash
|
||||
make installdeps
|
||||
```
|
||||
|
||||
To build and test all crates, run the following commands from the root folder
|
||||
#### Use Nix to install dependencies
|
||||
|
||||
```bash
|
||||
nix develop
|
||||
```
|
||||
|
||||
### Build and Test All Crates
|
||||
|
||||
```bash
|
||||
make build
|
||||
make test
|
||||
```
|
||||
|
||||
## Release assets
|
||||
## Release Assets
|
||||
|
||||
We use [`cross-rs`](https://github.com/cross-rs/cross) to cross-compile and generate release assets for rln.
|
||||
We use [`cross-rs`](https://github.com/cross-rs/cross) to cross-compile and generate release assets:
|
||||
|
||||
```bash
|
||||
# Example: Build for specific target
|
||||
cross build --target x86_64-unknown-linux-gnu --release -p rln
|
||||
```
|
||||
|
||||
## Used By
|
||||
|
||||
Zerokit powers zero-knowledge functionality in:
|
||||
|
||||
- [**nwaku**](https://github.com/waku-org/nwaku) - Nim implementation of the Waku v2 protocol
|
||||
- [**js-rln**](https://github.com/waku-org/js-rln) - JavaScript bindings for RLN
|
||||
|
||||
## Acknowledgements
|
||||
|
||||
- Inspired by [Applied ZKP](https://zkp.science/) group work, including [zk-kit](https://github.com/appliedzkp/zk-kit)
|
||||
- Uses [ark-circom](https://github.com/gakonst/ark-circom) for zkey and Groth16 proof generation
|
||||
- Witness calculation based on [circom-witnesscalc](https://github.com/iden3/circom-witnesscalc) by iden3.
|
||||
The execution graph file used by this code has been generated by means of the same iden3 software.
|
||||
|
||||
> [!IMPORTANT]
|
||||
> The circom-witnesscalc code fragments have been borrowed instead of depending on this crate,
|
||||
> because its types of input and output data were incompatible with the corresponding zerokit code fragments,
|
||||
> and circom-witnesscalc has some dependencies, which are redundant for our purpose.
|
||||
|
||||
## Documentation
|
||||
|
||||
For detailed documentation on each module:
|
||||
|
||||
```bash
|
||||
cargo doc --open
|
||||
```
|
||||
|
||||
48
flake.lock
generated
Normal file
48
flake.lock
generated
Normal file
@@ -0,0 +1,48 @@
|
||||
{
|
||||
"nodes": {
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1740603184,
|
||||
"narHash": "sha256-t+VaahjQAWyA+Ctn2idyo1yxRIYpaDxMgHkgCNiMJa4=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "f44bd8ca21e026135061a0a57dcf3d0775b67a49",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "f44bd8ca21e026135061a0a57dcf3d0775b67a49",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"nixpkgs": "nixpkgs",
|
||||
"rust-overlay": "rust-overlay"
|
||||
}
|
||||
},
|
||||
"rust-overlay": {
|
||||
"inputs": {
|
||||
"nixpkgs": [
|
||||
"nixpkgs"
|
||||
]
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1748399823,
|
||||
"narHash": "sha256-kahD8D5hOXOsGbNdoLLnqCL887cjHkx98Izc37nDjlA=",
|
||||
"owner": "oxalica",
|
||||
"repo": "rust-overlay",
|
||||
"rev": "d68a69dc71bc19beb3479800392112c2f6218159",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "oxalica",
|
||||
"repo": "rust-overlay",
|
||||
"type": "github"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
||||
69
flake.nix
Normal file
69
flake.nix
Normal file
@@ -0,0 +1,69 @@
|
||||
{
|
||||
description = "A flake for building zerokit";
|
||||
|
||||
inputs = {
|
||||
# Version 24.11
|
||||
nixpkgs.url = "github:NixOS/nixpkgs?rev=f44bd8ca21e026135061a0a57dcf3d0775b67a49";
|
||||
rust-overlay = {
|
||||
url = "github:oxalica/rust-overlay";
|
||||
inputs.nixpkgs.follows = "nixpkgs";
|
||||
};
|
||||
};
|
||||
|
||||
outputs = { self, nixpkgs, rust-overlay }:
|
||||
let
|
||||
stableSystems = [
|
||||
"x86_64-linux" "aarch64-linux"
|
||||
"x86_64-darwin" "aarch64-darwin"
|
||||
"x86_64-windows" "i686-linux"
|
||||
"i686-windows"
|
||||
];
|
||||
forAllSystems = nixpkgs.lib.genAttrs stableSystems;
|
||||
overlays = [
|
||||
(import rust-overlay)
|
||||
(f: p: { inherit rust-overlay; })
|
||||
];
|
||||
pkgsFor = forAllSystems (system: import nixpkgs { inherit system overlays; });
|
||||
in rec
|
||||
{
|
||||
packages = forAllSystems (system: let
|
||||
pkgs = pkgsFor.${system};
|
||||
buildPackage = pkgs.callPackage ./nix/default.nix;
|
||||
buildRln = (buildPackage { src = self; project = "rln"; }).override;
|
||||
in rec {
|
||||
rln = buildRln { };
|
||||
|
||||
rln-linux-arm64 = buildRln {
|
||||
target-platform = "aarch64-multiplatform";
|
||||
rust-target = "aarch64-unknown-linux-gnu";
|
||||
};
|
||||
|
||||
rln-android-arm64 = buildRln {
|
||||
target-platform = "aarch64-android-prebuilt";
|
||||
rust-target = "aarch64-linux-android";
|
||||
};
|
||||
|
||||
rln-ios-arm64 = buildRln {
|
||||
target-platform = "aarch64-darwin";
|
||||
rust-target = "aarch64-apple-ios";
|
||||
};
|
||||
|
||||
# TODO: Remove legacy name for RLN android library
|
||||
zerokit-android-arm64 = rln-android-arm64;
|
||||
|
||||
default = rln;
|
||||
});
|
||||
|
||||
devShells = forAllSystems (system: let
|
||||
pkgs = pkgsFor.${system};
|
||||
in {
|
||||
default = pkgs.mkShell {
|
||||
buildInputs = with pkgs; [
|
||||
git cmake cargo-make rustup
|
||||
binaryen ninja gnuplot
|
||||
rust-bin.stable.latest.default
|
||||
];
|
||||
};
|
||||
});
|
||||
};
|
||||
}
|
||||
64
nix/default.nix
Normal file
64
nix/default.nix
Normal file
@@ -0,0 +1,64 @@
|
||||
{
|
||||
pkgs,
|
||||
rust-overlay,
|
||||
project,
|
||||
src ? ../.,
|
||||
release ? true,
|
||||
target-platform ? null,
|
||||
rust-target ? null,
|
||||
features ? null,
|
||||
}:
|
||||
|
||||
let
|
||||
# Use cross-compilation if target-platform is specified.
|
||||
targetPlatformPkgs = if target-platform != null
|
||||
then pkgs.pkgsCross.${target-platform}
|
||||
else pkgs;
|
||||
|
||||
rust-bin = rust-overlay.lib.mkRustBin { } targetPlatformPkgs.buildPackages;
|
||||
|
||||
# Use Rust and Cargo versions from rust-overlay.
|
||||
rustPlatform = targetPlatformPkgs.makeRustPlatform {
|
||||
cargo = rust-bin.stable.latest.minimal;
|
||||
rustc = rust-bin.stable.latest.minimal;
|
||||
};
|
||||
in rustPlatform.buildRustPackage {
|
||||
pname = "zerokit";
|
||||
version = if src ? rev then src.rev else "nightly";
|
||||
|
||||
# Improve caching of sources
|
||||
src = builtins.path { path = src; name = "zerokit"; };
|
||||
|
||||
cargoLock = {
|
||||
lockFile = src + "/Cargo.lock";
|
||||
allowBuiltinFetchGit = true;
|
||||
};
|
||||
|
||||
nativeBuildInputs = [ pkgs.rust-cbindgen ];
|
||||
|
||||
doCheck = false;
|
||||
|
||||
CARGO_HOME = "/tmp";
|
||||
|
||||
buildPhase = ''
|
||||
cargo build --lib \
|
||||
${if release then "--release" else ""} \
|
||||
${if rust-target != null then "--target=${rust-target}" else ""} \
|
||||
${if features != null then "--features=${features}" else ""} \
|
||||
--manifest-path ${project}/Cargo.toml
|
||||
'';
|
||||
|
||||
installPhase = ''
|
||||
set -eu
|
||||
mkdir -p $out/lib
|
||||
find target -type f -name 'librln.*' -not -path '*/deps/*' -exec cp -v '{}' "$out/lib/" \;
|
||||
mkdir -p $out/include
|
||||
cbindgen ${src}/rln -l c > "$out/include/rln.h"
|
||||
'';
|
||||
|
||||
|
||||
meta = with pkgs.lib; {
|
||||
description = "Zerokit";
|
||||
license = licenses.mit;
|
||||
};
|
||||
}
|
||||
@@ -1,13 +1,25 @@
|
||||
[package]
|
||||
name = "rln-cli"
|
||||
version = "0.3.0"
|
||||
version = "0.4.0"
|
||||
edition = "2021"
|
||||
|
||||
[[example]]
|
||||
name = "relay"
|
||||
path = "src/examples/relay.rs"
|
||||
|
||||
[[example]]
|
||||
name = "stateless"
|
||||
path = "src/examples/stateless.rs"
|
||||
required-features = ["stateless"]
|
||||
|
||||
[dependencies]
|
||||
rln = { path = "../rln", default-features = true, features = ["arkzkey"] }
|
||||
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"] }
|
||||
rln = { path = "../rln", version = "0.8.0", default-features = false }
|
||||
zerokit_utils = { path = "../utils", version = "0.6.0", default-features = false }
|
||||
clap = { version = "4.5.41", features = ["cargo", "derive", "env"] }
|
||||
color-eyre = "0.6.5"
|
||||
serde_json = "1.0.141"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
|
||||
[features]
|
||||
default = ["rln/pmtree-ft", "rln/parallel"]
|
||||
stateless = ["rln/stateless", "rln/parallel"]
|
||||
|
||||
9
rln-cli/Makefile.toml
Normal file
9
rln-cli/Makefile.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[tasks.build]
|
||||
command = "cargo"
|
||||
args = ["build"]
|
||||
|
||||
[tasks.test]
|
||||
disabled = true
|
||||
|
||||
[tasks.bench]
|
||||
disabled = true
|
||||
148
rln-cli/README.md
Normal file
148
rln-cli/README.md
Normal file
@@ -0,0 +1,148 @@
|
||||
# Zerokit RLN-CLI
|
||||
|
||||
The Zerokit RLN-CLI provides a command-line interface for interacting with the public API of the [Zerokit RLN Module](../rln/README.md).
|
||||
|
||||
It also contain:
|
||||
|
||||
+ [Relay Example](#relay-example) to demonstrate the use of the RLN module for spam prevention.
|
||||
+ [Stateless Example](#stateless-example) to demonstrate the use of the RLN module for stateless features.
|
||||
|
||||
## Configuration
|
||||
|
||||
The CLI can be configured using a JSON configuration file (see the [example](example.config.json)).
|
||||
|
||||
You can specify the configuration file path using the `RLN_CONFIG_PATH` environment variable:
|
||||
|
||||
```bash
|
||||
export RLN_CONFIG_PATH=example.config.json
|
||||
```
|
||||
|
||||
Alternatively, you can provide the configuration file path as an argument for each command:
|
||||
|
||||
```bash
|
||||
RLN_CONFIG_PATH=example.config.json cargo run -- <SUBCOMMAND> [OPTIONS]
|
||||
```
|
||||
|
||||
If the configuration file is empty, default settings will be used, but the tree data folder will be temporary and not saved to the preconfigured path.
|
||||
|
||||
We recommend using the example config, as all commands (except `new` and `create-with-params`) require an initialized RLN instance.
|
||||
|
||||
## Relay Example
|
||||
|
||||
The following [Example](src/examples/relay.rs) demonstrates how RLN enables spam prevention in anonymous environments for multple users.
|
||||
|
||||
You can run the example using the following command:
|
||||
|
||||
```bash
|
||||
cargo run --example relay
|
||||
```
|
||||
|
||||
You can also change **MESSAGE_LIMIT** and **TREE_DEPTH** in the [relay.rs](src/examples/relay.rs) file to see how the RLN instance behaves with different parameters.
|
||||
|
||||
The customize **TREE_DEPTH** constant differs from the default value of `20` should follow [Custom Circuit Compilation](../rln/README.md#advanced-custom-circuit-compilation) instructions.
|
||||
|
||||
## Stateless Example
|
||||
|
||||
The following [Example](src/examples/stateless.rs) demonstrates how RLN can be used for stateless features by creating the Merkle tree outside of RLN instance.
|
||||
|
||||
This example function similarly to the [Relay Example](#relay-example) but uses a stateless RLN and seperate Merkle tree.
|
||||
|
||||
You can run the example using the following command:
|
||||
|
||||
```bash
|
||||
cargo run --example stateless --no-default-features --features stateless
|
||||
```
|
||||
|
||||
## CLI Commands
|
||||
|
||||
### Instance Management
|
||||
|
||||
To initialize a new RLN instance:
|
||||
|
||||
```bash
|
||||
cargo run new --tree-depth <DEPTH>
|
||||
```
|
||||
|
||||
To initialize an RLN instance with custom parameters:
|
||||
|
||||
```bash
|
||||
cargo run new-with-params --resources-path <PATH> --tree-depth <DEPTH>
|
||||
```
|
||||
|
||||
To update the Merkle tree depth:
|
||||
|
||||
```bash
|
||||
cargo run set-tree --tree-depth <DEPTH>
|
||||
```
|
||||
|
||||
### Leaf Operations
|
||||
|
||||
To set a single leaf:
|
||||
|
||||
```bash
|
||||
cargo run set-leaf --index <INDEX> --input <INPUT_PATH>
|
||||
```
|
||||
|
||||
To set multiple leaves:
|
||||
|
||||
```bash
|
||||
cargo run set-multiple-leaves --index <START_INDEX> --input <INPUT_PATH>
|
||||
```
|
||||
|
||||
To reset multiple leaves:
|
||||
|
||||
```bash
|
||||
cargo run reset-multiple-leaves --input <INPUT_PATH>
|
||||
```
|
||||
|
||||
To set the next available leaf:
|
||||
|
||||
```bash
|
||||
cargo run set-next-leaf --input <INPUT_PATH>
|
||||
```
|
||||
|
||||
To delete a specific leaf:
|
||||
|
||||
```bash
|
||||
cargo run delete-leaf --index <INDEX>
|
||||
```
|
||||
|
||||
### Proof Operations
|
||||
|
||||
To generate a proof:
|
||||
|
||||
```bash
|
||||
cargo run prove --input <INPUT_PATH>
|
||||
```
|
||||
|
||||
To generate an RLN proof:
|
||||
|
||||
```bash
|
||||
cargo run generate-proof --input <INPUT_PATH>
|
||||
```
|
||||
|
||||
To verify a proof:
|
||||
|
||||
```bash
|
||||
cargo run verify --input <PROOF_PATH>
|
||||
```
|
||||
|
||||
To verify a proof with multiple Merkle roots:
|
||||
|
||||
```bash
|
||||
cargo run verify-with-roots --input <INPUT_PATH> --roots <ROOTS_PATH>
|
||||
```
|
||||
|
||||
### Tree Information
|
||||
|
||||
To retrieve the current Merkle root:
|
||||
|
||||
```bash
|
||||
cargo run get-root
|
||||
```
|
||||
|
||||
To obtain a Merkle proof for a specific index:
|
||||
|
||||
```bash
|
||||
cargo run get-proof --index <INDEX>
|
||||
```
|
||||
9
rln-cli/example.config.json
Normal file
9
rln-cli/example.config.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"path": "database",
|
||||
"temporary": false,
|
||||
"cache_capacity": 1073741824,
|
||||
"flush_every_ms": 500,
|
||||
"mode": "HighThroughput",
|
||||
"use_compression": false,
|
||||
"tree_depth": 20
|
||||
}
|
||||
@@ -1,49 +1,51 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use clap::Subcommand;
|
||||
use rln::circuit::TEST_TREE_DEPTH;
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub(crate) enum Commands {
|
||||
New {
|
||||
tree_height: usize,
|
||||
/// Sets a custom config file
|
||||
#[arg(short, long)]
|
||||
config: PathBuf,
|
||||
#[arg(short, long, default_value_t = TEST_TREE_DEPTH)]
|
||||
tree_depth: usize,
|
||||
},
|
||||
NewWithParams {
|
||||
tree_height: usize,
|
||||
/// Sets a custom config file
|
||||
#[arg(short, long)]
|
||||
config: PathBuf,
|
||||
#[arg(short, long)]
|
||||
tree_config_input: PathBuf,
|
||||
#[arg(short, long, default_value_t = TEST_TREE_DEPTH)]
|
||||
tree_depth: usize,
|
||||
#[arg(short, long, default_value = "../rln/resources/tree_depth_30")]
|
||||
resources_path: PathBuf,
|
||||
},
|
||||
SetTree {
|
||||
tree_height: usize,
|
||||
#[arg(short, long, default_value_t = TEST_TREE_DEPTH)]
|
||||
tree_depth: usize,
|
||||
},
|
||||
SetLeaf {
|
||||
#[arg(short, long)]
|
||||
index: usize,
|
||||
#[arg(short, long)]
|
||||
file: PathBuf,
|
||||
input: PathBuf,
|
||||
},
|
||||
SetMultipleLeaves {
|
||||
#[arg(short, long)]
|
||||
index: usize,
|
||||
#[arg(short, long)]
|
||||
file: PathBuf,
|
||||
input: PathBuf,
|
||||
},
|
||||
ResetMultipleLeaves {
|
||||
#[arg(short, long)]
|
||||
file: PathBuf,
|
||||
input: PathBuf,
|
||||
},
|
||||
SetNextLeaf {
|
||||
#[arg(short, long)]
|
||||
file: PathBuf,
|
||||
input: PathBuf,
|
||||
},
|
||||
DeleteLeaf {
|
||||
#[arg(short, long)]
|
||||
index: usize,
|
||||
},
|
||||
GetRoot,
|
||||
GetProof {
|
||||
#[arg(short, long)]
|
||||
index: usize,
|
||||
},
|
||||
Prove {
|
||||
@@ -52,7 +54,7 @@ pub(crate) enum Commands {
|
||||
},
|
||||
Verify {
|
||||
#[arg(short, long)]
|
||||
file: PathBuf,
|
||||
input: PathBuf,
|
||||
},
|
||||
GenerateProof {
|
||||
#[arg(short, long)]
|
||||
|
||||
@@ -1,29 +1,31 @@
|
||||
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";
|
||||
use color_eyre::Result;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
|
||||
#[derive(Default, Serialize, Deserialize)]
|
||||
pub const RLN_CONFIG_PATH: &str = "RLN_CONFIG_PATH";
|
||||
|
||||
#[derive(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,
|
||||
pub tree_config: Option<String>,
|
||||
}
|
||||
|
||||
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)
|
||||
match std::env::var(RLN_CONFIG_PATH) {
|
||||
Ok(env) => {
|
||||
let path = PathBuf::from(env);
|
||||
let mut file = File::open(path)?;
|
||||
let mut contents = String::new();
|
||||
file.read_to_string(&mut contents)?;
|
||||
let tree_config: Value = serde_json::from_str(&contents)?;
|
||||
println!("Initializing RLN with custom config");
|
||||
Ok(Config {
|
||||
tree_config: Some(tree_config.to_string()),
|
||||
})
|
||||
}
|
||||
Err(_) => Ok(Config { tree_config: None }),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
315
rln-cli/src/examples/relay.rs
Normal file
315
rln-cli/src/examples/relay.rs
Normal file
@@ -0,0 +1,315 @@
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
fs::File,
|
||||
io::{stdin, stdout, Cursor, Read, Write},
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use clap::{Parser, Subcommand};
|
||||
use color_eyre::{eyre::eyre, Report, Result};
|
||||
use rln::{
|
||||
circuit::Fr,
|
||||
hashers::{hash_to_field_le, poseidon_hash},
|
||||
protocol::{keygen, prepare_prove_input, prepare_verify_input},
|
||||
public::RLN,
|
||||
utils::{fr_to_bytes_le, generate_input_buffer, IdSecret},
|
||||
};
|
||||
|
||||
const MESSAGE_LIMIT: u32 = 1;
|
||||
|
||||
const TREE_DEPTH: usize = 30;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(author, version, about, long_about = None)]
|
||||
struct Cli {
|
||||
#[command(subcommand)]
|
||||
command: Commands,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum Commands {
|
||||
List,
|
||||
Register,
|
||||
Send {
|
||||
#[arg(short, long)]
|
||||
user_index: usize,
|
||||
#[arg(short, long)]
|
||||
message_id: u32,
|
||||
#[arg(short, long)]
|
||||
signal: String,
|
||||
},
|
||||
Clear,
|
||||
Exit,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Identity {
|
||||
identity_secret_hash: IdSecret,
|
||||
id_commitment: Fr,
|
||||
}
|
||||
|
||||
impl Identity {
|
||||
fn new() -> Self {
|
||||
let (identity_secret_hash, id_commitment) = keygen();
|
||||
Identity {
|
||||
identity_secret_hash,
|
||||
id_commitment,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct RLNSystem {
|
||||
rln: RLN,
|
||||
used_nullifiers: HashMap<[u8; 32], Vec<u8>>,
|
||||
local_identities: HashMap<usize, Identity>,
|
||||
}
|
||||
|
||||
impl RLNSystem {
|
||||
fn new() -> Result<Self> {
|
||||
let mut resources: Vec<Vec<u8>> = Vec::new();
|
||||
let resources_path: PathBuf = format!("../rln/resources/tree_depth_{TREE_DEPTH}").into();
|
||||
let filenames = ["rln_final.arkzkey", "graph.bin"];
|
||||
for filename in filenames {
|
||||
let fullpath = resources_path.join(Path::new(filename));
|
||||
let mut file = File::open(&fullpath)?;
|
||||
let metadata = std::fs::metadata(&fullpath)?;
|
||||
let mut output_buffer = vec![0; metadata.len() as usize];
|
||||
file.read_exact(&mut output_buffer)?;
|
||||
resources.push(output_buffer);
|
||||
}
|
||||
let rln = RLN::new_with_params(
|
||||
TREE_DEPTH,
|
||||
resources[0].clone(),
|
||||
resources[1].clone(),
|
||||
generate_input_buffer(),
|
||||
)?;
|
||||
println!("RLN instance initialized successfully");
|
||||
Ok(RLNSystem {
|
||||
rln,
|
||||
used_nullifiers: HashMap::new(),
|
||||
local_identities: HashMap::new(),
|
||||
})
|
||||
}
|
||||
|
||||
fn list_users(&self) {
|
||||
if self.local_identities.is_empty() {
|
||||
println!("No users registered yet.");
|
||||
return;
|
||||
}
|
||||
|
||||
println!("Registered users:");
|
||||
for (index, identity) in &self.local_identities {
|
||||
println!("User Index: {index}");
|
||||
println!("+ Identity Secret Hash: {}", *identity.identity_secret_hash);
|
||||
println!("+ Identity Commitment: {}", identity.id_commitment);
|
||||
println!();
|
||||
}
|
||||
}
|
||||
|
||||
fn register_user(&mut self) -> Result<usize> {
|
||||
let index = self.rln.leaves_set();
|
||||
let identity = Identity::new();
|
||||
|
||||
let rate_commitment = poseidon_hash(&[identity.id_commitment, Fr::from(MESSAGE_LIMIT)]);
|
||||
let mut buffer = Cursor::new(fr_to_bytes_le(&rate_commitment));
|
||||
match self.rln.set_next_leaf(&mut buffer) {
|
||||
Ok(_) => {
|
||||
println!("Registered User Index: {index}");
|
||||
println!("+ Identity secret hash: {}", *identity.identity_secret_hash);
|
||||
println!("+ Identity commitment: {},", identity.id_commitment);
|
||||
self.local_identities.insert(index, identity);
|
||||
}
|
||||
Err(_) => {
|
||||
println!("Maximum user limit reached: 2^{TREE_DEPTH}");
|
||||
}
|
||||
};
|
||||
|
||||
Ok(index)
|
||||
}
|
||||
|
||||
fn generate_proof(
|
||||
&mut self,
|
||||
user_index: usize,
|
||||
message_id: u32,
|
||||
signal: &str,
|
||||
external_nullifier: Fr,
|
||||
) -> Result<Vec<u8>> {
|
||||
let identity = match self.local_identities.get(&user_index) {
|
||||
Some(identity) => identity,
|
||||
None => return Err(eyre!("user index {user_index} not found")),
|
||||
};
|
||||
|
||||
let serialized = prepare_prove_input(
|
||||
identity.identity_secret_hash.clone(),
|
||||
user_index,
|
||||
Fr::from(MESSAGE_LIMIT),
|
||||
Fr::from(message_id),
|
||||
external_nullifier,
|
||||
signal.as_bytes(),
|
||||
);
|
||||
let mut input_buffer = Cursor::new(serialized);
|
||||
let mut output_buffer = Cursor::new(Vec::new());
|
||||
self.rln
|
||||
.generate_rln_proof(&mut input_buffer, &mut output_buffer)?;
|
||||
|
||||
println!("Proof generated successfully:");
|
||||
println!("+ User Index: {user_index}");
|
||||
println!("+ Message ID: {message_id}");
|
||||
println!("+ Signal: {signal}");
|
||||
|
||||
Ok(output_buffer.into_inner())
|
||||
}
|
||||
|
||||
fn verify_proof(&mut self, proof_data: Vec<u8>, signal: &str) -> Result<()> {
|
||||
let proof_with_signal = prepare_verify_input(proof_data.clone(), signal.as_bytes());
|
||||
let mut input_buffer = Cursor::new(proof_with_signal);
|
||||
|
||||
match self.rln.verify_rln_proof(&mut input_buffer) {
|
||||
Ok(true) => {
|
||||
let nullifier = &proof_data[256..288];
|
||||
let nullifier_key: [u8; 32] = nullifier.try_into()?;
|
||||
|
||||
if let Some(previous_proof) = self.used_nullifiers.get(&nullifier_key) {
|
||||
self.handle_duplicate_message_id(previous_proof.clone(), proof_data)?;
|
||||
return Ok(());
|
||||
}
|
||||
self.used_nullifiers.insert(nullifier_key, proof_data);
|
||||
println!("Message verified and accepted");
|
||||
}
|
||||
Ok(false) => {
|
||||
println!("Verification failed: message_id must be unique within the epoch and satisfy 0 <= message_id < MESSAGE_LIMIT: {MESSAGE_LIMIT}");
|
||||
}
|
||||
Err(err) => return Err(Report::new(err)),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_duplicate_message_id(
|
||||
&mut self,
|
||||
previous_proof: Vec<u8>,
|
||||
current_proof: Vec<u8>,
|
||||
) -> Result<()> {
|
||||
let x = ¤t_proof[192..224];
|
||||
let y = ¤t_proof[224..256];
|
||||
|
||||
let prev_x = &previous_proof[192..224];
|
||||
let prev_y = &previous_proof[224..256];
|
||||
if x == prev_x && y == prev_y {
|
||||
return Err(eyre!("this exact message and signal has already been sent"));
|
||||
}
|
||||
|
||||
let mut proof1 = Cursor::new(previous_proof);
|
||||
let mut proof2 = Cursor::new(current_proof);
|
||||
let mut output = Cursor::new(Vec::new());
|
||||
|
||||
match self
|
||||
.rln
|
||||
.recover_id_secret(&mut proof1, &mut proof2, &mut output)
|
||||
{
|
||||
Ok(_) => {
|
||||
let output_data = output.into_inner();
|
||||
let (leaked_identity_secret_hash, _) = IdSecret::from_bytes_le(&output_data);
|
||||
|
||||
if let Some((user_index, identity)) = self
|
||||
.local_identities
|
||||
.iter()
|
||||
.find(|(_, identity)| {
|
||||
identity.identity_secret_hash == leaked_identity_secret_hash
|
||||
})
|
||||
.map(|(index, identity)| (*index, identity))
|
||||
{
|
||||
let real_identity_secret_hash = identity.identity_secret_hash.clone();
|
||||
if leaked_identity_secret_hash != real_identity_secret_hash {
|
||||
Err(eyre!("identity secret hash mismatch: leaked_identity_secret_hash != real_identity_secret_hash"))
|
||||
} else {
|
||||
println!(
|
||||
"DUPLICATE message ID detected! Reveal identity secret hash: {}",
|
||||
*leaked_identity_secret_hash
|
||||
);
|
||||
self.local_identities.remove(&user_index);
|
||||
self.rln.delete_leaf(user_index)?;
|
||||
println!("User index {user_index} has been SLASHED");
|
||||
Ok(())
|
||||
}
|
||||
} else {
|
||||
Err(eyre!("user identity secret hash ******** not found"))
|
||||
}
|
||||
}
|
||||
Err(err) => Err(eyre!("Failed to recover identity secret: {err}")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
println!("Initializing RLN instance...");
|
||||
print!("\x1B[2J\x1B[1;1H");
|
||||
let mut rln_system = RLNSystem::new()?;
|
||||
let rln_epoch = hash_to_field_le(b"epoch");
|
||||
let rln_identifier = hash_to_field_le(b"rln-identifier");
|
||||
let external_nullifier = poseidon_hash(&[rln_epoch, rln_identifier]);
|
||||
println!("RLN Relay Example:");
|
||||
println!("Message Limit: {MESSAGE_LIMIT}");
|
||||
println!("----------------------------------");
|
||||
println!();
|
||||
show_commands();
|
||||
loop {
|
||||
print!("\n> ");
|
||||
stdout().flush()?;
|
||||
let mut input = String::new();
|
||||
stdin().read_line(&mut input)?;
|
||||
let trimmed = input.trim();
|
||||
let args = std::iter::once("").chain(trimmed.split_whitespace());
|
||||
|
||||
match Cli::try_parse_from(args) {
|
||||
Ok(cli) => match cli.command {
|
||||
Commands::List => {
|
||||
rln_system.list_users();
|
||||
}
|
||||
Commands::Register => {
|
||||
rln_system.register_user()?;
|
||||
}
|
||||
Commands::Send {
|
||||
user_index,
|
||||
message_id,
|
||||
signal,
|
||||
} => {
|
||||
match rln_system.generate_proof(
|
||||
user_index,
|
||||
message_id,
|
||||
&signal,
|
||||
external_nullifier,
|
||||
) {
|
||||
Ok(proof) => {
|
||||
if let Err(err) = rln_system.verify_proof(proof, &signal) {
|
||||
println!("Verification error: {err}");
|
||||
};
|
||||
}
|
||||
Err(err) => {
|
||||
println!("Proof generation error: {err}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Commands::Clear => {
|
||||
print!("\x1B[2J\x1B[1;1H");
|
||||
show_commands();
|
||||
}
|
||||
Commands::Exit => {
|
||||
break;
|
||||
}
|
||||
},
|
||||
Err(err) => {
|
||||
eprintln!("Command error: {err}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn show_commands() {
|
||||
println!("Available commands:");
|
||||
println!(" list - List registered users");
|
||||
println!(" register - Register a new user index");
|
||||
println!(" send -u <index> -m <message_id> -s <signal> - Send a message with proof");
|
||||
println!(" clear - Clear the screen");
|
||||
println!(" exit - Exit the program");
|
||||
}
|
||||
316
rln-cli/src/examples/stateless.rs
Normal file
316
rln-cli/src/examples/stateless.rs
Normal file
@@ -0,0 +1,316 @@
|
||||
#![cfg(feature = "stateless")]
|
||||
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
io::{stdin, stdout, Cursor, Write},
|
||||
};
|
||||
|
||||
use clap::{Parser, Subcommand};
|
||||
use color_eyre::{eyre::eyre, Result};
|
||||
use rln::{
|
||||
circuit::{Fr, TEST_TREE_DEPTH},
|
||||
hashers::{hash_to_field_le, poseidon_hash, PoseidonHash},
|
||||
protocol::{keygen, prepare_verify_input, rln_witness_from_values, serialize_witness},
|
||||
public::RLN,
|
||||
utils::{fr_to_bytes_le, IdSecret},
|
||||
};
|
||||
use zerokit_utils::{OptimalMerkleTree, ZerokitMerkleProof, ZerokitMerkleTree};
|
||||
|
||||
const MESSAGE_LIMIT: u32 = 1;
|
||||
|
||||
type ConfigOf<T> = <T as ZerokitMerkleTree>::Config;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(author, version, about, long_about = None)]
|
||||
struct Cli {
|
||||
#[command(subcommand)]
|
||||
command: Commands,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum Commands {
|
||||
List,
|
||||
Register,
|
||||
Send {
|
||||
#[arg(short, long)]
|
||||
user_index: usize,
|
||||
#[arg(short, long)]
|
||||
message_id: u32,
|
||||
#[arg(short, long)]
|
||||
signal: String,
|
||||
},
|
||||
Clear,
|
||||
Exit,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Identity {
|
||||
identity_secret_hash: IdSecret,
|
||||
id_commitment: Fr,
|
||||
}
|
||||
|
||||
impl Identity {
|
||||
fn new() -> Self {
|
||||
let (identity_secret_hash, id_commitment) = keygen();
|
||||
Identity {
|
||||
identity_secret_hash,
|
||||
id_commitment,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct RLNSystem {
|
||||
rln: RLN,
|
||||
tree: OptimalMerkleTree<PoseidonHash>,
|
||||
used_nullifiers: HashMap<[u8; 32], Vec<u8>>,
|
||||
local_identities: HashMap<usize, Identity>,
|
||||
}
|
||||
|
||||
impl RLNSystem {
|
||||
fn new() -> Result<Self> {
|
||||
let rln = RLN::new()?;
|
||||
let default_leaf = Fr::from(0);
|
||||
let tree: OptimalMerkleTree<PoseidonHash> = OptimalMerkleTree::new(
|
||||
TEST_TREE_DEPTH,
|
||||
default_leaf,
|
||||
ConfigOf::<OptimalMerkleTree<PoseidonHash>>::default(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
Ok(RLNSystem {
|
||||
rln,
|
||||
tree,
|
||||
used_nullifiers: HashMap::new(),
|
||||
local_identities: HashMap::new(),
|
||||
})
|
||||
}
|
||||
|
||||
fn list_users(&self) {
|
||||
if self.local_identities.is_empty() {
|
||||
println!("No users registered yet.");
|
||||
return;
|
||||
}
|
||||
|
||||
println!("Registered users:");
|
||||
for (index, identity) in &self.local_identities {
|
||||
println!("User Index: {index}");
|
||||
println!("+ Identity Secret Hash: {}", *identity.identity_secret_hash);
|
||||
println!("+ Identity Commitment: {}", identity.id_commitment);
|
||||
println!();
|
||||
}
|
||||
}
|
||||
|
||||
fn register_user(&mut self) -> Result<usize> {
|
||||
let index = self.tree.leaves_set();
|
||||
let identity = Identity::new();
|
||||
|
||||
let rate_commitment = poseidon_hash(&[identity.id_commitment, Fr::from(MESSAGE_LIMIT)]);
|
||||
self.tree.update_next(rate_commitment)?;
|
||||
|
||||
println!("Registered User Index: {index}");
|
||||
println!("+ Identity secret hash: {}", *identity.identity_secret_hash);
|
||||
println!("+ Identity commitment: {}", identity.id_commitment);
|
||||
|
||||
self.local_identities.insert(index, identity);
|
||||
Ok(index)
|
||||
}
|
||||
|
||||
fn generate_proof(
|
||||
&mut self,
|
||||
user_index: usize,
|
||||
message_id: u32,
|
||||
signal: &str,
|
||||
external_nullifier: Fr,
|
||||
) -> Result<Vec<u8>> {
|
||||
let identity = match self.local_identities.get(&user_index) {
|
||||
Some(identity) => identity,
|
||||
None => return Err(eyre!("user index {user_index} not found")),
|
||||
};
|
||||
|
||||
let merkle_proof = self.tree.proof(user_index)?;
|
||||
let x = hash_to_field_le(signal.as_bytes());
|
||||
|
||||
let rln_witness = rln_witness_from_values(
|
||||
identity.identity_secret_hash.clone(),
|
||||
merkle_proof.get_path_elements(),
|
||||
merkle_proof.get_path_index(),
|
||||
x,
|
||||
external_nullifier,
|
||||
Fr::from(MESSAGE_LIMIT),
|
||||
Fr::from(message_id),
|
||||
)?;
|
||||
|
||||
let serialized = serialize_witness(&rln_witness)?;
|
||||
let mut input_buffer = Cursor::new(serialized);
|
||||
let mut output_buffer = Cursor::new(Vec::new());
|
||||
|
||||
self.rln
|
||||
.generate_rln_proof_with_witness(&mut input_buffer, &mut output_buffer)?;
|
||||
|
||||
println!("Proof generated successfully:");
|
||||
println!("+ User Index: {user_index}");
|
||||
println!("+ Message ID: {message_id}");
|
||||
println!("+ Signal: {signal}");
|
||||
|
||||
Ok(output_buffer.into_inner())
|
||||
}
|
||||
|
||||
fn verify_proof(&mut self, proof_data: Vec<u8>, signal: &str) -> Result<()> {
|
||||
let proof_with_signal = prepare_verify_input(proof_data.clone(), signal.as_bytes());
|
||||
let mut input_buffer = Cursor::new(proof_with_signal);
|
||||
|
||||
let root = self.tree.root();
|
||||
let roots_serialized = fr_to_bytes_le(&root);
|
||||
let mut roots_buffer = Cursor::new(roots_serialized);
|
||||
|
||||
match self
|
||||
.rln
|
||||
.verify_with_roots(&mut input_buffer, &mut roots_buffer)
|
||||
{
|
||||
Ok(true) => {
|
||||
let nullifier = &proof_data[256..288];
|
||||
let nullifier_key: [u8; 32] = nullifier.try_into()?;
|
||||
|
||||
if let Some(previous_proof) = self.used_nullifiers.get(&nullifier_key) {
|
||||
self.handle_duplicate_message_id(previous_proof.clone(), proof_data)?;
|
||||
return Ok(());
|
||||
}
|
||||
self.used_nullifiers.insert(nullifier_key, proof_data);
|
||||
println!("Message verified and accepted");
|
||||
}
|
||||
Ok(false) => {
|
||||
println!("Verification failed: message_id must be unique within the epoch and satisfy 0 <= message_id < MESSAGE_LIMIT: {MESSAGE_LIMIT}");
|
||||
}
|
||||
Err(err) => return Err(err.into()),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_duplicate_message_id(
|
||||
&mut self,
|
||||
previous_proof: Vec<u8>,
|
||||
current_proof: Vec<u8>,
|
||||
) -> Result<()> {
|
||||
let x = ¤t_proof[192..224];
|
||||
let y = ¤t_proof[224..256];
|
||||
|
||||
let prev_x = &previous_proof[192..224];
|
||||
let prev_y = &previous_proof[224..256];
|
||||
if x == prev_x && y == prev_y {
|
||||
return Err(eyre!("this exact message and signal has already been sent"));
|
||||
}
|
||||
|
||||
let mut proof1 = Cursor::new(previous_proof);
|
||||
let mut proof2 = Cursor::new(current_proof);
|
||||
let mut output = Cursor::new(Vec::new());
|
||||
|
||||
match self
|
||||
.rln
|
||||
.recover_id_secret(&mut proof1, &mut proof2, &mut output)
|
||||
{
|
||||
Ok(_) => {
|
||||
let output_data = output.into_inner();
|
||||
let (leaked_identity_secret_hash, _) = IdSecret::from_bytes_le(&output_data);
|
||||
|
||||
if let Some((user_index, identity)) = self
|
||||
.local_identities
|
||||
.iter()
|
||||
.find(|(_, identity)| {
|
||||
identity.identity_secret_hash == leaked_identity_secret_hash
|
||||
})
|
||||
.map(|(index, identity)| (*index, identity))
|
||||
{
|
||||
let real_identity_secret_hash = identity.identity_secret_hash.clone();
|
||||
if leaked_identity_secret_hash != real_identity_secret_hash {
|
||||
Err(eyre!("identity secret hash mismatch: leaked_identity_secret_hash != real_identity_secret_hash"))
|
||||
} else {
|
||||
println!(
|
||||
"DUPLICATE message ID detected! Reveal identity secret hash: ********"
|
||||
);
|
||||
self.local_identities.remove(&user_index);
|
||||
println!("User index {user_index} has been SLASHED");
|
||||
Ok(())
|
||||
}
|
||||
} else {
|
||||
Err(eyre!("user identity secret hash ******** not found"))
|
||||
}
|
||||
}
|
||||
Err(err) => Err(eyre!("Failed to recover identity secret: {err}")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
println!("Initializing RLN instance...");
|
||||
print!("\x1B[2J\x1B[1;1H");
|
||||
let mut rln_system = RLNSystem::new()?;
|
||||
let rln_epoch = hash_to_field_le(b"epoch");
|
||||
let rln_identifier = hash_to_field_le(b"rln-identifier");
|
||||
let external_nullifier = poseidon_hash(&[rln_epoch, rln_identifier]);
|
||||
println!("RLN Stateless Relay Example:");
|
||||
println!("Message Limit: {MESSAGE_LIMIT}");
|
||||
println!("----------------------------------");
|
||||
println!();
|
||||
show_commands();
|
||||
|
||||
loop {
|
||||
print!("\n> ");
|
||||
stdout().flush()?;
|
||||
let mut input = String::new();
|
||||
stdin().read_line(&mut input)?;
|
||||
let trimmed = input.trim();
|
||||
let args = std::iter::once("").chain(trimmed.split_whitespace());
|
||||
|
||||
match Cli::try_parse_from(args) {
|
||||
Ok(cli) => match cli.command {
|
||||
Commands::List => {
|
||||
rln_system.list_users();
|
||||
}
|
||||
Commands::Register => {
|
||||
rln_system.register_user()?;
|
||||
}
|
||||
Commands::Send {
|
||||
user_index,
|
||||
message_id,
|
||||
signal,
|
||||
} => {
|
||||
match rln_system.generate_proof(
|
||||
user_index,
|
||||
message_id,
|
||||
&signal,
|
||||
external_nullifier,
|
||||
) {
|
||||
Ok(proof) => {
|
||||
if let Err(err) = rln_system.verify_proof(proof, &signal) {
|
||||
println!("Verification error: {err}");
|
||||
};
|
||||
}
|
||||
Err(err) => {
|
||||
println!("Proof generation error: {err}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Commands::Clear => {
|
||||
print!("\x1B[2J\x1B[1;1H");
|
||||
show_commands();
|
||||
}
|
||||
Commands::Exit => {
|
||||
break;
|
||||
}
|
||||
},
|
||||
Err(err) => {
|
||||
eprintln!("Command error: {err}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn show_commands() {
|
||||
println!("Available commands:");
|
||||
println!(" list - List registered users");
|
||||
println!(" register - Register a new user index");
|
||||
println!(" send -u <index> -m <message_id> -s <signal> - Send a message with proof");
|
||||
println!(" clear - Clear the screen");
|
||||
println!(" exit - Exit the program");
|
||||
}
|
||||
@@ -1,9 +1,18 @@
|
||||
use std::{fs::File, io::Read, path::Path};
|
||||
use std::{
|
||||
fs::File,
|
||||
io::{Cursor, Read},
|
||||
path::Path,
|
||||
};
|
||||
|
||||
use clap::Parser;
|
||||
use color_eyre::{Report, Result};
|
||||
use color_eyre::{eyre::Report, Result};
|
||||
use commands::Commands;
|
||||
use rln::public::RLN;
|
||||
use config::Config;
|
||||
use rln::{
|
||||
public::RLN,
|
||||
utils::{bytes_le_to_fr, bytes_le_to_vec_fr},
|
||||
};
|
||||
use serde_json::json;
|
||||
use state::State;
|
||||
|
||||
mod commands;
|
||||
@@ -20,78 +29,90 @@ struct Cli {
|
||||
fn main() -> Result<()> {
|
||||
let cli = Cli::parse();
|
||||
|
||||
let mut state = State::load_state()?;
|
||||
let mut state = match &cli.command {
|
||||
Some(Commands::New { .. }) | Some(Commands::NewWithParams { .. }) => State::default(),
|
||||
_ => State::load_state()?,
|
||||
};
|
||||
|
||||
match &cli.command {
|
||||
Some(Commands::New {
|
||||
tree_height,
|
||||
config,
|
||||
}) => {
|
||||
let resources = File::open(&config)?;
|
||||
state.rln = Some(RLN::new(*tree_height, resources)?);
|
||||
match cli.command {
|
||||
Some(Commands::New { tree_depth }) => {
|
||||
let config = Config::load_config()?;
|
||||
state.rln = if let Some(tree_config) = config.tree_config {
|
||||
println!("Initializing RLN with custom config");
|
||||
Some(RLN::new(tree_depth, Cursor::new(tree_config.as_bytes()))?)
|
||||
} else {
|
||||
println!("Initializing RLN with default config");
|
||||
Some(RLN::new(tree_depth, Cursor::new(json!({}).to_string()))?)
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
Some(Commands::NewWithParams {
|
||||
tree_height,
|
||||
config,
|
||||
tree_config_input,
|
||||
tree_depth,
|
||||
resources_path,
|
||||
}) => {
|
||||
let mut resources: Vec<Vec<u8>> = Vec::new();
|
||||
#[cfg(feature = "arkzkey")]
|
||||
let filenames = ["rln.wasm", "rln_final.arkzkey", "verification_key.arkvkey"];
|
||||
#[cfg(not(feature = "arkzkey"))]
|
||||
let filenames = ["rln.wasm", "rln_final.zkey", "verification_key.arkvkey"];
|
||||
let filenames = ["rln_final.arkzkey", "graph.bin"];
|
||||
for filename in filenames {
|
||||
let fullpath = config.join(Path::new(filename));
|
||||
let fullpath = resources_path.join(Path::new(filename));
|
||||
let mut file = File::open(&fullpath)?;
|
||||
let metadata = std::fs::metadata(&fullpath)?;
|
||||
let mut buffer = vec![0; metadata.len() as usize];
|
||||
file.read_exact(&mut buffer)?;
|
||||
resources.push(buffer);
|
||||
}
|
||||
let tree_config_input_file = File::open(&tree_config_input)?;
|
||||
state.rln = Some(RLN::new_with_params(
|
||||
*tree_height,
|
||||
resources[0].clone(),
|
||||
resources[1].clone(),
|
||||
resources[2].clone(),
|
||||
tree_config_input_file,
|
||||
)?);
|
||||
let config = Config::load_config()?;
|
||||
if let Some(tree_config) = config.tree_config {
|
||||
println!("Initializing RLN with custom config");
|
||||
state.rln = Some(RLN::new_with_params(
|
||||
tree_depth,
|
||||
resources[0].clone(),
|
||||
resources[1].clone(),
|
||||
Cursor::new(tree_config.to_string().as_bytes()),
|
||||
)?)
|
||||
} else {
|
||||
println!("Initializing RLN with default config");
|
||||
state.rln = Some(RLN::new_with_params(
|
||||
tree_depth,
|
||||
resources[0].clone(),
|
||||
resources[1].clone(),
|
||||
Cursor::new(json!({}).to_string()),
|
||||
)?)
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
Some(Commands::SetTree { tree_height }) => {
|
||||
Some(Commands::SetTree { tree_depth }) => {
|
||||
state
|
||||
.rln
|
||||
.ok_or(Report::msg("no RLN instance initialized"))?
|
||||
.set_tree(*tree_height)?;
|
||||
.set_tree(tree_depth)?;
|
||||
Ok(())
|
||||
}
|
||||
Some(Commands::SetLeaf { index, file }) => {
|
||||
let input_data = File::open(&file)?;
|
||||
Some(Commands::SetLeaf { index, input }) => {
|
||||
let input_data = File::open(input)?;
|
||||
state
|
||||
.rln
|
||||
.ok_or(Report::msg("no RLN instance initialized"))?
|
||||
.set_leaf(*index, input_data)?;
|
||||
.set_leaf(index, input_data)?;
|
||||
Ok(())
|
||||
}
|
||||
Some(Commands::SetMultipleLeaves { index, file }) => {
|
||||
let input_data = File::open(&file)?;
|
||||
Some(Commands::SetMultipleLeaves { index, input }) => {
|
||||
let input_data = File::open(input)?;
|
||||
state
|
||||
.rln
|
||||
.ok_or(Report::msg("no RLN instance initialized"))?
|
||||
.set_leaves_from(*index, input_data)?;
|
||||
.set_leaves_from(index, input_data)?;
|
||||
Ok(())
|
||||
}
|
||||
Some(Commands::ResetMultipleLeaves { file }) => {
|
||||
let input_data = File::open(&file)?;
|
||||
Some(Commands::ResetMultipleLeaves { input }) => {
|
||||
let input_data = File::open(input)?;
|
||||
state
|
||||
.rln
|
||||
.ok_or(Report::msg("no RLN instance initialized"))?
|
||||
.init_tree_with_leaves(input_data)?;
|
||||
Ok(())
|
||||
}
|
||||
Some(Commands::SetNextLeaf { file }) => {
|
||||
let input_data = File::open(&file)?;
|
||||
Some(Commands::SetNextLeaf { input }) => {
|
||||
let input_data = File::open(input)?;
|
||||
state
|
||||
.rln
|
||||
.ok_or(Report::msg("no RLN instance initialized"))?
|
||||
@@ -102,60 +123,73 @@ fn main() -> Result<()> {
|
||||
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)?;
|
||||
.delete_leaf(index)?;
|
||||
Ok(())
|
||||
}
|
||||
Some(Commands::Prove { input }) => {
|
||||
let input_data = File::open(&input)?;
|
||||
let writer = std::io::stdout();
|
||||
let input_data = File::open(input)?;
|
||||
let mut output_buffer = Cursor::new(Vec::<u8>::new());
|
||||
state
|
||||
.rln
|
||||
.ok_or(Report::msg("no RLN instance initialized"))?
|
||||
.prove(input_data, writer)?;
|
||||
.prove(input_data, &mut output_buffer)?;
|
||||
let proof = output_buffer.into_inner();
|
||||
println!("proof: {proof:?}");
|
||||
Ok(())
|
||||
}
|
||||
Some(Commands::Verify { file }) => {
|
||||
let input_data = File::open(&file)?;
|
||||
state
|
||||
Some(Commands::Verify { input }) => {
|
||||
let input_data = File::open(input)?;
|
||||
let verified = state
|
||||
.rln
|
||||
.ok_or(Report::msg("no RLN instance initialized"))?
|
||||
.verify(input_data)?;
|
||||
println!("verified: {verified:?}");
|
||||
Ok(())
|
||||
}
|
||||
Some(Commands::GenerateProof { input }) => {
|
||||
let input_data = File::open(&input)?;
|
||||
let writer = std::io::stdout();
|
||||
let input_data = File::open(input)?;
|
||||
let mut output_buffer = Cursor::new(Vec::<u8>::new());
|
||||
state
|
||||
.rln
|
||||
.ok_or(Report::msg("no RLN instance initialized"))?
|
||||
.generate_rln_proof(input_data, writer)?;
|
||||
.generate_rln_proof(input_data, &mut output_buffer)?;
|
||||
let proof = output_buffer.into_inner();
|
||||
println!("proof: {proof:?}");
|
||||
Ok(())
|
||||
}
|
||||
Some(Commands::VerifyWithRoots { input, roots }) => {
|
||||
let input_data = File::open(&input)?;
|
||||
let roots_data = File::open(&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(())
|
||||
}
|
||||
Some(Commands::GetRoot) => {
|
||||
let mut output_buffer = Cursor::new(Vec::<u8>::new());
|
||||
state
|
||||
.rln
|
||||
.ok_or(Report::msg("no RLN instance initialized"))?
|
||||
.get_root(&mut output_buffer)
|
||||
.unwrap();
|
||||
let (root, _) = bytes_le_to_fr(&output_buffer.into_inner());
|
||||
println!("root: {root}");
|
||||
Ok(())
|
||||
}
|
||||
Some(Commands::GetProof { index }) => {
|
||||
let mut output_buffer = Cursor::new(Vec::<u8>::new());
|
||||
state
|
||||
.rln
|
||||
.ok_or(Report::msg("no RLN instance initialized"))?
|
||||
.get_proof(index, &mut output_buffer)?;
|
||||
let output_buffer_inner = output_buffer.into_inner();
|
||||
let (path_elements, _) = bytes_le_to_vec_fr(&output_buffer_inner)?;
|
||||
for (index, element) in path_elements.iter().enumerate() {
|
||||
println!("path element {index}: {element}");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
None => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
use color_eyre::Result;
|
||||
use rln::public::RLN;
|
||||
use std::fs::File;
|
||||
use std::io::Cursor;
|
||||
|
||||
use crate::config::{Config, InnerConfig};
|
||||
use color_eyre::Result;
|
||||
use rln::{circuit::TEST_TREE_DEPTH, public::RLN};
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::config::Config;
|
||||
|
||||
#[derive(Default)]
|
||||
pub(crate) struct State {
|
||||
@@ -12,9 +14,15 @@ pub(crate) struct State {
|
||||
impl State {
|
||||
pub(crate) fn load_state() -> Result<State> {
|
||||
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)?)
|
||||
let rln = if let Some(tree_config) = config.tree_config {
|
||||
let config_json: Value = serde_json::from_str(&tree_config)?;
|
||||
let tree_depth = config_json["tree_depth"]
|
||||
.as_u64()
|
||||
.unwrap_or(TEST_TREE_DEPTH as u64);
|
||||
Some(RLN::new(
|
||||
tree_depth as usize,
|
||||
Cursor::new(tree_config.as_bytes()),
|
||||
)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
21
rln-wasm-utils/.gitignore
vendored
Normal file
21
rln-wasm-utils/.gitignore
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
# Common files to ignore in Rust projects
|
||||
.DS_Store
|
||||
.idea
|
||||
*.log
|
||||
tmp/
|
||||
|
||||
# Generated by Cargo will have compiled files and executables
|
||||
/target
|
||||
Cargo.lock
|
||||
|
||||
# Generated by rln-wasm
|
||||
pkg/
|
||||
|
||||
# Generated by Nix
|
||||
result
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
||||
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||
*.pdb
|
||||
35
rln-wasm-utils/Cargo.toml
Normal file
35
rln-wasm-utils/Cargo.toml
Normal file
@@ -0,0 +1,35 @@
|
||||
[package]
|
||||
name = "rln-wasm-utils"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib", "rlib"]
|
||||
|
||||
[dependencies]
|
||||
# TODO: remove this once we have a proper release
|
||||
rln = { path = "../rln", default-features = false, features = ["stateless"] }
|
||||
js-sys = "0.3.77"
|
||||
wasm-bindgen = "0.2.100"
|
||||
rand = "0.8.5"
|
||||
|
||||
# The `console_error_panic_xhook` crate provides better debugging of panics by
|
||||
# logging them with `console.error`. This is great for development, but requires
|
||||
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
|
||||
# code size when deploying.
|
||||
console_error_panic_hook = { version = "0.1.7", optional = true }
|
||||
|
||||
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
||||
getrandom = { version = "0.2.16", features = ["js"] }
|
||||
|
||||
[dev-dependencies]
|
||||
wasm-bindgen-test = "0.3.50"
|
||||
web-sys = { version = "0.3.77", features = ["console"] }
|
||||
ark-std = { version = "0.5.0", default-features = false }
|
||||
|
||||
[features]
|
||||
default = ["console_error_panic_hook"]
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
all-features = true
|
||||
36
rln-wasm-utils/Makefile.toml
Normal file
36
rln-wasm-utils/Makefile.toml
Normal file
@@ -0,0 +1,36 @@
|
||||
[tasks.build]
|
||||
clear = true
|
||||
dependencies = ["pack_build", "pack_rename", "pack_resize"]
|
||||
|
||||
[tasks.pack_build]
|
||||
command = "wasm-pack"
|
||||
args = ["build", "--release", "--target", "web", "--scope", "waku"]
|
||||
|
||||
[tasks.pack_rename]
|
||||
script = "sed -i.bak 's/rln-wasm-utils/zerokit-rln-wasm-utils/g' pkg/package.json && rm pkg/package.json.bak"
|
||||
|
||||
[tasks.pack_resize]
|
||||
command = "wasm-opt"
|
||||
args = [
|
||||
"pkg/rln_wasm_utils_bg.wasm",
|
||||
"-Oz",
|
||||
"--strip-debug",
|
||||
"--strip-dwarf",
|
||||
"--remove-unused-module-elements",
|
||||
"--vacuum",
|
||||
"-o",
|
||||
"pkg/rln_wasm_utils_bg.wasm",
|
||||
]
|
||||
|
||||
[tasks.test]
|
||||
command = "wasm-pack"
|
||||
args = [
|
||||
"test",
|
||||
"--release",
|
||||
"--node",
|
||||
"--target",
|
||||
"wasm32-unknown-unknown",
|
||||
"--",
|
||||
"--nocapture",
|
||||
]
|
||||
dependencies = ["build"]
|
||||
206
rln-wasm-utils/README.md
Normal file
206
rln-wasm-utils/README.md
Normal file
@@ -0,0 +1,206 @@
|
||||
# RLN WASM Utils
|
||||
|
||||
[](https://badge.fury.io/js/@waku%2Fzerokit-rln-wasm-utils)
|
||||
[](https://opensource.org/licenses/MIT)
|
||||
[](https://opensource.org/licenses/Apache-2.0)
|
||||
|
||||
The Zerokit RLN WASM Utils Module provides WebAssembly bindings for Rate-Limiting Nullifier [RLN](https://rfc.vac.dev/spec/32/) cryptographic primitives.
|
||||
This module offers comprehensive functionality for identity generation and hashing needed for RLN applications.
|
||||
|
||||
## Features
|
||||
|
||||
### Identity Generation
|
||||
|
||||
- **Random Identity Generation**: Generate cryptographically secure random identities
|
||||
- **Seeded Identity Generation**: Generate deterministic identities from seeds
|
||||
- **Extended Identity Generation**: Generate extended identities with additional parameters
|
||||
- **Seeded Extended Identity Generation**: Generate deterministic extended identities from seeds
|
||||
- **Endianness Support**: Both little-endian and big-endian serialization support
|
||||
|
||||
### Hashing
|
||||
|
||||
- **Standard Hashing**: Hash arbitrary data to field elements
|
||||
- **Poseidon Hashing**: Advanced cryptographic hashing using Poseidon hash function
|
||||
- **Endianness Support**: Both little-endian and big-endian serialization support
|
||||
|
||||
## API Reference
|
||||
|
||||
### Identity Generation Functions
|
||||
|
||||
#### `generateMembershipKey(isLittleEndian: boolean): Uint8Array`
|
||||
|
||||
Generates a random membership key pair (identity secret and commitment).
|
||||
|
||||
**Inputs:**
|
||||
|
||||
- `isLittleEndian`: Boolean indicating endianness for serialization
|
||||
|
||||
**Outputs:** Serialized identity pair as `Uint8Array` in corresponding endianness
|
||||
|
||||
#### `generateExtendedMembershipKey(isLittleEndian: boolean): Uint8Array`
|
||||
|
||||
Generates an extended membership key with additional parameters.
|
||||
|
||||
**Inputs:**
|
||||
|
||||
- `isLittleEndian`: Boolean indicating endianness for serialization
|
||||
|
||||
**Outputs:** Serialized extended identity tuple as `Uint8Array` in corresponding endianness
|
||||
|
||||
#### `generateSeededMembershipKey(seed: Uint8Array, isLittleEndian: boolean): Uint8Array`
|
||||
|
||||
Generates a deterministic membership key from a seed.
|
||||
|
||||
**Inputs:**
|
||||
|
||||
- `seed`: Seed data as `Uint8Array`
|
||||
- `isLittleEndian`: Boolean indicating endianness for serialization
|
||||
|
||||
**Outputs:** Serialized identity pair as `Uint8Array` in corresponding endianness
|
||||
|
||||
#### `generateSeededExtendedMembershipKey(seed: Uint8Array, isLittleEndian: boolean): Uint8Array`
|
||||
|
||||
Generates a deterministic extended membership key from a seed.
|
||||
|
||||
**Inputs:**
|
||||
|
||||
- `seed`: Seed data as `Uint8Array`
|
||||
- `isLittleEndian`: Boolean indicating endianness for serialization
|
||||
|
||||
**Outputs:** Serialized extended identity tuple as `Uint8Array` in corresponding endianness
|
||||
|
||||
### Hashing Functions
|
||||
|
||||
#### `hash(input: Uint8Array, isLittleEndian: boolean): Uint8Array`
|
||||
|
||||
Hashes input data to a field element.
|
||||
|
||||
**Inputs:**
|
||||
|
||||
- `input`: Input data as `Uint8Array`
|
||||
- `isLittleEndian`: Boolean indicating endianness for serialization
|
||||
|
||||
**Outputs:** Serialized hash result as `Uint8Array` in corresponding endianness
|
||||
|
||||
#### `poseidonHash(input: Uint8Array, isLittleEndian: boolean): Uint8Array`
|
||||
|
||||
Computes Poseidon hash of input field elements.
|
||||
|
||||
**Inputs:**
|
||||
|
||||
- `input`: Serialized field elements as `Uint8Array` (format: length + field elements)
|
||||
- `isLittleEndian`: Boolean indicating endianness for serialization
|
||||
|
||||
**Outputs:** Serialized hash result as `Uint8Array` in corresponding endianness
|
||||
|
||||
## Usage Examples
|
||||
|
||||
### JavaScript/TypeScript
|
||||
|
||||
```javascript
|
||||
import init, {
|
||||
generateMembershipKey,
|
||||
generateSeededMembershipKey,
|
||||
hash,
|
||||
poseidonHash
|
||||
} from '@waku/zerokit-rln-wasm-utils';
|
||||
|
||||
// Initialize the WASM module
|
||||
await init();
|
||||
|
||||
// Generate a random membership key
|
||||
const membershipKey = generateMembershipKey(true); // little-endian
|
||||
console.log('Membership key:', membershipKey);
|
||||
|
||||
// Generate a deterministic membership key from seed
|
||||
const seed = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
|
||||
const seededKey = generateSeededMembershipKey(seed, true);
|
||||
console.log('Seeded key:', seededKey);
|
||||
|
||||
// Hash some data
|
||||
const input = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
|
||||
const hashResult = hash(input, true);
|
||||
console.log('Hash result:', hashResult);
|
||||
|
||||
// Poseidon hash with field elements
|
||||
const fieldElements = new Uint8Array([
|
||||
// Length (8 bytes) + field elements (32 bytes each)
|
||||
1, 0, 0, 0, 0, 0, 0, 0, // length = 1
|
||||
// field element data...
|
||||
]);
|
||||
const poseidonResult = poseidonHash(fieldElements, true);
|
||||
console.log('Poseidon hash:', poseidonResult);
|
||||
```
|
||||
|
||||
## Install Dependencies
|
||||
|
||||
> [!NOTE]
|
||||
> This project requires the following tools:
|
||||
>
|
||||
> - `wasm-pack` - for compiling Rust to WebAssembly
|
||||
> - `cargo-make` - for running build commands
|
||||
> - `nvm` - to install and manage Node.js
|
||||
>
|
||||
> Ensure all dependencies are installed before proceeding.
|
||||
|
||||
### Manually
|
||||
|
||||
#### Install `wasm-pack`
|
||||
|
||||
```bash
|
||||
cargo install wasm-pack --version=0.13.1
|
||||
```
|
||||
|
||||
#### Install `cargo-make`
|
||||
|
||||
```bash
|
||||
cargo install cargo-make
|
||||
```
|
||||
|
||||
#### Install `Node.js`
|
||||
|
||||
If you don't have `nvm` (Node Version Manager), install it by following
|
||||
the [installation instructions](https://github.com/nvm-sh/nvm?tab=readme-ov-file#install--update-script).
|
||||
|
||||
After installing `nvm`, install and use Node.js `v22.14.0`:
|
||||
|
||||
```bash
|
||||
nvm install 22.14.0
|
||||
nvm use 22.14.0
|
||||
nvm alias default 22.14.0
|
||||
```
|
||||
|
||||
If you already have Node.js installed,
|
||||
check your version with `node -v` command — the version must be strictly greater than 22.
|
||||
|
||||
### Or install everything
|
||||
|
||||
You can run the following command from the root of the repository to install all required dependencies for `zerokit`
|
||||
|
||||
```bash
|
||||
make installdeps
|
||||
```
|
||||
|
||||
## Building the library
|
||||
|
||||
First, navigate to the rln-wasm-utils directory:
|
||||
|
||||
```bash
|
||||
cd rln-wasm-utils
|
||||
```
|
||||
|
||||
Compile rln-wasm-utils for `wasm32-unknown-unknown`:
|
||||
|
||||
```bash
|
||||
cargo make build
|
||||
```
|
||||
|
||||
## Running tests
|
||||
|
||||
```bash
|
||||
cargo make test
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
This project is licensed under both MIT and Apache 2.0 licenses. See the LICENSE files for details.
|
||||
112
rln-wasm-utils/src/lib.rs
Normal file
112
rln-wasm-utils/src/lib.rs
Normal file
@@ -0,0 +1,112 @@
|
||||
#![cfg(target_arch = "wasm32")]
|
||||
|
||||
use js_sys::Uint8Array;
|
||||
use rln::public::{
|
||||
extended_key_gen, hash, key_gen, poseidon_hash, seeded_extended_key_gen, seeded_key_gen,
|
||||
};
|
||||
use std::vec::Vec;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[wasm_bindgen(js_name = generateMembershipKey)]
|
||||
pub fn wasm_key_gen(is_little_endian: bool) -> Result<Uint8Array, String> {
|
||||
let mut output_data: Vec<u8> = Vec::new();
|
||||
if let Err(err) = key_gen(&mut output_data, is_little_endian) {
|
||||
std::mem::forget(output_data);
|
||||
Err(format!(
|
||||
"Msg: could not generate membership keys, Error: {:#?}",
|
||||
err
|
||||
))
|
||||
} else {
|
||||
let result = Uint8Array::from(&output_data[..]);
|
||||
std::mem::forget(output_data);
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[wasm_bindgen(js_name = generateExtendedMembershipKey)]
|
||||
pub fn wasm_extended_key_gen(is_little_endian: bool) -> Result<Uint8Array, String> {
|
||||
let mut output_data: Vec<u8> = Vec::new();
|
||||
if let Err(err) = extended_key_gen(&mut output_data, is_little_endian) {
|
||||
std::mem::forget(output_data);
|
||||
Err(format!(
|
||||
"Msg: could not generate membership keys, Error: {:#?}",
|
||||
err
|
||||
))
|
||||
} else {
|
||||
let result = Uint8Array::from(&output_data[..]);
|
||||
std::mem::forget(output_data);
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[wasm_bindgen(js_name = generateSeededMembershipKey)]
|
||||
pub fn wasm_seeded_key_gen(seed: Uint8Array, is_little_endian: bool) -> Result<Uint8Array, String> {
|
||||
let mut output_data: Vec<u8> = Vec::new();
|
||||
let input_data = &seed.to_vec()[..];
|
||||
if let Err(err) = seeded_key_gen(input_data, &mut output_data, is_little_endian) {
|
||||
std::mem::forget(output_data);
|
||||
Err(format!(
|
||||
"Msg: could not generate membership key, Error: {:#?}",
|
||||
err
|
||||
))
|
||||
} else {
|
||||
let result = Uint8Array::from(&output_data[..]);
|
||||
std::mem::forget(output_data);
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[wasm_bindgen(js_name = generateSeededExtendedMembershipKey)]
|
||||
pub fn wasm_seeded_extended_key_gen(
|
||||
seed: Uint8Array,
|
||||
is_little_endian: bool,
|
||||
) -> Result<Uint8Array, String> {
|
||||
let mut output_data: Vec<u8> = Vec::new();
|
||||
let input_data = &seed.to_vec()[..];
|
||||
if let Err(err) = seeded_extended_key_gen(input_data, &mut output_data, is_little_endian) {
|
||||
std::mem::forget(output_data);
|
||||
Err(format!(
|
||||
"Msg: could not generate membership key, Error: {:#?}",
|
||||
err
|
||||
))
|
||||
} else {
|
||||
let result = Uint8Array::from(&output_data[..]);
|
||||
std::mem::forget(output_data);
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = hash)]
|
||||
pub fn wasm_hash(input: Uint8Array, is_little_endian: bool) -> Result<Uint8Array, String> {
|
||||
let mut output_data: Vec<u8> = Vec::new();
|
||||
let input_data = &input.to_vec()[..];
|
||||
if let Err(err) = hash(input_data, &mut output_data, is_little_endian) {
|
||||
std::mem::forget(output_data);
|
||||
Err(format!("Msg: could not generate hash, Error: {:#?}", err))
|
||||
} else {
|
||||
let result = Uint8Array::from(&output_data[..]);
|
||||
std::mem::forget(output_data);
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = poseidonHash)]
|
||||
pub fn wasm_poseidon_hash(input: Uint8Array, is_little_endian: bool) -> Result<Uint8Array, String> {
|
||||
let mut output_data: Vec<u8> = Vec::new();
|
||||
let input_data = &input.to_vec()[..];
|
||||
if let Err(err) = poseidon_hash(input_data, &mut output_data, is_little_endian) {
|
||||
std::mem::forget(output_data);
|
||||
Err(format!(
|
||||
"Msg: could not generate poseidon hash, Error: {:#?}",
|
||||
err
|
||||
))
|
||||
} else {
|
||||
let result = Uint8Array::from(&output_data[..]);
|
||||
std::mem::forget(output_data);
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
114
rln-wasm-utils/tests/wasm_utils_test.rs
Normal file
114
rln-wasm-utils/tests/wasm_utils_test.rs
Normal file
@@ -0,0 +1,114 @@
|
||||
#![cfg(target_arch = "wasm32")]
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use ark_std::{UniformRand, rand::thread_rng};
|
||||
use rand::Rng;
|
||||
use rln::circuit::Fr;
|
||||
use rln::hashers::{ROUND_PARAMS, hash_to_field_le, poseidon_hash};
|
||||
use rln::protocol::{
|
||||
deserialize_identity_pair_be, deserialize_identity_pair_le, deserialize_identity_tuple_be,
|
||||
deserialize_identity_tuple_le,
|
||||
};
|
||||
use rln::utils::{bytes_le_to_fr, vec_fr_to_bytes_le};
|
||||
use rln_wasm_utils::{
|
||||
wasm_extended_key_gen, wasm_hash, wasm_key_gen, wasm_poseidon_hash,
|
||||
wasm_seeded_extended_key_gen, wasm_seeded_key_gen,
|
||||
};
|
||||
use wasm_bindgen_test::*;
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn test_wasm_key_gen() {
|
||||
let result_le = wasm_key_gen(true);
|
||||
assert!(result_le.is_ok());
|
||||
deserialize_identity_pair_le(result_le.unwrap().to_vec());
|
||||
|
||||
let result_be = wasm_key_gen(false);
|
||||
assert!(result_be.is_ok());
|
||||
deserialize_identity_pair_be(result_be.unwrap().to_vec());
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn test_wasm_extended_key_gen() {
|
||||
let result_le = wasm_extended_key_gen(true);
|
||||
assert!(result_le.is_ok());
|
||||
deserialize_identity_tuple_le(result_le.unwrap().to_vec());
|
||||
|
||||
let result_be = wasm_extended_key_gen(false);
|
||||
assert!(result_be.is_ok());
|
||||
deserialize_identity_tuple_be(result_be.unwrap().to_vec());
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn test_wasm_seeded_key_gen() {
|
||||
// Create a test seed
|
||||
let seed_data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
||||
let seed = js_sys::Uint8Array::from(&seed_data[..]);
|
||||
|
||||
let result_le = wasm_seeded_key_gen(seed.clone(), true);
|
||||
assert!(result_le.is_ok());
|
||||
let fr_le = deserialize_identity_pair_le(result_le.unwrap().to_vec());
|
||||
|
||||
let result_be = wasm_seeded_key_gen(seed, false);
|
||||
assert!(result_be.is_ok());
|
||||
let fr_be = deserialize_identity_pair_be(result_be.unwrap().to_vec());
|
||||
|
||||
assert_eq!(fr_le, fr_be);
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn test_wasm_seeded_extended_key_gen() {
|
||||
// Create a test seed
|
||||
let seed_data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
||||
let seed = js_sys::Uint8Array::from(&seed_data[..]);
|
||||
|
||||
let result_le = wasm_seeded_extended_key_gen(seed.clone(), true);
|
||||
assert!(result_le.is_ok());
|
||||
let fr_le = deserialize_identity_tuple_le(result_le.unwrap().to_vec());
|
||||
|
||||
let result_be = wasm_seeded_extended_key_gen(seed, false);
|
||||
assert!(result_be.is_ok());
|
||||
let fr_be = deserialize_identity_tuple_be(result_be.unwrap().to_vec());
|
||||
|
||||
assert_eq!(fr_le, fr_be);
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn test_wasm_hash() {
|
||||
// Create test input data
|
||||
let signal: [u8; 32] = [0; 32];
|
||||
let input = js_sys::Uint8Array::from(&signal[..]);
|
||||
|
||||
let result_le = wasm_hash(input.clone(), true);
|
||||
assert!(result_le.is_ok());
|
||||
|
||||
let serialized_hash = result_le.unwrap().to_vec();
|
||||
let (hash1, _) = bytes_le_to_fr(&serialized_hash);
|
||||
|
||||
let hash2 = hash_to_field_le(&signal);
|
||||
|
||||
assert_eq!(hash1, hash2);
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn test_wasm_poseidon_hash() {
|
||||
let mut rng = thread_rng();
|
||||
let number_of_inputs = rng.gen_range(1..ROUND_PARAMS.len());
|
||||
let mut inputs = Vec::with_capacity(number_of_inputs);
|
||||
for _ in 0..number_of_inputs {
|
||||
inputs.push(Fr::rand(&mut rng));
|
||||
}
|
||||
let inputs_ser = vec_fr_to_bytes_le(&inputs);
|
||||
let input = js_sys::Uint8Array::from(&inputs_ser[..]);
|
||||
|
||||
let expected_hash = poseidon_hash(inputs.as_ref());
|
||||
|
||||
let result_le = wasm_poseidon_hash(input.clone(), true);
|
||||
assert!(result_le.is_ok());
|
||||
|
||||
let serialized_hash = result_le.unwrap().to_vec();
|
||||
let (received_hash, _) = bytes_le_to_fr(&serialized_hash);
|
||||
|
||||
assert_eq!(received_hash, expected_hash);
|
||||
}
|
||||
}
|
||||
21
rln-wasm/.gitignore
vendored
21
rln-wasm/.gitignore
vendored
@@ -1,6 +1,21 @@
|
||||
# Common files to ignore in Rust projects
|
||||
.DS_Store
|
||||
.idea
|
||||
*.log
|
||||
tmp/
|
||||
|
||||
# Generated by Cargo will have compiled files and executables
|
||||
/target
|
||||
**/*.rs.bk
|
||||
Cargo.lock
|
||||
bin/
|
||||
|
||||
# Generated by rln-wasm
|
||||
pkg/
|
||||
wasm-pack.log
|
||||
|
||||
# Generated by Nix
|
||||
result
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
||||
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||
*.pdb
|
||||
|
||||
@@ -1,39 +1,47 @@
|
||||
[package]
|
||||
name = "rln-wasm"
|
||||
version = "0.0.13"
|
||||
version = "0.2.0"
|
||||
edition = "2021"
|
||||
license = "MIT or Apache2"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib", "rlib"]
|
||||
|
||||
[features]
|
||||
default = ["console_error_panic_hook"]
|
||||
|
||||
[dependencies]
|
||||
rln = { path = "../rln", default-features = false, features = [
|
||||
"wasm",
|
||||
rln = { path = "../rln", version = "0.8.0", default-features = false, features = [
|
||||
"stateless",
|
||||
] }
|
||||
num-bigint = { version = "0.4", default-features = false, features = [
|
||||
"rand",
|
||||
"serde",
|
||||
] }
|
||||
wasmer = { version = "2.3", default-features = false, features = ["js", "std"] }
|
||||
web-sys = { version = "0.3", features = ["console"] }
|
||||
getrandom = { version = "0.2.7", default-features = false, features = ["js"] }
|
||||
wasm-bindgen = "0.2.63"
|
||||
serde-wasm-bindgen = "0.4"
|
||||
js-sys = "0.3.59"
|
||||
serde_json = "1.0.85"
|
||||
rln-wasm-utils = { path = "../rln-wasm-utils", version = "0.1.0", default-features = false }
|
||||
zerokit_utils = { path = "../utils", version = "0.6.0", default-features = false }
|
||||
num-bigint = { version = "0.4.6", default-features = false }
|
||||
js-sys = "0.3.77"
|
||||
wasm-bindgen = "0.2.100"
|
||||
serde-wasm-bindgen = "0.6.5"
|
||||
wasm-bindgen-rayon = { version = "1.3.0", features = [
|
||||
"no-bundler",
|
||||
], optional = true }
|
||||
|
||||
# The `console_error_panic_hook` crate provides better debugging of panics by
|
||||
# The `console_error_panic_xhook` crate provides better debugging of panics by
|
||||
# logging them with `console.error`. This is great for development, but requires
|
||||
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
|
||||
# code size when deploying.
|
||||
console_error_panic_hook = { version = "0.1.7", optional = true }
|
||||
zerokit_utils = { version = "0.5.1", path = "../utils" }
|
||||
|
||||
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
||||
getrandom = { version = "0.2.16", features = ["js"] }
|
||||
|
||||
[dev-dependencies]
|
||||
wasm-bindgen-test = "0.3.13"
|
||||
wasm-bindgen-futures = "0.4.33"
|
||||
serde_json = "1.0.141"
|
||||
wasm-bindgen-test = "0.3.50"
|
||||
wasm-bindgen-futures = "0.4.50"
|
||||
|
||||
[dev-dependencies.web-sys]
|
||||
version = "0.3.77"
|
||||
features = ["Window", "Navigator"]
|
||||
|
||||
[features]
|
||||
default = ["console_error_panic_hook"]
|
||||
parallel = ["rln/parallel", "wasm-bindgen-rayon"]
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
all-features = true
|
||||
|
||||
@@ -1,31 +1,99 @@
|
||||
[tasks.pack-build]
|
||||
[tasks.build]
|
||||
clear = true
|
||||
dependencies = ["pack_build", "pack_rename", "pack_resize"]
|
||||
|
||||
[tasks.build_parallel]
|
||||
clear = true
|
||||
dependencies = ["pack_build_parallel", "pack_rename", "pack_resize"]
|
||||
|
||||
[tasks.pack_build]
|
||||
command = "wasm-pack"
|
||||
args = ["build", "--release", "--target", "web", "--scope", "waku"]
|
||||
|
||||
[tasks.pack-rename]
|
||||
[tasks.pack_build_parallel]
|
||||
command = "env"
|
||||
args = [
|
||||
"RUSTFLAGS=-C target-feature=+atomics,+bulk-memory,+mutable-globals",
|
||||
"rustup",
|
||||
"run",
|
||||
"nightly",
|
||||
"wasm-pack",
|
||||
"build",
|
||||
"--release",
|
||||
"--target",
|
||||
"web",
|
||||
"--scope",
|
||||
"waku",
|
||||
"--features",
|
||||
"parallel",
|
||||
"-Z",
|
||||
"build-std=panic_abort,std",
|
||||
]
|
||||
[tasks.pack_rename]
|
||||
script = "sed -i.bak 's/rln-wasm/zerokit-rln-wasm/g' pkg/package.json && rm pkg/package.json.bak"
|
||||
|
||||
[tasks.build]
|
||||
clear = true
|
||||
dependencies = ["pack-build", "pack-rename", "post-build"]
|
||||
|
||||
[tasks.post-build]
|
||||
command = "wasm-strip"
|
||||
args = ["./pkg/rln_wasm_bg.wasm"]
|
||||
[tasks.pack_resize]
|
||||
command = "wasm-opt"
|
||||
args = [
|
||||
"pkg/rln_wasm_bg.wasm",
|
||||
"-Oz",
|
||||
"--strip-debug",
|
||||
"--strip-dwarf",
|
||||
"--remove-unused-module-elements",
|
||||
"--vacuum",
|
||||
"-o",
|
||||
"pkg/rln_wasm_bg.wasm",
|
||||
]
|
||||
|
||||
[tasks.test]
|
||||
command = "wasm-pack"
|
||||
args = ["test", "--release", "--node"]
|
||||
args = [
|
||||
"test",
|
||||
"--release",
|
||||
"--node",
|
||||
"--target",
|
||||
"wasm32-unknown-unknown",
|
||||
"--",
|
||||
"--nocapture",
|
||||
]
|
||||
dependencies = ["build"]
|
||||
|
||||
[tasks.login]
|
||||
[tasks.test_browser]
|
||||
command = "wasm-pack"
|
||||
args = ["login"]
|
||||
args = [
|
||||
"test",
|
||||
"--release",
|
||||
"--chrome",
|
||||
"--headless",
|
||||
"--target",
|
||||
"wasm32-unknown-unknown",
|
||||
"--",
|
||||
"--nocapture",
|
||||
]
|
||||
dependencies = ["build"]
|
||||
|
||||
[tasks.publish]
|
||||
command = "wasm-pack"
|
||||
args = ["publish", "--access", "public", "--target", "web"]
|
||||
[tasks.test_parallel]
|
||||
command = "env"
|
||||
args = [
|
||||
"RUSTFLAGS=-C target-feature=+atomics,+bulk-memory,+mutable-globals",
|
||||
"rustup",
|
||||
"run",
|
||||
"nightly",
|
||||
"wasm-pack",
|
||||
"test",
|
||||
"--release",
|
||||
"--chrome",
|
||||
"--headless",
|
||||
"--target",
|
||||
"wasm32-unknown-unknown",
|
||||
"--features",
|
||||
"parallel",
|
||||
"-Z",
|
||||
"build-std=panic_abort,std",
|
||||
"--",
|
||||
"--nocapture",
|
||||
]
|
||||
dependencies = ["build_parallel"]
|
||||
|
||||
[tasks.bench]
|
||||
command = "echo"
|
||||
args = ["'No benchmarks available for this project'"]
|
||||
disabled = true
|
||||
|
||||
@@ -1,41 +1,146 @@
|
||||
# RLN for WASM
|
||||
This library is used in [waku-org/js-rln](https://github.com/waku-org/js-rln/)
|
||||
|
||||
## Building the library
|
||||
1. Install `wasm-pack`
|
||||
```
|
||||
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
|
||||
```
|
||||
2. Install `cargo-make`
|
||||
[](https://badge.fury.io/js/@waku%2Fzerokit-rln-wasm)
|
||||
[](https://opensource.org/licenses/MIT)
|
||||
[](https://opensource.org/licenses/Apache-2.0)
|
||||
|
||||
The Zerokit RLN WASM Module provides WebAssembly bindings for working with
|
||||
Rate-Limiting Nullifier [RLN](https://rfc.vac.dev/spec/32/) zkSNARK proofs and primitives.
|
||||
This module is used by [waku-org/js-rln](https://github.com/waku-org/js-rln/) to enable
|
||||
RLN functionality in JavaScript/TypeScript applications.
|
||||
|
||||
## Install Dependencies
|
||||
|
||||
> [!NOTE]
|
||||
> This project requires the following tools:
|
||||
>
|
||||
> - `wasm-pack` - for compiling Rust to WebAssembly
|
||||
> - `cargo-make` - for running build commands
|
||||
> - `nvm` - to install and manage Node.js
|
||||
>
|
||||
> Ensure all dependencies are installed before proceeding.
|
||||
|
||||
### Manually
|
||||
|
||||
#### Install `wasm-pack`
|
||||
|
||||
```bash
|
||||
cargo install wasm-pack --version=0.13.1
|
||||
```
|
||||
|
||||
#### Install `cargo-make`
|
||||
|
||||
```bash
|
||||
cargo install cargo-make
|
||||
```
|
||||
|
||||
OR
|
||||
#### Install `Node.js`
|
||||
|
||||
If you don't have `nvm` (Node Version Manager), install it by following
|
||||
the [installation instructions](https://github.com/nvm-sh/nvm?tab=readme-ov-file#install--update-script).
|
||||
|
||||
After installing `nvm`, install and use Node.js `v22.14.0`:
|
||||
|
||||
```bash
|
||||
nvm install 22.14.0
|
||||
nvm use 22.14.0
|
||||
nvm alias default 22.14.0
|
||||
```
|
||||
|
||||
If you already have Node.js installed,
|
||||
check your version with `node -v` command — the version must be strictly greater than 22.
|
||||
|
||||
### Or install everything
|
||||
|
||||
You can run the following command from the root of the repository to install all required dependencies for `zerokit`
|
||||
|
||||
```bash
|
||||
make installdeps
|
||||
```
|
||||
3. Compile zerokit for `wasm32-unknown-unknown`:
|
||||
```
|
||||
|
||||
## Building the library
|
||||
|
||||
First, navigate to the rln-wasm directory:
|
||||
|
||||
```bash
|
||||
cd rln-wasm
|
||||
cargo make build
|
||||
```
|
||||
4. Compile a slimmer version of zerokit for `wasm32-unknown-unknown`:
|
||||
```
|
||||
cd rln-wasm
|
||||
cargo make post-build
|
||||
```
|
||||
|
||||
## Running tests
|
||||
Compile zerokit for `wasm32-unknown-unknown`:
|
||||
|
||||
```bash
|
||||
cargo make build
|
||||
```
|
||||
cd rln-wasm
|
||||
|
||||
## Running tests and benchmarks
|
||||
|
||||
```bash
|
||||
cargo make test
|
||||
```
|
||||
|
||||
## Publishing a npm package
|
||||
If you want to run the tests in browser headless mode, you can use the following command:
|
||||
|
||||
```bash
|
||||
cargo make test_browser
|
||||
```
|
||||
|
||||
## Parallel computation
|
||||
|
||||
The library supports parallel computation using the `wasm-bindgen-rayon` crate,
|
||||
enabling multi-threaded execution in the browser.
|
||||
|
||||
> [!NOTE]
|
||||
> Parallel support is not enabled by default due to WebAssembly and browser limitations. \
|
||||
> Compiling this feature requires `nightly` Rust.
|
||||
|
||||
### Build Setup
|
||||
|
||||
#### Install `nightly` Rust
|
||||
|
||||
```bash
|
||||
rustup install nightly
|
||||
```
|
||||
|
||||
### Build Commands
|
||||
|
||||
To enable parallel computation for WebAssembly threads, you can use the following command:
|
||||
|
||||
```bash
|
||||
cargo make build_parallel
|
||||
```
|
||||
|
||||
### WebAssembly Threading Support
|
||||
|
||||
Most modern browsers support WebAssembly threads,
|
||||
but they require the following headers to enable `SharedArrayBuffer`, which is necessary for multithreading:
|
||||
|
||||
- Cross-Origin-Opener-Policy: same-origin
|
||||
- Cross-Origin-Embedder-Policy: require-corp
|
||||
|
||||
Without these, the application will fall back to single-threaded mode.
|
||||
|
||||
## Feature detection
|
||||
|
||||
If you're targeting [older browser versions that didn't support WebAssembly threads yet](https://webassembly.org/roadmap/),
|
||||
you'll likely want to create two builds - one with thread support and one without -
|
||||
and use feature detection to choose the right one on the JavaScript side.
|
||||
|
||||
You can use [wasm-feature-detect](https://github.com/GoogleChromeLabs/wasm-feature-detect) library for this purpose.
|
||||
For example, your code might look like this:
|
||||
|
||||
```js
|
||||
import { threads } from 'wasm-feature-detect';
|
||||
|
||||
let wasmPkg;
|
||||
|
||||
if (await threads()) {
|
||||
wasmPkg = await import('./pkg-with-threads/index.js');
|
||||
await wasmPkg.default();
|
||||
await wasmPkg.initThreadPool(navigator.hardwareConcurrency);
|
||||
} else {
|
||||
wasmPkg = await import('./pkg-without-threads/index.js');
|
||||
await wasmPkg.default();
|
||||
}
|
||||
|
||||
wasmPkg.nowCallAnyExportedFuncs();
|
||||
```
|
||||
cd rln-wasm
|
||||
cargo make login
|
||||
cargo make publish
|
||||
```
|
||||
@@ -1,331 +1,328 @@
|
||||
module.exports = async function builder(code, options) {
|
||||
// File generated with https://github.com/iden3/circom
|
||||
// following the instructions from:
|
||||
// https://github.com/vacp2p/zerokit/tree/master/rln#advanced-custom-circuit-compilation
|
||||
|
||||
options = options || {};
|
||||
export async function builder(code, options) {
|
||||
options = options || {};
|
||||
|
||||
let wasmModule;
|
||||
try {
|
||||
wasmModule = await WebAssembly.compile(code);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
console.log("\nTry to run circom --c in order to generate c++ code instead\n");
|
||||
throw new Error(err);
|
||||
}
|
||||
let wasmModule;
|
||||
try {
|
||||
wasmModule = await WebAssembly.compile(code);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
console.log(
|
||||
"\nTry to run circom --c in order to generate c++ code instead\n"
|
||||
);
|
||||
throw new Error(err);
|
||||
}
|
||||
|
||||
let wc;
|
||||
let wc;
|
||||
|
||||
let errStr = "";
|
||||
let msgStr = "";
|
||||
|
||||
const instance = await WebAssembly.instantiate(wasmModule, {
|
||||
runtime: {
|
||||
exceptionHandler : function(code) {
|
||||
let err;
|
||||
if (code == 1) {
|
||||
err = "Signal not found.\n";
|
||||
} else if (code == 2) {
|
||||
err = "Too many signals set.\n";
|
||||
} else if (code == 3) {
|
||||
err = "Signal already set.\n";
|
||||
} else if (code == 4) {
|
||||
err = "Assert Failed.\n";
|
||||
} else if (code == 5) {
|
||||
err = "Not enough memory.\n";
|
||||
} else if (code == 6) {
|
||||
err = "Input signal array access exceeds the size.\n";
|
||||
} else {
|
||||
err = "Unknown error.\n";
|
||||
}
|
||||
throw new Error(err + errStr);
|
||||
},
|
||||
printErrorMessage : function() {
|
||||
errStr += getMessage() + "\n";
|
||||
// console.error(getMessage());
|
||||
},
|
||||
writeBufferMessage : function() {
|
||||
const msg = getMessage();
|
||||
// Any calls to `log()` will always end with a `\n`, so that's when we print and reset
|
||||
if (msg === "\n") {
|
||||
console.log(msgStr);
|
||||
msgStr = "";
|
||||
} else {
|
||||
// If we've buffered other content, put a space in between the items
|
||||
if (msgStr !== "") {
|
||||
msgStr += " "
|
||||
}
|
||||
// Then append the message to the message we are creating
|
||||
msgStr += msg;
|
||||
}
|
||||
},
|
||||
showSharedRWMemory : function() {
|
||||
printSharedRWMemory ();
|
||||
}
|
||||
let errStr = "";
|
||||
let msgStr = "";
|
||||
|
||||
const instance = await WebAssembly.instantiate(wasmModule, {
|
||||
runtime: {
|
||||
exceptionHandler: function (code) {
|
||||
let err;
|
||||
if (code == 1) {
|
||||
err = "Signal not found.\n";
|
||||
} else if (code == 2) {
|
||||
err = "Too many signals set.\n";
|
||||
} else if (code == 3) {
|
||||
err = "Signal already set.\n";
|
||||
} else if (code == 4) {
|
||||
err = "Assert Failed.\n";
|
||||
} else if (code == 5) {
|
||||
err = "Not enough memory.\n";
|
||||
} else if (code == 6) {
|
||||
err = "Input signal array access exceeds the size.\n";
|
||||
} else {
|
||||
err = "Unknown error.\n";
|
||||
}
|
||||
});
|
||||
throw new Error(err + errStr);
|
||||
},
|
||||
printErrorMessage: function () {
|
||||
errStr += getMessage() + "\n";
|
||||
// console.error(getMessage());
|
||||
},
|
||||
writeBufferMessage: function () {
|
||||
const msg = getMessage();
|
||||
// Any calls to `log()` will always end with a `\n`, so that's when we print and reset
|
||||
if (msg === "\n") {
|
||||
console.log(msgStr);
|
||||
msgStr = "";
|
||||
} else {
|
||||
// If we've buffered other content, put a space in between the items
|
||||
if (msgStr !== "") {
|
||||
msgStr += " ";
|
||||
}
|
||||
// Then append the message to the message we are creating
|
||||
msgStr += msg;
|
||||
}
|
||||
},
|
||||
showSharedRWMemory: function () {
|
||||
printSharedRWMemory();
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const sanityCheck =
|
||||
options
|
||||
// options &&
|
||||
// (
|
||||
// options.sanityCheck ||
|
||||
// options.logGetSignal ||
|
||||
// options.logSetSignal ||
|
||||
// options.logStartComponent ||
|
||||
// options.logFinishComponent
|
||||
// );
|
||||
const sanityCheck = options;
|
||||
// options &&
|
||||
// (
|
||||
// options.sanityCheck ||
|
||||
// options.logGetSignal ||
|
||||
// options.logSetSignal ||
|
||||
// options.logStartComponent ||
|
||||
// options.logFinishComponent
|
||||
// );
|
||||
|
||||
|
||||
wc = new WitnessCalculator(instance, sanityCheck);
|
||||
return wc;
|
||||
wc = new WitnessCalculator(instance, sanityCheck);
|
||||
return wc;
|
||||
|
||||
function getMessage() {
|
||||
var message = "";
|
||||
var c = instance.exports.getMessageChar();
|
||||
while ( c != 0 ) {
|
||||
message += String.fromCharCode(c);
|
||||
c = instance.exports.getMessageChar();
|
||||
}
|
||||
return message;
|
||||
function getMessage() {
|
||||
var message = "";
|
||||
var c = instance.exports.getMessageChar();
|
||||
while (c != 0) {
|
||||
message += String.fromCharCode(c);
|
||||
c = instance.exports.getMessageChar();
|
||||
}
|
||||
|
||||
function printSharedRWMemory () {
|
||||
const shared_rw_memory_size = instance.exports.getFieldNumLen32();
|
||||
const arr = new Uint32Array(shared_rw_memory_size);
|
||||
for (let j=0; j<shared_rw_memory_size; j++) {
|
||||
arr[shared_rw_memory_size-1-j] = instance.exports.readSharedRWMemory(j);
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
// If we've buffered other content, put a space in between the items
|
||||
if (msgStr !== "") {
|
||||
msgStr += " "
|
||||
}
|
||||
// Then append the value to the message we are creating
|
||||
msgStr += (fromArray32(arr).toString());
|
||||
}
|
||||
function printSharedRWMemory() {
|
||||
const shared_rw_memory_size = instance.exports.getFieldNumLen32();
|
||||
const arr = new Uint32Array(shared_rw_memory_size);
|
||||
for (let j = 0; j < shared_rw_memory_size; j++) {
|
||||
arr[shared_rw_memory_size - 1 - j] =
|
||||
instance.exports.readSharedRWMemory(j);
|
||||
}
|
||||
|
||||
};
|
||||
// If we've buffered other content, put a space in between the items
|
||||
if (msgStr !== "") {
|
||||
msgStr += " ";
|
||||
}
|
||||
// Then append the value to the message we are creating
|
||||
msgStr += fromArray32(arr).toString();
|
||||
}
|
||||
}
|
||||
|
||||
class WitnessCalculator {
|
||||
constructor(instance, sanityCheck) {
|
||||
this.instance = instance;
|
||||
constructor(instance, sanityCheck) {
|
||||
this.instance = instance;
|
||||
|
||||
this.version = this.instance.exports.getVersion();
|
||||
this.n32 = this.instance.exports.getFieldNumLen32();
|
||||
this.version = this.instance.exports.getVersion();
|
||||
this.n32 = this.instance.exports.getFieldNumLen32();
|
||||
|
||||
this.instance.exports.getRawPrime();
|
||||
const arr = new Uint32Array(this.n32);
|
||||
for (let i=0; i<this.n32; i++) {
|
||||
arr[this.n32-1-i] = this.instance.exports.readSharedRWMemory(i);
|
||||
this.instance.exports.getRawPrime();
|
||||
const arr = new Uint32Array(this.n32);
|
||||
for (let i = 0; i < this.n32; i++) {
|
||||
arr[this.n32 - 1 - i] = this.instance.exports.readSharedRWMemory(i);
|
||||
}
|
||||
this.prime = fromArray32(arr);
|
||||
|
||||
this.witnessSize = this.instance.exports.getWitnessSize();
|
||||
|
||||
this.sanityCheck = sanityCheck;
|
||||
}
|
||||
|
||||
circom_version() {
|
||||
return this.instance.exports.getVersion();
|
||||
}
|
||||
|
||||
async _doCalculateWitness(input, sanityCheck) {
|
||||
//input is assumed to be a map from signals to arrays of bigints
|
||||
this.instance.exports.init(this.sanityCheck || sanityCheck ? 1 : 0);
|
||||
const keys = Object.keys(input);
|
||||
var input_counter = 0;
|
||||
keys.forEach((k) => {
|
||||
const h = fnvHash(k);
|
||||
const hMSB = parseInt(h.slice(0, 8), 16);
|
||||
const hLSB = parseInt(h.slice(8, 16), 16);
|
||||
const fArr = flatArray(input[k]);
|
||||
let signalSize = this.instance.exports.getInputSignalSize(hMSB, hLSB);
|
||||
if (signalSize < 0) {
|
||||
throw new Error(`Signal ${k} not found\n`);
|
||||
}
|
||||
if (fArr.length < signalSize) {
|
||||
throw new Error(`Not enough values for input signal ${k}\n`);
|
||||
}
|
||||
if (fArr.length > signalSize) {
|
||||
throw new Error(`Too many values for input signal ${k}\n`);
|
||||
}
|
||||
for (let i = 0; i < fArr.length; i++) {
|
||||
const arrFr = toArray32(BigInt(fArr[i]) % this.prime, this.n32);
|
||||
for (let j = 0; j < this.n32; j++) {
|
||||
this.instance.exports.writeSharedRWMemory(j, arrFr[this.n32 - 1 - j]);
|
||||
}
|
||||
this.prime = fromArray32(arr);
|
||||
|
||||
this.witnessSize = this.instance.exports.getWitnessSize();
|
||||
|
||||
this.sanityCheck = sanityCheck;
|
||||
}
|
||||
|
||||
circom_version() {
|
||||
return this.instance.exports.getVersion();
|
||||
}
|
||||
|
||||
async _doCalculateWitness(input, sanityCheck) {
|
||||
//input is assumed to be a map from signals to arrays of bigints
|
||||
this.instance.exports.init((this.sanityCheck || sanityCheck) ? 1 : 0);
|
||||
const keys = Object.keys(input);
|
||||
var input_counter = 0;
|
||||
keys.forEach( (k) => {
|
||||
const h = fnvHash(k);
|
||||
const hMSB = parseInt(h.slice(0,8), 16);
|
||||
const hLSB = parseInt(h.slice(8,16), 16);
|
||||
const fArr = flatArray(input[k]);
|
||||
let signalSize = this.instance.exports.getInputSignalSize(hMSB, hLSB);
|
||||
if (signalSize < 0){
|
||||
throw new Error(`Signal ${k} not found\n`);
|
||||
}
|
||||
if (fArr.length < signalSize) {
|
||||
throw new Error(`Not enough values for input signal ${k}\n`);
|
||||
}
|
||||
if (fArr.length > signalSize) {
|
||||
throw new Error(`Too many values for input signal ${k}\n`);
|
||||
}
|
||||
for (let i=0; i<fArr.length; i++) {
|
||||
const arrFr = toArray32(BigInt(fArr[i])%this.prime,this.n32)
|
||||
for (let j=0; j<this.n32; j++) {
|
||||
this.instance.exports.writeSharedRWMemory(j,arrFr[this.n32-1-j]);
|
||||
}
|
||||
try {
|
||||
this.instance.exports.setInputSignal(hMSB, hLSB,i);
|
||||
input_counter++;
|
||||
} catch (err) {
|
||||
// console.log(`After adding signal ${i} of ${k}`)
|
||||
throw new Error(err);
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
if (input_counter < this.instance.exports.getInputSize()) {
|
||||
throw new Error(`Not all inputs have been set. Only ${input_counter} out of ${this.instance.exports.getInputSize()}`);
|
||||
}
|
||||
}
|
||||
|
||||
async calculateWitness(input, sanityCheck) {
|
||||
|
||||
const w = [];
|
||||
|
||||
await this._doCalculateWitness(input, sanityCheck);
|
||||
|
||||
for (let i=0; i<this.witnessSize; i++) {
|
||||
this.instance.exports.getWitness(i);
|
||||
const arr = new Uint32Array(this.n32);
|
||||
for (let j=0; j<this.n32; j++) {
|
||||
arr[this.n32-1-j] = this.instance.exports.readSharedRWMemory(j);
|
||||
}
|
||||
w.push(fromArray32(arr));
|
||||
try {
|
||||
this.instance.exports.setInputSignal(hMSB, hLSB, i);
|
||||
input_counter++;
|
||||
} catch (err) {
|
||||
// console.log(`After adding signal ${i} of ${k}`)
|
||||
throw new Error(err);
|
||||
}
|
||||
|
||||
return w;
|
||||
}
|
||||
});
|
||||
if (input_counter < this.instance.exports.getInputSize()) {
|
||||
throw new Error(
|
||||
`Not all inputs have been set. Only ${input_counter} out of ${this.instance.exports.getInputSize()}`
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
async calculateBinWitness(input, sanityCheck) {
|
||||
async calculateWitness(input, sanityCheck) {
|
||||
const w = [];
|
||||
|
||||
const buff32 = new Uint32Array(this.witnessSize*this.n32);
|
||||
const buff = new Uint8Array( buff32.buffer);
|
||||
await this._doCalculateWitness(input, sanityCheck);
|
||||
await this._doCalculateWitness(input, sanityCheck);
|
||||
|
||||
for (let i=0; i<this.witnessSize; i++) {
|
||||
this.instance.exports.getWitness(i);
|
||||
const pos = i*this.n32;
|
||||
for (let j=0; j<this.n32; j++) {
|
||||
buff32[pos+j] = this.instance.exports.readSharedRWMemory(j);
|
||||
}
|
||||
}
|
||||
|
||||
return buff;
|
||||
}
|
||||
|
||||
|
||||
async calculateWTNSBin(input, sanityCheck) {
|
||||
|
||||
const buff32 = new Uint32Array(this.witnessSize*this.n32+this.n32+11);
|
||||
const buff = new Uint8Array( buff32.buffer);
|
||||
await this._doCalculateWitness(input, sanityCheck);
|
||||
|
||||
//"wtns"
|
||||
buff[0] = "w".charCodeAt(0)
|
||||
buff[1] = "t".charCodeAt(0)
|
||||
buff[2] = "n".charCodeAt(0)
|
||||
buff[3] = "s".charCodeAt(0)
|
||||
|
||||
//version 2
|
||||
buff32[1] = 2;
|
||||
|
||||
//number of sections: 2
|
||||
buff32[2] = 2;
|
||||
|
||||
//id section 1
|
||||
buff32[3] = 1;
|
||||
|
||||
const n8 = this.n32*4;
|
||||
//id section 1 length in 64bytes
|
||||
const idSection1length = 8 + n8;
|
||||
const idSection1lengthHex = idSection1length.toString(16);
|
||||
buff32[4] = parseInt(idSection1lengthHex.slice(0,8), 16);
|
||||
buff32[5] = parseInt(idSection1lengthHex.slice(8,16), 16);
|
||||
|
||||
//this.n32
|
||||
buff32[6] = n8;
|
||||
|
||||
//prime number
|
||||
this.instance.exports.getRawPrime();
|
||||
|
||||
var pos = 7;
|
||||
for (let j=0; j<this.n32; j++) {
|
||||
buff32[pos+j] = this.instance.exports.readSharedRWMemory(j);
|
||||
}
|
||||
pos += this.n32;
|
||||
|
||||
// witness size
|
||||
buff32[pos] = this.witnessSize;
|
||||
pos++;
|
||||
|
||||
//id section 2
|
||||
buff32[pos] = 2;
|
||||
pos++;
|
||||
|
||||
// section 2 length
|
||||
const idSection2length = n8*this.witnessSize;
|
||||
const idSection2lengthHex = idSection2length.toString(16);
|
||||
buff32[pos] = parseInt(idSection2lengthHex.slice(0,8), 16);
|
||||
buff32[pos+1] = parseInt(idSection2lengthHex.slice(8,16), 16);
|
||||
|
||||
pos += 2;
|
||||
for (let i=0; i<this.witnessSize; i++) {
|
||||
this.instance.exports.getWitness(i);
|
||||
for (let j=0; j<this.n32; j++) {
|
||||
buff32[pos+j] = this.instance.exports.readSharedRWMemory(j);
|
||||
}
|
||||
pos += this.n32;
|
||||
}
|
||||
|
||||
return buff;
|
||||
for (let i = 0; i < this.witnessSize; i++) {
|
||||
this.instance.exports.getWitness(i);
|
||||
const arr = new Uint32Array(this.n32);
|
||||
for (let j = 0; j < this.n32; j++) {
|
||||
arr[this.n32 - 1 - j] = this.instance.exports.readSharedRWMemory(j);
|
||||
}
|
||||
w.push(fromArray32(arr));
|
||||
}
|
||||
|
||||
return w;
|
||||
}
|
||||
|
||||
async calculateBinWitness(input, sanityCheck) {
|
||||
const buff32 = new Uint32Array(this.witnessSize * this.n32);
|
||||
const buff = new Uint8Array(buff32.buffer);
|
||||
await this._doCalculateWitness(input, sanityCheck);
|
||||
|
||||
for (let i = 0; i < this.witnessSize; i++) {
|
||||
this.instance.exports.getWitness(i);
|
||||
const pos = i * this.n32;
|
||||
for (let j = 0; j < this.n32; j++) {
|
||||
buff32[pos + j] = this.instance.exports.readSharedRWMemory(j);
|
||||
}
|
||||
}
|
||||
|
||||
return buff;
|
||||
}
|
||||
|
||||
async calculateWTNSBin(input, sanityCheck) {
|
||||
const buff32 = new Uint32Array(this.witnessSize * this.n32 + this.n32 + 11);
|
||||
const buff = new Uint8Array(buff32.buffer);
|
||||
await this._doCalculateWitness(input, sanityCheck);
|
||||
|
||||
//"wtns"
|
||||
buff[0] = "w".charCodeAt(0);
|
||||
buff[1] = "t".charCodeAt(0);
|
||||
buff[2] = "n".charCodeAt(0);
|
||||
buff[3] = "s".charCodeAt(0);
|
||||
|
||||
//version 2
|
||||
buff32[1] = 2;
|
||||
|
||||
//number of sections: 2
|
||||
buff32[2] = 2;
|
||||
|
||||
//id section 1
|
||||
buff32[3] = 1;
|
||||
|
||||
const n8 = this.n32 * 4;
|
||||
//id section 1 length in 64bytes
|
||||
const idSection1length = 8 + n8;
|
||||
const idSection1lengthHex = idSection1length.toString(16);
|
||||
buff32[4] = parseInt(idSection1lengthHex.slice(0, 8), 16);
|
||||
buff32[5] = parseInt(idSection1lengthHex.slice(8, 16), 16);
|
||||
|
||||
//this.n32
|
||||
buff32[6] = n8;
|
||||
|
||||
//prime number
|
||||
this.instance.exports.getRawPrime();
|
||||
|
||||
var pos = 7;
|
||||
for (let j = 0; j < this.n32; j++) {
|
||||
buff32[pos + j] = this.instance.exports.readSharedRWMemory(j);
|
||||
}
|
||||
pos += this.n32;
|
||||
|
||||
// witness size
|
||||
buff32[pos] = this.witnessSize;
|
||||
pos++;
|
||||
|
||||
//id section 2
|
||||
buff32[pos] = 2;
|
||||
pos++;
|
||||
|
||||
// section 2 length
|
||||
const idSection2length = n8 * this.witnessSize;
|
||||
const idSection2lengthHex = idSection2length.toString(16);
|
||||
buff32[pos] = parseInt(idSection2lengthHex.slice(0, 8), 16);
|
||||
buff32[pos + 1] = parseInt(idSection2lengthHex.slice(8, 16), 16);
|
||||
|
||||
pos += 2;
|
||||
for (let i = 0; i < this.witnessSize; i++) {
|
||||
this.instance.exports.getWitness(i);
|
||||
for (let j = 0; j < this.n32; j++) {
|
||||
buff32[pos + j] = this.instance.exports.readSharedRWMemory(j);
|
||||
}
|
||||
pos += this.n32;
|
||||
}
|
||||
|
||||
return buff;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function toArray32(rem,size) {
|
||||
const res = []; //new Uint32Array(size); //has no unshift
|
||||
const radix = BigInt(0x100000000);
|
||||
while (rem) {
|
||||
res.unshift( Number(rem % radix));
|
||||
rem = rem / radix;
|
||||
function toArray32(rem, size) {
|
||||
const res = []; //new Uint32Array(size); //has no unshift
|
||||
const radix = BigInt(0x100000000);
|
||||
while (rem) {
|
||||
res.unshift(Number(rem % radix));
|
||||
rem = rem / radix;
|
||||
}
|
||||
if (size) {
|
||||
var i = size - res.length;
|
||||
while (i > 0) {
|
||||
res.unshift(0);
|
||||
i--;
|
||||
}
|
||||
if (size) {
|
||||
var i = size - res.length;
|
||||
while (i>0) {
|
||||
res.unshift(0);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
function fromArray32(arr) { //returns a BigInt
|
||||
var res = BigInt(0);
|
||||
const radix = BigInt(0x100000000);
|
||||
for (let i = 0; i<arr.length; i++) {
|
||||
res = res*radix + BigInt(arr[i]);
|
||||
}
|
||||
return res;
|
||||
function fromArray32(arr) {
|
||||
//returns a BigInt
|
||||
var res = BigInt(0);
|
||||
const radix = BigInt(0x100000000);
|
||||
for (let i = 0; i < arr.length; i++) {
|
||||
res = res * radix + BigInt(arr[i]);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
function flatArray(a) {
|
||||
var res = [];
|
||||
fillArray(res, a);
|
||||
return res;
|
||||
var res = [];
|
||||
fillArray(res, a);
|
||||
return res;
|
||||
|
||||
function fillArray(res, a) {
|
||||
if (Array.isArray(a)) {
|
||||
for (let i=0; i<a.length; i++) {
|
||||
fillArray(res, a[i]);
|
||||
}
|
||||
} else {
|
||||
res.push(a);
|
||||
}
|
||||
function fillArray(res, a) {
|
||||
if (Array.isArray(a)) {
|
||||
for (let i = 0; i < a.length; i++) {
|
||||
fillArray(res, a[i]);
|
||||
}
|
||||
} else {
|
||||
res.push(a);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function fnvHash(str) {
|
||||
const uint64_max = BigInt(2) ** BigInt(64);
|
||||
let hash = BigInt("0xCBF29CE484222325");
|
||||
for (var i = 0; i < str.length; i++) {
|
||||
hash ^= BigInt(str[i].charCodeAt());
|
||||
hash *= BigInt(0x100000001B3);
|
||||
hash %= uint64_max;
|
||||
}
|
||||
let shash = hash.toString(16);
|
||||
let n = 16 - shash.length;
|
||||
shash = '0'.repeat(n).concat(shash);
|
||||
return shash;
|
||||
const uint64_max = BigInt(2) ** BigInt(64);
|
||||
let hash = BigInt("0xCBF29CE484222325");
|
||||
for (var i = 0; i < str.length; i++) {
|
||||
hash ^= BigInt(str[i].charCodeAt());
|
||||
hash *= BigInt(0x100000001b3);
|
||||
hash %= uint64_max;
|
||||
}
|
||||
let shash = hash.toString(16);
|
||||
let n = 16 - shash.length;
|
||||
shash = "0".repeat(n).concat(shash);
|
||||
return shash;
|
||||
}
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
#![cfg(target_arch = "wasm32")]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
extern crate web_sys;
|
||||
|
||||
use std::vec::Vec;
|
||||
|
||||
use js_sys::{BigInt as JsBigInt, Object, Uint8Array};
|
||||
use num_bigint::BigInt;
|
||||
use rln::public::{hash, poseidon_hash, RLN};
|
||||
use rln::public::RLN;
|
||||
use std::vec::Vec;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[cfg(feature = "parallel")]
|
||||
pub use wasm_bindgen_rayon::init_thread_pool;
|
||||
|
||||
#[wasm_bindgen(js_name = initPanicHook)]
|
||||
pub fn init_panic_hook() {
|
||||
console_error_panic_hook::set_once();
|
||||
}
|
||||
@@ -61,24 +60,6 @@ macro_rules! call_with_output_and_error_msg {
|
||||
};
|
||||
}
|
||||
|
||||
// Macro to call_with_error_msg methods with arbitrary amount of arguments,
|
||||
// First argument to the macro is context,
|
||||
// second is the actual method on `RLNWrapper`
|
||||
// rest are all other arguments to the method
|
||||
macro_rules! call_with_error_msg {
|
||||
($instance:expr, $method:ident, $error_msg:expr $(, $arg:expr)*) => {
|
||||
{
|
||||
let new_instance: &mut RLNWrapper = $instance.process();
|
||||
if let Err(err) = new_instance.instance.$method($($arg.process()),*) {
|
||||
Err(format!("Msg: {:#?}, Error: {:#?}", $error_msg, err))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! call {
|
||||
($instance:expr, $method:ident $(, $arg:expr)*) => {
|
||||
{
|
||||
@@ -97,40 +78,6 @@ macro_rules! call_bool_method_with_error_msg {
|
||||
}
|
||||
}
|
||||
|
||||
// Macro to execute a function with arbitrary amount of arguments,
|
||||
// First argument is the function to execute
|
||||
// Rest are all other arguments to the method
|
||||
macro_rules! fn_call_with_output_and_error_msg {
|
||||
// this variant is needed for the case when
|
||||
// there are zero other arguments
|
||||
($func:ident, $error_msg:expr) => {
|
||||
{
|
||||
let mut output_data: Vec<u8> = Vec::new();
|
||||
if let Err(err) = $func(&mut output_data) {
|
||||
std::mem::forget(output_data);
|
||||
Err(format!("Msg: {:#?}, Error: {:#?}", $error_msg, err))
|
||||
} else {
|
||||
let result = Uint8Array::from(&output_data[..]);
|
||||
std::mem::forget(output_data);
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
};
|
||||
($func:ident, $error_msg:expr, $( $arg:expr ),* ) => {
|
||||
{
|
||||
let mut output_data: Vec<u8> = Vec::new();
|
||||
if let Err(err) = $func($($arg.process()),*, &mut output_data) {
|
||||
std::mem::forget(output_data);
|
||||
Err(format!("Msg: {:#?}, Error: {:#?}", $error_msg, err))
|
||||
} else {
|
||||
let result = Uint8Array::from(&output_data[..]);
|
||||
std::mem::forget(output_data);
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
trait ProcessArg {
|
||||
type ReturnType;
|
||||
fn process(self) -> Self::ReturnType;
|
||||
@@ -181,19 +128,15 @@ impl<'a> ProcessArg for &'a [u8] {
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[wasm_bindgen(js_name = newRLN)]
|
||||
pub fn wasm_new(
|
||||
zkey: Uint8Array,
|
||||
vk: Uint8Array,
|
||||
) -> Result<*mut RLNWrapper, String> {
|
||||
let instance = RLN::new_with_params(zkey.to_vec(), vk.to_vec())
|
||||
.map_err(|err| format!("{:#?}", err))?;
|
||||
pub fn wasm_new(zkey: Uint8Array) -> Result<*mut RLNWrapper, String> {
|
||||
let instance = RLN::new_with_params(zkey.to_vec()).map_err(|err| format!("{:#?}", err))?;
|
||||
let wrapper = RLNWrapper { instance };
|
||||
Ok(Box::into_raw(Box::new(wrapper)))
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[wasm_bindgen(js_name = RLNWitnessToJson)]
|
||||
pub fn rln_witness_to_json(
|
||||
#[wasm_bindgen(js_name = rlnWitnessToJson)]
|
||||
pub fn wasm_rln_witness_to_json(
|
||||
ctx: *mut RLNWrapper,
|
||||
serialized_witness: Uint8Array,
|
||||
) -> Result<Object, String> {
|
||||
@@ -208,8 +151,8 @@ pub fn rln_witness_to_json(
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[wasm_bindgen]
|
||||
pub fn generate_rln_proof_with_witness(
|
||||
#[wasm_bindgen(js_name = generateRLNProofWithWitness)]
|
||||
pub fn wasm_generate_rln_proof_with_witness(
|
||||
ctx: *mut RLNWrapper,
|
||||
calculated_witness: Vec<JsBigInt>,
|
||||
serialized_witness: Uint8Array,
|
||||
@@ -236,43 +179,6 @@ pub fn generate_rln_proof_with_witness(
|
||||
)
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[wasm_bindgen(js_name = generateMembershipKey)]
|
||||
pub fn wasm_key_gen(ctx: *const RLNWrapper) -> Result<Uint8Array, String> {
|
||||
call_with_output_and_error_msg!(ctx, key_gen, "could not generate membership keys")
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[wasm_bindgen(js_name = generateExtendedMembershipKey)]
|
||||
pub fn wasm_extended_key_gen(ctx: *const RLNWrapper) -> Result<Uint8Array, String> {
|
||||
call_with_output_and_error_msg!(ctx, extended_key_gen, "could not generate membership keys")
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[wasm_bindgen(js_name = generateSeededMembershipKey)]
|
||||
pub fn wasm_seeded_key_gen(ctx: *const RLNWrapper, seed: Uint8Array) -> Result<Uint8Array, String> {
|
||||
call_with_output_and_error_msg!(
|
||||
ctx,
|
||||
seeded_key_gen,
|
||||
"could not generate membership key",
|
||||
&seed.to_vec()[..]
|
||||
)
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[wasm_bindgen(js_name = generateSeededExtendedMembershipKey)]
|
||||
pub fn wasm_seeded_extended_key_gen(
|
||||
ctx: *const RLNWrapper,
|
||||
seed: Uint8Array,
|
||||
) -> Result<Uint8Array, String> {
|
||||
call_with_output_and_error_msg!(
|
||||
ctx,
|
||||
seeded_extended_key_gen,
|
||||
"could not generate membership key",
|
||||
&seed.to_vec()[..]
|
||||
)
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[wasm_bindgen(js_name = recovedIDSecret)]
|
||||
pub fn wasm_recover_id_secret(
|
||||
@@ -304,17 +210,3 @@ pub fn wasm_verify_with_roots(
|
||||
&roots.to_vec()[..]
|
||||
)
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = hash)]
|
||||
pub fn wasm_hash(input: Uint8Array) -> Result<Uint8Array, String> {
|
||||
fn_call_with_output_and_error_msg!(hash, "could not generate hash", &input.to_vec()[..])
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = poseidonHash)]
|
||||
pub fn wasm_poseidon_hash(input: Uint8Array) -> Result<Uint8Array, String> {
|
||||
fn_call_with_output_and_error_msg!(
|
||||
poseidon_hash,
|
||||
"could not generate poseidon hash",
|
||||
&input.to_vec()[..]
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
const fs = require("fs");
|
||||
|
||||
// Utils functions for loading circom witness calculator and reading files from test
|
||||
|
||||
module.exports = {
|
||||
read_file: function (path) {
|
||||
return fs.readFileSync(path);
|
||||
},
|
||||
|
||||
calculateWitness: async function(circom_path, inputs){
|
||||
const wc = require("resources/witness_calculator.js");
|
||||
const wasmFile = fs.readFileSync(circom_path);
|
||||
const wasmFileBuffer = wasmFile.slice(wasmFile.byteOffset, wasmFile.byteOffset + wasmFile.byteLength);
|
||||
const witnessCalculator = await wc(wasmFileBuffer);
|
||||
const calculatedWitness = await witnessCalculator.calculateWitness(inputs, false);
|
||||
return JSON.stringify(calculatedWitness, (key, value) => typeof value === "bigint" ? value.toString() : value);
|
||||
}
|
||||
}
|
||||
266
rln-wasm/tests/browser.rs
Normal file
266
rln-wasm/tests/browser.rs
Normal file
@@ -0,0 +1,266 @@
|
||||
#![cfg(target_arch = "wasm32")]
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use js_sys::{BigInt as JsBigInt, Date, Object, Uint8Array};
|
||||
use rln::circuit::{Fr, TEST_TREE_DEPTH};
|
||||
use rln::hashers::{hash_to_field_le, poseidon_hash, PoseidonHash};
|
||||
use rln::protocol::{prepare_verify_input, rln_witness_from_values, serialize_witness};
|
||||
use rln::utils::{bytes_le_to_fr, fr_to_bytes_le, IdSecret};
|
||||
use rln_wasm::{
|
||||
wasm_generate_rln_proof_with_witness, wasm_new, wasm_rln_witness_to_json,
|
||||
wasm_verify_with_roots,
|
||||
};
|
||||
use rln_wasm_utils::wasm_key_gen;
|
||||
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
|
||||
use wasm_bindgen_test::{console_log, wasm_bindgen_test, wasm_bindgen_test_configure};
|
||||
use zerokit_utils::{
|
||||
OptimalMerkleProof, OptimalMerkleTree, ZerokitMerkleProof, ZerokitMerkleTree,
|
||||
};
|
||||
|
||||
#[cfg(feature = "parallel")]
|
||||
use {rln_wasm::init_thread_pool, wasm_bindgen_futures::JsFuture, web_sys::window};
|
||||
|
||||
#[wasm_bindgen(inline_js = r#"
|
||||
export function isThreadpoolSupported() {
|
||||
return typeof SharedArrayBuffer !== 'undefined' &&
|
||||
typeof Atomics !== 'undefined' &&
|
||||
typeof crossOriginIsolated !== 'undefined' &&
|
||||
crossOriginIsolated;
|
||||
}
|
||||
|
||||
export function initWitnessCalculator(jsCode) {
|
||||
const processedCode = jsCode
|
||||
.replace(/export\s+async\s+function\s+builder/, 'async function builder')
|
||||
.replace(/export\s*\{\s*builder\s*\};?/g, '');
|
||||
|
||||
const moduleFunc = new Function(processedCode + '\nreturn { builder };');
|
||||
const witnessCalculatorModule = moduleFunc();
|
||||
|
||||
window.witnessCalculatorBuilder = witnessCalculatorModule.builder;
|
||||
|
||||
if (typeof window.witnessCalculatorBuilder !== 'function') {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
export function readFile(data) {
|
||||
return new Uint8Array(data);
|
||||
}
|
||||
|
||||
export async function calculateWitness(circom_data, inputs) {
|
||||
const wasmBuffer = circom_data instanceof Uint8Array ? circom_data : new Uint8Array(circom_data);
|
||||
const witnessCalculator = await window.witnessCalculatorBuilder(wasmBuffer);
|
||||
const calculatedWitness = await witnessCalculator.calculateWitness(inputs, false);
|
||||
return JSON.stringify(calculatedWitness, (key, value) =>
|
||||
typeof value === "bigint" ? value.toString() : value
|
||||
);
|
||||
}
|
||||
"#)]
|
||||
extern "C" {
|
||||
#[wasm_bindgen(catch)]
|
||||
fn isThreadpoolSupported() -> Result<bool, JsValue>;
|
||||
|
||||
#[wasm_bindgen(catch)]
|
||||
fn initWitnessCalculator(js: &str) -> Result<bool, JsValue>;
|
||||
|
||||
#[wasm_bindgen(catch)]
|
||||
fn readFile(data: &[u8]) -> Result<Uint8Array, JsValue>;
|
||||
|
||||
#[wasm_bindgen(catch)]
|
||||
async fn calculateWitness(circom_data: &[u8], inputs: Object) -> Result<JsValue, JsValue>;
|
||||
}
|
||||
|
||||
const WITNESS_CALCULATOR_JS: &str = include_str!("../resources/witness_calculator.js");
|
||||
|
||||
const ARKZKEY_BYTES: &[u8] =
|
||||
include_bytes!("../../rln/resources/tree_depth_30/rln_final.arkzkey");
|
||||
|
||||
const CIRCOM_BYTES: &[u8] = include_bytes!("../../rln/resources/tree_depth_30/rln.wasm");
|
||||
|
||||
wasm_bindgen_test_configure!(run_in_browser);
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
pub async fn rln_wasm_benchmark() {
|
||||
// Check if thread pool is supported
|
||||
#[cfg(feature = "parallel")]
|
||||
if !isThreadpoolSupported().expect("Failed to check thread pool support") {
|
||||
panic!("Thread pool is NOT supported");
|
||||
} else {
|
||||
// Initialize thread pool
|
||||
let cpu_count = window()
|
||||
.expect("Failed to get window")
|
||||
.navigator()
|
||||
.hardware_concurrency() as usize;
|
||||
JsFuture::from(init_thread_pool(cpu_count))
|
||||
.await
|
||||
.expect("Failed to initialize thread pool");
|
||||
}
|
||||
|
||||
// Initialize witness calculator
|
||||
initWitnessCalculator(WITNESS_CALCULATOR_JS)
|
||||
.expect("Failed to initialize witness calculator");
|
||||
|
||||
let mut results = String::from("\nbenchmarks:\n");
|
||||
let iterations = 10;
|
||||
|
||||
let zkey = readFile(&ARKZKEY_BYTES).expect("Failed to read zkey file");
|
||||
|
||||
// Benchmark wasm_new
|
||||
let start_wasm_new = Date::now();
|
||||
for _ in 0..iterations {
|
||||
let _ = wasm_new(zkey.clone()).expect("Failed to create RLN instance");
|
||||
}
|
||||
let wasm_new_result = Date::now() - start_wasm_new;
|
||||
|
||||
// Create RLN instance for other benchmarks
|
||||
let rln_instance = wasm_new(zkey).expect("Failed to create RLN instance");
|
||||
let mut tree: OptimalMerkleTree<PoseidonHash> =
|
||||
OptimalMerkleTree::default(TEST_TREE_DEPTH).expect("Failed to create tree");
|
||||
|
||||
// Benchmark wasm_key_gen
|
||||
let start_wasm_key_gen = Date::now();
|
||||
for _ in 0..iterations {
|
||||
let _ = wasm_key_gen(true).expect("Failed to generate keys");
|
||||
}
|
||||
let wasm_key_gen_result = Date::now() - start_wasm_key_gen;
|
||||
|
||||
// Generate identity pair for other benchmarks
|
||||
let mem_keys = wasm_key_gen(true).expect("Failed to generate keys");
|
||||
let id_key = mem_keys.subarray(0, 32);
|
||||
let (identity_secret_hash, _) = IdSecret::from_bytes_le(&id_key.to_vec());
|
||||
let (id_commitment, _) = bytes_le_to_fr(&mem_keys.subarray(32, 64).to_vec());
|
||||
|
||||
let epoch = hash_to_field_le(b"test-epoch");
|
||||
let rln_identifier = hash_to_field_le(b"test-rln-identifier");
|
||||
let external_nullifier = poseidon_hash(&[epoch, rln_identifier]);
|
||||
|
||||
let identity_index = tree.leaves_set();
|
||||
|
||||
let user_message_limit = Fr::from(100);
|
||||
|
||||
let rate_commitment = poseidon_hash(&[id_commitment, user_message_limit]);
|
||||
tree.update_next(rate_commitment)
|
||||
.expect("Failed to update tree");
|
||||
|
||||
let message_id = Fr::from(0);
|
||||
let signal: [u8; 32] = [0; 32];
|
||||
let x = hash_to_field_le(&signal);
|
||||
|
||||
let merkle_proof: OptimalMerkleProof<PoseidonHash> = tree
|
||||
.proof(identity_index)
|
||||
.expect("Failed to generate merkle proof");
|
||||
|
||||
let rln_witness = rln_witness_from_values(
|
||||
identity_secret_hash,
|
||||
merkle_proof.get_path_elements(),
|
||||
merkle_proof.get_path_index(),
|
||||
x,
|
||||
external_nullifier,
|
||||
user_message_limit,
|
||||
message_id,
|
||||
)
|
||||
.expect("Failed to create RLN witness");
|
||||
|
||||
let serialized_witness =
|
||||
serialize_witness(&rln_witness).expect("Failed to serialize witness");
|
||||
let witness_buffer = Uint8Array::from(&serialized_witness[..]);
|
||||
|
||||
let json_inputs = wasm_rln_witness_to_json(rln_instance, witness_buffer.clone())
|
||||
.expect("Failed to convert witness to JSON");
|
||||
|
||||
// Benchmark calculateWitness
|
||||
let start_calculate_witness = Date::now();
|
||||
for _ in 0..iterations {
|
||||
let _ = calculateWitness(&CIRCOM_BYTES, json_inputs.clone())
|
||||
.await
|
||||
.expect("Failed to calculate witness");
|
||||
}
|
||||
let calculate_witness_result = Date::now() - start_calculate_witness;
|
||||
|
||||
// Calculate witness for other benchmarks
|
||||
let calculated_witness_json = calculateWitness(&CIRCOM_BYTES, json_inputs)
|
||||
.await
|
||||
.expect("Failed to calculate witness")
|
||||
.as_string()
|
||||
.expect("Failed to convert calculated witness to string");
|
||||
let calculated_witness_vec_str: Vec<String> =
|
||||
serde_json::from_str(&calculated_witness_json).expect("Failed to parse JSON");
|
||||
let calculated_witness: Vec<JsBigInt> = calculated_witness_vec_str
|
||||
.iter()
|
||||
.map(|x| JsBigInt::new(&x.into()).expect("Failed to create JsBigInt"))
|
||||
.collect();
|
||||
|
||||
// Benchmark wasm_generate_rln_proof_with_witness
|
||||
let start_wasm_generate_rln_proof_with_witness = Date::now();
|
||||
for _ in 0..iterations {
|
||||
let _ = wasm_generate_rln_proof_with_witness(
|
||||
rln_instance,
|
||||
calculated_witness.clone(),
|
||||
witness_buffer.clone(),
|
||||
)
|
||||
.expect("Failed to generate proof");
|
||||
}
|
||||
let wasm_generate_rln_proof_with_witness_result =
|
||||
Date::now() - start_wasm_generate_rln_proof_with_witness;
|
||||
|
||||
// Generate a proof for other benchmarks
|
||||
let proof =
|
||||
wasm_generate_rln_proof_with_witness(rln_instance, calculated_witness, witness_buffer)
|
||||
.expect("Failed to generate proof");
|
||||
|
||||
let proof_data = proof.to_vec();
|
||||
let verify_input = prepare_verify_input(proof_data, &signal);
|
||||
let input_buffer = Uint8Array::from(&verify_input[..]);
|
||||
|
||||
let root = tree.root();
|
||||
let roots_serialized = fr_to_bytes_le(&root);
|
||||
let roots_buffer = Uint8Array::from(&roots_serialized[..]);
|
||||
|
||||
// Benchmark wasm_verify_with_roots
|
||||
let start_wasm_verify_with_roots = Date::now();
|
||||
for _ in 0..iterations {
|
||||
let _ =
|
||||
wasm_verify_with_roots(rln_instance, input_buffer.clone(), roots_buffer.clone())
|
||||
.expect("Failed to verify proof");
|
||||
}
|
||||
let wasm_verify_with_roots_result = Date::now() - start_wasm_verify_with_roots;
|
||||
|
||||
// Verify the proof with the root
|
||||
let is_proof_valid = wasm_verify_with_roots(rln_instance, input_buffer, roots_buffer)
|
||||
.expect("Failed to verify proof");
|
||||
assert!(is_proof_valid, "verification failed");
|
||||
|
||||
// Format and display results
|
||||
let format_duration = |duration_ms: f64| -> String {
|
||||
let avg_ms = duration_ms / (iterations as f64);
|
||||
if avg_ms >= 1000.0 {
|
||||
format!("{:.3} s", avg_ms / 1000.0)
|
||||
} else {
|
||||
format!("{:.3} ms", avg_ms)
|
||||
}
|
||||
};
|
||||
|
||||
results.push_str(&format!("wasm_new: {}\n", format_duration(wasm_new_result)));
|
||||
results.push_str(&format!(
|
||||
"wasm_key_gen: {}\n",
|
||||
format_duration(wasm_key_gen_result)
|
||||
));
|
||||
results.push_str(&format!(
|
||||
"calculateWitness: {}\n",
|
||||
format_duration(calculate_witness_result)
|
||||
));
|
||||
results.push_str(&format!(
|
||||
"wasm_generate_rln_proof_with_witness: {}\n",
|
||||
format_duration(wasm_generate_rln_proof_with_witness_result)
|
||||
));
|
||||
results.push_str(&format!(
|
||||
"wasm_verify_with_roots: {}\n",
|
||||
format_duration(wasm_verify_with_roots_result)
|
||||
));
|
||||
|
||||
// Log the results
|
||||
console_log!("{results}");
|
||||
}
|
||||
}
|
||||
247
rln-wasm/tests/node.rs
Normal file
247
rln-wasm/tests/node.rs
Normal file
@@ -0,0 +1,247 @@
|
||||
#![cfg(not(feature = "parallel"))]
|
||||
#![cfg(target_arch = "wasm32")]
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use js_sys::{BigInt as JsBigInt, Date, Object, Uint8Array};
|
||||
use rln::circuit::{Fr, TEST_TREE_DEPTH};
|
||||
use rln::hashers::{hash_to_field_le, poseidon_hash, PoseidonHash};
|
||||
use rln::protocol::{prepare_verify_input, rln_witness_from_values, serialize_witness};
|
||||
use rln::utils::{bytes_le_to_fr, fr_to_bytes_le, IdSecret};
|
||||
use rln_wasm::{
|
||||
wasm_generate_rln_proof_with_witness, wasm_new, wasm_rln_witness_to_json,
|
||||
wasm_verify_with_roots,
|
||||
};
|
||||
use rln_wasm_utils::wasm_key_gen;
|
||||
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
|
||||
use wasm_bindgen_test::{console_log, wasm_bindgen_test};
|
||||
use zerokit_utils::{
|
||||
OptimalMerkleProof, OptimalMerkleTree, ZerokitMerkleProof, ZerokitMerkleTree,
|
||||
};
|
||||
|
||||
const WITNESS_CALCULATOR_JS: &str = include_str!("../resources/witness_calculator.js");
|
||||
|
||||
#[wasm_bindgen(inline_js = r#"
|
||||
const fs = require("fs");
|
||||
|
||||
let witnessCalculatorModule = null;
|
||||
|
||||
module.exports = {
|
||||
initWitnessCalculator: function(code) {
|
||||
const processedCode = code
|
||||
.replace(/export\s+async\s+function\s+builder/, 'async function builder')
|
||||
.replace(/export\s*\{\s*builder\s*\};?/g, '');
|
||||
|
||||
const moduleFunc = new Function(processedCode + '\nreturn { builder };');
|
||||
witnessCalculatorModule = moduleFunc();
|
||||
|
||||
if (typeof witnessCalculatorModule.builder !== 'function') {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
},
|
||||
|
||||
readFile: function (path) {
|
||||
return fs.readFileSync(path);
|
||||
},
|
||||
|
||||
calculateWitness: async function (circom_path, inputs) {
|
||||
const wasmFile = fs.readFileSync(circom_path);
|
||||
const wasmFileBuffer = wasmFile.slice(
|
||||
wasmFile.byteOffset,
|
||||
wasmFile.byteOffset + wasmFile.byteLength
|
||||
);
|
||||
const witnessCalculator = await witnessCalculatorModule.builder(wasmFileBuffer);
|
||||
const calculatedWitness = await witnessCalculator.calculateWitness(
|
||||
inputs,
|
||||
false
|
||||
);
|
||||
return JSON.stringify(calculatedWitness, (key, value) =>
|
||||
typeof value === "bigint" ? value.toString() : value
|
||||
);
|
||||
},
|
||||
};
|
||||
"#)]
|
||||
extern "C" {
|
||||
#[wasm_bindgen(catch)]
|
||||
fn initWitnessCalculator(code: &str) -> Result<bool, JsValue>;
|
||||
|
||||
#[wasm_bindgen(catch)]
|
||||
fn readFile(path: &str) -> Result<Uint8Array, JsValue>;
|
||||
|
||||
#[wasm_bindgen(catch)]
|
||||
async fn calculateWitness(circom_path: &str, input: Object) -> Result<JsValue, JsValue>;
|
||||
}
|
||||
|
||||
const ARKZKEY_PATH: &str = "../rln/resources/tree_depth_30/rln_final.arkzkey";
|
||||
|
||||
const CIRCOM_PATH: &str = "../rln/resources/tree_depth_30/rln.wasm";
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
pub async fn rln_wasm_benchmark() {
|
||||
// Initialize witness calculator
|
||||
initWitnessCalculator(WITNESS_CALCULATOR_JS)
|
||||
.expect("Failed to initialize witness calculator");
|
||||
|
||||
let mut results = String::from("\nbenchmarks:\n");
|
||||
let iterations = 10;
|
||||
|
||||
let zkey = readFile(&ARKZKEY_PATH).expect("Failed to read zkey file");
|
||||
|
||||
// Benchmark wasm_new
|
||||
let start_wasm_new = Date::now();
|
||||
for _ in 0..iterations {
|
||||
let _ = wasm_new(zkey.clone()).expect("Failed to create RLN instance");
|
||||
}
|
||||
let wasm_new_result = Date::now() - start_wasm_new;
|
||||
|
||||
// Create RLN instance for other benchmarks
|
||||
let rln_instance = wasm_new(zkey).expect("Failed to create RLN instance");
|
||||
let mut tree: OptimalMerkleTree<PoseidonHash> =
|
||||
OptimalMerkleTree::default(TEST_TREE_DEPTH).expect("Failed to create tree");
|
||||
|
||||
// Benchmark wasm_key_gen
|
||||
let start_wasm_key_gen = Date::now();
|
||||
for _ in 0..iterations {
|
||||
let _ = wasm_key_gen(true).expect("Failed to generate keys");
|
||||
}
|
||||
let wasm_key_gen_result = Date::now() - start_wasm_key_gen;
|
||||
|
||||
// Generate identity pair for other benchmarks
|
||||
let mem_keys = wasm_key_gen(true).expect("Failed to generate keys");
|
||||
let id_key = mem_keys.subarray(0, 32);
|
||||
let (identity_secret_hash, _) = IdSecret::from_bytes_le(&id_key.to_vec());
|
||||
let (id_commitment, _) = bytes_le_to_fr(&mem_keys.subarray(32, 64).to_vec());
|
||||
|
||||
let epoch = hash_to_field_le(b"test-epoch");
|
||||
let rln_identifier = hash_to_field_le(b"test-rln-identifier");
|
||||
let external_nullifier = poseidon_hash(&[epoch, rln_identifier]);
|
||||
|
||||
let identity_index = tree.leaves_set();
|
||||
|
||||
let user_message_limit = Fr::from(100);
|
||||
|
||||
let rate_commitment = poseidon_hash(&[id_commitment, user_message_limit]);
|
||||
tree.update_next(rate_commitment)
|
||||
.expect("Failed to update tree");
|
||||
|
||||
let message_id = Fr::from(0);
|
||||
let signal: [u8; 32] = [0; 32];
|
||||
let x = hash_to_field_le(&signal);
|
||||
|
||||
let merkle_proof: OptimalMerkleProof<PoseidonHash> = tree
|
||||
.proof(identity_index)
|
||||
.expect("Failed to generate merkle proof");
|
||||
|
||||
let rln_witness = rln_witness_from_values(
|
||||
identity_secret_hash,
|
||||
merkle_proof.get_path_elements(),
|
||||
merkle_proof.get_path_index(),
|
||||
x,
|
||||
external_nullifier,
|
||||
user_message_limit,
|
||||
message_id,
|
||||
)
|
||||
.expect("Failed to create RLN witness");
|
||||
|
||||
let serialized_witness =
|
||||
serialize_witness(&rln_witness).expect("Failed to serialize witness");
|
||||
let witness_buffer = Uint8Array::from(&serialized_witness[..]);
|
||||
|
||||
let json_inputs = wasm_rln_witness_to_json(rln_instance, witness_buffer.clone())
|
||||
.expect("Failed to convert witness to JSON");
|
||||
|
||||
// Benchmark calculateWitness
|
||||
let start_calculate_witness = Date::now();
|
||||
for _ in 0..iterations {
|
||||
let _ = calculateWitness(&CIRCOM_PATH, json_inputs.clone())
|
||||
.await
|
||||
.expect("Failed to calculate witness");
|
||||
}
|
||||
let calculate_witness_result = Date::now() - start_calculate_witness;
|
||||
|
||||
// Calculate witness for other benchmarks
|
||||
let calculated_witness_json = calculateWitness(&CIRCOM_PATH, json_inputs)
|
||||
.await
|
||||
.expect("Failed to calculate witness")
|
||||
.as_string()
|
||||
.expect("Failed to convert calculated witness to string");
|
||||
let calculated_witness_vec_str: Vec<String> =
|
||||
serde_json::from_str(&calculated_witness_json).expect("Failed to parse JSON");
|
||||
let calculated_witness: Vec<JsBigInt> = calculated_witness_vec_str
|
||||
.iter()
|
||||
.map(|x| JsBigInt::new(&x.into()).expect("Failed to create JsBigInt"))
|
||||
.collect();
|
||||
|
||||
// Benchmark wasm_generate_rln_proof_with_witness
|
||||
let start_wasm_generate_rln_proof_with_witness = Date::now();
|
||||
for _ in 0..iterations {
|
||||
let _ = wasm_generate_rln_proof_with_witness(
|
||||
rln_instance,
|
||||
calculated_witness.clone(),
|
||||
witness_buffer.clone(),
|
||||
)
|
||||
.expect("Failed to generate proof");
|
||||
}
|
||||
let wasm_generate_rln_proof_with_witness_result =
|
||||
Date::now() - start_wasm_generate_rln_proof_with_witness;
|
||||
|
||||
// Generate a proof for other benchmarks
|
||||
let proof =
|
||||
wasm_generate_rln_proof_with_witness(rln_instance, calculated_witness, witness_buffer)
|
||||
.expect("Failed to generate proof");
|
||||
|
||||
let proof_data = proof.to_vec();
|
||||
let verify_input = prepare_verify_input(proof_data, &signal);
|
||||
let input_buffer = Uint8Array::from(&verify_input[..]);
|
||||
|
||||
let root = tree.root();
|
||||
let roots_serialized = fr_to_bytes_le(&root);
|
||||
let roots_buffer = Uint8Array::from(&roots_serialized[..]);
|
||||
|
||||
// Benchmark wasm_verify_with_roots
|
||||
let start_wasm_verify_with_roots = Date::now();
|
||||
for _ in 0..iterations {
|
||||
let _ =
|
||||
wasm_verify_with_roots(rln_instance, input_buffer.clone(), roots_buffer.clone())
|
||||
.expect("Failed to verify proof");
|
||||
}
|
||||
let wasm_verify_with_roots_result = Date::now() - start_wasm_verify_with_roots;
|
||||
|
||||
// Verify the proof with the root
|
||||
let is_proof_valid = wasm_verify_with_roots(rln_instance, input_buffer, roots_buffer)
|
||||
.expect("Failed to verify proof");
|
||||
assert!(is_proof_valid, "verification failed");
|
||||
|
||||
// Format and display results
|
||||
let format_duration = |duration_ms: f64| -> String {
|
||||
let avg_ms = duration_ms / (iterations as f64);
|
||||
if avg_ms >= 1000.0 {
|
||||
format!("{:.3} s", avg_ms / 1000.0)
|
||||
} else {
|
||||
format!("{:.3} ms", avg_ms)
|
||||
}
|
||||
};
|
||||
|
||||
results.push_str(&format!("wasm_new: {}\n", format_duration(wasm_new_result)));
|
||||
results.push_str(&format!(
|
||||
"wasm_key_gen: {}\n",
|
||||
format_duration(wasm_key_gen_result)
|
||||
));
|
||||
results.push_str(&format!(
|
||||
"calculate_witness: {}\n",
|
||||
format_duration(calculate_witness_result)
|
||||
));
|
||||
results.push_str(&format!(
|
||||
"wasm_generate_rln_proof_with_witness: {}\n",
|
||||
format_duration(wasm_generate_rln_proof_with_witness_result)
|
||||
));
|
||||
results.push_str(&format!(
|
||||
"wasm_verify_with_roots: {}\n",
|
||||
format_duration(wasm_verify_with_roots_result)
|
||||
));
|
||||
|
||||
// Log the results
|
||||
console_log!("{results}");
|
||||
}
|
||||
}
|
||||
@@ -1,123 +0,0 @@
|
||||
#![cfg(target_arch = "wasm32")]
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use js_sys::{BigInt as JsBigInt, Object, Uint8Array};
|
||||
use rln::circuit::{Fr, TEST_TREE_HEIGHT};
|
||||
use rln::hashers::{hash_to_field, poseidon_hash};
|
||||
use rln::utils::{bytes_le_to_fr, fr_to_bytes_le, normalize_usize};
|
||||
use rln_wasm::*;
|
||||
use rln::poseidon_tree::PoseidonTree;
|
||||
use rln::utils::vec_fr_to_bytes_le;
|
||||
use wasm_bindgen::{prelude::*, JsValue};
|
||||
use wasm_bindgen_test::wasm_bindgen_test;
|
||||
use zerokit_utils::merkle_tree::merkle_tree::ZerokitMerkleTree;
|
||||
use zerokit_utils::ZerokitMerkleProof;
|
||||
use rln::utils::vec_u8_to_bytes_le;
|
||||
|
||||
#[wasm_bindgen(module = "src/utils.js")]
|
||||
extern "C" {
|
||||
#[wasm_bindgen(catch)]
|
||||
fn read_file(path: &str) -> Result<Uint8Array, JsValue>;
|
||||
|
||||
#[wasm_bindgen(catch)]
|
||||
async fn calculateWitness(circom_path: &str, input: Object) -> Result<JsValue, JsValue>;
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
pub async fn test_basic_flow() {
|
||||
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.arkvkey");
|
||||
let zkey = read_file(&zkey_path).unwrap();
|
||||
let vk = read_file(&vk_path).unwrap();
|
||||
|
||||
// Creating an instance of RLN
|
||||
let rln_instance = wasm_new(zkey, vk).unwrap();
|
||||
|
||||
let mut tree = PoseidonTree::default(TEST_TREE_HEIGHT).unwrap();
|
||||
|
||||
// Creating membership key
|
||||
let mem_keys = wasm_key_gen(rln_instance).unwrap();
|
||||
let id_key = mem_keys.subarray(0, 32);
|
||||
let id_commitment = mem_keys.subarray(32, 64);
|
||||
|
||||
// Prepare the message
|
||||
let signal = b"Hello World";
|
||||
|
||||
let identity_index = tree.leaves_set();
|
||||
// Setting up the epoch and rln_identifier
|
||||
let epoch = hash_to_field(b"test-epoch");
|
||||
let rln_identifier = hash_to_field(b"test-rln-identifier");
|
||||
|
||||
let external_nullifier = poseidon_hash(&[epoch, rln_identifier]);
|
||||
let external_nullifier = fr_to_bytes_le(&external_nullifier);
|
||||
|
||||
let user_message_limit = Fr::from(100);
|
||||
let message_id = fr_to_bytes_le(&Fr::from(0));
|
||||
|
||||
let (id_commitment_fr, _) = bytes_le_to_fr(&id_commitment.to_vec()[..]);
|
||||
let rate_commitment = poseidon_hash(&[id_commitment_fr, user_message_limit]);
|
||||
tree.update_next(rate_commitment).unwrap();
|
||||
|
||||
let x = hash_to_field(signal);
|
||||
let merkle_proof = tree.proof(identity_index).expect("proof should exist");
|
||||
let path_elements = merkle_proof.get_path_elements();
|
||||
let identity_path_index = merkle_proof.get_path_index();
|
||||
|
||||
|
||||
// Serializing the message
|
||||
let mut serialized_vec: Vec<u8> = Vec::new();
|
||||
serialized_vec.append(&mut id_key.to_vec());
|
||||
serialized_vec.append(&mut fr_to_bytes_le(&user_message_limit).to_vec());
|
||||
serialized_vec.append(&mut message_id.to_vec());
|
||||
serialized_vec.append(&mut vec_fr_to_bytes_le(&path_elements).unwrap());
|
||||
serialized_vec.append(&mut vec_u8_to_bytes_le(&identity_path_index).unwrap());
|
||||
serialized_vec.append(&mut fr_to_bytes_le(&x));
|
||||
serialized_vec.append(&mut external_nullifier.to_vec());
|
||||
let serialized_message = Uint8Array::from(&serialized_vec[..]);
|
||||
|
||||
// Obtaining inputs that should be sent to circom witness calculator
|
||||
let json_inputs =
|
||||
rln_witness_to_json(rln_instance, serialized_message.clone()).unwrap();
|
||||
|
||||
// Calculating witness with JS
|
||||
// (Using a JSON since wasm_bindgen does not like Result<Vec<JsBigInt>,JsValue>)
|
||||
let calculated_witness_json = calculateWitness(&circom_path, json_inputs)
|
||||
.await
|
||||
.unwrap()
|
||||
.as_string()
|
||||
.unwrap();
|
||||
let calculated_witness_vec_str: Vec<String> =
|
||||
serde_json::from_str(&calculated_witness_json).unwrap();
|
||||
let calculated_witness: Vec<JsBigInt> = calculated_witness_vec_str
|
||||
.iter()
|
||||
.map(|x| JsBigInt::new(&x.into()).unwrap())
|
||||
.collect();
|
||||
|
||||
// Generating proof
|
||||
let proof = generate_rln_proof_with_witness(
|
||||
rln_instance,
|
||||
calculated_witness.into(),
|
||||
serialized_message,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
// Add signal_len | signal
|
||||
let mut proof_bytes = proof.to_vec();
|
||||
proof_bytes.append(&mut normalize_usize(signal.len()));
|
||||
proof_bytes.append(&mut signal.to_vec());
|
||||
let proof_with_signal = Uint8Array::from(&proof_bytes[..]);
|
||||
|
||||
// Validating Proof with Roots
|
||||
let root = tree.root();
|
||||
let root_le = fr_to_bytes_le(&root);
|
||||
let roots = Uint8Array::from(&root_le[..]);
|
||||
let proof_with_signal = Uint8Array::from(&proof_bytes[..]);
|
||||
|
||||
let is_proof_valid = wasm_verify_with_roots(rln_instance, proof_with_signal, roots);
|
||||
assert!(is_proof_valid.unwrap(), "verifying proof with roots failed");
|
||||
}
|
||||
}
|
||||
112
rln/Cargo.toml
112
rln/Cargo.toml
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "rln"
|
||||
version = "0.5.1"
|
||||
version = "0.8.0"
|
||||
edition = "2021"
|
||||
license = "MIT OR Apache-2.0"
|
||||
description = "APIs to manage, compute and verify zkSNARK proofs and RLN primitives"
|
||||
@@ -9,95 +9,73 @@ homepage = "https://vac.dev"
|
||||
repository = "https://github.com/vacp2p/zerokit"
|
||||
|
||||
[lib]
|
||||
crate-type = ["rlib", "staticlib"]
|
||||
crate-type = ["rlib", "staticlib", "cdylib"]
|
||||
bench = false
|
||||
|
||||
# This flag disable cargo doctests, i.e. testing example code-snippets in documentation
|
||||
doctest = false
|
||||
|
||||
|
||||
[dependencies]
|
||||
# ZKP Generation
|
||||
ark-ec = { version = "=0.4.1", default-features = false }
|
||||
ark-ff = { version = "=0.4.1", default-features = false, features = ["asm"] }
|
||||
ark-std = { version = "=0.4.0", default-features = false }
|
||||
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-zkey = { version = "0.1.0", optional = true, default-features = false }
|
||||
|
||||
# WASM
|
||||
wasmer = { version = "=2.3.0", default-features = false }
|
||||
ark-bn254 = { version = "0.5.0", features = ["std"] }
|
||||
ark-relations = { version = "0.5.1", features = ["std"] }
|
||||
ark-ff = { version = "0.5.0", default-features = false }
|
||||
ark-ec = { version = "0.5.0", default-features = false }
|
||||
ark-std = { version = "0.5.0", default-features = false }
|
||||
ark-poly = { version = "0.5.0", default-features = false }
|
||||
ark-groth16 = { version = "0.5.0", default-features = false }
|
||||
ark-serialize = { version = "0.5.0", default-features = false }
|
||||
|
||||
# error handling
|
||||
color-eyre = "=0.6.2"
|
||||
thiserror = "=1.0.39"
|
||||
thiserror = "2.0.12"
|
||||
|
||||
# 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"
|
||||
lazy_static = "=1.4.0"
|
||||
rand = "=0.8.5"
|
||||
rand_chacha = "=0.3.1"
|
||||
tiny-keccak = { version = "=2.0.2", features = ["keccak"] }
|
||||
utils = { package = "zerokit_utils", version = "=0.5.1", path = "../utils/", default-features = false }
|
||||
|
||||
rayon = { version = "1.10.0", optional = true }
|
||||
byteorder = "1.5.0"
|
||||
cfg-if = "1.0"
|
||||
num-bigint = { version = "0.4.6", default-features = false, features = ["std"] }
|
||||
num-traits = "0.2.19"
|
||||
once_cell = "1.21.3"
|
||||
lazy_static = "1.5.0"
|
||||
rand = "0.8.5"
|
||||
rand_chacha = "0.3.1"
|
||||
ruint = { version = "1.15.0", features = ["rand", "serde", "ark-ff-04"] }
|
||||
tiny-keccak = { version = "2.0.2", features = ["keccak"] }
|
||||
zeroize = "1.8"
|
||||
tempfile = "3.21.0"
|
||||
utils = { package = "zerokit_utils", version = "0.6.0", path = "../utils", default-features = false }
|
||||
|
||||
# serialization
|
||||
serde_json = "=1.0.96"
|
||||
serde = { version = "=1.0.163", features = ["derive"] }
|
||||
prost = "0.14.1"
|
||||
serde_json = "1.0.141"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
|
||||
document-features = { version = "=0.2.10", optional = true }
|
||||
document-features = { version = "0.2.11", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
sled = "=0.34.7"
|
||||
criterion = { version = "=0.4.0", features = ["html_reports"] }
|
||||
criterion = { version = "0.7.0", features = ["html_reports"] }
|
||||
|
||||
[features]
|
||||
default = ["parallel", "wasmer/sys-default", "pmtree-ft"]
|
||||
parallel = [
|
||||
"ark-ec/parallel",
|
||||
"ark-ff/parallel",
|
||||
"ark-std/parallel",
|
||||
"ark-groth16/parallel",
|
||||
"utils/parallel",
|
||||
]
|
||||
wasm = ["wasmer/js", "wasmer/std"]
|
||||
fullmerkletree = ["default"]
|
||||
arkzkey = ["ark-zkey"]
|
||||
default = ["parallel", "pmtree-ft"]
|
||||
stateless = []
|
||||
|
||||
# Note: pmtree feature is still experimental
|
||||
pmtree-ft = ["utils/pmtree-ft"]
|
||||
parallel = [
|
||||
"rayon",
|
||||
"utils/parallel",
|
||||
"ark-ff/parallel",
|
||||
"ark-ec/parallel",
|
||||
"ark-std/parallel",
|
||||
"ark-poly/parallel",
|
||||
"ark-groth16/parallel",
|
||||
"ark-serialize/parallel",
|
||||
]
|
||||
fullmerkletree = [] # Pre-allocated tree, fastest access
|
||||
optimalmerkletree = [] # Sparse storage, memory efficient
|
||||
pmtree-ft = ["utils/pmtree-ft"] # Persistent storage, disk-based
|
||||
|
||||
[[bench]]
|
||||
name = "pmtree_benchmark"
|
||||
harness = false
|
||||
|
||||
[[bench]]
|
||||
name = "circuit_loading_benchmark"
|
||||
harness = false
|
||||
|
||||
[[bench]]
|
||||
name = "circuit_loading_arkzkey_benchmark"
|
||||
harness = false
|
||||
required-features = ["arkzkey"]
|
||||
|
||||
[[bench]]
|
||||
name = "circuit_deser_benchmark"
|
||||
harness = false
|
||||
required-features = ["pmtree-ft"]
|
||||
|
||||
[[bench]]
|
||||
name = "poseidon_tree_benchmark"
|
||||
|
||||
@@ -2,17 +2,13 @@
|
||||
command = "cargo"
|
||||
args = ["build", "--release"]
|
||||
|
||||
[tasks.test_default]
|
||||
[tasks.test]
|
||||
command = "cargo"
|
||||
args = ["test", "--release"]
|
||||
args = ["test", "--release", "--", "--nocapture"]
|
||||
|
||||
[tasks.test_stateless]
|
||||
command = "cargo"
|
||||
args = ["test", "--release", "--features", "stateless"]
|
||||
|
||||
[tasks.test_arkzkey]
|
||||
command = "cargo"
|
||||
args = ["test", "--release", "--features", "arkzkey"]
|
||||
args = ["test", "--release", "--no-default-features", "--features", "stateless"]
|
||||
|
||||
[tasks.bench]
|
||||
command = "cargo"
|
||||
|
||||
412
rln/README.md
412
rln/README.md
@@ -1,65 +1,22 @@
|
||||
# Zerokit RLN Module
|
||||
|
||||
This module provides APIs to manage, compute and verify [RLN](https://rfc.vac.dev/spec/32/) zkSNARK proofs and RLN primitives.
|
||||
[](https://crates.io/crates/rln)
|
||||
[](https://opensource.org/licenses/MIT)
|
||||
[](https://opensource.org/licenses/Apache-2.0)
|
||||
|
||||
## Pre-requisites
|
||||
The Zerokit RLN Module provides a Rust implementation for working with
|
||||
Rate-Limiting Nullifier [RLN](https://rfc.vac.dev/spec/32/) zkSNARK proofs and primitives.
|
||||
This module allows you to:
|
||||
|
||||
### Install dependencies and clone repo
|
||||
- Generate and verify RLN proofs
|
||||
- Work with Merkle trees for commitment storage
|
||||
- Implement rate-limiting mechanisms for distributed systems
|
||||
|
||||
```sh
|
||||
make installdeps
|
||||
git clone https://github.com/vacp2p/zerokit.git
|
||||
cd zerokit/rln
|
||||
```
|
||||
## Quick Start
|
||||
|
||||
### Build and Test
|
||||
|
||||
To build and test, run the following commands within the module folder
|
||||
|
||||
```bash
|
||||
cargo make build
|
||||
cargo make test
|
||||
```
|
||||
|
||||
### Compile ZK circuits
|
||||
|
||||
The `rln` (https://github.com/rate-limiting-nullifier/circom-rln) repository, which contains the RLN circuit implementation is a submodule of zerokit RLN.
|
||||
|
||||
To compile the RLN circuit
|
||||
|
||||
```sh
|
||||
# Update submodules
|
||||
git submodule update --init --recursive
|
||||
|
||||
# Install rln dependencies
|
||||
cd vendor/rln/ && npm install
|
||||
|
||||
# Build circuits
|
||||
./scripts/build-circuits.sh rln
|
||||
|
||||
# Copy over assets
|
||||
cp build/zkeyFiles/rln-final.zkey ../../resources/tree_height_15
|
||||
cp build/zkeyFiles/rln.wasm ../../resources/tree_height_15
|
||||
```
|
||||
|
||||
Note that the above code snippet will compile a RLN circuit with a Merkle tree of height equal `15` based on the default value set in `vendor/rln/circuit/rln.circom`.
|
||||
|
||||
In order to compile a RLN circuit with Merkle tree height `N`, it suffices to change `vendor/rln/circuit/rln.circom` to
|
||||
|
||||
```
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "./rln-base.circom";
|
||||
|
||||
component main {public [x, epoch, rln_identifier ]} = RLN(N);
|
||||
```
|
||||
|
||||
However, if `N` is too big, this might require a bigger Powers of Tau ceremony than the one hardcoded in `./scripts/build-circuits.sh`, which is `2^14`.
|
||||
In such case we refer to the official [Circom documentation](https://docs.circom.io/getting-started/proving-circuits/#powers-of-tau) for instructions on how to run an appropriate Powers of Tau ceremony and Phase 2 in order to compile the desired circuit.
|
||||
|
||||
Currently, the `rln` module comes with 2 [pre-compiled](https://github.com/vacp2p/zerokit/tree/master/rln/resources) RLN circuits having Merkle tree of height `20` and `32`, respectively.
|
||||
|
||||
## Getting started
|
||||
> [!IMPORTANT]
|
||||
> Version 0.7.0 is the only version that does not support WASM and x32 architecture.
|
||||
> WASM support is available in version 0.8.0 and above.
|
||||
|
||||
### Add RLN as dependency
|
||||
|
||||
@@ -70,136 +27,284 @@ We start by adding zerokit RLN to our `Cargo.toml`
|
||||
rln = { git = "https://github.com/vacp2p/zerokit" }
|
||||
```
|
||||
|
||||
### Create a RLN object
|
||||
## Basic Usage Example
|
||||
|
||||
First, we need to create a RLN object for a chosen input Merkle tree size.
|
||||
The RLN object constructor requires the following files:
|
||||
|
||||
Note that we need to pass to RLN object constructor the path where the circuit (`rln.wasm`, built for the input tree size), the corresponding proving key (`rln_final.zkey`) or (`rln_final.arkzkey`) and verification key (`verification_key.arkvkey`, optional) are found.
|
||||
- `rln_final.arkzkey`: The proving key in arkzkey format.
|
||||
- `graph.bin`: The graph file built for the input tree size
|
||||
|
||||
In the following we will use [cursors](https://doc.rust-lang.org/std/io/struct.Cursor.html) as readers/writers for interfacing with RLN public APIs.
|
||||
Additionally, `rln.wasm` is used for testing in the rln-wasm module.
|
||||
|
||||
In the following we will use [cursors](https://doc.rust-lang.org/std/io/struct.Cursor.html)
|
||||
as readers/writers for interfacing with RLN public APIs.
|
||||
|
||||
```rust
|
||||
use rln::protocol::*;
|
||||
use rln::public::*;
|
||||
use std::io::Cursor;
|
||||
|
||||
// We set the RLN parameters:
|
||||
// - the tree height;
|
||||
// - the tree config, if it is not defined, the default value will be set
|
||||
let tree_height = 20;
|
||||
let input = Cursor::new(json!({}).to_string());
|
||||
use rln::{
|
||||
circuit::Fr,
|
||||
hashers::{hash_to_field, poseidon_hash},
|
||||
protocol::{keygen, prepare_prove_input, prepare_verify_input},
|
||||
public::RLN,
|
||||
utils::fr_to_bytes_le,
|
||||
};
|
||||
use serde_json::json;
|
||||
|
||||
// We create a new RLN instance
|
||||
let mut rln = RLN::new(tree_height, input);
|
||||
fn main() {
|
||||
// 1. Initialize RLN with parameters:
|
||||
// - the tree depth;
|
||||
// - the tree config, if it is not defined, the default value will be set
|
||||
let tree_depth = 30;
|
||||
let input = Cursor::new(json!({}).to_string());
|
||||
let mut rln = RLN::new(tree_depth, input).unwrap();
|
||||
|
||||
// 2. Generate an identity keypair
|
||||
let (identity_secret_hash, id_commitment) = keygen();
|
||||
|
||||
// 3. Add a rate commitment to the Merkle tree
|
||||
let id_index = 10;
|
||||
let user_message_limit = Fr::from(10);
|
||||
let rate_commitment = poseidon_hash(&[id_commitment, user_message_limit]);
|
||||
let mut buffer = Cursor::new(fr_to_bytes_le(&rate_commitment));
|
||||
rln.set_leaf(id_index, &mut buffer).unwrap();
|
||||
|
||||
// 4. Set up external nullifier (epoch + app identifier)
|
||||
// We generate epoch from a date seed and we ensure is
|
||||
// mapped to a field element by hashing-to-field its content
|
||||
let epoch = hash_to_field(b"Today at noon, this year");
|
||||
// We generate rln_identifier from a date seed and we ensure is
|
||||
// mapped to a field element by hashing-to-field its content
|
||||
let rln_identifier = hash_to_field(b"test-rln-identifier");
|
||||
// We generate a external nullifier
|
||||
let external_nullifier = poseidon_hash(&[epoch, rln_identifier]);
|
||||
// We choose a message_id satisfy 0 <= message_id < user_message_limit
|
||||
let message_id = Fr::from(1);
|
||||
|
||||
// 5. Generate and verify a proof for a message
|
||||
let signal = b"RLN is awesome";
|
||||
|
||||
// 6. Prepare input for generate_rln_proof API
|
||||
// input_data is [ identity_secret<32> | id_index<8> | external_nullifier<32>
|
||||
// | user_message_limit<32> | message_id<32> | signal_len<8> | signal<var> ]
|
||||
let prove_input = prepare_prove_input(
|
||||
identity_secret_hash,
|
||||
id_index,
|
||||
user_message_limit,
|
||||
message_id,
|
||||
external_nullifier,
|
||||
signal,
|
||||
);
|
||||
|
||||
// 7. Generate a RLN proof
|
||||
// We generate a RLN proof for proof_input
|
||||
let mut input_buffer = Cursor::new(prove_input);
|
||||
let mut output_buffer = Cursor::new(Vec::<u8>::new());
|
||||
rln.generate_rln_proof(&mut input_buffer, &mut output_buffer)
|
||||
.unwrap();
|
||||
|
||||
// We get the public outputs returned by the circuit evaluation
|
||||
// The byte vector `proof_data` is serialized as
|
||||
// `[ zk-proof | tree_root | external_nullifier | share_x | share_y | nullifier ]`.
|
||||
let proof_data = output_buffer.into_inner();
|
||||
|
||||
// 8. Verify a RLN proof
|
||||
// Input buffer is serialized as `[proof_data | signal_len | signal ]`,
|
||||
// where `proof_data` is (computed as) the output obtained by `generate_rln_proof`.
|
||||
let verify_data = prepare_verify_input(proof_data, signal);
|
||||
|
||||
// We verify the zk-proof against the provided proof values
|
||||
let mut input_buffer = Cursor::new(verify_data);
|
||||
let verified = rln.verify_rln_proof(&mut input_buffer).unwrap();
|
||||
|
||||
// We ensure the proof is valid
|
||||
assert!(verified);
|
||||
}
|
||||
```
|
||||
|
||||
### Generate an identity keypair
|
||||
|
||||
We generate an identity keypair
|
||||
|
||||
```rust
|
||||
// We generate an identity pair
|
||||
let mut buffer = Cursor::new(Vec::<u8>::new());
|
||||
rln.key_gen(&mut buffer).unwrap();
|
||||
|
||||
// We deserialize the keygen output to obtain
|
||||
// the identity_secret and id_commitment
|
||||
let (identity_secret_hash, id_commitment) = deserialize_identity_pair(buffer.into_inner());
|
||||
```
|
||||
|
||||
### Add Rate commitment to the RLN Merkle tree
|
||||
|
||||
```rust
|
||||
// We define the tree index where id_commitment will be added
|
||||
let id_index = 10;
|
||||
let user_message_limit = 10;
|
||||
|
||||
// We serialize id_commitment and pass it to set_leaf
|
||||
let rate_commitment = poseidon_hash(&[id_commitment, user_message_limit]);
|
||||
let mut buffer = Cursor::new(serialize_field_element(rate_commitment));
|
||||
rln.set_leaf(id_index, &mut buffer).unwrap();
|
||||
```
|
||||
|
||||
Note that when tree leaves are not explicitly set by the user (in this example, all those with index less and greater than `10`), their values is set to an hardcoded default (all-`0` bytes in current implementation).
|
||||
|
||||
### Set external nullifier
|
||||
### Comments for the code above for point 4
|
||||
|
||||
The `external nullifier` includes two parameters.
|
||||
|
||||
The first one is `epoch` and it's used to identify messages received in a certain time frame. It usually corresponds to the current UNIX time but can also be set to a random value or generated by a seed, provided that it corresponds to a field element.
|
||||
The first one is `epoch` and it's used to identify messages received in a certain time frame.
|
||||
It usually corresponds to the current UNIX time but can also be set to a random value or generated by a seed,
|
||||
provided that it corresponds to a field element.
|
||||
|
||||
The second one is `rln_identifier` and it's used to prevent a RLN ZK proof generated for one application to be re-used in another one.
|
||||
The second one is `rln_identifier` and it's used to prevent a RLN ZK proof generated
|
||||
for one application to be re-used in another one.
|
||||
|
||||
```rust
|
||||
// We generate epoch from a date seed and we ensure is
|
||||
// mapped to a field element by hashing-to-field its content
|
||||
let epoch = hash_to_field(b"Today at noon, this year");
|
||||
// We generate rln_identifier from a date seed and we ensure is
|
||||
// mapped to a field element by hashing-to-field its content
|
||||
let rln_identifier = hash_to_field(b"test-rln-identifier");
|
||||
### Features
|
||||
|
||||
let external_nullifier = poseidon_hash(&[epoch, rln_identifier]);
|
||||
- **Stateless Mode**: Allows the use of RLN without maintaining state of the Merkle tree.
|
||||
- **Pre-compiled Circuits**: Ready-to-use circuits with Merkle tree depth of 20
|
||||
- **Wasm Support**: WebAssembly bindings via rln-wasm crate with features like:
|
||||
- Browser and Node.js compatibility
|
||||
- Optional parallel feature support using [wasm-bindgen-rayon](https://github.com/RReverser/wasm-bindgen-rayon)
|
||||
- Headless browser testing capabilities
|
||||
- **Merkle Tree Implementations**: Multiple tree variants optimized for different use cases:
|
||||
- **Full Merkle Tree**: Fastest access with complete pre-allocated tree in memory. Best for frequent random access (enable with `fullmerkletree` feature).
|
||||
- **Optimal Merkle Tree**: Memory-efficient sparse storage using HashMap. Ideal for partially populated trees (enable with `optimalmerkletree` feature).
|
||||
- **Persistent Merkle Tree**: Disk-based storage with [sled](https://github.com/spacejam/sled) for persistence across application restarts and large datasets (enable with `pmtree-ft` feature).
|
||||
|
||||
## Building and Testing
|
||||
|
||||
### Prerequisites
|
||||
|
||||
```sh
|
||||
git clone https://github.com/vacp2p/zerokit.git
|
||||
make installdeps
|
||||
cd zerokit/rln
|
||||
```
|
||||
|
||||
### Set signal
|
||||
### Build Commands
|
||||
|
||||
The signal is the message for which we are computing a RLN proof.
|
||||
```sh
|
||||
# Build with default features
|
||||
cargo make build
|
||||
|
||||
```rust
|
||||
// We set our signal
|
||||
let signal = b"RLN is awesome";
|
||||
# Test with default features
|
||||
cargo make test
|
||||
|
||||
# Test with stateless features
|
||||
cargo make test_stateless
|
||||
```
|
||||
|
||||
### Generate a RLN proof
|
||||
## Advanced: Custom Circuit Compilation
|
||||
|
||||
We prepare the input to the proof generation routine.
|
||||
The `rln` (<https://github.com/rate-limiting-nullifier/circom-rln>) repository,
|
||||
which contains the RLN circuit implementation is using for pre-compiled RLN circuit for zerokit RLN.
|
||||
If you want to compile your own RLN circuit, you can follow the instructions below.
|
||||
|
||||
Input buffer is serialized as `[ identity_key | id_index | external_nullifier | user_message_limit | message_id | signal_len | signal ]`.
|
||||
### 1. Compile ZK Circuits for getting the zkey file
|
||||
|
||||
```rust
|
||||
// We prepare input to the proof generation routine
|
||||
let proof_input = prepare_prove_input(identity_secret_hash, id_index, external_nullifier, signal);
|
||||
This script actually generates not only the zkey file for the RLN circuit,
|
||||
but also the execution wasm file used for witness calculation.
|
||||
However, the wasm file is not needed for the `rln` module,
|
||||
because current implementation uses the iden3 graph file for witness calculation.
|
||||
This graph file is generated by the `circom-witnesscalc` tool in [step 2](#2-generate-witness-calculation-graph).
|
||||
|
||||
To customize the circuit parameters, modify `circom-rln/circuits/rln.circom`:
|
||||
|
||||
```circom
|
||||
pragma circom 2.1.0;
|
||||
include "./rln.circom";
|
||||
component main { public [x, externalNullifier] } = RLN(N, M);
|
||||
```
|
||||
|
||||
We are now ready to generate a RLN ZK proof along with the _public outputs_ of the ZK circuit evaluation.
|
||||
Where:
|
||||
|
||||
```rust
|
||||
- `N`: Merkle tree depth, determining the maximum membership capacity (2^N members).
|
||||
|
||||
// We generate a RLN proof for proof_input
|
||||
let mut in_buffer = Cursor::new(proof_input);
|
||||
let mut out_buffer = Cursor::new(Vec::<u8>::new());
|
||||
rln.generate_rln_proof(&mut in_buffer, &mut out_buffer)
|
||||
.unwrap();
|
||||
- `M`: Bit size for range checks, setting an upper bound for the number of messages per epoch (2^M messages).
|
||||
|
||||
// We get the public outputs returned by the circuit evaluation
|
||||
let proof_data = out_buffer.into_inner();
|
||||
> [!NOTE]
|
||||
> However, if `N` is too big, this might require a larger Powers of Tau ceremony
|
||||
> than the one hardcoded in `./scripts/build-circuits.sh`, which is `2^14`.
|
||||
> In such case, we refer to the official
|
||||
> [Circom documentation](https://docs.circom.io/getting-started/proving-circuits/#powers-of-tau)
|
||||
> for instructions on how to run an appropriate Powers of Tau ceremony and Phase 2 in order to compile the desired circuit. \
|
||||
> Additionally, while `M` sets an upper bound on the number of messages per epoch (`2^M`),
|
||||
> you can configure lower message limit for your use case, as long as it satisfies `user_message_limit ≤ 2^M`. \
|
||||
> Currently, the `rln` module comes with a [pre-compiled](https://github.com/vacp2p/zerokit/tree/master/rln/resources)
|
||||
> RLN circuit with a Merkle tree of depth `20` and a bit size of `16`,
|
||||
> allowing up to `2^20` registered members and a `2^16` message limit per epoch.
|
||||
|
||||
#### Install circom compiler
|
||||
|
||||
You can follow the instructions below or refer to the
|
||||
[installing Circom](https://docs.circom.io/getting-started/installation/#installing-circom) guide for more details,
|
||||
but make sure to use the specific version `v2.1.0`.
|
||||
|
||||
```sh
|
||||
# Clone the circom repository
|
||||
git clone https://github.com/iden3/circom.git
|
||||
|
||||
# Checkout the specific version
|
||||
cd circom && git checkout v2.1.0
|
||||
|
||||
# Build the circom compiler
|
||||
cargo build --release
|
||||
|
||||
# Install the circom binary globally
|
||||
cargo install --path circom
|
||||
|
||||
# Check the circom version to ensure it's v2.1.0
|
||||
circom --version
|
||||
```
|
||||
|
||||
The byte vector `proof_data` is serialized as `[ zk-proof | tree_root | external_nullifier | share_x | share_y | nullifier ]`.
|
||||
#### Generate the zkey and verification key files example
|
||||
|
||||
### Verify a RLN proof
|
||||
```sh
|
||||
# Clone the circom-rln repository
|
||||
git clone https://github.com/rate-limiting-nullifier/circom-rln
|
||||
|
||||
We prepare the input to the proof verification routine.
|
||||
# Install dependencies
|
||||
cd circom-rln && npm install
|
||||
|
||||
Input buffer is serialized as `[proof_data | signal_len | signal ]`, where `proof_data` is (computed as) the output obtained by `generate_rln_proof`.
|
||||
# Build circuits
|
||||
./scripts/build-circuits.sh rln
|
||||
|
||||
```rust
|
||||
// We prepare input to the proof verification routine
|
||||
let verify_data = prepare_verify_input(proof_data, signal);
|
||||
|
||||
// We verify the zk-proof against the provided proof values
|
||||
let mut in_buffer = Cursor::new(verify_data);
|
||||
let verified = rln.verify(&mut in_buffer).unwrap();
|
||||
# Use the generated zkey file in subsequent steps
|
||||
cp zkeyFiles/rln/final.zkey <path_to_rln_final.zkey>
|
||||
```
|
||||
|
||||
We check if the proof verification was successful:
|
||||
### 2. Generate Witness Calculation Graph
|
||||
|
||||
```rust
|
||||
// We ensure the proof is valid
|
||||
assert!(verified);
|
||||
The execution graph file used for witness calculation can be compiled following instructions
|
||||
in the [circom-witnesscalc](https://github.com/iden3/circom-witnesscalc) repository.
|
||||
As mentioned in step 1, we should use `rln.circom` file from `circom-rln` repository.
|
||||
|
||||
```sh
|
||||
# Clone the circom-witnesscalc repository
|
||||
git clone https://github.com/iden3/circom-witnesscalc
|
||||
|
||||
# Load the submodules
|
||||
cd circom-witnesscalc && git submodule update --init --recursive
|
||||
|
||||
# Build the circom-witnesscalc tool
|
||||
cargo build
|
||||
|
||||
# Generate the witness calculation graph
|
||||
cargo run --package circom_witnesscalc --bin build-circuit ../circom-rln/circuits/rln.circom <path_to_graph.bin>
|
||||
```
|
||||
|
||||
## Get involved!
|
||||
The `rln` module comes with [pre-compiled](https://github.com/vacp2p/zerokit/tree/master/rln/resources)
|
||||
execution graph files for the RLN circuit.
|
||||
|
||||
### 3. Generate Arkzkey Representation for zkey file
|
||||
|
||||
For faster loading, compile the zkey file into the arkzkey format using
|
||||
[ark-zkey](https://github.com/seemenkina/ark-zkey).
|
||||
This is fork of the [original](https://github.com/zkmopro/ark-zkey) repository with the uncompressed arkzkey support.
|
||||
|
||||
```sh
|
||||
# Clone the ark-zkey repository
|
||||
git clone https://github.com/seemenkina/ark-zkey.git
|
||||
|
||||
# Build the ark-zkey tool
|
||||
cd ark-zkey && cargo build
|
||||
|
||||
# Generate the arkzkey representation for the zkey file
|
||||
cargo run --bin arkzkey-util <path_to_rln_final.zkey>
|
||||
```
|
||||
|
||||
This will generate the `rln_final.arkzkey` file, which is used by the `rln` module.
|
||||
|
||||
Currently, the `rln` module comes with
|
||||
[pre-compiled](https://github.com/vacp2p/zerokit/tree/master/rln/resources) arkzkey keys for the RLN circuit.
|
||||
|
||||
> [!NOTE]
|
||||
> You can use this [convert_zkey.sh](./convert_zkey.sh) script
|
||||
> to automate the process of generating the arkzkey file from any zkey file
|
||||
|
||||
Run the script as follows:
|
||||
|
||||
```sh
|
||||
chmod +x ./convert_zkey.sh
|
||||
./convert_zkey.sh <path_to_rln_final.zkey>
|
||||
```
|
||||
|
||||
## Get involved
|
||||
|
||||
Zerokit RLN public and FFI APIs allow interaction with many more features than what briefly showcased above.
|
||||
|
||||
@@ -210,3 +315,20 @@ cargo doc --no-deps
|
||||
```
|
||||
|
||||
and look at unit tests to have an hint on how to interface and use them.
|
||||
|
||||
## Detailed Protocol Flow
|
||||
|
||||
1. **Identity Creation**: Generate a secret key and commitment
|
||||
2. **Rate Commitment**: Add commitment to a Merkle tree
|
||||
3. **External Nullifier Setup**: Combine epoch and application identifier
|
||||
4. **Proof Generation**: Create a zkSNARK proof that:
|
||||
- Proves membership in the Merkle tree
|
||||
- Ensures rate-limiting constraints are satisfied
|
||||
- Generates a nullifier to prevent double-usage
|
||||
5. **Proof Verification**: Verify the proof without revealing the prover's identity
|
||||
|
||||
## Getting Involved
|
||||
|
||||
- Check the [unit tests](https://github.com/vacp2p/zerokit/tree/master/rln/tests) for more usage examples
|
||||
- [RFC specification](https://rfc.vac.dev/spec/32/) for the Rate-Limiting Nullifier protocol
|
||||
- [GitHub repository](https://github.com/vacp2p/zerokit) for the latest updates
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
use criterion::{criterion_group, criterion_main, Criterion};
|
||||
use rln::circuit::{vk_from_ark_serialized, VK_BYTES};
|
||||
|
||||
// Here we benchmark how long the deserialization of the
|
||||
// verifying_key takes, only testing the json => verifying_key conversion,
|
||||
// and skipping conversion from bytes => string => serde_json::Value
|
||||
pub fn vk_deserialize_benchmark(c: &mut Criterion) {
|
||||
let vk = VK_BYTES;
|
||||
|
||||
c.bench_function("vk::vk_from_ark_serialized", |b| {
|
||||
b.iter(|| {
|
||||
let _ = vk_from_ark_serialized(vk);
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
criterion_group! {
|
||||
name = benches;
|
||||
config = Criterion::default().measurement_time(std::time::Duration::from_secs(10));
|
||||
targets = vk_deserialize_benchmark
|
||||
}
|
||||
criterion_main!(benches);
|
||||
@@ -1,43 +0,0 @@
|
||||
use criterion::{criterion_group, criterion_main, Criterion};
|
||||
use rln::circuit::{
|
||||
read_arkzkey_from_bytes_compressed, read_arkzkey_from_bytes_uncompressed, ARKZKEY_BYTES,
|
||||
ARKZKEY_BYTES_UNCOMPR,
|
||||
};
|
||||
|
||||
pub fn uncompressed_bench(c: &mut Criterion) {
|
||||
let arkzkey = ARKZKEY_BYTES_UNCOMPR.to_vec();
|
||||
let size = arkzkey.len() as f32;
|
||||
println!(
|
||||
"Size of uncompressed arkzkey: {:.2?} MB",
|
||||
size / 1024.0 / 1024.0
|
||||
);
|
||||
|
||||
c.bench_function("arkzkey::arkzkey_from_raw_uncompressed", |b| {
|
||||
b.iter(|| {
|
||||
let r = read_arkzkey_from_bytes_uncompressed(&arkzkey);
|
||||
assert_eq!(r.is_ok(), true);
|
||||
})
|
||||
});
|
||||
}
|
||||
pub fn compressed_bench(c: &mut Criterion) {
|
||||
let arkzkey = ARKZKEY_BYTES.to_vec();
|
||||
let size = arkzkey.len() as f32;
|
||||
println!(
|
||||
"Size of compressed arkzkey: {:.2?} MB",
|
||||
size / 1024.0 / 1024.0
|
||||
);
|
||||
|
||||
c.bench_function("arkzkey::arkzkey_from_raw_compressed", |b| {
|
||||
b.iter(|| {
|
||||
let r = read_arkzkey_from_bytes_compressed(&arkzkey);
|
||||
assert_eq!(r.is_ok(), true);
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
criterion_group! {
|
||||
name = benches;
|
||||
config = Criterion::default().measurement_time(std::time::Duration::from_secs(250));
|
||||
targets = uncompressed_bench, compressed_bench
|
||||
}
|
||||
criterion_main!(benches);
|
||||
@@ -1,24 +0,0 @@
|
||||
use ark_circom::read_zkey;
|
||||
use criterion::{criterion_group, criterion_main, Criterion};
|
||||
use std::io::Cursor;
|
||||
|
||||
pub fn zkey_load_benchmark(c: &mut Criterion) {
|
||||
let zkey = rln::circuit::ZKEY_BYTES.to_vec();
|
||||
let size = zkey.len() as f32;
|
||||
println!("Size of zkey: {:.2?} MB", size / 1024.0 / 1024.0);
|
||||
|
||||
c.bench_function("zkey::zkey_from_raw", |b| {
|
||||
b.iter(|| {
|
||||
let mut reader = Cursor::new(zkey.clone());
|
||||
let r = read_zkey(&mut reader);
|
||||
assert_eq!(r.is_ok(), true);
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
criterion_group! {
|
||||
name = benches;
|
||||
config = Criterion::default().measurement_time(std::time::Duration::from_secs(250));
|
||||
targets = zkey_load_benchmark
|
||||
}
|
||||
criterion_main!(benches);
|
||||
@@ -5,7 +5,7 @@ use utils::ZerokitMerkleTree;
|
||||
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();
|
||||
let leaves: Vec<Fr> = (0..4).map(Fr::from).collect();
|
||||
|
||||
c.bench_function("Pmtree::set", |b| {
|
||||
b.iter(|| {
|
||||
@@ -21,17 +21,11 @@ pub fn pmtree_benchmark(c: &mut Criterion) {
|
||||
|
||||
c.bench_function("Pmtree::override_range", |b| {
|
||||
b.iter(|| {
|
||||
tree.override_range(0, leaves.clone(), [0, 1, 2, 3])
|
||||
tree.override_range(0, leaves.clone().into_iter(), [0, 1, 2, 3].into_iter())
|
||||
.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();
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
|
||||
use rln::{
|
||||
circuit::{Fr, TEST_TREE_HEIGHT},
|
||||
circuit::{Fr, TEST_TREE_DEPTH},
|
||||
hashers::PoseidonHash,
|
||||
};
|
||||
use utils::{FullMerkleTree, OptimalMerkleTree, ZerokitMerkleTree};
|
||||
|
||||
pub fn get_leaves(n: u32) -> Vec<Fr> {
|
||||
(0..n).map(|s| Fr::from(s)).collect()
|
||||
(0..n).map(Fr::from).collect()
|
||||
}
|
||||
|
||||
pub fn optimal_merkle_tree_poseidon_benchmark(c: &mut Criterion) {
|
||||
c.bench_function("OptimalMerkleTree::<Poseidon>::full_height_gen", |b| {
|
||||
c.bench_function("OptimalMerkleTree::<Poseidon>::full_depth_gen", |b| {
|
||||
b.iter(|| {
|
||||
OptimalMerkleTree::<PoseidonHash>::default(TEST_TREE_HEIGHT).unwrap();
|
||||
OptimalMerkleTree::<PoseidonHash>::default(TEST_TREE_DEPTH).unwrap();
|
||||
})
|
||||
});
|
||||
|
||||
@@ -20,7 +20,7 @@ pub fn optimal_merkle_tree_poseidon_benchmark(c: &mut Criterion) {
|
||||
for &n in [1u32, 10, 100].iter() {
|
||||
let leaves = get_leaves(n);
|
||||
|
||||
let mut tree = OptimalMerkleTree::<PoseidonHash>::default(TEST_TREE_HEIGHT).unwrap();
|
||||
let mut tree = OptimalMerkleTree::<PoseidonHash>::default(TEST_TREE_DEPTH).unwrap();
|
||||
group.bench_function(
|
||||
BenchmarkId::new("OptimalMerkleTree::<Poseidon>::set", n),
|
||||
|b| {
|
||||
@@ -41,9 +41,9 @@ pub fn optimal_merkle_tree_poseidon_benchmark(c: &mut Criterion) {
|
||||
}
|
||||
|
||||
pub fn full_merkle_tree_poseidon_benchmark(c: &mut Criterion) {
|
||||
c.bench_function("FullMerkleTree::<Poseidon>::full_height_gen", |b| {
|
||||
c.bench_function("FullMerkleTree::<Poseidon>::full_depth_gen", |b| {
|
||||
b.iter(|| {
|
||||
FullMerkleTree::<PoseidonHash>::default(TEST_TREE_HEIGHT).unwrap();
|
||||
FullMerkleTree::<PoseidonHash>::default(TEST_TREE_DEPTH).unwrap();
|
||||
})
|
||||
});
|
||||
|
||||
@@ -51,7 +51,7 @@ pub fn full_merkle_tree_poseidon_benchmark(c: &mut Criterion) {
|
||||
for &n in [1u32, 10, 100].iter() {
|
||||
let leaves = get_leaves(n);
|
||||
|
||||
let mut tree = FullMerkleTree::<PoseidonHash>::default(TEST_TREE_HEIGHT).unwrap();
|
||||
let mut tree = FullMerkleTree::<PoseidonHash>::default(TEST_TREE_DEPTH).unwrap();
|
||||
group.bench_function(
|
||||
BenchmarkId::new("FullMerkleTree::<Poseidon>::set", n),
|
||||
|b| {
|
||||
|
||||
53
rln/convert_zkey.sh
Executable file
53
rln/convert_zkey.sh
Executable file
@@ -0,0 +1,53 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Convert zkey to arkzkey using /tmp directory
|
||||
# Usage: ./convert.sh <path_to_zkey_file>
|
||||
|
||||
set -e
|
||||
|
||||
# Check input
|
||||
if [ $# -eq 0 ]; then
|
||||
echo "Usage: $0 <path_to_zkey_file>"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
ZKEY_FILE="$1"
|
||||
|
||||
if [ ! -f "$ZKEY_FILE" ]; then
|
||||
echo "Error: File '$ZKEY_FILE' does not exist"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Get absolute path before changing directories
|
||||
ZKEY_ABSOLUTE_PATH=$(realpath "$ZKEY_FILE")
|
||||
|
||||
# Create temp directory in /tmp
|
||||
TEMP_DIR="/tmp/ark-zkey-$$"
|
||||
echo "Using temp directory: $TEMP_DIR"
|
||||
|
||||
# Cleanup function
|
||||
cleanup() {
|
||||
echo "Cleaning up temp directory: $TEMP_DIR"
|
||||
rm -rf "$TEMP_DIR"
|
||||
}
|
||||
|
||||
# Setup cleanup trap
|
||||
trap cleanup EXIT
|
||||
|
||||
# Create temp directory and clone ark-zkey
|
||||
mkdir -p "$TEMP_DIR"
|
||||
cd "$TEMP_DIR"
|
||||
git clone https://github.com/seemenkina/ark-zkey.git
|
||||
cd ark-zkey
|
||||
cargo build
|
||||
|
||||
# Convert
|
||||
cargo run --bin arkzkey-util "$ZKEY_ABSOLUTE_PATH"
|
||||
|
||||
# Check if arkzkey file was created (tool creates it in same directory as input)
|
||||
ARKZKEY_FILE="${ZKEY_ABSOLUTE_PATH%.zkey}.arkzkey"
|
||||
|
||||
if [ ! -f "$ARKZKEY_FILE" ]; then
|
||||
echo "Could not find generated .arkzkey file at $ARKZKEY_FILE"
|
||||
exit 1
|
||||
fi
|
||||
BIN
rln/resources/tree_depth_20/graph.bin
Normal file
BIN
rln/resources/tree_depth_20/graph.bin
Normal file
Binary file not shown.
Binary file not shown.
BIN
rln/resources/tree_depth_30/graph.bin
Normal file
BIN
rln/resources/tree_depth_30/graph.bin
Normal file
Binary file not shown.
BIN
rln/resources/tree_depth_30/rln.wasm
Normal file
BIN
rln/resources/tree_depth_30/rln.wasm
Normal file
Binary file not shown.
BIN
rln/resources/tree_depth_30/rln_final.arkzkey
Normal file
BIN
rln/resources/tree_depth_30/rln_final.arkzkey
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,225 +0,0 @@
|
||||
// This crate provides interfaces for the zero-knowledge circuit and keys
|
||||
|
||||
use ark_bn254::{
|
||||
Bn254, Fq as ArkFq, Fq2 as ArkFq2, Fr as ArkFr, G1Affine as ArkG1Affine,
|
||||
G1Projective as ArkG1Projective, G2Affine as ArkG2Affine, G2Projective as ArkG2Projective,
|
||||
};
|
||||
use ark_groth16::{ProvingKey, VerifyingKey};
|
||||
use ark_relations::r1cs::ConstraintMatrices;
|
||||
use ark_serialize::CanonicalDeserialize;
|
||||
use cfg_if::cfg_if;
|
||||
use color_eyre::{Report, Result};
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use {
|
||||
ark_circom::WitnessCalculator,
|
||||
lazy_static::lazy_static,
|
||||
std::sync::{Arc, Mutex},
|
||||
wasmer::{Module, Store},
|
||||
};
|
||||
|
||||
#[cfg(feature = "arkzkey")]
|
||||
use {
|
||||
ark_zkey::{read_arkzkey_from_bytes, SerializableConstraintMatrices, SerializableProvingKey},
|
||||
color_eyre::eyre::WrapErr,
|
||||
};
|
||||
|
||||
#[cfg(not(feature = "arkzkey"))]
|
||||
use {ark_circom::read_zkey, std::io::Cursor};
|
||||
|
||||
#[cfg(feature = "arkzkey")]
|
||||
pub const ARKZKEY_BYTES: &[u8] = include_bytes!("../resources/tree_height_20/rln_final.arkzkey");
|
||||
#[cfg(feature = "arkzkey")]
|
||||
pub const ARKZKEY_BYTES_UNCOMPR: &[u8] =
|
||||
include_bytes!("../resources/tree_height_20/rln_final_uncompr.arkzkey");
|
||||
|
||||
pub const ZKEY_BYTES: &[u8] = include_bytes!("../resources/tree_height_20/rln_final.zkey");
|
||||
pub const VK_BYTES: &[u8] = include_bytes!("../resources/tree_height_20/verification_key.arkvkey");
|
||||
const WASM_BYTES: &[u8] = include_bytes!("../resources/tree_height_20/rln.wasm");
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
lazy_static! {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
static ref ZKEY: (ProvingKey<Curve>, ConstraintMatrices<Fr>) = {
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "arkzkey")] {
|
||||
read_arkzkey_from_bytes_uncompressed(ARKZKEY_BYTES_UNCOMPR).expect("Failed to read arkzkey")
|
||||
} else {
|
||||
let mut reader = Cursor::new(ZKEY_BYTES);
|
||||
read_zkey(&mut reader).expect("Failed to read zkey")
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
static ref VK: VerifyingKey<Curve> = vk_from_ark_serialized(VK_BYTES).expect("Failed to read vk");
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
static ref WITNESS_CALCULATOR: Arc<Mutex<WitnessCalculator>> = {
|
||||
circom_from_raw(WASM_BYTES).expect("Failed to create witness calculator")
|
||||
};
|
||||
}
|
||||
|
||||
pub const TEST_TREE_HEIGHT: usize = 20;
|
||||
|
||||
// The following types define the pairing friendly elliptic curve, the underlying finite fields and groups default to this module
|
||||
// Note that proofs are serialized assuming Fr to be 4x8 = 32 bytes in size. Hence, changing to a curve with different encoding will make proof verification to fail
|
||||
pub type Curve = Bn254;
|
||||
pub type Fr = ArkFr;
|
||||
pub type Fq = ArkFq;
|
||||
pub type Fq2 = ArkFq2;
|
||||
pub type G1Affine = ArkG1Affine;
|
||||
pub type G1Projective = ArkG1Projective;
|
||||
pub type G2Affine = ArkG2Affine;
|
||||
pub type G2Projective = ArkG2Projective;
|
||||
|
||||
// Loads the proving key using a bytes vector
|
||||
pub fn zkey_from_raw(zkey_data: &[u8]) -> Result<(ProvingKey<Curve>, ConstraintMatrices<Fr>)> {
|
||||
if zkey_data.is_empty() {
|
||||
return Err(Report::msg("No proving key found!"));
|
||||
}
|
||||
|
||||
let proving_key_and_matrices = match () {
|
||||
#[cfg(feature = "arkzkey")]
|
||||
() => read_arkzkey_from_bytes(zkey_data)?,
|
||||
#[cfg(not(feature = "arkzkey"))]
|
||||
() => {
|
||||
let mut reader = Cursor::new(zkey_data);
|
||||
read_zkey(&mut reader)?
|
||||
}
|
||||
};
|
||||
|
||||
Ok(proving_key_and_matrices)
|
||||
}
|
||||
|
||||
// Loads the proving key
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub fn zkey_from_folder() -> &'static (ProvingKey<Curve>, ConstraintMatrices<Fr>) {
|
||||
&ZKEY
|
||||
}
|
||||
|
||||
// Loads the verification key from a bytes vector
|
||||
pub fn vk_from_raw(vk_data: &[u8], zkey_data: &[u8]) -> Result<VerifyingKey<Curve>> {
|
||||
if !vk_data.is_empty() {
|
||||
return vk_from_ark_serialized(vk_data);
|
||||
}
|
||||
|
||||
if !zkey_data.is_empty() {
|
||||
let (proving_key, _matrices) = zkey_from_raw(zkey_data)?;
|
||||
return Ok(proving_key.vk);
|
||||
}
|
||||
|
||||
Err(Report::msg("No proving/verification key found!"))
|
||||
}
|
||||
|
||||
// Loads the verification key
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub fn vk_from_folder() -> &'static VerifyingKey<Curve> {
|
||||
&VK
|
||||
}
|
||||
|
||||
// Initializes the witness calculator using a bytes vector
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub fn circom_from_raw(wasm_buffer: &[u8]) -> Result<Arc<Mutex<WitnessCalculator>>> {
|
||||
let module = Module::new(&Store::default(), wasm_buffer)?;
|
||||
let result = WitnessCalculator::from_module(module)?;
|
||||
Ok(Arc::new(Mutex::new(result)))
|
||||
}
|
||||
|
||||
// Initializes the witness calculator
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub fn circom_from_folder() -> &'static Arc<Mutex<WitnessCalculator>> {
|
||||
&WITNESS_CALCULATOR
|
||||
}
|
||||
|
||||
// Computes the verification key from a bytes vector containing pre-processed ark-serialized verification key
|
||||
// uncompressed, unchecked
|
||||
pub fn vk_from_ark_serialized(data: &[u8]) -> Result<VerifyingKey<Curve>> {
|
||||
let vk = VerifyingKey::<Curve>::deserialize_uncompressed_unchecked(data)?;
|
||||
Ok(vk)
|
||||
}
|
||||
|
||||
// Checks verification key to be correct with respect to proving key
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub fn check_vk_from_zkey(verifying_key: VerifyingKey<Curve>) -> Result<()> {
|
||||
let (proving_key, _matrices) = zkey_from_folder();
|
||||
if proving_key.vk == verifying_key {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Report::msg("verifying_keys are not equal"))
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////
|
||||
// Functions from [arkz-key](https://github.com/zkmopro/ark-zkey/blob/main/src/lib.rs#L106)
|
||||
// without print and allow to choose between compressed and uncompressed arkzkey
|
||||
////////////////////////////////////////////////////////
|
||||
#[cfg(feature = "arkzkey")]
|
||||
pub fn read_arkzkey_from_bytes_uncompressed(
|
||||
arkzkey_data: &[u8],
|
||||
) -> Result<(ProvingKey<Curve>, ConstraintMatrices<Fr>)> {
|
||||
if arkzkey_data.is_empty() {
|
||||
return Err(Report::msg("No proving key found!"));
|
||||
}
|
||||
|
||||
let mut cursor = std::io::Cursor::new(arkzkey_data);
|
||||
|
||||
let serialized_proving_key =
|
||||
SerializableProvingKey::deserialize_uncompressed_unchecked(&mut cursor)
|
||||
.wrap_err("Failed to deserialize proving key")?;
|
||||
|
||||
let serialized_constraint_matrices =
|
||||
SerializableConstraintMatrices::deserialize_uncompressed_unchecked(&mut cursor)
|
||||
.wrap_err("Failed to deserialize constraint matrices")?;
|
||||
|
||||
// Get on right form for API
|
||||
let proving_key: ProvingKey<Bn254> = serialized_proving_key.0;
|
||||
let constraint_matrices: ConstraintMatrices<ark_bn254::Fr> = ConstraintMatrices {
|
||||
num_instance_variables: serialized_constraint_matrices.num_instance_variables,
|
||||
num_witness_variables: serialized_constraint_matrices.num_witness_variables,
|
||||
num_constraints: serialized_constraint_matrices.num_constraints,
|
||||
a_num_non_zero: serialized_constraint_matrices.a_num_non_zero,
|
||||
b_num_non_zero: serialized_constraint_matrices.b_num_non_zero,
|
||||
c_num_non_zero: serialized_constraint_matrices.c_num_non_zero,
|
||||
a: serialized_constraint_matrices.a.data,
|
||||
b: serialized_constraint_matrices.b.data,
|
||||
c: serialized_constraint_matrices.c.data,
|
||||
};
|
||||
|
||||
Ok((proving_key, constraint_matrices))
|
||||
}
|
||||
|
||||
#[cfg(feature = "arkzkey")]
|
||||
pub fn read_arkzkey_from_bytes_compressed(
|
||||
arkzkey_data: &[u8],
|
||||
) -> Result<(ProvingKey<Curve>, ConstraintMatrices<Fr>)> {
|
||||
if arkzkey_data.is_empty() {
|
||||
return Err(Report::msg("No proving key found!"));
|
||||
}
|
||||
|
||||
let mut cursor = std::io::Cursor::new(arkzkey_data);
|
||||
|
||||
let serialized_proving_key =
|
||||
SerializableProvingKey::deserialize_compressed_unchecked(&mut cursor)
|
||||
.wrap_err("Failed to deserialize proving key")?;
|
||||
|
||||
let serialized_constraint_matrices =
|
||||
SerializableConstraintMatrices::deserialize_compressed_unchecked(&mut cursor)
|
||||
.wrap_err("Failed to deserialize constraint matrices")?;
|
||||
|
||||
// Get on right form for API
|
||||
let proving_key: ProvingKey<Bn254> = serialized_proving_key.0;
|
||||
let constraint_matrices: ConstraintMatrices<ark_bn254::Fr> = ConstraintMatrices {
|
||||
num_instance_variables: serialized_constraint_matrices.num_instance_variables,
|
||||
num_witness_variables: serialized_constraint_matrices.num_witness_variables,
|
||||
num_constraints: serialized_constraint_matrices.num_constraints,
|
||||
a_num_non_zero: serialized_constraint_matrices.a_num_non_zero,
|
||||
b_num_non_zero: serialized_constraint_matrices.b_num_non_zero,
|
||||
c_num_non_zero: serialized_constraint_matrices.c_num_non_zero,
|
||||
a: serialized_constraint_matrices.a.data,
|
||||
b: serialized_constraint_matrices.b.data,
|
||||
c: serialized_constraint_matrices.c.data,
|
||||
};
|
||||
|
||||
Ok((proving_key, constraint_matrices))
|
||||
}
|
||||
7
rln/src/circuit/error.rs
Normal file
7
rln/src/circuit/error.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum ZKeyReadError {
|
||||
#[error("No proving key found!")]
|
||||
EmptyBytes,
|
||||
#[error("{0}")]
|
||||
SerializationError(#[from] ark_serialize::SerializationError),
|
||||
}
|
||||
94
rln/src/circuit/iden3calc.rs
Normal file
94
rln/src/circuit/iden3calc.rs
Normal file
@@ -0,0 +1,94 @@
|
||||
// This file is based on the code by iden3. Its preimage can be found here:
|
||||
// https://github.com/iden3/circom-witnesscalc/blob/5cb365b6e4d9052ecc69d4567fcf5bc061c20e94/src/lib.rs
|
||||
|
||||
pub mod graph;
|
||||
pub mod proto;
|
||||
pub mod storage;
|
||||
|
||||
use ruint::aliases::U256;
|
||||
use std::collections::HashMap;
|
||||
use storage::deserialize_witnesscalc_graph;
|
||||
use zeroize::zeroize_flat_type;
|
||||
|
||||
use crate::circuit::iden3calc::graph::fr_to_u256;
|
||||
use crate::circuit::Fr;
|
||||
use crate::utils::FrOrSecret;
|
||||
use graph::Node;
|
||||
|
||||
pub type InputSignalsInfo = HashMap<String, (usize, usize)>;
|
||||
|
||||
pub fn calc_witness<I: IntoIterator<Item = (String, Vec<FrOrSecret>)>>(
|
||||
inputs: I,
|
||||
graph_data: &[u8],
|
||||
) -> Vec<Fr> {
|
||||
let mut inputs: HashMap<String, Vec<U256>> = inputs
|
||||
.into_iter()
|
||||
.map(|(key, value)| {
|
||||
(
|
||||
key,
|
||||
value
|
||||
.iter()
|
||||
.map(|f_| match f_ {
|
||||
FrOrSecret::IdSecret(s) => s.to_u256(),
|
||||
FrOrSecret::Fr(f) => fr_to_u256(f),
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
|
||||
let (nodes, signals, input_mapping): (Vec<Node>, Vec<usize>, InputSignalsInfo) =
|
||||
deserialize_witnesscalc_graph(std::io::Cursor::new(graph_data)).unwrap();
|
||||
|
||||
let mut inputs_buffer = get_inputs_buffer(get_inputs_size(&nodes));
|
||||
populate_inputs(&inputs, &input_mapping, &mut inputs_buffer);
|
||||
if let Some(v) = inputs.get_mut("identitySecret") {
|
||||
// ~== v[0] = U256::ZERO;
|
||||
unsafe { zeroize_flat_type(v) };
|
||||
}
|
||||
let res = graph::evaluate(&nodes, inputs_buffer.as_slice(), &signals);
|
||||
inputs_buffer.iter_mut().for_each(|i| {
|
||||
unsafe { zeroize_flat_type(i) };
|
||||
});
|
||||
res
|
||||
}
|
||||
|
||||
fn get_inputs_size(nodes: &[Node]) -> usize {
|
||||
let mut start = false;
|
||||
let mut max_index = 0usize;
|
||||
for &node in nodes.iter() {
|
||||
if let Node::Input(i) = node {
|
||||
if i > max_index {
|
||||
max_index = i;
|
||||
}
|
||||
start = true
|
||||
} else if start {
|
||||
break;
|
||||
}
|
||||
}
|
||||
max_index + 1
|
||||
}
|
||||
|
||||
fn populate_inputs(
|
||||
input_list: &HashMap<String, Vec<U256>>,
|
||||
inputs_info: &InputSignalsInfo,
|
||||
input_buffer: &mut [U256],
|
||||
) {
|
||||
for (key, value) in input_list {
|
||||
let (offset, len) = inputs_info[key];
|
||||
if len != value.len() {
|
||||
panic!("Invalid input length for {key}");
|
||||
}
|
||||
|
||||
for (i, v) in value.iter().enumerate() {
|
||||
input_buffer[offset + i] = *v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Allocates inputs vec with position 0 set to 1
|
||||
fn get_inputs_buffer(size: usize) -> Vec<U256> {
|
||||
let mut inputs = vec![U256::ZERO; size];
|
||||
inputs[0] = U256::from(1);
|
||||
inputs
|
||||
}
|
||||
957
rln/src/circuit/iden3calc/graph.rs
Normal file
957
rln/src/circuit/iden3calc/graph.rs
Normal file
@@ -0,0 +1,957 @@
|
||||
// This file is based on the code by iden3. Its preimage can be found here:
|
||||
// https://github.com/iden3/circom-witnesscalc/blob/5cb365b6e4d9052ecc69d4567fcf5bc061c20e94/src/graph.rs
|
||||
|
||||
use ark_ff::{BigInt, BigInteger, One, PrimeField, Zero};
|
||||
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize, Compress, Validate};
|
||||
use rand::Rng;
|
||||
use ruint::{aliases::U256, uint};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{
|
||||
cmp::Ordering,
|
||||
collections::HashMap,
|
||||
error::Error,
|
||||
ops::{Deref, Shl, Shr},
|
||||
};
|
||||
|
||||
use crate::circuit::iden3calc::proto;
|
||||
use crate::circuit::Fr;
|
||||
|
||||
pub const M: U256 =
|
||||
uint!(21888242871839275222246405745257275088548364400416034343698204186575808495617_U256);
|
||||
|
||||
fn ark_se<S, A: CanonicalSerialize>(a: &A, s: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
let mut bytes = vec![];
|
||||
a.serialize_with_mode(&mut bytes, Compress::Yes)
|
||||
.map_err(serde::ser::Error::custom)?;
|
||||
s.serialize_bytes(&bytes)
|
||||
}
|
||||
|
||||
fn ark_de<'de, D, A: CanonicalDeserialize>(data: D) -> Result<A, D::Error>
|
||||
where
|
||||
D: serde::de::Deserializer<'de>,
|
||||
{
|
||||
let s: Vec<u8> = serde::de::Deserialize::deserialize(data)?;
|
||||
let a = A::deserialize_with_mode(s.as_slice(), Compress::Yes, Validate::Yes);
|
||||
a.map_err(serde::de::Error::custom)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn fr_to_u256(x: &Fr) -> U256 {
|
||||
U256::from_limbs(x.into_bigint().0)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn u256_to_fr(x: &U256) -> Fr {
|
||||
Fr::from_bigint(BigInt::new(x.into_limbs())).expect("Failed to convert U256 to Fr")
|
||||
}
|
||||
|
||||
#[derive(Hash, PartialEq, Eq, Debug, Clone, Copy, Serialize, Deserialize)]
|
||||
pub enum Operation {
|
||||
Mul,
|
||||
Div,
|
||||
Add,
|
||||
Sub,
|
||||
Pow,
|
||||
Idiv,
|
||||
Mod,
|
||||
Eq,
|
||||
Neq,
|
||||
Lt,
|
||||
Gt,
|
||||
Leq,
|
||||
Geq,
|
||||
Land,
|
||||
Lor,
|
||||
Shl,
|
||||
Shr,
|
||||
Bor,
|
||||
Band,
|
||||
Bxor,
|
||||
}
|
||||
|
||||
impl Operation {
|
||||
// TODO: rewrite to &U256 type
|
||||
pub fn eval(&self, a: U256, b: U256) -> U256 {
|
||||
use Operation::*;
|
||||
match self {
|
||||
Mul => a.mul_mod(b, M),
|
||||
Div => {
|
||||
if b == U256::ZERO {
|
||||
// as we are simulating a circuit execution with signals
|
||||
// values all equal to 0, just return 0 here in case of
|
||||
// division by zero
|
||||
U256::ZERO
|
||||
} else {
|
||||
a.mul_mod(b.inv_mod(M).unwrap(), M)
|
||||
}
|
||||
}
|
||||
Add => a.add_mod(b, M),
|
||||
Sub => a.add_mod(M - b, M),
|
||||
Pow => a.pow_mod(b, M),
|
||||
Mod => a.div_rem(b).1,
|
||||
Eq => U256::from(a == b),
|
||||
Neq => U256::from(a != b),
|
||||
Lt => u_lt(&a, &b),
|
||||
Gt => u_gt(&a, &b),
|
||||
Leq => u_lte(&a, &b),
|
||||
Geq => u_gte(&a, &b),
|
||||
Land => U256::from(a != U256::ZERO && b != U256::ZERO),
|
||||
Lor => U256::from(a != U256::ZERO || b != U256::ZERO),
|
||||
Shl => compute_shl_uint(a, b),
|
||||
Shr => compute_shr_uint(a, b),
|
||||
// TODO test with conner case when it is possible to get the number
|
||||
// bigger then modulus
|
||||
Bor => a.bitor(b),
|
||||
Band => a.bitand(b),
|
||||
// TODO test with conner case when it is possible to get the number
|
||||
// bigger then modulus
|
||||
Bxor => a.bitxor(b),
|
||||
Idiv => a / b,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn eval_fr(&self, a: Fr, b: Fr) -> Fr {
|
||||
use Operation::*;
|
||||
match self {
|
||||
Mul => a * b,
|
||||
// We always should return something on the circuit execution.
|
||||
// So in case of division by 0 we would return 0. And the proof
|
||||
// should be invalid in the end.
|
||||
Div => {
|
||||
if b.is_zero() {
|
||||
Fr::zero()
|
||||
} else {
|
||||
a / b
|
||||
}
|
||||
}
|
||||
Add => a + b,
|
||||
Sub => a - b,
|
||||
Idiv => {
|
||||
if b.is_zero() {
|
||||
Fr::zero()
|
||||
} else {
|
||||
let a_u256 = fr_to_u256(&a);
|
||||
let b_u256 = fr_to_u256(&b);
|
||||
u256_to_fr(&(a_u256 / b_u256))
|
||||
}
|
||||
}
|
||||
Mod => {
|
||||
if b.is_zero() {
|
||||
Fr::zero()
|
||||
} else {
|
||||
let a_u256 = fr_to_u256(&a);
|
||||
let b_u256 = fr_to_u256(&b);
|
||||
u256_to_fr(&(a_u256 % b_u256))
|
||||
}
|
||||
}
|
||||
Eq => match a.cmp(&b) {
|
||||
Ordering::Equal => Fr::one(),
|
||||
_ => Fr::zero(),
|
||||
},
|
||||
Neq => match a.cmp(&b) {
|
||||
Ordering::Equal => Fr::zero(),
|
||||
_ => Fr::one(),
|
||||
},
|
||||
Lt => u256_to_fr(&u_lt(&fr_to_u256(&a), &fr_to_u256(&b))),
|
||||
Gt => u256_to_fr(&u_gt(&fr_to_u256(&a), &fr_to_u256(&b))),
|
||||
Leq => u256_to_fr(&u_lte(&fr_to_u256(&a), &fr_to_u256(&b))),
|
||||
Geq => u256_to_fr(&u_gte(&fr_to_u256(&a), &fr_to_u256(&b))),
|
||||
Land => {
|
||||
if a.is_zero() || b.is_zero() {
|
||||
Fr::zero()
|
||||
} else {
|
||||
Fr::one()
|
||||
}
|
||||
}
|
||||
Lor => {
|
||||
if a.is_zero() && b.is_zero() {
|
||||
Fr::zero()
|
||||
} else {
|
||||
Fr::one()
|
||||
}
|
||||
}
|
||||
Shl => shl(a, b),
|
||||
Shr => shr(a, b),
|
||||
Bor => bit_or(a, b),
|
||||
Band => bit_and(a, b),
|
||||
Bxor => bit_xor(a, b),
|
||||
// TODO implement other operators
|
||||
_ => unimplemented!("operator {:?} not implemented for Montgomery", self),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&Operation> for proto::DuoOp {
|
||||
fn from(v: &Operation) -> Self {
|
||||
match v {
|
||||
Operation::Mul => proto::DuoOp::Mul,
|
||||
Operation::Div => proto::DuoOp::Div,
|
||||
Operation::Add => proto::DuoOp::Add,
|
||||
Operation::Sub => proto::DuoOp::Sub,
|
||||
Operation::Pow => proto::DuoOp::Pow,
|
||||
Operation::Idiv => proto::DuoOp::Idiv,
|
||||
Operation::Mod => proto::DuoOp::Mod,
|
||||
Operation::Eq => proto::DuoOp::Eq,
|
||||
Operation::Neq => proto::DuoOp::Neq,
|
||||
Operation::Lt => proto::DuoOp::Lt,
|
||||
Operation::Gt => proto::DuoOp::Gt,
|
||||
Operation::Leq => proto::DuoOp::Leq,
|
||||
Operation::Geq => proto::DuoOp::Geq,
|
||||
Operation::Land => proto::DuoOp::Land,
|
||||
Operation::Lor => proto::DuoOp::Lor,
|
||||
Operation::Shl => proto::DuoOp::Shl,
|
||||
Operation::Shr => proto::DuoOp::Shr,
|
||||
Operation::Bor => proto::DuoOp::Bor,
|
||||
Operation::Band => proto::DuoOp::Band,
|
||||
Operation::Bxor => proto::DuoOp::Bxor,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Hash, PartialEq, Eq, Debug, Clone, Copy, Serialize, Deserialize)]
|
||||
pub enum UnoOperation {
|
||||
Neg,
|
||||
Id, // identity - just return self
|
||||
}
|
||||
|
||||
impl UnoOperation {
|
||||
pub fn eval(&self, a: U256) -> U256 {
|
||||
match self {
|
||||
UnoOperation::Neg => {
|
||||
if a == U256::ZERO {
|
||||
U256::ZERO
|
||||
} else {
|
||||
M - a
|
||||
}
|
||||
}
|
||||
UnoOperation::Id => a,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn eval_fr(&self, a: Fr) -> Fr {
|
||||
match self {
|
||||
UnoOperation::Neg => {
|
||||
if a.is_zero() {
|
||||
Fr::zero()
|
||||
} else {
|
||||
let mut x = Fr::MODULUS;
|
||||
x.sub_with_borrow(&a.into_bigint());
|
||||
Fr::from_bigint(x).unwrap()
|
||||
}
|
||||
}
|
||||
_ => unimplemented!("uno operator {:?} not implemented for Montgomery", self),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&UnoOperation> for proto::UnoOp {
|
||||
fn from(v: &UnoOperation) -> Self {
|
||||
match v {
|
||||
UnoOperation::Neg => proto::UnoOp::Neg,
|
||||
UnoOperation::Id => proto::UnoOp::Id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Hash, PartialEq, Eq, Debug, Clone, Copy, Serialize, Deserialize)]
|
||||
pub enum TresOperation {
|
||||
TernCond,
|
||||
}
|
||||
|
||||
impl TresOperation {
|
||||
pub fn eval(&self, a: U256, b: U256, c: U256) -> U256 {
|
||||
match self {
|
||||
TresOperation::TernCond => {
|
||||
if a == U256::ZERO {
|
||||
c
|
||||
} else {
|
||||
b
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn eval_fr(&self, a: Fr, b: Fr, c: Fr) -> Fr {
|
||||
match self {
|
||||
TresOperation::TernCond => {
|
||||
if a.is_zero() {
|
||||
c
|
||||
} else {
|
||||
b
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&TresOperation> for proto::TresOp {
|
||||
fn from(v: &TresOperation) -> Self {
|
||||
match v {
|
||||
TresOperation::TernCond => proto::TresOp::TernCond,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum Node {
|
||||
Input(usize),
|
||||
Constant(U256),
|
||||
#[serde(serialize_with = "ark_se", deserialize_with = "ark_de")]
|
||||
MontConstant(Fr),
|
||||
UnoOp(UnoOperation, usize),
|
||||
Op(Operation, usize, usize),
|
||||
TresOp(TresOperation, usize, usize, usize),
|
||||
}
|
||||
|
||||
// TODO remove pub from Vec<Node>
|
||||
#[derive(Default)]
|
||||
pub struct Nodes(pub Vec<Node>);
|
||||
|
||||
impl Nodes {
|
||||
pub fn new() -> Self {
|
||||
Nodes(Vec::new())
|
||||
}
|
||||
|
||||
pub fn to_const(&self, idx: NodeIdx) -> Result<U256, NodeConstErr> {
|
||||
let me = self.0.get(idx.0).ok_or(NodeConstErr::EmptyNode(idx))?;
|
||||
match me {
|
||||
Node::Constant(v) => Ok(*v),
|
||||
Node::UnoOp(op, a) => Ok(op.eval(self.to_const(NodeIdx(*a))?)),
|
||||
Node::Op(op, a, b) => {
|
||||
Ok(op.eval(self.to_const(NodeIdx(*a))?, self.to_const(NodeIdx(*b))?))
|
||||
}
|
||||
Node::TresOp(op, a, b, c) => Ok(op.eval(
|
||||
self.to_const(NodeIdx(*a))?,
|
||||
self.to_const(NodeIdx(*b))?,
|
||||
self.to_const(NodeIdx(*c))?,
|
||||
)),
|
||||
Node::Input(_) => Err(NodeConstErr::InputSignal),
|
||||
Node::MontConstant(_) => {
|
||||
panic!("MontConstant should not be used here")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push(&mut self, n: Node) -> NodeIdx {
|
||||
self.0.push(n);
|
||||
NodeIdx(self.0.len() - 1)
|
||||
}
|
||||
|
||||
pub fn get(&self, idx: NodeIdx) -> Option<&Node> {
|
||||
self.0.get(idx.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for Nodes {
|
||||
type Target = Vec<Node>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub struct NodeIdx(pub usize);
|
||||
|
||||
impl From<usize> for NodeIdx {
|
||||
fn from(v: usize) -> Self {
|
||||
NodeIdx(v)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum NodeConstErr {
|
||||
EmptyNode(NodeIdx),
|
||||
InputSignal,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for NodeConstErr {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
NodeConstErr::EmptyNode(idx) => {
|
||||
write!(f, "empty node at index {}", idx.0)
|
||||
}
|
||||
NodeConstErr::InputSignal => {
|
||||
write!(f, "input signal is not a constant")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for NodeConstErr {}
|
||||
|
||||
fn compute_shl_uint(a: U256, b: U256) -> U256 {
|
||||
debug_assert!(b.lt(&U256::from(256)));
|
||||
let ls_limb = b.as_limbs()[0];
|
||||
a.shl(ls_limb as usize)
|
||||
}
|
||||
|
||||
fn compute_shr_uint(a: U256, b: U256) -> U256 {
|
||||
debug_assert!(b.lt(&U256::from(256)));
|
||||
let ls_limb = b.as_limbs()[0];
|
||||
a.shr(ls_limb as usize)
|
||||
}
|
||||
|
||||
/// All references must be backwards.
|
||||
fn assert_valid(nodes: &[Node]) {
|
||||
for (i, &node) in nodes.iter().enumerate() {
|
||||
if let Node::Op(_, a, b) = node {
|
||||
assert!(a < i);
|
||||
assert!(b < i);
|
||||
} else if let Node::UnoOp(_, a) = node {
|
||||
assert!(a < i);
|
||||
} else if let Node::TresOp(_, a, b, c) = node {
|
||||
assert!(a < i);
|
||||
assert!(b < i);
|
||||
assert!(c < i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn optimize(nodes: &mut Vec<Node>, outputs: &mut [usize]) {
|
||||
tree_shake(nodes, outputs);
|
||||
propagate(nodes);
|
||||
value_numbering(nodes, outputs);
|
||||
constants(nodes);
|
||||
tree_shake(nodes, outputs);
|
||||
montgomery_form(nodes);
|
||||
}
|
||||
|
||||
pub fn evaluate(nodes: &[Node], inputs: &[U256], outputs: &[usize]) -> Vec<Fr> {
|
||||
// assert_valid(nodes);
|
||||
|
||||
// Evaluate the graph.
|
||||
let mut values = Vec::with_capacity(nodes.len());
|
||||
for &node in nodes.iter() {
|
||||
let value = match node {
|
||||
Node::Constant(c) => u256_to_fr(&c),
|
||||
Node::MontConstant(c) => c,
|
||||
Node::Input(i) => u256_to_fr(&inputs[i]),
|
||||
Node::Op(op, a, b) => op.eval_fr(values[a], values[b]),
|
||||
Node::UnoOp(op, a) => op.eval_fr(values[a]),
|
||||
Node::TresOp(op, a, b, c) => op.eval_fr(values[a], values[b], values[c]),
|
||||
};
|
||||
values.push(value);
|
||||
}
|
||||
|
||||
// Convert from Montgomery form and return the outputs.
|
||||
let mut out = vec![Fr::from(0); outputs.len()];
|
||||
for i in 0..outputs.len() {
|
||||
out[i] = values[outputs[i]];
|
||||
}
|
||||
|
||||
out
|
||||
}
|
||||
|
||||
/// Constant propagation
|
||||
pub fn propagate(nodes: &mut [Node]) {
|
||||
assert_valid(nodes);
|
||||
for i in 0..nodes.len() {
|
||||
if let Node::Op(op, a, b) = nodes[i] {
|
||||
if let (Node::Constant(va), Node::Constant(vb)) = (nodes[a], nodes[b]) {
|
||||
nodes[i] = Node::Constant(op.eval(va, vb));
|
||||
} else if a == b {
|
||||
// Not constant but equal
|
||||
use Operation::*;
|
||||
if let Some(c) = match op {
|
||||
Eq | Leq | Geq => Some(true),
|
||||
Neq | Lt | Gt => Some(false),
|
||||
_ => None,
|
||||
} {
|
||||
nodes[i] = Node::Constant(U256::from(c));
|
||||
}
|
||||
}
|
||||
} else if let Node::UnoOp(op, a) = nodes[i] {
|
||||
if let Node::Constant(va) = nodes[a] {
|
||||
nodes[i] = Node::Constant(op.eval(va));
|
||||
}
|
||||
} else if let Node::TresOp(op, a, b, c) = nodes[i] {
|
||||
if let (Node::Constant(va), Node::Constant(vb), Node::Constant(vc)) =
|
||||
(nodes[a], nodes[b], nodes[c])
|
||||
{
|
||||
nodes[i] = Node::Constant(op.eval(va, vb, vc));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove unused nodes
|
||||
pub fn tree_shake(nodes: &mut Vec<Node>, outputs: &mut [usize]) {
|
||||
assert_valid(nodes);
|
||||
|
||||
// Mark all nodes that are used.
|
||||
let mut used = vec![false; nodes.len()];
|
||||
for &i in outputs.iter() {
|
||||
used[i] = true;
|
||||
}
|
||||
|
||||
// Work backwards from end as all references are backwards.
|
||||
for i in (0..nodes.len()).rev() {
|
||||
if used[i] {
|
||||
if let Node::Op(_, a, b) = nodes[i] {
|
||||
used[a] = true;
|
||||
used[b] = true;
|
||||
}
|
||||
if let Node::UnoOp(_, a) = nodes[i] {
|
||||
used[a] = true;
|
||||
}
|
||||
if let Node::TresOp(_, a, b, c) = nodes[i] {
|
||||
used[a] = true;
|
||||
used[b] = true;
|
||||
used[c] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Remove unused nodes
|
||||
let n = nodes.len();
|
||||
let mut retain = used.iter();
|
||||
nodes.retain(|_| *retain.next().unwrap());
|
||||
|
||||
// Renumber references.
|
||||
let mut renumber = vec![None; n];
|
||||
let mut index = 0;
|
||||
for (i, &used) in used.iter().enumerate() {
|
||||
if used {
|
||||
renumber[i] = Some(index);
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
assert_eq!(index, nodes.len());
|
||||
for (&used, renumber) in used.iter().zip(renumber.iter()) {
|
||||
assert_eq!(used, renumber.is_some());
|
||||
}
|
||||
|
||||
// Renumber references.
|
||||
for node in nodes.iter_mut() {
|
||||
if let Node::Op(_, a, b) = node {
|
||||
*a = renumber[*a].unwrap();
|
||||
*b = renumber[*b].unwrap();
|
||||
}
|
||||
if let Node::UnoOp(_, a) = node {
|
||||
*a = renumber[*a].unwrap();
|
||||
}
|
||||
if let Node::TresOp(_, a, b, c) = node {
|
||||
*a = renumber[*a].unwrap();
|
||||
*b = renumber[*b].unwrap();
|
||||
*c = renumber[*c].unwrap();
|
||||
}
|
||||
}
|
||||
for output in outputs.iter_mut() {
|
||||
*output = renumber[*output].unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
/// Randomly evaluate the graph
|
||||
fn random_eval(nodes: &mut [Node]) -> Vec<U256> {
|
||||
let mut rng = rand::thread_rng();
|
||||
let mut values = Vec::with_capacity(nodes.len());
|
||||
let mut inputs = HashMap::new();
|
||||
let mut prfs = HashMap::new();
|
||||
let mut prfs_uno = HashMap::new();
|
||||
let mut prfs_tres = HashMap::new();
|
||||
for node in nodes.iter() {
|
||||
use Operation::*;
|
||||
let value = match node {
|
||||
// Constants evaluate to themselves
|
||||
Node::Constant(c) => *c,
|
||||
|
||||
Node::MontConstant(_) => unimplemented!("should not be used"),
|
||||
|
||||
// Algebraic Ops are evaluated directly
|
||||
// Since the field is large, by Swartz-Zippel if
|
||||
// two values are the same then they are likely algebraically equal.
|
||||
Node::Op(op @ (Add | Sub | Mul), a, b) => op.eval(values[*a], values[*b]),
|
||||
|
||||
// Input and non-algebraic ops are random functions
|
||||
// TODO: https://github.com/recmo/uint/issues/95 and use .gen_range(..M)
|
||||
Node::Input(i) => *inputs.entry(*i).or_insert_with(|| rng.gen::<U256>() % M),
|
||||
Node::Op(op, a, b) => *prfs
|
||||
.entry((*op, values[*a], values[*b]))
|
||||
.or_insert_with(|| rng.gen::<U256>() % M),
|
||||
Node::UnoOp(op, a) => *prfs_uno
|
||||
.entry((*op, values[*a]))
|
||||
.or_insert_with(|| rng.gen::<U256>() % M),
|
||||
Node::TresOp(op, a, b, c) => *prfs_tres
|
||||
.entry((*op, values[*a], values[*b], values[*c]))
|
||||
.or_insert_with(|| rng.gen::<U256>() % M),
|
||||
};
|
||||
values.push(value);
|
||||
}
|
||||
values
|
||||
}
|
||||
|
||||
/// Value numbering
|
||||
pub fn value_numbering(nodes: &mut [Node], outputs: &mut [usize]) {
|
||||
assert_valid(nodes);
|
||||
|
||||
// Evaluate the graph in random field elements.
|
||||
let values = random_eval(nodes);
|
||||
|
||||
// Find all nodes with the same value.
|
||||
let mut value_map = HashMap::new();
|
||||
for (i, &value) in values.iter().enumerate() {
|
||||
value_map.entry(value).or_insert_with(Vec::new).push(i);
|
||||
}
|
||||
|
||||
// For nodes that are the same, pick the first index.
|
||||
let renumber: Vec<_> = values.into_iter().map(|v| value_map[&v][0]).collect();
|
||||
|
||||
// Renumber references.
|
||||
for node in nodes.iter_mut() {
|
||||
if let Node::Op(_, a, b) = node {
|
||||
*a = renumber[*a];
|
||||
*b = renumber[*b];
|
||||
}
|
||||
if let Node::UnoOp(_, a) = node {
|
||||
*a = renumber[*a];
|
||||
}
|
||||
if let Node::TresOp(_, a, b, c) = node {
|
||||
*a = renumber[*a];
|
||||
*b = renumber[*b];
|
||||
*c = renumber[*c];
|
||||
}
|
||||
}
|
||||
for output in outputs.iter_mut() {
|
||||
*output = renumber[*output];
|
||||
}
|
||||
}
|
||||
|
||||
/// Probabilistic constant determination
|
||||
pub fn constants(nodes: &mut [Node]) {
|
||||
assert_valid(nodes);
|
||||
|
||||
// Evaluate the graph in random field elements.
|
||||
let values_a = random_eval(nodes);
|
||||
let values_b = random_eval(nodes);
|
||||
|
||||
// Find all nodes with the same value.
|
||||
for i in 0..nodes.len() {
|
||||
if let Node::Constant(_) = nodes[i] {
|
||||
continue;
|
||||
}
|
||||
if values_a[i] == values_b[i] {
|
||||
nodes[i] = Node::Constant(values_a[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert to Montgomery form
|
||||
pub fn montgomery_form(nodes: &mut [Node]) {
|
||||
for node in nodes.iter_mut() {
|
||||
use Node::*;
|
||||
use Operation::*;
|
||||
match node {
|
||||
Constant(c) => *node = MontConstant(u256_to_fr(c)),
|
||||
MontConstant(..) => (),
|
||||
Input(..) => (),
|
||||
Op(
|
||||
Mul | Div | Add | Sub | Idiv | Mod | Eq | Neq | Lt | Gt | Leq | Geq | Land | Lor
|
||||
| Shl | Shr | Bor | Band | Bxor,
|
||||
..,
|
||||
) => (),
|
||||
Op(op @ Pow, ..) => unimplemented!("Operators Montgomery form: {:?}", op),
|
||||
UnoOp(UnoOperation::Neg, ..) => (),
|
||||
UnoOp(op, ..) => unimplemented!("Uno Operators Montgomery form: {:?}", op),
|
||||
TresOp(TresOperation::TernCond, ..) => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn shl(a: Fr, b: Fr) -> Fr {
|
||||
if b.is_zero() {
|
||||
return a;
|
||||
}
|
||||
|
||||
if b.cmp(&Fr::from(Fr::MODULUS_BIT_SIZE)).is_ge() {
|
||||
return Fr::zero();
|
||||
}
|
||||
|
||||
let n = b.into_bigint().0[0] as u32;
|
||||
let a = a.into_bigint();
|
||||
Fr::from_bigint(a << n).unwrap()
|
||||
}
|
||||
|
||||
fn shr(a: Fr, b: Fr) -> Fr {
|
||||
if b.is_zero() {
|
||||
return a;
|
||||
}
|
||||
|
||||
match b.cmp(&Fr::from(254u64)) {
|
||||
Ordering::Equal => return Fr::zero(),
|
||||
Ordering::Greater => return Fr::zero(),
|
||||
_ => (),
|
||||
};
|
||||
|
||||
let mut n = b.into_bigint().to_bytes_le()[0];
|
||||
let mut result = a.into_bigint();
|
||||
let c = result.as_mut();
|
||||
while n >= 64 {
|
||||
for i in 0..3 {
|
||||
c[i as usize] = c[(i + 1) as usize];
|
||||
}
|
||||
c[3] = 0;
|
||||
n -= 64;
|
||||
}
|
||||
|
||||
if n == 0 {
|
||||
return Fr::from_bigint(result).unwrap();
|
||||
}
|
||||
|
||||
let mask: u64 = (1 << n) - 1;
|
||||
let mut carrier: u64 = c[3] & mask;
|
||||
c[3] >>= n;
|
||||
for i in (0..3).rev() {
|
||||
let new_carrier = c[i] & mask;
|
||||
c[i] = (c[i] >> n) | (carrier << (64 - n));
|
||||
carrier = new_carrier;
|
||||
}
|
||||
Fr::from_bigint(result).unwrap()
|
||||
}
|
||||
|
||||
fn bit_and(a: Fr, b: Fr) -> Fr {
|
||||
let a = a.into_bigint();
|
||||
let b = b.into_bigint();
|
||||
let c: [u64; 4] = [
|
||||
a.0[0] & b.0[0],
|
||||
a.0[1] & b.0[1],
|
||||
a.0[2] & b.0[2],
|
||||
a.0[3] & b.0[3],
|
||||
];
|
||||
let mut d: BigInt<4> = BigInt::new(c);
|
||||
if d > Fr::MODULUS {
|
||||
d.sub_with_borrow(&Fr::MODULUS);
|
||||
}
|
||||
|
||||
Fr::from_bigint(d).unwrap()
|
||||
}
|
||||
|
||||
fn bit_or(a: Fr, b: Fr) -> Fr {
|
||||
let a = a.into_bigint();
|
||||
let b = b.into_bigint();
|
||||
let c: [u64; 4] = [
|
||||
a.0[0] | b.0[0],
|
||||
a.0[1] | b.0[1],
|
||||
a.0[2] | b.0[2],
|
||||
a.0[3] | b.0[3],
|
||||
];
|
||||
let mut d: BigInt<4> = BigInt::new(c);
|
||||
if d > Fr::MODULUS {
|
||||
d.sub_with_borrow(&Fr::MODULUS);
|
||||
}
|
||||
|
||||
Fr::from_bigint(d).unwrap()
|
||||
}
|
||||
|
||||
fn bit_xor(a: Fr, b: Fr) -> Fr {
|
||||
let a = a.into_bigint();
|
||||
let b = b.into_bigint();
|
||||
let c: [u64; 4] = [
|
||||
a.0[0] ^ b.0[0],
|
||||
a.0[1] ^ b.0[1],
|
||||
a.0[2] ^ b.0[2],
|
||||
a.0[3] ^ b.0[3],
|
||||
];
|
||||
let mut d: BigInt<4> = BigInt::new(c);
|
||||
if d > Fr::MODULUS {
|
||||
d.sub_with_borrow(&Fr::MODULUS);
|
||||
}
|
||||
|
||||
Fr::from_bigint(d).unwrap()
|
||||
}
|
||||
|
||||
// M / 2
|
||||
const HALF_M: U256 =
|
||||
uint!(10944121435919637611123202872628637544274182200208017171849102093287904247808_U256);
|
||||
|
||||
fn u_gte(a: &U256, b: &U256) -> U256 {
|
||||
let a_neg = &HALF_M < a;
|
||||
let b_neg = &HALF_M < b;
|
||||
|
||||
match (a_neg, b_neg) {
|
||||
(false, false) => U256::from(a >= b),
|
||||
(true, false) => uint!(0_U256),
|
||||
(false, true) => uint!(1_U256),
|
||||
(true, true) => U256::from(a >= b),
|
||||
}
|
||||
}
|
||||
|
||||
fn u_lte(a: &U256, b: &U256) -> U256 {
|
||||
let a_neg = &HALF_M < a;
|
||||
let b_neg = &HALF_M < b;
|
||||
|
||||
match (a_neg, b_neg) {
|
||||
(false, false) => U256::from(a <= b),
|
||||
(true, false) => uint!(1_U256),
|
||||
(false, true) => uint!(0_U256),
|
||||
(true, true) => U256::from(a <= b),
|
||||
}
|
||||
}
|
||||
|
||||
fn u_gt(a: &U256, b: &U256) -> U256 {
|
||||
let a_neg = &HALF_M < a;
|
||||
let b_neg = &HALF_M < b;
|
||||
|
||||
match (a_neg, b_neg) {
|
||||
(false, false) => U256::from(a > b),
|
||||
(true, false) => uint!(0_U256),
|
||||
(false, true) => uint!(1_U256),
|
||||
(true, true) => U256::from(a > b),
|
||||
}
|
||||
}
|
||||
|
||||
fn u_lt(a: &U256, b: &U256) -> U256 {
|
||||
let a_neg = &HALF_M < a;
|
||||
let b_neg = &HALF_M < b;
|
||||
|
||||
match (a_neg, b_neg) {
|
||||
(false, false) => U256::from(a < b),
|
||||
(true, false) => uint!(1_U256),
|
||||
(false, true) => uint!(0_U256),
|
||||
(true, true) => U256::from(a < b),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use ruint::uint;
|
||||
use std::ops::Div;
|
||||
use std::str::FromStr;
|
||||
|
||||
#[test]
|
||||
fn test_ok() {
|
||||
let a = Fr::from(4u64);
|
||||
let b = Fr::from(2u64);
|
||||
let c = shl(a, b);
|
||||
assert_eq!(c.cmp(&Fr::from(16u64)), Ordering::Equal)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_div() {
|
||||
assert_eq!(
|
||||
Operation::Div.eval_fr(Fr::from(2u64), Fr::from(3u64)),
|
||||
Fr::from_str(
|
||||
"7296080957279758407415468581752425029516121466805344781232734728858602831873"
|
||||
)
|
||||
.unwrap()
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
Operation::Div.eval_fr(Fr::from(6u64), Fr::from(2u64)),
|
||||
Fr::from_str("3").unwrap()
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
Operation::Div.eval_fr(Fr::from(7u64), Fr::from(2u64)),
|
||||
Fr::from_str(
|
||||
"10944121435919637611123202872628637544274182200208017171849102093287904247812"
|
||||
)
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_idiv() {
|
||||
assert_eq!(
|
||||
Operation::Idiv.eval_fr(Fr::from(2u64), Fr::from(3u64)),
|
||||
Fr::from_str("0").unwrap()
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
Operation::Idiv.eval_fr(Fr::from(6u64), Fr::from(2u64)),
|
||||
Fr::from_str("3").unwrap()
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
Operation::Idiv.eval_fr(Fr::from(7u64), Fr::from(2u64)),
|
||||
Fr::from_str("3").unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fr_mod() {
|
||||
assert_eq!(
|
||||
Operation::Mod.eval_fr(Fr::from(7u64), Fr::from(2u64)),
|
||||
Fr::from_str("1").unwrap()
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
Operation::Mod.eval_fr(Fr::from(7u64), Fr::from(9u64)),
|
||||
Fr::from_str("7").unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_u_gte() {
|
||||
let result = u_gte(&uint!(10_U256), &uint!(3_U256));
|
||||
assert_eq!(result, uint!(1_U256));
|
||||
|
||||
let result = u_gte(&uint!(3_U256), &uint!(3_U256));
|
||||
assert_eq!(result, uint!(1_U256));
|
||||
|
||||
let result = u_gte(&uint!(2_U256), &uint!(3_U256));
|
||||
assert_eq!(result, uint!(0_U256));
|
||||
|
||||
// -1 >= 3 => 0
|
||||
let result = u_gte(
|
||||
&uint!(
|
||||
21888242871839275222246405745257275088548364400416034343698204186575808495616_U256
|
||||
),
|
||||
&uint!(3_U256),
|
||||
);
|
||||
assert_eq!(result, uint!(0_U256));
|
||||
|
||||
// -1 >= -2 => 1
|
||||
let result = u_gte(
|
||||
&uint!(
|
||||
21888242871839275222246405745257275088548364400416034343698204186575808495616_U256
|
||||
),
|
||||
&uint!(
|
||||
21888242871839275222246405745257275088548364400416034343698204186575808495615_U256
|
||||
),
|
||||
);
|
||||
assert_eq!(result, uint!(1_U256));
|
||||
|
||||
// -2 >= -1 => 0
|
||||
let result = u_gte(
|
||||
&uint!(
|
||||
21888242871839275222246405745257275088548364400416034343698204186575808495615_U256
|
||||
),
|
||||
&uint!(
|
||||
21888242871839275222246405745257275088548364400416034343698204186575808495616_U256
|
||||
),
|
||||
);
|
||||
assert_eq!(result, uint!(0_U256));
|
||||
|
||||
// -2 == -2 => 1
|
||||
let result = u_gte(
|
||||
&uint!(
|
||||
21888242871839275222246405745257275088548364400416034343698204186575808495615_U256
|
||||
),
|
||||
&uint!(
|
||||
21888242871839275222246405745257275088548364400416034343698204186575808495615_U256
|
||||
),
|
||||
);
|
||||
assert_eq!(result, uint!(1_U256));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_x() {
|
||||
let x = M.div(uint!(2_U256));
|
||||
|
||||
println!("x: {:?}", x.as_limbs());
|
||||
println!("x: {M}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_2() {
|
||||
let nodes: Vec<Node> = vec![];
|
||||
// let node = nodes[0];
|
||||
let node = nodes.first();
|
||||
println!("{node:?}");
|
||||
}
|
||||
}
|
||||
117
rln/src/circuit/iden3calc/proto.rs
Normal file
117
rln/src/circuit/iden3calc/proto.rs
Normal file
@@ -0,0 +1,117 @@
|
||||
// This file has been generated by prost-build during compilation of the code by iden3
|
||||
// and modified manually. The *.proto file used to generate this on can be found here:
|
||||
// https://github.com/iden3/circom-witnesscalc/blob/5cb365b6e4d9052ecc69d4567fcf5bc061c20e94/protos/messages.proto
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||
pub struct BigUInt {
|
||||
#[prost(bytes = "vec", tag = "1")]
|
||||
pub value_le: Vec<u8>,
|
||||
}
|
||||
#[derive(Clone, Copy, PartialEq, ::prost::Message)]
|
||||
pub struct InputNode {
|
||||
#[prost(uint32, tag = "1")]
|
||||
pub idx: u32,
|
||||
}
|
||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||
pub struct ConstantNode {
|
||||
#[prost(message, optional, tag = "1")]
|
||||
pub value: Option<BigUInt>,
|
||||
}
|
||||
#[derive(Clone, Copy, PartialEq, ::prost::Message)]
|
||||
pub struct UnoOpNode {
|
||||
#[prost(enumeration = "UnoOp", tag = "1")]
|
||||
pub op: i32,
|
||||
#[prost(uint32, tag = "2")]
|
||||
pub a_idx: u32,
|
||||
}
|
||||
#[derive(Clone, Copy, PartialEq, ::prost::Message)]
|
||||
pub struct DuoOpNode {
|
||||
#[prost(enumeration = "DuoOp", tag = "1")]
|
||||
pub op: i32,
|
||||
#[prost(uint32, tag = "2")]
|
||||
pub a_idx: u32,
|
||||
#[prost(uint32, tag = "3")]
|
||||
pub b_idx: u32,
|
||||
}
|
||||
#[derive(Clone, Copy, PartialEq, ::prost::Message)]
|
||||
pub struct TresOpNode {
|
||||
#[prost(enumeration = "TresOp", tag = "1")]
|
||||
pub op: i32,
|
||||
#[prost(uint32, tag = "2")]
|
||||
pub a_idx: u32,
|
||||
#[prost(uint32, tag = "3")]
|
||||
pub b_idx: u32,
|
||||
#[prost(uint32, tag = "4")]
|
||||
pub c_idx: u32,
|
||||
}
|
||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||
pub struct Node {
|
||||
#[prost(oneof = "node::Node", tags = "1, 2, 3, 4, 5")]
|
||||
pub node: Option<node::Node>,
|
||||
}
|
||||
/// Nested message and enum types in `Node`.
|
||||
pub mod node {
|
||||
#[derive(Clone, PartialEq, ::prost::Oneof)]
|
||||
pub enum Node {
|
||||
#[prost(message, tag = "1")]
|
||||
Input(super::InputNode),
|
||||
#[prost(message, tag = "2")]
|
||||
Constant(super::ConstantNode),
|
||||
#[prost(message, tag = "3")]
|
||||
UnoOp(super::UnoOpNode),
|
||||
#[prost(message, tag = "4")]
|
||||
DuoOp(super::DuoOpNode),
|
||||
#[prost(message, tag = "5")]
|
||||
TresOp(super::TresOpNode),
|
||||
}
|
||||
}
|
||||
#[derive(Clone, Copy, PartialEq, ::prost::Message)]
|
||||
pub struct SignalDescription {
|
||||
#[prost(uint32, tag = "1")]
|
||||
pub offset: u32,
|
||||
#[prost(uint32, tag = "2")]
|
||||
pub len: u32,
|
||||
}
|
||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||
pub struct GraphMetadata {
|
||||
#[prost(uint32, repeated, tag = "1")]
|
||||
pub witness_signals: Vec<u32>,
|
||||
#[prost(map = "string, message", tag = "2")]
|
||||
pub inputs: HashMap<String, SignalDescription>,
|
||||
}
|
||||
#[derive(Clone, Copy, Debug, PartialEq, ::prost::Enumeration)]
|
||||
pub enum DuoOp {
|
||||
Mul = 0,
|
||||
Div = 1,
|
||||
Add = 2,
|
||||
Sub = 3,
|
||||
Pow = 4,
|
||||
Idiv = 5,
|
||||
Mod = 6,
|
||||
Eq = 7,
|
||||
Neq = 8,
|
||||
Lt = 9,
|
||||
Gt = 10,
|
||||
Leq = 11,
|
||||
Geq = 12,
|
||||
Land = 13,
|
||||
Lor = 14,
|
||||
Shl = 15,
|
||||
Shr = 16,
|
||||
Bor = 17,
|
||||
Band = 18,
|
||||
Bxor = 19,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, ::prost::Enumeration)]
|
||||
pub enum UnoOp {
|
||||
Neg = 0,
|
||||
Id = 1,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, ::prost::Enumeration)]
|
||||
pub enum TresOp {
|
||||
TernCond = 0,
|
||||
}
|
||||
497
rln/src/circuit/iden3calc/storage.rs
Normal file
497
rln/src/circuit/iden3calc/storage.rs
Normal file
@@ -0,0 +1,497 @@
|
||||
// This file is based on the code by iden3. Its preimage can be found here:
|
||||
// https://github.com/iden3/circom-witnesscalc/blob/5cb365b6e4d9052ecc69d4567fcf5bc061c20e94/src/storage.rs
|
||||
|
||||
use ark_bn254::Fr;
|
||||
use ark_ff::PrimeField;
|
||||
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
|
||||
use prost::Message;
|
||||
use std::io::{Read, Write};
|
||||
|
||||
use crate::circuit::iden3calc::{
|
||||
graph,
|
||||
graph::{Operation, TresOperation, UnoOperation},
|
||||
proto, InputSignalsInfo,
|
||||
};
|
||||
|
||||
// format of the wtns.graph file:
|
||||
// + magic line: wtns.graph.001
|
||||
// + 4 bytes unsigned LE 32-bit integer: number of nodes
|
||||
// + series of protobuf serialized nodes. Each node prefixed by varint length
|
||||
// + protobuf serialized GraphMetadata
|
||||
// + 8 bytes unsigned LE 64-bit integer: offset of GraphMetadata message
|
||||
|
||||
const WITNESSCALC_GRAPH_MAGIC: &[u8] = b"wtns.graph.001";
|
||||
|
||||
const MAX_VARINT_LENGTH: usize = 10;
|
||||
|
||||
impl From<proto::Node> for graph::Node {
|
||||
fn from(value: proto::Node) -> Self {
|
||||
match value.node.unwrap() {
|
||||
proto::node::Node::Input(input_node) => graph::Node::Input(input_node.idx as usize),
|
||||
proto::node::Node::Constant(constant_node) => {
|
||||
let i = constant_node.value.unwrap();
|
||||
graph::Node::MontConstant(Fr::from_le_bytes_mod_order(i.value_le.as_slice()))
|
||||
}
|
||||
proto::node::Node::UnoOp(uno_op_node) => {
|
||||
let op = proto::UnoOp::try_from(uno_op_node.op).unwrap();
|
||||
graph::Node::UnoOp(op.into(), uno_op_node.a_idx as usize)
|
||||
}
|
||||
proto::node::Node::DuoOp(duo_op_node) => {
|
||||
let op = proto::DuoOp::try_from(duo_op_node.op).unwrap();
|
||||
graph::Node::Op(
|
||||
op.into(),
|
||||
duo_op_node.a_idx as usize,
|
||||
duo_op_node.b_idx as usize,
|
||||
)
|
||||
}
|
||||
proto::node::Node::TresOp(tres_op_node) => {
|
||||
let op = proto::TresOp::try_from(tres_op_node.op).unwrap();
|
||||
graph::Node::TresOp(
|
||||
op.into(),
|
||||
tres_op_node.a_idx as usize,
|
||||
tres_op_node.b_idx as usize,
|
||||
tres_op_node.c_idx as usize,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&graph::Node> for proto::node::Node {
|
||||
fn from(node: &graph::Node) -> Self {
|
||||
match node {
|
||||
graph::Node::Input(i) => proto::node::Node::Input(proto::InputNode { idx: *i as u32 }),
|
||||
graph::Node::Constant(_) => {
|
||||
panic!("We are not supposed to write Constant to the witnesscalc graph. All Constant should be converted to MontConstant.");
|
||||
}
|
||||
graph::Node::UnoOp(op, a) => {
|
||||
let op = proto::UnoOp::from(op);
|
||||
proto::node::Node::UnoOp(proto::UnoOpNode {
|
||||
op: op as i32,
|
||||
a_idx: *a as u32,
|
||||
})
|
||||
}
|
||||
graph::Node::Op(op, a, b) => proto::node::Node::DuoOp(proto::DuoOpNode {
|
||||
op: proto::DuoOp::from(op) as i32,
|
||||
a_idx: *a as u32,
|
||||
b_idx: *b as u32,
|
||||
}),
|
||||
graph::Node::TresOp(op, a, b, c) => proto::node::Node::TresOp(proto::TresOpNode {
|
||||
op: proto::TresOp::from(op) as i32,
|
||||
a_idx: *a as u32,
|
||||
b_idx: *b as u32,
|
||||
c_idx: *c as u32,
|
||||
}),
|
||||
graph::Node::MontConstant(c) => {
|
||||
let bi = Into::<num_bigint::BigUint>::into(*c);
|
||||
let i = proto::BigUInt {
|
||||
value_le: bi.to_bytes_le(),
|
||||
};
|
||||
proto::node::Node::Constant(proto::ConstantNode { value: Some(i) })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<proto::UnoOp> for UnoOperation {
|
||||
fn from(value: proto::UnoOp) -> Self {
|
||||
match value {
|
||||
proto::UnoOp::Neg => UnoOperation::Neg,
|
||||
proto::UnoOp::Id => UnoOperation::Id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<proto::DuoOp> for Operation {
|
||||
fn from(value: proto::DuoOp) -> Self {
|
||||
match value {
|
||||
proto::DuoOp::Mul => Operation::Mul,
|
||||
proto::DuoOp::Div => Operation::Div,
|
||||
proto::DuoOp::Add => Operation::Add,
|
||||
proto::DuoOp::Sub => Operation::Sub,
|
||||
proto::DuoOp::Pow => Operation::Pow,
|
||||
proto::DuoOp::Idiv => Operation::Idiv,
|
||||
proto::DuoOp::Mod => Operation::Mod,
|
||||
proto::DuoOp::Eq => Operation::Eq,
|
||||
proto::DuoOp::Neq => Operation::Neq,
|
||||
proto::DuoOp::Lt => Operation::Lt,
|
||||
proto::DuoOp::Gt => Operation::Gt,
|
||||
proto::DuoOp::Leq => Operation::Leq,
|
||||
proto::DuoOp::Geq => Operation::Geq,
|
||||
proto::DuoOp::Land => Operation::Land,
|
||||
proto::DuoOp::Lor => Operation::Lor,
|
||||
proto::DuoOp::Shl => Operation::Shl,
|
||||
proto::DuoOp::Shr => Operation::Shr,
|
||||
proto::DuoOp::Bor => Operation::Bor,
|
||||
proto::DuoOp::Band => Operation::Band,
|
||||
proto::DuoOp::Bxor => Operation::Bxor,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<proto::TresOp> for graph::TresOperation {
|
||||
fn from(value: proto::TresOp) -> Self {
|
||||
match value {
|
||||
proto::TresOp::TernCond => TresOperation::TernCond,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn serialize_witnesscalc_graph<T: Write>(
|
||||
mut w: T,
|
||||
nodes: &Vec<graph::Node>,
|
||||
witness_signals: &[usize],
|
||||
input_signals: &InputSignalsInfo,
|
||||
) -> std::io::Result<()> {
|
||||
let mut ptr = 0usize;
|
||||
w.write_all(WITNESSCALC_GRAPH_MAGIC).unwrap();
|
||||
ptr += WITNESSCALC_GRAPH_MAGIC.len();
|
||||
|
||||
w.write_u64::<LittleEndian>(nodes.len() as u64)?;
|
||||
ptr += 8;
|
||||
|
||||
let metadata = proto::GraphMetadata {
|
||||
witness_signals: witness_signals
|
||||
.iter()
|
||||
.map(|x| *x as u32)
|
||||
.collect::<Vec<u32>>(),
|
||||
inputs: input_signals
|
||||
.iter()
|
||||
.map(|(k, v)| {
|
||||
let sig = proto::SignalDescription {
|
||||
offset: v.0 as u32,
|
||||
len: v.1 as u32,
|
||||
};
|
||||
(k.clone(), sig)
|
||||
})
|
||||
.collect(),
|
||||
};
|
||||
|
||||
// capacity of buf should be enough to hold the largest message + 10 bytes
|
||||
// of varint length
|
||||
let mut buf = Vec::with_capacity(metadata.encoded_len() + MAX_VARINT_LENGTH);
|
||||
|
||||
for node in nodes {
|
||||
let node_pb = proto::Node {
|
||||
node: Some(proto::node::Node::from(node)),
|
||||
};
|
||||
|
||||
assert_eq!(buf.len(), 0);
|
||||
node_pb.encode_length_delimited(&mut buf)?;
|
||||
ptr += buf.len();
|
||||
|
||||
w.write_all(&buf)?;
|
||||
buf.clear();
|
||||
}
|
||||
|
||||
metadata.encode_length_delimited(&mut buf)?;
|
||||
w.write_all(&buf)?;
|
||||
buf.clear();
|
||||
|
||||
w.write_u64::<LittleEndian>(ptr as u64)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read_message_length<R: Read>(rw: &mut WriteBackReader<R>) -> std::io::Result<usize> {
|
||||
let mut buf = [0u8; MAX_VARINT_LENGTH];
|
||||
let bytes_read = rw.read(&mut buf)?;
|
||||
if bytes_read == 0 {
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::UnexpectedEof,
|
||||
"Unexpected EOF",
|
||||
));
|
||||
}
|
||||
|
||||
let len_delimiter = prost::decode_length_delimiter(buf.as_ref())?;
|
||||
|
||||
let lnln = prost::length_delimiter_len(len_delimiter);
|
||||
|
||||
if lnln < bytes_read {
|
||||
rw.write_all(&buf[lnln..bytes_read])?;
|
||||
}
|
||||
|
||||
Ok(len_delimiter)
|
||||
}
|
||||
|
||||
fn read_message<R: Read, M: Message + std::default::Default>(
|
||||
rw: &mut WriteBackReader<R>,
|
||||
) -> std::io::Result<M> {
|
||||
let ln = read_message_length(rw)?;
|
||||
let mut buf = vec![0u8; ln];
|
||||
let bytes_read = rw.read(&mut buf)?;
|
||||
if bytes_read != ln {
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::UnexpectedEof,
|
||||
"Unexpected EOF",
|
||||
));
|
||||
}
|
||||
|
||||
let msg = prost::Message::decode(&buf[..])?;
|
||||
|
||||
Ok(msg)
|
||||
}
|
||||
|
||||
pub fn deserialize_witnesscalc_graph(
|
||||
r: impl Read,
|
||||
) -> std::io::Result<(Vec<graph::Node>, Vec<usize>, InputSignalsInfo)> {
|
||||
let mut br = WriteBackReader::new(r);
|
||||
let mut magic = [0u8; WITNESSCALC_GRAPH_MAGIC.len()];
|
||||
|
||||
br.read_exact(&mut magic)?;
|
||||
|
||||
if !magic.eq(WITNESSCALC_GRAPH_MAGIC) {
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidData,
|
||||
"Invalid magic",
|
||||
));
|
||||
}
|
||||
|
||||
let nodes_num = br.read_u64::<LittleEndian>()?;
|
||||
let mut nodes = Vec::with_capacity(nodes_num as usize);
|
||||
for _ in 0..nodes_num {
|
||||
let n: proto::Node = read_message(&mut br)?;
|
||||
let n2: graph::Node = n.into();
|
||||
nodes.push(n2);
|
||||
}
|
||||
|
||||
let md: proto::GraphMetadata = read_message(&mut br)?;
|
||||
|
||||
let witness_signals = md
|
||||
.witness_signals
|
||||
.iter()
|
||||
.map(|x| *x as usize)
|
||||
.collect::<Vec<usize>>();
|
||||
|
||||
let input_signals = md
|
||||
.inputs
|
||||
.iter()
|
||||
.map(|(k, v)| (k.clone(), (v.offset as usize, v.len as usize)))
|
||||
.collect::<InputSignalsInfo>();
|
||||
|
||||
Ok((nodes, witness_signals, input_signals))
|
||||
}
|
||||
|
||||
struct WriteBackReader<R: Read> {
|
||||
reader: R,
|
||||
buffer: Vec<u8>,
|
||||
}
|
||||
|
||||
impl<R: Read> WriteBackReader<R> {
|
||||
fn new(reader: R) -> Self {
|
||||
WriteBackReader {
|
||||
reader,
|
||||
buffer: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: Read> Read for WriteBackReader<R> {
|
||||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
||||
if buf.is_empty() {
|
||||
return Ok(0);
|
||||
}
|
||||
|
||||
let mut n = 0usize;
|
||||
|
||||
if !self.buffer.is_empty() {
|
||||
n = std::cmp::min(buf.len(), self.buffer.len());
|
||||
self.buffer[self.buffer.len() - n..]
|
||||
.iter()
|
||||
.rev()
|
||||
.enumerate()
|
||||
.for_each(|(i, x)| {
|
||||
buf[i] = *x;
|
||||
});
|
||||
self.buffer.truncate(self.buffer.len() - n);
|
||||
}
|
||||
|
||||
while n < buf.len() {
|
||||
let m = self.reader.read(&mut buf[n..])?;
|
||||
if m == 0 {
|
||||
break;
|
||||
}
|
||||
n += m;
|
||||
}
|
||||
|
||||
Ok(n)
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: Read> Write for WriteBackReader<R> {
|
||||
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
|
||||
self.buffer.reserve(buf.len());
|
||||
self.buffer.extend(buf.iter().rev());
|
||||
Ok(buf.len())
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> std::io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use byteorder::ByteOrder;
|
||||
use core::str::FromStr;
|
||||
use graph::{Operation, TresOperation, UnoOperation};
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[test]
|
||||
fn test_read_message() {
|
||||
let mut buf = Vec::new();
|
||||
let n1 = proto::Node {
|
||||
node: Some(proto::node::Node::Input(proto::InputNode { idx: 1 })),
|
||||
};
|
||||
n1.encode_length_delimited(&mut buf).unwrap();
|
||||
|
||||
let n2 = proto::Node {
|
||||
node: Some(proto::node::Node::Input(proto::InputNode { idx: 2 })),
|
||||
};
|
||||
n2.encode_length_delimited(&mut buf).unwrap();
|
||||
|
||||
let mut reader = std::io::Cursor::new(&buf);
|
||||
|
||||
let mut rw = WriteBackReader::new(&mut reader);
|
||||
|
||||
let got_n1: proto::Node = read_message(&mut rw).unwrap();
|
||||
assert!(n1.eq(&got_n1));
|
||||
|
||||
let got_n2: proto::Node = read_message(&mut rw).unwrap();
|
||||
assert!(n2.eq(&got_n2));
|
||||
|
||||
assert_eq!(reader.position(), buf.len() as u64);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_message_variant() {
|
||||
let nodes = vec![
|
||||
proto::Node {
|
||||
node: Some(proto::node::Node::from(&graph::Node::Input(0))),
|
||||
},
|
||||
proto::Node {
|
||||
node: Some(proto::node::Node::from(&graph::Node::MontConstant(
|
||||
Fr::from_str("1").unwrap(),
|
||||
))),
|
||||
},
|
||||
proto::Node {
|
||||
node: Some(proto::node::Node::from(&graph::Node::UnoOp(
|
||||
UnoOperation::Id,
|
||||
4,
|
||||
))),
|
||||
},
|
||||
proto::Node {
|
||||
node: Some(proto::node::Node::from(&graph::Node::Op(
|
||||
Operation::Mul,
|
||||
5,
|
||||
6,
|
||||
))),
|
||||
},
|
||||
proto::Node {
|
||||
node: Some(proto::node::Node::from(&graph::Node::TresOp(
|
||||
TresOperation::TernCond,
|
||||
7,
|
||||
8,
|
||||
9,
|
||||
))),
|
||||
},
|
||||
];
|
||||
|
||||
let mut buf = Vec::new();
|
||||
for n in &nodes {
|
||||
n.encode_length_delimited(&mut buf).unwrap();
|
||||
}
|
||||
|
||||
let mut nodes_got: Vec<proto::Node> = Vec::new();
|
||||
let mut reader = std::io::Cursor::new(&buf);
|
||||
let mut rw = WriteBackReader::new(&mut reader);
|
||||
for _ in 0..nodes.len() {
|
||||
nodes_got.push(read_message(&mut rw).unwrap());
|
||||
}
|
||||
|
||||
assert_eq!(nodes, nodes_got);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_write_back_reader() {
|
||||
let data = [1u8, 2, 3, 4, 5, 6];
|
||||
let mut r = WriteBackReader::new(std::io::Cursor::new(&data));
|
||||
|
||||
let buf = &mut [0u8; 5];
|
||||
r.read_exact(buf).unwrap();
|
||||
assert_eq!(buf, &[1, 2, 3, 4, 5]);
|
||||
|
||||
// return [4, 5] to reader
|
||||
r.write_all(&buf[3..]).unwrap();
|
||||
// return [2, 3] to reader
|
||||
r.write_all(&buf[1..3]).unwrap();
|
||||
|
||||
buf.fill(0);
|
||||
|
||||
// read 3 bytes, expect [2, 3, 4] after returns
|
||||
let mut n = r.read(&mut buf[..3]).unwrap();
|
||||
assert_eq!(n, 3);
|
||||
assert_eq!(buf, &[2, 3, 4, 0, 0]);
|
||||
|
||||
buf.fill(0);
|
||||
|
||||
// read everything left in reader
|
||||
n = r.read(buf).unwrap();
|
||||
assert_eq!(n, 2);
|
||||
assert_eq!(buf, &[5, 6, 0, 0, 0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_inputs() {
|
||||
let nodes = vec![
|
||||
graph::Node::Input(0),
|
||||
graph::Node::MontConstant(Fr::from_str("1").unwrap()),
|
||||
graph::Node::UnoOp(UnoOperation::Id, 4),
|
||||
graph::Node::Op(Operation::Mul, 5, 6),
|
||||
graph::Node::TresOp(TresOperation::TernCond, 7, 8, 9),
|
||||
];
|
||||
|
||||
let witness_signals = vec![4, 1];
|
||||
|
||||
let mut input_signals: InputSignalsInfo = HashMap::new();
|
||||
input_signals.insert("sig1".to_string(), (1, 3));
|
||||
input_signals.insert("sig2".to_string(), (5, 1));
|
||||
|
||||
let mut tmp = Vec::new();
|
||||
serialize_witnesscalc_graph(&mut tmp, &nodes, &witness_signals, &input_signals).unwrap();
|
||||
|
||||
let mut reader = std::io::Cursor::new(&tmp);
|
||||
|
||||
let (nodes_res, witness_signals_res, input_signals_res) =
|
||||
deserialize_witnesscalc_graph(&mut reader).unwrap();
|
||||
|
||||
assert_eq!(nodes, nodes_res);
|
||||
assert_eq!(input_signals, input_signals_res);
|
||||
assert_eq!(witness_signals, witness_signals_res);
|
||||
|
||||
let metadata_start = LittleEndian::read_u64(&tmp[tmp.len() - 8..]);
|
||||
|
||||
let mt_reader = std::io::Cursor::new(&tmp[metadata_start as usize..]);
|
||||
let mut rw = WriteBackReader::new(mt_reader);
|
||||
let metadata: proto::GraphMetadata = read_message(&mut rw).unwrap();
|
||||
|
||||
let metadata_want = proto::GraphMetadata {
|
||||
witness_signals: vec![4, 1],
|
||||
inputs: input_signals
|
||||
.iter()
|
||||
.map(|(k, v)| {
|
||||
(
|
||||
k.clone(),
|
||||
proto::SignalDescription {
|
||||
offset: v.0 as u32,
|
||||
len: v.1 as u32,
|
||||
},
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
};
|
||||
|
||||
assert_eq!(metadata, metadata_want);
|
||||
}
|
||||
}
|
||||
132
rln/src/circuit/mod.rs
Normal file
132
rln/src/circuit/mod.rs
Normal file
@@ -0,0 +1,132 @@
|
||||
// This crate provides interfaces for the zero-knowledge circuit and keys
|
||||
|
||||
pub mod error;
|
||||
pub mod iden3calc;
|
||||
pub mod qap;
|
||||
|
||||
use ::lazy_static::lazy_static;
|
||||
use ark_bn254::{
|
||||
Bn254, Fq as ArkFq, Fq2 as ArkFq2, Fr as ArkFr, G1Affine as ArkG1Affine,
|
||||
G1Projective as ArkG1Projective, G2Affine as ArkG2Affine, G2Projective as ArkG2Projective,
|
||||
};
|
||||
use ark_groth16::ProvingKey;
|
||||
use ark_relations::r1cs::ConstraintMatrices;
|
||||
|
||||
use crate::circuit::error::ZKeyReadError;
|
||||
use crate::circuit::iden3calc::calc_witness;
|
||||
|
||||
use {ark_ff::Field, ark_serialize::CanonicalDeserialize, ark_serialize::CanonicalSerialize};
|
||||
|
||||
use crate::utils::FrOrSecret;
|
||||
|
||||
pub const ARKZKEY_BYTES: &[u8] = include_bytes!("../../resources/tree_depth_30/rln_final.arkzkey");
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
const GRAPH_BYTES: &[u8] = include_bytes!("../../resources/tree_depth_30/graph.bin");
|
||||
|
||||
lazy_static! {
|
||||
static ref ARKZKEY: (ProvingKey<Curve>, ConstraintMatrices<Fr>) =
|
||||
read_arkzkey_from_bytes_uncompressed(ARKZKEY_BYTES).expect("Failed to read arkzkey");
|
||||
}
|
||||
|
||||
pub const TEST_TREE_DEPTH: usize = 30;
|
||||
|
||||
// The following types define the pairing friendly elliptic curve, the underlying finite fields and groups default to this module
|
||||
// Note that proofs are serialized assuming Fr to be 4x8 = 32 bytes in size. Hence, changing to a curve with different encoding will make proof verification to fail
|
||||
pub type Curve = Bn254;
|
||||
pub type Fr = ArkFr;
|
||||
pub type Fq = ArkFq;
|
||||
pub type Fq2 = ArkFq2;
|
||||
pub type G1Affine = ArkG1Affine;
|
||||
pub type G1Projective = ArkG1Projective;
|
||||
pub type G2Affine = ArkG2Affine;
|
||||
pub type G2Projective = ArkG2Projective;
|
||||
|
||||
// Loads the proving key using a bytes vector
|
||||
pub fn zkey_from_raw(
|
||||
zkey_data: &[u8],
|
||||
) -> Result<(ProvingKey<Curve>, ConstraintMatrices<Fr>), ZKeyReadError> {
|
||||
if zkey_data.is_empty() {
|
||||
return Err(ZKeyReadError::EmptyBytes);
|
||||
}
|
||||
|
||||
let proving_key_and_matrices = read_arkzkey_from_bytes_uncompressed(zkey_data)?;
|
||||
|
||||
Ok(proving_key_and_matrices)
|
||||
}
|
||||
|
||||
// Loads the proving key
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub fn zkey_from_folder() -> &'static (ProvingKey<Curve>, ConstraintMatrices<Fr>) {
|
||||
&ARKZKEY
|
||||
}
|
||||
|
||||
pub fn calculate_rln_witness<I: IntoIterator<Item = (String, Vec<FrOrSecret>)>>(
|
||||
inputs: I,
|
||||
graph_data: &[u8],
|
||||
) -> Vec<Fr> {
|
||||
calc_witness(inputs, graph_data)
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub fn graph_from_folder() -> &'static [u8] {
|
||||
GRAPH_BYTES
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////
|
||||
// Functions and structs from [arkz-key](https://github.com/zkmopro/ark-zkey/blob/main/src/lib.rs#L106)
|
||||
// without print and allow to choose between compressed and uncompressed arkzkey
|
||||
////////////////////////////////////////////////////////
|
||||
|
||||
#[derive(CanonicalSerialize, CanonicalDeserialize, Clone, Debug, PartialEq)]
|
||||
pub struct SerializableProvingKey(pub ProvingKey<Bn254>);
|
||||
|
||||
#[derive(CanonicalSerialize, CanonicalDeserialize, Clone, Debug, PartialEq)]
|
||||
pub struct SerializableConstraintMatrices<F: Field> {
|
||||
pub num_instance_variables: usize,
|
||||
pub num_witness_variables: usize,
|
||||
pub num_constraints: usize,
|
||||
pub a_num_non_zero: usize,
|
||||
pub b_num_non_zero: usize,
|
||||
pub c_num_non_zero: usize,
|
||||
pub a: SerializableMatrix<F>,
|
||||
pub b: SerializableMatrix<F>,
|
||||
pub c: SerializableMatrix<F>,
|
||||
}
|
||||
|
||||
#[derive(CanonicalSerialize, CanonicalDeserialize, Clone, Debug, PartialEq)]
|
||||
pub struct SerializableMatrix<F: Field> {
|
||||
pub data: Vec<Vec<(F, usize)>>,
|
||||
}
|
||||
|
||||
pub fn read_arkzkey_from_bytes_uncompressed(
|
||||
arkzkey_data: &[u8],
|
||||
) -> Result<(ProvingKey<Curve>, ConstraintMatrices<Fr>), ZKeyReadError> {
|
||||
if arkzkey_data.is_empty() {
|
||||
return Err(ZKeyReadError::EmptyBytes);
|
||||
}
|
||||
|
||||
let mut cursor = std::io::Cursor::new(arkzkey_data);
|
||||
|
||||
let serialized_proving_key =
|
||||
SerializableProvingKey::deserialize_uncompressed_unchecked(&mut cursor)?;
|
||||
|
||||
let serialized_constraint_matrices =
|
||||
SerializableConstraintMatrices::deserialize_uncompressed_unchecked(&mut cursor)?;
|
||||
|
||||
// Get on right form for API
|
||||
let proving_key: ProvingKey<Bn254> = serialized_proving_key.0;
|
||||
let constraint_matrices: ConstraintMatrices<ark_bn254::Fr> = ConstraintMatrices {
|
||||
num_instance_variables: serialized_constraint_matrices.num_instance_variables,
|
||||
num_witness_variables: serialized_constraint_matrices.num_witness_variables,
|
||||
num_constraints: serialized_constraint_matrices.num_constraints,
|
||||
a_num_non_zero: serialized_constraint_matrices.a_num_non_zero,
|
||||
b_num_non_zero: serialized_constraint_matrices.b_num_non_zero,
|
||||
c_num_non_zero: serialized_constraint_matrices.c_num_non_zero,
|
||||
a: serialized_constraint_matrices.a.data,
|
||||
b: serialized_constraint_matrices.b.data,
|
||||
c: serialized_constraint_matrices.c.data,
|
||||
};
|
||||
|
||||
Ok((proving_key, constraint_matrices))
|
||||
}
|
||||
120
rln/src/circuit/qap.rs
Normal file
120
rln/src/circuit/qap.rs
Normal file
@@ -0,0 +1,120 @@
|
||||
// This file is based on the code by arkworks. Its preimage can be found here:
|
||||
// https://github.com/arkworks-rs/circom-compat/blob/3c95ed98e23a408b4d99a53e483a9bba39685a4e/src/circom/qap.rs
|
||||
|
||||
use ark_ff::PrimeField;
|
||||
use ark_groth16::r1cs_to_qap::{evaluate_constraint, LibsnarkReduction, R1CSToQAP};
|
||||
use ark_poly::EvaluationDomain;
|
||||
use ark_relations::r1cs::{ConstraintMatrices, ConstraintSystemRef, SynthesisError};
|
||||
use ark_std::{cfg_into_iter, cfg_iter, cfg_iter_mut, vec};
|
||||
|
||||
#[cfg(feature = "parallel")]
|
||||
use rayon::iter::{
|
||||
IndexedParallelIterator, IntoParallelIterator, IntoParallelRefIterator,
|
||||
IntoParallelRefMutIterator, ParallelIterator,
|
||||
};
|
||||
|
||||
/// Implements the witness map used by snarkjs. The arkworks witness map calculates the
|
||||
/// coefficients of H through computing (AB-C)/Z in the evaluation domain and going back to the
|
||||
/// coefficients domain. snarkjs instead precomputes the Lagrange form of the powers of tau bases
|
||||
/// in a domain twice as large and the witness map is computed as the odd coefficients of (AB-C)
|
||||
/// in that domain. This serves as HZ when computing the C proof element.
|
||||
pub struct CircomReduction;
|
||||
|
||||
impl R1CSToQAP for CircomReduction {
|
||||
#[allow(clippy::type_complexity)]
|
||||
fn instance_map_with_evaluation<F: PrimeField, D: EvaluationDomain<F>>(
|
||||
cs: ConstraintSystemRef<F>,
|
||||
t: &F,
|
||||
) -> Result<(Vec<F>, Vec<F>, Vec<F>, F, usize, usize), SynthesisError> {
|
||||
LibsnarkReduction::instance_map_with_evaluation::<F, D>(cs, t)
|
||||
}
|
||||
|
||||
fn witness_map_from_matrices<F: PrimeField, D: EvaluationDomain<F>>(
|
||||
matrices: &ConstraintMatrices<F>,
|
||||
num_inputs: usize,
|
||||
num_constraints: usize,
|
||||
full_assignment: &[F],
|
||||
) -> Result<Vec<F>, SynthesisError> {
|
||||
let zero = F::zero();
|
||||
let domain =
|
||||
D::new(num_constraints + num_inputs).ok_or(SynthesisError::PolynomialDegreeTooLarge)?;
|
||||
let domain_size = domain.size();
|
||||
|
||||
let mut a = vec![zero; domain_size];
|
||||
let mut b = vec![zero; domain_size];
|
||||
|
||||
#[allow(unexpected_cfgs)]
|
||||
cfg_iter_mut!(a[..num_constraints])
|
||||
.zip(cfg_iter_mut!(b[..num_constraints]))
|
||||
.zip(cfg_iter!(&matrices.a))
|
||||
.zip(cfg_iter!(&matrices.b))
|
||||
.for_each(|(((a, b), at_i), bt_i)| {
|
||||
*a = evaluate_constraint(at_i, full_assignment);
|
||||
*b = evaluate_constraint(bt_i, full_assignment);
|
||||
});
|
||||
|
||||
{
|
||||
let start = num_constraints;
|
||||
let end = start + num_inputs;
|
||||
a[start..end].clone_from_slice(&full_assignment[..num_inputs]);
|
||||
}
|
||||
|
||||
let mut c = vec![zero; domain_size];
|
||||
#[allow(unexpected_cfgs)]
|
||||
cfg_iter_mut!(c[..num_constraints])
|
||||
.zip(&a)
|
||||
.zip(&b)
|
||||
.for_each(|((c_i, &a), &b)| {
|
||||
*c_i = a * b;
|
||||
});
|
||||
|
||||
domain.ifft_in_place(&mut a);
|
||||
domain.ifft_in_place(&mut b);
|
||||
|
||||
let root_of_unity = {
|
||||
let domain_size_double = 2 * domain_size;
|
||||
let domain_double =
|
||||
D::new(domain_size_double).ok_or(SynthesisError::PolynomialDegreeTooLarge)?;
|
||||
domain_double.element(1)
|
||||
};
|
||||
D::distribute_powers_and_mul_by_const(&mut a, root_of_unity, F::one());
|
||||
D::distribute_powers_and_mul_by_const(&mut b, root_of_unity, F::one());
|
||||
|
||||
domain.fft_in_place(&mut a);
|
||||
domain.fft_in_place(&mut b);
|
||||
|
||||
let mut ab = domain.mul_polynomials_in_evaluation_domain(&a, &b);
|
||||
drop(a);
|
||||
drop(b);
|
||||
|
||||
domain.ifft_in_place(&mut c);
|
||||
D::distribute_powers_and_mul_by_const(&mut c, root_of_unity, F::one());
|
||||
domain.fft_in_place(&mut c);
|
||||
|
||||
#[allow(unexpected_cfgs)]
|
||||
cfg_iter_mut!(ab)
|
||||
.zip(c)
|
||||
.for_each(|(ab_i, c_i)| *ab_i -= &c_i);
|
||||
|
||||
Ok(ab)
|
||||
}
|
||||
|
||||
fn h_query_scalars<F: PrimeField, D: EvaluationDomain<F>>(
|
||||
max_power: usize,
|
||||
t: F,
|
||||
_: F,
|
||||
delta_inverse: F,
|
||||
) -> Result<Vec<F>, SynthesisError> {
|
||||
// the usual H query has domain-1 powers. Z has domain powers. So HZ has 2*domain-1 powers.
|
||||
#[allow(unexpected_cfgs)]
|
||||
let mut scalars = cfg_into_iter!(0..2 * max_power + 1)
|
||||
.map(|i| delta_inverse * t.pow([i as u64]))
|
||||
.collect::<Vec<_>>();
|
||||
let domain_size = scalars.len();
|
||||
let domain = D::new(domain_size).ok_or(SynthesisError::PolynomialDegreeTooLarge)?;
|
||||
// generate the lagrange coefficients
|
||||
domain.ifft_in_place(&mut scalars);
|
||||
#[allow(unexpected_cfgs)]
|
||||
Ok(cfg_into_iter!(scalars).skip(1).step_by(2).collect())
|
||||
}
|
||||
}
|
||||
79
rln/src/error.rs
Normal file
79
rln/src/error.rs
Normal file
@@ -0,0 +1,79 @@
|
||||
use crate::circuit::error::ZKeyReadError;
|
||||
use ark_bn254::Fr;
|
||||
use ark_relations::r1cs::SynthesisError;
|
||||
use ark_serialize::SerializationError;
|
||||
use num_bigint::{BigInt, ParseBigIntError};
|
||||
use std::array::TryFromSliceError;
|
||||
use std::num::TryFromIntError;
|
||||
use std::string::FromUtf8Error;
|
||||
use thiserror::Error;
|
||||
use utils::error::{FromConfigError, ZerokitMerkleTreeError};
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum ConversionError {
|
||||
#[error("Expected radix 10 or 16")]
|
||||
WrongRadix,
|
||||
#[error("{0}")]
|
||||
ParseBigInt(#[from] ParseBigIntError),
|
||||
#[error("{0}")]
|
||||
ToUsize(#[from] TryFromIntError),
|
||||
#[error("{0}")]
|
||||
FromSlice(#[from] TryFromSliceError),
|
||||
#[error("Input data too short: expected at least {expected} bytes, got {actual} bytes")]
|
||||
InsufficientData { expected: usize, actual: usize },
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum ProofError {
|
||||
#[error("{0}")]
|
||||
ProtocolError(#[from] ProtocolError),
|
||||
#[error("Error producing proof: {0}")]
|
||||
SynthesisError(#[from] SynthesisError),
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum ProtocolError {
|
||||
#[error("{0}")]
|
||||
Conversion(#[from] ConversionError),
|
||||
#[error("Expected to read {0} bytes but read only {1} bytes")]
|
||||
InvalidReadLen(usize, usize),
|
||||
#[error("Cannot convert bigint {0:?} to biguint")]
|
||||
BigUintConversion(BigInt),
|
||||
#[error("{0}")]
|
||||
JsonError(#[from] serde_json::Error),
|
||||
#[error("Message id ({0}) is not within user_message_limit ({1})")]
|
||||
InvalidMessageId(Fr, Fr),
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum ComputeIdSecretError {
|
||||
/// Usually it means that the same signal is used to recover the user secret hash
|
||||
#[error("Cannot recover secret: division by zero")]
|
||||
DivisionByZero,
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum RLNError {
|
||||
#[error("I/O error: {0}")]
|
||||
IO(#[from] std::io::Error),
|
||||
#[error("Utf8 error: {0}")]
|
||||
Utf8(#[from] FromUtf8Error),
|
||||
#[error("Serde json error: {0}")]
|
||||
JSON(#[from] serde_json::Error),
|
||||
#[error("Config error: {0}")]
|
||||
Config(#[from] FromConfigError),
|
||||
#[error("Serialization error: {0}")]
|
||||
Serialization(#[from] SerializationError),
|
||||
#[error("Merkle tree error: {0}")]
|
||||
MerkleTree(#[from] ZerokitMerkleTreeError),
|
||||
#[error("ZKey error: {0}")]
|
||||
ZKey(#[from] ZKeyReadError),
|
||||
#[error("Conversion error: {0}")]
|
||||
Conversion(#[from] ConversionError),
|
||||
#[error("Protocol error: {0}")]
|
||||
Protocol(#[from] ProtocolError),
|
||||
#[error("Proof error: {0}")]
|
||||
Proof(#[from] ProofError),
|
||||
#[error("Unable to extract secret")]
|
||||
RecoverSecret(#[from] ComputeIdSecretError),
|
||||
}
|
||||
205
rln/src/ffi.rs
205
rln/src/ffi.rs
@@ -2,12 +2,18 @@
|
||||
|
||||
use std::slice;
|
||||
|
||||
use crate::public::{hash as public_hash, poseidon_hash as public_poseidon_hash, RLN};
|
||||
use crate::public::{
|
||||
extended_key_gen as public_extended_key_gen, hash as public_hash, key_gen as public_key_gen,
|
||||
poseidon_hash as public_poseidon_hash,
|
||||
seeded_extended_key_gen as public_seeded_extended_key_gen,
|
||||
seeded_key_gen as public_seeded_key_gen, RLN,
|
||||
};
|
||||
|
||||
// Macro to call methods with arbitrary amount of arguments,
|
||||
// First argument to the macro is context,
|
||||
// second is the actual method on `RLN`
|
||||
// rest are all other arguments to the method
|
||||
#[cfg(not(feature = "stateless"))]
|
||||
macro_rules! call {
|
||||
($instance:expr, $method:ident $(, $arg:expr)*) => {
|
||||
{
|
||||
@@ -79,23 +85,48 @@ macro_rules! call_with_output_arg {
|
||||
// Second argument is the output buffer argument
|
||||
// The remaining arguments are all other inputs to the method
|
||||
macro_rules! no_ctx_call_with_output_arg {
|
||||
($method:ident, $output_arg:expr, $( $arg:expr ),* ) => {
|
||||
{
|
||||
let mut output_data: Vec<u8> = Vec::new();
|
||||
match $method($($arg.process()),*, &mut output_data) {
|
||||
Ok(()) => {
|
||||
unsafe { *$output_arg = Buffer::from(&output_data[..]) };
|
||||
std::mem::forget(output_data);
|
||||
true
|
||||
}
|
||||
Err(err) => {
|
||||
std::mem::forget(output_data);
|
||||
eprintln!("execution error: {err}");
|
||||
false
|
||||
}
|
||||
($method:ident, $output_arg:expr, $input_arg:expr, $endianness_arg:expr) => {{
|
||||
let mut output_data: Vec<u8> = Vec::new();
|
||||
match $method(
|
||||
$input_arg.process(),
|
||||
&mut output_data,
|
||||
$endianness_arg.process(),
|
||||
) {
|
||||
Ok(()) => {
|
||||
unsafe { *$output_arg = Buffer::from(&output_data[..]) };
|
||||
std::mem::forget(output_data);
|
||||
true
|
||||
}
|
||||
Err(err) => {
|
||||
std::mem::forget(output_data);
|
||||
eprintln!("execution error: {err}");
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
||||
// Macro to call methods with arbitrary amount of arguments,
|
||||
// which are not implemented in a ctx RLN object
|
||||
// First argument is the method to call
|
||||
// Second argument is the output buffer argument
|
||||
// The remaining arguments are all other inputs to the method
|
||||
macro_rules! no_ctx_call_with_output_arg_and_endianness {
|
||||
($method:ident, $output_arg:expr, $endianness_arg:expr) => {{
|
||||
let mut output_data: Vec<u8> = Vec::new();
|
||||
match $method(&mut output_data, $endianness_arg.process()) {
|
||||
Ok(()) => {
|
||||
unsafe { *$output_arg = Buffer::from(&output_data[..]) };
|
||||
std::mem::forget(output_data);
|
||||
true
|
||||
}
|
||||
Err(err) => {
|
||||
std::mem::forget(output_data);
|
||||
eprintln!("execution error: {err}");
|
||||
false
|
||||
}
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
||||
// Macro to call methods with arbitrary amount of arguments,
|
||||
@@ -157,10 +188,17 @@ impl ProcessArg for *mut RLN {
|
||||
}
|
||||
}
|
||||
|
||||
/// Buffer struct is taken from
|
||||
/// <https://github.com/celo-org/celo-threshold-bls-rs/blob/master/crates/threshold-bls-ffi/src/ffi.rs>
|
||||
///
|
||||
/// Also heavily inspired by <https://github.com/kilic/rln/blob/master/src/ffi.rs>
|
||||
impl ProcessArg for bool {
|
||||
type ReturnType = bool;
|
||||
fn process(self) -> Self::ReturnType {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
///// Buffer struct is taken from
|
||||
///// <https://github.com/celo-org/celo-threshold-bls-rs/blob/master/crates/threshold-bls-ffi/src/ffi.rs>
|
||||
/////
|
||||
///// Also heavily inspired by <https://github.com/kilic/rln/blob/master/src/ffi.rs>
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
@@ -194,8 +232,8 @@ impl<'a> From<&Buffer> for &'a [u8] {
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[cfg(not(feature = "stateless"))]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn new(tree_height: usize, input_buffer: *const Buffer, ctx: *mut *mut RLN) -> bool {
|
||||
match RLN::new(tree_height, input_buffer.process()) {
|
||||
pub extern "C" fn new(tree_depth: usize, input_buffer: *const Buffer, ctx: *mut *mut RLN) -> bool {
|
||||
match RLN::new(tree_depth, input_buffer.process()) {
|
||||
Ok(rln) => {
|
||||
unsafe { *ctx = Box::into_raw(Box::new(rln)) };
|
||||
true
|
||||
@@ -227,18 +265,16 @@ pub extern "C" fn new(ctx: *mut *mut RLN) -> bool {
|
||||
#[cfg(not(feature = "stateless"))]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn new_with_params(
|
||||
tree_height: usize,
|
||||
circom_buffer: *const Buffer,
|
||||
tree_depth: usize,
|
||||
zkey_buffer: *const Buffer,
|
||||
vk_buffer: *const Buffer,
|
||||
graph_data: *const Buffer,
|
||||
tree_config: *const Buffer,
|
||||
ctx: *mut *mut RLN,
|
||||
) -> bool {
|
||||
match RLN::new_with_params(
|
||||
tree_height,
|
||||
circom_buffer.process().to_vec(),
|
||||
tree_depth,
|
||||
zkey_buffer.process().to_vec(),
|
||||
vk_buffer.process().to_vec(),
|
||||
graph_data.process().to_vec(),
|
||||
tree_config.process(),
|
||||
) {
|
||||
Ok(rln) => {
|
||||
@@ -256,15 +292,13 @@ pub extern "C" fn new_with_params(
|
||||
#[cfg(feature = "stateless")]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn new_with_params(
|
||||
circom_buffer: *const Buffer,
|
||||
zkey_buffer: *const Buffer,
|
||||
vk_buffer: *const Buffer,
|
||||
graph_buffer: *const Buffer,
|
||||
ctx: *mut *mut RLN,
|
||||
) -> bool {
|
||||
match RLN::new_with_params(
|
||||
circom_buffer.process().to_vec(),
|
||||
zkey_buffer.process().to_vec(),
|
||||
vk_buffer.process().to_vec(),
|
||||
graph_buffer.process().to_vec(),
|
||||
) {
|
||||
Ok(rln) => {
|
||||
unsafe { *ctx = Box::into_raw(Box::new(rln)) };
|
||||
@@ -283,8 +317,8 @@ pub extern "C" fn new_with_params(
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[no_mangle]
|
||||
#[cfg(not(feature = "stateless"))]
|
||||
pub extern "C" fn set_tree(ctx: *mut RLN, tree_height: usize) -> bool {
|
||||
call!(ctx, set_tree, tree_height)
|
||||
pub extern "C" fn set_tree(ctx: *mut RLN, tree_depth: usize) -> bool {
|
||||
call!(ctx, set_tree, tree_depth)
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
@@ -463,38 +497,6 @@ pub extern "C" fn verify_with_roots(
|
||||
////////////////////////////////////////////////////////
|
||||
// Utils
|
||||
////////////////////////////////////////////////////////
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn key_gen(ctx: *const RLN, output_buffer: *mut Buffer) -> bool {
|
||||
call_with_output_arg!(ctx, key_gen, output_buffer)
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn seeded_key_gen(
|
||||
ctx: *const RLN,
|
||||
input_buffer: *const Buffer,
|
||||
output_buffer: *mut Buffer,
|
||||
) -> bool {
|
||||
call_with_output_arg!(ctx, seeded_key_gen, output_buffer, input_buffer)
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn extended_key_gen(ctx: *const RLN, output_buffer: *mut Buffer) -> bool {
|
||||
call_with_output_arg!(ctx, extended_key_gen, output_buffer)
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn seeded_extended_key_gen(
|
||||
ctx: *const RLN,
|
||||
input_buffer: *const Buffer,
|
||||
output_buffer: *mut Buffer,
|
||||
) -> bool {
|
||||
call_with_output_arg!(ctx, seeded_extended_key_gen, output_buffer, input_buffer)
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn recover_id_secret(
|
||||
@@ -537,14 +539,77 @@ pub extern "C" fn flush(ctx: *mut RLN) -> bool {
|
||||
call!(ctx, flush)
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////
|
||||
// Utils APIs
|
||||
////////////////////////////////////////////////////////
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn hash(input_buffer: *const Buffer, output_buffer: *mut Buffer) -> bool {
|
||||
no_ctx_call_with_output_arg!(public_hash, output_buffer, input_buffer)
|
||||
pub extern "C" fn hash(
|
||||
input_buffer: *const Buffer,
|
||||
output_buffer: *mut Buffer,
|
||||
is_little_endian: bool,
|
||||
) -> bool {
|
||||
no_ctx_call_with_output_arg!(public_hash, output_buffer, input_buffer, is_little_endian)
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn poseidon_hash(input_buffer: *const Buffer, output_buffer: *mut Buffer) -> bool {
|
||||
no_ctx_call_with_output_arg!(public_poseidon_hash, output_buffer, input_buffer)
|
||||
pub extern "C" fn poseidon_hash(
|
||||
input_buffer: *const Buffer,
|
||||
output_buffer: *mut Buffer,
|
||||
is_little_endian: bool,
|
||||
) -> bool {
|
||||
no_ctx_call_with_output_arg!(
|
||||
public_poseidon_hash,
|
||||
output_buffer,
|
||||
input_buffer,
|
||||
is_little_endian
|
||||
)
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn key_gen(output_buffer: *mut Buffer, is_little_endian: bool) -> bool {
|
||||
no_ctx_call_with_output_arg_and_endianness!(public_key_gen, output_buffer, is_little_endian)
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn seeded_key_gen(
|
||||
input_buffer: *const Buffer,
|
||||
output_buffer: *mut Buffer,
|
||||
is_little_endian: bool,
|
||||
) -> bool {
|
||||
no_ctx_call_with_output_arg!(
|
||||
public_seeded_key_gen,
|
||||
output_buffer,
|
||||
input_buffer,
|
||||
is_little_endian
|
||||
)
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn extended_key_gen(output_buffer: *mut Buffer, is_little_endian: bool) -> bool {
|
||||
no_ctx_call_with_output_arg_and_endianness!(
|
||||
public_extended_key_gen,
|
||||
output_buffer,
|
||||
is_little_endian
|
||||
)
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn seeded_extended_key_gen(
|
||||
input_buffer: *const Buffer,
|
||||
output_buffer: *mut Buffer,
|
||||
is_little_endian: bool,
|
||||
) -> bool {
|
||||
no_ctx_call_with_output_arg!(
|
||||
public_seeded_extended_key_gen,
|
||||
output_buffer,
|
||||
input_buffer,
|
||||
is_little_endian
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
/// This crate instantiates the Poseidon hash algorithm.
|
||||
use crate::{circuit::Fr, utils::bytes_le_to_fr};
|
||||
use crate::{
|
||||
circuit::Fr,
|
||||
utils::{bytes_be_to_fr, bytes_le_to_fr},
|
||||
};
|
||||
use once_cell::sync::Lazy;
|
||||
use tiny_keccak::{Hasher, Keccak};
|
||||
use utils::poseidon::Poseidon;
|
||||
@@ -23,7 +26,7 @@ static POSEIDON: Lazy<Poseidon<Fr>> = Lazy::new(|| Poseidon::<Fr>::from(&ROUND_P
|
||||
|
||||
pub fn poseidon_hash(input: &[Fr]) -> Fr {
|
||||
POSEIDON
|
||||
.hash(input.to_vec())
|
||||
.hash(input)
|
||||
.expect("hash with fixed input size can't fail")
|
||||
}
|
||||
|
||||
@@ -45,7 +48,7 @@ impl utils::merkle_tree::Hasher for PoseidonHash {
|
||||
}
|
||||
|
||||
/// Hashes arbitrary signal to the underlying prime field.
|
||||
pub fn hash_to_field(signal: &[u8]) -> Fr {
|
||||
pub fn hash_to_field_le(signal: &[u8]) -> Fr {
|
||||
// We hash the input signal using Keccak256
|
||||
let mut hash = [0; 32];
|
||||
let mut hasher = Keccak::v256();
|
||||
@@ -56,3 +59,19 @@ pub fn hash_to_field(signal: &[u8]) -> Fr {
|
||||
let (el, _) = bytes_le_to_fr(hash.as_ref());
|
||||
el
|
||||
}
|
||||
|
||||
/// Hashes arbitrary signal to the underlying prime field.
|
||||
pub fn hash_to_field_be(signal: &[u8]) -> Fr {
|
||||
// We hash the input signal using Keccak256
|
||||
let mut hash = [0; 32];
|
||||
let mut hasher = Keccak::v256();
|
||||
hasher.update(signal);
|
||||
hasher.finalize(&mut hash);
|
||||
|
||||
// Reverse the bytes to get big endian representation
|
||||
hash.reverse();
|
||||
|
||||
// We export the hash as a field element
|
||||
let (el, _) = bytes_be_to_fr(hash.as_ref());
|
||||
el
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
pub mod circuit;
|
||||
pub mod error;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub mod ffi;
|
||||
pub mod hashers;
|
||||
#[cfg(feature = "pmtree-ft")]
|
||||
pub mod pm_tree_adapter;
|
||||
#[cfg(not(feature = "stateless"))]
|
||||
pub mod poseidon_tree;
|
||||
pub mod protocol;
|
||||
pub mod public;
|
||||
@@ -11,5 +13,23 @@ pub mod public;
|
||||
pub mod public_api_tests;
|
||||
pub mod utils;
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub mod ffi;
|
||||
// Ensure that only one Merkle tree feature is enabled at a time
|
||||
#[cfg(any(
|
||||
all(feature = "fullmerkletree", feature = "optimalmerkletree"),
|
||||
all(feature = "fullmerkletree", feature = "pmtree-ft"),
|
||||
all(feature = "optimalmerkletree", feature = "pmtree-ft"),
|
||||
))]
|
||||
compile_error!(
|
||||
"Only one of `fullmerkletree`, `optimalmerkletree`, or `pmtree-ft` can be enabled at a time."
|
||||
);
|
||||
|
||||
// Ensure that the `stateless` feature is not enabled with any Merkle tree features
|
||||
#[cfg(all(
|
||||
feature = "stateless",
|
||||
any(
|
||||
feature = "fullmerkletree",
|
||||
feature = "optimalmerkletree",
|
||||
feature = "pmtree-ft"
|
||||
)
|
||||
))]
|
||||
compile_error!("Cannot enable any Merkle tree features with stateless");
|
||||
|
||||
@@ -1,17 +1,16 @@
|
||||
use serde_json::Value;
|
||||
use std::fmt::Debug;
|
||||
use std::path::PathBuf;
|
||||
use std::str::FromStr;
|
||||
|
||||
use color_eyre::{Report, Result};
|
||||
use serde_json::Value;
|
||||
|
||||
use utils::pmtree::tree::Key;
|
||||
use utils::pmtree::{Database, Hasher};
|
||||
use utils::*;
|
||||
use tempfile::Builder;
|
||||
|
||||
use crate::circuit::Fr;
|
||||
use crate::hashers::{poseidon_hash, PoseidonHash};
|
||||
use crate::utils::{bytes_le_to_fr, fr_to_bytes_le};
|
||||
use utils::error::{FromConfigError, ZerokitMerkleTreeError};
|
||||
use utils::pmtree::tree::Key;
|
||||
use utils::pmtree::{Database, Hasher, PmtreeErrorKind};
|
||||
use utils::{pmtree, Config, Mode, SledDB, ZerokitMerkleProof, ZerokitMerkleTree};
|
||||
|
||||
const METADATA_KEY: [u8; 8] = *b"metadata";
|
||||
|
||||
@@ -52,20 +51,104 @@ impl Hasher for PoseidonHash {
|
||||
}
|
||||
}
|
||||
|
||||
fn get_tmp_path() -> PathBuf {
|
||||
std::env::temp_dir().join(format!("pmtree-{}", rand::random::<u64>()))
|
||||
fn default_tmp_path() -> PathBuf {
|
||||
Builder::new()
|
||||
.prefix("pmtree-")
|
||||
.tempfile()
|
||||
.expect("Failed to create temp file")
|
||||
.into_temp_path()
|
||||
.to_path_buf()
|
||||
}
|
||||
|
||||
fn get_tmp() -> bool {
|
||||
true
|
||||
const DEFAULT_TEMPORARY: bool = true;
|
||||
const DEFAULT_CACHE_CAPACITY: u64 = 1073741824; // 1 Gigabyte
|
||||
const DEFAULT_FLUSH_EVERY_MS: u64 = 500; // 500 Milliseconds
|
||||
const DEFAULT_MODE: Mode = Mode::HighThroughput;
|
||||
const DEFAULT_USE_COMPRESSION: bool = false;
|
||||
|
||||
pub struct PmtreeConfigBuilder {
|
||||
path: Option<PathBuf>,
|
||||
temporary: bool,
|
||||
cache_capacity: u64,
|
||||
flush_every_ms: u64,
|
||||
mode: Mode,
|
||||
use_compression: bool,
|
||||
}
|
||||
|
||||
impl PmtreeConfigBuilder {
|
||||
fn new() -> Self {
|
||||
PmtreeConfigBuilder {
|
||||
path: None,
|
||||
temporary: DEFAULT_TEMPORARY,
|
||||
cache_capacity: DEFAULT_CACHE_CAPACITY,
|
||||
flush_every_ms: DEFAULT_FLUSH_EVERY_MS,
|
||||
mode: DEFAULT_MODE,
|
||||
use_compression: DEFAULT_USE_COMPRESSION,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn path<P: Into<PathBuf>>(mut self, path: P) -> Self {
|
||||
self.path = Some(path.into());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn temporary(mut self, temporary: bool) -> Self {
|
||||
self.temporary = temporary;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn cache_capacity(mut self, capacity: u64) -> Self {
|
||||
self.cache_capacity = capacity;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn flush_every_ms(mut self, ms: u64) -> Self {
|
||||
self.flush_every_ms = ms;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn mode(mut self, mode: Mode) -> Self {
|
||||
self.mode = mode;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn use_compression(mut self, compression: bool) -> Self {
|
||||
self.use_compression = compression;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn build(self) -> Result<PmtreeConfig, FromConfigError> {
|
||||
let path = match (self.temporary, self.path) {
|
||||
(true, None) => default_tmp_path(),
|
||||
(false, None) => return Err(FromConfigError::MissingPath),
|
||||
(true, Some(path)) if path.exists() => return Err(FromConfigError::PathExists),
|
||||
(_, Some(path)) => path,
|
||||
};
|
||||
|
||||
let config = Config::new()
|
||||
.temporary(self.temporary)
|
||||
.path(path)
|
||||
.cache_capacity(self.cache_capacity)
|
||||
.flush_every_ms(Some(self.flush_every_ms))
|
||||
.mode(self.mode)
|
||||
.use_compression(self.use_compression);
|
||||
|
||||
Ok(PmtreeConfig(config))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PmtreeConfig(Config);
|
||||
|
||||
impl FromStr for PmtreeConfig {
|
||||
type Err = Report;
|
||||
impl PmtreeConfig {
|
||||
pub fn builder() -> PmtreeConfigBuilder {
|
||||
PmtreeConfigBuilder::new()
|
||||
}
|
||||
}
|
||||
|
||||
fn from_str(s: &str) -> Result<Self> {
|
||||
impl FromStr for PmtreeConfig {
|
||||
type Err = FromConfigError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let config: Value = serde_json::from_str(s)?;
|
||||
|
||||
let path = config["path"].as_str();
|
||||
@@ -80,21 +163,16 @@ impl FromStr for PmtreeConfig {
|
||||
};
|
||||
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()
|
||||
)));
|
||||
if let (Some(true), Some(path)) = (temporary, path.as_ref()) {
|
||||
if path.exists() {
|
||||
return Err(FromConfigError::PathExists);
|
||||
}
|
||||
}
|
||||
|
||||
let config = Config::new()
|
||||
.temporary(temporary.unwrap_or(get_tmp()))
|
||||
.path(path.unwrap_or(get_tmp_path()))
|
||||
.cache_capacity(cache_capacity.unwrap_or(1024 * 1024 * 1024))
|
||||
.temporary(temporary.unwrap_or(DEFAULT_TEMPORARY))
|
||||
.path(path.unwrap_or(default_tmp_path()))
|
||||
.cache_capacity(cache_capacity.unwrap_or(DEFAULT_CACHE_CAPACITY))
|
||||
.flush_every_ms(flush_every_ms)
|
||||
.mode(mode)
|
||||
.use_compression(use_compression.unwrap_or(false));
|
||||
@@ -104,16 +182,9 @@ impl FromStr for PmtreeConfig {
|
||||
|
||||
impl Default for PmtreeConfig {
|
||||
fn default() -> Self {
|
||||
let tmp_path = get_tmp_path();
|
||||
PmtreeConfig(
|
||||
Config::new()
|
||||
.temporary(true)
|
||||
.path(tmp_path)
|
||||
.cache_capacity(150_000)
|
||||
.mode(Mode::HighThroughput)
|
||||
.use_compression(false)
|
||||
.flush_every_ms(Some(12_000)),
|
||||
)
|
||||
Self::builder()
|
||||
.build()
|
||||
.expect("Default configuration should never fail")
|
||||
}
|
||||
}
|
||||
impl Debug for PmtreeConfig {
|
||||
@@ -133,12 +204,16 @@ impl ZerokitMerkleTree for PmTree {
|
||||
type Hasher = PoseidonHash;
|
||||
type Config = PmtreeConfig;
|
||||
|
||||
fn default(depth: usize) -> Result<Self> {
|
||||
fn default(depth: usize) -> Result<Self, ZerokitMerkleTreeError> {
|
||||
let default_config = PmtreeConfig::default();
|
||||
PmTree::new(depth, Self::Hasher::default_leaf(), default_config)
|
||||
}
|
||||
|
||||
fn new(depth: usize, _default_leaf: FrOf<Self::Hasher>, config: Self::Config) -> Result<Self> {
|
||||
fn new(
|
||||
depth: usize,
|
||||
_default_leaf: FrOf<Self::Hasher>,
|
||||
config: Self::Config,
|
||||
) -> Result<Self, ZerokitMerkleTreeError> {
|
||||
let tree_loaded = pmtree::MerkleTree::load(config.clone().0);
|
||||
let tree = match tree_loaded {
|
||||
Ok(tree) => tree,
|
||||
@@ -168,14 +243,12 @@ impl ZerokitMerkleTree for PmTree {
|
||||
self.tree.root()
|
||||
}
|
||||
|
||||
fn compute_root(&mut self) -> Result<FrOf<Self::Hasher>> {
|
||||
Ok(self.tree.root())
|
||||
}
|
||||
|
||||
fn set(&mut self, index: usize, leaf: FrOf<Self::Hasher>) -> Result<()> {
|
||||
self.tree
|
||||
.set(index, leaf)
|
||||
.map_err(|e| Report::msg(e.to_string()))?;
|
||||
fn set(
|
||||
&mut self,
|
||||
index: usize,
|
||||
leaf: FrOf<Self::Hasher>,
|
||||
) -> Result<(), ZerokitMerkleTreeError> {
|
||||
self.tree.set(index, leaf)?;
|
||||
self.cached_leaves_indices[index] = 1;
|
||||
Ok(())
|
||||
}
|
||||
@@ -184,27 +257,31 @@ impl ZerokitMerkleTree for PmTree {
|
||||
&mut self,
|
||||
start: usize,
|
||||
values: I,
|
||||
) -> Result<()> {
|
||||
) -> Result<(), ZerokitMerkleTreeError> {
|
||||
let v = values.into_iter().collect::<Vec<_>>();
|
||||
self.tree
|
||||
.set_range(start, v.clone().into_iter())
|
||||
.map_err(|e| Report::msg(e.to_string()))?;
|
||||
self.tree.set_range(start, v.clone().into_iter())?;
|
||||
for i in start..v.len() {
|
||||
self.cached_leaves_indices[i] = 1
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get(&self, index: usize) -> Result<FrOf<Self::Hasher>> {
|
||||
self.tree.get(index).map_err(|e| Report::msg(e.to_string()))
|
||||
fn get(&self, index: usize) -> Result<FrOf<Self::Hasher>, ZerokitMerkleTreeError> {
|
||||
self.tree
|
||||
.get(index)
|
||||
.map_err(ZerokitMerkleTreeError::PmtreeErrorKind)
|
||||
}
|
||||
|
||||
fn get_subtree_root(&self, n: usize, index: usize) -> Result<FrOf<Self::Hasher>> {
|
||||
fn get_subtree_root(
|
||||
&self,
|
||||
n: usize,
|
||||
index: usize,
|
||||
) -> Result<FrOf<Self::Hasher>, ZerokitMerkleTreeError> {
|
||||
if n > self.depth() {
|
||||
return Err(Report::msg("level exceeds depth size"));
|
||||
return Err(ZerokitMerkleTreeError::InvalidLevel);
|
||||
}
|
||||
if index >= self.capacity() {
|
||||
return Err(Report::msg("index exceeds set size"));
|
||||
return Err(ZerokitMerkleTreeError::InvalidLeaf);
|
||||
}
|
||||
if n == 0 {
|
||||
Ok(self.root())
|
||||
@@ -235,55 +312,71 @@ impl ZerokitMerkleTree for PmTree {
|
||||
start: usize,
|
||||
leaves: I,
|
||||
indices: J,
|
||||
) -> Result<()> {
|
||||
) -> Result<(), ZerokitMerkleTreeError> {
|
||||
let leaves = leaves.into_iter().collect::<Vec<_>>();
|
||||
let mut indices = indices.into_iter().collect::<Vec<_>>();
|
||||
indices.sort();
|
||||
|
||||
match (leaves.len(), indices.len()) {
|
||||
(0, 0) => Err(Report::msg("no leaves or indices to be removed")),
|
||||
(0, 0) => Err(ZerokitMerkleTreeError::InvalidLeaf),
|
||||
(1, 0) => self.set(start, leaves[0]),
|
||||
(0, 1) => self.delete(indices[0]),
|
||||
(_, 0) => self.set_range(start, leaves),
|
||||
(0, _) => self.remove_indices(&indices),
|
||||
(_, _) => self.remove_indices_and_set_leaves(start, leaves, &indices),
|
||||
(_, 0) => self.set_range(start, leaves.into_iter()),
|
||||
(0, _) => self
|
||||
.remove_indices(&indices)
|
||||
.map_err(ZerokitMerkleTreeError::PmtreeErrorKind),
|
||||
(_, _) => self
|
||||
.remove_indices_and_set_leaves(start, leaves, &indices)
|
||||
.map_err(ZerokitMerkleTreeError::PmtreeErrorKind),
|
||||
}
|
||||
}
|
||||
|
||||
fn update_next(&mut self, leaf: FrOf<Self::Hasher>) -> Result<()> {
|
||||
fn update_next(&mut self, leaf: FrOf<Self::Hasher>) -> Result<(), ZerokitMerkleTreeError> {
|
||||
self.tree
|
||||
.update_next(leaf)
|
||||
.map_err(|e| Report::msg(e.to_string()))
|
||||
.map_err(ZerokitMerkleTreeError::PmtreeErrorKind)
|
||||
}
|
||||
|
||||
fn delete(&mut self, index: usize) -> Result<()> {
|
||||
/// Delete a leaf in the merkle tree given its index
|
||||
///
|
||||
/// Deleting a leaf is done by resetting it to its default value. Note that the next_index field
|
||||
/// will not be changed (== previously used index cannot be reused - this to avoid replay
|
||||
/// attacks or unexpected and very hard to tackle issues)
|
||||
fn delete(&mut self, index: usize) -> Result<(), ZerokitMerkleTreeError> {
|
||||
self.tree
|
||||
.delete(index)
|
||||
.map_err(|e| Report::msg(e.to_string()))?;
|
||||
.map_err(ZerokitMerkleTreeError::PmtreeErrorKind)?;
|
||||
self.cached_leaves_indices[index] = 0;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn proof(&self, index: usize) -> Result<Self::Proof> {
|
||||
fn proof(&self, index: usize) -> Result<Self::Proof, ZerokitMerkleTreeError> {
|
||||
let proof = self.tree.proof(index)?;
|
||||
Ok(PmTreeProof { proof })
|
||||
}
|
||||
|
||||
fn verify(&self, leaf: &FrOf<Self::Hasher>, witness: &Self::Proof) -> Result<bool> {
|
||||
fn verify(
|
||||
&self,
|
||||
leaf: &FrOf<Self::Hasher>,
|
||||
witness: &Self::Proof,
|
||||
) -> Result<bool, ZerokitMerkleTreeError> {
|
||||
if self.tree.verify(leaf, &witness.proof) {
|
||||
Ok(true)
|
||||
} else {
|
||||
Err(Report::msg("verify failed"))
|
||||
Err(ZerokitMerkleTreeError::InvalidWitness)
|
||||
}
|
||||
}
|
||||
|
||||
fn set_metadata(&mut self, metadata: &[u8]) -> Result<()> {
|
||||
self.tree.db.put(METADATA_KEY, metadata.to_vec())?;
|
||||
fn set_metadata(&mut self, metadata: &[u8]) -> Result<(), ZerokitMerkleTreeError> {
|
||||
self.tree
|
||||
.db
|
||||
.put(METADATA_KEY, metadata.to_vec())
|
||||
.map_err(ZerokitMerkleTreeError::PmtreeErrorKind)?;
|
||||
self.metadata = metadata.to_vec();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn metadata(&self) -> Result<Vec<u8>> {
|
||||
fn metadata(&self) -> Result<Vec<u8>, ZerokitMerkleTreeError> {
|
||||
if !self.metadata.is_empty() {
|
||||
return Ok(self.metadata.clone());
|
||||
}
|
||||
@@ -297,8 +390,11 @@ impl ZerokitMerkleTree for PmTree {
|
||||
Ok(data.unwrap())
|
||||
}
|
||||
|
||||
fn close_db_connection(&mut self) -> Result<()> {
|
||||
self.tree.db.close().map_err(|e| Report::msg(e.to_string()))
|
||||
fn close_db_connection(&mut self) -> Result<(), ZerokitMerkleTreeError> {
|
||||
self.tree
|
||||
.db
|
||||
.close()
|
||||
.map_err(ZerokitMerkleTreeError::PmtreeErrorKind)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -306,15 +402,13 @@ type PmTreeHasher = <PmTree as ZerokitMerkleTree>::Hasher;
|
||||
type FrOfPmTreeHasher = FrOf<PmTreeHasher>;
|
||||
|
||||
impl PmTree {
|
||||
fn remove_indices(&mut self, indices: &[usize]) -> Result<()> {
|
||||
fn remove_indices(&mut self, indices: &[usize]) -> Result<(), PmtreeErrorKind> {
|
||||
let start = indices[0];
|
||||
let end = indices.last().unwrap() + 1;
|
||||
|
||||
let new_leaves = (start..end).map(|_| PmTreeHasher::default_leaf());
|
||||
|
||||
self.tree
|
||||
.set_range(start, new_leaves)
|
||||
.map_err(|e| Report::msg(e.to_string()))?;
|
||||
self.tree.set_range(start, new_leaves)?;
|
||||
|
||||
for i in start..end {
|
||||
self.cached_leaves_indices[i] = 0
|
||||
@@ -327,7 +421,7 @@ impl PmTree {
|
||||
start: usize,
|
||||
leaves: Vec<FrOfPmTreeHasher>,
|
||||
indices: &[usize],
|
||||
) -> Result<()> {
|
||||
) -> Result<(), PmtreeErrorKind> {
|
||||
let min_index = *indices.first().unwrap();
|
||||
let max_index = start + leaves.len();
|
||||
|
||||
@@ -344,9 +438,7 @@ impl PmTree {
|
||||
set_values[start - min_index + i] = leaf;
|
||||
}
|
||||
|
||||
self.tree
|
||||
.set_range(start, set_values)
|
||||
.map_err(|e| Report::msg(e.to_string()))?;
|
||||
self.tree.set_range(start, set_values)?;
|
||||
|
||||
for i in indices {
|
||||
self.cached_leaves_indices[*i] = 0;
|
||||
@@ -382,3 +474,32 @@ impl ZerokitMerkleProof for PmTreeProof {
|
||||
self.proof.compute_root_from(leaf)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_pmtree_json_config() {
|
||||
let json = r#"
|
||||
{
|
||||
"path": "pmtree-123456",
|
||||
"temporary": false,
|
||||
"cache_capacity": 1073741824,
|
||||
"flush_every_ms": 500,
|
||||
"mode": "HighThroughput",
|
||||
"use_compression": false
|
||||
}"#;
|
||||
|
||||
let _: PmtreeConfig = json.parse().expect("Failed to parse JSON config");
|
||||
|
||||
let _ = PmtreeConfig::builder()
|
||||
.path(default_tmp_path())
|
||||
.temporary(DEFAULT_TEMPORARY)
|
||||
.cache_capacity(DEFAULT_CACHE_CAPACITY)
|
||||
.mode(DEFAULT_MODE)
|
||||
.use_compression(DEFAULT_USE_COMPRESSION)
|
||||
.build()
|
||||
.expect("Failed to build config");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,30 +1,32 @@
|
||||
// This crate defines the RLN module default Merkle tree implementation and its Hasher
|
||||
// Implementation inspired by https://github.com/worldcoin/semaphore-rs/blob/d462a4372f1fd9c27610f2acfe4841fab1d396aa/src/poseidon_tree.rs
|
||||
|
||||
// Implementation inspired by https://github.com/worldcoin/semaphore-rs/blob/d462a4372f1fd9c27610f2acfe4841fab1d396aa/src/poseidon_tree.rs (no differences)
|
||||
#![cfg(not(feature = "stateless"))]
|
||||
|
||||
use cfg_if::cfg_if;
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "pmtree-ft")] {
|
||||
use crate::pm_tree_adapter::*;
|
||||
} else {
|
||||
use crate::hashers::{PoseidonHash};
|
||||
use utils::merkle_tree::*;
|
||||
}
|
||||
}
|
||||
|
||||
// The zerokit RLN default Merkle tree implementation is the OptimalMerkleTree.
|
||||
// To switch to FullMerkleTree implementation, it is enough to enable the fullmerkletree feature
|
||||
// The zerokit RLN default Merkle tree implementation is the PMTree from the vacp2p_pmtree crate
|
||||
// To switch to FullMerkleTree or OptimalMerkleTree, enable the corresponding feature in the Cargo.toml file
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "fullmerkletree")] {
|
||||
use utils::{FullMerkleTree, FullMerkleProof};
|
||||
use crate::hashers::PoseidonHash;
|
||||
|
||||
pub type PoseidonTree = FullMerkleTree<PoseidonHash>;
|
||||
pub type MerkleProof = FullMerkleProof<PoseidonHash>;
|
||||
} else if #[cfg(feature = "pmtree-ft")] {
|
||||
pub type PoseidonTree = PmTree;
|
||||
pub type MerkleProof = PmTreeProof;
|
||||
} else {
|
||||
} else if #[cfg(feature = "optimalmerkletree")] {
|
||||
use utils::{OptimalMerkleTree, OptimalMerkleProof};
|
||||
use crate::hashers::PoseidonHash;
|
||||
|
||||
pub type PoseidonTree = OptimalMerkleTree<PoseidonHash>;
|
||||
pub type MerkleProof = OptimalMerkleProof<PoseidonHash>;
|
||||
} else if #[cfg(feature = "pmtree-ft")] {
|
||||
use crate::pm_tree_adapter::{PmTree, PmTreeProof};
|
||||
|
||||
pub type PoseidonTree = PmTree;
|
||||
pub type MerkleProof = PmTreeProof;
|
||||
} else {
|
||||
compile_error!("One of the features `fullmerkletree`, `optimalmerkletree`, or `pmtree-ft` must be enabled.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,32 +1,35 @@
|
||||
// This crate collects all the underlying primitives used to implement RLN
|
||||
|
||||
use ark_circom::{CircomReduction, WitnessCalculator};
|
||||
#[cfg(not(feature = "stateless"))]
|
||||
use {
|
||||
crate::error::ConversionError,
|
||||
crate::poseidon_tree::PoseidonTree,
|
||||
utils::{ZerokitMerkleProof, ZerokitMerkleTree},
|
||||
};
|
||||
|
||||
use crate::circuit::{calculate_rln_witness, qap::CircomReduction, Curve};
|
||||
use crate::error::{ComputeIdSecretError, ProofError, ProtocolError};
|
||||
use crate::hashers::{hash_to_field_le, poseidon_hash};
|
||||
use crate::public::RLN_IDENTIFIER;
|
||||
use crate::utils::{
|
||||
bytes_be_to_fr, bytes_le_to_fr, bytes_le_to_vec_fr, bytes_le_to_vec_u8, fr_byte_size,
|
||||
fr_to_bytes_le, normalize_usize_le, to_bigint, vec_fr_to_bytes_le, vec_u8_to_bytes_le,
|
||||
FrOrSecret, IdSecret,
|
||||
};
|
||||
use ark_bn254::{Fr, FrConfig};
|
||||
use ark_ff::{AdditiveGroup, Fp, MontBackend};
|
||||
use ark_groth16::{prepare_verifying_key, Groth16, Proof as ArkProof, ProvingKey, VerifyingKey};
|
||||
use ark_relations::r1cs::ConstraintMatrices;
|
||||
use ark_relations::r1cs::SynthesisError;
|
||||
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
|
||||
use ark_std::{rand::thread_rng, UniformRand};
|
||||
use color_eyre::{Report, Result};
|
||||
use num_bigint::BigInt;
|
||||
use rand::{Rng, SeedableRng};
|
||||
use rand_chacha::ChaCha20Rng;
|
||||
use serde::{Deserialize, Serialize};
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use std::sync::Mutex;
|
||||
#[cfg(debug_assertions)]
|
||||
#[cfg(test)]
|
||||
use std::time::Instant;
|
||||
use thiserror::Error;
|
||||
use tiny_keccak::{Hasher as _, Keccak};
|
||||
|
||||
use crate::circuit::{Curve, Fr};
|
||||
use crate::hashers::hash_to_field;
|
||||
use crate::hashers::poseidon_hash;
|
||||
use crate::poseidon_tree::*;
|
||||
use crate::public::RLN_IDENTIFIER;
|
||||
use crate::utils::*;
|
||||
use cfg_if::cfg_if;
|
||||
use utils::{ZerokitMerkleProof, ZerokitMerkleTree};
|
||||
|
||||
use zeroize::Zeroize;
|
||||
///////////////////////////////////////////////////////
|
||||
// RLN Witness data structure and utility functions
|
||||
///////////////////////////////////////////////////////
|
||||
@@ -34,7 +37,7 @@ use utils::{ZerokitMerkleProof, ZerokitMerkleTree};
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct RLNWitnessInput {
|
||||
#[serde(serialize_with = "ark_se", deserialize_with = "ark_de")]
|
||||
identity_secret: Fr,
|
||||
identity_secret: IdSecret,
|
||||
#[serde(serialize_with = "ark_se", deserialize_with = "ark_de")]
|
||||
user_message_limit: Fr,
|
||||
#[serde(serialize_with = "ark_se", deserialize_with = "ark_de")]
|
||||
@@ -69,14 +72,21 @@ pub fn deserialize_field_element(serialized: Vec<u8>) -> Fr {
|
||||
element
|
||||
}
|
||||
|
||||
pub fn deserialize_identity_pair(serialized: Vec<u8>) -> (Fr, Fr) {
|
||||
pub fn deserialize_identity_pair_le(serialized: Vec<u8>) -> (Fr, Fr) {
|
||||
let (identity_secret_hash, read) = bytes_le_to_fr(&serialized);
|
||||
let (id_commitment, _) = bytes_le_to_fr(&serialized[read..]);
|
||||
|
||||
(identity_secret_hash, id_commitment)
|
||||
}
|
||||
|
||||
pub fn deserialize_identity_tuple(serialized: Vec<u8>) -> (Fr, Fr, Fr, Fr) {
|
||||
pub fn deserialize_identity_pair_be(serialized: Vec<u8>) -> (Fr, Fr) {
|
||||
let (identity_secret_hash, read) = bytes_be_to_fr(&serialized);
|
||||
let (id_commitment, _) = bytes_be_to_fr(&serialized[read..]);
|
||||
|
||||
(identity_secret_hash, id_commitment)
|
||||
}
|
||||
|
||||
pub fn deserialize_identity_tuple_le(serialized: Vec<u8>) -> (Fr, Fr, Fr, Fr) {
|
||||
let mut all_read = 0;
|
||||
|
||||
let (identity_trapdoor, read) = bytes_le_to_fr(&serialized[all_read..]);
|
||||
@@ -98,23 +108,53 @@ pub fn deserialize_identity_tuple(serialized: Vec<u8>) -> (Fr, Fr, Fr, Fr) {
|
||||
)
|
||||
}
|
||||
|
||||
pub fn deserialize_identity_tuple_be(serialized: Vec<u8>) -> (Fr, Fr, Fr, Fr) {
|
||||
let mut all_read = 0;
|
||||
|
||||
let (identity_trapdoor, read) = bytes_be_to_fr(&serialized[all_read..]);
|
||||
all_read += read;
|
||||
|
||||
let (identity_nullifier, read) = bytes_be_to_fr(&serialized[all_read..]);
|
||||
all_read += read;
|
||||
|
||||
let (identity_secret_hash, read) = bytes_be_to_fr(&serialized[all_read..]);
|
||||
all_read += read;
|
||||
|
||||
let (identity_commitment, _) = bytes_be_to_fr(&serialized[all_read..]);
|
||||
|
||||
(
|
||||
identity_trapdoor,
|
||||
identity_nullifier,
|
||||
identity_secret_hash,
|
||||
identity_commitment,
|
||||
)
|
||||
}
|
||||
|
||||
/// Serializes witness
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns an error if `rln_witness.message_id` is not within `rln_witness.user_message_limit`.
|
||||
pub fn serialize_witness(rln_witness: &RLNWitnessInput) -> Result<Vec<u8>> {
|
||||
/// input data is [ identity_secret<32> | user_message_limit<32> | message_id<32> | path_elements<32> | identity_path_index<8> | x<32> | external_nullifier<32> ]
|
||||
pub fn serialize_witness(rln_witness: &RLNWitnessInput) -> Result<Vec<u8>, ProtocolError> {
|
||||
// Check if message_id is within user_message_limit
|
||||
message_id_range_check(&rln_witness.message_id, &rln_witness.user_message_limit)?;
|
||||
|
||||
let mut serialized: Vec<u8> = Vec::new();
|
||||
|
||||
serialized.append(&mut fr_to_bytes_le(&rln_witness.identity_secret));
|
||||
serialized.append(&mut fr_to_bytes_le(&rln_witness.user_message_limit));
|
||||
serialized.append(&mut fr_to_bytes_le(&rln_witness.message_id));
|
||||
serialized.append(&mut vec_fr_to_bytes_le(&rln_witness.path_elements)?);
|
||||
serialized.append(&mut vec_u8_to_bytes_le(&rln_witness.identity_path_index)?);
|
||||
serialized.append(&mut fr_to_bytes_le(&rln_witness.x));
|
||||
serialized.append(&mut fr_to_bytes_le(&rln_witness.external_nullifier));
|
||||
// Calculate capacity for Vec:
|
||||
// - 5 fixed field elements: identity_secret, user_message_limit, message_id, x, external_nullifier
|
||||
// - variable number of path elements
|
||||
// - identity_path_index (variable size)
|
||||
let mut serialized: Vec<u8> = Vec::with_capacity(
|
||||
fr_byte_size() * (5 + rln_witness.path_elements.len())
|
||||
+ rln_witness.identity_path_index.len(),
|
||||
);
|
||||
serialized.extend_from_slice(&rln_witness.identity_secret.to_bytes_le());
|
||||
serialized.extend_from_slice(&fr_to_bytes_le(&rln_witness.user_message_limit));
|
||||
serialized.extend_from_slice(&fr_to_bytes_le(&rln_witness.message_id));
|
||||
serialized.extend_from_slice(&vec_fr_to_bytes_le(&rln_witness.path_elements));
|
||||
serialized.extend_from_slice(&vec_u8_to_bytes_le(&rln_witness.identity_path_index));
|
||||
serialized.extend_from_slice(&fr_to_bytes_le(&rln_witness.x));
|
||||
serialized.extend_from_slice(&fr_to_bytes_le(&rln_witness.external_nullifier));
|
||||
|
||||
Ok(serialized)
|
||||
}
|
||||
@@ -124,10 +164,10 @@ pub fn serialize_witness(rln_witness: &RLNWitnessInput) -> Result<Vec<u8>> {
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns an error if `message_id` is not within `user_message_limit`.
|
||||
pub fn deserialize_witness(serialized: &[u8]) -> Result<(RLNWitnessInput, usize)> {
|
||||
pub fn deserialize_witness(serialized: &[u8]) -> Result<(RLNWitnessInput, usize), ProtocolError> {
|
||||
let mut all_read: usize = 0;
|
||||
|
||||
let (identity_secret, read) = bytes_le_to_fr(&serialized[all_read..]);
|
||||
let (identity_secret, read) = IdSecret::from_bytes_le(&serialized[all_read..]);
|
||||
all_read += read;
|
||||
|
||||
let (user_message_limit, read) = bytes_le_to_fr(&serialized[all_read..]);
|
||||
@@ -151,7 +191,7 @@ pub fn deserialize_witness(serialized: &[u8]) -> Result<(RLNWitnessInput, usize)
|
||||
all_read += read;
|
||||
|
||||
if serialized.len() != all_read {
|
||||
return Err(Report::msg("serialized length is not equal to all_read"));
|
||||
return Err(ProtocolError::InvalidReadLen(serialized.len(), all_read));
|
||||
}
|
||||
|
||||
Ok((
|
||||
@@ -172,18 +212,22 @@ pub fn deserialize_witness(serialized: &[u8]) -> Result<(RLNWitnessInput, usize)
|
||||
// https://github.com/kilic/rln/blob/7ac74183f8b69b399e3bc96c1ae8ab61c026dc43/src/public.rs#L148
|
||||
// input_data is [ identity_secret<32> | id_index<8> | user_message_limit<32> | message_id<32> | external_nullifier<32> | signal_len<8> | signal<var> ]
|
||||
// return value is a rln witness populated according to this information
|
||||
#[cfg(not(feature = "stateless"))]
|
||||
pub fn proof_inputs_to_rln_witness(
|
||||
tree: &mut PoseidonTree,
|
||||
serialized: &[u8],
|
||||
) -> Result<(RLNWitnessInput, usize)> {
|
||||
) -> Result<(RLNWitnessInput, usize), ProtocolError> {
|
||||
let mut all_read: usize = 0;
|
||||
|
||||
let (identity_secret, read) = bytes_le_to_fr(&serialized[all_read..]);
|
||||
let (identity_secret, read) = IdSecret::from_bytes_le(&serialized[all_read..]);
|
||||
all_read += read;
|
||||
|
||||
let id_index = usize::try_from(u64::from_le_bytes(
|
||||
serialized[all_read..all_read + 8].try_into()?,
|
||||
))?;
|
||||
serialized[all_read..all_read + 8]
|
||||
.try_into()
|
||||
.map_err(ConversionError::FromSlice)?,
|
||||
))
|
||||
.map_err(ConversionError::ToUsize)?;
|
||||
all_read += 8;
|
||||
|
||||
let (user_message_limit, read) = bytes_le_to_fr(&serialized[all_read..]);
|
||||
@@ -196,8 +240,11 @@ pub fn proof_inputs_to_rln_witness(
|
||||
all_read += read;
|
||||
|
||||
let signal_len = usize::try_from(u64::from_le_bytes(
|
||||
serialized[all_read..all_read + 8].try_into()?,
|
||||
))?;
|
||||
serialized[all_read..all_read + 8]
|
||||
.try_into()
|
||||
.map_err(ConversionError::FromSlice)?,
|
||||
))
|
||||
.map_err(ConversionError::ToUsize)?;
|
||||
all_read += 8;
|
||||
|
||||
let signal: Vec<u8> = serialized[all_read..all_read + signal_len].to_vec();
|
||||
@@ -206,7 +253,7 @@ pub fn proof_inputs_to_rln_witness(
|
||||
let path_elements = merkle_proof.get_path_elements();
|
||||
let identity_path_index = merkle_proof.get_path_index();
|
||||
|
||||
let x = hash_to_field(&signal);
|
||||
let x = hash_to_field_le(&signal);
|
||||
|
||||
Ok((
|
||||
RLNWitnessInput {
|
||||
@@ -222,24 +269,22 @@ pub fn proof_inputs_to_rln_witness(
|
||||
))
|
||||
}
|
||||
|
||||
/// Creates `RLNWitnessInput` from it's fields.
|
||||
/// Creates [`RLNWitnessInput`] from it's fields.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns an error if `message_id` is not within `user_message_limit`.
|
||||
pub fn rln_witness_from_values(
|
||||
identity_secret: Fr,
|
||||
merkle_proof: &MerkleProof,
|
||||
identity_secret: IdSecret,
|
||||
path_elements: Vec<Fp<MontBackend<FrConfig, 4>, 4>>,
|
||||
identity_path_index: Vec<u8>,
|
||||
x: Fr,
|
||||
external_nullifier: Fr,
|
||||
user_message_limit: Fr,
|
||||
message_id: Fr,
|
||||
) -> Result<RLNWitnessInput> {
|
||||
) -> Result<RLNWitnessInput, ProtocolError> {
|
||||
message_id_range_check(&message_id, &user_message_limit)?;
|
||||
|
||||
let path_elements = merkle_proof.get_path_elements();
|
||||
let identity_path_index = merkle_proof.get_path_index();
|
||||
|
||||
Ok(RLNWitnessInput {
|
||||
identity_secret,
|
||||
path_elements,
|
||||
@@ -251,19 +296,19 @@ pub fn rln_witness_from_values(
|
||||
})
|
||||
}
|
||||
|
||||
pub fn random_rln_witness(tree_height: usize) -> RLNWitnessInput {
|
||||
pub fn random_rln_witness(tree_depth: usize) -> RLNWitnessInput {
|
||||
let mut rng = thread_rng();
|
||||
|
||||
let identity_secret = hash_to_field(&rng.gen::<[u8; 32]>());
|
||||
let x = hash_to_field(&rng.gen::<[u8; 32]>());
|
||||
let epoch = hash_to_field(&rng.gen::<[u8; 32]>());
|
||||
let rln_identifier = hash_to_field(RLN_IDENTIFIER); //hash_to_field(&rng.gen::<[u8; 32]>());
|
||||
let identity_secret = IdSecret::rand(&mut rng);
|
||||
let x = hash_to_field_le(&rng.gen::<[u8; 32]>());
|
||||
let epoch = hash_to_field_le(&rng.gen::<[u8; 32]>());
|
||||
let rln_identifier = hash_to_field_le(RLN_IDENTIFIER);
|
||||
|
||||
let mut path_elements: Vec<Fr> = Vec::new();
|
||||
let mut identity_path_index: Vec<u8> = Vec::new();
|
||||
|
||||
for _ in 0..tree_height {
|
||||
path_elements.push(hash_to_field(&rng.gen::<[u8; 32]>()));
|
||||
for _ in 0..tree_depth {
|
||||
path_elements.push(hash_to_field_le(&rng.gen::<[u8; 32]>()));
|
||||
identity_path_index.push(rng.gen_range(0..2) as u8);
|
||||
}
|
||||
|
||||
@@ -281,16 +326,24 @@ pub fn random_rln_witness(tree_height: usize) -> RLNWitnessInput {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn proof_values_from_witness(rln_witness: &RLNWitnessInput) -> Result<RLNProofValues> {
|
||||
pub fn proof_values_from_witness(
|
||||
rln_witness: &RLNWitnessInput,
|
||||
) -> Result<RLNProofValues, ProtocolError> {
|
||||
message_id_range_check(&rln_witness.message_id, &rln_witness.user_message_limit)?;
|
||||
|
||||
// y share
|
||||
let a_0 = rln_witness.identity_secret;
|
||||
let a_1 = poseidon_hash(&[a_0, rln_witness.external_nullifier, rln_witness.message_id]);
|
||||
let y = a_0 + rln_witness.x * a_1;
|
||||
let a_0 = &rln_witness.identity_secret;
|
||||
let mut to_hash = [
|
||||
*(a_0.clone()),
|
||||
rln_witness.external_nullifier,
|
||||
rln_witness.message_id,
|
||||
];
|
||||
let a_1 = poseidon_hash(&to_hash);
|
||||
let y = *(a_0.clone()) + rln_witness.x * a_1;
|
||||
|
||||
// Nullifier
|
||||
let nullifier = poseidon_hash(&[a_1]);
|
||||
to_hash[0].zeroize();
|
||||
|
||||
// Merkle tree root computations
|
||||
let root = compute_tree_root(
|
||||
@@ -309,14 +362,17 @@ pub fn proof_values_from_witness(rln_witness: &RLNWitnessInput) -> Result<RLNPro
|
||||
})
|
||||
}
|
||||
|
||||
/// input_data is [ root<32> | external_nullifier<32> | x<32> | y<32> | nullifier<32> ]
|
||||
pub fn serialize_proof_values(rln_proof_values: &RLNProofValues) -> Vec<u8> {
|
||||
let mut serialized: Vec<u8> = Vec::new();
|
||||
// Calculate capacity for Vec:
|
||||
// 5 field elements: root, external_nullifier, x, y, nullifier
|
||||
let mut serialized = Vec::with_capacity(fr_byte_size() * 5);
|
||||
|
||||
serialized.append(&mut fr_to_bytes_le(&rln_proof_values.root));
|
||||
serialized.append(&mut fr_to_bytes_le(&rln_proof_values.external_nullifier));
|
||||
serialized.append(&mut fr_to_bytes_le(&rln_proof_values.x));
|
||||
serialized.append(&mut fr_to_bytes_le(&rln_proof_values.y));
|
||||
serialized.append(&mut fr_to_bytes_le(&rln_proof_values.nullifier));
|
||||
serialized.extend_from_slice(&fr_to_bytes_le(&rln_proof_values.root));
|
||||
serialized.extend_from_slice(&fr_to_bytes_le(&rln_proof_values.external_nullifier));
|
||||
serialized.extend_from_slice(&fr_to_bytes_le(&rln_proof_values.x));
|
||||
serialized.extend_from_slice(&fr_to_bytes_le(&rln_proof_values.y));
|
||||
serialized.extend_from_slice(&fr_to_bytes_le(&rln_proof_values.nullifier));
|
||||
|
||||
serialized
|
||||
}
|
||||
@@ -353,30 +409,43 @@ pub fn deserialize_proof_values(serialized: &[u8]) -> (RLNProofValues, usize) {
|
||||
)
|
||||
}
|
||||
|
||||
// input_data is [ identity_secret<32> | id_index<8> | user_message_limit<32> | message_id<32> | external_nullifier<32> | signal_len<8> | signal<var> ]
|
||||
pub fn prepare_prove_input(
|
||||
identity_secret: Fr,
|
||||
identity_secret: IdSecret,
|
||||
id_index: usize,
|
||||
user_message_limit: Fr,
|
||||
message_id: Fr,
|
||||
external_nullifier: Fr,
|
||||
signal: &[u8],
|
||||
) -> Vec<u8> {
|
||||
let mut serialized: Vec<u8> = Vec::new();
|
||||
// Calculate capacity for Vec:
|
||||
// - 4 field elements: identity_secret, user_message_limit, message_id, external_nullifier
|
||||
// - 16 bytes for two normalized usize values (id_index<8> + signal_len<8>)
|
||||
// - variable length signal data
|
||||
let mut serialized = Vec::with_capacity(fr_byte_size() * 4 + 16 + signal.len()); // length of 4 fr elements + 16 bytes (id_index + len) + signal length
|
||||
|
||||
serialized.append(&mut fr_to_bytes_le(&identity_secret));
|
||||
serialized.append(&mut normalize_usize(id_index));
|
||||
serialized.append(&mut fr_to_bytes_le(&external_nullifier));
|
||||
serialized.append(&mut normalize_usize(signal.len()));
|
||||
serialized.append(&mut signal.to_vec());
|
||||
serialized.extend_from_slice(&identity_secret.to_bytes_le());
|
||||
serialized.extend_from_slice(&normalize_usize_le(id_index));
|
||||
serialized.extend_from_slice(&fr_to_bytes_le(&user_message_limit));
|
||||
serialized.extend_from_slice(&fr_to_bytes_le(&message_id));
|
||||
serialized.extend_from_slice(&fr_to_bytes_le(&external_nullifier));
|
||||
serialized.extend_from_slice(&normalize_usize_le(signal.len()));
|
||||
serialized.extend_from_slice(signal);
|
||||
|
||||
serialized
|
||||
}
|
||||
|
||||
#[allow(clippy::redundant_clone)]
|
||||
// input_data is [ proof<128> | root<32> | external_nullifier<32> | x<32> | y<32> | nullifier<32> | signal_len<8> | signal<var> ]
|
||||
pub fn prepare_verify_input(proof_data: Vec<u8>, signal: &[u8]) -> Vec<u8> {
|
||||
let mut serialized: Vec<u8> = Vec::new();
|
||||
// Calculate capacity for Vec:
|
||||
// - proof_data contains the proof and proof values (proof<128> + root<32> + external_nullifier<32> + x<32> + y<32> + nullifier<32>)
|
||||
// - 8 bytes for normalized signal length value (signal_len<8>)
|
||||
// - variable length signal data
|
||||
let mut serialized = Vec::with_capacity(proof_data.len() + 8 + signal.len());
|
||||
|
||||
serialized.append(&mut proof_data.clone());
|
||||
serialized.append(&mut normalize_usize(signal.len()));
|
||||
serialized.append(&mut signal.to_vec());
|
||||
serialized.extend(proof_data);
|
||||
serialized.extend_from_slice(&normalize_usize_le(signal.len()));
|
||||
serialized.extend_from_slice(signal);
|
||||
|
||||
serialized
|
||||
}
|
||||
@@ -386,12 +455,15 @@ pub fn prepare_verify_input(proof_data: Vec<u8>, signal: &[u8]) -> Vec<u8> {
|
||||
///////////////////////////////////////////////////////
|
||||
|
||||
pub fn compute_tree_root(
|
||||
identity_secret: &Fr,
|
||||
identity_secret: &IdSecret,
|
||||
user_message_limit: &Fr,
|
||||
path_elements: &[Fr],
|
||||
identity_path_index: &[u8],
|
||||
) -> Fr {
|
||||
let id_commitment = poseidon_hash(&[*identity_secret]);
|
||||
let mut to_hash = [*identity_secret.clone()];
|
||||
let id_commitment = poseidon_hash(&to_hash);
|
||||
to_hash[0].zeroize();
|
||||
|
||||
let mut root = poseidon_hash(&[id_commitment, *user_message_limit]);
|
||||
|
||||
for i in 0..identity_path_index.len() {
|
||||
@@ -412,10 +484,12 @@ pub fn compute_tree_root(
|
||||
// Generates a tuple (identity_secret_hash, id_commitment) where
|
||||
// identity_secret_hash is random and id_commitment = PoseidonHash(identity_secret_hash)
|
||||
// RNG is instantiated using thread_rng()
|
||||
pub fn keygen() -> (Fr, Fr) {
|
||||
pub fn keygen() -> (IdSecret, Fr) {
|
||||
let mut rng = thread_rng();
|
||||
let identity_secret_hash = Fr::rand(&mut rng);
|
||||
let id_commitment = poseidon_hash(&[identity_secret_hash]);
|
||||
let identity_secret_hash = IdSecret::rand(&mut rng);
|
||||
let mut to_hash = [*identity_secret_hash.clone()];
|
||||
let id_commitment = poseidon_hash(&to_hash);
|
||||
to_hash[0].zeroize();
|
||||
(identity_secret_hash, id_commitment)
|
||||
}
|
||||
|
||||
@@ -483,7 +557,10 @@ pub fn extended_seeded_keygen(signal: &[u8]) -> (Fr, Fr, Fr, Fr) {
|
||||
)
|
||||
}
|
||||
|
||||
pub fn compute_id_secret(share1: (Fr, Fr), share2: (Fr, Fr)) -> Result<Fr, String> {
|
||||
pub fn compute_id_secret(
|
||||
share1: (Fr, Fr),
|
||||
share2: (Fr, Fr),
|
||||
) -> Result<IdSecret, ComputeIdSecretError> {
|
||||
// Assuming a0 is the identity secret and a1 = poseidonHash([a0, external_nullifier]),
|
||||
// a (x,y) share satisfies the following relation
|
||||
// y = a_0 + x * a_1
|
||||
@@ -493,30 +570,26 @@ pub fn compute_id_secret(share1: (Fr, Fr), share2: (Fr, Fr)) -> Result<Fr, Strin
|
||||
// If the two input shares were computed for the same external_nullifier and identity secret, we can recover the latter
|
||||
// y1 = a_0 + x1 * a_1
|
||||
// y2 = a_0 + x2 * a_1
|
||||
let a_1 = (y1 - y2) / (x1 - x2);
|
||||
let a_0 = y1 - x1 * a_1;
|
||||
|
||||
// If shares come from the same polynomial, a0 is correctly recovered and a1 = poseidonHash([a0, external_nullifier])
|
||||
Ok(a_0)
|
||||
if (x1 - x2) != Fr::ZERO {
|
||||
let a_1 = (y1 - y2) / (x1 - x2);
|
||||
let mut a_0 = y1 - x1 * a_1;
|
||||
|
||||
// If shares come from the same polynomial, a0 is correctly recovered and a1 = poseidonHash([a0, external_nullifier])
|
||||
let id_secret = IdSecret::from(&mut a_0);
|
||||
Ok(id_secret)
|
||||
} else {
|
||||
Err(ComputeIdSecretError::DivisionByZero)
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////
|
||||
// zkSNARK utility functions
|
||||
///////////////////////////////////////////////////////
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum ProofError {
|
||||
#[error("Error reading circuit key: {0}")]
|
||||
CircuitKeyError(#[from] Report),
|
||||
#[error("Error producing witness: {0}")]
|
||||
WitnessError(Report),
|
||||
#[error("Error producing proof: {0}")]
|
||||
SynthesisError(#[from] SynthesisError),
|
||||
}
|
||||
|
||||
fn calculate_witness_element<E: ark_ec::pairing::Pairing>(
|
||||
witness: Vec<BigInt>,
|
||||
) -> Result<Vec<E::ScalarField>> {
|
||||
) -> Result<Vec<E::ScalarField>, ProtocolError> {
|
||||
use ark_ff::PrimeField;
|
||||
let modulus = <E::ScalarField as PrimeField>::MODULUS;
|
||||
|
||||
@@ -529,9 +602,9 @@ fn calculate_witness_element<E: ark_ec::pairing::Pairing>(
|
||||
modulus.into()
|
||||
- w.abs()
|
||||
.to_biguint()
|
||||
.ok_or(Report::msg("not a biguint value"))?
|
||||
.ok_or(ProtocolError::BigUintConversion(w))?
|
||||
} else {
|
||||
w.to_biguint().ok_or(Report::msg("not a biguint value"))?
|
||||
w.to_biguint().ok_or(ProtocolError::BigUintConversion(w))?
|
||||
};
|
||||
witness_vec.push(E::ScalarField::from(w))
|
||||
}
|
||||
@@ -544,13 +617,12 @@ pub fn generate_proof_with_witness(
|
||||
proving_key: &(ProvingKey<Curve>, ConstraintMatrices<Fr>),
|
||||
) -> Result<ArkProof<Curve>, ProofError> {
|
||||
// If in debug mode, we measure and later print time take to compute witness
|
||||
#[cfg(debug_assertions)]
|
||||
#[cfg(test)]
|
||||
let now = Instant::now();
|
||||
|
||||
let full_assignment =
|
||||
calculate_witness_element::<Curve>(witness).map_err(ProofError::WitnessError)?;
|
||||
let full_assignment = calculate_witness_element::<Curve>(witness)?;
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
#[cfg(test)]
|
||||
println!("witness generation took: {:.2?}", now.elapsed());
|
||||
|
||||
// Random Values
|
||||
@@ -559,7 +631,7 @@ pub fn generate_proof_with_witness(
|
||||
let s = Fr::rand(&mut rng);
|
||||
|
||||
// If in debug mode, we measure and later print time take to compute proof
|
||||
#[cfg(debug_assertions)]
|
||||
#[cfg(test)]
|
||||
let now = Instant::now();
|
||||
|
||||
let proof = Groth16::<_, CircomReduction>::create_proof_with_reduction_and_matrices(
|
||||
@@ -572,7 +644,7 @@ pub fn generate_proof_with_witness(
|
||||
full_assignment.as_slice(),
|
||||
)?;
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
#[cfg(test)]
|
||||
println!("proof generation took: {:.2?}", now.elapsed());
|
||||
|
||||
Ok(proof)
|
||||
@@ -585,39 +657,42 @@ pub fn generate_proof_with_witness(
|
||||
/// Returns an error if `rln_witness.message_id` is not within `rln_witness.user_message_limit`.
|
||||
pub fn inputs_for_witness_calculation(
|
||||
rln_witness: &RLNWitnessInput,
|
||||
) -> Result<[(&str, Vec<BigInt>); 7]> {
|
||||
) -> Result<[(&str, Vec<FrOrSecret>); 7], ProtocolError> {
|
||||
message_id_range_check(&rln_witness.message_id, &rln_witness.user_message_limit)?;
|
||||
|
||||
// We convert the path indexes to field elements
|
||||
// TODO: check if necessary
|
||||
let mut path_elements = Vec::new();
|
||||
|
||||
for v in rln_witness.path_elements.iter() {
|
||||
path_elements.push(to_bigint(v)?);
|
||||
}
|
||||
|
||||
let mut identity_path_index = Vec::new();
|
||||
let mut identity_path_index = Vec::with_capacity(rln_witness.identity_path_index.len());
|
||||
rln_witness
|
||||
.identity_path_index
|
||||
.iter()
|
||||
.for_each(|v| identity_path_index.push(BigInt::from(*v)));
|
||||
.for_each(|v| identity_path_index.push(Fr::from(*v)));
|
||||
|
||||
Ok([
|
||||
(
|
||||
"identitySecret",
|
||||
vec![to_bigint(&rln_witness.identity_secret)?],
|
||||
vec![rln_witness.identity_secret.clone().into()],
|
||||
),
|
||||
(
|
||||
"userMessageLimit",
|
||||
vec![to_bigint(&rln_witness.user_message_limit)?],
|
||||
vec![rln_witness.user_message_limit.into()],
|
||||
),
|
||||
("messageId", vec![to_bigint(&rln_witness.message_id)?]),
|
||||
("pathElements", path_elements),
|
||||
("identityPathIndex", identity_path_index),
|
||||
("x", vec![to_bigint(&rln_witness.x)?]),
|
||||
("messageId", vec![rln_witness.message_id.into()]),
|
||||
(
|
||||
"pathElements",
|
||||
rln_witness
|
||||
.path_elements
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(Into::into)
|
||||
.collect(),
|
||||
),
|
||||
(
|
||||
"identityPathIndex",
|
||||
identity_path_index.into_iter().map(Into::into).collect(),
|
||||
),
|
||||
("x", vec![rln_witness.x.into()]),
|
||||
(
|
||||
"externalNullifier",
|
||||
vec![to_bigint(&rln_witness.external_nullifier)?],
|
||||
vec![rln_witness.external_nullifier.into()],
|
||||
),
|
||||
])
|
||||
}
|
||||
@@ -628,34 +703,20 @@ pub fn inputs_for_witness_calculation(
|
||||
///
|
||||
/// Returns a [`ProofError`] if proving fails.
|
||||
pub fn generate_proof(
|
||||
#[cfg(not(target_arch = "wasm32"))] witness_calculator: &Mutex<WitnessCalculator>,
|
||||
#[cfg(target_arch = "wasm32")] witness_calculator: &mut WitnessCalculator,
|
||||
proving_key: &(ProvingKey<Curve>, ConstraintMatrices<Fr>),
|
||||
rln_witness: &RLNWitnessInput,
|
||||
graph_data: &[u8],
|
||||
) -> Result<ArkProof<Curve>, ProofError> {
|
||||
let inputs = inputs_for_witness_calculation(rln_witness)?
|
||||
.into_iter()
|
||||
.map(|(name, values)| (name.to_string(), values));
|
||||
|
||||
// If in debug mode, we measure and later print time take to compute witness
|
||||
#[cfg(debug_assertions)]
|
||||
#[cfg(test)]
|
||||
let now = Instant::now();
|
||||
let full_assignment = calculate_rln_witness(inputs, graph_data);
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(target_arch = "wasm32")] {
|
||||
let full_assignment = witness_calculator
|
||||
.calculate_witness_element::<Curve, _>(inputs, false)
|
||||
.map_err(ProofError::WitnessError)?;
|
||||
} else {
|
||||
let full_assignment = witness_calculator
|
||||
.lock()
|
||||
.expect("witness_calculator mutex should not get poisoned")
|
||||
.calculate_witness_element::<Curve, _>(inputs, false)
|
||||
.map_err(ProofError::WitnessError)?;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
#[cfg(test)]
|
||||
println!("witness generation took: {:.2?}", now.elapsed());
|
||||
|
||||
// Random Values
|
||||
@@ -664,7 +725,7 @@ pub fn generate_proof(
|
||||
let s = Fr::rand(&mut rng);
|
||||
|
||||
// If in debug mode, we measure and later print time take to compute proof
|
||||
#[cfg(debug_assertions)]
|
||||
#[cfg(test)]
|
||||
let now = Instant::now();
|
||||
let proof = Groth16::<_, CircomReduction>::create_proof_with_reduction_and_matrices(
|
||||
&proving_key.0,
|
||||
@@ -676,7 +737,7 @@ pub fn generate_proof(
|
||||
full_assignment.as_slice(),
|
||||
)?;
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
#[cfg(test)]
|
||||
println!("proof generation took: {:.2?}", now.elapsed());
|
||||
|
||||
Ok(proof)
|
||||
@@ -707,12 +768,12 @@ pub fn verify_proof(
|
||||
//let pr: ArkProof<Curve> = (*proof).into();
|
||||
|
||||
// If in debug mode, we measure and later print time take to verify proof
|
||||
#[cfg(debug_assertions)]
|
||||
#[cfg(test)]
|
||||
let now = Instant::now();
|
||||
|
||||
let verified = Groth16::<_, CircomReduction>::verify_proof(&pvk, proof, &inputs)?;
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
#[cfg(test)]
|
||||
println!("verify took: {:.2?}", now.elapsed());
|
||||
|
||||
Ok(verified)
|
||||
@@ -739,43 +800,49 @@ where
|
||||
a.map_err(serde::de::Error::custom)
|
||||
}
|
||||
|
||||
/// Converts a JSON value into [`RLNWitnessInput`](crate::protocol::RLNWitnessInput) object.
|
||||
/// Converts a JSON value into [`RLNWitnessInput`] object.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns an error if `rln_witness.message_id` is not within `rln_witness.user_message_limit`.
|
||||
pub fn rln_witness_from_json(input_json: serde_json::Value) -> Result<RLNWitnessInput> {
|
||||
pub fn rln_witness_from_json(
|
||||
input_json: serde_json::Value,
|
||||
) -> Result<RLNWitnessInput, ProtocolError> {
|
||||
let rln_witness: RLNWitnessInput = serde_json::from_value(input_json).unwrap();
|
||||
message_id_range_check(&rln_witness.message_id, &rln_witness.user_message_limit)?;
|
||||
|
||||
Ok(rln_witness)
|
||||
}
|
||||
|
||||
/// Converts a [`RLNWitnessInput`](crate::protocol::RLNWitnessInput) object to the corresponding JSON serialization.
|
||||
/// Converts a [`RLNWitnessInput`] object to the corresponding JSON serialization.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns an error if `message_id` is not within `user_message_limit`.
|
||||
pub fn rln_witness_to_json(rln_witness: &RLNWitnessInput) -> Result<serde_json::Value> {
|
||||
pub fn rln_witness_to_json(
|
||||
rln_witness: &RLNWitnessInput,
|
||||
) -> Result<serde_json::Value, ProtocolError> {
|
||||
message_id_range_check(&rln_witness.message_id, &rln_witness.user_message_limit)?;
|
||||
|
||||
let rln_witness_json = serde_json::to_value(rln_witness)?;
|
||||
Ok(rln_witness_json)
|
||||
}
|
||||
|
||||
/// Converts a [`RLNWitnessInput`](crate::protocol::RLNWitnessInput) object to the corresponding JSON serialization.
|
||||
/// Converts a [`RLNWitnessInput`] object to the corresponding JSON serialization.
|
||||
/// Before serialisation the data should be translated into big int for further calculation in the witness calculator.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns an error if `message_id` is not within `user_message_limit`.
|
||||
pub fn rln_witness_to_bigint_json(rln_witness: &RLNWitnessInput) -> Result<serde_json::Value> {
|
||||
pub fn rln_witness_to_bigint_json(
|
||||
rln_witness: &RLNWitnessInput,
|
||||
) -> Result<serde_json::Value, ProtocolError> {
|
||||
message_id_range_check(&rln_witness.message_id, &rln_witness.user_message_limit)?;
|
||||
|
||||
let mut path_elements = Vec::new();
|
||||
|
||||
for v in rln_witness.path_elements.iter() {
|
||||
path_elements.push(to_bigint(v)?.to_str_radix(10));
|
||||
path_elements.push(to_bigint(v).to_str_radix(10));
|
||||
}
|
||||
|
||||
let mut identity_path_index = Vec::new();
|
||||
@@ -785,22 +852,26 @@ pub fn rln_witness_to_bigint_json(rln_witness: &RLNWitnessInput) -> Result<serde
|
||||
.for_each(|v| identity_path_index.push(BigInt::from(*v).to_str_radix(10)));
|
||||
|
||||
let inputs = serde_json::json!({
|
||||
"identitySecret": to_bigint(&rln_witness.identity_secret)?.to_str_radix(10),
|
||||
"userMessageLimit": to_bigint(&rln_witness.user_message_limit)?.to_str_radix(10),
|
||||
"messageId": to_bigint(&rln_witness.message_id)?.to_str_radix(10),
|
||||
"identitySecret": to_bigint(&rln_witness.identity_secret).to_str_radix(10),
|
||||
"userMessageLimit": to_bigint(&rln_witness.user_message_limit).to_str_radix(10),
|
||||
"messageId": to_bigint(&rln_witness.message_id).to_str_radix(10),
|
||||
"pathElements": path_elements,
|
||||
"identityPathIndex": identity_path_index,
|
||||
"x": to_bigint(&rln_witness.x)?.to_str_radix(10),
|
||||
"externalNullifier": to_bigint(&rln_witness.external_nullifier)?.to_str_radix(10),
|
||||
"x": to_bigint(&rln_witness.x).to_str_radix(10),
|
||||
"externalNullifier": to_bigint(&rln_witness.external_nullifier).to_str_radix(10),
|
||||
});
|
||||
|
||||
Ok(inputs)
|
||||
}
|
||||
|
||||
pub fn message_id_range_check(message_id: &Fr, user_message_limit: &Fr) -> Result<()> {
|
||||
pub fn message_id_range_check(
|
||||
message_id: &Fr,
|
||||
user_message_limit: &Fr,
|
||||
) -> Result<(), ProtocolError> {
|
||||
if message_id > user_message_limit {
|
||||
return Err(color_eyre::Report::msg(
|
||||
"message_id is not within user_message_limit",
|
||||
return Err(ProtocolError::InvalidMessageId(
|
||||
*message_id,
|
||||
*user_message_limit,
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
451
rln/src/utils.rs
451
rln/src/utils.rs
@@ -1,27 +1,34 @@
|
||||
// This crate provides cross-module useful utilities (mainly type conversions) not necessarily specific to RLN
|
||||
|
||||
use crate::circuit::Fr;
|
||||
use crate::error::ConversionError;
|
||||
use ark_ff::PrimeField;
|
||||
use color_eyre::{Report, Result};
|
||||
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
|
||||
use ark_std::UniformRand;
|
||||
use num_bigint::{BigInt, BigUint};
|
||||
use num_traits::Num;
|
||||
use rand::Rng;
|
||||
use ruint::aliases::U256;
|
||||
use serde_json::json;
|
||||
use std::io::Cursor;
|
||||
use std::iter::Extend;
|
||||
use std::ops::Deref;
|
||||
use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};
|
||||
|
||||
pub fn to_bigint(el: &Fr) -> Result<BigInt> {
|
||||
let res: BigUint = (*el).into();
|
||||
Ok(res.into())
|
||||
#[inline(always)]
|
||||
pub fn to_bigint(el: &Fr) -> BigInt {
|
||||
BigUint::from(*el).into()
|
||||
}
|
||||
|
||||
pub fn fr_byte_size() -> usize {
|
||||
#[inline(always)]
|
||||
pub const fn fr_byte_size() -> usize {
|
||||
let mbs = <Fr as PrimeField>::MODULUS_BIT_SIZE;
|
||||
((mbs + 64 - (mbs % 64)) / 8) as usize
|
||||
}
|
||||
|
||||
pub fn str_to_fr(input: &str, radix: u32) -> Result<Fr> {
|
||||
#[inline(always)]
|
||||
pub fn str_to_fr(input: &str, radix: u32) -> Result<Fr, ConversionError> {
|
||||
if !(radix == 10 || radix == 16) {
|
||||
return Err(Report::msg("wrong radix"));
|
||||
return Err(ConversionError::WrongRadix);
|
||||
}
|
||||
|
||||
// We remove any quote present and we trim
|
||||
@@ -37,6 +44,7 @@ pub fn str_to_fr(input: &str, radix: u32) -> Result<Fr> {
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn bytes_le_to_fr(input: &[u8]) -> (Fr, usize) {
|
||||
let el_size = fr_byte_size();
|
||||
(
|
||||
@@ -45,6 +53,7 @@ pub fn bytes_le_to_fr(input: &[u8]) -> (Fr, usize) {
|
||||
)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn bytes_be_to_fr(input: &[u8]) -> (Fr, usize) {
|
||||
let el_size = fr_byte_size();
|
||||
(
|
||||
@@ -53,282 +62,360 @@ pub fn bytes_be_to_fr(input: &[u8]) -> (Fr, usize) {
|
||||
)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn fr_to_bytes_le(input: &Fr) -> Vec<u8> {
|
||||
let input_biguint: BigUint = (*input).into();
|
||||
let mut res = input_biguint.to_bytes_le();
|
||||
//BigUint conversion ignores most significant zero bytes. We restore them otherwise serialization will fail (length % 8 != 0)
|
||||
while res.len() != fr_byte_size() {
|
||||
res.push(0);
|
||||
}
|
||||
res.resize(fr_byte_size(), 0);
|
||||
res
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn fr_to_bytes_be(input: &Fr) -> Vec<u8> {
|
||||
let input_biguint: BigUint = (*input).into();
|
||||
let mut res = input_biguint.to_bytes_be();
|
||||
// BigUint conversion ignores most significant zero bytes. We restore them otherwise serialization might fail
|
||||
// Fr elements are stored using 64 bits nimbs
|
||||
while res.len() != fr_byte_size() {
|
||||
res.insert(0, 0);
|
||||
// For BE, insert 0 at the start of the Vec (see also fr_to_bytes_le comments)
|
||||
let to_insert_count = fr_byte_size().saturating_sub(res.len());
|
||||
if to_insert_count > 0 {
|
||||
// Insert multi 0 at index 0
|
||||
res.splice(0..0, std::iter::repeat_n(0, to_insert_count));
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
pub fn vec_fr_to_bytes_le(input: &[Fr]) -> Result<Vec<u8>> {
|
||||
let mut bytes: Vec<u8> = Vec::new();
|
||||
//We store the vector length
|
||||
bytes.extend(u64::try_from(input.len())?.to_le_bytes().to_vec());
|
||||
#[inline(always)]
|
||||
pub fn vec_fr_to_bytes_le(input: &[Fr]) -> Vec<u8> {
|
||||
// Calculate capacity for Vec:
|
||||
// - 8 bytes for normalized vector length (usize)
|
||||
// - each Fr element requires fr_byte_size() bytes (typically 32 bytes)
|
||||
let mut bytes = Vec::with_capacity(8 + input.len() * fr_byte_size());
|
||||
|
||||
// We store the vector length
|
||||
bytes.extend_from_slice(&normalize_usize_le(input.len()));
|
||||
|
||||
// We store each element
|
||||
input.iter().for_each(|el| bytes.extend(fr_to_bytes_le(el)));
|
||||
for el in input {
|
||||
bytes.extend_from_slice(&fr_to_bytes_le(el));
|
||||
}
|
||||
|
||||
Ok(bytes)
|
||||
bytes
|
||||
}
|
||||
|
||||
pub fn vec_fr_to_bytes_be(input: &[Fr]) -> Result<Vec<u8>> {
|
||||
let mut bytes: Vec<u8> = Vec::new();
|
||||
//We store the vector length
|
||||
bytes.extend(u64::try_from(input.len())?.to_be_bytes().to_vec());
|
||||
#[inline(always)]
|
||||
pub fn vec_fr_to_bytes_be(input: &[Fr]) -> Vec<u8> {
|
||||
// Calculate capacity for Vec:
|
||||
// - 8 bytes for normalized vector length (usize)
|
||||
// - each Fr element requires fr_byte_size() bytes (typically 32 bytes)
|
||||
let mut bytes = Vec::with_capacity(8 + input.len() * fr_byte_size());
|
||||
|
||||
// We store the vector length
|
||||
bytes.extend_from_slice(&normalize_usize_be(input.len()));
|
||||
|
||||
// We store each element
|
||||
input.iter().for_each(|el| bytes.extend(fr_to_bytes_be(el)));
|
||||
for el in input {
|
||||
bytes.extend_from_slice(&fr_to_bytes_be(el));
|
||||
}
|
||||
|
||||
Ok(bytes)
|
||||
bytes
|
||||
}
|
||||
|
||||
pub fn vec_u8_to_bytes_le(input: &[u8]) -> Result<Vec<u8>> {
|
||||
let mut bytes: Vec<u8> = Vec::new();
|
||||
//We store the vector length
|
||||
bytes.extend(u64::try_from(input.len())?.to_le_bytes().to_vec());
|
||||
#[inline(always)]
|
||||
pub fn vec_u8_to_bytes_le(input: &[u8]) -> Vec<u8> {
|
||||
// Calculate capacity for Vec:
|
||||
// - 8 bytes for normalized vector length (usize)
|
||||
// - variable length input data
|
||||
let mut bytes = Vec::with_capacity(8 + input.len());
|
||||
|
||||
bytes.extend(input);
|
||||
// We store the vector length
|
||||
bytes.extend_from_slice(&normalize_usize_le(input.len()));
|
||||
|
||||
Ok(bytes)
|
||||
// We store the input
|
||||
bytes.extend_from_slice(input);
|
||||
|
||||
bytes
|
||||
}
|
||||
|
||||
pub fn vec_u8_to_bytes_be(input: Vec<u8>) -> Result<Vec<u8>> {
|
||||
let mut bytes: Vec<u8> = Vec::new();
|
||||
//We store the vector length
|
||||
bytes.extend(u64::try_from(input.len())?.to_be_bytes().to_vec());
|
||||
#[inline(always)]
|
||||
pub fn vec_u8_to_bytes_be(input: &[u8]) -> Vec<u8> {
|
||||
// Calculate capacity for Vec:
|
||||
// - 8 bytes for normalized vector length (usize)
|
||||
// - variable length input data
|
||||
let mut bytes = Vec::with_capacity(8 + input.len());
|
||||
|
||||
bytes.extend(input);
|
||||
// We store the vector length
|
||||
bytes.extend_from_slice(&normalize_usize_be(input.len()));
|
||||
|
||||
Ok(bytes)
|
||||
// We store the input
|
||||
bytes.extend_from_slice(input);
|
||||
|
||||
bytes
|
||||
}
|
||||
|
||||
pub fn bytes_le_to_vec_u8(input: &[u8]) -> Result<(Vec<u8>, usize)> {
|
||||
#[inline(always)]
|
||||
pub fn bytes_le_to_vec_u8(input: &[u8]) -> Result<(Vec<u8>, usize), ConversionError> {
|
||||
let mut read: usize = 0;
|
||||
|
||||
if input.len() < 8 {
|
||||
return Err(ConversionError::InsufficientData {
|
||||
expected: 8,
|
||||
actual: input.len(),
|
||||
});
|
||||
}
|
||||
let len = usize::try_from(u64::from_le_bytes(input[0..8].try_into()?))?;
|
||||
read += 8;
|
||||
|
||||
if input.len() < 8 + len {
|
||||
return Err(ConversionError::InsufficientData {
|
||||
expected: 8 + len,
|
||||
actual: input.len(),
|
||||
});
|
||||
}
|
||||
let res = input[8..8 + len].to_vec();
|
||||
read += res.len();
|
||||
|
||||
Ok((res, read))
|
||||
}
|
||||
|
||||
pub fn bytes_be_to_vec_u8(input: &[u8]) -> Result<(Vec<u8>, usize)> {
|
||||
#[inline(always)]
|
||||
pub fn bytes_be_to_vec_u8(input: &[u8]) -> Result<(Vec<u8>, usize), ConversionError> {
|
||||
let mut read: usize = 0;
|
||||
|
||||
if input.len() < 8 {
|
||||
return Err(ConversionError::InsufficientData {
|
||||
expected: 8,
|
||||
actual: input.len(),
|
||||
});
|
||||
}
|
||||
let len = usize::try_from(u64::from_be_bytes(input[0..8].try_into()?))?;
|
||||
read += 8;
|
||||
|
||||
if input.len() < 8 + len {
|
||||
return Err(ConversionError::InsufficientData {
|
||||
expected: 8 + len,
|
||||
actual: input.len(),
|
||||
});
|
||||
}
|
||||
let res = input[8..8 + len].to_vec();
|
||||
|
||||
read += res.len();
|
||||
|
||||
Ok((res, read))
|
||||
}
|
||||
|
||||
pub fn bytes_le_to_vec_fr(input: &[u8]) -> Result<(Vec<Fr>, usize)> {
|
||||
#[inline(always)]
|
||||
pub fn bytes_le_to_vec_fr(input: &[u8]) -> Result<(Vec<Fr>, usize), ConversionError> {
|
||||
let mut read: usize = 0;
|
||||
let mut res: Vec<Fr> = Vec::new();
|
||||
|
||||
if input.len() < 8 {
|
||||
return Err(ConversionError::InsufficientData {
|
||||
expected: 8,
|
||||
actual: input.len(),
|
||||
});
|
||||
}
|
||||
let len = usize::try_from(u64::from_le_bytes(input[0..8].try_into()?))?;
|
||||
read += 8;
|
||||
|
||||
let el_size = fr_byte_size();
|
||||
if input.len() < 8 + len * el_size {
|
||||
return Err(ConversionError::InsufficientData {
|
||||
expected: 8 + len * el_size,
|
||||
actual: input.len(),
|
||||
});
|
||||
}
|
||||
let mut res: Vec<Fr> = Vec::with_capacity(len);
|
||||
for i in 0..len {
|
||||
let (curr_el, _) = bytes_le_to_fr(&input[8 + el_size * i..8 + el_size * (i + 1)]);
|
||||
res.push(curr_el);
|
||||
read += el_size;
|
||||
}
|
||||
|
||||
Ok((res, read))
|
||||
}
|
||||
|
||||
pub fn bytes_be_to_vec_fr(input: &[u8]) -> Result<(Vec<Fr>, usize)> {
|
||||
#[inline(always)]
|
||||
pub fn bytes_be_to_vec_fr(input: &[u8]) -> Result<(Vec<Fr>, usize), ConversionError> {
|
||||
let mut read: usize = 0;
|
||||
let mut res: Vec<Fr> = Vec::new();
|
||||
|
||||
if input.len() < 8 {
|
||||
return Err(ConversionError::InsufficientData {
|
||||
expected: 8,
|
||||
actual: input.len(),
|
||||
});
|
||||
}
|
||||
let len = usize::try_from(u64::from_be_bytes(input[0..8].try_into()?))?;
|
||||
read += 8;
|
||||
|
||||
let el_size = fr_byte_size();
|
||||
if input.len() < 8 + len * el_size {
|
||||
return Err(ConversionError::InsufficientData {
|
||||
expected: 8 + len * el_size,
|
||||
actual: input.len(),
|
||||
});
|
||||
}
|
||||
let mut res: Vec<Fr> = Vec::with_capacity(len);
|
||||
for i in 0..len {
|
||||
let (curr_el, _) = bytes_be_to_fr(&input[8 + el_size * i..8 + el_size * (i + 1)]);
|
||||
res.push(curr_el);
|
||||
read += el_size;
|
||||
}
|
||||
|
||||
Ok((res, read))
|
||||
}
|
||||
|
||||
pub fn normalize_usize(input: usize) -> Vec<u8> {
|
||||
let mut normalized_usize = input.to_le_bytes().to_vec();
|
||||
normalized_usize.resize(8, 0);
|
||||
normalized_usize
|
||||
}
|
||||
|
||||
pub fn bytes_le_to_vec_usize(input: &[u8]) -> Result<Vec<usize>> {
|
||||
#[inline(always)]
|
||||
pub fn bytes_le_to_vec_usize(input: &[u8]) -> Result<Vec<usize>, ConversionError> {
|
||||
if input.len() < 8 {
|
||||
return Err(ConversionError::InsufficientData {
|
||||
expected: 8,
|
||||
actual: input.len(),
|
||||
});
|
||||
}
|
||||
let nof_elem = usize::try_from(u64::from_le_bytes(input[0..8].try_into()?))?;
|
||||
if nof_elem == 0 {
|
||||
Ok(vec![])
|
||||
} else {
|
||||
if input.len() < 8 + nof_elem * 8 {
|
||||
return Err(ConversionError::InsufficientData {
|
||||
expected: 8 + nof_elem * 8,
|
||||
actual: input.len(),
|
||||
});
|
||||
}
|
||||
let elements: Vec<usize> = input[8..]
|
||||
.chunks(8)
|
||||
.take(nof_elem)
|
||||
.map(|ch| usize::from_le_bytes(ch[0..8].try_into().unwrap()))
|
||||
.collect();
|
||||
Ok(elements)
|
||||
}
|
||||
}
|
||||
|
||||
// using for test
|
||||
#[inline(always)]
|
||||
pub fn bytes_be_to_vec_usize(input: &[u8]) -> Result<Vec<usize>, ConversionError> {
|
||||
if input.len() < 8 {
|
||||
return Err(ConversionError::InsufficientData {
|
||||
expected: 8,
|
||||
actual: input.len(),
|
||||
});
|
||||
}
|
||||
let nof_elem = usize::try_from(u64::from_be_bytes(input[0..8].try_into()?))?;
|
||||
if nof_elem == 0 {
|
||||
Ok(vec![])
|
||||
} else {
|
||||
if input.len() < 8 + nof_elem * 8 {
|
||||
return Err(ConversionError::InsufficientData {
|
||||
expected: 8 + nof_elem * 8,
|
||||
actual: input.len(),
|
||||
});
|
||||
}
|
||||
let elements: Vec<usize> = input[8..]
|
||||
.chunks(8)
|
||||
.take(nof_elem)
|
||||
.map(|ch| usize::from_be_bytes(ch[0..8].try_into().unwrap()))
|
||||
.collect();
|
||||
Ok(elements)
|
||||
}
|
||||
}
|
||||
|
||||
/// Normalizes a `usize` into an 8-byte array, ensuring consistency across architectures.
|
||||
/// On 32-bit systems, the result is zero-padded to 8 bytes.
|
||||
/// On 64-bit systems, it directly represents the `usize` value.
|
||||
#[inline(always)]
|
||||
pub fn normalize_usize_le(input: usize) -> [u8; 8] {
|
||||
let mut bytes = [0u8; 8];
|
||||
let input_bytes = input.to_le_bytes();
|
||||
bytes[..input_bytes.len()].copy_from_slice(&input_bytes);
|
||||
bytes
|
||||
}
|
||||
|
||||
/// Normalizes a `usize` into an 8-byte array, ensuring consistency across architectures.
|
||||
/// On 32-bit systems, the result is zero-padded to 8 bytes.
|
||||
/// On 64-bit systems, it directly represents the `usize` value.
|
||||
#[inline(always)]
|
||||
pub fn normalize_usize_be(input: usize) -> [u8; 8] {
|
||||
let mut bytes = [0u8; 8];
|
||||
let input_bytes = input.to_be_bytes();
|
||||
bytes[..input_bytes.len()].copy_from_slice(&input_bytes);
|
||||
bytes
|
||||
}
|
||||
|
||||
#[inline(always)] // using for test
|
||||
pub fn generate_input_buffer() -> Cursor<String> {
|
||||
Cursor::new(json!({}).to_string())
|
||||
}
|
||||
|
||||
/* Old conversion utilities between different libraries data types
|
||||
#[derive(
|
||||
Debug, Zeroize, ZeroizeOnDrop, Clone, PartialEq, CanonicalSerialize, CanonicalDeserialize,
|
||||
)]
|
||||
pub struct IdSecret(ark_bn254::Fr);
|
||||
|
||||
// Conversion Utilities between poseidon-rs Field and arkworks Fr (in order to call directly poseidon-rs' poseidon_hash)
|
||||
impl IdSecret {
|
||||
pub fn rand<R: Rng + ?Sized>(rng: &mut R) -> Self {
|
||||
let mut fr = Fr::rand(rng);
|
||||
let res = Self::from(&mut fr);
|
||||
// No need to zeroize fr (already zeroiz'ed in from implementation)
|
||||
#[allow(clippy::let_and_return)]
|
||||
res
|
||||
}
|
||||
|
||||
use ff::{PrimeField as _, PrimeFieldRepr as _};
|
||||
use poseidon_rs::Fr as PosFr;
|
||||
pub fn from_bytes_le(input: &[u8]) -> (Self, usize) {
|
||||
let el_size = fr_byte_size();
|
||||
let b_uint = BigUint::from_bytes_le(&input[0..el_size]);
|
||||
let mut fr = Fr::from(b_uint);
|
||||
let res = IdSecret::from(&mut fr);
|
||||
// Note: no zeroize on b_uint as it has been moved
|
||||
(res, el_size)
|
||||
}
|
||||
|
||||
pub fn fr_to_posfr(value: Fr) -> PosFr {
|
||||
let mut bytes = [0_u8; 32];
|
||||
let byte_vec = value.into_repr().to_bytes_be();
|
||||
bytes.copy_from_slice(&byte_vec[..]);
|
||||
let mut repr = <PosFr as ff::PrimeField>::Repr::default();
|
||||
repr.read_be(&bytes[..])
|
||||
.expect("read from correctly sized slice always succeeds");
|
||||
PosFr::from_repr(repr).expect("value is always in range")
|
||||
}
|
||||
pub(crate) fn to_bytes_le(&self) -> Zeroizing<Vec<u8>> {
|
||||
let input_biguint: BigUint = self.0.into();
|
||||
let mut res = input_biguint.to_bytes_le();
|
||||
res.resize(fr_byte_size(), 0);
|
||||
Zeroizing::new(res)
|
||||
}
|
||||
|
||||
pub fn posfr_to_fr(value: PosFr) -> Fr {
|
||||
let mut bytes = [0u8; 32];
|
||||
value
|
||||
.into_repr()
|
||||
.write_be(&mut bytes[..])
|
||||
.expect("write to correctly sized slice always succeeds");
|
||||
Fr::from_be_bytes_mod_order(&bytes)
|
||||
}
|
||||
pub(crate) fn to_bytes_be(&self) -> Zeroizing<Vec<u8>> {
|
||||
let input_biguint: BigUint = self.0.into();
|
||||
let mut res = input_biguint.to_bytes_be();
|
||||
let to_insert_count = fr_byte_size().saturating_sub(res.len());
|
||||
if to_insert_count > 0 {
|
||||
// Insert multi 0 at index 0
|
||||
res.splice(0..0, std::iter::repeat_n(0, to_insert_count));
|
||||
}
|
||||
Zeroizing::new(res)
|
||||
}
|
||||
|
||||
|
||||
// Conversion Utilities between semaphore-rs Field and arkworks Fr
|
||||
|
||||
use semaphore::Field;
|
||||
|
||||
pub fn to_fr(el: &Field) -> Fr {
|
||||
Fr::try_from(*el).unwrap()
|
||||
}
|
||||
|
||||
pub fn to_field(el: &Fr) -> Field {
|
||||
(*el).try_into().unwrap()
|
||||
}
|
||||
|
||||
pub fn vec_to_fr(v: &[Field]) -> Vec<Fr> {
|
||||
v.iter().map(|el| to_fr(el)).collect()
|
||||
}
|
||||
|
||||
pub fn vec_to_field(v: &[Fr]) -> Vec<Field> {
|
||||
v.iter().map(|el| to_field(el)).collect()
|
||||
}
|
||||
|
||||
pub fn vec_fr_to_field(input: &[Fr]) -> Vec<Field> {
|
||||
input.iter().map(|el| to_field(el)).collect()
|
||||
}
|
||||
|
||||
pub fn vec_field_to_fr(input: &[Field]) -> Vec<Fr> {
|
||||
input.iter().map(|el| to_fr(el)).collect()
|
||||
}
|
||||
|
||||
pub fn str_to_field(input: String, radix: i32) -> Field {
|
||||
assert!((radix == 10) || (radix == 16));
|
||||
|
||||
// We remove any quote present and we trim
|
||||
let single_quote: char = '\"';
|
||||
let input_clean = input.replace(single_quote, "");
|
||||
let input_clean = input_clean.trim();
|
||||
|
||||
if radix == 10 {
|
||||
Field::from_str(&format!(
|
||||
"{:01$x}",
|
||||
BigUint::from_str(input_clean).unwrap(),
|
||||
64
|
||||
))
|
||||
.unwrap()
|
||||
} else {
|
||||
let input_clean = input_clean.replace("0x", "");
|
||||
Field::from_str(&format!("{:0>64}", &input_clean)).unwrap()
|
||||
/// Warning: this can leak the secret value
|
||||
/// Warning: Leaked value is of type 'U256' which implement Copy (every copy will not be zeroized)
|
||||
pub(crate) fn to_u256(&self) -> U256 {
|
||||
let mut big_int = self.0.into_bigint();
|
||||
let res = U256::from_limbs(big_int.0);
|
||||
big_int.zeroize();
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
pub fn bytes_le_to_field(input: &[u8]) -> (Field, usize) {
|
||||
let (fr_el, read) = bytes_le_to_fr(input);
|
||||
(to_field(&fr_el), read)
|
||||
impl From<&mut Fr> for IdSecret {
|
||||
fn from(value: &mut Fr) -> Self {
|
||||
let id_secret = Self(*value);
|
||||
value.zeroize();
|
||||
id_secret
|
||||
}
|
||||
}
|
||||
|
||||
pub fn bytes_be_to_field(input: &[u8]) -> (Field, usize) {
|
||||
let (fr_el, read) = bytes_be_to_fr(input);
|
||||
(to_field(&fr_el), read)
|
||||
impl Deref for IdSecret {
|
||||
type Target = Fr;
|
||||
|
||||
/// Deref to &Fr
|
||||
///
|
||||
/// Warning: this can leak the secret value
|
||||
/// Warning: Leaked value is of type 'Fr' which implement Copy (every copy will not be zeroized)
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn field_to_bytes_le(input: &Field) -> Vec<u8> {
|
||||
fr_to_bytes_le(&to_fr(input))
|
||||
#[derive(Debug, Zeroize, ZeroizeOnDrop)]
|
||||
pub enum FrOrSecret {
|
||||
IdSecret(IdSecret),
|
||||
Fr(Fr),
|
||||
}
|
||||
|
||||
pub fn field_to_bytes_be(input: &Field) -> Vec<u8> {
|
||||
fr_to_bytes_be(&to_fr(input))
|
||||
impl From<Fr> for FrOrSecret {
|
||||
fn from(value: Fr) -> Self {
|
||||
FrOrSecret::Fr(value)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn vec_field_to_bytes_le(input: &[Field]) -> Vec<u8> {
|
||||
vec_fr_to_bytes_le(&vec_field_to_fr(input))
|
||||
impl From<IdSecret> for FrOrSecret {
|
||||
fn from(value: IdSecret) -> Self {
|
||||
FrOrSecret::IdSecret(value)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn vec_field_to_bytes_be(input: &[Field]) -> Vec<u8> {
|
||||
vec_fr_to_bytes_be(&vec_field_to_fr(input))
|
||||
}
|
||||
|
||||
|
||||
pub fn bytes_le_to_vec_field(input: &[u8]) -> (Vec<Field>, usize) {
|
||||
let (vec_fr, read) = bytes_le_to_vec_fr(input);
|
||||
(vec_fr_to_field(&vec_fr), read)
|
||||
}
|
||||
|
||||
pub fn bytes_be_to_vec_field(input: &[u8]) -> (Vec<Field>, usize) {
|
||||
let (vec_fr, read) = bytes_be_to_vec_fr(input);
|
||||
(vec_fr_to_field(&vec_fr), read)
|
||||
}
|
||||
|
||||
// Arithmetic over Field elements (wrapped over arkworks algebra crate)
|
||||
|
||||
pub fn add(a: &Field, b: &Field) -> Field {
|
||||
to_field(&(to_fr(a) + to_fr(b)))
|
||||
}
|
||||
|
||||
pub fn mul(a: &Field, b: &Field) -> Field {
|
||||
to_field(&(to_fr(a) * to_fr(b)))
|
||||
}
|
||||
|
||||
pub fn div(a: &Field, b: &Field) -> Field {
|
||||
to_field(&(to_fr(a) / to_fr(b)))
|
||||
}
|
||||
|
||||
pub fn inv(a: &Field) -> Field {
|
||||
to_field(&(Fr::from(1) / to_fr(a)))
|
||||
}
|
||||
*/
|
||||
|
||||
667
rln/tests/ffi.rs
667
rln/tests/ffi.rs
File diff suppressed because it is too large
Load Diff
@@ -1,28 +1,38 @@
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Tests
|
||||
// Tests
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
#![cfg(not(feature = "stateless"))]
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use rln::hashers::{poseidon_hash, PoseidonHash};
|
||||
use rln::{circuit::*, poseidon_tree::PoseidonTree};
|
||||
use rln::{
|
||||
circuit::{Fr, TEST_TREE_DEPTH},
|
||||
poseidon_tree::PoseidonTree,
|
||||
};
|
||||
use utils::{FullMerkleTree, OptimalMerkleTree, ZerokitMerkleProof, ZerokitMerkleTree};
|
||||
|
||||
#[test]
|
||||
// The test is checked correctness for `FullMerkleTree` and `OptimalMerkleTree` with Poseidon hash
|
||||
// The test checked correctness for `FullMerkleTree` and `OptimalMerkleTree` with Poseidon hash
|
||||
fn test_zerokit_merkle_implementations() {
|
||||
let sample_size = 100;
|
||||
let leaves: Vec<Fr> = (0..sample_size).map(|s| Fr::from(s)).collect();
|
||||
let leaves: Vec<Fr> = (0..sample_size).map(Fr::from).collect();
|
||||
|
||||
let mut tree_full = FullMerkleTree::<PoseidonHash>::default(TEST_TREE_HEIGHT).unwrap();
|
||||
let mut tree_opt = OptimalMerkleTree::<PoseidonHash>::default(TEST_TREE_HEIGHT).unwrap();
|
||||
let mut tree_full = FullMerkleTree::<PoseidonHash>::default(TEST_TREE_DEPTH).unwrap();
|
||||
let mut tree_opt = OptimalMerkleTree::<PoseidonHash>::default(TEST_TREE_DEPTH).unwrap();
|
||||
|
||||
for i in 0..sample_size.try_into().unwrap() {
|
||||
tree_full.set(i, leaves[i]).unwrap();
|
||||
for (i, leave) in leaves
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.take(sample_size.try_into().unwrap())
|
||||
{
|
||||
tree_full.set(i, leave).unwrap();
|
||||
let proof = tree_full.proof(i).expect("index should be set");
|
||||
assert_eq!(proof.leaf_index(), i);
|
||||
|
||||
tree_opt.set(i, leaves[i]).unwrap();
|
||||
tree_opt.set(i, leave).unwrap();
|
||||
assert_eq!(tree_opt.root(), tree_full.root());
|
||||
let proof = tree_opt.proof(i).expect("index should be set");
|
||||
assert_eq!(proof.leaf_index(), i);
|
||||
}
|
||||
@@ -37,11 +47,11 @@ mod test {
|
||||
#[test]
|
||||
fn test_subtree_root() {
|
||||
const DEPTH: usize = 3;
|
||||
const LEAVES_LEN: usize = 6;
|
||||
const LEAVES_LEN: usize = 8;
|
||||
|
||||
let mut tree = PoseidonTree::default(DEPTH).unwrap();
|
||||
let leaves: Vec<Fr> = (0..LEAVES_LEN).map(|s| Fr::from(s as i32)).collect();
|
||||
let _ = tree.set_range(0, leaves);
|
||||
let _ = tree.set_range(0, leaves.into_iter());
|
||||
|
||||
for i in 0..LEAVES_LEN {
|
||||
// check leaves
|
||||
@@ -78,7 +88,7 @@ mod test {
|
||||
let leaves: Vec<Fr> = (0..nof_leaves).map(|s| Fr::from(s as i32)).collect();
|
||||
|
||||
// check set_range
|
||||
let _ = tree.set_range(0, leaves.clone());
|
||||
let _ = tree.set_range(0, leaves.clone().into_iter());
|
||||
assert!(tree.get_empty_leaves_indices().is_empty());
|
||||
|
||||
let mut vec_idxs = Vec::new();
|
||||
@@ -97,27 +107,29 @@ mod test {
|
||||
|
||||
// check remove_indices_and_set_leaves inside override_range function
|
||||
assert!(tree.get_empty_leaves_indices().is_empty());
|
||||
let leaves_2: Vec<Fr> = (0..2).map(|s| Fr::from(s as i32)).collect();
|
||||
tree.override_range(0, leaves_2.clone(), [0, 1, 2, 3])
|
||||
let leaves_2: Vec<Fr> = (0..2).map(Fr::from).collect();
|
||||
tree.override_range(0, leaves_2.clone().into_iter(), [0, 1, 2, 3].into_iter())
|
||||
.unwrap();
|
||||
assert_eq!(tree.get_empty_leaves_indices(), vec![2, 3]);
|
||||
|
||||
// check remove_indices inside override_range function
|
||||
tree.override_range(0, [], [0, 1]).unwrap();
|
||||
tree.override_range(0, [].into_iter(), [0, 1].into_iter())
|
||||
.unwrap();
|
||||
assert_eq!(tree.get_empty_leaves_indices(), vec![0, 1, 2, 3]);
|
||||
|
||||
// check set_range inside override_range function
|
||||
tree.override_range(0, leaves_2.clone(), []).unwrap();
|
||||
tree.override_range(0, leaves_2.clone().into_iter(), [].into_iter())
|
||||
.unwrap();
|
||||
assert_eq!(tree.get_empty_leaves_indices(), vec![2, 3]);
|
||||
|
||||
let leaves_4: Vec<Fr> = (0..4).map(|s| Fr::from(s as i32)).collect();
|
||||
let leaves_4: Vec<Fr> = (0..4).map(Fr::from).collect();
|
||||
// check if the indexes for write and delete are the same
|
||||
tree.override_range(0, leaves_4.clone(), [0, 1, 2, 3])
|
||||
tree.override_range(0, leaves_4.clone().into_iter(), [0, 1, 2, 3].into_iter())
|
||||
.unwrap();
|
||||
assert!(tree.get_empty_leaves_indices().is_empty());
|
||||
|
||||
// check if indexes for deletion are before indexes for overwriting
|
||||
tree.override_range(4, leaves_4.clone(), [0, 1, 2, 3])
|
||||
tree.override_range(4, leaves_4.clone().into_iter(), [0, 1, 2, 3].into_iter())
|
||||
.unwrap();
|
||||
// The result will be like this, because in the set_range function in pmtree
|
||||
// the next_index value is increased not by the number of elements to insert,
|
||||
@@ -128,7 +140,7 @@ mod test {
|
||||
);
|
||||
|
||||
// check if the indices for write and delete do not overlap completely
|
||||
tree.override_range(2, leaves_4.clone(), [0, 1, 2, 3])
|
||||
tree.override_range(2, leaves_4.clone().into_iter(), [0, 1, 2, 3].into_iter())
|
||||
.unwrap();
|
||||
// The result will be like this, because in the set_range function in pmtree
|
||||
// the next_index value is increased not by the number of elements to insert,
|
||||
|
||||
@@ -1,11 +1,18 @@
|
||||
#![cfg(not(feature = "stateless"))]
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use ark_ff::BigInt;
|
||||
use rln::circuit::zkey_from_folder;
|
||||
use rln::circuit::{circom_from_folder, vk_from_folder, Fr, TEST_TREE_HEIGHT};
|
||||
use rln::hashers::{hash_to_field, poseidon_hash};
|
||||
use rln::circuit::{graph_from_folder, zkey_from_folder};
|
||||
use rln::circuit::{Fr, TEST_TREE_DEPTH};
|
||||
use rln::hashers::{hash_to_field_le, poseidon_hash};
|
||||
use rln::poseidon_tree::PoseidonTree;
|
||||
use rln::protocol::*;
|
||||
use rln::protocol::{
|
||||
deserialize_proof_values, deserialize_witness, generate_proof, keygen,
|
||||
proof_values_from_witness, rln_witness_from_json, rln_witness_from_values,
|
||||
rln_witness_to_json, seeded_keygen, serialize_proof_values, serialize_witness,
|
||||
verify_proof, RLNWitnessInput,
|
||||
};
|
||||
use rln::utils::str_to_fr;
|
||||
use utils::{ZerokitMerkleProof, ZerokitMerkleTree};
|
||||
|
||||
@@ -17,19 +24,19 @@ mod test {
|
||||
let leaf_index = 3;
|
||||
|
||||
// generate identity
|
||||
let identity_secret_hash = hash_to_field(b"test-merkle-proof");
|
||||
let identity_secret_hash = hash_to_field_le(b"test-merkle-proof");
|
||||
let id_commitment = poseidon_hash(&[identity_secret_hash]);
|
||||
let rate_commitment = poseidon_hash(&[id_commitment, 100.into()]);
|
||||
|
||||
// generate merkle tree
|
||||
let default_leaf = Fr::from(0);
|
||||
let mut tree = PoseidonTree::new(
|
||||
TEST_TREE_HEIGHT,
|
||||
TEST_TREE_DEPTH,
|
||||
default_leaf,
|
||||
ConfigOf::<PoseidonTree>::default(),
|
||||
)
|
||||
.unwrap();
|
||||
tree.set(leaf_index, rate_commitment.into()).unwrap();
|
||||
tree.set(leaf_index, rate_commitment).unwrap();
|
||||
|
||||
// We check correct computation of the root
|
||||
let root = tree.root();
|
||||
@@ -95,26 +102,27 @@ mod test {
|
||||
//// generate merkle tree
|
||||
let default_leaf = Fr::from(0);
|
||||
let mut tree = PoseidonTree::new(
|
||||
TEST_TREE_HEIGHT,
|
||||
TEST_TREE_DEPTH,
|
||||
default_leaf,
|
||||
ConfigOf::<PoseidonTree>::default(),
|
||||
)
|
||||
.unwrap();
|
||||
tree.set(leaf_index, rate_commitment.into()).unwrap();
|
||||
tree.set(leaf_index, rate_commitment).unwrap();
|
||||
|
||||
let merkle_proof = tree.proof(leaf_index).expect("proof should exist");
|
||||
|
||||
let signal = b"hey hey";
|
||||
let x = hash_to_field(signal);
|
||||
let x = hash_to_field_le(signal);
|
||||
|
||||
// We set the remaining values to random ones
|
||||
let epoch = hash_to_field(b"test-epoch");
|
||||
let rln_identifier = hash_to_field(b"test-rln-identifier");
|
||||
let epoch = hash_to_field_le(b"test-epoch");
|
||||
let rln_identifier = hash_to_field_le(b"test-rln-identifier");
|
||||
let external_nullifier = poseidon_hash(&[epoch, rln_identifier]);
|
||||
|
||||
rln_witness_from_values(
|
||||
identity_secret_hash,
|
||||
&merkle_proof,
|
||||
merkle_proof.get_path_elements(),
|
||||
merkle_proof.get_path_index(),
|
||||
x,
|
||||
external_nullifier,
|
||||
user_message_limit,
|
||||
@@ -128,9 +136,8 @@ mod test {
|
||||
fn test_witness_from_json() {
|
||||
// We generate all relevant keys
|
||||
let proving_key = zkey_from_folder();
|
||||
let verification_key = vk_from_folder();
|
||||
let builder = circom_from_folder();
|
||||
|
||||
let verification_key = &proving_key.0.vk;
|
||||
let graph_data = graph_from_folder();
|
||||
// We compute witness from the json input
|
||||
let rln_witness = get_test_witness();
|
||||
let rln_witness_json = rln_witness_to_json(&rln_witness).unwrap();
|
||||
@@ -138,11 +145,11 @@ mod test {
|
||||
assert_eq!(rln_witness_deser, rln_witness);
|
||||
|
||||
// Let's generate a zkSNARK proof
|
||||
let proof = generate_proof(builder, &proving_key, &rln_witness_deser).unwrap();
|
||||
let proof = generate_proof(proving_key, &rln_witness_deser, graph_data).unwrap();
|
||||
let proof_values = proof_values_from_witness(&rln_witness_deser).unwrap();
|
||||
|
||||
// Let's verify the proof
|
||||
let verified = verify_proof(&verification_key, &proof, &proof_values);
|
||||
let verified = verify_proof(verification_key, &proof, &proof_values);
|
||||
|
||||
assert!(verified.unwrap());
|
||||
}
|
||||
@@ -157,16 +164,16 @@ mod test {
|
||||
|
||||
// We generate all relevant keys
|
||||
let proving_key = zkey_from_folder();
|
||||
let verification_key = vk_from_folder();
|
||||
let builder = circom_from_folder();
|
||||
let verification_key = &proving_key.0.vk;
|
||||
let graph_data = graph_from_folder();
|
||||
|
||||
// Let's generate a zkSNARK proof
|
||||
let proof = generate_proof(builder, &proving_key, &rln_witness_deser).unwrap();
|
||||
let proof = generate_proof(proving_key, &rln_witness_deser, graph_data).unwrap();
|
||||
|
||||
let proof_values = proof_values_from_witness(&rln_witness_deser).unwrap();
|
||||
|
||||
// Let's verify the proof
|
||||
let success = verify_proof(&verification_key, &proof, &proof_values).unwrap();
|
||||
let success = verify_proof(verification_key, &proof, &proof_values).unwrap();
|
||||
|
||||
assert!(success);
|
||||
}
|
||||
|
||||
@@ -1,13 +1,37 @@
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use ark_ff::BigInt;
|
||||
#[cfg(not(feature = "stateless"))]
|
||||
use {
|
||||
ark_ff::BigInt,
|
||||
rln::{
|
||||
circuit::TEST_TREE_DEPTH,
|
||||
protocol::compute_tree_root,
|
||||
public::RLN,
|
||||
utils::{
|
||||
bytes_le_to_vec_fr, bytes_le_to_vec_u8, bytes_le_to_vec_usize, fr_to_bytes_le,
|
||||
generate_input_buffer, IdSecret,
|
||||
},
|
||||
},
|
||||
zeroize::Zeroize,
|
||||
};
|
||||
|
||||
use ark_std::{rand::thread_rng, UniformRand};
|
||||
use rand::Rng;
|
||||
use rln::circuit::{Fr, TEST_TREE_HEIGHT};
|
||||
use rln::hashers::{hash_to_field, poseidon_hash as utils_poseidon_hash, ROUND_PARAMS};
|
||||
use rln::protocol::{compute_tree_root, deserialize_identity_tuple};
|
||||
use rln::public::{hash as public_hash, poseidon_hash as public_poseidon_hash, RLN};
|
||||
use rln::utils::*;
|
||||
use rln::circuit::Fr;
|
||||
use rln::hashers::{
|
||||
hash_to_field_be, hash_to_field_le, poseidon_hash as utils_poseidon_hash, ROUND_PARAMS,
|
||||
};
|
||||
use rln::protocol::{
|
||||
deserialize_identity_pair_be, deserialize_identity_pair_le, deserialize_identity_tuple_be,
|
||||
deserialize_identity_tuple_le,
|
||||
};
|
||||
use rln::public::{
|
||||
hash as public_hash, poseidon_hash as public_poseidon_hash, seeded_extended_key_gen,
|
||||
seeded_key_gen,
|
||||
};
|
||||
use rln::utils::{
|
||||
bytes_be_to_fr, bytes_le_to_fr, str_to_fr, vec_fr_to_bytes_be, vec_fr_to_bytes_le,
|
||||
};
|
||||
use std::io::Cursor;
|
||||
|
||||
#[test]
|
||||
@@ -17,11 +41,16 @@ mod test {
|
||||
let leaf_index = 3;
|
||||
let user_message_limit = 1;
|
||||
|
||||
let mut rln = RLN::new(TEST_TREE_HEIGHT, generate_input_buffer()).unwrap();
|
||||
let mut rln = RLN::new(TEST_TREE_DEPTH, generate_input_buffer()).unwrap();
|
||||
|
||||
// generate identity
|
||||
let identity_secret_hash = hash_to_field(b"test-merkle-proof");
|
||||
let id_commitment = utils_poseidon_hash(&vec![identity_secret_hash]);
|
||||
let mut identity_secret_hash_ = hash_to_field_le(b"test-merkle-proof");
|
||||
let identity_secret_hash = IdSecret::from(&mut identity_secret_hash_);
|
||||
|
||||
let mut to_hash = [*identity_secret_hash.clone()];
|
||||
let id_commitment = utils_poseidon_hash(&to_hash);
|
||||
to_hash[0].zeroize();
|
||||
|
||||
let rate_commitment = utils_poseidon_hash(&[id_commitment, user_message_limit.into()]);
|
||||
|
||||
// check that leaves indices is empty
|
||||
@@ -61,7 +90,7 @@ mod test {
|
||||
|
||||
let buffer_inner = buffer.into_inner();
|
||||
let (path_elements, read) = bytes_le_to_vec_fr(&buffer_inner).unwrap();
|
||||
let (identity_path_index, _) = bytes_le_to_vec_u8(&buffer_inner[read..].to_vec()).unwrap();
|
||||
let (identity_path_index, _) = bytes_le_to_vec_u8(&buffer_inner[read..]).unwrap();
|
||||
|
||||
// We check correct computation of the path and indexes
|
||||
let expected_path_elements: Vec<Fr> = [
|
||||
@@ -97,9 +126,9 @@ mod test {
|
||||
|
||||
// check subtree root computation for leaf 0 for all corresponding node until the root
|
||||
let l_idx = 0;
|
||||
for n in (1..=TEST_TREE_HEIGHT).rev() {
|
||||
let idx_l = l_idx * (1 << (TEST_TREE_HEIGHT - n));
|
||||
let idx_r = (l_idx + 1) * (1 << (TEST_TREE_HEIGHT - n));
|
||||
for n in (1..=TEST_TREE_DEPTH).rev() {
|
||||
let idx_l = l_idx * (1 << (TEST_TREE_DEPTH - n));
|
||||
let idx_r = (l_idx + 1) * (1 << (TEST_TREE_DEPTH - n));
|
||||
let idx_sr = idx_l;
|
||||
|
||||
let mut buffer = Cursor::new(Vec::<u8>::new());
|
||||
@@ -131,19 +160,46 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn test_seeded_keygen() {
|
||||
let rln = RLN::default();
|
||||
|
||||
let seed_bytes: &[u8] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||
|
||||
let mut input_buffer = Cursor::new(&seed_bytes);
|
||||
let mut output_buffer = Cursor::new(Vec::<u8>::new());
|
||||
|
||||
rln.seeded_key_gen(&mut input_buffer, &mut output_buffer)
|
||||
.unwrap();
|
||||
seeded_key_gen(&mut input_buffer, &mut output_buffer, true).unwrap();
|
||||
let serialized_output = output_buffer.into_inner();
|
||||
|
||||
let (identity_secret_hash, read) = bytes_le_to_fr(&serialized_output);
|
||||
let (id_commitment, _) = bytes_le_to_fr(&serialized_output[read..].to_vec());
|
||||
let (identity_secret_hash, id_commitment) = deserialize_identity_pair_le(serialized_output);
|
||||
|
||||
// We check against expected values
|
||||
let expected_identity_secret_hash_seed_bytes = str_to_fr(
|
||||
"0x766ce6c7e7a01bdf5b3f257616f603918c30946fa23480f2859c597817e6716",
|
||||
16,
|
||||
)
|
||||
.unwrap();
|
||||
let expected_id_commitment_seed_bytes = str_to_fr(
|
||||
"0xbf16d2b5c0d6f9d9d561e05bfca16a81b4b873bb063508fae360d8c74cef51f",
|
||||
16,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
identity_secret_hash,
|
||||
expected_identity_secret_hash_seed_bytes
|
||||
);
|
||||
assert_eq!(id_commitment, expected_id_commitment_seed_bytes);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_seeded_keygen_big_endian() {
|
||||
let seed_bytes: &[u8] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||
|
||||
let mut input_buffer = Cursor::new(&seed_bytes);
|
||||
let mut output_buffer = Cursor::new(Vec::<u8>::new());
|
||||
|
||||
seeded_key_gen(&mut input_buffer, &mut output_buffer, false).unwrap();
|
||||
let serialized_output = output_buffer.into_inner();
|
||||
|
||||
let (identity_secret_hash, id_commitment) = deserialize_identity_pair_be(serialized_output);
|
||||
|
||||
// We check against expected values
|
||||
let expected_identity_secret_hash_seed_bytes = str_to_fr(
|
||||
@@ -166,19 +222,60 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn test_seeded_extended_keygen() {
|
||||
let rln = RLN::default();
|
||||
|
||||
let seed_bytes: &[u8] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||
|
||||
let mut input_buffer = Cursor::new(&seed_bytes);
|
||||
let mut output_buffer = Cursor::new(Vec::<u8>::new());
|
||||
|
||||
rln.seeded_extended_key_gen(&mut input_buffer, &mut output_buffer)
|
||||
.unwrap();
|
||||
seeded_extended_key_gen(&mut input_buffer, &mut output_buffer, true).unwrap();
|
||||
let serialized_output = output_buffer.into_inner();
|
||||
|
||||
let (identity_trapdoor, identity_nullifier, identity_secret_hash, id_commitment) =
|
||||
deserialize_identity_tuple(serialized_output);
|
||||
deserialize_identity_tuple_le(serialized_output);
|
||||
|
||||
// We check against expected values
|
||||
let expected_identity_trapdoor_seed_bytes = str_to_fr(
|
||||
"0x766ce6c7e7a01bdf5b3f257616f603918c30946fa23480f2859c597817e6716",
|
||||
16,
|
||||
)
|
||||
.unwrap();
|
||||
let expected_identity_nullifier_seed_bytes = str_to_fr(
|
||||
"0x1f18714c7bc83b5bca9e89d404cf6f2f585bc4c0f7ed8b53742b7e2b298f50b4",
|
||||
16,
|
||||
)
|
||||
.unwrap();
|
||||
let expected_identity_secret_hash_seed_bytes = str_to_fr(
|
||||
"0x2aca62aaa7abaf3686fff2caf00f55ab9462dc12db5b5d4bcf3994e671f8e521",
|
||||
16,
|
||||
)
|
||||
.unwrap();
|
||||
let expected_id_commitment_seed_bytes = str_to_fr(
|
||||
"0x68b66aa0a8320d2e56842581553285393188714c48f9b17acd198b4f1734c5c",
|
||||
16,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(identity_trapdoor, expected_identity_trapdoor_seed_bytes);
|
||||
assert_eq!(identity_nullifier, expected_identity_nullifier_seed_bytes);
|
||||
assert_eq!(
|
||||
identity_secret_hash,
|
||||
expected_identity_secret_hash_seed_bytes
|
||||
);
|
||||
assert_eq!(id_commitment, expected_id_commitment_seed_bytes);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_seeded_extended_keygen_big_endian() {
|
||||
let seed_bytes: &[u8] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||
|
||||
let mut input_buffer = Cursor::new(&seed_bytes);
|
||||
let mut output_buffer = Cursor::new(Vec::<u8>::new());
|
||||
|
||||
seeded_extended_key_gen(&mut input_buffer, &mut output_buffer, false).unwrap();
|
||||
let serialized_output = output_buffer.into_inner();
|
||||
|
||||
let (identity_trapdoor, identity_nullifier, identity_secret_hash, id_commitment) =
|
||||
deserialize_identity_tuple_be(serialized_output);
|
||||
|
||||
// We check against expected values
|
||||
let expected_identity_trapdoor_seed_bytes = str_to_fr(
|
||||
@@ -219,11 +316,28 @@ mod test {
|
||||
let mut input_buffer = Cursor::new(&signal);
|
||||
let mut output_buffer = Cursor::new(Vec::<u8>::new());
|
||||
|
||||
public_hash(&mut input_buffer, &mut output_buffer).unwrap();
|
||||
public_hash(&mut input_buffer, &mut output_buffer, true).unwrap();
|
||||
let serialized_hash = output_buffer.into_inner();
|
||||
let (hash1, _) = bytes_le_to_fr(&serialized_hash);
|
||||
|
||||
let hash2 = hash_to_field(&signal);
|
||||
let hash2 = hash_to_field_le(&signal);
|
||||
|
||||
assert_eq!(hash1, hash2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hash_to_field_big_endian() {
|
||||
let mut rng = thread_rng();
|
||||
let signal: [u8; 32] = rng.gen();
|
||||
|
||||
let mut input_buffer = Cursor::new(&signal);
|
||||
let mut output_buffer = Cursor::new(Vec::<u8>::new());
|
||||
|
||||
public_hash(&mut input_buffer, &mut output_buffer, false).unwrap();
|
||||
let serialized_hash = output_buffer.into_inner();
|
||||
let (hash1, _) = bytes_be_to_fr(&serialized_hash);
|
||||
|
||||
let hash2 = hash_to_field_be(&signal);
|
||||
|
||||
assert_eq!(hash1, hash2);
|
||||
}
|
||||
@@ -238,13 +352,33 @@ mod test {
|
||||
}
|
||||
let expected_hash = utils_poseidon_hash(&inputs);
|
||||
|
||||
let mut input_buffer = Cursor::new(vec_fr_to_bytes_le(&inputs).unwrap());
|
||||
let mut input_buffer = Cursor::new(vec_fr_to_bytes_le(&inputs));
|
||||
let mut output_buffer = Cursor::new(Vec::<u8>::new());
|
||||
|
||||
public_poseidon_hash(&mut input_buffer, &mut output_buffer).unwrap();
|
||||
public_poseidon_hash(&mut input_buffer, &mut output_buffer, true).unwrap();
|
||||
let serialized_hash = output_buffer.into_inner();
|
||||
let (hash, _) = bytes_le_to_fr(&serialized_hash);
|
||||
|
||||
assert_eq!(hash, expected_hash);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_poseidon_hash_big_endian() {
|
||||
let mut rng = thread_rng();
|
||||
let number_of_inputs = rng.gen_range(1..ROUND_PARAMS.len());
|
||||
let mut inputs = Vec::with_capacity(number_of_inputs);
|
||||
for _ in 0..number_of_inputs {
|
||||
inputs.push(Fr::rand(&mut rng));
|
||||
}
|
||||
let expected_hash = utils_poseidon_hash(&inputs);
|
||||
|
||||
let mut input_buffer = Cursor::new(vec_fr_to_bytes_be(&inputs));
|
||||
let mut output_buffer = Cursor::new(Vec::<u8>::new());
|
||||
|
||||
public_poseidon_hash(&mut input_buffer, &mut output_buffer, false).unwrap();
|
||||
let serialized_hash = output_buffer.into_inner();
|
||||
let (hash, _) = bytes_be_to_fr(&serialized_hash);
|
||||
|
||||
assert_eq!(hash, expected_hash);
|
||||
}
|
||||
}
|
||||
|
||||
411
rln/tests/utils.rs
Normal file
411
rln/tests/utils.rs
Normal file
@@ -0,0 +1,411 @@
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use rln::utils::{
|
||||
bytes_be_to_fr, bytes_be_to_vec_fr, bytes_be_to_vec_u8, bytes_be_to_vec_usize,
|
||||
bytes_le_to_fr, bytes_le_to_vec_fr, bytes_le_to_vec_u8, bytes_le_to_vec_usize,
|
||||
fr_to_bytes_be, fr_to_bytes_le, normalize_usize_be, normalize_usize_le, str_to_fr,
|
||||
vec_fr_to_bytes_be, vec_fr_to_bytes_le, vec_u8_to_bytes_be, vec_u8_to_bytes_le,
|
||||
};
|
||||
|
||||
use ark_std::{rand::thread_rng, UniformRand};
|
||||
use rln::circuit::Fr;
|
||||
|
||||
#[test]
|
||||
fn test_normalize_usize_le() {
|
||||
// Test basic cases
|
||||
assert_eq!(normalize_usize_le(0), [0, 0, 0, 0, 0, 0, 0, 0]);
|
||||
assert_eq!(normalize_usize_le(1), [1, 0, 0, 0, 0, 0, 0, 0]);
|
||||
assert_eq!(normalize_usize_le(255), [255, 0, 0, 0, 0, 0, 0, 0]);
|
||||
assert_eq!(normalize_usize_le(256), [0, 1, 0, 0, 0, 0, 0, 0]);
|
||||
assert_eq!(normalize_usize_le(65535), [255, 255, 0, 0, 0, 0, 0, 0]);
|
||||
assert_eq!(normalize_usize_le(65536), [0, 0, 1, 0, 0, 0, 0, 0]);
|
||||
|
||||
// Test 32-bit boundary
|
||||
assert_eq!(
|
||||
normalize_usize_le(4294967295),
|
||||
[255, 255, 255, 255, 0, 0, 0, 0]
|
||||
);
|
||||
assert_eq!(normalize_usize_le(4294967296), [0, 0, 0, 0, 1, 0, 0, 0]);
|
||||
|
||||
// Test maximum value
|
||||
assert_eq!(
|
||||
normalize_usize_le(usize::MAX),
|
||||
[255, 255, 255, 255, 255, 255, 255, 255]
|
||||
);
|
||||
|
||||
// Test that result is always 8 bytes
|
||||
assert_eq!(normalize_usize_le(0).len(), 8);
|
||||
assert_eq!(normalize_usize_le(usize::MAX).len(), 8);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_normalize_usize_be() {
|
||||
// Test basic cases
|
||||
assert_eq!(normalize_usize_be(0), [0, 0, 0, 0, 0, 0, 0, 0]);
|
||||
assert_eq!(normalize_usize_be(1), [0, 0, 0, 0, 0, 0, 0, 1]);
|
||||
assert_eq!(normalize_usize_be(255), [0, 0, 0, 0, 0, 0, 0, 255]);
|
||||
assert_eq!(normalize_usize_be(256), [0, 0, 0, 0, 0, 0, 1, 0]);
|
||||
assert_eq!(normalize_usize_be(65535), [0, 0, 0, 0, 0, 0, 255, 255]);
|
||||
assert_eq!(normalize_usize_be(65536), [0, 0, 0, 0, 0, 1, 0, 0]);
|
||||
|
||||
// Test 32-bit boundary
|
||||
assert_eq!(
|
||||
normalize_usize_be(4294967295),
|
||||
[0, 0, 0, 0, 255, 255, 255, 255]
|
||||
);
|
||||
assert_eq!(normalize_usize_be(4294967296), [0, 0, 0, 1, 0, 0, 0, 0]);
|
||||
|
||||
// Test maximum value
|
||||
assert_eq!(
|
||||
normalize_usize_be(usize::MAX),
|
||||
[255, 255, 255, 255, 255, 255, 255, 255]
|
||||
);
|
||||
|
||||
// Test that result is always 8 bytes
|
||||
assert_eq!(normalize_usize_be(0).len(), 8);
|
||||
assert_eq!(normalize_usize_be(usize::MAX).len(), 8);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_normalize_usize_endianness() {
|
||||
// Test that little-endian and big-endian produce different results for non-zero values
|
||||
let test_values = vec![1, 255, 256, 65535, 65536, 4294967295, 4294967296];
|
||||
|
||||
for &value in &test_values {
|
||||
let le_result = normalize_usize_le(value);
|
||||
let be_result = normalize_usize_be(value);
|
||||
|
||||
// For non-zero values, LE and BE should be different
|
||||
assert_ne!(
|
||||
le_result, be_result,
|
||||
"LE and BE should differ for value {value}"
|
||||
);
|
||||
|
||||
// Both should be 8 bytes
|
||||
assert_eq!(le_result.len(), 8);
|
||||
assert_eq!(be_result.len(), 8);
|
||||
}
|
||||
|
||||
// Zero should be the same in both endianness
|
||||
assert_eq!(normalize_usize_le(0), normalize_usize_be(0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_normalize_usize_roundtrip() {
|
||||
// Test that we can reconstruct the original value from the normalized bytes
|
||||
let test_values = vec![
|
||||
0,
|
||||
1,
|
||||
255,
|
||||
256,
|
||||
65535,
|
||||
65536,
|
||||
4294967295,
|
||||
4294967296,
|
||||
usize::MAX,
|
||||
];
|
||||
|
||||
for &value in &test_values {
|
||||
let le_bytes = normalize_usize_le(value);
|
||||
let be_bytes = normalize_usize_be(value);
|
||||
|
||||
// Reconstruct from little-endian bytes
|
||||
let reconstructed_le = usize::from_le_bytes(le_bytes);
|
||||
assert_eq!(
|
||||
reconstructed_le, value,
|
||||
"LE roundtrip failed for value {value}"
|
||||
);
|
||||
|
||||
// Reconstruct from big-endian bytes
|
||||
let reconstructed_be = usize::from_be_bytes(be_bytes);
|
||||
assert_eq!(
|
||||
reconstructed_be, value,
|
||||
"BE roundtrip failed for value {value}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_normalize_usize_edge_cases() {
|
||||
// Test edge cases and boundary values
|
||||
let edge_cases = vec![
|
||||
0,
|
||||
1,
|
||||
255,
|
||||
256,
|
||||
65535,
|
||||
65536,
|
||||
16777215, // 2^24 - 1
|
||||
16777216, // 2^24
|
||||
4294967295, // 2^32 - 1
|
||||
4294967296, // 2^32
|
||||
1099511627775, // 2^40 - 1
|
||||
1099511627776, // 2^40
|
||||
281474976710655, // 2^48 - 1
|
||||
281474976710656, // 2^48
|
||||
72057594037927935, // 2^56 - 1
|
||||
72057594037927936, // 2^56
|
||||
usize::MAX,
|
||||
];
|
||||
|
||||
for &value in &edge_cases {
|
||||
let le_result = normalize_usize_le(value);
|
||||
let be_result = normalize_usize_be(value);
|
||||
|
||||
// Both should be 8 bytes
|
||||
assert_eq!(le_result.len(), 8);
|
||||
assert_eq!(be_result.len(), 8);
|
||||
|
||||
// Roundtrip should work
|
||||
assert_eq!(usize::from_le_bytes(le_result), value);
|
||||
assert_eq!(usize::from_be_bytes(be_result), value);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_normalize_usize_architecture_independence() {
|
||||
// Test that the functions work consistently regardless of the underlying architecture
|
||||
// This test ensures that the functions provide consistent 8-byte output
|
||||
// even on 32-bit systems where usize might be 4 bytes
|
||||
|
||||
let test_values = vec![0, 1, 255, 256, 65535, 65536, 4294967295, 4294967296];
|
||||
|
||||
for &value in &test_values {
|
||||
let le_result = normalize_usize_le(value);
|
||||
let be_result = normalize_usize_be(value);
|
||||
|
||||
// Always 8 bytes regardless of architecture
|
||||
assert_eq!(le_result.len(), 8);
|
||||
assert_eq!(be_result.len(), 8);
|
||||
|
||||
// The result should be consistent with the original value
|
||||
assert_eq!(usize::from_le_bytes(le_result), value);
|
||||
assert_eq!(usize::from_be_bytes(be_result), value);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fr_serialization_roundtrip() {
|
||||
let mut rng = thread_rng();
|
||||
|
||||
// Test multiple random Fr values
|
||||
for _ in 0..10 {
|
||||
let fr = Fr::rand(&mut rng);
|
||||
|
||||
// Test little-endian roundtrip
|
||||
let le_bytes = fr_to_bytes_le(&fr);
|
||||
let (reconstructed_le, _) = bytes_le_to_fr(&le_bytes);
|
||||
assert_eq!(fr, reconstructed_le);
|
||||
|
||||
// Test big-endian roundtrip
|
||||
let be_bytes = fr_to_bytes_be(&fr);
|
||||
let (reconstructed_be, _) = bytes_be_to_fr(&be_bytes);
|
||||
assert_eq!(fr, reconstructed_be);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_vec_fr_serialization_roundtrip() {
|
||||
let mut rng = thread_rng();
|
||||
|
||||
// Test with different vector sizes
|
||||
for size in [0, 1, 5, 10] {
|
||||
let fr_vec: Vec<Fr> = (0..size).map(|_| Fr::rand(&mut rng)).collect();
|
||||
|
||||
// Test little-endian roundtrip
|
||||
let le_bytes = vec_fr_to_bytes_le(&fr_vec);
|
||||
let (reconstructed_le, _) = bytes_le_to_vec_fr(&le_bytes).unwrap();
|
||||
assert_eq!(fr_vec, reconstructed_le);
|
||||
|
||||
// Test big-endian roundtrip
|
||||
let be_bytes = vec_fr_to_bytes_be(&fr_vec);
|
||||
let (reconstructed_be, _) = bytes_be_to_vec_fr(&be_bytes).unwrap();
|
||||
assert_eq!(fr_vec, reconstructed_be);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_vec_u8_serialization_roundtrip() {
|
||||
// Test with different vector sizes and content
|
||||
let test_cases = vec![
|
||||
vec![],
|
||||
vec![0],
|
||||
vec![255],
|
||||
vec![1, 2, 3, 4, 5],
|
||||
vec![0, 255, 128, 64, 32, 16, 8, 4, 2, 1],
|
||||
(0..100).collect::<Vec<u8>>(),
|
||||
];
|
||||
|
||||
for test_case in test_cases {
|
||||
// Test little-endian roundtrip
|
||||
let le_bytes = vec_u8_to_bytes_le(&test_case);
|
||||
let (reconstructed_le, _) = bytes_le_to_vec_u8(&le_bytes).unwrap();
|
||||
assert_eq!(test_case, reconstructed_le);
|
||||
|
||||
// Test big-endian roundtrip
|
||||
let be_bytes = vec_u8_to_bytes_be(&test_case);
|
||||
let (reconstructed_be, _) = bytes_be_to_vec_u8(&be_bytes).unwrap();
|
||||
assert_eq!(test_case, reconstructed_be);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_vec_usize_serialization_roundtrip() {
|
||||
// Test with different vector sizes and content
|
||||
let test_cases = vec![
|
||||
vec![],
|
||||
vec![0],
|
||||
vec![usize::MAX],
|
||||
vec![1, 2, 3, 4, 5],
|
||||
vec![0, 255, 65535, 4294967295, usize::MAX],
|
||||
(0..10).collect::<Vec<usize>>(),
|
||||
];
|
||||
|
||||
for test_case in test_cases {
|
||||
// Test little-endian roundtrip
|
||||
let le_bytes = {
|
||||
let mut bytes = Vec::new();
|
||||
bytes.extend_from_slice(&normalize_usize_le(test_case.len()));
|
||||
for &value in &test_case {
|
||||
bytes.extend_from_slice(&normalize_usize_le(value));
|
||||
}
|
||||
bytes
|
||||
};
|
||||
let reconstructed_le = bytes_le_to_vec_usize(&le_bytes).unwrap();
|
||||
assert_eq!(test_case, reconstructed_le);
|
||||
|
||||
// Test big-endian roundtrip
|
||||
let be_bytes = {
|
||||
let mut bytes = Vec::new();
|
||||
bytes.extend_from_slice(&normalize_usize_be(test_case.len()));
|
||||
for &value in &test_case {
|
||||
bytes.extend_from_slice(&normalize_usize_be(value));
|
||||
}
|
||||
bytes
|
||||
};
|
||||
let reconstructed_be = bytes_be_to_vec_usize(&be_bytes).unwrap();
|
||||
assert_eq!(test_case, reconstructed_be);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_str_to_fr() {
|
||||
// Test valid hex strings
|
||||
let test_cases = vec![
|
||||
("0x0", 16, Fr::from(0u64)),
|
||||
("0x1", 16, Fr::from(1u64)),
|
||||
("0xff", 16, Fr::from(255u64)),
|
||||
("0x100", 16, Fr::from(256u64)),
|
||||
];
|
||||
|
||||
for (input, radix, expected) in test_cases {
|
||||
let result = str_to_fr(input, radix).unwrap();
|
||||
assert_eq!(result, expected);
|
||||
}
|
||||
|
||||
// Test invalid inputs
|
||||
assert!(str_to_fr("invalid", 16).is_err());
|
||||
assert!(str_to_fr("0x", 16).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_endianness_differences() {
|
||||
let mut rng = thread_rng();
|
||||
let fr = Fr::rand(&mut rng);
|
||||
|
||||
// Test that LE and BE produce different byte representations
|
||||
let le_bytes = fr_to_bytes_le(&fr);
|
||||
let be_bytes = fr_to_bytes_be(&fr);
|
||||
|
||||
// They should be different (unless the value is symmetric)
|
||||
if le_bytes != be_bytes {
|
||||
// Verify they can both be reconstructed correctly
|
||||
let (reconstructed_le, _) = bytes_le_to_fr(&le_bytes);
|
||||
let (reconstructed_be, _) = bytes_be_to_fr(&be_bytes);
|
||||
assert_eq!(fr, reconstructed_le);
|
||||
assert_eq!(fr, reconstructed_be);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_error_handling() {
|
||||
// Test with valid length but insufficient data
|
||||
let valid_length_invalid_data = vec![0u8; 8]; // Length 0, but no data
|
||||
assert!(bytes_le_to_vec_u8(&valid_length_invalid_data).is_ok());
|
||||
assert!(bytes_be_to_vec_u8(&valid_length_invalid_data).is_ok());
|
||||
assert!(bytes_le_to_vec_fr(&valid_length_invalid_data).is_ok());
|
||||
assert!(bytes_be_to_vec_fr(&valid_length_invalid_data).is_ok());
|
||||
assert!(bytes_le_to_vec_usize(&valid_length_invalid_data).is_ok());
|
||||
assert!(bytes_be_to_vec_usize(&valid_length_invalid_data).is_ok());
|
||||
|
||||
// Test with reasonable length but insufficient data for vector deserialization
|
||||
let reasonable_length = {
|
||||
let mut bytes = vec![0u8; 8];
|
||||
bytes[0] = 1; // Length 1
|
||||
bytes
|
||||
};
|
||||
// This should fail because we don't have enough data for the vector elements
|
||||
assert!(bytes_le_to_vec_u8(&reasonable_length).is_err());
|
||||
assert!(bytes_be_to_vec_u8(&reasonable_length).is_err());
|
||||
assert!(bytes_le_to_vec_fr(&reasonable_length).is_err());
|
||||
assert!(bytes_be_to_vec_fr(&reasonable_length).is_err());
|
||||
assert!(bytes_le_to_vec_usize(&reasonable_length).is_err());
|
||||
assert!(bytes_be_to_vec_usize(&reasonable_length).is_err());
|
||||
|
||||
// Test with valid data for u8 vector
|
||||
let valid_u8_data_le = {
|
||||
let mut bytes = vec![0u8; 9];
|
||||
bytes[..8].copy_from_slice(&(1u64.to_le_bytes())); // Length 1, little-endian
|
||||
bytes[8] = 42; // One byte of data
|
||||
bytes
|
||||
};
|
||||
let valid_u8_data_be = {
|
||||
let mut bytes = vec![0u8; 9];
|
||||
bytes[..8].copy_from_slice(&(1u64.to_be_bytes())); // Length 1, big-endian
|
||||
bytes[8] = 42; // One byte of data
|
||||
bytes
|
||||
};
|
||||
assert!(bytes_le_to_vec_u8(&valid_u8_data_le).is_ok());
|
||||
assert!(bytes_be_to_vec_u8(&valid_u8_data_be).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_empty_vectors() {
|
||||
// Test empty vector serialization/deserialization
|
||||
let empty_fr: Vec<Fr> = vec![];
|
||||
let empty_u8: Vec<u8> = vec![];
|
||||
let empty_usize: Vec<usize> = vec![];
|
||||
|
||||
// Test Fr vectors
|
||||
let le_fr_bytes = vec_fr_to_bytes_le(&empty_fr);
|
||||
let be_fr_bytes = vec_fr_to_bytes_be(&empty_fr);
|
||||
let (reconstructed_le_fr, _) = bytes_le_to_vec_fr(&le_fr_bytes).unwrap();
|
||||
let (reconstructed_be_fr, _) = bytes_be_to_vec_fr(&be_fr_bytes).unwrap();
|
||||
assert_eq!(empty_fr, reconstructed_le_fr);
|
||||
assert_eq!(empty_fr, reconstructed_be_fr);
|
||||
|
||||
// Test u8 vectors
|
||||
let le_u8_bytes = vec_u8_to_bytes_le(&empty_u8);
|
||||
let be_u8_bytes = vec_u8_to_bytes_be(&empty_u8);
|
||||
let (reconstructed_le_u8, _) = bytes_le_to_vec_u8(&le_u8_bytes).unwrap();
|
||||
let (reconstructed_be_u8, _) = bytes_be_to_vec_u8(&be_u8_bytes).unwrap();
|
||||
assert_eq!(empty_u8, reconstructed_le_u8);
|
||||
assert_eq!(empty_u8, reconstructed_be_u8);
|
||||
|
||||
// Test usize vectors
|
||||
let le_usize_bytes = {
|
||||
let mut bytes = Vec::new();
|
||||
bytes.extend_from_slice(&normalize_usize_le(0));
|
||||
bytes
|
||||
};
|
||||
let be_usize_bytes = {
|
||||
let mut bytes = Vec::new();
|
||||
bytes.extend_from_slice(&normalize_usize_be(0));
|
||||
bytes
|
||||
};
|
||||
let reconstructed_le_usize = bytes_le_to_vec_usize(&le_usize_bytes).unwrap();
|
||||
let reconstructed_be_usize = bytes_be_to_vec_usize(&be_usize_bytes).unwrap();
|
||||
assert_eq!(empty_usize, reconstructed_le_usize);
|
||||
assert_eq!(empty_usize, reconstructed_be_usize);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "zerokit_utils"
|
||||
version = "0.5.1"
|
||||
version = "0.6.0"
|
||||
edition = "2021"
|
||||
license = "MIT OR Apache-2.0"
|
||||
description = "Various utilities for Zerokit"
|
||||
@@ -12,29 +12,35 @@ repository = "https://github.com/vacp2p/zerokit"
|
||||
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 = "=2.0.2", optional = true }
|
||||
sled = "=0.34.7"
|
||||
serde = "=1.0.163"
|
||||
lazy_static = "1.4.0"
|
||||
hex = "0.4"
|
||||
ark-ff = { version = "0.5.0", default-features = false }
|
||||
num-bigint = { version = "0.4.6", default-features = false }
|
||||
pmtree = { package = "vacp2p_pmtree", version = "2.0.3", optional = true }
|
||||
sled = "0.34.7"
|
||||
serde_json = "1.0.141"
|
||||
lazy_static = "1.5.0"
|
||||
hex = "0.4.3"
|
||||
rayon = "1.10.0"
|
||||
thiserror = "2.0"
|
||||
|
||||
[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"] }
|
||||
ark-bn254 = { version = "0.5.0", features = ["std"] }
|
||||
num-traits = "0.2.19"
|
||||
hex-literal = "0.4.1"
|
||||
tiny-keccak = { version = "2.0.2", features = ["keccak"] }
|
||||
criterion = { version = "0.7.0", features = ["html_reports"] }
|
||||
|
||||
[features]
|
||||
default = ["parallel"]
|
||||
default = []
|
||||
parallel = ["ark-ff/parallel"]
|
||||
pmtree-ft = ["pmtree"]
|
||||
|
||||
[[bench]]
|
||||
name = "merkle_tree_benchmark"
|
||||
harness = false
|
||||
|
||||
[[bench]]
|
||||
name = "poseidon_benchmark"
|
||||
harness = false
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
all-features = true
|
||||
|
||||
@@ -8,4 +8,4 @@ args = ["test", "--release"]
|
||||
|
||||
[tasks.bench]
|
||||
command = "cargo"
|
||||
args = ["bench"]
|
||||
args = ["bench"]
|
||||
|
||||
134
utils/README.md
134
utils/README.md
@@ -1,15 +1,133 @@
|
||||
# Utils crate
|
||||
# Zerokit Utils Crate
|
||||
|
||||
## Building
|
||||
[](https://crates.io/crates/zerokit_utils)
|
||||
[](https://opensource.org/licenses/MIT)
|
||||
[](https://opensource.org/licenses/Apache-2.0)
|
||||
|
||||
1. `cargo build`
|
||||
**Zerokit Utils** provides essential cryptographic primitives optimized for zero-knowledge applications.
|
||||
This crate features efficient Merkle tree implementations and a Poseidon hash function,
|
||||
designed to be robust and performant.
|
||||
|
||||
## Testing
|
||||
## Overview
|
||||
|
||||
1. `cargo test`
|
||||
This crate provides core cryptographic components optimized for zero-knowledge proof systems:
|
||||
|
||||
## Benchmarking
|
||||
- **Multiple Merkle Trees**: Various implementations optimised for the trade-off between space and time.
|
||||
- **Poseidon Hash Function**: An efficient hashing algorithm suitable for ZK contexts, with customizable parameters.
|
||||
- **Parallel Performance**: Leverages Rayon for significant speed-ups in Merkle tree computations.
|
||||
- **Arkworks Compatibility**: Poseidon hash implementation is designed to work seamlessly
|
||||
with Arkworks field traits and data structures.
|
||||
|
||||
1. `cargo bench`
|
||||
## Merkle Tree Implementations
|
||||
|
||||
To view the results of the benchmark, open the `target/criterion/report/index.html` file generated after the bench
|
||||
Merkle trees are fundamental data structures for verifying data integrity and set membership.
|
||||
Zerokit Utils offers two interchangeable implementations:
|
||||
|
||||
### Understanding Merkle Tree Terminology
|
||||
|
||||
To better understand the structure and parameters of our Merkle trees, here's a quick glossary:
|
||||
|
||||
- **Depth (`depth`)**: level of leaves if we count from root.
|
||||
If the root is at level 0, leaves are at level `depth`.
|
||||
- **Number of Levels**: `depth + 1`.
|
||||
- **Capacity (Number of Leaves)**: $2^{\text{depth}}$. This is the maximum number of leaves the tree can hold.
|
||||
- **Total Number of Nodes**: $2^{(\text{depth} + 1)} - 1$ for a full binary tree.
|
||||
|
||||
**Example for a tree with `depth: 3`**:
|
||||
|
||||
- Number of Levels: 4 (levels 0, 1, 2, 3)
|
||||
- Capacity (Number of Leaves): $2^3 = 8$
|
||||
- Total Number of Nodes: $2^{(3+1)} - 1 = 15$
|
||||
|
||||
Visual representation of a Merkle tree with `depth: 3`:
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
A[Root] --> N1
|
||||
A[Root] --> N2
|
||||
N1 --> N3
|
||||
N1 --> N4
|
||||
N2 --> N5
|
||||
N2 --> N6
|
||||
N3 -->|Leaf| L1
|
||||
N3 -->|Leaf| L2
|
||||
N4 -->|Leaf| L3
|
||||
N4 -->|Leaf| L4
|
||||
N5 -->|Leaf| L5
|
||||
N5 -->|Leaf| L6
|
||||
N6 -->|Leaf| L7
|
||||
N6 -->|Leaf| L8
|
||||
```
|
||||
|
||||
### Available Implementations
|
||||
|
||||
- **FullMerkleTree**
|
||||
- Stores all tree nodes in memory.
|
||||
- Use Case: Use when memory is abundant and operation speed is critical.
|
||||
|
||||
- **OptimalMerkleTree**
|
||||
- Stores only the nodes required to prove the accumulation of set leaves (i.e., authentication paths).
|
||||
- Use Case: Suited for environments where memory efficiency is a higher priority than raw speed.
|
||||
|
||||
#### Parallel Processing with Rayon
|
||||
|
||||
Both `OptimalMerkleTree` and `FullMerkleTree` internally utilize the Rayon crate
|
||||
to accelerate computations through data parallelism.
|
||||
This can lead to significant performance improvements, particularly during updates to large Merkle trees.
|
||||
|
||||
## Poseidon Hash Implementation
|
||||
|
||||
This crate provides an implementation for computing Poseidon hash round constants and MDS matrices.
|
||||
Key characteristics include:
|
||||
|
||||
- **Customizable parameters**: Supports various security levels and input sizes,
|
||||
allowing you to tailor the hash function to your specific needs.
|
||||
- **Arkworks-friendly**: Adapted to integrate smoothly with Arkworks field traits and custom data structures.
|
||||
|
||||
### ⚠️ Security Note
|
||||
|
||||
The MDS matrices used in the Poseidon hash function are generated iteratively
|
||||
using the Grain LFSR (Linear Feedback Shift Register) algorithm until specific cryptographic criteria are met.
|
||||
|
||||
- The reference Poseidon implementation includes validation algorithms to ensure these criteria are satisfied.
|
||||
These validation algorithms are not currently implemented in this crate.
|
||||
- For the hardcoded parameters provided within this crate,
|
||||
the initially generated random matrix has been verified to meet these conditions.
|
||||
- If you intend to use custom parameters, it is crucial to verify your generated MDS matrix.
|
||||
You should consult the Poseidon reference implementation to determine
|
||||
how many matrices are typically skipped before a valid one is found.
|
||||
This count should then be passed as the `skip_matrices parameter` to the `find_poseidon_ark_and_mds`
|
||||
function in this crate.
|
||||
|
||||
## Installation
|
||||
|
||||
Add zerokit-utils as a dependency to your Cargo.toml file:
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
zerokit-utils = "0.6.0"
|
||||
```
|
||||
|
||||
## Building and Testing
|
||||
|
||||
```bash
|
||||
# Build the crate
|
||||
cargo make build
|
||||
|
||||
# Run tests
|
||||
cargo make test
|
||||
|
||||
# Run benchmarks
|
||||
cargo make bench
|
||||
```
|
||||
|
||||
To view the results of the benchmark, open the `target/criterion/report/index.html` file generated after the bench
|
||||
|
||||
## Acknowledgements
|
||||
|
||||
- The Merkle tree implementations are adapted from:
|
||||
- [kilic/rln](https://github.com/kilic/rln/blob/master/src/merkle.rs)
|
||||
- [worldcoin/semaphore-rs](https://github.com/worldcoin/semaphore-rs/blob/d462a4372f1fd9c27610f2acfe4841fab1d396aa/src/merkle_tree.rs)
|
||||
|
||||
- The Poseidon implementation references:
|
||||
- [Poseidon reference implementation](https://extgit.iaik.tugraz.at/krypto/hadeshash/-/blob/master/code/generate_parameters_grain.sage)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use criterion::{criterion_group, criterion_main, Criterion};
|
||||
use hex_literal::hex;
|
||||
use lazy_static::lazy_static;
|
||||
use std::{fmt::Display, str::FromStr};
|
||||
|
||||
use criterion::{criterion_group, criterion_main, Criterion};
|
||||
use lazy_static::lazy_static;
|
||||
use tiny_keccak::{Hasher as _, Keccak};
|
||||
use zerokit_utils::{
|
||||
FullMerkleConfig, FullMerkleTree, Hasher, OptimalMerkleConfig, OptimalMerkleTree,
|
||||
@@ -47,54 +47,78 @@ impl FromStr for TestFr {
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref LEAVES: [TestFr; 4] = [
|
||||
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
|
||||
hex!("0000000000000000000000000000000000000000000000000000000000000002"),
|
||||
hex!("0000000000000000000000000000000000000000000000000000000000000003"),
|
||||
hex!("0000000000000000000000000000000000000000000000000000000000000004"),
|
||||
]
|
||||
.map(TestFr);
|
||||
static ref LEAVES: Vec<TestFr> = {
|
||||
let mut leaves = Vec::with_capacity(1 << 20);
|
||||
for i in 0..(1 << 20) {
|
||||
let mut bytes = [0u8; 32];
|
||||
bytes[28..].copy_from_slice(&(i as u32).to_be_bytes());
|
||||
leaves.push(TestFr(bytes));
|
||||
}
|
||||
leaves
|
||||
};
|
||||
static ref INDICES: Vec<usize> = (0..(1 << 20)).collect();
|
||||
}
|
||||
|
||||
const NOF_LEAVES: usize = 8192;
|
||||
|
||||
pub fn optimal_merkle_tree_benchmark(c: &mut Criterion) {
|
||||
let mut tree =
|
||||
OptimalMerkleTree::<Keccak256>::new(2, TestFr([0; 32]), OptimalMerkleConfig::default())
|
||||
OptimalMerkleTree::<Keccak256>::new(20, TestFr([0; 32]), OptimalMerkleConfig::default())
|
||||
.unwrap();
|
||||
|
||||
for i in 0..NOF_LEAVES {
|
||||
tree.set(i, LEAVES[i % LEAVES.len()]).unwrap();
|
||||
}
|
||||
|
||||
c.bench_function("OptimalMerkleTree::set", |b| {
|
||||
let mut index = NOF_LEAVES;
|
||||
b.iter(|| {
|
||||
tree.set(0, LEAVES[0]).unwrap();
|
||||
tree.set(index % (1 << 20), LEAVES[index % LEAVES.len()])
|
||||
.unwrap();
|
||||
index = (index + 1) % (1 << 20);
|
||||
})
|
||||
});
|
||||
|
||||
c.bench_function("OptimalMerkleTree::delete", |b| {
|
||||
let mut index = 0;
|
||||
b.iter(|| {
|
||||
tree.delete(0).unwrap();
|
||||
tree.delete(index % NOF_LEAVES).unwrap();
|
||||
tree.set(index % NOF_LEAVES, LEAVES[index % LEAVES.len()])
|
||||
.unwrap();
|
||||
index = (index + 1) % NOF_LEAVES;
|
||||
})
|
||||
});
|
||||
|
||||
c.bench_function("OptimalMerkleTree::override_range", |b| {
|
||||
let mut offset = 0;
|
||||
b.iter(|| {
|
||||
tree.override_range(0, *LEAVES, [0, 1, 2, 3]).unwrap();
|
||||
})
|
||||
});
|
||||
|
||||
c.bench_function("OptimalMerkleTree::compute_root", |b| {
|
||||
b.iter(|| {
|
||||
tree.compute_root().unwrap();
|
||||
let range = offset..offset + NOF_LEAVES;
|
||||
tree.override_range(
|
||||
offset,
|
||||
LEAVES[range.clone()].iter().cloned(),
|
||||
INDICES[range.clone()].iter().cloned(),
|
||||
)
|
||||
.unwrap();
|
||||
offset = (offset + NOF_LEAVES) % (1 << 20);
|
||||
})
|
||||
});
|
||||
|
||||
c.bench_function("OptimalMerkleTree::get", |b| {
|
||||
let mut index = 0;
|
||||
b.iter(|| {
|
||||
tree.get(0).unwrap();
|
||||
tree.get(index % NOF_LEAVES).unwrap();
|
||||
index = (index + 1) % NOF_LEAVES;
|
||||
})
|
||||
});
|
||||
|
||||
// check intermediate node getter which required additional computation of sub root index
|
||||
c.bench_function("OptimalMerkleTree::get_subtree_root", |b| {
|
||||
let mut level = 1;
|
||||
let mut index = 0;
|
||||
b.iter(|| {
|
||||
tree.get_subtree_root(1, 0).unwrap();
|
||||
tree.get_subtree_root(level % 20, index % (1 << (20 - (level % 20))))
|
||||
.unwrap();
|
||||
index = (index + 1) % (1 << (20 - (level % 20)));
|
||||
level = 1 + (level % 20);
|
||||
})
|
||||
});
|
||||
|
||||
@@ -107,42 +131,61 @@ pub fn optimal_merkle_tree_benchmark(c: &mut Criterion) {
|
||||
|
||||
pub fn full_merkle_tree_benchmark(c: &mut Criterion) {
|
||||
let mut tree =
|
||||
FullMerkleTree::<Keccak256>::new(2, TestFr([0; 32]), FullMerkleConfig::default()).unwrap();
|
||||
FullMerkleTree::<Keccak256>::new(20, TestFr([0; 32]), FullMerkleConfig::default()).unwrap();
|
||||
|
||||
for i in 0..NOF_LEAVES {
|
||||
tree.set(i, LEAVES[i % LEAVES.len()]).unwrap();
|
||||
}
|
||||
|
||||
c.bench_function("FullMerkleTree::set", |b| {
|
||||
let mut index = NOF_LEAVES;
|
||||
b.iter(|| {
|
||||
tree.set(0, LEAVES[0]).unwrap();
|
||||
tree.set(index % (1 << 20), LEAVES[index % LEAVES.len()])
|
||||
.unwrap();
|
||||
index = (index + 1) % (1 << 20);
|
||||
})
|
||||
});
|
||||
|
||||
c.bench_function("FullMerkleTree::delete", |b| {
|
||||
let mut index = 0;
|
||||
b.iter(|| {
|
||||
tree.delete(0).unwrap();
|
||||
tree.delete(index % NOF_LEAVES).unwrap();
|
||||
tree.set(index % NOF_LEAVES, LEAVES[index % LEAVES.len()])
|
||||
.unwrap();
|
||||
index = (index + 1) % NOF_LEAVES;
|
||||
})
|
||||
});
|
||||
|
||||
c.bench_function("FullMerkleTree::override_range", |b| {
|
||||
let mut offset = 0;
|
||||
b.iter(|| {
|
||||
tree.override_range(0, *LEAVES, [0, 1, 2, 3]).unwrap();
|
||||
})
|
||||
});
|
||||
|
||||
c.bench_function("FullMerkleTree::compute_root", |b| {
|
||||
b.iter(|| {
|
||||
tree.compute_root().unwrap();
|
||||
let range = offset..offset + NOF_LEAVES;
|
||||
tree.override_range(
|
||||
offset,
|
||||
LEAVES[range.clone()].iter().cloned(),
|
||||
INDICES[range.clone()].iter().cloned(),
|
||||
)
|
||||
.unwrap();
|
||||
offset = (offset + NOF_LEAVES) % (1 << 20);
|
||||
})
|
||||
});
|
||||
|
||||
c.bench_function("FullMerkleTree::get", |b| {
|
||||
let mut index = 0;
|
||||
b.iter(|| {
|
||||
tree.get(0).unwrap();
|
||||
tree.get(index % NOF_LEAVES).unwrap();
|
||||
index = (index + 1) % NOF_LEAVES;
|
||||
})
|
||||
});
|
||||
|
||||
// check intermediate node getter which required additional computation of sub root index
|
||||
c.bench_function("FullMerkleTree::get_subtree_root", |b| {
|
||||
let mut level = 1;
|
||||
let mut index = 0;
|
||||
b.iter(|| {
|
||||
tree.get_subtree_root(1, 0).unwrap();
|
||||
tree.get_subtree_root(level % 20, index % (1 << (20 - (level % 20))))
|
||||
.unwrap();
|
||||
index = (index + 1) % (1 << (20 - (level % 20)));
|
||||
level = 1 + (level % 20);
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
65
utils/benches/poseidon_benchmark.rs
Normal file
65
utils/benches/poseidon_benchmark.rs
Normal file
@@ -0,0 +1,65 @@
|
||||
use std::hint::black_box;
|
||||
|
||||
use ark_bn254::Fr;
|
||||
use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion, Throughput};
|
||||
use zerokit_utils::Poseidon;
|
||||
|
||||
const ROUND_PARAMS: [(usize, usize, usize, usize); 8] = [
|
||||
(2, 8, 56, 0),
|
||||
(3, 8, 57, 0),
|
||||
(4, 8, 56, 0),
|
||||
(5, 8, 60, 0),
|
||||
(6, 8, 60, 0),
|
||||
(7, 8, 63, 0),
|
||||
(8, 8, 64, 0),
|
||||
(9, 8, 63, 0),
|
||||
];
|
||||
|
||||
pub fn poseidon_benchmark(c: &mut Criterion) {
|
||||
let hasher = Poseidon::<Fr>::from(&ROUND_PARAMS);
|
||||
let mut group = c.benchmark_group("poseidon Fr");
|
||||
|
||||
for size in [10u32, 100, 1000].iter() {
|
||||
group.throughput(Throughput::Elements(*size as u64));
|
||||
|
||||
group.bench_with_input(BenchmarkId::new("Array hash", size), size, |b, &size| {
|
||||
b.iter_batched(
|
||||
// Setup: create values for each benchmark iteration
|
||||
|| {
|
||||
let mut values = Vec::with_capacity(size as usize);
|
||||
for i in 0..size {
|
||||
values.push([Fr::from(i)]);
|
||||
}
|
||||
values
|
||||
},
|
||||
// Actual benchmark
|
||||
|values| {
|
||||
for v in values.iter() {
|
||||
let _ = hasher.hash(black_box(&v[..]));
|
||||
}
|
||||
},
|
||||
BatchSize::SmallInput,
|
||||
)
|
||||
});
|
||||
}
|
||||
|
||||
// Benchmark single hash operation separately
|
||||
group.bench_function("Single hash", |b| {
|
||||
let input = [Fr::from(u64::MAX)];
|
||||
b.iter(|| {
|
||||
let _ = hasher.hash(black_box(&input[..]));
|
||||
})
|
||||
});
|
||||
|
||||
group.finish();
|
||||
}
|
||||
|
||||
criterion_group! {
|
||||
name = benches;
|
||||
config = Criterion::default()
|
||||
.warm_up_time(std::time::Duration::from_millis(500))
|
||||
.measurement_time(std::time::Duration::from_secs(4))
|
||||
.sample_size(20);
|
||||
targets = poseidon_benchmark
|
||||
}
|
||||
criterion_main!(benches);
|
||||
33
utils/src/merkle_tree/error.rs
Normal file
33
utils/src/merkle_tree/error.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum ZerokitMerkleTreeError {
|
||||
#[error("Invalid index")]
|
||||
InvalidIndex,
|
||||
// InvalidProof,
|
||||
#[error("Leaf index out of bounds")]
|
||||
InvalidLeaf,
|
||||
#[error("Level exceeds tree depth")]
|
||||
InvalidLevel,
|
||||
#[error("Subtree index out of bounds")]
|
||||
InvalidSubTreeIndex,
|
||||
#[error("Start level is != from end level")]
|
||||
InvalidStartAndEndLevel,
|
||||
#[error("set_range got too many leaves")]
|
||||
TooManySet,
|
||||
#[error("Unknown error while computing merkle proof")]
|
||||
ComputingProofError,
|
||||
#[error("Invalid witness length (!= tree depth)")]
|
||||
InvalidWitness,
|
||||
#[cfg(feature = "pmtree-ft")]
|
||||
#[error("Pmtree error: {0}")]
|
||||
PmtreeErrorKind(#[from] pmtree::PmtreeErrorKind),
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum FromConfigError {
|
||||
#[error("Error while reading pmtree config: {0}")]
|
||||
JsonError(#[from] serde_json::Error),
|
||||
#[error("Error while creating pmtree config: missing path")]
|
||||
MissingPath,
|
||||
#[error("Error while creating pmtree config: path already exists")]
|
||||
PathExists,
|
||||
}
|
||||
@@ -1,28 +1,29 @@
|
||||
use crate::merkle_tree::{FrOf, Hasher, ZerokitMerkleProof, ZerokitMerkleTree};
|
||||
use color_eyre::{Report, Result};
|
||||
use std::{
|
||||
cmp::max,
|
||||
fmt::Debug,
|
||||
iter::{once, repeat, successors},
|
||||
iter::{once, repeat_n},
|
||||
str::FromStr,
|
||||
};
|
||||
|
||||
use rayon::iter::{IntoParallelIterator, ParallelIterator};
|
||||
|
||||
use crate::merkle_tree::{
|
||||
error::{FromConfigError, ZerokitMerkleTreeError},
|
||||
FrOf, Hasher, ZerokitMerkleProof, ZerokitMerkleTree, MIN_PARALLEL_NODES,
|
||||
};
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Full Merkle Tree Implementation
|
||||
///// Full Merkle Tree Implementation
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
/// Merkle tree with all leaf and intermediate hashes stored
|
||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
||||
pub struct FullMerkleTree<H: Hasher> {
|
||||
pub struct FullMerkleTree<H>
|
||||
where
|
||||
H: Hasher,
|
||||
{
|
||||
/// The depth of the tree, i.e. the number of levels from leaf to root
|
||||
depth: usize,
|
||||
|
||||
/// The nodes cached from the empty part of the tree (where leaves are set to default).
|
||||
/// Since the rightmost part of the tree is usually changed much later than its creation,
|
||||
/// we can prove accumulation of elements in the leftmost part, with no need to initialize the full tree
|
||||
/// and by caching few intermediate nodes to the root computed from default leaves
|
||||
cached_nodes: Vec<H::Fr>,
|
||||
|
||||
/// The tree nodes
|
||||
nodes: Vec<H::Fr>,
|
||||
|
||||
@@ -30,11 +31,11 @@ pub struct FullMerkleTree<H: Hasher> {
|
||||
/// Set to 0 if the leaf is empty and set to 1 in otherwise.
|
||||
cached_leaves_indices: Vec<u8>,
|
||||
|
||||
// The next available (i.e., never used) tree index. Equivalently, the number of leaves added to the tree
|
||||
// (deletions leave next_index unchanged)
|
||||
/// 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 that an application may use to store additional information
|
||||
metadata: Vec<u8>,
|
||||
}
|
||||
|
||||
@@ -56,9 +57,9 @@ pub struct FullMerkleProof<H: Hasher>(pub Vec<FullMerkleBranch<H>>);
|
||||
pub struct FullMerkleConfig(());
|
||||
|
||||
impl FromStr for FullMerkleConfig {
|
||||
type Err = Report;
|
||||
type Err = FromConfigError;
|
||||
|
||||
fn from_str(_s: &str) -> Result<Self> {
|
||||
fn from_str(_s: &str) -> Result<Self, Self::Err> {
|
||||
Ok(FullMerkleConfig::default())
|
||||
}
|
||||
}
|
||||
@@ -72,86 +73,89 @@ where
|
||||
type Hasher = H;
|
||||
type Config = FullMerkleConfig;
|
||||
|
||||
fn default(depth: usize) -> Result<Self> {
|
||||
fn default(depth: usize) -> Result<Self, ZerokitMerkleTreeError> {
|
||||
FullMerkleTree::<H>::new(depth, Self::Hasher::default_leaf(), Self::Config::default())
|
||||
}
|
||||
|
||||
/// Creates a new `MerkleTree`
|
||||
/// depth - the height of the tree made only of hash nodes. 2^depth is the maximum number of leaves hash nodes
|
||||
fn new(depth: usize, initial_leaf: FrOf<Self::Hasher>, _config: Self::Config) -> Result<Self> {
|
||||
/// depth - the depth of the tree made only of hash nodes. 2^depth is the maximum number of leaves hash nodes
|
||||
fn new(
|
||||
depth: usize,
|
||||
default_leaf: FrOf<Self::Hasher>,
|
||||
_config: Self::Config,
|
||||
) -> Result<Self, ZerokitMerkleTreeError> {
|
||||
// Compute cache node values, leaf to root
|
||||
let cached_nodes = successors(Some(initial_leaf), |prev| Some(H::hash(&[*prev, *prev])))
|
||||
.take(depth + 1)
|
||||
.collect::<Vec<_>>();
|
||||
let mut cached_nodes: Vec<H::Fr> = Vec::with_capacity(depth + 1);
|
||||
cached_nodes.push(default_leaf);
|
||||
for i in 0..depth {
|
||||
cached_nodes.push(H::hash(&[cached_nodes[i]; 2]));
|
||||
}
|
||||
cached_nodes.reverse();
|
||||
|
||||
// Compute node values
|
||||
let nodes = cached_nodes
|
||||
.iter()
|
||||
.rev()
|
||||
.enumerate()
|
||||
.flat_map(|(levels, hash)| repeat(hash).take(1 << levels))
|
||||
.flat_map(|(levels, hash)| repeat_n(hash, 1 << levels))
|
||||
.cloned()
|
||||
.collect::<Vec<_>>();
|
||||
debug_assert!(nodes.len() == (1 << (depth + 1)) - 1);
|
||||
|
||||
let next_index = 0;
|
||||
|
||||
Ok(Self {
|
||||
depth,
|
||||
cached_nodes,
|
||||
nodes,
|
||||
cached_leaves_indices: vec![0; 1 << depth],
|
||||
next_index,
|
||||
next_index: 0,
|
||||
metadata: Vec::new(),
|
||||
})
|
||||
}
|
||||
|
||||
fn close_db_connection(&mut self) -> Result<()> {
|
||||
fn close_db_connection(&mut self) -> Result<(), ZerokitMerkleTreeError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Returns the depth of the tree
|
||||
/// Returns the depth of the tree
|
||||
fn depth(&self) -> usize {
|
||||
self.depth
|
||||
}
|
||||
|
||||
// Returns the capacity of the tree, i.e. the maximum number of accumulatable leaves
|
||||
/// Returns the capacity of the tree, i.e. the maximum number of accumulatable leaves
|
||||
fn capacity(&self) -> usize {
|
||||
1 << self.depth
|
||||
}
|
||||
|
||||
// Returns the total number of leaves set
|
||||
/// Returns the total number of leaves set
|
||||
fn leaves_set(&self) -> usize {
|
||||
self.next_index
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
// Returns the root of the tree
|
||||
/// Returns the root of the tree
|
||||
fn root(&self) -> FrOf<Self::Hasher> {
|
||||
self.nodes[0]
|
||||
}
|
||||
|
||||
// Sets a leaf at the specified tree index
|
||||
fn set(&mut self, leaf: usize, hash: FrOf<Self::Hasher>) -> Result<()> {
|
||||
/// Sets a leaf at the specified tree index
|
||||
fn set(&mut self, leaf: usize, hash: FrOf<Self::Hasher>) -> Result<(), ZerokitMerkleTreeError> {
|
||||
self.set_range(leaf, once(hash))?;
|
||||
self.next_index = max(self.next_index, leaf + 1);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Get a leaf from the specified tree index
|
||||
fn get(&self, leaf: usize) -> Result<FrOf<Self::Hasher>> {
|
||||
/// Get a leaf from the specified tree index
|
||||
fn get(&self, leaf: usize) -> Result<FrOf<Self::Hasher>, ZerokitMerkleTreeError> {
|
||||
if leaf >= self.capacity() {
|
||||
return Err(Report::msg("leaf index out of bounds"));
|
||||
return Err(ZerokitMerkleTreeError::InvalidLeaf);
|
||||
}
|
||||
Ok(self.nodes[self.capacity() + leaf - 1])
|
||||
}
|
||||
|
||||
fn get_subtree_root(&self, n: usize, index: usize) -> Result<H::Fr> {
|
||||
/// Returns the root of the subtree at level n and index
|
||||
fn get_subtree_root(&self, n: usize, index: usize) -> Result<H::Fr, ZerokitMerkleTreeError> {
|
||||
if n > self.depth() {
|
||||
return Err(Report::msg("level exceeds depth size"));
|
||||
return Err(ZerokitMerkleTreeError::InvalidIndex);
|
||||
}
|
||||
if index >= self.capacity() {
|
||||
return Err(Report::msg("index exceeds set size"));
|
||||
return Err(ZerokitMerkleTreeError::InvalidLeaf);
|
||||
}
|
||||
if n == 0 {
|
||||
Ok(self.root())
|
||||
@@ -161,7 +165,7 @@ where
|
||||
let mut idx = self.capacity() + index - 1;
|
||||
let mut nd = self.depth;
|
||||
loop {
|
||||
let parent = self.parent(idx).unwrap();
|
||||
let parent = self.parent(idx).expect("parent should exist");
|
||||
nd -= 1;
|
||||
if nd == n {
|
||||
return Ok(self.nodes[parent]);
|
||||
@@ -172,6 +176,8 @@ where
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the indices of the leaves that are empty
|
||||
fn get_empty_leaves_indices(&self) -> Vec<usize> {
|
||||
self.cached_leaves_indices
|
||||
.iter()
|
||||
@@ -182,40 +188,45 @@ where
|
||||
.collect()
|
||||
}
|
||||
|
||||
// Sets tree nodes, starting from start index
|
||||
// Function proper of FullMerkleTree implementation
|
||||
fn set_range<I: IntoIterator<Item = FrOf<Self::Hasher>>>(
|
||||
/// Sets multiple leaves from the specified tree index
|
||||
fn set_range<I: ExactSizeIterator<Item = FrOf<Self::Hasher>>>(
|
||||
&mut self,
|
||||
start: usize,
|
||||
hashes: I,
|
||||
) -> Result<()> {
|
||||
leaves: I,
|
||||
) -> Result<(), ZerokitMerkleTreeError> {
|
||||
let index = self.capacity() + start - 1;
|
||||
let mut count = 0;
|
||||
// first count number of hashes, and check that they fit in the tree
|
||||
// first count number of leaves, and check that they fit in the tree
|
||||
// then insert into the tree
|
||||
let hashes = hashes.into_iter().collect::<Vec<_>>();
|
||||
if hashes.len() + start > self.capacity() {
|
||||
return Err(Report::msg("provided hashes do not fit in the tree"));
|
||||
let leaves = leaves.into_iter().collect::<Vec<_>>();
|
||||
if leaves.len() + start > self.capacity() {
|
||||
return Err(ZerokitMerkleTreeError::TooManySet);
|
||||
}
|
||||
hashes.into_iter().for_each(|hash| {
|
||||
leaves.into_iter().for_each(|hash| {
|
||||
self.nodes[index + count] = hash;
|
||||
self.cached_leaves_indices[start + count] = 1;
|
||||
count += 1;
|
||||
});
|
||||
if count != 0 {
|
||||
self.update_nodes(index, index + (count - 1))?;
|
||||
self.update_hashes(index, index + (count - 1))?;
|
||||
self.next_index = max(self.next_index, start + count);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn override_range<I, J>(&mut self, start: usize, leaves: I, indices: J) -> Result<()>
|
||||
/// Overrides a range of leaves while resetting specified indices to default and preserving unaffected values.
|
||||
fn override_range<I, J>(
|
||||
&mut self,
|
||||
start: usize,
|
||||
leaves: I,
|
||||
indices: J,
|
||||
) -> Result<(), ZerokitMerkleTreeError>
|
||||
where
|
||||
I: IntoIterator<Item = FrOf<Self::Hasher>>,
|
||||
J: IntoIterator<Item = usize>,
|
||||
I: ExactSizeIterator<Item = FrOf<Self::Hasher>>,
|
||||
J: ExactSizeIterator<Item = usize>,
|
||||
{
|
||||
let indices = indices.into_iter().collect::<Vec<_>>();
|
||||
let min_index = *indices.first().unwrap();
|
||||
let min_index = *indices.first().expect("indices should not be empty");
|
||||
let leaves_vec = leaves.into_iter().collect::<Vec<_>>();
|
||||
|
||||
let max_index = start + leaves_vec.len();
|
||||
@@ -237,18 +248,17 @@ where
|
||||
self.cached_leaves_indices[i] = 0;
|
||||
}
|
||||
|
||||
self.set_range(start, set_values)
|
||||
.map_err(|e| Report::msg(e.to_string()))
|
||||
self.set_range(start, set_values.into_iter())
|
||||
}
|
||||
|
||||
// Sets a leaf at the next available index
|
||||
fn update_next(&mut self, leaf: FrOf<Self::Hasher>) -> Result<()> {
|
||||
/// Sets a leaf at the next available index
|
||||
fn update_next(&mut self, leaf: FrOf<Self::Hasher>) -> Result<(), ZerokitMerkleTreeError> {
|
||||
self.set(self.next_index, leaf)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Deletes a leaf at a certain index by setting it to its default value (next_index is not updated)
|
||||
fn delete(&mut self, index: usize) -> Result<()> {
|
||||
/// Deletes a leaf at a certain index by setting it to its default value (next_index is not updated)
|
||||
fn delete(&mut self, index: usize) -> Result<(), ZerokitMerkleTreeError> {
|
||||
// We reset the leaf only if we previously set a leaf at that index
|
||||
if index < self.next_index {
|
||||
self.set(index, H::default_leaf())?;
|
||||
@@ -257,10 +267,10 @@ where
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Computes a merkle proof the the leaf at the specified index
|
||||
fn proof(&self, leaf: usize) -> Result<FullMerkleProof<H>> {
|
||||
// Computes a merkle proof the leaf at the specified index
|
||||
fn proof(&self, leaf: usize) -> Result<FullMerkleProof<H>, ZerokitMerkleTreeError> {
|
||||
if leaf >= self.capacity() {
|
||||
return Err(Report::msg("index exceeds set size"));
|
||||
return Err(ZerokitMerkleTreeError::InvalidLeaf);
|
||||
}
|
||||
let mut index = self.capacity() + leaf - 1;
|
||||
let mut path = Vec::with_capacity(self.depth + 1);
|
||||
@@ -277,30 +287,29 @@ where
|
||||
}
|
||||
|
||||
// Verifies a Merkle proof with respect to the input leaf and the tree root
|
||||
fn verify(&self, hash: &FrOf<Self::Hasher>, proof: &FullMerkleProof<H>) -> Result<bool> {
|
||||
fn verify(
|
||||
&self,
|
||||
hash: &FrOf<Self::Hasher>,
|
||||
proof: &FullMerkleProof<H>,
|
||||
) -> Result<bool, ZerokitMerkleTreeError> {
|
||||
Ok(proof.compute_root_from(hash) == self.root())
|
||||
}
|
||||
|
||||
fn compute_root(&mut self) -> Result<FrOf<Self::Hasher>> {
|
||||
Ok(self.root())
|
||||
}
|
||||
|
||||
fn set_metadata(&mut self, metadata: &[u8]) -> Result<()> {
|
||||
fn set_metadata(&mut self, metadata: &[u8]) -> Result<(), ZerokitMerkleTreeError> {
|
||||
self.metadata = metadata.to_vec();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn metadata(&self) -> Result<Vec<u8>> {
|
||||
fn metadata(&self) -> Result<Vec<u8>, ZerokitMerkleTreeError> {
|
||||
Ok(self.metadata.to_vec())
|
||||
}
|
||||
}
|
||||
|
||||
// Utilities for updating the tree nodes
|
||||
impl<H: Hasher> FullMerkleTree<H>
|
||||
where
|
||||
H: Hasher,
|
||||
{
|
||||
// Utilities for updating the tree nodes
|
||||
|
||||
/// For a given node index, return the parent node index
|
||||
/// Returns None if there is no parent (root node)
|
||||
fn parent(&self, index: usize) -> Option<usize> {
|
||||
@@ -316,23 +325,60 @@ where
|
||||
(index << 1) + 1
|
||||
}
|
||||
|
||||
/// Returns the depth level of a node based on its index in the flattened tree.
|
||||
fn levels(&self, index: usize) -> usize {
|
||||
// `n.next_power_of_two()` will return `n` iff `n` is a power of two.
|
||||
// The extra offset corrects this.
|
||||
(index + 2).next_power_of_two().trailing_zeros() as usize - 1
|
||||
}
|
||||
|
||||
fn update_nodes(&mut self, start: usize, end: usize) -> Result<()> {
|
||||
if self.levels(start) != self.levels(end) {
|
||||
return Err(Report::msg("self.levels(start) != self.levels(end)"));
|
||||
/// Updates parent hashes after modifying a range of nodes at the same level.
|
||||
///
|
||||
/// - `start_index`: The first index at the current level that was updated.
|
||||
/// - `end_index`: The last index (inclusive) at the same level that was updated.
|
||||
fn update_hashes(
|
||||
&mut self,
|
||||
start_index: usize,
|
||||
end_index: usize,
|
||||
) -> Result<(), ZerokitMerkleTreeError> {
|
||||
// Ensure the range is within the same tree level
|
||||
if self.levels(start_index) != self.levels(end_index) {
|
||||
return Err(ZerokitMerkleTreeError::InvalidStartAndEndLevel);
|
||||
}
|
||||
if let (Some(start), Some(end)) = (self.parent(start), self.parent(end)) {
|
||||
for parent in start..=end {
|
||||
let child = self.first_child(parent);
|
||||
self.nodes[parent] = H::hash(&[self.nodes[child], self.nodes[child + 1]]);
|
||||
|
||||
// Compute parent indices for the range
|
||||
if let (Some(start_parent), Some(end_parent)) =
|
||||
(self.parent(start_index), self.parent(end_index))
|
||||
{
|
||||
// Use parallel processing when the number of pairs exceeds the threshold
|
||||
if end_parent - start_parent + 1 >= MIN_PARALLEL_NODES {
|
||||
let updates: Vec<(usize, H::Fr)> = (start_parent..=end_parent)
|
||||
.into_par_iter()
|
||||
.map(|parent| {
|
||||
let left_child = self.first_child(parent);
|
||||
let right_child = left_child + 1;
|
||||
let hash = H::hash(&[self.nodes[left_child], self.nodes[right_child]]);
|
||||
(parent, hash)
|
||||
})
|
||||
.collect();
|
||||
|
||||
for (parent, hash) in updates {
|
||||
self.nodes[parent] = hash;
|
||||
}
|
||||
} else {
|
||||
// Otherwise, fallback to sequential update for small ranges
|
||||
for parent in start_parent..=end_parent {
|
||||
let left_child = self.first_child(parent);
|
||||
let right_child = left_child + 1;
|
||||
self.nodes[parent] =
|
||||
H::hash(&[self.nodes[left_child], self.nodes[right_child]]);
|
||||
}
|
||||
}
|
||||
self.update_nodes(start, end)?;
|
||||
|
||||
// Recurse to update upper levels
|
||||
self.update_hashes(start_parent, end_parent)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -341,14 +387,12 @@ impl<H: Hasher> ZerokitMerkleProof for FullMerkleProof<H> {
|
||||
type Index = u8;
|
||||
type Hasher = H;
|
||||
|
||||
#[must_use]
|
||||
// Returns the length of a Merkle proof
|
||||
fn length(&self) -> usize {
|
||||
self.0.len()
|
||||
}
|
||||
|
||||
/// Computes the leaf index corresponding to a Merkle proof
|
||||
#[must_use]
|
||||
fn leaf_index(&self) -> usize {
|
||||
self.0.iter().rev().fold(0, |index, branch| match branch {
|
||||
FullMerkleBranch::Left(_) => index << 1,
|
||||
@@ -356,7 +400,6 @@ impl<H: Hasher> ZerokitMerkleProof for FullMerkleProof<H> {
|
||||
})
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
/// Returns the path elements forming a Merkle proof
|
||||
fn get_path_elements(&self) -> Vec<FrOf<Self::Hasher>> {
|
||||
self.0
|
||||
@@ -368,7 +411,6 @@ impl<H: Hasher> ZerokitMerkleProof for FullMerkleProof<H> {
|
||||
}
|
||||
|
||||
/// Returns the path indexes forming a Merkle proof
|
||||
#[must_use]
|
||||
fn get_path_index(&self) -> Vec<Self::Index> {
|
||||
self.0
|
||||
.iter()
|
||||
@@ -380,7 +422,6 @@ impl<H: Hasher> ZerokitMerkleProof for FullMerkleProof<H> {
|
||||
}
|
||||
|
||||
/// Computes the Merkle root corresponding by iteratively hashing a Merkle proof with a given input leaf
|
||||
#[must_use]
|
||||
fn compute_root_from(&self, hash: &FrOf<Self::Hasher>) -> FrOf<Self::Hasher> {
|
||||
self.0.iter().fold(*hash, |hash, branch| match branch {
|
||||
FullMerkleBranch::Left(sibling) => H::hash(&[hash, *sibling]),
|
||||
|
||||
@@ -8,20 +8,25 @@
|
||||
// and https://github.com/worldcoin/semaphore-rs/blob/d462a4372f1fd9c27610f2acfe4841fab1d396aa/src/merkle_tree.rs
|
||||
|
||||
//!
|
||||
//! # To do
|
||||
//! # TODO
|
||||
//!
|
||||
//! * Disk based storage backend (using mmaped files should be easy)
|
||||
//! * Implement serialization for tree and Merkle proof
|
||||
|
||||
use std::str::FromStr;
|
||||
use crate::merkle_tree::error::ZerokitMerkleTreeError;
|
||||
use std::{
|
||||
fmt::{Debug, Display},
|
||||
str::FromStr,
|
||||
};
|
||||
|
||||
use color_eyre::Result;
|
||||
/// Enables parallel hashing when there are at least 8 nodes (4 pairs to hash), justifying the overhead.
|
||||
pub const MIN_PARALLEL_NODES: usize = 8;
|
||||
|
||||
/// In the Hasher trait we define the node type, the default leaf
|
||||
/// and the hash function used to initialize a Merkle Tree implementation
|
||||
pub trait Hasher {
|
||||
/// Type of the leaf and tree node
|
||||
type Fr: Clone + Copy + Eq + Default + std::fmt::Debug + std::fmt::Display + FromStr;
|
||||
type Fr: Clone + Copy + Eq + Default + Debug + Display + FromStr + Send + Sync;
|
||||
|
||||
/// Returns the default tree leaf
|
||||
fn default_leaf() -> Self::Fr;
|
||||
@@ -39,35 +44,52 @@ pub trait ZerokitMerkleTree {
|
||||
type Hasher: Hasher;
|
||||
type Config: Default + FromStr;
|
||||
|
||||
fn default(depth: usize) -> Result<Self>
|
||||
fn default(depth: usize) -> Result<Self, ZerokitMerkleTreeError>
|
||||
where
|
||||
Self: Sized;
|
||||
fn new(depth: usize, default_leaf: FrOf<Self::Hasher>, config: Self::Config) -> Result<Self>
|
||||
fn new(
|
||||
depth: usize,
|
||||
default_leaf: FrOf<Self::Hasher>,
|
||||
config: Self::Config,
|
||||
) -> Result<Self, ZerokitMerkleTreeError>
|
||||
where
|
||||
Self: Sized;
|
||||
fn depth(&self) -> usize;
|
||||
fn capacity(&self) -> usize;
|
||||
fn leaves_set(&self) -> usize;
|
||||
fn root(&self) -> FrOf<Self::Hasher>;
|
||||
fn compute_root(&mut self) -> Result<FrOf<Self::Hasher>>;
|
||||
fn get_subtree_root(&self, n: usize, index: usize) -> Result<FrOf<Self::Hasher>>;
|
||||
fn set(&mut self, index: usize, leaf: FrOf<Self::Hasher>) -> Result<()>;
|
||||
fn set_range<I>(&mut self, start: usize, leaves: I) -> Result<()>
|
||||
fn get_subtree_root(
|
||||
&self,
|
||||
n: usize,
|
||||
index: usize,
|
||||
) -> Result<FrOf<Self::Hasher>, ZerokitMerkleTreeError>;
|
||||
fn set(&mut self, index: usize, leaf: FrOf<Self::Hasher>)
|
||||
-> Result<(), ZerokitMerkleTreeError>;
|
||||
fn set_range<I>(&mut self, start: usize, leaves: I) -> Result<(), ZerokitMerkleTreeError>
|
||||
where
|
||||
I: IntoIterator<Item = FrOf<Self::Hasher>>;
|
||||
fn get(&self, index: usize) -> Result<FrOf<Self::Hasher>>;
|
||||
I: ExactSizeIterator<Item = FrOf<Self::Hasher>>;
|
||||
fn get(&self, index: usize) -> Result<FrOf<Self::Hasher>, ZerokitMerkleTreeError>;
|
||||
fn get_empty_leaves_indices(&self) -> Vec<usize>;
|
||||
fn override_range<I, J>(&mut self, start: usize, leaves: I, to_remove_indices: J) -> Result<()>
|
||||
fn override_range<I, J>(
|
||||
&mut self,
|
||||
start: usize,
|
||||
leaves: I,
|
||||
to_remove_indices: J,
|
||||
) -> Result<(), ZerokitMerkleTreeError>
|
||||
where
|
||||
I: IntoIterator<Item = FrOf<Self::Hasher>>,
|
||||
J: IntoIterator<Item = usize>;
|
||||
fn update_next(&mut self, leaf: FrOf<Self::Hasher>) -> Result<()>;
|
||||
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<()>;
|
||||
I: ExactSizeIterator<Item = FrOf<Self::Hasher>>,
|
||||
J: ExactSizeIterator<Item = usize>;
|
||||
fn update_next(&mut self, leaf: FrOf<Self::Hasher>) -> Result<(), ZerokitMerkleTreeError>;
|
||||
fn delete(&mut self, index: usize) -> Result<(), ZerokitMerkleTreeError>;
|
||||
fn proof(&self, index: usize) -> Result<Self::Proof, ZerokitMerkleTreeError>;
|
||||
fn verify(
|
||||
&self,
|
||||
leaf: &FrOf<Self::Hasher>,
|
||||
witness: &Self::Proof,
|
||||
) -> Result<bool, ZerokitMerkleTreeError>;
|
||||
fn set_metadata(&mut self, metadata: &[u8]) -> Result<(), ZerokitMerkleTreeError>;
|
||||
fn metadata(&self) -> Result<Vec<u8>, ZerokitMerkleTreeError>;
|
||||
fn close_db_connection(&mut self) -> Result<(), ZerokitMerkleTreeError>;
|
||||
}
|
||||
|
||||
pub trait ZerokitMerkleProof {
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
pub mod error;
|
||||
pub mod full_merkle_tree;
|
||||
#[allow(clippy::module_inception)]
|
||||
pub mod merkle_tree;
|
||||
pub mod optimal_merkle_tree;
|
||||
pub use self::full_merkle_tree::*;
|
||||
pub use self::merkle_tree::*;
|
||||
pub use self::optimal_merkle_tree::*;
|
||||
|
||||
pub use self::full_merkle_tree::{FullMerkleConfig, FullMerkleProof, FullMerkleTree};
|
||||
pub use self::merkle_tree::{
|
||||
FrOf, Hasher, ZerokitMerkleProof, ZerokitMerkleTree, MIN_PARALLEL_NODES,
|
||||
};
|
||||
pub use self::optimal_merkle_tree::{OptimalMerkleConfig, OptimalMerkleProof, OptimalMerkleTree};
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
use crate::merkle_tree::{Hasher, ZerokitMerkleProof, ZerokitMerkleTree};
|
||||
use crate::FrOf;
|
||||
use color_eyre::{Report, Result};
|
||||
use std::collections::HashMap;
|
||||
use std::str::FromStr;
|
||||
use std::{cmp::max, fmt::Debug};
|
||||
use std::{cmp::max, collections::HashMap, fmt::Debug, str::FromStr};
|
||||
|
||||
use rayon::iter::{IntoParallelIterator, ParallelIterator};
|
||||
|
||||
use crate::merkle_tree::{
|
||||
error::{FromConfigError, ZerokitMerkleTreeError},
|
||||
FrOf, Hasher, ZerokitMerkleProof, ZerokitMerkleTree, MIN_PARALLEL_NODES,
|
||||
};
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Optimal Merkle Tree Implementation
|
||||
///// Optimal Merkle Tree Implementation
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
/// The Merkle tree structure
|
||||
@@ -31,11 +32,11 @@ where
|
||||
/// Set to 0 if the leaf is empty and set to 1 in otherwise.
|
||||
cached_leaves_indices: Vec<u8>,
|
||||
|
||||
// The next available (i.e., never used) tree index. Equivalently, the number of leaves added to the tree
|
||||
// (deletions leave next_index unchanged)
|
||||
/// 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 that an application may use to store additional information
|
||||
metadata: Vec<u8>,
|
||||
}
|
||||
|
||||
@@ -48,15 +49,14 @@ pub struct OptimalMerkleProof<H: Hasher>(pub Vec<(H::Fr, u8)>);
|
||||
pub struct OptimalMerkleConfig(());
|
||||
|
||||
impl FromStr for OptimalMerkleConfig {
|
||||
type Err = Report;
|
||||
type Err = FromConfigError;
|
||||
|
||||
fn from_str(_s: &str) -> Result<Self> {
|
||||
fn from_str(_s: &str) -> Result<Self, Self::Err> {
|
||||
Ok(OptimalMerkleConfig::default())
|
||||
}
|
||||
}
|
||||
|
||||
/// Implementations
|
||||
|
||||
impl<H: Hasher> ZerokitMerkleTree for OptimalMerkleTree<H>
|
||||
where
|
||||
H: Hasher,
|
||||
@@ -65,60 +65,86 @@ where
|
||||
type Hasher = H;
|
||||
type Config = OptimalMerkleConfig;
|
||||
|
||||
fn default(depth: usize) -> Result<Self> {
|
||||
fn default(depth: usize) -> Result<Self, ZerokitMerkleTreeError> {
|
||||
OptimalMerkleTree::<H>::new(depth, H::default_leaf(), Self::Config::default())
|
||||
}
|
||||
|
||||
/// Creates a new `MerkleTree`
|
||||
/// depth - the height of the tree made only of hash nodes. 2^depth is the maximum number of leaves hash nodes
|
||||
fn new(depth: usize, default_leaf: H::Fr, _config: Self::Config) -> Result<Self> {
|
||||
/// depth - the depth of the tree made only of hash nodes. 2^depth is the maximum number of leaves hash nodes
|
||||
fn new(
|
||||
depth: usize,
|
||||
default_leaf: H::Fr,
|
||||
_config: Self::Config,
|
||||
) -> Result<Self, ZerokitMerkleTreeError> {
|
||||
// Compute cache node values, leaf to root
|
||||
let mut cached_nodes: Vec<H::Fr> = Vec::with_capacity(depth + 1);
|
||||
cached_nodes.push(default_leaf);
|
||||
for i in 0..depth {
|
||||
cached_nodes.push(H::hash(&[cached_nodes[i]; 2]));
|
||||
}
|
||||
cached_nodes.reverse();
|
||||
|
||||
Ok(OptimalMerkleTree {
|
||||
cached_nodes: cached_nodes.clone(),
|
||||
depth,
|
||||
nodes: HashMap::new(),
|
||||
cached_nodes,
|
||||
nodes: HashMap::with_capacity(1 << depth),
|
||||
cached_leaves_indices: vec![0; 1 << depth],
|
||||
next_index: 0,
|
||||
metadata: Vec::new(),
|
||||
})
|
||||
}
|
||||
|
||||
fn close_db_connection(&mut self) -> Result<()> {
|
||||
fn close_db_connection(&mut self) -> Result<(), ZerokitMerkleTreeError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Returns the depth of the tree
|
||||
/// Returns the depth of the tree
|
||||
fn depth(&self) -> usize {
|
||||
self.depth
|
||||
}
|
||||
|
||||
// Returns the capacity of the tree, i.e. the maximum number of accumulatable leaves
|
||||
/// Returns the capacity of the tree, i.e. the maximum number of accumulatable leaves
|
||||
fn capacity(&self) -> usize {
|
||||
1 << self.depth
|
||||
}
|
||||
|
||||
// Returns the total number of leaves set
|
||||
/// Returns the total number of leaves set
|
||||
fn leaves_set(&self) -> usize {
|
||||
self.next_index
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
// Returns the root of the tree
|
||||
/// Returns the root of the tree
|
||||
fn root(&self) -> H::Fr {
|
||||
self.get_node(0, 0)
|
||||
}
|
||||
|
||||
fn get_subtree_root(&self, n: usize, index: usize) -> Result<H::Fr> {
|
||||
/// Sets a leaf at the specified tree index
|
||||
fn set(&mut self, index: usize, leaf: H::Fr) -> Result<(), ZerokitMerkleTreeError> {
|
||||
if index >= self.capacity() {
|
||||
return Err(ZerokitMerkleTreeError::InvalidLeaf);
|
||||
}
|
||||
self.nodes.insert((self.depth, index), leaf);
|
||||
self.update_hashes(index, 1)?;
|
||||
self.next_index = max(self.next_index, index + 1);
|
||||
self.cached_leaves_indices[index] = 1;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get a leaf from the specified tree index
|
||||
fn get(&self, index: usize) -> Result<H::Fr, ZerokitMerkleTreeError> {
|
||||
if index >= self.capacity() {
|
||||
return Err(ZerokitMerkleTreeError::InvalidLeaf);
|
||||
}
|
||||
Ok(self.get_node(self.depth, index))
|
||||
}
|
||||
|
||||
/// Returns the root of the subtree at level n and index
|
||||
fn get_subtree_root(&self, n: usize, index: usize) -> Result<H::Fr, ZerokitMerkleTreeError> {
|
||||
if n > self.depth() {
|
||||
return Err(Report::msg("level exceeds depth size"));
|
||||
return Err(ZerokitMerkleTreeError::InvalidLevel);
|
||||
}
|
||||
if index >= self.capacity() {
|
||||
return Err(Report::msg("index exceeds set size"));
|
||||
return Err(ZerokitMerkleTreeError::InvalidLeaf);
|
||||
}
|
||||
if n == 0 {
|
||||
Ok(self.root())
|
||||
@@ -129,26 +155,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
// Sets a leaf at the specified tree index
|
||||
fn set(&mut self, index: usize, leaf: H::Fr) -> Result<()> {
|
||||
if index >= self.capacity() {
|
||||
return Err(Report::msg("index exceeds set size"));
|
||||
}
|
||||
self.nodes.insert((self.depth, index), leaf);
|
||||
self.recalculate_from(index)?;
|
||||
self.next_index = max(self.next_index, index + 1);
|
||||
self.cached_leaves_indices[index] = 1;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Get a leaf from the specified tree index
|
||||
fn get(&self, index: usize) -> Result<H::Fr> {
|
||||
if index >= self.capacity() {
|
||||
return Err(Report::msg("index exceeds set size"));
|
||||
}
|
||||
Ok(self.get_node(self.depth, index))
|
||||
}
|
||||
|
||||
/// Returns the indices of the leaves that are empty
|
||||
fn get_empty_leaves_indices(&self) -> Vec<usize> {
|
||||
self.cached_leaves_indices
|
||||
.iter()
|
||||
@@ -159,29 +166,39 @@ where
|
||||
.collect()
|
||||
}
|
||||
|
||||
// Sets multiple leaves from the specified tree index
|
||||
fn set_range<I: IntoIterator<Item = H::Fr>>(&mut self, start: usize, leaves: I) -> Result<()> {
|
||||
let leaves = leaves.into_iter().collect::<Vec<_>>();
|
||||
/// Sets multiple leaves from the specified tree index
|
||||
fn set_range<I: ExactSizeIterator<Item = H::Fr>>(
|
||||
&mut self,
|
||||
start: usize,
|
||||
leaves: I,
|
||||
) -> Result<(), ZerokitMerkleTreeError> {
|
||||
// check if the range is valid
|
||||
if start + leaves.len() > self.capacity() {
|
||||
return Err(Report::msg("provided range exceeds set size"));
|
||||
let leaves_len = leaves.len();
|
||||
if start + leaves_len > self.capacity() {
|
||||
return Err(ZerokitMerkleTreeError::TooManySet);
|
||||
}
|
||||
for (i, leaf) in leaves.iter().enumerate() {
|
||||
self.nodes.insert((self.depth, start + i), *leaf);
|
||||
for (i, leaf) in leaves.enumerate() {
|
||||
self.nodes.insert((self.depth, start + i), leaf);
|
||||
self.cached_leaves_indices[start + i] = 1;
|
||||
self.recalculate_from(start + i)?;
|
||||
}
|
||||
self.next_index = max(self.next_index, start + leaves.len());
|
||||
self.update_hashes(start, leaves_len)?;
|
||||
self.next_index = max(self.next_index, start + leaves_len);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn override_range<I, J>(&mut self, start: usize, leaves: I, indices: J) -> Result<()>
|
||||
/// Overrides a range of leaves while resetting specified indices to default and preserving unaffected values.
|
||||
fn override_range<I, J>(
|
||||
&mut self,
|
||||
start: usize,
|
||||
leaves: I,
|
||||
indices: J,
|
||||
) -> Result<(), ZerokitMerkleTreeError>
|
||||
where
|
||||
I: IntoIterator<Item = FrOf<Self::Hasher>>,
|
||||
J: IntoIterator<Item = usize>,
|
||||
I: ExactSizeIterator<Item = FrOf<Self::Hasher>>,
|
||||
J: ExactSizeIterator<Item = usize>,
|
||||
{
|
||||
let indices = indices.into_iter().collect::<Vec<_>>();
|
||||
let min_index = *indices.first().unwrap();
|
||||
let min_index = *indices.first().expect("indices should not be empty");
|
||||
let leaves_vec = leaves.into_iter().collect::<Vec<_>>();
|
||||
|
||||
let max_index = start + leaves_vec.len();
|
||||
@@ -190,7 +207,7 @@ where
|
||||
|
||||
for i in min_index..start {
|
||||
if !indices.contains(&i) {
|
||||
let value = self.get_leaf(i);
|
||||
let value = self.get(i)?;
|
||||
set_values[i - min_index] = value;
|
||||
}
|
||||
}
|
||||
@@ -203,18 +220,17 @@ where
|
||||
self.cached_leaves_indices[i] = 0;
|
||||
}
|
||||
|
||||
self.set_range(start, set_values)
|
||||
.map_err(|e| Report::msg(e.to_string()))
|
||||
self.set_range(start, set_values.into_iter())
|
||||
}
|
||||
|
||||
// Sets a leaf at the next available index
|
||||
fn update_next(&mut self, leaf: H::Fr) -> Result<()> {
|
||||
/// Sets a leaf at the next available index
|
||||
fn update_next(&mut self, leaf: H::Fr) -> Result<(), ZerokitMerkleTreeError> {
|
||||
self.set(self.next_index, leaf)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Deletes a leaf at a certain index by setting it to its default value (next_index is not updated)
|
||||
fn delete(&mut self, index: usize) -> Result<()> {
|
||||
/// Deletes a leaf at a certain index by setting it to its default value (next_index is not updated)
|
||||
fn delete(&mut self, index: usize) -> Result<(), ZerokitMerkleTreeError> {
|
||||
// We reset the leaf only if we previously set a leaf at that index
|
||||
if index < self.next_index {
|
||||
self.set(index, H::default_leaf())?;
|
||||
@@ -223,17 +239,20 @@ where
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Computes a merkle proof the the leaf at the specified index
|
||||
fn proof(&self, index: usize) -> Result<Self::Proof> {
|
||||
/// Computes a merkle proof the leaf at the specified index
|
||||
fn proof(&self, index: usize) -> Result<Self::Proof, ZerokitMerkleTreeError> {
|
||||
if index >= self.capacity() {
|
||||
return Err(Report::msg("index exceeds set size"));
|
||||
return Err(ZerokitMerkleTreeError::InvalidLeaf);
|
||||
}
|
||||
let mut witness = Vec::<(H::Fr, u8)>::with_capacity(self.depth);
|
||||
let mut i = index;
|
||||
let mut depth = self.depth;
|
||||
loop {
|
||||
i ^= 1;
|
||||
witness.push((self.get_node(depth, i), (1 - (i & 1)).try_into().unwrap()));
|
||||
witness.push((
|
||||
self.get_node(depth, i),
|
||||
(1 - (i & 1)).try_into().expect("0 or 1 expected"),
|
||||
));
|
||||
i >>= 1;
|
||||
depth -= 1;
|
||||
if depth == 0 {
|
||||
@@ -241,78 +260,102 @@ where
|
||||
}
|
||||
}
|
||||
if i != 0 {
|
||||
Err(Report::msg("i != 0"))
|
||||
Err(ZerokitMerkleTreeError::ComputingProofError)
|
||||
} else {
|
||||
Ok(OptimalMerkleProof(witness))
|
||||
}
|
||||
}
|
||||
|
||||
// Verifies a Merkle proof with respect to the input leaf and the tree root
|
||||
fn verify(&self, leaf: &H::Fr, witness: &Self::Proof) -> Result<bool> {
|
||||
/// Verifies a Merkle proof with respect to the input leaf and the tree root
|
||||
fn verify(&self, leaf: &H::Fr, witness: &Self::Proof) -> Result<bool, ZerokitMerkleTreeError> {
|
||||
if witness.length() != self.depth {
|
||||
return Err(Report::msg("witness length doesn't match tree depth"));
|
||||
return Err(ZerokitMerkleTreeError::InvalidWitness);
|
||||
}
|
||||
let expected_root = witness.compute_root_from(leaf);
|
||||
Ok(expected_root.eq(&self.root()))
|
||||
}
|
||||
|
||||
fn compute_root(&mut self) -> Result<FrOf<Self::Hasher>> {
|
||||
self.recalculate_from(0)?;
|
||||
Ok(self.root())
|
||||
}
|
||||
|
||||
fn set_metadata(&mut self, metadata: &[u8]) -> Result<()> {
|
||||
fn set_metadata(&mut self, metadata: &[u8]) -> Result<(), ZerokitMerkleTreeError> {
|
||||
self.metadata = metadata.to_vec();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn metadata(&self) -> Result<Vec<u8>> {
|
||||
fn metadata(&self) -> Result<Vec<u8>, ZerokitMerkleTreeError> {
|
||||
Ok(self.metadata.to_vec())
|
||||
}
|
||||
}
|
||||
|
||||
// Utilities for updating the tree nodes
|
||||
impl<H: Hasher> OptimalMerkleTree<H>
|
||||
where
|
||||
H: Hasher,
|
||||
{
|
||||
// Utilities for updating the tree nodes
|
||||
|
||||
/// Returns the value of a node at a specific (depth, index).
|
||||
/// Falls back to a cached default if the node hasn't been set.
|
||||
fn get_node(&self, depth: usize, index: usize) -> H::Fr {
|
||||
let node = *self
|
||||
*self
|
||||
.nodes
|
||||
.get(&(depth, index))
|
||||
.unwrap_or_else(|| &self.cached_nodes[depth]);
|
||||
node
|
||||
.unwrap_or(&self.cached_nodes[depth])
|
||||
}
|
||||
|
||||
pub fn get_leaf(&self, index: usize) -> H::Fr {
|
||||
self.get_node(self.depth, index)
|
||||
}
|
||||
|
||||
fn hash_couple(&mut self, depth: usize, index: usize) -> H::Fr {
|
||||
/// Computes the hash of a node’s two children at the given depth.
|
||||
/// If the index is odd, it is rounded down to the nearest even index.
|
||||
fn hash_couple(&self, depth: usize, index: usize) -> H::Fr {
|
||||
let b = index & !1;
|
||||
H::hash(&[self.get_node(depth, b), self.get_node(depth, b + 1)])
|
||||
}
|
||||
|
||||
fn recalculate_from(&mut self, index: usize) -> Result<()> {
|
||||
let mut i = index;
|
||||
let mut depth = self.depth;
|
||||
loop {
|
||||
let h = self.hash_couple(depth, i);
|
||||
i >>= 1;
|
||||
depth -= 1;
|
||||
self.nodes.insert((depth, i), h);
|
||||
self.cached_leaves_indices[index] = 1;
|
||||
if depth == 0 {
|
||||
break;
|
||||
/// Updates parent hashes after modifying a range of leaf nodes.
|
||||
///
|
||||
/// - `start`: Starting leaf index that was updated.
|
||||
/// - `length`: Number of consecutive leaves that were updated.
|
||||
fn update_hashes(&mut self, start: usize, length: usize) -> Result<(), ZerokitMerkleTreeError> {
|
||||
// Start at the leaf level
|
||||
let mut current_depth = self.depth;
|
||||
|
||||
// Round down to include the left sibling in the pair (if start is odd)
|
||||
let mut current_index = start & !1;
|
||||
|
||||
// Compute the max index at this level, round up to include the last updated leaf’s right sibling (if start + length is odd)
|
||||
let mut current_index_max = (start + length + 1) & !1;
|
||||
|
||||
// Traverse from the leaf level up to the root
|
||||
while current_depth > 0 {
|
||||
// Compute the parent level (one level above the current)
|
||||
let parent_depth = current_depth - 1;
|
||||
|
||||
// Use parallel processing when the number of pairs exceeds the threshold
|
||||
if current_index_max - current_index >= MIN_PARALLEL_NODES {
|
||||
let updates: Vec<((usize, usize), H::Fr)> = (current_index..current_index_max)
|
||||
.step_by(2)
|
||||
.collect::<Vec<_>>()
|
||||
.into_par_iter()
|
||||
.map(|index| {
|
||||
// Hash two child nodes at positions (current_depth, index) and (current_depth, index + 1)
|
||||
let hash = self.hash_couple(current_depth, index);
|
||||
// Return the computed parent hash and its position at
|
||||
((parent_depth, index >> 1), hash)
|
||||
})
|
||||
.collect();
|
||||
|
||||
for (parent, hash) in updates {
|
||||
self.nodes.insert(parent, hash);
|
||||
}
|
||||
} else {
|
||||
// Otherwise, fallback to sequential update for small ranges
|
||||
for index in (current_index..current_index_max).step_by(2) {
|
||||
let hash = self.hash_couple(current_depth, index);
|
||||
self.nodes.insert((parent_depth, index >> 1), hash);
|
||||
}
|
||||
}
|
||||
|
||||
// Move up one level in the tree
|
||||
current_index >>= 1;
|
||||
current_index_max = (current_index_max + 1) >> 1;
|
||||
current_depth -= 1;
|
||||
}
|
||||
if depth != 0 {
|
||||
return Err(Report::msg("did not reach the depth"));
|
||||
}
|
||||
if i != 0 {
|
||||
return Err(Report::msg("did not go through all indexes"));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -324,14 +367,12 @@ where
|
||||
type Index = u8;
|
||||
type Hasher = H;
|
||||
|
||||
#[must_use]
|
||||
// Returns the length of a Merkle proof
|
||||
/// Returns the length of a Merkle proof
|
||||
fn length(&self) -> usize {
|
||||
self.0.len()
|
||||
}
|
||||
|
||||
/// Computes the leaf index corresponding to a Merkle proof
|
||||
#[must_use]
|
||||
fn leaf_index(&self) -> usize {
|
||||
// In current implementation the path indexes in a proof correspond to the binary representation of the leaf index
|
||||
let mut binary_repr = self.get_path_index();
|
||||
@@ -341,19 +382,16 @@ where
|
||||
.fold(0, |acc, digit| (acc << 1) + usize::from(digit))
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
/// Returns the path elements forming a Merkle proof
|
||||
fn get_path_elements(&self) -> Vec<H::Fr> {
|
||||
self.0.iter().map(|x| x.0).collect()
|
||||
}
|
||||
|
||||
/// Returns the path indexes forming a Merkle proof
|
||||
#[must_use]
|
||||
fn get_path_index(&self) -> Vec<u8> {
|
||||
self.0.iter().map(|x| x.1).collect()
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
/// Computes the Merkle root corresponding by iteratively hashing a Merkle proof with a given input leaf
|
||||
fn compute_root_from(&self, leaf: &H::Fr) -> H::Fr {
|
||||
let mut acc: H::Fr = *leaf;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
pub mod sled_adapter;
|
||||
pub use self::sled_adapter::*;
|
||||
pub use self::sled_adapter::SledDB;
|
||||
pub use pmtree;
|
||||
pub use sled::*;
|
||||
pub use sled::{Config, Mode};
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
pub mod poseidon_hash;
|
||||
pub use self::poseidon_hash::*;
|
||||
pub use poseidon_hash::Poseidon;
|
||||
|
||||
pub mod poseidon_constants;
|
||||
pub use self::poseidon_constants::*;
|
||||
|
||||
@@ -9,8 +9,6 @@
|
||||
|
||||
// The following implementation was adapted from https://github.com/arkworks-rs/sponge/blob/7d9b3a474c9ddb62890014aeaefcb142ac2b3776/src/poseidon/grain_lfsr.rs
|
||||
|
||||
#![allow(dead_code)]
|
||||
|
||||
use ark_ff::PrimeField;
|
||||
use num_bigint::BigUint;
|
||||
|
||||
@@ -20,7 +18,6 @@ pub struct PoseidonGrainLFSR {
|
||||
pub head: usize,
|
||||
}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
impl PoseidonGrainLFSR {
|
||||
pub fn new(
|
||||
is_field: u64,
|
||||
|
||||
@@ -7,7 +7,7 @@ use crate::poseidon_constants::find_poseidon_ark_and_mds;
|
||||
use ark_ff::PrimeField;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct RoundParamenters<F: PrimeField> {
|
||||
pub struct RoundParameters<F: PrimeField> {
|
||||
pub t: usize,
|
||||
pub n_rounds_f: usize,
|
||||
pub n_rounds_p: usize,
|
||||
@@ -17,16 +17,16 @@ pub struct RoundParamenters<F: PrimeField> {
|
||||
}
|
||||
|
||||
pub struct Poseidon<F: PrimeField> {
|
||||
round_params: Vec<RoundParamenters<F>>,
|
||||
round_params: Vec<RoundParameters<F>>,
|
||||
}
|
||||
impl<F: PrimeField> Poseidon<F> {
|
||||
// Loads round parameters and generates round constants
|
||||
// poseidon_params is a vector containing tuples (t, RF, RP, skip_matrices)
|
||||
// where: t is the rate (input lenght + 1), RF is the number of full rounds, RP is the number of partial rounds
|
||||
// where: t is the rate (input length + 1), RF is the number of full rounds, RP is the number of partial rounds
|
||||
// and skip_matrices is a (temporary) parameter used to generate secure MDS matrices (see comments in the description of find_poseidon_ark_and_mds)
|
||||
// TODO: implement automatic generation of round parameters
|
||||
pub fn from(poseidon_params: &[(usize, usize, usize, usize)]) -> Self {
|
||||
let mut read_params = Vec::<RoundParamenters<F>>::new();
|
||||
let mut read_params = Vec::<RoundParameters<F>>::with_capacity(poseidon_params.len());
|
||||
|
||||
for &(t, n_rounds_f, n_rounds_p, skip_matrices) in poseidon_params {
|
||||
let (ark, mds) = find_poseidon_ark_and_mds::<F>(
|
||||
@@ -38,7 +38,7 @@ impl<F: PrimeField> Poseidon<F> {
|
||||
n_rounds_p as u64,
|
||||
skip_matrices,
|
||||
);
|
||||
let rp = RoundParamenters {
|
||||
let rp = RoundParameters {
|
||||
t,
|
||||
n_rounds_p,
|
||||
n_rounds_f,
|
||||
@@ -54,24 +54,24 @@ impl<F: PrimeField> Poseidon<F> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_parameters(&self) -> Vec<RoundParamenters<F>> {
|
||||
self.round_params.clone()
|
||||
pub fn get_parameters(&self) -> &Vec<RoundParameters<F>> {
|
||||
&self.round_params
|
||||
}
|
||||
|
||||
pub fn ark(&self, state: &mut [F], c: &[F], it: usize) {
|
||||
for i in 0..state.len() {
|
||||
state[i] += c[it + i];
|
||||
}
|
||||
state.iter_mut().enumerate().for_each(|(i, elem)| {
|
||||
*elem += c[it + i];
|
||||
});
|
||||
}
|
||||
|
||||
pub fn sbox(&self, n_rounds_f: usize, n_rounds_p: usize, state: &mut [F], i: usize) {
|
||||
if (i < n_rounds_f / 2) || (i >= n_rounds_f / 2 + n_rounds_p) {
|
||||
for current_state in &mut state.iter_mut() {
|
||||
state.iter_mut().for_each(|current_state| {
|
||||
let aux = *current_state;
|
||||
*current_state *= *current_state;
|
||||
*current_state *= *current_state;
|
||||
*current_state *= aux;
|
||||
}
|
||||
})
|
||||
} else {
|
||||
let aux = state[0];
|
||||
state[0] *= state[0];
|
||||
@@ -80,21 +80,20 @@ impl<F: PrimeField> Poseidon<F> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn mix(&self, state: &[F], m: &[Vec<F>]) -> Vec<F> {
|
||||
let mut new_state: Vec<F> = Vec::new();
|
||||
pub fn mix_2(&self, state: &[F], m: &[Vec<F>], state_2: &mut [F]) {
|
||||
for i in 0..state.len() {
|
||||
new_state.push(F::zero());
|
||||
for (j, state_item) in state.iter().enumerate() {
|
||||
let mut mij = m[i][j];
|
||||
mij *= state_item;
|
||||
new_state[i] += mij;
|
||||
// Cache the row reference
|
||||
let row = &m[i];
|
||||
let mut acc = F::ZERO;
|
||||
for j in 0..state.len() {
|
||||
acc += row[j] * state[j];
|
||||
}
|
||||
state_2[i] = acc;
|
||||
}
|
||||
new_state.clone()
|
||||
}
|
||||
|
||||
pub fn hash(&self, inp: Vec<F>) -> Result<F, String> {
|
||||
// Note that the rate t becomes input lenght + 1, hence for lenght N we pick parameters with T = N + 1
|
||||
pub fn hash(&self, inp: &[F]) -> Result<F, String> {
|
||||
// Note that the rate t becomes input length + 1; hence for length N we pick parameters with T = N + 1
|
||||
let t = inp.len() + 1;
|
||||
|
||||
// We seek the index (Poseidon's round_params is an ordered vector) for the parameters corresponding to t
|
||||
@@ -106,8 +105,9 @@ impl<F: PrimeField> Poseidon<F> {
|
||||
|
||||
let param_index = param_index.unwrap();
|
||||
|
||||
let mut state = vec![F::zero(); t];
|
||||
state[1..].clone_from_slice(&inp);
|
||||
let mut state = vec![F::ZERO; t];
|
||||
let mut state_2 = state.clone();
|
||||
state[1..].clone_from_slice(inp);
|
||||
|
||||
for i in 0..(self.round_params[param_index].n_rounds_f
|
||||
+ self.round_params[param_index].n_rounds_p)
|
||||
@@ -123,7 +123,8 @@ impl<F: PrimeField> Poseidon<F> {
|
||||
&mut state,
|
||||
i,
|
||||
);
|
||||
state = self.mix(&state, &self.round_params[param_index].m);
|
||||
self.mix_2(&state, &self.round_params[param_index].m, &mut state_2);
|
||||
std::mem::swap(&mut state, &mut state_2);
|
||||
}
|
||||
|
||||
Ok(state[0])
|
||||
|
||||
@@ -6,7 +6,7 @@ pub mod test {
|
||||
use tiny_keccak::{Hasher as _, Keccak};
|
||||
use zerokit_utils::{
|
||||
FullMerkleConfig, FullMerkleTree, Hasher, OptimalMerkleConfig, OptimalMerkleTree,
|
||||
ZerokitMerkleProof, ZerokitMerkleTree,
|
||||
ZerokitMerkleProof, ZerokitMerkleTree, MIN_PARALLEL_NODES,
|
||||
};
|
||||
#[derive(Clone, Copy, Eq, PartialEq)]
|
||||
struct Keccak256;
|
||||
@@ -42,7 +42,7 @@ pub mod test {
|
||||
type Err = std::string::FromUtf8Error;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
Ok(TestFr(s.as_bytes().try_into().unwrap()))
|
||||
Ok(TestFr(s.as_bytes().try_into().expect("Invalid length")))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ pub mod test {
|
||||
fn from(value: u32) -> Self {
|
||||
let mut bytes: Vec<u8> = vec![0; 28];
|
||||
bytes.extend_from_slice(&value.to_be_bytes());
|
||||
TestFr(bytes.as_slice().try_into().unwrap())
|
||||
TestFr(bytes.as_slice().try_into().expect("Invalid length"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,12 +58,12 @@ pub mod test {
|
||||
|
||||
fn default_full_merkle_tree(depth: usize) -> FullMerkleTree<Keccak256> {
|
||||
FullMerkleTree::<Keccak256>::new(depth, TestFr([0; 32]), FullMerkleConfig::default())
|
||||
.unwrap()
|
||||
.expect("Failed to create FullMerkleTree")
|
||||
}
|
||||
|
||||
fn default_optimal_merkle_tree(depth: usize) -> OptimalMerkleTree<Keccak256> {
|
||||
OptimalMerkleTree::<Keccak256>::new(depth, TestFr([0; 32]), OptimalMerkleConfig::default())
|
||||
.unwrap()
|
||||
.expect("Failed to create OptimalMerkleTree")
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -83,21 +83,101 @@ pub mod test {
|
||||
let nof_leaves = 4;
|
||||
let leaves: Vec<TestFr> = (1..=nof_leaves as u32).map(TestFr::from).collect();
|
||||
|
||||
let mut tree = default_full_merkle_tree(DEFAULT_DEPTH);
|
||||
assert_eq!(tree.root(), default_tree_root);
|
||||
let mut tree_full = default_full_merkle_tree(DEFAULT_DEPTH);
|
||||
assert_eq!(tree_full.root(), default_tree_root);
|
||||
for i in 0..nof_leaves {
|
||||
tree.set(i, leaves[i]).unwrap();
|
||||
assert_eq!(tree.root(), roots[i]);
|
||||
tree_full.set(i, leaves[i]).expect("Failed to set leaf");
|
||||
assert_eq!(tree_full.root(), roots[i]);
|
||||
}
|
||||
|
||||
let mut tree = default_optimal_merkle_tree(DEFAULT_DEPTH);
|
||||
assert_eq!(tree.root(), default_tree_root);
|
||||
let mut tree_opt = default_optimal_merkle_tree(DEFAULT_DEPTH);
|
||||
assert_eq!(tree_opt.root(), default_tree_root);
|
||||
for i in 0..nof_leaves {
|
||||
tree.set(i, leaves[i]).unwrap();
|
||||
assert_eq!(tree.root(), roots[i]);
|
||||
tree_opt.set(i, leaves[i]).expect("Failed to set leaf");
|
||||
assert_eq!(tree_opt.root(), roots[i]);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_set_range() {
|
||||
let depth = 4;
|
||||
let leaves: Vec<TestFr> = (0..(1 << depth) as u32).map(TestFr::from).collect();
|
||||
|
||||
let mut tree_full = default_full_merkle_tree(depth);
|
||||
let root_before = tree_full.root();
|
||||
tree_full
|
||||
.set_range(0, leaves.iter().cloned())
|
||||
.expect("Failed to set leaves");
|
||||
let root_after = tree_full.root();
|
||||
assert_ne!(root_before, root_after);
|
||||
|
||||
let mut tree_opt = default_optimal_merkle_tree(depth);
|
||||
let root_before = tree_opt.root();
|
||||
tree_opt
|
||||
.set_range(0, leaves.iter().cloned())
|
||||
.expect("Failed to set leaves");
|
||||
let root_after = tree_opt.root();
|
||||
assert_ne!(root_before, root_after);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_update_next() {
|
||||
let mut tree_full = default_full_merkle_tree(DEFAULT_DEPTH);
|
||||
let mut tree_opt = default_optimal_merkle_tree(DEFAULT_DEPTH);
|
||||
|
||||
for i in 0..4 {
|
||||
let leaf = TestFr::from(i as u32);
|
||||
tree_full.update_next(leaf).expect("Failed to update leaf");
|
||||
tree_opt.update_next(leaf).expect("Failed to update leaf");
|
||||
assert_eq!(tree_full.get(i).expect("Failed to get leaf"), leaf);
|
||||
assert_eq!(tree_opt.get(i).expect("Failed to get leaf"), leaf);
|
||||
}
|
||||
|
||||
assert_eq!(tree_full.leaves_set(), 4);
|
||||
assert_eq!(tree_opt.leaves_set(), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_delete_and_reset() {
|
||||
let index = 1;
|
||||
let original_leaf = TestFr::from(42);
|
||||
let new_leaf = TestFr::from(99);
|
||||
|
||||
let mut tree_full = default_full_merkle_tree(DEFAULT_DEPTH);
|
||||
tree_full
|
||||
.set(index, original_leaf)
|
||||
.expect("Failed to set leaf");
|
||||
let root_with_original = tree_full.root();
|
||||
|
||||
tree_full.delete(index).expect("Failed to delete leaf");
|
||||
let root_after_delete = tree_full.root();
|
||||
assert_ne!(root_with_original, root_after_delete);
|
||||
|
||||
tree_full.set(index, new_leaf).expect("Failed to set leaf");
|
||||
let root_after_reset = tree_full.root();
|
||||
|
||||
assert_ne!(root_after_delete, root_after_reset);
|
||||
assert_ne!(root_with_original, root_after_reset);
|
||||
assert_eq!(tree_full.get(index).expect("Failed to get leaf"), new_leaf);
|
||||
|
||||
let mut tree_opt = default_optimal_merkle_tree(DEFAULT_DEPTH);
|
||||
tree_opt
|
||||
.set(index, original_leaf)
|
||||
.expect("Failed to set leaf");
|
||||
let root_with_original = tree_opt.root();
|
||||
|
||||
tree_opt.delete(index).expect("Failed to delete leaf");
|
||||
let root_after_delete = tree_opt.root();
|
||||
assert_ne!(root_with_original, root_after_delete);
|
||||
|
||||
tree_opt.set(index, new_leaf).expect("Failed to set leaf");
|
||||
let root_after_reset = tree_opt.root();
|
||||
|
||||
assert_ne!(root_after_delete, root_after_reset);
|
||||
assert_ne!(root_with_original, root_after_reset);
|
||||
assert_eq!(tree_opt.get(index).expect("Failed to get leaf"), new_leaf);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_empty_leaves_indices() {
|
||||
let depth = 4;
|
||||
@@ -107,7 +187,7 @@ pub mod test {
|
||||
let leaves_4: Vec<TestFr> = (0u32..4).map(TestFr::from).collect();
|
||||
|
||||
let mut tree_full = default_full_merkle_tree(depth);
|
||||
let _ = tree_full.set_range(0, leaves.clone());
|
||||
let _ = tree_full.set_range(0, leaves.clone().into_iter());
|
||||
assert!(tree_full.get_empty_leaves_indices().is_empty());
|
||||
|
||||
let mut vec_idxs = Vec::new();
|
||||
@@ -123,33 +203,31 @@ pub mod test {
|
||||
assert_eq!(tree_full.get_empty_leaves_indices(), vec_idxs);
|
||||
}
|
||||
|
||||
// Check situation when the number of items to insert is less than the number of items to delete
|
||||
// check situation when the number of items to insert is less than the number of items to delete
|
||||
tree_full
|
||||
.override_range(0, leaves_2.clone(), [0, 1, 2, 3])
|
||||
.unwrap();
|
||||
.override_range(0, leaves_2.clone().into_iter(), [0, 1, 2, 3].into_iter())
|
||||
.expect("Failed to override range");
|
||||
|
||||
// check if the indexes for write and delete are the same
|
||||
tree_full
|
||||
.override_range(0, leaves_4.clone(), [0, 1, 2, 3])
|
||||
.unwrap();
|
||||
assert_eq!(tree_full.get_empty_leaves_indices(), vec![]);
|
||||
.override_range(0, leaves_4.clone().into_iter(), [0, 1, 2, 3].into_iter())
|
||||
.expect("Failed to override range");
|
||||
assert_eq!(tree_full.get_empty_leaves_indices(), Vec::<usize>::new());
|
||||
|
||||
// check if indexes for deletion are before indexes for overwriting
|
||||
tree_full
|
||||
.override_range(4, leaves_4.clone(), [0, 1, 2, 3])
|
||||
.unwrap();
|
||||
.override_range(4, leaves_4.clone().into_iter(), [0, 1, 2, 3].into_iter())
|
||||
.expect("Failed to override range");
|
||||
assert_eq!(tree_full.get_empty_leaves_indices(), vec![0, 1, 2, 3]);
|
||||
|
||||
// check if the indices for write and delete do not overlap completely
|
||||
tree_full
|
||||
.override_range(2, leaves_4.clone(), [0, 1, 2, 3])
|
||||
.unwrap();
|
||||
.override_range(2, leaves_4.clone().into_iter(), [0, 1, 2, 3].into_iter())
|
||||
.expect("Failed to override range");
|
||||
assert_eq!(tree_full.get_empty_leaves_indices(), vec![0, 1]);
|
||||
|
||||
//// Optimal Merkle Tree Trest
|
||||
|
||||
let mut tree_opt = default_optimal_merkle_tree(depth);
|
||||
let _ = tree_opt.set_range(0, leaves.clone());
|
||||
let _ = tree_opt.set_range(0, leaves.clone().into_iter());
|
||||
assert!(tree_opt.get_empty_leaves_indices().is_empty());
|
||||
|
||||
let mut vec_idxs = Vec::new();
|
||||
@@ -164,48 +242,55 @@ pub mod test {
|
||||
assert_eq!(tree_opt.get_empty_leaves_indices(), vec_idxs);
|
||||
}
|
||||
|
||||
// Check situation when the number of items to insert is less than the number of items to delete
|
||||
// check situation when the number of items to insert is less than the number of items to delete
|
||||
tree_opt
|
||||
.override_range(0, leaves_2.clone(), [0, 1, 2, 3])
|
||||
.unwrap();
|
||||
.override_range(0, leaves_2.clone().into_iter(), [0, 1, 2, 3].into_iter())
|
||||
.expect("Failed to override range");
|
||||
|
||||
// check if the indexes for write and delete are the same
|
||||
tree_opt
|
||||
.override_range(0, leaves_4.clone(), [0, 1, 2, 3])
|
||||
.unwrap();
|
||||
assert_eq!(tree_opt.get_empty_leaves_indices(), vec![]);
|
||||
.override_range(0, leaves_4.clone().into_iter(), [0, 1, 2, 3].into_iter())
|
||||
.expect("Failed to override range");
|
||||
assert_eq!(tree_opt.get_empty_leaves_indices(), Vec::<usize>::new());
|
||||
|
||||
// check if indexes for deletion are before indexes for overwriting
|
||||
tree_opt
|
||||
.override_range(4, leaves_4.clone(), [0, 1, 2, 3])
|
||||
.unwrap();
|
||||
.override_range(4, leaves_4.clone().into_iter(), [0, 1, 2, 3].into_iter())
|
||||
.expect("Failed to override range");
|
||||
assert_eq!(tree_opt.get_empty_leaves_indices(), vec![0, 1, 2, 3]);
|
||||
|
||||
// check if the indices for write and delete do not overlap completely
|
||||
tree_opt
|
||||
.override_range(2, leaves_4.clone(), [0, 1, 2, 3])
|
||||
.unwrap();
|
||||
.override_range(2, leaves_4.clone().into_iter(), [0, 1, 2, 3].into_iter())
|
||||
.expect("Failed to override range");
|
||||
assert_eq!(tree_opt.get_empty_leaves_indices(), vec![0, 1]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_subtree_root() {
|
||||
let depth = 3;
|
||||
let nof_leaves: usize = 6;
|
||||
let nof_leaves: usize = 4;
|
||||
let leaves: Vec<TestFr> = (0..nof_leaves as u32).map(TestFr::from).collect();
|
||||
|
||||
let mut tree_full = default_optimal_merkle_tree(depth);
|
||||
let mut tree_full = default_full_merkle_tree(depth);
|
||||
let _ = tree_full.set_range(0, leaves.iter().cloned());
|
||||
|
||||
for i in 0..nof_leaves {
|
||||
// check leaves
|
||||
assert_eq!(
|
||||
tree_full.get(i).unwrap(),
|
||||
tree_full.get_subtree_root(depth, i).unwrap()
|
||||
tree_full.get(i).expect("Failed to get leaf"),
|
||||
tree_full
|
||||
.get_subtree_root(depth, i)
|
||||
.expect("Failed to get subtree root")
|
||||
);
|
||||
|
||||
// check root
|
||||
assert_eq!(tree_full.root(), tree_full.get_subtree_root(0, i).unwrap());
|
||||
assert_eq!(
|
||||
tree_full.root(),
|
||||
tree_full
|
||||
.get_subtree_root(0, i)
|
||||
.expect("Failed to get subtree root")
|
||||
);
|
||||
}
|
||||
|
||||
// check intermediate nodes
|
||||
@@ -215,26 +300,39 @@ pub mod test {
|
||||
let idx_r = (i + 1) * (1 << (depth - n));
|
||||
let idx_sr = idx_l;
|
||||
|
||||
let prev_l = tree_full.get_subtree_root(n, idx_l).unwrap();
|
||||
let prev_r = tree_full.get_subtree_root(n, idx_r).unwrap();
|
||||
let subroot = tree_full.get_subtree_root(n - 1, idx_sr).unwrap();
|
||||
let prev_l = tree_full
|
||||
.get_subtree_root(n, idx_l)
|
||||
.expect("Failed to get subtree root");
|
||||
let prev_r = tree_full
|
||||
.get_subtree_root(n, idx_r)
|
||||
.expect("Failed to get subtree root");
|
||||
let subroot = tree_full
|
||||
.get_subtree_root(n - 1, idx_sr)
|
||||
.expect("Failed to get subtree root");
|
||||
|
||||
// check intermediate nodes
|
||||
assert_eq!(Keccak256::hash(&[prev_l, prev_r]), subroot);
|
||||
}
|
||||
}
|
||||
|
||||
let mut tree_opt = default_full_merkle_tree(depth);
|
||||
let mut tree_opt = default_optimal_merkle_tree(depth);
|
||||
let _ = tree_opt.set_range(0, leaves.iter().cloned());
|
||||
|
||||
for i in 0..nof_leaves {
|
||||
// check leaves
|
||||
assert_eq!(
|
||||
tree_opt.get(i).unwrap(),
|
||||
tree_opt.get_subtree_root(depth, i).unwrap()
|
||||
tree_opt.get(i).expect("Failed to get leaf"),
|
||||
tree_opt
|
||||
.get_subtree_root(depth, i)
|
||||
.expect("Failed to get subtree root")
|
||||
);
|
||||
// check root
|
||||
assert_eq!(tree_opt.root(), tree_opt.get_subtree_root(0, i).unwrap());
|
||||
assert_eq!(
|
||||
tree_opt.root(),
|
||||
tree_opt
|
||||
.get_subtree_root(0, i)
|
||||
.expect("Failed to get subtree root")
|
||||
);
|
||||
}
|
||||
|
||||
// check intermediate nodes
|
||||
@@ -244,9 +342,15 @@ pub mod test {
|
||||
let idx_r = (i + 1) * (1 << (depth - n));
|
||||
let idx_sr = idx_l;
|
||||
|
||||
let prev_l = tree_opt.get_subtree_root(n, idx_l).unwrap();
|
||||
let prev_r = tree_opt.get_subtree_root(n, idx_r).unwrap();
|
||||
let subroot = tree_opt.get_subtree_root(n - 1, idx_sr).unwrap();
|
||||
let prev_l = tree_opt
|
||||
.get_subtree_root(n, idx_l)
|
||||
.expect("Failed to get subtree root");
|
||||
let prev_r = tree_opt
|
||||
.get_subtree_root(n, idx_r)
|
||||
.expect("Failed to get subtree root");
|
||||
let subroot = tree_opt
|
||||
.get_subtree_root(n - 1, idx_sr)
|
||||
.expect("Failed to get subtree root");
|
||||
|
||||
// check intermediate nodes
|
||||
assert_eq!(Keccak256::hash(&[prev_l, prev_r]), subroot);
|
||||
@@ -259,61 +363,83 @@ pub mod test {
|
||||
let nof_leaves = 4;
|
||||
let leaves: Vec<TestFr> = (0..nof_leaves as u32).map(TestFr::from).collect();
|
||||
|
||||
// We thest the FullMerkleTree implementation
|
||||
let mut tree = default_full_merkle_tree(DEFAULT_DEPTH);
|
||||
// We test the FullMerkleTree implementation
|
||||
let mut tree_full = default_full_merkle_tree(DEFAULT_DEPTH);
|
||||
for i in 0..nof_leaves {
|
||||
// We set the leaves
|
||||
tree.set(i, leaves[i]).unwrap();
|
||||
tree_full.set(i, leaves[i]).expect("Failed to set leaf");
|
||||
|
||||
// We compute a merkle proof
|
||||
let proof = tree.proof(i).expect("index should be set");
|
||||
let proof = tree_full.proof(i).expect("Failed to compute proof");
|
||||
|
||||
// We verify if the merkle proof corresponds to the right leaf index
|
||||
assert_eq!(proof.leaf_index(), i);
|
||||
|
||||
// We verify the proof
|
||||
assert!(tree.verify(&leaves[i], &proof).unwrap());
|
||||
assert!(tree_full
|
||||
.verify(&leaves[i], &proof)
|
||||
.expect("Failed to verify proof"));
|
||||
|
||||
// We ensure that the Merkle proof and the leaf generate the same root as the tree
|
||||
assert_eq!(proof.compute_root_from(&leaves[i]), tree.root());
|
||||
assert_eq!(proof.compute_root_from(&leaves[i]), tree_full.root());
|
||||
|
||||
// We check that the proof is not valid for another leaf
|
||||
assert!(!tree.verify(&leaves[(i + 1) % nof_leaves], &proof).unwrap());
|
||||
assert!(!tree_full
|
||||
.verify(&leaves[(i + 1) % nof_leaves], &proof)
|
||||
.expect("Failed to verify proof"));
|
||||
}
|
||||
|
||||
// We test the OptimalMerkleTree implementation
|
||||
let mut tree = default_optimal_merkle_tree(DEFAULT_DEPTH);
|
||||
let mut tree_opt = default_optimal_merkle_tree(DEFAULT_DEPTH);
|
||||
for i in 0..nof_leaves {
|
||||
// We set the leaves
|
||||
tree.set(i, leaves[i]).unwrap();
|
||||
tree_opt.set(i, leaves[i]).expect("Failed to set leaf");
|
||||
|
||||
// We compute a merkle proof
|
||||
let proof = tree.proof(i).expect("index should be set");
|
||||
let proof = tree_opt.proof(i).expect("Failed to compute proof");
|
||||
|
||||
// We verify if the merkle proof corresponds to the right leaf index
|
||||
assert_eq!(proof.leaf_index(), i);
|
||||
|
||||
// We verify the proof
|
||||
assert!(tree.verify(&leaves[i], &proof).unwrap());
|
||||
assert!(tree_opt
|
||||
.verify(&leaves[i], &proof)
|
||||
.expect("Failed to verify proof"));
|
||||
|
||||
// We ensure that the Merkle proof and the leaf generate the same root as the tree
|
||||
assert_eq!(proof.compute_root_from(&leaves[i]), tree.root());
|
||||
assert_eq!(proof.compute_root_from(&leaves[i]), tree_opt.root());
|
||||
|
||||
// We check that the proof is not valid for another leaf
|
||||
assert!(!tree.verify(&leaves[(i + 1) % nof_leaves], &proof).unwrap());
|
||||
assert!(!tree_opt
|
||||
.verify(&leaves[(i + 1) % nof_leaves], &proof)
|
||||
.expect("Failed to verify proof"));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_proof_fail() {
|
||||
let tree_full = default_full_merkle_tree(DEFAULT_DEPTH);
|
||||
let tree_opt = default_optimal_merkle_tree(DEFAULT_DEPTH);
|
||||
|
||||
let invalid_leaf = TestFr::from(12345);
|
||||
|
||||
let proof_full = tree_full.proof(0).expect("Failed to compute proof");
|
||||
let proof_opt = tree_opt.proof(0).expect("Failed to compute proof");
|
||||
|
||||
// Should fail because no leaf was set
|
||||
assert!(!tree_full
|
||||
.verify(&invalid_leaf, &proof_full)
|
||||
.expect("Failed to verify proof"));
|
||||
assert!(!tree_opt
|
||||
.verify(&invalid_leaf, &proof_opt)
|
||||
.expect("Failed to verify proof"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_override_range() {
|
||||
let nof_leaves = 4;
|
||||
let leaves: Vec<TestFr> = (0..nof_leaves as u32).map(TestFr::from).collect();
|
||||
|
||||
let mut tree = default_optimal_merkle_tree(DEFAULT_DEPTH);
|
||||
|
||||
// We set the leaves
|
||||
tree.set_range(0, leaves.iter().cloned()).unwrap();
|
||||
|
||||
let new_leaves = [
|
||||
hex!("0000000000000000000000000000000000000000000000000000000000000005"),
|
||||
hex!("0000000000000000000000000000000000000000000000000000000000000006"),
|
||||
@@ -322,17 +448,70 @@ pub mod test {
|
||||
|
||||
let to_delete_indices: [usize; 2] = [0, 1];
|
||||
|
||||
// We override the leaves
|
||||
tree.override_range(
|
||||
0, // start from the end of the initial leaves
|
||||
new_leaves.iter().cloned(),
|
||||
to_delete_indices.iter().cloned(),
|
||||
)
|
||||
.unwrap();
|
||||
let mut tree_full = default_full_merkle_tree(DEFAULT_DEPTH);
|
||||
tree_full
|
||||
.set_range(0, leaves.iter().cloned())
|
||||
.expect("Failed to set leaves");
|
||||
|
||||
tree_full
|
||||
.override_range(
|
||||
0,
|
||||
new_leaves.iter().cloned(),
|
||||
to_delete_indices.iter().cloned(),
|
||||
)
|
||||
.expect("Failed to override range");
|
||||
|
||||
// ensure that the leaves are set correctly
|
||||
for (i, &new_leaf) in new_leaves.iter().enumerate() {
|
||||
assert_eq!(tree.get_leaf(i), new_leaf);
|
||||
assert_eq!(tree_full.get(i).expect("Failed to get leaf"), new_leaf);
|
||||
}
|
||||
|
||||
let mut tree_opt = default_optimal_merkle_tree(DEFAULT_DEPTH);
|
||||
tree_opt
|
||||
.set_range(0, leaves.iter().cloned())
|
||||
.expect("Failed to set leaves");
|
||||
|
||||
tree_opt
|
||||
.override_range(
|
||||
0,
|
||||
new_leaves.iter().cloned(),
|
||||
to_delete_indices.iter().cloned(),
|
||||
)
|
||||
.expect("Failed to override range");
|
||||
|
||||
for (i, &new_leaf) in new_leaves.iter().enumerate() {
|
||||
assert_eq!(tree_opt.get(i).expect("Failed to get leaf"), new_leaf);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_override_range_parallel_triggered() {
|
||||
let depth = 13;
|
||||
let nof_leaves = 8192;
|
||||
|
||||
// number of leaves larger than MIN_PARALLEL_NODES to trigger parallel hashing
|
||||
assert!(MIN_PARALLEL_NODES < nof_leaves);
|
||||
|
||||
let leaves: Vec<TestFr> = (0..nof_leaves as u32).map(TestFr::from).collect();
|
||||
let indices: Vec<usize> = (0..nof_leaves).collect();
|
||||
|
||||
let mut tree_full = default_full_merkle_tree(depth);
|
||||
|
||||
tree_full
|
||||
.override_range(0, leaves.iter().cloned(), indices.iter().cloned())
|
||||
.expect("Failed to override range");
|
||||
|
||||
for (i, &leaf) in leaves.iter().enumerate() {
|
||||
assert_eq!(tree_full.get(i).expect("Failed to get leaf"), leaf);
|
||||
}
|
||||
|
||||
let mut tree_opt = default_optimal_merkle_tree(depth);
|
||||
|
||||
tree_opt
|
||||
.override_range(0, leaves.iter().cloned(), indices.iter().cloned())
|
||||
.expect("Failed to override range");
|
||||
|
||||
for (i, &leaf) in leaves.iter().enumerate() {
|
||||
assert_eq!(tree_opt.get(i).expect("Failed to get leaf"), leaf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3530,7 +3530,8 @@ mod test {
|
||||
{
|
||||
// We check if the round constants and matrices correspond to the one generated when instantiating Poseidon with ROUND_PARAMS
|
||||
let (loaded_c, loaded_m) = load_constants();
|
||||
let poseidon_parameters = Poseidon::<Fr>::from(&ROUND_PARAMS).get_parameters();
|
||||
let poseidon_hasher = Poseidon::<Fr>::from(&ROUND_PARAMS);
|
||||
let poseidon_parameters = poseidon_hasher.get_parameters();
|
||||
for i in 0..poseidon_parameters.len() {
|
||||
assert_eq!(loaded_c[i], poseidon_parameters[i].c);
|
||||
assert_eq!(loaded_m[i], poseidon_parameters[i].m);
|
||||
|
||||
131
utils/tests/poseidon_hash_test.rs
Normal file
131
utils/tests/poseidon_hash_test.rs
Normal file
@@ -0,0 +1,131 @@
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use ark_bn254::Fr;
|
||||
use ark_ff::{AdditiveGroup, Field};
|
||||
use std::collections::HashMap;
|
||||
use std::str::FromStr;
|
||||
use zerokit_utils::poseidon_hash::Poseidon;
|
||||
|
||||
const ROUND_PARAMS: [(usize, usize, usize, usize); 8] = [
|
||||
(2, 8, 56, 0),
|
||||
(3, 8, 57, 0),
|
||||
(4, 8, 56, 0),
|
||||
(5, 8, 60, 0),
|
||||
(6, 8, 60, 0),
|
||||
(7, 8, 63, 0),
|
||||
(8, 8, 64, 0),
|
||||
(9, 8, 63, 0),
|
||||
];
|
||||
|
||||
#[test]
|
||||
fn test_poseidon_hash_basic() {
|
||||
let map = HashMap::from([
|
||||
(
|
||||
Fr::ZERO,
|
||||
Fr::from_str(
|
||||
"19014214495641488759237505126948346942972912379615652741039992445865937985820",
|
||||
)
|
||||
.unwrap(),
|
||||
),
|
||||
(
|
||||
Fr::ONE,
|
||||
Fr::from_str(
|
||||
"18586133768512220936620570745912940619677854269274689475585506675881198879027",
|
||||
)
|
||||
.unwrap(),
|
||||
),
|
||||
(
|
||||
Fr::from(255),
|
||||
Fr::from_str(
|
||||
"20026131459732984724454933360292530547665726761019872861025481903072111625788",
|
||||
)
|
||||
.unwrap(),
|
||||
),
|
||||
(
|
||||
Fr::from(u16::MAX),
|
||||
Fr::from_str(
|
||||
"12358868638722666642632413418981275677998688723398440898957566982787708451243",
|
||||
)
|
||||
.unwrap(),
|
||||
),
|
||||
(
|
||||
Fr::from(u64::MAX),
|
||||
Fr::from_str(
|
||||
"17449307747295017006142981453320720946812828330895590310359634430146721583189",
|
||||
)
|
||||
.unwrap(),
|
||||
),
|
||||
]);
|
||||
|
||||
// map (key: what to hash, value: expected value)
|
||||
for (k, v) in map.into_iter() {
|
||||
let hasher = Poseidon::<Fr>::from(&ROUND_PARAMS);
|
||||
let h = hasher.hash(&[k]);
|
||||
assert_eq!(h.unwrap(), v);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_poseidon_hash_multi() {
|
||||
// All hashes done in a merkle tree (with leaves: [0, 1, 2, 3, 4, 5, 6, 7])
|
||||
|
||||
// ~ leaves
|
||||
let fr_0 = Fr::ZERO;
|
||||
let fr_1 = Fr::ONE;
|
||||
let fr_2 = Fr::from(2);
|
||||
let fr_3 = Fr::from(3);
|
||||
let fr_4 = Fr::from(4);
|
||||
let fr_5 = Fr::from(5);
|
||||
let fr_6 = Fr::from(6);
|
||||
let fr_7 = Fr::from(7);
|
||||
|
||||
let fr_0_1 = Fr::from_str(
|
||||
"12583541437132735734108669866114103169564651237895298778035846191048104863326",
|
||||
)
|
||||
.unwrap();
|
||||
let fr_2_3 = Fr::from_str(
|
||||
"17197790661637433027297685226742709599380837544520340689137581733613433332983",
|
||||
)
|
||||
.unwrap();
|
||||
let fr_4_5 = Fr::from_str(
|
||||
"756592041685769348226045093946546956867261766023639881791475046640232555043",
|
||||
)
|
||||
.unwrap();
|
||||
let fr_6_7 = Fr::from_str(
|
||||
"5558359459771725727593826278265342308584225092343962757289948761260561575479",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let fr_0_3 = Fr::from_str(
|
||||
"3720616653028013822312861221679392249031832781774563366107458835261883914924",
|
||||
)
|
||||
.unwrap();
|
||||
let fr_4_7 = Fr::from_str(
|
||||
"7960741062684589801276390367952372418815534638314682948141519164356522829957",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
// ~ root
|
||||
let fr_0_7 = Fr::from_str(
|
||||
"11780650233517635876913804110234352847867393797952240856403268682492028497284",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
// map (key: what to hash, value: expected value)
|
||||
let map = HashMap::from([
|
||||
((fr_0, fr_1), fr_0_1),
|
||||
((fr_2, fr_3), fr_2_3),
|
||||
((fr_4, fr_5), fr_4_5),
|
||||
((fr_6, fr_7), fr_6_7),
|
||||
((fr_0_1, fr_2_3), fr_0_3),
|
||||
((fr_4_5, fr_6_7), fr_4_7),
|
||||
((fr_0_3, fr_4_7), fr_0_7),
|
||||
]);
|
||||
|
||||
for (k, v) in map.into_iter() {
|
||||
let hasher = Poseidon::<Fr>::from(&ROUND_PARAMS);
|
||||
let h = hasher.hash(&[k.0, k.1]);
|
||||
assert_eq!(h.unwrap(), v);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user