Skip to content

Commit

Permalink
feat: introduce no_std compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
tdelabro committed Feb 10, 2023
1 parent 6c25adf commit 61c9f64
Show file tree
Hide file tree
Showing 65 changed files with 163 additions and 81 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ description = "Blazing fast Cairo interpreter"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = ["with_mimalloc"]
default = ["with_mimalloc", "std"]
with_mimalloc = ["mimalloc"]
skip_next_instruction_hint = []
# This feature will reference every test-oriented feature.
# Note that these features are not retro-compatible with the cairo Python VM.
test_utils = ["skip_next_instruction_hint"]
std = ["no-std-compat/std", "felt/std"]

[dependencies]
mimalloc = { version = "0.1.29", default-features = false, optional = true }
Expand Down Expand Up @@ -44,7 +45,13 @@ keccak = "0.1.2"
# There is a proposal for extending nom::delimited to use this function:
# https://github.com/Geal/nom/issues/1253
parse-hyperlinks = { path = "./deps/parse-hyperlinks", version = "0.23.4" }
felt = { package = "cairo-felt", path = "./felt", version = "0.1.0" }
felt = { package = "cairo-felt", path = "./felt", default-features = false }
no-std-compat = { version = "0.4.1", features = [
"alloc",
"compat_macros",
"std",
] }


[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
wasm-bindgen-test = "0.3.34"
Expand Down
2 changes: 2 additions & 0 deletions src/cairo_run.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::prelude::v1::*;

use crate::{
hint_processor::hint_processor_definition::HintProcessor,
types::program::Program,
Expand Down
2 changes: 2 additions & 0 deletions src/hint_processor/builtin_hint_processor/blake2s_hash.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::prelude::v1::*;

use std::ops::Shl;

pub const IV: [u32; 8] = [
Expand Down
2 changes: 2 additions & 0 deletions src/hint_processor/builtin_hint_processor/blake2s_utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::prelude::v1::*;

use crate::{
hint_processor::{
builtin_hint_processor::{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::prelude::v1::*;

use crate::{
hint_processor::{
builtin_hint_processor::{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::prelude::v1::*;

use crate::{
hint_processor::{
builtin_hint_processor::hint_utils::{
Expand Down
5 changes: 4 additions & 1 deletion src/hint_processor/builtin_hint_processor/dict_hint_utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use std::prelude::v1::*;

use std::{any::Any, cell::RefCell, collections::HashMap, rc::Rc};

use crate::{
types::{exec_scope::ExecutionScopes, relocatable::MaybeRelocatable},
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine},
};
use std::{any::Any, cell::RefCell, collections::HashMap, rc::Rc};

use crate::{
any_box,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::prelude::v1::*;

use crate::{
hint_processor::{
builtin_hint_processor::hint_utils::{
Expand Down
2 changes: 2 additions & 0 deletions src/hint_processor/builtin_hint_processor/hint_utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::prelude::v1::*;

use felt::Felt;

use crate::hint_processor::hint_processor_definition::HintReference;
Expand Down
2 changes: 2 additions & 0 deletions src/hint_processor/builtin_hint_processor/keccak_utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::prelude::v1::*;

use crate::{
hint_processor::{
builtin_hint_processor::hint_utils::{
Expand Down
2 changes: 2 additions & 0 deletions src/hint_processor/builtin_hint_processor/math_utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::prelude::v1::*;

use crate::{
any_box,
hint_processor::{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::prelude::v1::*;

use crate::{
hint_processor::{
builtin_hint_processor::hint_utils::{
Expand Down
2 changes: 2 additions & 0 deletions src/hint_processor/builtin_hint_processor/memset_utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::prelude::v1::*;

use crate::{
hint_processor::{
builtin_hint_processor::hint_utils::{
Expand Down
2 changes: 2 additions & 0 deletions src/hint_processor/builtin_hint_processor/pow_utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::prelude::v1::*;

use crate::{
hint_processor::{
builtin_hint_processor::hint_utils::{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::prelude::v1::*;

use crate::{
hint_processor::{
builtin_hint_processor::{
Expand Down
2 changes: 2 additions & 0 deletions src/hint_processor/builtin_hint_processor/secp/ec_utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::prelude::v1::*;

use crate::{
hint_processor::{
builtin_hint_processor::{
Expand Down
2 changes: 2 additions & 0 deletions src/hint_processor/builtin_hint_processor/secp/field_utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::prelude::v1::*;

use super::secp_utils::pack_from_var_name;
use crate::{
hint_processor::{
Expand Down
2 changes: 2 additions & 0 deletions src/hint_processor/builtin_hint_processor/secp/secp_utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::prelude::v1::*;

use crate::{
hint_processor::{
builtin_hint_processor::hint_utils::get_relocatable_from_var_name,
Expand Down
2 changes: 2 additions & 0 deletions src/hint_processor/builtin_hint_processor/secp/signature.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::prelude::v1::*;

use crate::{
hint_processor::{
builtin_hint_processor::{
Expand Down
2 changes: 2 additions & 0 deletions src/hint_processor/builtin_hint_processor/segments.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::prelude::v1::*;

use crate::hint_processor::builtin_hint_processor::hint_utils::get_ptr_from_var_name;
use crate::hint_processor::{
builtin_hint_processor::hint_utils::insert_value_from_var_name,
Expand Down
2 changes: 2 additions & 0 deletions src/hint_processor/builtin_hint_processor/set.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::prelude::v1::*;

use crate::{
hint_processor::{
builtin_hint_processor::hint_utils::{
Expand Down
2 changes: 2 additions & 0 deletions src/hint_processor/builtin_hint_processor/sha256_utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::prelude::v1::*;

use crate::{
hint_processor::{
builtin_hint_processor::hint_utils::{
Expand Down
2 changes: 2 additions & 0 deletions src/hint_processor/builtin_hint_processor/signature.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::prelude::v1::*;

use crate::{
hint_processor::{
builtin_hint_processor::hint_utils::{get_integer_from_var_name, get_ptr_from_var_name},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::prelude::v1::*;

use crate::{
hint_processor::{
builtin_hint_processor::{
Expand Down
2 changes: 2 additions & 0 deletions src/hint_processor/builtin_hint_processor/uint256_utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::prelude::v1::*;

use crate::{
hint_processor::builtin_hint_processor::hint_utils::{
get_integer_from_var_name, get_relocatable_from_var_name, insert_value_from_var_name,
Expand Down
2 changes: 2 additions & 0 deletions src/hint_processor/builtin_hint_processor/usort.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::prelude::v1::*;

use crate::{
hint_processor::{
builtin_hint_processor::hint_utils::{
Expand Down
2 changes: 2 additions & 0 deletions src/hint_processor/hint_processor_definition.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::prelude::v1::*;

use crate::any_box;
use crate::serde::deserialize_program::ApTracking;
use crate::serde::deserialize_program::OffsetValue;
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#![deny(warnings)]
#![cfg_attr(any(target_arch = "wasm32", not(feature = "std")), no_std)]
extern crate no_std_compat as std;

pub mod cairo_run;
pub mod hint_processor;
pub mod math_utils;
Expand Down
63 changes: 2 additions & 61 deletions src/serde/deserialize_program.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::prelude::v1::*;

use crate::{
serde::deserialize_utils,
types::{
Expand Down Expand Up @@ -379,9 +381,6 @@ mod tests {
use felt::felt_str;
use num_traits::Zero;

#[cfg(not(target_arch = "wasm32"))]
use std::{fs::File, io::BufReader};

#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;

Expand Down Expand Up @@ -658,12 +657,6 @@ mod tests {
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn deserialize_program_json_from_json_file_a() {
// Open json file with (valid) even length encoded hex
#[cfg(not(target_arch = "wasm32"))]
let reader = {
let file = File::open("cairo_programs/manually_compiled/valid_program_a.json").unwrap();
BufReader::new(file)
};
#[cfg(target_arch = "wasm32")]
let reader =
include_str!("../../cairo_programs/manually_compiled/valid_program_a.json").as_bytes();

Expand All @@ -683,12 +676,6 @@ mod tests {
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn deserialize_program_json_from_json_file_b() {
// Open json file with (valid) odd length encoded hex
#[cfg(not(target_arch = "wasm32"))]
let reader = {
let file = File::open("cairo_programs/manually_compiled/valid_program_b.json").unwrap();
BufReader::new(file)
};
#[cfg(target_arch = "wasm32")]
let reader =
include_str!("../../cairo_programs/manually_compiled/valid_program_b.json").as_bytes();

Expand All @@ -708,13 +695,6 @@ mod tests {
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn deserialize_program_json_from_json_file_gives_error() {
// Open json file with (invalid) even length encoded hex
#[cfg(not(target_arch = "wasm32"))]
let reader = {
let file = File::open("cairo_programs/manually_compiled/invalid_even_length_hex.json")
.unwrap();
BufReader::new(file)
};
#[cfg(target_arch = "wasm32")]
let reader =
include_str!("../../cairo_programs/manually_compiled/invalid_even_length_hex.json")
.as_bytes();
Expand All @@ -724,13 +704,6 @@ mod tests {
assert!(even_result.is_err());

// Open json file with (invalid) odd length encoded hex
#[cfg(not(target_arch = "wasm32"))]
let reader = {
let file =
File::open("cairo_programs/manually_compiled/invalid_odd_length_hex.json").unwrap();
BufReader::new(file)
};
#[cfg(target_arch = "wasm32")]
let reader =
include_str!("../../cairo_programs/manually_compiled/invalid_odd_length_hex.json")
.as_bytes();
Expand All @@ -743,12 +716,6 @@ mod tests {
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn deserialize_missing_entrypoint_gives_error() {
#[cfg(not(target_arch = "wasm32"))]
let reader = {
let file = File::open("cairo_programs/manually_compiled/valid_program_a.json").unwrap();
BufReader::new(file)
};
#[cfg(target_arch = "wasm32")]
let reader =
include_str!("../../cairo_programs/manually_compiled/valid_program_a.json").as_bytes();

Expand All @@ -764,12 +731,6 @@ mod tests {
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn deserialize_program_test() {
#[cfg(not(target_arch = "wasm32"))]
let reader = {
let file = File::open("cairo_programs/manually_compiled/valid_program_a.json").unwrap();
BufReader::new(file)
};
#[cfg(target_arch = "wasm32")]
let reader =
include_str!("../../cairo_programs/manually_compiled/valid_program_a.json").as_bytes();

Expand Down Expand Up @@ -833,12 +794,6 @@ mod tests {
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn deserialize_program_without_entrypoint_test() {
#[cfg(not(target_arch = "wasm32"))]
let reader = {
let file = File::open("cairo_programs/manually_compiled/valid_program_a.json").unwrap();
BufReader::new(file)
};
#[cfg(target_arch = "wasm32")]
let reader =
include_str!("../../cairo_programs/manually_compiled/valid_program_a.json").as_bytes();

Expand Down Expand Up @@ -901,14 +856,6 @@ mod tests {
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn deserialize_constant() {
#[cfg(not(target_arch = "wasm32"))]
let reader = {
let file =
File::open("cairo_programs/manually_compiled/deserialize_constant_test.json")
.unwrap();
BufReader::new(file)
};
#[cfg(target_arch = "wasm32")]
let reader =
include_str!("../../cairo_programs/manually_compiled/deserialize_constant_test.json")
.as_bytes();
Expand Down Expand Up @@ -1355,12 +1302,6 @@ mod tests {
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn deserialize_program_with_type_definition() {
#[cfg(not(target_arch = "wasm32"))]
let reader = {
let file = File::open("cairo_programs/uint256_integration_tests.json").unwrap();
BufReader::new(file)
};
#[cfg(target_arch = "wasm32")]
let reader = include_str!("../../cairo_programs/uint256_integration_tests.json").as_bytes();

let program_json: ProgramJson = serde_json::from_reader(reader).unwrap();
Expand Down
2 changes: 2 additions & 0 deletions src/serde/deserialize_utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::prelude::v1::*;

use crate::{
serde::deserialize_program::{OffsetValue, ValueAddress},
types::instruction::Register,
Expand Down
2 changes: 2 additions & 0 deletions src/tests/bitwise_test.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::prelude::v1::*;

use crate::{
hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor,
utils::test_utils::*,
Expand Down
6 changes: 4 additions & 2 deletions src/tests/cairo_run_test.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::prelude::v1::*;

use crate::cairo_run::{self, CairoRunConfig};
use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor;

Expand Down Expand Up @@ -1528,9 +1530,9 @@ fn error_msg_attr_tempvar() {
let file =
include_str!("../../cairo_programs/bad_programs/error_msg_attr_tempvar.json").as_bytes();

#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "std")]
let expected_error_message = "Error message: SafeUint256: addition overflow: {x} (Cannot evaluate ap-based or complex references: ['x'])\ncairo_programs/bad_programs/error_msg_attr_tempvar.cairo:4:9: Error at pc=0:2:\nAn ASSERT_EQ instruction failed: 3 != 2.\n assert x = 2;\n ^***********^\n";
#[cfg(target_arch = "wasm32")]
#[cfg(not(feature = "std"))]
let expected_error_message = "Error message: SafeUint256: addition overflow: {x} (Cannot evaluate ap-based or complex references: ['x'])\ncairo_programs/bad_programs/error_msg_attr_tempvar.cairo:4:9: Error at pc=0:2:\nAn ASSERT_EQ instruction failed: 3 != 2.\n";
let res = cairo_run::cairo_run(
file,
Expand Down
Loading

0 comments on commit 61c9f64

Please sign in to comment.