Skip to content

Commit

Permalink
0.1.0 (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan-000 authored Jun 9, 2023
1 parent 9890df4 commit 1b5d191
Show file tree
Hide file tree
Showing 14 changed files with 179 additions and 377 deletions.
176 changes: 53 additions & 123 deletions Cargo.lock

Large diffs are not rendered by default.

37 changes: 29 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ acvm = "0.14.1"
bincode = "1.3.3"
bytesize = "1.2"
rand = "0.8"
reqwest = { version = "0.11.16", default-features = false, features = ["stream", "rustls-tls"] }
reqwest = { version = "0.11.16", default-features = false, features = [
"stream",
"rustls-tls",
] }
serde = { version = "1.0.136", features = ["derive"] }
tokio = "1.0"
futures-util = "0.3.14"
Expand All @@ -22,11 +25,22 @@ zcash_halo2_proofs = { package = "halo2_proofs", version = "0.3.0", optional = t

# pse
# pse_halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2.git", package = "halo2_proofs", tag = "v2023_04_20", optional = true }
pse_halo2wrong = { git = "https://github.com/privacy-scaling-explorations/halo2wrong", package = "halo2wrong", tag = "v2023_04_20", features = ["circuit-params"], optional = true }
pse_maingate = { git = "https://github.com/privacy-scaling-explorations/halo2wrong", package = "maingate", tag = "v2023_04_20", features = ["circuit-params"], optional = true }
pse_ecdsa = { git = "https://github.com/privacy-scaling-explorations/halo2wrong", package = "ecdsa", tag = "v2023_04_20", features = ["circuit-params"], optional = true }
pse_ecc = { git = "https://github.com/privacy-scaling-explorations/halo2wrong", package = "ecc", tag = "v2023_04_20", features = ["circuit-params"], optional = true }
pse_snark_verifier = { git = "https://github.com/privacy-scaling-explorations/snark-verifier", package = "snark-verifier", tag = "v2023_04_20", features = ["halo2_circuit_params"], optional = true }
pse_halo2wrong = { git = "https://github.com/privacy-scaling-explorations/halo2wrong", package = "halo2wrong", tag = "v2023_04_20", features = [
"circuit-params",
], optional = true }
pse_maingate = { git = "https://github.com/privacy-scaling-explorations/halo2wrong", package = "maingate", tag = "v2023_04_20", features = [
"circuit-params",
], optional = true }
pse_ecdsa = { git = "https://github.com/privacy-scaling-explorations/halo2wrong", package = "ecdsa", tag = "v2023_04_20", features = [
"circuit-params",
], optional = true }
pse_ecc = { git = "https://github.com/privacy-scaling-explorations/halo2wrong", package = "ecc", tag = "v2023_04_20", features = [
"circuit-params",
], optional = true }
pse_snark_verifier = { git = "https://github.com/privacy-scaling-explorations/snark-verifier", package = "snark-verifier", tag = "v2023_04_20", features = [
"halo2_circuit_params",
], optional = true }
# pse_zkevm_circuit = { git = "https://github.com/privacy-scaling-explorations/zkevm-circuits", package = "zkevm-circuits", tag = "v0.3.1", optional = true }

# axiom
halo2-base = { git = "https://github.com/axiom-crypto/halo2-lib", branch = "release-0.3.0", optional = true }
Expand All @@ -37,5 +51,12 @@ halo2-ecc = { git = "https://github.com/axiom-crypto/halo2-lib", branch = "relea
[features]
default = ["pse_halo2"]
zcash_halo2 = ["dep:zcash_halo2_proofs"]
pse_halo2 = ["dep:pse_halo2wrong", "dep:pse_maingate", "dep:pse_ecdsa", "dep:pse_ecc", "dep:pse_snark_verifier"]
axiom_halo2 = ["dep:halo2-base", "dep:snark-verifier", "dep:halo2-ecc"]
pse_halo2 = [
"dep:pse_halo2wrong",
"dep:pse_maingate",
"dep:pse_ecdsa",
"dep:pse_ecc",
"dep:pse_snark_verifier",
# "dep:pse_zkevm_circuit",
]
axiom_halo2 = ["dep:halo2-base", "dep:snark-verifier", "dep:halo2-ecc"]
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ This crate will not be possible without
2. [halo2-zcash](https://github.com/zcash/halo2)
3. [halo2-pse](https://github.com/privacy-scaling-explorations/halo2)
4. [halo2-axiom](https://github.com/axiom-crypto/halo2-lib)
5. [vamp-ir](https://github.com/anoma/vamp-ir)
5. the gadets and educational resources provided by the halo2 community see [awesome-halo2](https://github.com/adria0/awesome-halo2)

## Installtion
Expand Down Expand Up @@ -45,6 +44,8 @@ To install axioms's halo2 as backend:
cargo install --path . --locked --features axiom_halo2_backend --no-default-features
```

Notice that axiom's fork is built on pse's fork and should be similar.

## Examples

cd to the `examples` folder of this crate
Expand Down
4 changes: 0 additions & 4 deletions src/aztec_crs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ pub(crate) async fn get_aztec_crs(points_needed: u32) -> Result<(Vec<u8>, Vec<u8
}

async fn download(start: usize, end: usize) -> Result<Vec<u8>, CRSError> {
// TODO(#187): Allow downloading from more than just the first transcript
// We try to load a URL from the environment and otherwise fallback to a hardcoded URL to allow
// Nix to override the URL for testing in the sandbox, where there is no network access on Linux
let transcript_url = match env::var(TRANSCRIPT_URL_ENV_VAR) {
Ok(url) => url,
Err(_) => TRANSCRIPT_URL_FALLBACK.into(),
Expand All @@ -51,7 +48,6 @@ async fn download(start: usize, end: usize) -> Result<Vec<u8>, CRSError> {
url: transcript_url.to_string(),
})?;

// TODO(#195): We probably want to consider an injectable logger so we can have logging in JS
println!(
"\nDownloading the Ignite SRS ({})",
ByteSize(total_size).to_string_as(false)
Expand Down
2 changes: 1 addition & 1 deletion src/dimension_measure.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// copied from https://github.com/privacy-scaling-explorations/halo2wrong/blob/master/halo2wrong/src/utils.rs
// copied and adapted from https://github.com/privacy-scaling-explorations/halo2wrong/blob/master/halo2wrong/src/utils.rs
// cus fn k() is private.

use std::{cell::RefCell, ops::RangeInclusive};
Expand Down
7 changes: 4 additions & 3 deletions src/pse_halo2/acvm_interop/common_reference_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ use crate::pse_halo2::circuit_translator::NoirHalo2Translator;
use crate::pse_halo2::halo2_params::constuct_halo2_params_from_aztec_crs;
use crate::pse_halo2::PseHalo2;

// TODO(#185): Ensure CRS download works in JS
#[async_trait(?Send)]
impl CommonReferenceString for PseHalo2 {
type Error = BackendError;

/// Generate common reference string from
/// Aztec CRS ceremony
async fn generate_common_reference_string(
&self,
circuit: &Circuit,
Expand All @@ -32,11 +33,11 @@ impl CommonReferenceString for PseHalo2 {
pse_halo2wrong::halo2::SerdeFormat::RawBytes,
)
.unwrap();
// Separated to have nicer coercion on error types
Ok(common_reference_string)
}

// Separated to have nicer coercion on error types
/// Update common reference string from
/// Aztec CRS ceremony
async fn update_common_reference_string(
&self,
_common_reference_string: Vec<u8>,
Expand Down
32 changes: 25 additions & 7 deletions src/pse_halo2/acvm_interop/proof_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use pse_halo2wrong::halo2::plonk::{ProvingKey, VerifyingKey};
use pse_halo2wrong::halo2::poly::kzg::commitment::ParamsKZG;
use pse_halo2wrong::halo2::SerdeFormat;

use crate::dimension_measure::DimensionMeasurement;
use crate::errors::BackendError;
use crate::pse_halo2::circuit_translator::NoirHalo2Translator;
use crate::pse_halo2::halo2_plonk_api::OpcodeFlags;
Expand All @@ -25,10 +26,22 @@ use crate::noir_field_to_halo2_field;
impl ProofSystemCompiler for PseHalo2 {
type Error = BackendError;

/// Get the size of the circuit
fn get_exact_circuit_size(&self, circuit: &NoirCircuit) -> Result<u32, BackendError> {
Ok(circuit.opcodes.len() as u32)
let translator = NoirHalo2Translator::<Fr> {
circuit: circuit.clone(),
witness_values: WitnessMap::new(),
_marker: PhantomData::<Fr>,
};

let dimension = DimensionMeasurement::measure(&translator).unwrap();
let k = dimension.k();

Ok(1 << k)
}

/// Preprocess the circuit to get
/// Proving Key and Verifying Key
fn preprocess(
&self,
mut common_reference_string: &[u8],
Expand All @@ -51,6 +64,7 @@ impl ProofSystemCompiler for PseHalo2 {
))
}

/// Generate proof with Proving Key
fn prove_with_pk(
&self,
mut common_reference_string: &[u8],
Expand All @@ -63,7 +77,7 @@ impl ProofSystemCompiler for PseHalo2 {
ParamsKZG::<Bn256>::read_custom(&mut common_reference_string, SerdeFormat::RawBytes)
.unwrap();

let opcode_flags = OpcodeFlags::new(circuit.opcodes.clone());
let opcode_flags = OpcodeFlags::new(&circuit.opcodes);

let pk = ProvingKey::<G1Affine>::from_bytes::<NoirHalo2Translator<Fr>>(
proving_key,
Expand Down Expand Up @@ -93,11 +107,12 @@ impl ProofSystemCompiler for PseHalo2 {
Ok(proof)
}

/// Verify proof with Verification Key
fn verify_with_vk(
&self,
mut common_reference_string: &[u8],
proof: &[u8],
_public_inputs: WitnessMap,
public_inputs: WitnessMap,
circuit: &NoirCircuit,
verification_key: &[u8],
_is_recursive: bool,
Expand All @@ -106,7 +121,7 @@ impl ProofSystemCompiler for PseHalo2 {
ParamsKZG::<Bn256>::read_custom(&mut common_reference_string, SerdeFormat::RawBytes)
.unwrap();

let opcode_flags = OpcodeFlags::new(circuit.opcodes.clone());
let opcode_flags = OpcodeFlags::new(&circuit.opcodes);

let vk = VerifyingKey::<G1Affine>::from_bytes::<NoirHalo2Translator<Fr>>(
verification_key,
Expand All @@ -115,18 +130,22 @@ impl ProofSystemCompiler for PseHalo2 {
)
.unwrap();

let instance: Vec<Fr> = _public_inputs
let instance: Vec<Fr> = public_inputs
.into_iter()
.map(|(_, el)| noir_field_to_halo2_field(el))
.collect();

Ok(halo2_verify(&params, &vk, proof, &instance[..]).is_ok())
}

/// Type of constraint system
// The pse-halo2 backend supports Plonkish constraint
// of width 5 but we only used 3
fn np_language(&self) -> Language {
Language::PLONKCSat { width: 3 }
}

/// Opcodes supported by pse-halo2 backend
fn supports_opcode(&self, opcode: &acvm::acir::circuit::Opcode) -> bool {
match opcode {
Opcode::Arithmetic(_) => true,
Expand All @@ -136,10 +155,9 @@ impl ProofSystemCompiler for PseHalo2 {
Opcode::RAM(_) => false,
Opcode::Oracle(_) => false,
Opcode::BlackBoxFuncCall(func) => match func.get_black_box_func() {
BlackBoxFunc::RANGE => true,
BlackBoxFunc::RANGE | BlackBoxFunc::AND => true,

BlackBoxFunc::XOR
| BlackBoxFunc::AND
| BlackBoxFunc::SHA256
| BlackBoxFunc::Blake2s
| BlackBoxFunc::Pedersen
Expand Down
1 change: 0 additions & 1 deletion src/pse_halo2/acvm_interop/pwg.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use acvm::acir::native_types::Witness;

use acvm::pwg::OpcodeResolutionError;
use acvm::PartialWitnessGenerator;

Expand Down
3 changes: 3 additions & 0 deletions src/pse_halo2/acvm_interop/smart_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::pse_halo2::halo2_plonk_api::OpcodeFlags;
use crate::pse_halo2::PseHalo2;
type PlonkVerifier = verifier::plonk::PlonkVerifier<KzgAs<Bn256, Gwc19>>;

/// Generate the evm verifier of the circuit as Yul code
fn gen_evm_verifier(
params: &ParamsKZG<Bn256>,
vk: &VerifyingKey<G1Affine>,
Expand All @@ -44,6 +45,8 @@ fn gen_evm_verifier(

impl SmartContract for PseHalo2 {
type Error = BackendError;

/// Get ethereum verification contract from Verification Key
fn eth_contract_from_vk(
&self,
mut common_reference_string: &[u8],
Expand Down
Loading

0 comments on commit 1b5d191

Please sign in to comment.