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

fix halo2 verifier guest code #1137

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ openvm-rv32-adapters = { path = "extensions/rv32-adapters", default-features = f
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-snark-verifier = { path = "extensions/verifier", default-features = false }

# Plonky3
p3-air = { git = "https://github.com/Plonky3/Plonky3.git", rev = "9b267c4" }
Expand Down
2 changes: 1 addition & 1 deletion extensions/verifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ openvm-pairing-guest = { workspace = true, features = [
] }
openvm-keccak256-guest = { workspace = true, default-features = false }
openvm-ecc-guest = { workspace = true, features = ["halo2curves"] }
snark-verifier = { git = "https://github.com/axiom-crypto/snark-verifier.git", tag = "v0.1.7-git", default-features = false, features = ["halo2-axiom"] }
snark-verifier-sdk = { workspace = true, default-features = false, features = ["halo2-axiom", "loader_evm"] }
ff = { workspace = true }
halo2curves-axiom = { workspace = true }
itertools.workspace = true
Expand Down
67 changes: 57 additions & 10 deletions extensions/verifier/src/verifier/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@
use std::{fmt::Debug, marker::PhantomData};

use halo2curves_axiom::bn256::{Bn256, Fq as Halo2Fp, Fr as Halo2Fr, G1Affine, G2Affine};
use itertools::Itertools;
use lazy_static::lazy_static;
use openvm_ecc_guest::{
algebra::{field::FieldExtension, IntMod},
msm, AffinePoint,
};
use openvm_pairing_guest::{
affine_point::AffineCoords,
bn254::{Bn254, Bn254Fp as Fp, Bn254G1Affine as EcPoint, Fp2, Scalar as Fr},
pairing::PairingCheck,
};
use snark_verifier::{
use snark_verifier_sdk::snark_verifier::{
loader::{EcPointLoader, Loader, ScalarLoader},
pcs::{
kzg::{KzgAccumulator, KzgAs, KzgDecidingKey},
AccumulationDecider,
kzg::{KzgAccumulator, KzgAs, KzgDecidingKey, LimbsEncoding},
AccumulationDecider, AccumulatorEncoding,
},
util::arithmetic::fe_from_limbs,
Error,
};

Expand All @@ -31,13 +32,59 @@ lazy_static! {
#[derive(Clone, Debug)]
pub struct OpenVmLoader;

impl<const LIMBS: usize, const BITS: usize> AccumulatorEncoding<G1Affine, OpenVmLoader>
for LimbsEncoding<LIMBS, BITS>
{
type Accumulator = KzgAccumulator<G1Affine, OpenVmLoader>;

fn from_repr(limbs: &[&OpenVmScalar<Halo2Fr, Fr>]) -> Result<Self::Accumulator, Error> {
assert_eq!(limbs.len(), 4 * LIMBS);

let [lhs_x, lhs_y, rhs_x, rhs_y]: [_; 4] = limbs
.chunks(LIMBS)
.map(|limbs| {
let v: [Halo2Fr; LIMBS] = limbs
.iter()
.map(|limb| {
let mut buf = limb.0.to_be_bytes();
buf.reverse();
Halo2Fr::from_bytes(&buf).expect("Halo2Fr::from_bytes")
jonathanpwang marked this conversation as resolved.
Show resolved Hide resolved
})
.collect_vec()
.try_into()
.unwrap();
fe_from_limbs::<_, Halo2Fp, LIMBS, BITS>(v)
})
.collect_vec()
.try_into()
.unwrap();
let accumulator = KzgAccumulator::new(
OpenVmEcPoint(
EcPoint {
x: Fp::from_le_bytes(&lhs_x.to_bytes()),
y: Fp::from_le_bytes(&lhs_y.to_bytes()),
},
PhantomData,
),
OpenVmEcPoint(
EcPoint {
x: Fp::from_le_bytes(&rhs_x.to_bytes()),
y: Fp::from_le_bytes(&rhs_y.to_bytes()),
},
PhantomData,
),
);
Ok(accumulator)
}
}

impl EcPointLoader<G1Affine> for OpenVmLoader {
type LoadedEcPoint = OpenVmEcPoint<G1Affine, EcPoint>;

fn ec_point_load_const(&self, value: &G1Affine) -> Self::LoadedEcPoint {
let point = EcPoint {
x: Fp::from_be_bytes(&value.x().to_bytes()),
y: Fp::from_be_bytes(&value.y().to_bytes()),
x: Fp::from_le_bytes(&value.x.to_bytes()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just noting the halo2curves .to_bytes() actually does some computation, but with the way snark-verifier traits work, this seems unavoidable

y: Fp::from_le_bytes(&value.y.to_bytes()),
};
// new(value.x(), value.y());
OpenVmEcPoint(point, PhantomData)
Expand Down Expand Up @@ -74,7 +121,7 @@ impl ScalarLoader<Halo2Fr> for OpenVmLoader {
type LoadedScalar = OpenVmScalar<Halo2Fr, Fr>;

fn load_const(&self, value: &Halo2Fr) -> Self::LoadedScalar {
let value = Fr::from_be_bytes(&value.to_bytes());
let value = Fr::from_le_bytes(&value.to_bytes());
OpenVmScalar(value, PhantomData)
}

Expand All @@ -89,7 +136,7 @@ impl ScalarLoader<Halo2Fp> for OpenVmLoader {
type LoadedScalar = OpenVmScalar<Halo2Fp, Fp>;

fn load_const(&self, value: &Halo2Fp) -> Self::LoadedScalar {
let value = Fp::from_be_bytes(&value.to_bytes());
let value = Fp::from_le_bytes(&value.to_bytes());
OpenVmScalar(value, PhantomData)
}

Expand Down Expand Up @@ -122,8 +169,8 @@ where
let mut P = Vec::with_capacity(2);
let mut Q = Vec::with_capacity(2);
for t in terms {
let x = t.1.x().to_bytes();
let y = t.1.y().to_bytes();
let x = t.1.x.to_bytes();
let y = t.1.y.to_bytes();
let point = AffinePoint { x: t.0.x, y: t.0.y };
P.push(point);
let point = AffinePoint {
Expand Down
2 changes: 1 addition & 1 deletion extensions/verifier/src/verifier/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use halo2curves_axiom::{
};
use openvm_ecc_guest::algebra::{ExpBytes, Field, IntMod};
use openvm_pairing_guest::bn254::{Bn254G1Affine as EcPoint, Fp, Scalar as Fr};
use snark_verifier::{
use snark_verifier_sdk::snark_verifier::{
loader::{LoadedEcPoint, LoadedScalar},
util::arithmetic::FieldOps,
};
Expand Down
31 changes: 22 additions & 9 deletions extensions/verifier/src/verifier/transcript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ use halo2curves_axiom::{
use itertools::Itertools;
use openvm_ecc_guest::algebra::IntMod;
use openvm_keccak256_guest::keccak256;
use openvm_pairing_guest::{
affine_point::AffineCoords,
bn254::{Bn254G1Affine as EcPoint, Fp, Scalar as Fr},
};
use snark_verifier::{
use openvm_pairing_guest::bn254::{Bn254G1Affine as EcPoint, Fp, Scalar as Fr};
use snark_verifier_sdk::snark_verifier::{
util::transcript::{Transcript, TranscriptRead},
Error,
};
Expand All @@ -31,14 +28,25 @@ pub struct OpenVmTranscript<C: CurveAffine, S, B> {
_marker: PhantomData<C>,
}

impl<S> OpenVmTranscript<G1Affine, S, Vec<u8>> {
/// Initialize [`OpenVmTranscript`] given readable or writeable stream for
/// verifying or proving with [`OpenVmLoader`].
pub fn new(stream: S) -> Self {
Self {
stream,
buf: Vec::new(),
_marker: PhantomData,
}
}
}
impl<S> Transcript<G1Affine, OpenVmLoader> for OpenVmTranscript<G1Affine, S, Vec<u8>> {
fn loader(&self) -> &OpenVmLoader {
&LOADER
}

fn squeeze_challenge(
&mut self,
) -> <super::loader::OpenVmLoader as snark_verifier::loader::ScalarLoader<Halo2Fr>>::LoadedScalar
) -> <super::loader::OpenVmLoader as snark_verifier_sdk::snark_verifier::loader::ScalarLoader<Halo2Fr>>::LoadedScalar
{
let data = self
.buf
Expand All @@ -52,7 +60,9 @@ impl<S> Transcript<G1Affine, OpenVmLoader> for OpenVmTranscript<G1Affine, S, Vec
.collect_vec();
let hash = keccak256(&data);
self.buf = hash.to_vec();
OpenVmScalar(Fr::from_be_bytes(&hash), PhantomData)
let mut fr = Fr::from_be_bytes(&hash);
fr.reduce();
OpenVmScalar(fr, PhantomData)
}

fn common_ec_point(
Expand All @@ -64,7 +74,11 @@ impl<S> Transcript<G1Affine, OpenVmLoader> for OpenVmTranscript<G1Affine, S, Vec
x.copy_from_slice(ec_point.0.x.as_le_bytes());
y.copy_from_slice(ec_point.0.y.as_le_bytes());
let coordinates = Option::<Coordinates<G1Affine>>::from(
G1Affine::new(Fq::from_bytes(&x).unwrap(), Fq::from_bytes(&y).unwrap()).coordinates(),
G1Affine {
x: Fq::from_bytes(&x).unwrap(),
y: Fq::from_bytes(&y).unwrap(),
}
.coordinates(),
)
.ok_or_else(|| {
Error::Transcript(
Expand Down Expand Up @@ -108,7 +122,6 @@ where
self.stream
.read_exact(repr.as_mut())
.map_err(|err| Error::Transcript(err.kind(), err.to_string()))?;
repr.as_mut().reverse();
}
let x = Fp::from_be_bytes(&x);
let y = Fp::from_be_bytes(&y);
Expand Down