Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/kzg_keygen_tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use nova_snark::{
hyperkzg::{CommitmentEngine, CommitmentKey},
Bn256EngineKZG,
},
traits::commitment::CommitmentEngineTrait,
traits::commitment::{CommitmentEngineTrait, CommitmentKeyFileTrait},
};
use rand_core::OsRng;

Expand Down
15 changes: 12 additions & 3 deletions src/provider/hyperkzg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ use crate::{
errors::NovaError,
gadgets::utils::to_bignat_repr,
provider::{
check_sanity_of_ptau_file,
ptau::PtauFileError,
read_ptau,
traits::{DlogGroup, DlogGroupExt, PairingGroup},
write_ptau,
},
traits::{
commitment::{CommitmentEngineTrait, CommitmentTrait, Len},
commitment::{CommitmentEngineTrait, CommitmentKeyFileTrait, CommitmentTrait, Len},
evaluation::EvaluationEngineTrait,
AbsorbInRO2Trait, AbsorbInROTrait, Engine, ROTrait, TranscriptEngineTrait, TranscriptReprTrait,
},
Expand Down Expand Up @@ -135,12 +136,12 @@ where
}
}

impl<E: Engine> CommitmentKey<E>
impl<E: Engine> CommitmentKeyFileTrait for CommitmentKey<E>
where
E::GE: PairingGroup,
{
/// Save keys
pub fn save_to(
fn save_to(
&self,
mut writer: &mut (impl std::io::Write + std::io::Seek),
) -> Result<(), PtauFileError> {
Expand All @@ -151,6 +152,14 @@ where

write_ptau(&mut writer, g1_points, g2_points, power)
}

fn check_sanity_of_key_file(
path: impl AsRef<std::path::Path>,
num_g1: usize,
num_g2: usize,
) -> Result<(), PtauFileError> {
check_sanity_of_ptau_file::<<E::GE as DlogGroup>::AffineGroupElement>(&path, num_g1, num_g2)
}
}

impl<E: Engine> Default for Commitment<E>
Expand Down
22 changes: 19 additions & 3 deletions src/provider/pedersen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
traits::{DlogGroup, DlogGroupExt},
},
traits::{
commitment::{CommitmentEngineTrait, CommitmentTrait, Len},
commitment::{CommitmentEngineTrait, CommitmentKeyFileTrait, CommitmentTrait, Len},
AbsorbInRO2Trait, AbsorbInROTrait, Engine, ROTrait, TranscriptReprTrait,
},
};
Expand All @@ -21,6 +21,7 @@ use num_integer::Integer;
use num_traits::ToPrimitive;
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use std::{fs::File, io::Read};

const KEY_FILE_HEAD: [u8; 12] = *b"PEDERSEN_KEY";

Expand Down Expand Up @@ -188,17 +189,32 @@ pub struct CommitmentEngine<E: Engine> {
_p: PhantomData<E>,
}

impl<E: Engine> CommitmentKey<E>
impl<E: Engine> CommitmentKeyFileTrait for CommitmentKey<E>
where
E::GE: DlogGroup,
{
pub fn save_to(&self, writer: &mut impl std::io::Write) -> Result<(), PtauFileError> {
fn save_to(&self, writer: &mut impl std::io::Write) -> Result<(), PtauFileError> {
writer.write_all(&KEY_FILE_HEAD)?;
let mut points = Vec::with_capacity(self.ck.len() + 1);
points.push(self.h);
points.extend(self.ck.iter().cloned());
write_points(writer, points)
}

fn check_sanity_of_key_file(
path: impl AsRef<std::path::Path>,
_num_g1: usize,
_num_g2: usize,
) -> Result<(), PtauFileError> {
let mut reader = File::open(path)?;
let mut head = [0u8; 12];
reader.read_exact(&mut head)?;
if head != KEY_FILE_HEAD {
Err(PtauFileError::InvalidHead)
} else {
Ok(())
}
}
}

impl<E: Engine> CommitmentEngineTrait<E> for CommitmentEngine<E>
Expand Down
26 changes: 25 additions & 1 deletion src/traits/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use num_integer::Integer;
use num_traits::ToPrimitive;
use rayon::iter::{IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator};
use serde::{Deserialize, Serialize};
use std::path::Path;

/// A helper trait for types implementing scalar multiplication.
pub trait ScalarMul<Rhs, Output = Self>: Mul<Rhs, Output = Output> + MulAssign<Rhs> {}
Expand Down Expand Up @@ -48,11 +49,34 @@ pub trait Len {
fn length(&self) -> usize;
}

/// A trait for saving key to a writer and checking sanity of key file
pub trait CommitmentKeyFileTrait {
/// Saves the key to the provided writer.
fn save_to(
&self,
writer: &mut (impl std::io::Write + std::io::Seek),
) -> Result<(), PtauFileError>;

/// Checks the sanity of the key file at the provided path.
fn check_sanity_of_key_file(
path: impl AsRef<Path>,
num_g1: usize,
num_g2: usize,
) -> Result<(), PtauFileError>;
}

/// A trait that ties different pieces of the commitment generation together
pub trait CommitmentEngineTrait<E: Engine>: Clone + Send + Sync {
/// Holds the type of the commitment key
/// The key should quantify its length in terms of group generators.
type CommitmentKey: Len + Clone + Debug + Send + Sync + Serialize + for<'de> Deserialize<'de>;
type CommitmentKey: Len
+ CommitmentKeyFileTrait
+ Clone
+ Debug
+ Send
+ Sync
+ Serialize
+ for<'de> Deserialize<'de>;

/// Holds the type of the derandomization key
type DerandKey: Clone + Debug + Send + Sync + Serialize + for<'de> Deserialize<'de>;
Expand Down