Skip to content

Commit

Permalink
feat(halo2_proofs): bump up tachyon api version
Browse files Browse the repository at this point in the history
`from_params()` and `s_g2()` method was added to
`TachyonGWCProver` and `TachyonSHPlonkProver`.
  • Loading branch information
dongchangYoo committed Mar 21, 2024
1 parent a6f9e84 commit 4568fdb
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 1 deletion.
7 changes: 7 additions & 0 deletions halo2_proofs/include/bn254_gwc_prover.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace tachyon::halo2_api::bn254 {

struct Fr;
struct G2AffinePoint;
struct G1JacobianPoint;
struct InstanceSingle;
struct AdviceSingle;
Expand All @@ -23,6 +24,8 @@ class Poly;
class GWCProver {
public:
GWCProver(uint8_t transcript_type, uint32_t k, const Fr& s);
GWCProver(uint8_t transcript_type, uint32_t k, const uint8_t* params,
size_t params_len);
GWCProver(const GWCProver& other) = delete;
GWCProver& operator=(const GWCProver& other) = delete;
~GWCProver();
Expand All @@ -31,6 +34,7 @@ class GWCProver {

uint32_t k() const;
uint64_t n() const;
rust::Box<G2AffinePoint> s_g2() const;
rust::Box<G1JacobianPoint> commit(const Poly& poly) const;
rust::Box<G1JacobianPoint> commit_lagrange(const Evals& evals) const;
std::unique_ptr<Evals> empty_evals() const;
Expand All @@ -55,6 +59,9 @@ class GWCProver {
std::unique_ptr<GWCProver> new_gwc_prover(uint8_t transcript_type, uint32_t k,
const Fr& s);

std::unique_ptr<GWCProver> new_gwc_prover_from_params(
uint8_t transcript_type, uint32_t k, rust::Slice<const uint8_t> params);

} // namespace tachyon::halo2_api::bn254

#endif // HALO2_PROOFS_INCLUDE_BN254_GWC_PROVER_H_
7 changes: 7 additions & 0 deletions halo2_proofs/include/bn254_shplonk_prover.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace tachyon::halo2_api::bn254 {

struct Fr;
struct G2AffinePoint;
struct G1JacobianPoint;
struct InstanceSingle;
struct AdviceSingle;
Expand All @@ -23,6 +24,8 @@ class Poly;
class SHPlonkProver {
public:
SHPlonkProver(uint8_t transcript_type, uint32_t k, const Fr& s);
SHPlonkProver(uint8_t transcript_type, uint32_t k, const uint8_t* params,
size_t params_len);
SHPlonkProver(const SHPlonkProver& other) = delete;
SHPlonkProver& operator=(const SHPlonkProver& other) = delete;
~SHPlonkProver();
Expand All @@ -31,6 +34,7 @@ class SHPlonkProver {

uint32_t k() const;
uint64_t n() const;
rust::Box<G2AffinePoint> s_g2() const;
rust::Box<G1JacobianPoint> commit(const Poly& poly) const;
rust::Box<G1JacobianPoint> commit_lagrange(const Evals& evals) const;
std::unique_ptr<Evals> empty_evals() const;
Expand All @@ -55,6 +59,9 @@ class SHPlonkProver {
std::unique_ptr<SHPlonkProver> new_shplonk_prover(uint8_t transcript_type,
uint32_t k, const Fr& s);

std::unique_ptr<SHPlonkProver> new_shplonk_prover_from_params(
uint8_t transcript_type, uint32_t k, rust::Slice<const uint8_t> params);

} // namespace tachyon::halo2_api::bn254

#endif // HALO2_PROOFS_INCLUDE_BN254_SHPLONK_PROVER_H_
53 changes: 52 additions & 1 deletion halo2_proofs/src/bn254.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
},
};
use ff::{Field, PrimeField};
use halo2curves::{Coordinates, CurveAffine};
use halo2curves::{bn256::G2Affine, Coordinates, CurveAffine};

#[repr(C)]
#[derive(Debug)]
Expand All @@ -23,6 +23,20 @@ pub struct G1Point2 {
pub y: Fq,
}

#[repr(C)]
#[derive(Debug)]
pub struct Fq2 {
pub c0: Fq,
pub c1: Fq,
}

#[derive(Debug)]
pub struct G2AffinePoint {
pub x: Fq2,
pub y: Fq2,
pub infinity: bool,
}

#[repr(C)]
#[derive(Debug)]
pub struct G1JacobianPoint {
Expand Down Expand Up @@ -56,6 +70,7 @@ pub mod ffi {
extern "Rust" {
type G1JacobianPoint;
type G1Point2;
type G2AffinePoint;
type Fr;
type InstanceSingle;
type AdviceSingle;
Expand Down Expand Up @@ -151,8 +166,14 @@ pub mod ffi {
type GWCProver;

fn new_gwc_prover(transcript_type: u8, k: u32, s: &Fr) -> UniquePtr<GWCProver>;
fn new_gwc_prover_from_params(
transcript_type: u8,
k: u32,
params: &[u8],
) -> UniquePtr<GWCProver>;
fn k(&self) -> u32;
fn n(&self) -> u64;
fn s_g2(&self) -> Box<G2AffinePoint>;
fn commit(&self, poly: &Poly) -> Box<G1JacobianPoint>;
fn commit_lagrange(&self, evals: &Evals) -> Box<G1JacobianPoint>;
fn empty_evals(&self) -> UniquePtr<Evals>;
Expand Down Expand Up @@ -182,8 +203,14 @@ pub mod ffi {
type SHPlonkProver;

fn new_shplonk_prover(transcript_type: u8, k: u32, s: &Fr) -> UniquePtr<SHPlonkProver>;
fn new_shplonk_prover_from_params(
transcript_type: u8,
k: u32,
params: &[u8],
) -> UniquePtr<SHPlonkProver>;
fn k(&self) -> u32;
fn n(&self) -> u64;
fn s_g2(&self) -> Box<G2AffinePoint>;
fn commit(&self, poly: &Poly) -> Box<G1JacobianPoint>;
fn commit_lagrange(&self, evals: &Evals) -> Box<G1JacobianPoint>;
fn empty_evals(&self) -> UniquePtr<Evals>;
Expand Down Expand Up @@ -791,6 +818,8 @@ pub trait TachyonProver<Scheme: CommitmentScheme> {

fn n(&self) -> u64;

fn s_g2(&self) -> G2Affine;

fn commit(&self, poly: &Poly) -> <Scheme::Curve as CurveAffine>::CurveExt;

fn commit_lagrange(&self, evals: &Evals) -> <Scheme::Curve as CurveAffine>::CurveExt;
Expand Down Expand Up @@ -836,6 +865,13 @@ impl<Scheme: CommitmentScheme> GWCProver<Scheme> {
_marker: PhantomData,
}
}

pub fn from_params(transcript_type: u8, k: u32, params: &[u8]) -> GWCProver<Scheme> {
GWCProver {
inner: ffi::new_gwc_prover_from_params(transcript_type, k, params),
_marker: PhantomData,
}
}
}

impl<Scheme: CommitmentScheme> TachyonProver<Scheme> for GWCProver<Scheme> {
Expand All @@ -849,6 +885,10 @@ impl<Scheme: CommitmentScheme> TachyonProver<Scheme> for GWCProver<Scheme> {
self.inner.n()
}

fn s_g2(&self) -> G2Affine {
*unsafe { std::mem::transmute::<_, Box<G2Affine>>(self.inner.s_g2()) }
}

fn commit(&self, poly: &Poly) -> <Scheme::Curve as CurveAffine>::CurveExt {
*unsafe {
std::mem::transmute::<_, Box<<Scheme::Curve as CurveAffine>::CurveExt>>(
Expand Down Expand Up @@ -939,6 +979,13 @@ impl<Scheme: CommitmentScheme> SHPlonkProver<Scheme> {
_marker: PhantomData,
}
}

pub fn from_params(transcript_type: u8, k: u32, params: &[u8]) -> SHPlonkProver<Scheme> {
SHPlonkProver {
inner: ffi::new_shplonk_prover_from_params(transcript_type, k, params),
_marker: PhantomData,
}
}
}

impl<Scheme: CommitmentScheme> TachyonProver<Scheme> for SHPlonkProver<Scheme> {
Expand All @@ -952,6 +999,10 @@ impl<Scheme: CommitmentScheme> TachyonProver<Scheme> for SHPlonkProver<Scheme> {
self.inner.n()
}

fn s_g2(&self) -> G2Affine {
*unsafe { std::mem::transmute::<_, Box<G2Affine>>(self.inner.s_g2()) }
}

fn commit(&self, poly: &Poly) -> <Scheme::Curve as CurveAffine>::CurveExt {
*unsafe {
std::mem::transmute::<_, Box<<Scheme::Curve as CurveAffine>::CurveExt>>(
Expand Down
17 changes: 17 additions & 0 deletions halo2_proofs/src/bn254_gwc_prover.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ GWCProver::GWCProver(uint8_t transcript_type, uint32_t k, const Fr& s)
: prover_(tachyon_halo2_bn254_gwc_prover_create_from_unsafe_setup(
transcript_type, k, reinterpret_cast<const tachyon_bn254_fr*>(&s))) {}

GWCProver::GWCProver(uint8_t transcript_type, uint32_t k, const uint8_t* params,
size_t params_len)
: prover_(tachyon_halo2_bn254_gwc_prover_create_from_params(
transcript_type, k, params, params_len)) {}

GWCProver::~GWCProver() { tachyon_halo2_bn254_gwc_prover_destroy(prover_); }

uint32_t GWCProver::k() const {
Expand All @@ -21,6 +26,12 @@ uint64_t GWCProver::n() const {
return static_cast<uint64_t>(tachyon_halo2_bn254_gwc_prover_get_n(prover_));
}

rust::Box<G2AffinePoint> GWCProver::s_g2() const {
return rust::Box<G2AffinePoint>::from_raw(
reinterpret_cast<G2AffinePoint*>(new tachyon_bn254_g2_affine(
*tachyon_halo2_bn254_gwc_prover_get_s_g2(prover_))));
}

rust::Box<G1JacobianPoint> GWCProver::commit(const Poly& poly) const {
return rust::Box<G1JacobianPoint>::from_raw(
reinterpret_cast<G1JacobianPoint*>(
Expand Down Expand Up @@ -180,6 +191,12 @@ std::unique_ptr<GWCProver> new_gwc_prover(uint8_t transcript_type, uint32_t k,
return std::make_unique<GWCProver>(transcript_type, k, s);
}

std::unique_ptr<GWCProver> new_gwc_prover_from_params(
uint8_t transcript_type, uint32_t k, rust::Slice<const uint8_t> params) {
return std::make_unique<GWCProver>(transcript_type, k, params.data(),
params.size());
}

rust::Box<Fr> ProvingKey::transcript_repr_gwc(const GWCProver& prover) {
tachyon_halo2_bn254_gwc_prover_set_transcript_repr(prover.prover(), pk_);
tachyon_bn254_fr* ret = new tachyon_bn254_fr;
Expand Down
17 changes: 17 additions & 0 deletions halo2_proofs/src/bn254_shplonk_prover.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ SHPlonkProver::SHPlonkProver(uint8_t transcript_type, uint32_t k, const Fr& s)
: prover_(tachyon_halo2_bn254_shplonk_prover_create_from_unsafe_setup(
transcript_type, k, reinterpret_cast<const tachyon_bn254_fr*>(&s))) {}

SHPlonkProver::SHPlonkProver(uint8_t transcript_type, uint32_t k,
const uint8_t* params, size_t params_len)
: prover_(tachyon_halo2_bn254_shplonk_prover_create_from_params(
transcript_type, k, params, params_len)) {}

SHPlonkProver::~SHPlonkProver() {
tachyon_halo2_bn254_shplonk_prover_destroy(prover_);
}
Expand All @@ -24,6 +29,12 @@ uint64_t SHPlonkProver::n() const {
tachyon_halo2_bn254_shplonk_prover_get_n(prover_));
}

rust::Box<G2AffinePoint> SHPlonkProver::s_g2() const {
return rust::Box<G2AffinePoint>::from_raw(
reinterpret_cast<G2AffinePoint*>(new tachyon_bn254_g2_affine(
*tachyon_halo2_bn254_shplonk_prover_get_s_g2(prover_))));
}

rust::Box<G1JacobianPoint> SHPlonkProver::commit(const Poly& poly) const {
return rust::Box<G1JacobianPoint>::from_raw(
reinterpret_cast<G1JacobianPoint*>(
Expand Down Expand Up @@ -184,6 +195,12 @@ std::unique_ptr<SHPlonkProver> new_shplonk_prover(uint8_t transcript_type,
return std::make_unique<SHPlonkProver>(transcript_type, k, s);
}

std::unique_ptr<SHPlonkProver> new_shplonk_prover_from_params(
uint8_t transcript_type, uint32_t k, rust::Slice<const uint8_t> params) {
return std::make_unique<SHPlonkProver>(transcript_type, k, params.data(),
params.size());
}

rust::Box<Fr> ProvingKey::transcript_repr_shplonk(const SHPlonkProver& prover) {
tachyon_halo2_bn254_shplonk_prover_set_transcript_repr(prover.prover(), pk_);
tachyon_bn254_fr* ret = new tachyon_bn254_fr;
Expand Down

0 comments on commit 4568fdb

Please sign in to comment.