From 26043e6b1d28dc65ae0ea2063d6f8552c4e9a58c Mon Sep 17 00:00:00 2001 From: Olivier Desenfans Date: Thu, 23 May 2024 22:29:34 +0200 Subject: [PATCH] feat: load Cairo PIE from bytes (#1773) * feat: load Cairo PIE from bytes Context: bootloader support. Like for programs, it is convenient to be able to load Cairo PIEs directly from bytes. Added a `from_bytes()` method to `CairoPie`. This method shares most of its code with `read_from_file()`. * changelog --- CHANGELOG.md | 2 ++ vm/src/vm/runners/cairo_pie.rs | 24 +++++++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f8ca0a11c2..c8e35b7f08 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ * fix: make MemorySegmentManager.finalize() public [#1771](https://github.com/lambdaclass/cairo-vm/pull/1771) +* feat: load Cairo PIE from bytes [#1773](https://github.com/lambdaclass/cairo-vm/pull/1773) + * feat(BREAKING): Serialize `Array` return value into output segment in cairo1-run crate: * Checks that only `PanicResult>` or `Array` can be returned by the program when running with either `--proof_mode` or `--append_return_values`. * Serializes return values into the output segment under the previous conditions following the format: diff --git a/vm/src/vm/runners/cairo_pie.rs b/vm/src/vm/runners/cairo_pie.rs index 228f4048c6..8c5659e748 100644 --- a/vm/src/vm/runners/cairo_pie.rs +++ b/vm/src/vm/runners/cairo_pie.rs @@ -268,12 +268,10 @@ impl CairoPie { } #[cfg(feature = "std")] - pub fn read_zip_file(file_path: &Path) -> Result { + pub fn from_zip_archive( + mut zip_reader: zip::ZipArchive, + ) -> Result { use std::io::Read; - use zip::ZipArchive; - - let file = File::open(file_path)?; - let mut zip_reader = ZipArchive::new(file)?; let reader = std::io::BufReader::new(zip_reader.by_name("version.json")?); let version: CairoPieVersion = serde_json::from_reader(reader)?; @@ -300,6 +298,22 @@ impl CairoPie { version, }) } + + #[cfg(feature = "std")] + pub fn from_bytes(bytes: &[u8]) -> Result { + let reader = std::io::Cursor::new(bytes); + let zip_archive = zip::ZipArchive::new(reader)?; + + Self::from_zip_archive(zip_archive) + } + + #[cfg(feature = "std")] + pub fn read_zip_file(path: &Path) -> Result { + let file = File::open(path)?; + let zip = zip::ZipArchive::new(file)?; + + Self::from_zip_archive(zip) + } } pub(super) mod serde_impl {