From 813b34015b920dd2d614e4c336841084a9350dad Mon Sep 17 00:00:00 2001 From: Denys Zadorozhnyi Date: Tue, 19 Nov 2024 13:26:51 +0200 Subject: [PATCH] refactor: remove `Package::rodata`, swap `Package::digest` with `digest()` --- package/src/lib.rs | 2 -- package/src/package.rs | 38 +++++------------------- package/src/rodata.rs | 67 ------------------------------------------ 3 files changed, 7 insertions(+), 100 deletions(-) delete mode 100644 package/src/rodata.rs diff --git a/package/src/lib.rs b/package/src/lib.rs index 376d6fcb1..b9699c753 100644 --- a/package/src/lib.rs +++ b/package/src/lib.rs @@ -11,7 +11,6 @@ extern crate alloc; mod de; mod dep; mod package; -mod rodata; mod se; #[cfg(test)] @@ -28,7 +27,6 @@ pub use self::{ Dependency, DependencyName, SystemLibraryId, }, package::{MastArtifact, Package, PackageExport, PackageManifest}, - rodata::{PtrDesc, Rodata}, }; type Digest = vm_core::chiplets::hasher::Digest; diff --git a/package/src/package.rs b/package/src/package.rs index efc94a7f2..91972697a 100644 --- a/package/src/package.rs +++ b/package/src/package.rs @@ -1,18 +1,12 @@ -use alloc::{ - collections::{BTreeMap, BTreeSet}, - format, - string::String, - sync::Arc, - vec::Vec, -}; +use alloc::{collections::BTreeSet, format, string::String, sync::Arc, vec::Vec}; use core::fmt; use assembly::{ast::QualifiedProcedureName, Library, Report}; use serde::{Deserialize, Serialize}; -use vm_core::{mast::MastForest, utils::DisplayHex, Felt, Program}; +use vm_core::{mast::MastForest, utils::DisplayHex, Program}; use super::{de, se}; -use crate::{Dependency, Digest, Rodata}; +use crate::{Dependency, Digest}; // MAST ARTIFACT // ================================================================================================ @@ -113,18 +107,9 @@ impl fmt::Debug for PackageExport { pub struct Package { /// Name of the package pub name: String, - /// Content digest of the package - #[serde( - serialize_with = "se::serialize_digest", - deserialize_with = "de::deserialize_digest" - )] - pub digest: Digest, /// The MAST artifact ([Program] or [Library]) of the package #[serde(serialize_with = "se::serialize_mast", deserialize_with = "de::deserialize_mast")] pub mast: MastArtifact, - /// The rodata segments required to be loaded in the advice provider before executing the code - /// in this package - pub rodata: Vec, /// The package manifest, containing the set of exported procedures and their signatures, /// if known. pub manifest: PackageManifest, @@ -166,6 +151,10 @@ impl Package { Ok(bytes) } + pub fn digest(&self) -> Digest { + self.mast.digest() + } + /// Checks if the package's MAST artifact is a [Program] pub fn is_program(&self) -> bool { matches!(self.mast, MastArtifact::Executable(_)) @@ -226,12 +215,10 @@ impl Package { Ok(Self { name: self.name.clone(), - digest, mast: MastArtifact::Executable(Arc::new(Program::new( library.mast_forest().clone(), node_id, ))), - rodata: self.rodata.clone(), manifest: PackageManifest { exports, dependencies: self.manifest.dependencies.clone(), @@ -244,15 +231,4 @@ impl Package { ))) } } - - /// The advice map that is expected to be loaded into the advice provider so that the - /// program's compiler-generated prologue will load the rodata from the advice provider into - /// memory before the program is executed - pub fn advice_map(&self) -> BTreeMap> { - let mut advice_inputs = BTreeMap::default(); - for rodata in self.rodata.iter() { - advice_inputs.extend([(rodata.digest, rodata.to_elements())]); - } - advice_inputs - } } diff --git a/package/src/rodata.rs b/package/src/rodata.rs deleted file mode 100644 index fdb5d3d42..000000000 --- a/package/src/rodata.rs +++ /dev/null @@ -1,67 +0,0 @@ -use alloc::vec::Vec; - -use serde::{Deserialize, Serialize}; -use vm_core::{utils::ToElements, Felt, FieldElement}; - -use super::{de, se}; -use crate::Digest; - -/// This represents a descriptor for a pointer referencing data in Miden's linear memory. -#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct PtrDesc { - /// This is the address of the word containing the first byte of data - pub waddr: u32, - /// This is the element index of the word referenced by `waddr` containing the first byte of - /// data - /// - /// Each element is assumed to be a 32-bit value/chunk - pub index: u8, - /// This is the byte offset into the 32-bit chunk referenced by `index` - /// - /// This offset is where the data referenced by the pointer actually starts. - pub offset: u8, -} - -/// Represents a read-only data segment, combined with its content digest -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct Rodata { - /// The content digest computed for `data` - #[serde( - serialize_with = "se::serialize_digest", - deserialize_with = "de::deserialize_digest" - )] - pub digest: Digest, - /// The address at which the data for this segment begins - pub start: PtrDesc, - /// The raw binary data for this segment - pub data: Vec, -} - -impl Rodata { - /// Returns the size of the data in bytes - pub fn size_in_bytes(&self) -> usize { - self.data.len() - } - - /// Returns the size of the data in felts - pub fn size_in_felts(&self) -> usize { - self.data.len().next_multiple_of(4) / 4 - } - - /// Returns the size of the data in VM words - pub fn size_in_words(&self) -> usize { - self.size_in_felts().next_multiple_of(4) / 4 - } - - /// Converts this rodata object to its equivalent representation in felts - /// - /// The resulting felts will be in padded out to the nearest number of words, i.e. if the data - /// only takes up 3 felts worth of bytes, then the resulting `Vec` will contain 4 felts, so that - /// the total size is a valid number of words. - pub fn to_elements(&self) -> Vec { - let mut felts = self.data.as_slice().to_elements(); - let padding = (self.size_in_words() * 4).abs_diff(felts.len()); - felts.resize(felts.len() + padding, Felt::ZERO); - felts - } -}