Skip to content

Commit 06e8a00

Browse files
feat: remove the blitzar requirement from building nova-snark when using the HyperKZG commitment scheme (#1008)
# Rationale for this change Currently when you run `cargo install` on the Proof of SQL CLI project you need to manually download the Blitzar `.so` file to make things work. This is required because they HyperKZG commitment scheme builds with `blitzar` enabled. This PR removes that requirement by moving the code required by the HyperKZG setup that uses Blitzar, but not the GPU, locally to the project. That allows the HyperKZG commitment scheme to no longer require `blitzar` as a feature. The optional flag to build `nova-snark` with `blitzar` enabled is moved to the `blitzar` feature flag. # What changes are included in this PR? - `arkworks_halo2_interop` is copied from the `blitzar-rs` project - Code using `arkworks_halo2_interop` functions no longer require `blitzar` - `nova-snark` no longer has a `blitzar` requirement - `nova-snark` will built with `blitzar` feature flag when `proof-of-sql` also is using the `blitzar` feature flag # Are these changes tested? Yes locally with CI checks and benchmarks
1 parent 56ffd5f commit 06e8a00

File tree

6 files changed

+100
-12
lines changed

6 files changed

+100
-12
lines changed

crates/proof-of-sql-benches/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ indexmap = { version = "2.8", default-features = false }
2222
nova-snark = { version = "0.41.0", default-features = false }
2323
opentelemetry = { version = "0.23.0" }
2424
opentelemetry-jaeger = { version = "0.20.0" }
25-
proof-of-sql = { path = "../proof-of-sql", default-features = false, features = ["arrow", "hyperkzg_proof"] }
25+
proof-of-sql = { path = "../proof-of-sql", features = ["hyperkzg_proof"] }
2626
proof-of-sql-planner = { path = "../proof-of-sql-planner" }
2727
rand = { version = "0.8", default-features = false }
2828
sqlparser = { version = "0.45.0", default-features = false }

crates/proof-of-sql/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ indexmap = { workspace = true, features = ["serde"] }
4343
indicatif = { workspace = true, optional = true }
4444
itertools = { workspace = true }
4545
merlin = { workspace = true, optional = true }
46-
nova-snark = { workspace = true, optional = true, features = ["blitzar"] }
46+
nova-snark = { workspace = true, optional = true }
4747
num-traits = { workspace = true }
4848
num-bigint = { workspace = true, default-features = false }
4949
postcard = { workspace = true, features = ["alloc"] }
@@ -81,8 +81,8 @@ development = ["arrow-csv"]
8181
default = ["arrow", "perf"]
8282
utils = ["dep:indicatif", "dep:rand_chacha", "dep:sha2", "dep:clap", "dep:tempfile"]
8383
arrow = ["dep:arrow", "std"]
84-
blitzar = ["dep:blitzar", "dep:merlin", "std"]
85-
hyperkzg_proof = ["dep:nova-snark", "std", "dep:ff", "dep:halo2curves", "blitzar"]
84+
blitzar = ["dep:blitzar", "dep:merlin", "std", "nova-snark?/blitzar"]
85+
hyperkzg_proof = ["dep:nova-snark", "std", "dep:ff", "dep:halo2curves"]
8686
test = ["dep:rand", "std"]
8787
perf = ["blitzar", "cpu-perf"]
8888
cpu-perf = ["rayon", "ark-ec/parallel", "ark-poly/parallel", "ark-ff/asm", "halo2curves/asm"]
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// This code is adapted from the blitzar-rs project:
2+
// https://github.com/spaceandtimefdn/blitzar-rs/blob/main/src/compute/arkworks_halo2_interop.rs
3+
4+
use ark_bn254::{Fq as Bn254Fq, G1Affine as Bn254G1Affine};
5+
use ark_ff::BigInteger256;
6+
use core::mem;
7+
use halo2curves::{
8+
bn256::{Fq as Halo2Bn256Fq, G1Affine as Halo2Bn256G1Affine},
9+
serde::SerdeObject,
10+
};
11+
12+
fn convert_halo2_to_limbs(point: &Halo2Bn256Fq) -> [u64; 4] {
13+
let limbs: [u64; 4] = unsafe { mem::transmute(*point) };
14+
limbs
15+
}
16+
17+
/// Converts a Halo2 BN256 G1 Affine point to an Arkworks BN254 G1 Affine point.
18+
pub fn convert_to_ark_bn254_g1_affine(point: &Halo2Bn256G1Affine) -> Bn254G1Affine {
19+
let x_limbs: [u64; 4] = convert_halo2_to_limbs(&point.x);
20+
let y_limbs: [u64; 4] = convert_halo2_to_limbs(&point.y);
21+
22+
Bn254G1Affine {
23+
x: Bn254Fq::new_unchecked(BigInteger256::new(x_limbs)),
24+
y: Bn254Fq::new_unchecked(BigInteger256::new(y_limbs)),
25+
infinity: *point == Halo2Bn256G1Affine::default(),
26+
}
27+
}
28+
29+
/// Converts an Arkworks BN254 G1 Affine point to a Halo2 BN256 G1 Affine point.
30+
pub fn convert_to_halo2_bn256_g1_affine(point: &Bn254G1Affine) -> Halo2Bn256G1Affine {
31+
if point.infinity {
32+
return Halo2Bn256G1Affine::default();
33+
}
34+
35+
let x_bytes = bytemuck::cast::<[u64; 4], [u8; 32]>(point.x.0 .0);
36+
let y_bytes = bytemuck::cast::<[u64; 4], [u8; 32]>(point.y.0 .0);
37+
38+
Halo2Bn256G1Affine {
39+
x: Halo2Bn256Fq::from_raw_bytes_unchecked(&x_bytes),
40+
y: Halo2Bn256Fq::from_raw_bytes_unchecked(&y_bytes),
41+
}
42+
}
43+
44+
#[cfg(test)]
45+
mod tests {
46+
use super::*;
47+
use halo2curves::bn256::Fq as Halo2Bn256Fq;
48+
49+
#[test]
50+
fn test_convert_halo2_modulus_to_limbs() {
51+
let expected: [u64; 4] = [
52+
4_332_616_871_279_656_263,
53+
10_917_124_144_477_883_021,
54+
13_281_191_951_274_694_749,
55+
3_486_998_266_802_970_665,
56+
];
57+
let modulus = Halo2Bn256Fq::from_raw(expected);
58+
let point = convert_halo2_to_limbs(&modulus);
59+
assert_eq!(point, [0, 0, 0, 0]);
60+
}
61+
62+
#[test]
63+
fn test_convert_halo2_one_to_one_in_montgomery_form_in_limbs() {
64+
let one: [u64; 4] = [1, 0, 0, 0];
65+
let one_in_mont = Halo2Bn256Fq::from_raw(one);
66+
let point = convert_halo2_to_limbs(&one_in_mont);
67+
68+
let expected: [u64; 4] = [
69+
15_230_403_791_020_821_917,
70+
754_611_498_739_239_741,
71+
7_381_016_538_464_732_716,
72+
1_011_752_739_694_698_287,
73+
];
74+
75+
assert_eq!(point, expected);
76+
}
77+
}

crates/proof-of-sql/src/proof_primitive/hyperkzg/commitment_evaluation_proof.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use super::{BNScalar, HyperKZGCommitment, HyperKZGEngine, HyperKZGPublicSetup};
22
use crate::{
33
base::{commitment::CommitmentEvaluationProof, slice_ops},
4-
proof_primitive::hyperkzg::nova_commitment::NovaCommitment,
4+
proof_primitive::hyperkzg::{
5+
convert_to_halo2_bn256_g1_affine, nova_commitment::NovaCommitment,
6+
},
57
};
68
use ark_bn254::{G1Affine, G1Projective};
7-
use blitzar;
89
use core::ops::Add;
910
use ff::Field;
1011
use halo2curves::bn256::G2Affine;
@@ -80,7 +81,7 @@ impl CommitmentEvaluationProof for HyperKZGCommitmentEvaluationProof {
8081

8182
let span = span!(Level::DEBUG, "initialize nova_ck").entered();
8283
let nova_ck: CommitmentKey<HyperKZGEngine> = CommitmentKey::new(
83-
slice_ops::slice_cast_with(setup, blitzar::compute::convert_to_halo2_bn256_g1_affine),
84+
slice_ops::slice_cast_with(setup, convert_to_halo2_bn256_g1_affine),
8485
Affine::default(), // I'm pretty sure this is unused in the proof
8586
G2Affine::default(), // I'm pretty sure this is unused in the proof
8687
);
@@ -126,7 +127,7 @@ impl CommitmentEvaluationProof for HyperKZGCommitmentEvaluationProof {
126127
.fold(G1Projective::default(), Add::add)
127128
.into();
128129
let nova_commit = nova_snark::provider::hyperkzg::Commitment::new(
129-
blitzar::compute::convert_to_halo2_bn256_g1_affine(&commit).into(),
130+
convert_to_halo2_bn256_g1_affine(&commit).into(),
130131
);
131132
let nova_eval = evaluations
132133
.iter()

crates/proof-of-sql/src/proof_primitive/hyperkzg/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ pub use public_setup::{
2222
mod commitment;
2323
pub use commitment::HyperKZGCommitment;
2424

25+
#[cfg(feature = "hyperkzg_proof")]
26+
mod arkworks_halo2_interop;
27+
#[cfg(feature = "hyperkzg_proof")]
28+
pub(crate) use arkworks_halo2_interop::{
29+
convert_to_ark_bn254_g1_affine, convert_to_halo2_bn256_g1_affine,
30+
};
31+
2532
#[cfg(feature = "hyperkzg_proof")]
2633
mod nova_commitment;
2734

crates/proof-of-sql/src/proof_primitive/hyperkzg/nova_engine.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use super::{BNScalar, HyperKZGPublicSetupOwned};
2-
use crate::base::{
3-
proof::{Keccak256Transcript, Transcript},
4-
slice_ops,
2+
use crate::{
3+
base::{
4+
proof::{Keccak256Transcript, Transcript},
5+
slice_ops,
6+
},
7+
proof_primitive::hyperkzg::convert_to_ark_bn254_g1_affine,
58
};
69
use nova_snark::{
710
errors::NovaError,
@@ -52,5 +55,5 @@ impl TranscriptEngineTrait<HyperKZGEngine> for Keccak256Transcript {
5255
pub fn nova_commitment_key_to_hyperkzg_public_setup(
5356
setup: &CommitmentKey<HyperKZGEngine>,
5457
) -> HyperKZGPublicSetupOwned {
55-
slice_ops::slice_cast_with(setup.ck(), blitzar::compute::convert_to_ark_bn254_g1_affine)
58+
slice_ops::slice_cast_with(setup.ck(), convert_to_ark_bn254_g1_affine)
5659
}

0 commit comments

Comments
 (0)