Skip to content

Commit 26043e6

Browse files
authored
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
1 parent 5c5d1f2 commit 26043e6

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
* fix: make MemorySegmentManager.finalize() public [#1771](https://github.com/lambdaclass/cairo-vm/pull/1771)
1010

11+
* feat: load Cairo PIE from bytes [#1773](https://github.com/lambdaclass/cairo-vm/pull/1773)
12+
1113
* feat(BREAKING): Serialize `Array<Felt252>` return value into output segment in cairo1-run crate:
1214
* 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`.
1315
* Serializes return values into the output segment under the previous conditions following the format:

vm/src/vm/runners/cairo_pie.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,12 +268,10 @@ impl CairoPie {
268268
}
269269

270270
#[cfg(feature = "std")]
271-
pub fn read_zip_file(file_path: &Path) -> Result<CairoPie, std::io::Error> {
271+
pub fn from_zip_archive<R: std::io::Read + std::io::Seek>(
272+
mut zip_reader: zip::ZipArchive<R>,
273+
) -> Result<CairoPie, std::io::Error> {
272274
use std::io::Read;
273-
use zip::ZipArchive;
274-
275-
let file = File::open(file_path)?;
276-
let mut zip_reader = ZipArchive::new(file)?;
277275

278276
let reader = std::io::BufReader::new(zip_reader.by_name("version.json")?);
279277
let version: CairoPieVersion = serde_json::from_reader(reader)?;
@@ -300,6 +298,22 @@ impl CairoPie {
300298
version,
301299
})
302300
}
301+
302+
#[cfg(feature = "std")]
303+
pub fn from_bytes(bytes: &[u8]) -> Result<Self, std::io::Error> {
304+
let reader = std::io::Cursor::new(bytes);
305+
let zip_archive = zip::ZipArchive::new(reader)?;
306+
307+
Self::from_zip_archive(zip_archive)
308+
}
309+
310+
#[cfg(feature = "std")]
311+
pub fn read_zip_file(path: &Path) -> Result<Self, std::io::Error> {
312+
let file = File::open(path)?;
313+
let zip = zip::ZipArchive::new(file)?;
314+
315+
Self::from_zip_archive(zip)
316+
}
303317
}
304318

305319
pub(super) mod serde_impl {

0 commit comments

Comments
 (0)