🦀 Rust support library for semaphore
Go to file
Remco Bloemen 9b8988419d
Merge pull request #1 from worldcoin/philsippl/extract_external_nullifier
extract external_nullifier calculation
2022-03-10 13:23:00 -08:00
semaphore@5186a940ff added semaphore submodule 2022-03-06 19:12:39 +01:00
src extract external_nullifier calculation 2022-03-09 15:53:03 +01:00
.gitignore remove snarkfiles dir 2022-03-06 19:15:44 +01:00
.gitmodules added semaphore submodule 2022-03-06 19:12:39 +01:00
Cargo.lock Add optional mimc support 2022-03-07 11:41:42 +01:00
Cargo.toml Add optional mimc support 2022-03-07 11:41:42 +01:00
README.md extract external_nullifier calculation 2022-03-09 15:53:03 +01:00
criterion.rs Add benchmark 2022-02-28 19:44:30 -08: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 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.

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

// generate merkle tree
const LEAF: Hash = Hash::from_bytes_be([0u8; 32]);

let mut tree = PoseidonTree::new(21, LEAF);
let (_, leaf) = id.commitment().to_bytes_be();
tree.set(0, leaf.into());

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

// change signal and external_nullifier here
let signal = "xxx".as_bytes();
let external_nullifier = "appId".as_bytes();

let external_nullifier_hash = hash_external_nullifier(external_nullifier);
let nullifier_hash = generate_nullifier_hash(&id, &external_nullifier_hash);

let config = SnarkFileConfig {
    zkey: "./semaphore/build/snark/semaphore_final.zkey".to_string(),
    wasm: "./semaphore/build/snark/semaphore.wasm".to_string(),
};

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

assert!(success);