Skip to content

Commit

Permalink
refine rust sumcheck to be generic in a nice way
Browse files Browse the repository at this point in the history
  • Loading branch information
yshekel committed Dec 4, 2024
1 parent 5327c33 commit aef9f5f
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 12 deletions.
1 change: 1 addition & 0 deletions icicle/src/sumcheck/sumcheck_c_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace icicle {

SumcheckHandle* CONCAT_EXPAND(FIELD, sumcheck_create)()
{
ICICLE_LOG_INFO << "hello world";
// TODO Yuval update params and implement
return nullptr;
}
Expand Down
70 changes: 59 additions & 11 deletions wrappers/rust/icicle-core/src/sumcheck/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
pub mod tests;

use crate::hash::Hasher;
use crate::traits::FieldImpl;
use icicle_runtime::eIcicleError;

pub struct SumcheckTranscriptConfig<'a, S> {
pub hash: &'a Hasher,
Expand All @@ -11,6 +13,21 @@ pub struct SumcheckTranscriptConfig<'a, S> {
pub little_endian: bool,
pub seed_rng: S,
}
// This trait is implemented on FieldConfig to enable Sumcheck struct to create a sumcheck prover
pub trait SumcheckConstructor<F> {
fn new(transcript_config: &SumcheckTranscriptConfig<F>) -> Result<impl SumcheckOps<F>, eIcicleError>;
}

pub trait SumcheckOps<F> {
// TODO replace with sumcheck proof type
fn prove(&self) -> String;
fn verify(&self, proof: &str) -> bool;
}

// This struct is used simply to construct a sumcheck instance that implements sumcheck ops in a generic way
pub struct Sumcheck;

/*******************/

impl<'a, S> SumcheckTranscriptConfig<'a, S> {
/// Constructor for `SumcheckTranscriptConfig` with explicit parameters.
Expand Down Expand Up @@ -58,7 +75,17 @@ impl<'a, S> SumcheckTranscriptConfig<'a, S> {
}
}

// TODO Yuval: add a Sumcheck trait and implement it in the macro per field
impl Sumcheck {
fn new<'a, F: FieldImpl>(
transcript_config: &'a SumcheckTranscriptConfig<'a, F>,
) -> Result<impl SumcheckOps<F> + 'a, eIcicleError>
where
F: FieldImpl,
F::Config: SumcheckConstructor<F>,
{
<<F as FieldImpl>::Config as SumcheckConstructor<F>>::new(&transcript_config)
}
}

#[macro_export]
macro_rules! impl_sumcheck {
Expand All @@ -68,13 +95,13 @@ macro_rules! impl_sumcheck {
$field:ident,
$field_cfg:ident
) => {
use icicle_core::sumcheck::SumcheckTranscriptConfig;
use icicle_core::sumcheck::{SumcheckConstructor, SumcheckOps, SumcheckTranscriptConfig};
use icicle_core::traits::FieldImpl;
use icicle_runtime::eIcicleError;
use std::ffi::c_void;

type SumcheckHandle = *const c_void;
pub struct Sumcheck {
pub type SumcheckHandle = *const c_void;
pub struct SumcheckInternal {
handle: SumcheckHandle,
}

Expand All @@ -83,13 +110,28 @@ macro_rules! impl_sumcheck {
fn create() -> SumcheckHandle;
}

impl Sumcheck {
pub fn new<S: FieldImpl>(transcript_config: &SumcheckTranscriptConfig<S>) -> Result<Self, eIcicleError> {
unsafe {
// TODO real call
create();
}
Err(eIcicleError::UnknownError)
impl SumcheckConstructor<$field> for $field_cfg {
fn new(
transcript_config: &SumcheckTranscriptConfig<$field>,
) -> Result<impl SumcheckOps<$field>, eIcicleError> {
let handle: SumcheckHandle = unsafe {
// TODO add params
create()
};
// if handle.is_null() {
// return Err(eIcicleError::UnknownError);
// }
Ok(SumcheckInternal { handle })
}
}

impl SumcheckOps<$field> for SumcheckInternal {
fn prove(&self) -> String {
String::from("hello")
}

fn verify(&self, _: &str) -> bool {
true
}
}
};
Expand All @@ -106,5 +148,11 @@ macro_rules! impl_sumcheck_tests {
let hash = Keccak256::new(0).unwrap();
check_sumcheck_transcript_config::<$field>(&hash)
}

#[test]
fn test_sumcheck_simple() {
let hash = Keccak256::new(0).unwrap();
check_sumcheck_simple::<$field>(&hash)
}
};
}
21 changes: 20 additions & 1 deletion wrappers/rust/icicle-core/src/sumcheck/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::hash::Hasher;
use crate::sumcheck::SumcheckTranscriptConfig;
use crate::sumcheck::{Sumcheck, SumcheckConstructor, SumcheckOps, SumcheckTranscriptConfig};
use crate::traits::{FieldImpl, GenerateRandom};

pub fn check_sumcheck_transcript_config<F: FieldImpl>(hash: &Hasher)
Expand Down Expand Up @@ -40,3 +40,22 @@ where
assert!(!config2.little_endian);
assert_eq!(config2.seed_rng, seed_rng);
}

pub fn check_sumcheck_simple<F: FieldImpl>(hash: &Hasher)
where
<F as FieldImpl>::Config: GenerateRandom<F> + SumcheckConstructor<F>,
{
let config = SumcheckTranscriptConfig::new(
hash,
b"DomainLabel".to_vec(),
b"PolyLabel".to_vec(),
b"ChallengeLabel".to_vec(),
true, // little endian
F::zero(),
);

let sumcheck = Sumcheck::new::<F>(&config).unwrap();
let proof = sumcheck.prove();
println!("proof = {}", proof);
let _valid = sumcheck.verify(&proof);
}

0 comments on commit aef9f5f

Please sign in to comment.