Skip to content

Commit

Permalink
feat: load Cairo PIE from bytes (#1773)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
odesenfans committed May 23, 2024
1 parent 5c5d1f2 commit 26043e6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Felt252>` return value into output segment in cairo1-run crate:
* Checks that only `PanicResult<Array<Felt252>>` or `Array<Felt252>` 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:
Expand Down
24 changes: 19 additions & 5 deletions vm/src/vm/runners/cairo_pie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,10 @@ impl CairoPie {
}

#[cfg(feature = "std")]
pub fn read_zip_file(file_path: &Path) -> Result<CairoPie, std::io::Error> {
pub fn from_zip_archive<R: std::io::Read + std::io::Seek>(
mut zip_reader: zip::ZipArchive<R>,
) -> Result<CairoPie, std::io::Error> {
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)?;
Expand All @@ -300,6 +298,22 @@ impl CairoPie {
version,
})
}

#[cfg(feature = "std")]
pub fn from_bytes(bytes: &[u8]) -> Result<Self, std::io::Error> {
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<Self, std::io::Error> {
let file = File::open(path)?;
let zip = zip::ZipArchive::new(file)?;

Self::from_zip_archive(zip)
}
}

pub(super) mod serde_impl {
Expand Down

0 comments on commit 26043e6

Please sign in to comment.