Skip to content

Commit

Permalink
Merge pull request #430 from deltadevsde/comparmentalize-serialization
Browse files Browse the repository at this point in the history
refac: Comparmentalize Serialization Logic
  • Loading branch information
sragss authored Aug 13, 2024
2 parents 0cc7aa3 + 1a36633 commit 4805d03
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions jolt-core/src/jolt/vm/rv32i_vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,51 +176,53 @@ pub type RV32IJoltProof<F, CS> = JoltProof<C, M, F, CS, RV32I, RV32ISubtables<F>

use eyre::Result;
use std::fs::File;
use std::io::Cursor;
use std::path::PathBuf;

pub type PCS = HyraxScheme<G1Projective>;

#[derive(CanonicalSerialize, CanonicalDeserialize)]
pub struct RV32IHyraxProof {
pub proof: RV32IJoltProof<Fr, PCS>,
pub commitments: JoltCommitments<PCS>,
}

impl RV32IHyraxProof {
/// Gets the byte size of the full proof
pub fn size(&self) -> Result<usize> {
pub trait Serializable: CanonicalSerialize + CanonicalDeserialize + Sized {
/// Gets the byte size of the serialized data
fn size(&self) -> Result<usize> {
let mut buffer = Vec::new();
self.serialize_compressed(&mut buffer)?;
Ok(buffer.len())
}

/// Saves the proof to a file
pub fn save_to_file<P: Into<PathBuf>>(&self, path: P) -> Result<()> {
/// Saves the data to a file
fn save_to_file<P: Into<PathBuf>>(&self, path: P) -> Result<()> {
let file = File::create(path.into())?;
self.serialize_compressed(file)?;
Ok(())
}

/// Reads a proof from a file
pub fn from_file<P: Into<PathBuf>>(path: P) -> Result<Self> {
/// Reads data from a file
fn from_file<P: Into<PathBuf>>(path: P) -> Result<Self> {
let file = File::open(path.into())?;
Ok(RV32IHyraxProof::deserialize_compressed(file)?)
Ok(Self::deserialize_compressed(file)?)
}

/// Serializes the proof to a byte vector
pub fn serialize_to_bytes(&self) -> Result<Vec<u8>> {
/// Serializes the data to a byte vector
fn serialize_to_bytes(&self) -> Result<Vec<u8>> {
let mut buffer = Vec::new();
self.serialize_compressed(&mut buffer)?;
Ok(buffer)
}

/// Deserializes a proof from a byte vector
pub fn deserialize_from_bytes(bytes: &[u8]) -> Result<Self> {
let cursor = std::io::Cursor::new(bytes);
Ok(RV32IHyraxProof::deserialize_compressed(cursor)?)
/// Deserializes data from a byte vector
fn deserialize_from_bytes(bytes: &[u8]) -> Result<Self> {
let cursor = Cursor::new(bytes);
Ok(Self::deserialize_compressed(cursor)?)
}
}

#[derive(CanonicalSerialize, CanonicalDeserialize)]
pub struct RV32IHyraxProof {
pub proof: RV32IJoltProof<Fr, PCS>,
pub commitments: JoltCommitments<PCS>,
}

impl Serializable for RV32IHyraxProof {}

// ==================== TEST ====================

#[cfg(test)]
Expand Down

0 comments on commit 4805d03

Please sign in to comment.