Provide methods for serializing Kernel

This commit is contained in:
Robin Salen 2023-04-15 10:45:56 -04:00
parent 6b2503f778
commit df5a90cca4
No known key found for this signature in database
GPG Key ID: FB87BACFB3CB2007
3 changed files with 16 additions and 2 deletions

View File

@ -36,6 +36,7 @@ serde = { version = "1.0.144", features = ["derive"] }
static_assertions = "1.1.0"
hashbrown = { version = "0.12.3" }
tiny-keccak = "2.0.2"
serde_json = "1.0"
[target.'cfg(not(target_env = "msvc"))'.dependencies]
jemallocator = "0.5.0"

View File

@ -1,10 +1,12 @@
use std::collections::HashMap;
use std::fs;
use std::time::Instant;
use ethereum_types::U256;
use itertools::{izip, Itertools};
use keccak_hash::keccak;
use log::debug;
use serde::{Deserialize, Serialize};
use super::ast::PushTarget;
use crate::cpu::kernel::ast::Item::LocalLabelDeclaration;
@ -20,7 +22,7 @@ use crate::generation::prover_input::ProverInputFn;
/// nontrivial given the circular dependency between an offset and its size.
pub(crate) const BYTES_PER_OFFSET: u8 = 3;
#[derive(PartialEq, Eq, Debug)]
#[derive(PartialEq, Eq, Debug, Serialize, Deserialize)]
pub struct Kernel {
pub(crate) code: Vec<u8>,
@ -60,6 +62,16 @@ impl Kernel {
}
}
pub fn to_file(&self, path: &str) {
let kernel_serialized = serde_json::to_string(self).unwrap();
fs::write(path, kernel_serialized).expect("Unable to write kernel to file");
}
pub fn from_file(path: &str) -> Self {
let bytes = fs::read(path).expect("Unable to read kernel file");
serde_json::from_slice(&bytes).unwrap()
}
/// Get a string representation of the current offset for debugging purposes.
pub(crate) fn offset_name(&self, offset: usize) -> String {
match self

View File

@ -5,6 +5,7 @@ use anyhow::{bail, Error};
use ethereum_types::{BigEndianHash, H256, U256, U512};
use itertools::Itertools;
use plonky2::field::types::Field;
use serde::{Deserialize, Serialize};
use crate::extension_tower::{FieldExt, Fp12, BLS381, BN254};
use crate::generation::prover_input::EvmField::{
@ -19,7 +20,7 @@ use crate::witness::util::{kernel_peek, stack_peek};
/// Prover input function represented as a scoped function name.
/// Example: `PROVER_INPUT(ff::bn254_base::inverse)` is represented as `ProverInputFn([ff, bn254_base, inverse])`.
#[derive(PartialEq, Eq, Debug, Clone)]
#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)]
pub struct ProverInputFn(Vec<String>);
impl From<Vec<String>> for ProverInputFn {