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

Solidity verifier revisit #37

Merged
merged 14 commits into from
Dec 4, 2023
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ snark-verifier-sdk = { git = "https://github.com/axiom-crypto/snark-verifier.git
"loader_evm",
"halo2-pse",
] }
halo2_solidity_verifier = { git = "https://github.com/privacy-scaling-explorations/halo2-solidity-verifier", branch = "ac/initial-verifier-PR" }

# ethereum types
ethereum-consensus-types = { git = "https://github.com/ChainSafe/ethereum-consensus-types", branch = "capella" }
Expand All @@ -77,7 +76,7 @@ group = "0.12"
num-bigint = { version = "0.4", features = ["rand"] }
pasta_curves = "0.4.1"
ff = "0.12"
sha2 = { version = "0.10.6", features = ["compress"] }
sha2 = { version = "0.9", features = ["compress"] }
uint = "0.9.1"
ark-std = { version = "0.4.0", features = ["print-trace"] }

Expand All @@ -89,7 +88,7 @@ log = "0.4.14"
hex = "0.4"

[patch.crates-io]
halo2curves = { git = "https://github.com/timoftime/halo2curves", package = "halo2curves-axiom", rev = "f3bb3f5a7d3a8ca806368f185c112283a73a94cb" }
halo2curves = { git = "https://github.com/timoftime/halo2curves", package = "halo2curves-axiom", rev = "1bd39b8" }
ssz_rs = { git = "https://github.com/ralexstokes/ssz-rs", rev = "5f1ec833718efa07bbbff427ab28a1eeaa706164" }


Expand All @@ -115,7 +114,8 @@ zkevm-hashes = { git = "https://github.com/timoftime/halo2-lib", branch = "feat/


[patch."https://github.com/axiom-crypto/snark-verifier.git"]
snark-verifier = { git = "https://github.com/timoftime/snark-verifier", branch = "halo2-pse-fix", default-features = false }
snark-verifier-sdk = { git = "https://github.com/timoftime/snark-verifier", branch = "halo2-pse-fix", default-features = false }
snark-verifier = { git = "https://github.com/timoftime/snark-verifier", branch = "yul-codegen", default-features = false }
snark-verifier-sdk = { git = "https://github.com/timoftime/snark-verifier", branch = "yul-codegen", default-features = false }
# snark-verifier = { path = "../snark-verifier/snark-verifier" }
# snark-verifier-sdk = { path = "../snark-verifier/snark-verifier-sdk" }

2 changes: 1 addition & 1 deletion contracts/rust-abi/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ abigen!(
StepVerifier,
"./out/sync_step.sol/Verifier.json";
CommitteeUpdateVerifier,
"./out/committee_update_aggregated.sol/Verifier.json";
"./out/committee_update_verifier.sol/Verifier.json";
StepMockVerifier,
"./out/SyncStepMockVerifier.sol/SyncStepMockVerifier.json";
CommitteeUpdateMockVerifier,
Expand Down
2 changes: 1 addition & 1 deletion contracts/script/DeploySpectre.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "forge-std/Script.sol";

import {Spectre} from "../src/Spectre.sol";
import {Verifier as SyncStepVerifier} from "../snark-verifiers/sync_step.sol";
import {Verifier as CommitteeUpdateVerifier} from "../snark-verifiers/committee_update_aggregated.sol";
import {Verifier as CommitteeUpdateVerifier} from "../snark-verifiers/committee_update_verifier.sol";

contract DeploySpectre is Script {

Expand Down
27 changes: 27 additions & 0 deletions contracts/script/DeploySpectreTestnet.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "forge-std/Script.sol";

import {Spectre} from "../src/Spectre.sol";
import {Verifier as SyncStepVerifier} from "../snark-verifiers/sync_step.sol";
import {Verifier as CommitteeUpdateVerifier} from "../snark-verifiers/committee_update_verifier.sol";

contract DeploySpectre is Script {

function run() external {
uint256 deployerPrivateKey = vm.envUint("DEPLOYER_PRIVATE_KEY");
uint256 initialSyncPeriod = vm.envUint("INITIAL_SYNC_PERIOD");
bytes32 initialCommitteePoseidon = vm.envBytes32("INITIAL_COMMITTEE_POSEIDON");
uint256 slotsPerPeriod = vm.envUint("SLOTS_PER_PERIOD");

vm.startBroadcast(deployerPrivateKey);

SyncStepVerifier stepVerifier = new SyncStepVerifier();
CommitteeUpdateVerifier updateVerifier = new CommitteeUpdateVerifier();

Spectre spectre = new Spectre(address(stepVerifier), address(updateVerifier), initialSyncPeriod, initialCommitteePoseidon, slotsPerPeriod);

vm.stopBroadcast();
}
}
6 changes: 6 additions & 0 deletions contracts/script/deploy_local.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh
cd $(git rev-parse --show-toplevel)
source .env
LOCAL_RPC_URL="http://localhost:8545"

forge script script/DeploySpectre.s.sol:DeploySpectre --private-key $ANVIL_PRIVATE_KEY --rpc-url $LOCAL_RPC_URL --broadcast -vvvv
6 changes: 6 additions & 0 deletions contracts/script/deploy_testnet.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh
cd $(git rev-parse --show-toplevel)
source .env
LOCAL_RPC_URL="http://localhost:8545"

forge script script/DeploySpectreTestnet.s.sol:DeploySpectre --private-key $ANVIL_PRIVATE_KEY --rpc-url $LOCAL_RPC_URL --broadcast -vvvv
23 changes: 0 additions & 23 deletions contracts/snark-verifiers/committee_update_aggregated.sol

This file was deleted.

23 changes: 23 additions & 0 deletions contracts/snark-verifiers/committee_update_verifier.sol

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions contracts/snark-verifiers/sync_step.sol

Large diffs are not rendered by default.

21 changes: 11 additions & 10 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ check:
lint: fmt
cargo clippy --all-targets --all-features --workspace

setup-step network:
cargo run -r -- circuit sync-step -p ./build/sync_step_$1.pkey -k 22 setup
setup-step network *k='22':
cargo run -r -- circuit sync-step -p ./build/sync_step_$1.pkey -k $2 setup

setup-committee-update network:
cargo run -r -- circuit committee-update -p ./build/committee_update_$1.pkey -k 18 \
--verifier-k 25 --verifier-pk-path ./build/committee_update_verifier_$1.pkey setup
setup-committee-update network *k='25':
cargo run -r -- circuit committee-update -p ./build/committee_update_$1.pkey -k 18 \
--verifier-k $2 --verifier-pk-path ./build/committee_update_verifier_$1.pkey setup

gen-step-evm-verifier:
cargo run -r -- circuit sync-step -c ./lightclient-circuits/config/sync_step.json -o evm-verifier ./contracts/snark-verifiers/sync_step.yul
gen-verifier-step network:
cargo run -r -- circuit sync-step -p ./build/sync_step_$1.pkey gen-verifier -o ./contracts/snark-verifiers/sync_step.sol

gen-rotation-evm-verifier:
cargo run -r -- circuit aggregation -c ./lightclient-circuits/config/aggregation.json --app-pk-path ./build/committee_update.pkey --app-config-path ./lightclient-circuits/config/committee_update.json -i ./rotation -o evm-verifier ./contracts/snark-verifiers/committee_update_aggregated.yul
gen-verifier-committee-update network:
cargo run -r -- circuit committee-update -p ./build/committee_update_$1.pkey --verifier-pk-path ./build/committee_update_verifier_$1.pkey \
gen-verifier -o ./contracts/snark-verifiers/committee_update_verifier.sol

build-contracts:
cd contracts && forge build
Expand All @@ -42,7 +43,7 @@ download-spec-tests: clean-spec-tests
#!/usr/bin/env bash
if [[ ! -d 'consensus-spec-tests' ]]; then
echo "Downloading test data."
scripts/download_consensus_specs.sh
test-utils/scripts/download_consensus_specs.sh
fi

# deletes all the downloaded spec tests
Expand Down
11 changes: 4 additions & 7 deletions lightclient-circuits/src/committee_update_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,10 @@ impl<S: Spec> AppCircuit for CommitteeUpdateCircuit<S, bn256::Fr> {
let fp_chip = FpChip::new(&range, LIMB_BITS, NUM_LIMBS);

let assigned_instances = Self::synthesize(&mut builder, &fp_chip, witness)?;
builder.set_instances(0, assigned_instances);

match stage {
CircuitBuilderStage::Prover => {
builder.set_instances(0, assigned_instances);
if let Some(pinning) = pinning {
builder.set_params(pinning.params);
builder.set_break_points(pinning.break_points);
Expand All @@ -245,7 +245,7 @@ mod tests {
use std::fs;

use crate::{
aggregation::AggregationConfigPinning, util::Halo2ConfigPinning,
aggregation_circuit::AggregationConfigPinning, util::Halo2ConfigPinning,
witness::CommitteeRotationArgs,
};

Expand Down Expand Up @@ -306,16 +306,13 @@ mod tests {
#[test]
fn test_committee_update_circuit() {
const K: u32 = 18;
let params = gen_srs(K);

let witness = load_circuit_args();

let pinning = Eth2ConfigPinning::from_path("./config/committee_update_18.json");
let circuit = CommitteeUpdateCircuit::<Testnet, Fr>::create_circuit(
CircuitBuilderStage::Mock,
Some(pinning),
None,
&witness,
params.k(),
K,
)
.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion lightclient-circuits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub mod gadget;
pub mod util;
pub mod witness;

pub mod aggregation;
pub mod aggregation_circuit;
pub mod committee_update_circuit;
pub mod sync_step_circuit;

Expand Down
16 changes: 7 additions & 9 deletions lightclient-circuits/src/sync_step_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use halo2_ecc::{
};
use halo2curves::{
bls12_381::{G1Affine, G2Affine},
group::{GroupEncoding, UncompressedEncoding},
group::UncompressedEncoding,
};
use itertools::Itertools;
use num_bigint::BigUint;
Expand Down Expand Up @@ -259,7 +259,7 @@ impl<S: Spec, F: Field> StepCircuit<S, F> {
let poseidon_commitment_le = poseidon_commitment.to_bytes_le();
input[88..].copy_from_slice(&poseidon_commitment_le);

let mut public_input_commitment = sha2::Sha256::digest(input).to_vec();
let mut public_input_commitment = sha2::Sha256::digest(&input).to_vec();
// Truncate to 253 bits
public_input_commitment[31] &= 0b00011111;
bn256::Fr::from_bytes_le(&public_input_commitment)
Expand Down Expand Up @@ -311,8 +311,8 @@ impl<S: Spec, F: Field> StepCircuit<S, F> {
g2_chip: &G2Chip<F>,
bytes_compressed: &[u8],
) -> EcPoint<F, Fp2Point<F>> {
let sig_affine =
G2Affine::from_bytes(&bytes_compressed.try_into().unwrap()).expect("correct signature");
let sig_affine = G2Affine::from_compressed_be(&bytes_compressed.try_into().unwrap())
.expect("correct signature");

g2_chip.load_private_unchecked(ctx, sig_affine.into_coordinates())
}
Expand Down Expand Up @@ -390,10 +390,10 @@ impl<S: Spec> AppCircuit for StepCircuit<S, bn256::Fr> {
let fp_chip = FpChip::new(&range, LIMB_BITS, NUM_LIMBS);

let assigned_instances = Self::synthesize(&mut builder, &fp_chip, args)?;
builder.set_instances(0, assigned_instances);

match stage {
CircuitBuilderStage::Prover => {
builder.set_instances(0, assigned_instances);
if let Some(pinning) = pinning {
builder.set_params(pinning.params);
builder.set_break_points(pinning.break_points);
Expand Down Expand Up @@ -436,14 +436,12 @@ mod tests {

#[test]
fn test_sync_circuit() {
const K: u32 = 21;
const K: u32 = 20;
let witness = load_circuit_args();

let pinning = Eth2ConfigPinning::from_path("./config/sync_step_21.json");

let circuit = StepCircuit::<Testnet, Fr>::create_circuit(
CircuitBuilderStage::Mock,
Some(pinning),
None,
&witness,
K,
)
Expand Down
4 changes: 2 additions & 2 deletions lightclient-circuits/src/witness.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Witnesses for all circuits.

mod sync;
pub use sync::*;
mod step;
pub use step::*;

mod rotation;
pub use rotation::*;
Expand Down
36 changes: 33 additions & 3 deletions lightclient-circuits/src/witness/rotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct CommitteeRotationArgs<S: Spec> {

pub sync_committee_branch: Vec<Vec<u8>>,

#[serde(skip)]
pub _spec: PhantomData<S>,
}

Expand All @@ -23,7 +24,7 @@ impl<S: Spec> Default for CommitteeRotationArgs<S> {
let sync_committee_branch = vec![vec![0; 32]; S::SYNC_COMMITTEE_PUBKEYS_DEPTH];

let hashed_pk = sha2::Sha256::digest(
dummy_x_bytes
&dummy_x_bytes
.iter()
.copied()
.pad_using(64, |_| 0)
Expand All @@ -39,7 +40,7 @@ impl<S: Spec> Default for CommitteeRotationArgs<S> {
chunks = chunks
.into_iter()
.tuples()
.map(|(left, right)| sha2::Sha256::digest([left, right].concat()).to_vec())
.map(|(left, right)| sha2::Sha256::digest(&[left, right].concat()).to_vec())
.collect();
}

Expand Down Expand Up @@ -70,7 +71,7 @@ pub(crate) fn mock_root(leaf: Vec<u8>, branch: &[Vec<u8>], mut gindex: usize) ->

for i in 0..branch.len() {
last_hash = Sha256::digest(
if gindex % 2 == 0 {
&if gindex % 2 == 0 {
[last_hash, branch[i].clone()]
} else {
[branch[i].clone(), last_hash]
Expand All @@ -83,3 +84,32 @@ pub(crate) fn mock_root(leaf: Vec<u8>, branch: &[Vec<u8>], mut gindex: usize) ->

last_hash
}

#[cfg(test)]
mod tests {
use super::*;
use crate::{committee_update_circuit::CommitteeUpdateCircuit, util::AppCircuit};
use eth_types::Testnet;
use halo2_base::{
gates::circuit::CircuitBuilderStage, halo2_proofs::dev::MockProver,
halo2_proofs::halo2curves::bn256::Fr,
};
use snark_verifier_sdk::CircuitExt;

#[test]
fn test_committee_update_default_witness() {
const K: u32 = 18;
let witness = CommitteeRotationArgs::<Testnet>::default();

let circuit = CommitteeUpdateCircuit::<Testnet, Fr>::create_circuit(
CircuitBuilderStage::Mock,
None,
&witness,
K,
)
.unwrap();

let prover = MockProver::<Fr>::run(K, &circuit, circuit.instances()).unwrap();
prover.assert_satisfied_par();
}
}
Loading
Loading