Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mratsim authored Dec 5, 2023
1 parent 0a17002 commit eebe34e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion constantine-rust/constantine-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn main() {
.expect("failed to execute process");

println!("cargo:rustc-link-search=native={}", out_dir.display());

// On windows stable channel (msvc) expects constantine.lib
// while stable-gnu channel (gcc) expects libconstantine.a
// hence we use +verbatim
Expand Down
55 changes: 55 additions & 0 deletions constantine-rust/constantine-zal-halo2kzg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@ use ::core::mem::MaybeUninit;
use constantine_sys::*;
use halo2curves::bn256;
use halo2curves::zal::{MsmAccel, ZalEngine};
use halo2curves::CurveAffine;
use std::mem;

pub struct CttEngine {
ctx: *mut ctt_threadpool,
}
pub struct CttMsmCoeffsDesc<'c, C: CurveAffine> {
raw: &'c [C::Scalar],
}
pub struct CttMsmBaseDesc<'b, C: CurveAffine> {
raw: &'b [C],
}

impl CttEngine {
#[inline(always)]
Expand Down Expand Up @@ -50,6 +57,45 @@ impl MsmAccel<bn256::G1Affine> for CttEngine {
mem::transmute::<MaybeUninit<bn254_snarks_g1_prj>, bn256::G1>(result)
}
}

// Caching API
// -------------------------------------------------

type CoeffsDescriptor<'c> = CttMsmCoeffsDesc<'c, bn256::G1Affine>;
type BaseDescriptor<'b> = CttMsmBaseDesc<'b, bn256::G1Affine>;

fn get_coeffs_descriptor<'c>(&self, coeffs: &'c [bn256::Fr]) -> Self::CoeffsDescriptor<'c> {
// Do expensive device/library specific preprocessing here
Self::CoeffsDescriptor { raw: coeffs }
}
fn get_base_descriptor<'b>(&self, base: &'b [bn256::G1Affine]) -> Self::BaseDescriptor<'b> {
// Do expensive device/library specific preprocessing here
Self::BaseDescriptor { raw: base }
}

fn msm_with_cached_scalars(
&self,
coeffs: &Self::CoeffsDescriptor<'_>,
base: &[bn256::G1Affine],
) -> bn256::G1 {
self.msm(coeffs.raw, base)
}

fn msm_with_cached_base(
&self,
coeffs: &[bn256::Fr],
base: &Self::BaseDescriptor<'_>,
) -> bn256::G1 {
self.msm(coeffs, base.raw)
}

fn msm_with_cached_inputs(
&self,
coeffs: &Self::CoeffsDescriptor<'_>,
base: &Self::BaseDescriptor<'_>,
) -> bn256::G1 {
self.msm(coeffs.raw, base.raw)
}
}

#[cfg(test)]
Expand Down Expand Up @@ -98,6 +144,15 @@ mod tests {
end_timer!(t1);

assert_eq!(e0, e1);

// Caching API
// -----------
let t2 = start_timer!(|| format!("CttEngine msm cached base k={}", k));
let base_descriptor = engine.get_base_descriptor(points);
let e2 = engine.msm_with_cached_base(scalars, &base_descriptor);
end_timer!(t2);

assert_eq!(e0, e2)
}
}

Expand Down

0 comments on commit eebe34e

Please sign in to comment.