Skip to content

Commit

Permalink
Fix RPC handlers (#34)
Browse files Browse the repository at this point in the history
* WIP

* update version in circuits crate

* fix step circuit

* fix sha wide chip gate

* fix prover

* fix proofgen

* fix instance gen & refactor

* fix prover rpc

* fix some tests

* switch to remote deps

* rename circuit files

* refactor

* rename circuit files back (to merge main)

* post merge fixes

* fixes

* fmt

* wip

* remove redundant

* fix endianness

* remove redundant tests

* cargo fix + fmt

* rename config files

* fix

* cargo fmt

* switch to https://github.com/ChainSafe/ethereum-consensus-types (#35)

* CLI wip

* add requirements to readme

* simplify cli

* fix CI tests

* fix paths

* fmt + fix
  • Loading branch information
nulltea authored Nov 30, 2023
1 parent 222f0b5 commit 915a593
Show file tree
Hide file tree
Showing 22 changed files with 557 additions and 673 deletions.
23 changes: 14 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
[workspace]
members = ["lightclient-circuits", "prover", "preprocessor", "eth-types", "contract-tests", "test-utils", "contracts"]
members = [
"lightclient-circuits",
"prover",
"preprocessor",
"eth-types",
"contract-tests",
"test-utils",
"contracts",
]

resolver = "2"

Expand Down Expand Up @@ -36,7 +44,7 @@ preprocessor = { path = "preprocessor" }
halo2-base = { git = "https://github.com/axiom-crypto/halo2-lib", branch = "community-edition", default-features = false, features = [
"halo2-pse",
"display",
"jemallocator"
"jemallocator",
] }
halo2-ecc = { git = "https://github.com/axiom-crypto/halo2-lib", branch = "community-edition", default-features = false }
zkevm-hashes = { git = "https://github.com/axiom-crypto/halo2-lib", branch = "community-edition", default-features = false }
Expand All @@ -60,10 +68,7 @@ snark-verifier-sdk = { git = "https://github.com/axiom-crypto/snark-verifier.git
halo2_solidity_verifier = { git = "https://github.com/privacy-scaling-explorations/halo2-solidity-verifier", branch = "ac/initial-verifier-PR" }

# ethereum types
ethereum-consensus-types = { git = "ssh://[email protected]/sygmaprotocol/Zipline.git", rev = "5ecf28d24690862814a7ebc5385c7e5c2eeb6e3d" }
zipline-test-utils = { package = "test-utils", git = "ssh://[email protected]/sygmaprotocol/Zipline.git", rev = "5ecf28d24690862814a7ebc5385c7e5c2eeb6e3d" }
zipline-cryptography = { package = "cryptography", git = "ssh://[email protected]/sygmaprotocol/Zipline.git", rev = "5ecf28d24690862814a7ebc5385c7e5c2eeb6e3d" }
light-client-verifier = { git = "ssh://[email protected]/sygmaprotocol/Zipline.git", rev = "5ecf28d24690862814a7ebc5385c7e5c2eeb6e3d" }
ethereum-consensus-types = { git = "https://github.com/ChainSafe/ethereum-consensus-types", branch = "capella" }
beacon-api-client = { git = "https://github.com/ralexstokes/ethereum-consensus.git", rev = "f3bff52e9c43866f231ec40c8ab0e34125a8957f" }
ssz_rs = "0.9"

Expand All @@ -84,19 +89,19 @@ 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 = "f3bb3f5a7d3a8ca806368f185c112283a73a94cb" }
ssz_rs = { git = "https://github.com/ralexstokes/ssz-rs", rev = "5f1ec833718efa07bbbff427ab28a1eeaa706164" }


[patch."https://github.com/axiom-crypto/halo2-lib"]
halo2-base = { git = "https://github.com/timoftime/halo2-lib", branch = "feat/zkevm-sha256-builder", default-features = false, features = [
"halo2-pse",
"display",
"jemallocator"
"jemallocator",
] }
halo2-ecc = { git = "https://github.com/timoftime/halo2-lib", branch = "feat/zkevm-sha256-builder", default-features = false, features = [
"halo2-pse",
"jemallocator"
"jemallocator",
] }
zkevm-hashes = { git = "https://github.com/timoftime/halo2-lib", branch = "feat/zkevm-sha256-builder", default-features = false }
# halo2-base = { path = "../halo2-lib/halo2-base", default-features = false, features = [
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Spectre
Spectre is a ZK-based block header oracle protocol based on Altair fork light-client sync protocol.

## Requirements
- `build-essential clang pkg-config libssl-dev`

## Deploying contracts

Just scripts are provided to deploy the contracts either to a local testnet, or public networks.
Expand Down
14 changes: 6 additions & 8 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ check:
lint: fmt
cargo clippy --all-targets --all-features --workspace

setup-step-circuit:
cargo run -r -- circuit sync-step -c ./lightclient-circuits/config/sync_step.json -o artifacts -k 22

setup-rotation-circuit:
cargo run -r -- circuit committee-update -c ./lightclient-circuits/config/committee_update.json -o artifacts -k 18
# TODO: generate committee-update snark
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 artifacts -k 22
setup-step network:
cargo run -r -- circuit sync-step -p ./build/sync_step_$1.pkey -k 22 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

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
Expand Down
31 changes: 31 additions & 0 deletions lightclient-circuits/config/committee_update_testnet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"params": {
"k": 18,
"num_advice_per_phase": [
12
],
"num_fixed": 1,
"num_lookup_advice_per_phase": [
1,
0,
0
],
"lookup_bits": 8,
"num_instance_columns": 1
},
"break_points": [
[
262134,
262132,
262134,
262132,
262133,
262132,
262132,
262133,
262132,
262134,
262133
]
]
}
12 changes: 12 additions & 0 deletions lightclient-circuits/config/committee_update_verifier_testnet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"params": {
"degree": 25,
"num_advice": 1,
"num_lookup_advice": 1,
"num_fixed": 1,
"lookup_bits": 8
},
"break_points": [
[]
]
}
40 changes: 40 additions & 0 deletions lightclient-circuits/config/sync_step_20.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"params": {
"k": 20,
"num_advice_per_phase": [
21
],
"num_fixed": 1,
"num_lookup_advice_per_phase": [
3,
0,
0
],
"lookup_bits": 8,
"num_instance_columns": 1
},
"break_points": [
[
1048565,
1048564,
1048566,
1048565,
1048565,
1048566,
1048566,
1048566,
1048564,
1048565,
1048566,
1048564,
1048566,
1048566,
1048564,
1048564,
1048566,
1048564,
1048566,
1048566
]
]
}
25 changes: 25 additions & 0 deletions lightclient-circuits/config/sync_step_testnet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"params": {
"k": 22,
"num_advice_per_phase": [
6
],
"num_fixed": 1,
"num_lookup_advice_per_phase": [
1,
0,
0
],
"lookup_bits": 8,
"num_instance_columns": 1
},
"break_points": [
[
4194292,
4194294,
4194292,
4194293,
4194293
]
]
}
3 changes: 1 addition & 2 deletions lightclient-circuits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#![feature(generic_arg_infer)]
#![feature(return_position_impl_trait_in_trait)]
#![allow(clippy::needless_range_loop)]

pub mod gadget;
pub mod util;
pub mod witness;
Expand All @@ -21,8 +20,8 @@ mod ssz_merkle;

pub use halo2_base;
pub use halo2_base::halo2_proofs;
use halo2_base::halo2_proofs::halo2curves::bn256;

use halo2_base::halo2_proofs::halo2curves::bn256;
#[allow(type_alias_bounds)]
pub type Eth2CircuitBuilder<GateManager: util::CommonGateManager<bn256::Fr>> =
gadget::crypto::ShaCircuitBuilder<bn256::Fr, GateManager>;
58 changes: 55 additions & 3 deletions lightclient-circuits/src/witness/rotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use eth_types::Spec;
use ethereum_consensus_types::BeaconBlockHeader;
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::{iter, marker::PhantomData};

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand All @@ -17,17 +18,68 @@ pub struct CommitteeRotationArgs<S: Spec> {

impl<S: Spec> Default for CommitteeRotationArgs<S> {
fn default() -> Self {
let dummy_x_bytes = iter::once(192).pad_using(48, |_| 0).rev().collect();
let dummy_x_bytes = iter::once(192).pad_using(48, |_| 0).rev().collect_vec();

let sync_committee_branch = vec![vec![0; 32]; S::SYNC_COMMITTEE_DEPTH + 1];
let sync_committee_branch = vec![vec![0; 32]; S::SYNC_COMMITTEE_PUBKEYS_DEPTH];

let hashed_pk = sha2::Sha256::digest(
dummy_x_bytes
.iter()
.copied()
.pad_using(64, |_| 0)
.collect_vec(),
)
.to_vec();

assert!(S::SYNC_COMMITTEE_SIZE.is_power_of_two());

let mut chunks = vec![hashed_pk; S::SYNC_COMMITTEE_SIZE];

while chunks.len() > 1 {
chunks = chunks
.into_iter()
.tuples()
.map(|(left, right)| sha2::Sha256::digest([left, right].concat()).to_vec())
.collect();
}

let committee_root = chunks.pop().unwrap();

let state_root = mock_root(
committee_root,
&sync_committee_branch,
S::SYNC_COMMITTEE_PUBKEYS_ROOT_INDEX,
);

Self {
pubkeys_compressed: iter::repeat(dummy_x_bytes)
.take(S::SYNC_COMMITTEE_SIZE)
.collect_vec(),
sync_committee_branch,
finalized_header: Default::default(),
finalized_header: BeaconBlockHeader {
state_root: state_root.as_slice().try_into().unwrap(),
..Default::default()
},
_spec: PhantomData,
}
}
}

pub(crate) fn mock_root(leaf: Vec<u8>, branch: &[Vec<u8>], mut gindex: usize) -> Vec<u8> {
let mut last_hash = leaf;

for i in 0..branch.len() {
last_hash = Sha256::digest(
if gindex % 2 == 0 {
[last_hash, branch[i].clone()]
} else {
[branch[i].clone(), last_hash]
}
.concat(),
)
.to_vec();
gindex /= 2;
}

last_hash
}
21 changes: 7 additions & 14 deletions lightclient-circuits/src/witness/sync.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use eth_types::Spec;
use ethereum_consensus_types;
use ethereum_consensus_types::BeaconBlockHeader;
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use ssz_rs::Node;
use std::iter;
use std::marker::PhantomData;

use super::mock_root;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SyncStepArgs<S: Spec> {
pub signature_compressed: Vec<u8>,
Expand Down Expand Up @@ -41,20 +41,13 @@ impl<S: Spec> Default for SyncStepArgs<S> {
.take(S::FINALIZED_HEADER_DEPTH)
.collect_vec();

let compute_root = |leaf: Vec<u8>, branch: &[Vec<u8>]| -> Vec<u8> {
let mut last_hash = Sha256::digest([leaf, branch[0].clone()].concat()).to_vec();

for i in 1..branch.len() {
last_hash = Sha256::digest([last_hash, branch[i].clone()].concat()).to_vec();
}

last_hash
};

let execution_state_root = vec![0; 32];
let execution_merkle_branch = vec![vec![0; 32]; S::EXECUTION_STATE_ROOT_DEPTH];
let beacon_block_body_root =
compute_root(execution_state_root.clone(), &state_merkle_branch);
let beacon_block_body_root = mock_root(
execution_state_root.clone(),
&state_merkle_branch,
S::EXECUTION_STATE_ROOT_INDEX,
);

let finalized_block = BeaconBlockHeader {
body_root: Node::try_from(beacon_block_body_root.as_slice()).unwrap(),
Expand Down
11 changes: 1 addition & 10 deletions lightclient-circuits/tests/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,7 @@ fn test_eth2_spec_mock_1(
#[exclude("deneb*")]
path: PathBuf,
) {
run_test_eth2_spec_mock::<16, 20>(path)
}

#[rstest]
fn test_eth2_spec_mock_3(
#[files("../consensus-spec-tests/tests/minimal/capella/light_client/sync/pyspec_tests/**")]
#[exclude("deneb*")]
path: PathBuf,
) {
run_test_eth2_spec_mock::<17, 20>(path)
run_test_eth2_spec_mock::<18, 20>(path)
}

fn run_test_eth2_spec_mock<const K_ROTATION: u32, const K_SYNC: u32>(path: PathBuf) {
Expand Down
3 changes: 1 addition & 2 deletions preprocessor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ beacon-api-client.workspace = true

# misc
eyre = "0.6"
tokio = { version = "1", features = ["full"]}
tokio = { version = "1", features = ["full"] }

hex.workspace = true
log.workspace = true
itertools.workspace = true
serde_json.workspace = true
serde.workspace = true
ethereum-consensus-types.workspace = true
zipline-cryptography = { workspace = true, features = ["serde"] }
# local
eth-types.workspace = true
lightclient-circuits.workspace = true
Expand Down
5 changes: 2 additions & 3 deletions preprocessor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ use ssz_rs::{Node, Vector};
use std::ops::Deref;
pub use sync::*;
mod rotation;
use ethereum_consensus_types::bls::BlsPublicKey;
use ethereum_consensus_types::bls::BlsSignature;
pub use rotation::*;
use zipline_cryptography::bls::BlsPublicKey;
use zipline_cryptography::bls::BlsSignature;

pub async fn light_client_update_to_args<S: Spec>(
update: &mut LightClientUpdateCapella<
{ S::SYNC_COMMITTEE_SIZE },
Expand Down
Loading

0 comments on commit 915a593

Please sign in to comment.