🦀 Rust support library for semaphore
Go to file
Remco Bloemen 88429bfc1a Cleanup 2022-06-03 21:19:06 -07:00
semaphore@5186a940ff added semaphore submodule 2022-03-06 19:12:39 +01:00
src Cleanup 2022-06-03 21:19:06 -07:00
.gitignore Merge branch 'main' into remco/ci 2022-03-11 10:59:55 -08:00
.gitmodules chore: update semaphore submodule to https 2022-03-18 13:10:14 +08:00
Cargo.toml Cleanup 2022-06-03 21:19:06 -07:00
README.md Newtype the Field so we can add serializers 2022-03-17 14:58:14 -07:00
build.rs Clippy 2022-04-04 16:05:44 -07:00
criterion.rs Add benchmark 2022-02-28 19:44:30 -08:00
cspell.json Newtype the Field so we can add serializers 2022-03-17 14:58:14 -07:00
mit-license.md Update crate info 2022-02-01 12:17:05 -08:00
rustfmt.toml Add formating rules 2022-02-01 11:40:49 -08:00

README.md

🦀 semaphore-rs

Rust support library for using semaphore. It's mostly a Rust rewrite of zk-kit, but just focuses on semaphore (for now) and still covers a much smaller scope. It's using ark-circom under the hood for generating the groth16 proofs.

Usage

Add this line to your cargo.toml:

semaphore = { git = "https://github.com/worldcoin/semaphore-rs" }

Building semaphore circuits

  1. Check out submodule (if not done before already): git submodule update --init --recursive
  2. Install semaphore dependencies cd semaphore && npm install
  3. Compile circuits npm exec ts-node ./scripts/compile-circuits.ts
  4. You'll find the zkey and wasm file in semaphore/build/snark

Example

Example as in src/lib.rs, run with cargo test.

use semaphore::{hash_to_field, Field, identity::Identity, poseidon_tree::PoseidonTree,
    protocol::* };
use num_bigint::BigInt;

// generate identity
let id = Identity::from_seed(b"secret");

// generate merkle tree
let leaf = Field::from(0);
let mut tree = PoseidonTree::new(21, leaf);
tree.set(0, id.commitment());

let merkle_proof = tree.proof(0).expect("proof should exist");
let root = tree.root();

// change signal and external_nullifier here
let signal_hash = hash_to_field(b"xxx");
let external_nullifier_hash = hash_to_field(b"appId");

let nullifier_hash = generate_nullifier_hash(&id, external_nullifier_hash);

let proof = generate_proof(&id, &merkle_proof, external_nullifier_hash, signal_hash).unwrap();
let success = verify_proof(root, nullifier_hash, signal_hash, external_nullifier_hash, &proof).unwrap();

assert!(success);