Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Axiom V2 Halo2 implementation #83

Open
wants to merge 40 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
144e258
feat: halo2 circuit init
shreyas-londhe Jan 2, 2024
ac1ee74
feat: added plume input struct
shreyas-londhe Jan 4, 2024
b4554ba
feat: added circuit steps
shreyas-londhe Jan 4, 2024
8c65683
feat: impl
shreyas-londhe Jan 7, 2024
f77e371
fix: refactoring
shreyas-londhe Jan 11, 2024
7d4b2d2
test: working without hash-to-curve
shreyas-londhe Jan 11, 2024
270c2d0
fix: message type
shreyas-londhe Feb 16, 2024
36f8599
test: added test inputs
shreyas-londhe Feb 17, 2024
1e6d7b5
feat: plume verify impl complete
shreyas-londhe Feb 19, 2024
4cd4541
test(wip): fixing tests
shreyas-londhe Feb 19, 2024
a020b8c
test: added bench test
shreyas-londhe Feb 20, 2024
a1c7ec0
test: plume_verify test completed 🚀
shreyas-londhe Feb 20, 2024
2bdfc4f
chore: cargo clippy
shreyas-londhe Feb 22, 2024
3f63a91
fix: minor changes
shreyas-londhe Feb 22, 2024
b6c854d
test: custom messages in test
shreyas-londhe Feb 23, 2024
4ea66b8
chore: minor changes
shreyas-londhe Feb 25, 2024
3488880
chore: lib name change
shreyas-londhe Feb 26, 2024
f6ec9b6
chore: minor change
shreyas-londhe Feb 26, 2024
f39e164
chore: updated halo2-lib features
shreyas-londhe Mar 5, 2024
2e8c8c9
chore: updated halo2-lib features
shreyas-londhe Mar 5, 2024
cf6f31e
feat: wasm support for plume verification
shreyas-londhe Apr 18, 2024
706e9d0
test: config calculation test
shreyas-londhe Apr 20, 2024
a1e274c
fix: replaced by inner product in bytes_to_limbs
shreyas-londhe Apr 26, 2024
5a486eb
feat: replaced sha256 w/ poseidon
shreyas-londhe May 2, 2024
5139462
fix: fixed Fp and Fq in tests
shreyas-londhe May 9, 2024
4422596
fix: plume_verify bench
shreyas-londhe May 17, 2024
56e9c80
feat: plume-wasm npm package
shreyas-londhe May 17, 2024
dddd2f0
feat: frontend example for wasm proving
shreyas-londhe May 17, 2024
bd58ec7
feat: public input support for plume_verify
shreyas-londhe May 17, 2024
afe58ea
chore: code cleanup
shreyas-londhe May 20, 2024
6099a0d
chore: code cleanup
shreyas-londhe May 21, 2024
d0321cb
feat: e2e test with poseidon hash2curve
shreyas-londhe May 22, 2024
a42a35a
fix: updated poseidon DST
shreyas-londhe May 24, 2024
c562fd2
fix: message type
shreyas-londhe May 29, 2024
41c5b8f
feat: plume-wasm package
shreyas-londhe Jun 3, 2024
a72d689
feat: plume verify in browser working e2e 🚀
shreyas-londhe Jun 3, 2024
fbdb142
feat: merkle verify support for plume-wasm
shreyas-londhe Jun 3, 2024
43754c1
feat: frontend example complete
shreyas-londhe Jun 3, 2024
a54bc2f
docs: added readme for halo2 circuits and wasm
shreyas-londhe Jun 5, 2024
0b86326
docs: added readme for example-frontend
shreyas-londhe Jun 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[workspace]
resolver = "2"

members = ["rust-arkworks", "rust-k256"]
members = [ "circuits/halo2","rust-arkworks", "rust-k256", "circuits/halo2/wasm", "js-wasm" ]

[patch.crates-io]
ark-ec = { git = "https://github.com/FindoraNetwork/ark-algebra" }
Expand Down
Binary file added circuits/.DS_Store
Binary file not shown.
Binary file added circuits/halo2/.DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions circuits/halo2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/params
/build
34 changes: 34 additions & 0 deletions circuits/halo2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[package]
name = "halo2-plume"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
halo2-base = { git = "https://github.com/shreyas-londhe/halo2-lib.git", branch = "feat/secp256k1-hash2curve-poseidon", default-features = false, features = ["halo2-axiom", "test-utils"] }
halo2-ecc = { git = "https://github.com/shreyas-londhe/halo2-lib.git", branch = "feat/secp256k1-hash2curve-poseidon", default-features = false, features = ["halo2-axiom"] }
snark-verifier-sdk = { git = "https://github.com/shreyas-londhe/snark-verifier.git", optional = true}
k256 = { version = "0.13.3", features = ["arithmetic", "hash2curve", "expose-field", "sha2"]}
rand = "0.8.5"
pse-poseidon = { git = "https://github.com/shreyas-londhe/pse-poseidon.git" }

[features]
scripts = ["snark-verifier-sdk"]

[dev-dependencies]
ark-std = "0.4.0"
num-bigint = "0.4.4"
num-traits = "0.2.18"
criterion = "0.5.1"
pprof = { version = "0.13", features = ["criterion", "flamegraph"] }

[[bin]]
name = "generate-build-files"
path = "./scripts/generate_build_files.rs"
required-features = ["scripts"]

[[bench]]
name = "plume_verify"
harness = false

25 changes: 25 additions & 0 deletions circuits/halo2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# halo2-plume

Plume signature verification circuits in halo2.

## Build

You can install and build our library with the following commands.

```bash
git clone https://github.com/shreyas-londhe/zk-nullifier-sig.git -b feat/plume-halo2
cd zk-nullifier-sig/circuits/halo2
cargo build
```

## Test

You can run the tests by executing `cargo test --release`.

## Usage

You can refer to the test at `src/lib.rs` for an example of how to use the Plume verification circuit in your halo2 circuit.

## WASM Prover in Browser

You can generate a proof on browser with our wasm prover. For more information, please see `wasm/README.md`.
123 changes: 123 additions & 0 deletions circuits/halo2/benches/plume_verify.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
use ark_std::{ end_timer, start_timer };
use halo2_base::gates::circuit::BaseCircuitParams;
use halo2_base::gates::circuit::{ builder::RangeCircuitBuilder, CircuitBuilderStage };
use halo2_base::gates::flex_gate::MultiPhaseThreadBreakPoints;
use halo2_base::gates::RangeInstructions;
use halo2_base::poseidon::hasher::{ PoseidonHasher, spec::OptimizedPoseidonSpec };
use halo2_base::{
halo2_proofs::{ halo2curves::bn256::{ Bn256, Fr }, plonk::*, poly::kzg::commitment::ParamsKZG },
utils::testing::gen_proof,
};
use halo2_ecc::ecc::EccChip;
use halo2_ecc::fields::FieldChip;
use halo2_ecc::secp256k1::{ FpChip, FqChip };
use plume_halo2::utils::generate_test_data;
use plume_halo2::{ verify_plume, PlumeCircuitInput, PlumeInput };
use rand::rngs::OsRng;

use criterion::{ criterion_group, criterion_main };
use criterion::{ BenchmarkId, Criterion };

use pprof::criterion::{ Output, PProfProfiler };

const K: u32 = 15;

fn plume_verify_bench(
stage: CircuitBuilderStage,
input: PlumeCircuitInput,
config_params: Option<BaseCircuitParams>,
break_points: Option<MultiPhaseThreadBreakPoints>
) -> RangeCircuitBuilder<Fr> {
let k = K as usize;
let lookup_bits = k - 1;
let mut builder = match stage {
CircuitBuilderStage::Prover => {
RangeCircuitBuilder::prover(config_params.unwrap(), break_points.unwrap())
}
_ => RangeCircuitBuilder::from_stage(stage).use_k(k).use_lookup_bits(lookup_bits),
};

let start0 = start_timer!(|| format!("Witness generation for circuit in {stage:?} stage"));
let range = builder.range_chip();
let ctx = builder.main(0);

let fp_chip = FpChip::<Fr>::new(&range, 88, 3);
let fq_chip = FqChip::<Fr>::new(&range, 88, 3);
let ecc_chip = EccChip::<Fr, FpChip<Fr>>::new(&fp_chip);

let mut poseidon_hasher = PoseidonHasher::<Fr, 3, 2>::new(
OptimizedPoseidonSpec::new::<8, 57, 0>()
);
poseidon_hasher.initialize_consts(ctx, range.gate());

let nullifier = ecc_chip.load_private_unchecked(ctx, (input.nullifier.0, input.nullifier.1));
let s = fq_chip.load_private(ctx, input.s);
let c = fq_chip.load_private(ctx, input.c);
let pk = ecc_chip.load_private_unchecked(ctx, (input.pk.0, input.pk.1));
let m = input.m
.iter()
.map(|m| ctx.load_witness(*m))
.collect::<Vec<_>>();

let plume_input = PlumeInput {
nullifier,
s,
c,
pk,
m,
};

verify_plume::<Fr>(ctx, &ecc_chip, &poseidon_hasher, 4, 4, plume_input);

end_timer!(start0);
if !stage.witness_gen_only() {
builder.calculate_params(Some(20));
}
builder
}

fn bench(c: &mut Criterion) {
let plume_verify_input = generate_test_data(
b"vulputate ut pharetra tis amet aliquam id diam maecenas ultricies mi eget mauris pharetra et adasdds"
);
let circuit = plume_verify_bench(
CircuitBuilderStage::Keygen,
plume_verify_input.clone(),
None,
None
);
let config_params = circuit.params();

let params = ParamsKZG::<Bn256>::setup(K, OsRng);
let vk = keygen_vk(&params, &circuit).expect("vk should not fail");
let pk = keygen_pk(&params, vk, &circuit).expect("pk should not fail");
let break_points = circuit.break_points();

let mut group = c.benchmark_group("plonk-prover");
group.sample_size(10);
group.bench_with_input(
BenchmarkId::new("plume verify", K),
&(&params, &pk, &plume_verify_input),
|bencher, &(params, pk, voter_input)| {
let input = voter_input.clone();
bencher.iter(|| {
let circuit = plume_verify_bench(
CircuitBuilderStage::Prover,
input.clone(),
Some(config_params.clone()),
Some(break_points.clone())
);

gen_proof(params, pk, circuit);
})
}
);
group.finish()
}

criterion_group! {
name = benches;
config = Criterion::default().with_profiler(PProfProfiler::new(10, Output::Flamegraph(None)));
targets = bench
}
criterion_main!(benches);
3 changes: 3 additions & 0 deletions circuits/halo2/example-frontend/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
36 changes: 36 additions & 0 deletions circuits/halo2/example-frontend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js
.yarn/install-state.gz

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
28 changes: 28 additions & 0 deletions circuits/halo2/example-frontend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Plume Verification Frontend

Nextjs frontend example showcasing the use of [plume-wasm](https://www.npmjs.com/package/plume-wasm) to generate a proof on browser.

## Prerequisites

You need a custom build of [Taho wallet](https://taho.xyz) to generate the nullifier.

- Download this [zip](https://storage.googleapis.com/plume-keys/taho-plume.zip) and extract it.
- Follow [this](https://knowledge.workspace.google.com/kb/load-unpacked-extensions-000005962) guide to load the extension.
- Setup the wallet with a dummy account.

Once you have the wallet setup, you can follow the instructions below to test.

## Try it out

To test the browser proof generation, you can follow the instructions below.

```bash
git clone https://github.com/shreyas-londhe/zk-nullifier-sig.git -b feat/plume-halo2
cd zk-nullifier-sig/circuits/halo2/example-frontend
npm install
npm run dev
```

This will start the frontend at [http://localhost:3000](http://localhost:3000).

Note: The console will display the time taken to generate the proof.
39 changes: 39 additions & 0 deletions circuits/halo2/example-frontend/next.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,

// Add the headers function to set the required headers
async headers() {
return [
{
source: "/(.*)",
headers: [
{
key: "Cross-Origin-Embedder-Policy",
value: "require-corp",
},
{
key: "Cross-Origin-Opener-Policy",
value: "same-origin",
},
],
},
];
},

webpack: (config, { isServer }) => {
if (!isServer) {
config.module.rules.push({
test: /\.worker\.ts$/,
loader: "worker-loader",
options: {
publicPath: "/_next/static/workers/",
},
});
}

return config;
},
};

export default nextConfig;
Loading