semaphore-rs/README.md

51 lines
1.7 KiB
Markdown
Raw Permalink Normal View History

2022-03-12 00:11:06 +00:00
# 🦀 semaphore-rs
2022-01-28 17:17:18 +00:00
2022-03-06 18:12:39 +00:00
Rust support library for using [semaphore](https://github.com/appliedzkp/semaphore). It's mostly a Rust rewrite of [zk-kit](https://github.com/appliedzkp/zk-kit), but just focuses on semaphore (for now) and still covers a much smaller scope. It's using [ark-circom](https://github.com/gakonst/ark-circom) under the hood for generating the groth16 proofs.
2022-03-06 18:23:42 +00:00
## Usage
Add this line to your `cargo.toml`:
2022-03-11 19:12:44 +00:00
```toml
2022-03-06 18:23:42 +00:00
semaphore = { git = "https://github.com/worldcoin/semaphore-rs" }
```
2022-03-06 18:12:39 +00:00
## Building semaphore circuits
1. Check out submodule (if not done before already): `git submodule update --init --recursive`
1. Install semaphore dependencies `cd semaphore && npm install`
2022-03-11 19:12:44 +00:00
1. Compile circuits `npm exec ts-node ./scripts/compile-circuits.ts`
2022-03-06 18:12:39 +00:00
1. You'll find the `zkey` and `wasm` file in `semaphore/build/snark`
2022-02-09 02:13:06 +00:00
2022-02-26 17:55:04 +00:00
## Example
2022-02-09 02:13:06 +00:00
2022-03-06 18:12:39 +00:00
Example as in `src/lib.rs`, run with `cargo test`.
2022-02-26 17:55:04 +00:00
```rust
use semaphore::{hash_to_field, Field, identity::Identity, poseidon_tree::PoseidonTree,
2022-03-10 23:32:59 +00:00
protocol::* };
use num_bigint::BigInt;
2022-02-26 17:55:04 +00:00
// generate identity
let id = Identity::from_seed(b"secret");
2022-02-09 02:13:06 +00:00
2022-02-26 17:55:04 +00:00
// generate merkle tree
let leaf = Field::from(0);
let mut tree = PoseidonTree::new(21, leaf);
tree.set(0, id.commitment());
2022-02-09 02:13:06 +00:00
2022-02-26 17:55:04 +00:00
let merkle_proof = tree.proof(0).expect("proof should exist");
2022-03-09 14:53:03 +00:00
let root = tree.root();
2022-02-26 17:55:04 +00:00
// change signal and external_nullifier here
2022-03-17 18:07:06 +00:00
let signal_hash = hash_to_field(b"xxx");
let external_nullifier_hash = hash_to_field(b"appId");
2022-02-26 17:55:04 +00:00
2022-03-12 00:13:18 +00:00
let nullifier_hash = generate_nullifier_hash(&id, external_nullifier_hash);
2022-02-26 17:55:04 +00:00
2022-03-17 18:07:06 +00:00
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();
2022-03-06 18:12:39 +00:00
assert!(success);
2022-03-11 00:06:26 +00:00
```