Skip to content

Commit

Permalink
feat: (halo2) SNARK verifier (#805)
Browse files Browse the repository at this point in the history
* make msm implementation with blackbox

* feat: use msm_serial variant

* feat: set up foundations

* wip

* implement accumulation decider

* wip

* wip

* wip

* wip

* finish loader

* finish decider

* finish transcript(?)

* add files

* verifier code done

* rename and clean

* fix halo2 verifier guest code (#1137)

* fix halo2 verifier guest code

* clean

* build

* switch to workspace snark verifier

* add tests for verifier

* rename and add files

* wip: make integration test

* chore: protect EcPoint constructor

* feat: add integration test

* chore: remove unused

* chore: lints

---------

Co-authored-by: Zhang Zhuo <[email protected]>
Co-authored-by: Jonathan Wang <[email protected]>
  • Loading branch information
3 people authored Dec 31, 2024
1 parent 32e6bbf commit 3ab4123
Show file tree
Hide file tree
Showing 24 changed files with 1,473 additions and 78 deletions.
39 changes: 33 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 37 additions & 32 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ members = [
"crates/toolchain/platform",
"crates/toolchain/tests",
"crates/vm",
"extensions/rv32im/circuit",
"extensions/rv32im/transpiler",
"extensions/rv32im/guest",
"extensions/rv32im/tests",
"extensions/rv32-adapters",
"extensions/native/circuit",
"extensions/native/compiler",
"extensions/native/compiler/derive",
"extensions/native/recursion",
"extensions/algebra/circuit",
"extensions/algebra/transpiler",
"extensions/algebra/guest",
Expand All @@ -38,14 +47,10 @@ members = [
"extensions/keccak256/transpiler",
"extensions/keccak256/guest",
"extensions/keccak256/tests",
"extensions/native/circuit",
"extensions/native/compiler",
"extensions/native/compiler/derive",
"extensions/native/recursion",
"extensions/rv32im/circuit",
"extensions/rv32im/transpiler",
"extensions/rv32im/guest",
"extensions/rv32im/tests",
"extensions/sha256/circuit",
"extensions/sha256/transpiler",
"extensions/sha256/guest",
"extensions/sha256/tests",
"extensions/ecc/circuit",
"extensions/ecc/transpiler",
"extensions/ecc/guest",
Expand All @@ -55,11 +60,7 @@ members = [
"extensions/pairing/transpiler",
"extensions/pairing/guest",
"extensions/pairing/tests",
"extensions/rv32-adapters",
"extensions/sha256/circuit",
"extensions/sha256/transpiler",
"extensions/sha256/guest",
"extensions/sha256/tests",
"extensions/snark-verifier",
"ci/scripts/metric_unify",
]
exclude = ["crates/sdk/example"]
Expand Down Expand Up @@ -101,6 +102,10 @@ incremental = true
lto = "thin"

[workspace.dependencies]
# Stark Backend
openvm-stark-backend = { git = "https://github.com/openvm-org/stark-backend.git", tag = "v0.1.3-alpha", default-features = false }
openvm-stark-sdk = { git = "https://github.com/openvm-org/stark-backend.git", tag = "v0.1.3-alpha", default-features = false }

# OpenVM
openvm-sdk = { path = "crates/sdk", default-features = false }
cargo-openvm = { path = "crates/cli", default-features = false }
Expand All @@ -118,39 +123,39 @@ openvm-platform = { path = "crates/toolchain/platform", default-features = false
openvm-transpiler = { path = "crates/toolchain/transpiler", default-features = false }
openvm-circuit = { path = "crates/vm", default-features = false }
openvm-circuit-derive = { path = "crates/vm/derive", default-features = false }
openvm-stark-backend = { git = "https://github.com/openvm-org/stark-backend.git", tag = "v0.1.3-alpha", default-features = false }
openvm-stark-sdk = { git = "https://github.com/openvm-org/stark-backend.git", tag = "v0.1.3-alpha", default-features = false }
openvm-toolchain-tests = { path = "crates/toolchain/tests", default-features = false }

# Extensions
openvm-rv32im-circuit = { path = "extensions/rv32im/circuit", default-features = false }
openvm-rv32im-transpiler = { path = "extensions/rv32im/transpiler", default-features = false }
openvm-rv32im-guest = { path = "extensions/rv32im/guest", default-features = false }
openvm-rv32-adapters = { path = "extensions/rv32-adapters", default-features = false }
openvm-native-circuit = { path = "extensions/native/circuit", default-features = false }
openvm-native-compiler = { path = "extensions/native/compiler", default-features = false }
openvm-native-compiler-derive = { path = "extensions/native/compiler/derive", default-features = false }
openvm-native-recursion = { path = "extensions/native/recursion", default-features = false }
openvm-keccak256-circuit = { path = "extensions/keccak256/circuit", default-features = false }
openvm-keccak256-transpiler = { path = "extensions/keccak256/transpiler", default-features = false }
openvm-keccak256-guest = { path = "extensions/keccak256/guest", default-features = false }
openvm-sha256-circuit = { path = "extensions/sha256/circuit", default-features = false }
openvm-sha256-transpiler = { path = "extensions/sha256/transpiler", default-features = false }
openvm-sha256-guest = { path = "extensions/sha256/guest", default-features = false }
openvm-bigint-circuit = { path = "extensions/bigint/circuit", default-features = false }
openvm-bigint-transpiler = { path = "extensions/bigint/transpiler", default-features = false }
openvm-bigint-guest = { path = "extensions/bigint/guest", default-features = false }
openvm-algebra-circuit = { path = "extensions/algebra/circuit", default-features = false }
openvm-algebra-transpiler = { path = "extensions/algebra/transpiler", default-features = false }
openvm-algebra-guest = { path = "extensions/algebra/guest", default-features = false }
openvm-algebra-moduli-setup = { path = "extensions/algebra/moduli-setup", default-features = false }
openvm-algebra-complex-macros = { path = "extensions/algebra/guest/src/field/complex-macros", default-features = false }
openvm-bigint-circuit = { path = "extensions/bigint/circuit", default-features = false }
openvm-bigint-transpiler = { path = "extensions/bigint/transpiler", default-features = false }
openvm-bigint-guest = { path = "extensions/bigint/guest", default-features = false }
openvm-ecc-circuit = { path = "extensions/ecc/circuit", default-features = false }
openvm-ecc-transpiler = { path = "extensions/ecc/transpiler", default-features = false }
openvm-ecc-guest = { path = "extensions/ecc/guest", default-features = false }
openvm-ecc-sw-setup = { path = "extensions/ecc/sw-setup", default-features = false }
openvm-keccak256-circuit = { path = "extensions/keccak256/circuit", default-features = false }
openvm-keccak256-transpiler = { path = "extensions/keccak256/transpiler", default-features = false }
openvm-keccak256-guest = { path = "extensions/keccak256/guest", default-features = false }
openvm-native-circuit = { path = "extensions/native/circuit", default-features = false }
openvm-native-compiler = { path = "extensions/native/compiler", default-features = false }
openvm-native-compiler-derive = { path = "extensions/native/compiler/derive", default-features = false }
openvm-native-recursion = { path = "extensions/native/recursion", default-features = false }
openvm-pairing-circuit = { path = "extensions/pairing/circuit", default-features = false }
openvm-pairing-transpiler = { path = "extensions/pairing/transpiler", default-features = false }
openvm-pairing-guest = { path = "extensions/pairing/guest", default-features = false }
openvm-rv32-adapters = { path = "extensions/rv32-adapters", default-features = false }
openvm-rv32im-circuit = { path = "extensions/rv32im/circuit", default-features = false }
openvm-rv32im-transpiler = { path = "extensions/rv32im/transpiler", default-features = false }
openvm-rv32im-guest = { path = "extensions/rv32im/guest", default-features = false }
openvm-sha256-circuit = { path = "extensions/sha256/circuit", default-features = false }
openvm-sha256-transpiler = { path = "extensions/sha256/transpiler", default-features = false }
openvm-sha256-guest = { path = "extensions/sha256/guest", default-features = false }
openvm-snark-verifier = { path = "extensions/snark-verifier", default-features = false }

# Plonky3
p3-air = { git = "https://github.com/Plonky3/Plonky3.git", rev = "9b267c4" }
Expand Down
1 change: 1 addition & 0 deletions extensions/algebra/guest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ repository.workspace = true
openvm = { workspace = true }
openvm-platform = { workspace = true }
openvm-algebra-moduli-setup = { workspace = true }
openvm-algebra-complex-macros = { workspace = true }
serde = { workspace = true }
serde-big-array = "0.5.1"
strum_macros = { workspace = true }
Expand Down
8 changes: 4 additions & 4 deletions extensions/algebra/guest/src/field/complex-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ pub fn complex_declare(input: TokenStream) -> TokenStream {
{
let (c0, c1) = (&self.c0, &self.c1);
let (d0, d1) = (&other.c0, &other.c1);
let denom = <#intmod_type as openvm_algebra_guest::IntMod>::ONE.div_unsafe(d0.square() + d1.square());
let denom = openvm_algebra_guest::DivUnsafe::div_unsafe(<#intmod_type as openvm_algebra_guest::IntMod>::ONE, d0.square() + d1.square());
*self = Self::new(
denom.clone() * (c0.clone() * d0 + c1.clone() * d1),
denom * &(c1.clone() * d0 - c0.clone() * d1),
Expand Down Expand Up @@ -575,7 +575,7 @@ pub fn complex_init(input: TokenStream) -> TokenStream {
externs.push(quote::quote_spanned! { span.into() =>
#[no_mangle]
extern "C" fn #func_name(rd: usize, rs1: usize, rs2: usize) {
openvm_platform::custom_insn_r!(
openvm::platform::custom_insn_r!(
openvm_algebra_guest::OPCODE,
openvm_algebra_guest::COMPLEX_EXT_FIELD_FUNCT3,
openvm_algebra_guest::ComplexExtFieldBaseFunct7::#local_opcode as usize
Expand Down Expand Up @@ -604,7 +604,7 @@ pub fn complex_init(input: TokenStream) -> TokenStream {
// We are going to use the numeric representation of the `rs2` register to distinguish the chip to setup.
// The transpiler will transform this instruction, based on whether `rs2` is `x0` or `x1`, into a `SETUP_ADDSUB` or `SETUP_MULDIV` instruction.
let mut uninit: core::mem::MaybeUninit<[u8; openvm_intrinsics_meta_do_not_type_this_by_yourself::limb_list_borders[#mod_idx + 1] - openvm_intrinsics_meta_do_not_type_this_by_yourself::limb_list_borders[#mod_idx]]> = core::mem::MaybeUninit::uninit();
openvm_platform::custom_insn_r!(
openvm::platform::custom_insn_r!(
::openvm_algebra_guest::OPCODE,
::openvm_algebra_guest::COMPLEX_EXT_FIELD_FUNCT3,
::openvm_algebra_guest::ComplexExtFieldBaseFunct7::Setup as usize
Expand All @@ -614,7 +614,7 @@ pub fn complex_init(input: TokenStream) -> TokenStream {
two_modulus_bytes.as_ptr(),
"x0" // will be parsed as 0 and therefore transpiled to SETUP_ADDMOD
);
openvm_platform::custom_insn_r!(
openvm::platform::custom_insn_r!(
::openvm_algebra_guest::OPCODE,
::openvm_algebra_guest::COMPLEX_EXT_FIELD_FUNCT3,
::openvm_algebra_guest::ComplexExtFieldBaseFunct7::Setup as usize
Expand Down
1 change: 1 addition & 0 deletions extensions/algebra/guest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ use core::{
pub use field::Field;
#[cfg(not(target_os = "zkvm"))]
use num_bigint_dig::BigUint;
pub use openvm_algebra_complex_macros as complex_macros;
pub use openvm_algebra_moduli_setup as moduli_setup;
pub use serde_big_array::BigArray;
use strum_macros::FromRepr;
Expand Down
6 changes: 3 additions & 3 deletions extensions/algebra/moduli-setup/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ pub fn moduli_init(input: TokenStream) -> TokenStream {
externs.push(quote::quote_spanned! { span.into() =>
#[no_mangle]
extern "C" fn #func_name(rd: usize, rs1: usize, rs2: usize) {
openvm_platform::custom_insn_r!(
openvm::platform::custom_insn_r!(
::openvm_algebra_guest::OPCODE,
::openvm_algebra_guest::MODULAR_ARITHMETIC_FUNCT3 as usize,
::openvm_algebra_guest::ModArithBaseFunct7::#local_opcode as usize + #mod_idx * (::openvm_algebra_guest::ModArithBaseFunct7::MODULAR_ARITHMETIC_MAX_KINDS as usize),
Expand Down Expand Up @@ -832,7 +832,7 @@ pub fn moduli_init(input: TokenStream) -> TokenStream {
// We are going to use the numeric representation of the `rs2` register to distinguish the chip to setup.
// The transpiler will transform this instruction, based on whether `rs2` is `x0`, `x1` or `x2`, into a `SETUP_ADDSUB`, `SETUP_MULDIV` or `SETUP_ISEQ` instruction.
let mut uninit: core::mem::MaybeUninit<[u8; #limbs]> = core::mem::MaybeUninit::uninit();
openvm_platform::custom_insn_r!(
openvm::platform::custom_insn_r!(
::openvm_algebra_guest::OPCODE,
::openvm_algebra_guest::MODULAR_ARITHMETIC_FUNCT3,
::openvm_algebra_guest::ModArithBaseFunct7::SetupMod as usize
Expand All @@ -842,7 +842,7 @@ pub fn moduli_init(input: TokenStream) -> TokenStream {
remaining.as_ptr(),
"x0" // will be parsed as 0 and therefore transpiled to SETUP_ADDMOD
);
openvm_platform::custom_insn_r!(
openvm::platform::custom_insn_r!(
::openvm_algebra_guest::OPCODE,
::openvm_algebra_guest::MODULAR_ARITHMETIC_FUNCT3,
::openvm_algebra_guest::ModArithBaseFunct7::SetupMod as usize
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![cfg_attr(not(feature = "std"), no_main)]
#![cfg_attr(not(feature = "std"), no_std)]

use openvm_algebra_guest::{DivUnsafe, IntMod};
use openvm_algebra_guest::IntMod;

openvm::entry!(main);

Expand Down
12 changes: 6 additions & 6 deletions extensions/ecc/sw-setup/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ pub fn sw_declare(input: TokenStream) -> TokenStream {
#[derive(Eq, PartialEq, Clone, Debug, serde::Serialize, serde::Deserialize)]
#[repr(C)]
pub struct #struct_name {
pub x: #intmod_type,
pub y: #intmod_type,
x: #intmod_type,
y: #intmod_type,
}

impl #struct_name {
Expand Down Expand Up @@ -370,7 +370,7 @@ pub fn sw_init(input: TokenStream) -> TokenStream {
externs.push(quote::quote_spanned! { span.into() =>
#[no_mangle]
extern "C" fn #add_ne_extern_func(rd: usize, rs1: usize, rs2: usize) {
openvm_platform::custom_insn_r!(
openvm::platform::custom_insn_r!(
OPCODE,
SW_FUNCT3 as usize,
SwBaseFunct7::SwAddNe as usize + #ec_idx
Expand All @@ -383,7 +383,7 @@ pub fn sw_init(input: TokenStream) -> TokenStream {

#[no_mangle]
extern "C" fn #double_extern_func(rd: usize, rs1: usize) {
openvm_platform::custom_insn_r!(
openvm::platform::custom_insn_r!(
OPCODE,
SW_FUNCT3 as usize,
SwBaseFunct7::SwDouble as usize + #ec_idx
Expand Down Expand Up @@ -425,7 +425,7 @@ pub fn sw_init(input: TokenStream) -> TokenStream {
// (EcAdd only) p2 is (x2, y2), and x1 - x2 has to be non-zero to avoid division over zero in add.
let p2 = [one.as_ref(), one.as_ref()].concat();
let mut uninit: core::mem::MaybeUninit<[#item; 2]> = core::mem::MaybeUninit::uninit();
openvm_platform::custom_insn_r!(
openvm::platform::custom_insn_r!(
::openvm_ecc_guest::OPCODE,
::openvm_ecc_guest::SW_FUNCT3 as usize,
::openvm_ecc_guest::SwBaseFunct7::SwSetup as usize
Expand All @@ -435,7 +435,7 @@ pub fn sw_init(input: TokenStream) -> TokenStream {
p1.as_ptr(),
p2.as_ptr()
);
openvm_platform::custom_insn_r!(
openvm::platform::custom_insn_r!(
::openvm_ecc_guest::OPCODE,
::openvm_ecc_guest::SW_FUNCT3 as usize,
::openvm_ecc_guest::SwBaseFunct7::SwSetup as usize
Expand Down
15 changes: 9 additions & 6 deletions extensions/ecc/tests/programs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ k256 = { version = "0.13.3", default-features = false, features = [

[features]
default = []
std = [
"serde/std",
"openvm/std",
"openvm-ecc-guest/std",
]

std = ["serde/std", "openvm/std", "openvm-ecc-guest/std"]
k256 = ["openvm-ecc-guest/k256", "dep:k256"]

[profile.release]
Expand All @@ -43,3 +38,11 @@ lto = "thin" # turn on lto = fat to decrease binary size, but this optimizes
[[example]]
name = "ec"
required-features = ["k256"]

[[example]]
name = "decompress"
required-features = ["k256"]

[[example]]
name = "ecdsa"
required-features = ["k256"]
6 changes: 3 additions & 3 deletions extensions/ecc/tests/programs/examples/decompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use openvm::io::read_vec;
use openvm_ecc_guest::{
algebra::IntMod,
k256::{Secp256k1Coord, Secp256k1Point},
weierstrass::FromCompressed,
weierstrass::{FromCompressed, WeierstrassPoint},
};

openvm::entry!(main);
Expand All @@ -31,6 +31,6 @@ pub fn main() {
assert_eq!(y, hint_y);

let p = Secp256k1Point::decompress(x.clone(), &rec_id);
assert_eq!(p.x, x);
assert_eq!(p.y, y);
assert_eq!(p.x(), &x);
assert_eq!(p.y(), &y);
}
Loading

0 comments on commit 3ab4123

Please sign in to comment.