Skip to content

Commit

Permalink
Update structs
Browse files Browse the repository at this point in the history
  • Loading branch information
ytham committed Dec 15, 2023
1 parent 3972ed6 commit e3492b3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
42 changes: 31 additions & 11 deletions halo2-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::cell::RefCell;
use std::io::BufReader;
use std::rc::Rc;

use ethers_core::types::H256;
use halo2_base::{
gates::circuit::{builder::BaseCircuitBuilder, BaseCircuitParams},
AssignedValue,
Expand Down Expand Up @@ -48,6 +49,8 @@ pub mod tests;
use vkey::{write_onchain_vkey, OnchainVerifyingKey};
use metadata::AxiomV2CircuitMetadata;

use crate::metadata::decode_axiom_v2_circuit_metadata;

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
Expand Down Expand Up @@ -83,20 +86,22 @@ pub struct CircuitConfig {
num_virtual_instance: usize,
}

#[derive(Tsify, Serialize, Deserialize)]
#[derive(Tsify, Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct CircuitLimits {
max_user_subqueries: usize,
max_user_outputs: usize,
user_max_outputs: usize,
user_max_subqueries: usize,
}

#[derive(Tsify, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct CircuitComposition {
circuit_config: CircuitConfig,
circuit_limits: CircuitLimits,
impl Default for CircuitLimits {
fn default() -> Self {
CircuitLimits {
// Note: ideally should grab from axiom-tools
user_max_outputs: 128,
user_max_subqueries: 128,
}
}
}

#[wasm_bindgen]
Expand All @@ -107,6 +112,8 @@ pub struct Halo2Wasm {
pub public: Vec<Vec<AssignedValue<Fr>>>,
#[wasm_bindgen(skip)]
pub circuit_params: Option<BaseCircuitParams>,
#[wasm_bindgen(skip)]
pub circuit_limits: Option<CircuitLimits>,
params: Option<ParamsKZG<Bn256>>,
pk: Option<ProvingKey<G1Affine>>,
vk: Option<VerifyingKey<G1Affine>>,
Expand All @@ -119,6 +126,7 @@ impl Default for Halo2Wasm {
circuit: Rc::new(RefCell::new(circuit)),
public: vec![],
circuit_params: None,
circuit_limits: Some(CircuitLimits::default()),
params: None,
pk: None,
vk: None,
Expand Down Expand Up @@ -249,10 +257,19 @@ impl Halo2Wasm {

#[wasm_bindgen(js_name = getEncodedCircuitMetadata)]
pub fn get_encoded_circuit_metadata(&mut self) -> Vec<u8> {
let circuit_metadata = AxiomV2CircuitMetadata::from_circuit_params(&self.circuit_params.clone().unwrap());
let circuit_metadata = AxiomV2CircuitMetadata::from_circuit_params(
&self.circuit_params.clone().unwrap(),
&self.circuit_limits.clone().unwrap()
);
circuit_metadata.encode().unwrap().0.to_vec()
}

#[wasm_bindgen(js_name = decodeCircuitMetadata)]
pub fn decode_circuit_metadata(bytes: &[u8]) -> AxiomV2CircuitMetadata {
let encoded = H256::from_slice(bytes);
decode_axiom_v2_circuit_metadata(encoded).unwrap()
}

#[wasm_bindgen(js_name = getVk)]
pub fn get_vk(&self) -> Vec<u8> {
let file = self
Expand All @@ -266,7 +283,10 @@ impl Halo2Wasm {
#[wasm_bindgen(js_name = getOnchainVk)]
pub fn get_onchain_vk(&self) -> Vec<u8> {
let vk = self.vk.as_ref().unwrap();
let circuit_metadata = AxiomV2CircuitMetadata::from_circuit_params(&self.circuit_params.clone().unwrap());
let circuit_metadata = AxiomV2CircuitMetadata::from_circuit_params(
&self.circuit_params.clone().unwrap(),
&self.circuit_limits.clone().unwrap()
);
let preprocessed = vk
.fixed_commitments()
.iter()
Expand Down
11 changes: 8 additions & 3 deletions halo2-wasm/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use ethers_core::types::H256;
use halo2_base::gates::circuit::BaseCircuitParams;
use serde::{Deserialize, Serialize};
use tsify::Tsify;

use crate::CircuitLimits;

/// All configuration parameters of an Axiom Client Circuit that are
/// hard-coded into the Verify Compute Circuit (which is an Aggregation Circuit with Universality::Full).
///
/// This metadata is only for a circuit built using `RlcCircuitBuilder`
/// or `BaseCircuitBuilder`, where the circuit _may_ be an aggregation circuit.
#[derive(Clone, Debug, Default, Serialize, Deserialize, Hash)]
#[derive(Tsify, Clone, Debug, Default, Serialize, Deserialize, Hash)]
#[serde(rename_all = "camelCase")]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct AxiomV2CircuitMetadata {
/// Version byte for domain separation on version of Axiom client, halo2-lib, snark-verifier (for example if we switch to mv_lookup)
/// If `version = x`, this should be thought of as Axiom Query v2.x
Expand Down Expand Up @@ -104,7 +108,8 @@ impl AxiomV2CircuitMetadata {
}

pub fn from_circuit_params(
circuit_params: &BaseCircuitParams
circuit_params: &BaseCircuitParams,
circuit_limits: &CircuitLimits
) -> Self {
Self {
version: 0,
Expand All @@ -114,7 +119,7 @@ impl AxiomV2CircuitMetadata {
num_advice_per_phase: circuit_params.num_advice_per_phase.clone().iter().map(|x| *x as u16).collect(),
num_lookup_advice_per_phase: circuit_params.num_lookup_advice_per_phase.clone().iter().map(|x| *x as u8).collect(),
num_fixed: circuit_params.num_fixed.clone() as u8,
max_outputs: 1,
max_outputs: circuit_limits.user_max_outputs as u16,
..Default::default()
}
}
Expand Down

0 comments on commit e3492b3

Please sign in to comment.