From aef9f5fb4597eca0da5a6786cdff816a39c4de54 Mon Sep 17 00:00:00 2001 From: Yuval Shekel Date: Wed, 4 Dec 2024 22:44:07 +0200 Subject: [PATCH] refine rust sumcheck to be generic in a nice way --- icicle/src/sumcheck/sumcheck_c_api.cpp | 1 + wrappers/rust/icicle-core/src/sumcheck/mod.rs | 70 ++++++++++++++++--- .../rust/icicle-core/src/sumcheck/tests.rs | 21 +++++- 3 files changed, 80 insertions(+), 12 deletions(-) diff --git a/icicle/src/sumcheck/sumcheck_c_api.cpp b/icicle/src/sumcheck/sumcheck_c_api.cpp index 084f30522..1ff43ccc6 100644 --- a/icicle/src/sumcheck/sumcheck_c_api.cpp +++ b/icicle/src/sumcheck/sumcheck_c_api.cpp @@ -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; } diff --git a/wrappers/rust/icicle-core/src/sumcheck/mod.rs b/wrappers/rust/icicle-core/src/sumcheck/mod.rs index bc16d6638..6cbd1ecc7 100644 --- a/wrappers/rust/icicle-core/src/sumcheck/mod.rs +++ b/wrappers/rust/icicle-core/src/sumcheck/mod.rs @@ -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, @@ -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 { + fn new(transcript_config: &SumcheckTranscriptConfig) -> Result, eIcicleError>; +} + +pub trait SumcheckOps { + // 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. @@ -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 + 'a, eIcicleError> + where + F: FieldImpl, + F::Config: SumcheckConstructor, + { + <::Config as SumcheckConstructor>::new(&transcript_config) + } +} #[macro_export] macro_rules! impl_sumcheck { @@ -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, } @@ -83,13 +110,28 @@ macro_rules! impl_sumcheck { fn create() -> SumcheckHandle; } - impl Sumcheck { - pub fn new(transcript_config: &SumcheckTranscriptConfig) -> Result { - unsafe { - // TODO real call - create(); - } - Err(eIcicleError::UnknownError) + impl SumcheckConstructor<$field> for $field_cfg { + fn new( + transcript_config: &SumcheckTranscriptConfig<$field>, + ) -> Result, 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 } } }; @@ -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) + } }; } diff --git a/wrappers/rust/icicle-core/src/sumcheck/tests.rs b/wrappers/rust/icicle-core/src/sumcheck/tests.rs index e9a521106..e403f7152 100644 --- a/wrappers/rust/icicle-core/src/sumcheck/tests.rs +++ b/wrappers/rust/icicle-core/src/sumcheck/tests.rs @@ -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(hash: &Hasher) @@ -40,3 +40,22 @@ where assert!(!config2.little_endian); assert_eq!(config2.seed_rng, seed_rng); } + +pub fn check_sumcheck_simple(hash: &Hasher) +where + ::Config: GenerateRandom + SumcheckConstructor, +{ + 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::(&config).unwrap(); + let proof = sumcheck.prove(); + println!("proof = {}", proof); + let _valid = sumcheck.verify(&proof); +}