Skip to content

Commit

Permalink
feature: fix and improve no_std support (lambdaclass#953)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdelabro authored and kariy committed Jun 23, 2023
1 parent 763ff09 commit b834af1
Show file tree
Hide file tree
Showing 20 changed files with 56 additions and 64 deletions.
27 changes: 15 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@ std = [
"parse-hyperlinks/std",
"felt/std",
]
alloc = [
"serde_json/alloc",
"serde_bytes/alloc",
"starknet-crypto/alloc",
"parse-hyperlinks/alloc",
"felt/alloc",
]


# Note that these features are not retro-compatible with the cairo Python VM.
Expand All @@ -43,15 +36,21 @@ num-bigint = { version = "0.4", features = ["serde"], default-features = false }
num-traits = { version = "0.2", default-features = false }
num-integer = { version = "0.1.45", default-features = false }
serde = { version = "1.0", features = ["derive"], default-features = false }
serde_bytes = { version = "0.11.9", default-features = false }
serde_bytes = { version = "0.11.9", default-features = false, features = [
"alloc",
] }
serde_json = { version = "1.0", features = [
"arbitrary_precision",
"alloc",
], default-features = false }
hex = { version = "0.4.3", default-features = false }
bincode = { version = "2.0.0-rc.2", tag = "v2.0.0-rc.2", git = "https://github.com/bincode-org/bincode.git", default-features = false, features = [
"serde",
] }
starknet-crypto = { version = "0.4.2", default-features = false, features = ["signature-display"] }
starknet-crypto = { version = "0.4.2", default-features = false, features = [
"signature-display",
"alloc",
] }
sha3 = { version = "0.10.1", default-features = false }
rand_core = { version = "0.6.4", default-features = false }
lazy_static = { version = "1.4.0", default-features = false, features = [
Expand All @@ -71,9 +70,13 @@ thiserror-no-std = "2.0.2"
# https://stackoverflow.com/questions/70630556/parse-allowing-nested-parentheses-in-nom
# There is a proposal for extending nom::delimited to use this function:
# https://github.com/Geal/nom/issues/1253
parse-hyperlinks = { package = "cairo-take_until_unbalanced", path = "./deps/parse-hyperlinks", version = "0.24.2-rc1", default-features = false }
felt = { package = "cairo-felt", path = "./felt", version = "0.3.0-rc1", default-features = false }
bitvec = "1"
parse-hyperlinks = { package = "cairo-take_until_unbalanced", path = "./deps/parse-hyperlinks", version = "0.24.2-rc1", default-features = false, features = [
"alloc",
] }
felt = { package = "cairo-felt", path = "./felt", version = "0.3.0-rc1", default-features = false, features = [
"alloc",
] }
bitvec = { version = "1", default-features = false, features = ["alloc"] }

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
wasm-bindgen-test = "0.3.34"
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ cairo-rs_trace: $(CAIRO_RS_TRACE) $(CAIRO_RS_MEM)
test: $(COMPILED_PROOF_TESTS) $(COMPILED_TESTS) $(COMPILED_BAD_TESTS) $(COMPILED_NORETROCOMPAT_TESTS)
cargo llvm-cov nextest --no-report --workspace --features test_utils
test-no_std: $(COMPILED_PROOF_TESTS) $(COMPILED_TESTS) $(COMPILED_BAD_TESTS) $(COMPILED_NORETROCOMPAT_TESTS)
cargo llvm-cov nextest --no-report --workspace --features test_utils --no-default-features --features alloc
cargo llvm-cov nextest --no-report --workspace --features test_utils --no-default-features
test-wasm: $(COMPILED_PROOF_TESTS) $(COMPILED_TESTS) $(COMPILED_BAD_TESTS) $(COMPILED_NORETROCOMPAT_TESTS)
wasm-pack test --node --no-default-features --features alloc
wasm-pack test --node --no-default-features

clippy:
cargo clippy --tests --examples --all-features -- -D warnings
Expand Down
5 changes: 1 addition & 4 deletions src/hint_processor/builtin_hint_processor/poseidon_utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::stdlib::collections::HashMap;
use crate::stdlib::{collections::HashMap, string::String};

use felt::Felt252;
use num_traits::ToPrimitive;
Expand All @@ -9,9 +9,6 @@ use crate::{
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine},
};

#[cfg(all(not(feature = "std"), feature = "alloc"))]
use alloc::string::String;

use super::hint_utils::{get_integer_from_var_name, insert_value_into_ap};

// Implements hint: "memory[ap] = to_felt_or_relocatable(ids.n >= 10)"
Expand Down
10 changes: 2 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,18 @@

#[cfg(feature = "std")]
include!("./with_std.rs");
#[cfg(all(not(feature = "std"), feature = "alloc"))]
include!("./with_alloc.rs");
#[cfg(not(feature = "std"))]
include!("./without_std.rs");

mod stdlib {
pub mod collections {
#[cfg(all(not(feature = "std"), feature = "alloc"))]
pub use crate::with_alloc::collections::*;
#[cfg(feature = "std")]
pub use crate::with_std::collections::*;
#[cfg(not(feature = "std"))]
pub use crate::without_std::collections::*;
}

pub mod borrow {
#[cfg(all(not(feature = "std"), feature = "alloc"))]
pub use crate::with_alloc::borrow::*;
#[cfg(feature = "std")]
pub use crate::with_std::borrow::*;
#[cfg(not(feature = "std"))]
Expand All @@ -46,8 +42,6 @@ mod stdlib {
};
}

#[cfg(all(not(feature = "std"), feature = "alloc"))]
pub use crate::with_alloc::*;
#[cfg(feature = "std")]
pub use crate::with_std::*;
#[cfg(not(feature = "std"))]
Expand Down
1 change: 0 additions & 1 deletion src/math_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,6 @@ mod tests {
#[test]
// Test for sqrt of a quadratic residue. Result should be the minimum root.
fn sqrt_felt_test(ref x in "([1-9][0-9]*)") {
println!("{x}");
let x = &Felt252::parse_bytes(x.as_bytes(), 10).unwrap();
let x_sq = x * x;
let sqrt = x_sq.sqrt();
Expand Down
2 changes: 1 addition & 1 deletion src/tests/cairo_run_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1363,7 +1363,7 @@ fn error_msg_attr_tempvar() {

#[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(all(not(feature = "std"), feature = "alloc"))]
#[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
2 changes: 1 addition & 1 deletion src/types/errors/math_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use num_bigint::{BigInt, BigUint};

#[cfg(feature = "std")]
use thiserror::Error;
#[cfg(all(not(feature = "std"), feature = "alloc"))]
#[cfg(not(feature = "std"))]
use thiserror_no_std::Error;

use crate::types::relocatable::{MaybeRelocatable, Relocatable};
Expand Down
2 changes: 1 addition & 1 deletion src/types/errors/program_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::stdlib::prelude::*;

#[cfg(feature = "std")]
use thiserror::Error;
#[cfg(all(not(feature = "std"), feature = "alloc"))]
#[cfg(not(feature = "std"))]
use thiserror_no_std::Error;

use felt::PRIME_STR;
Expand Down
2 changes: 1 addition & 1 deletion src/vm/errors/cairo_run_errors.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(feature = "std")]
use thiserror::Error;
#[cfg(all(not(feature = "std"), feature = "alloc"))]
#[cfg(not(feature = "std"))]
use thiserror_no_std::Error;

use super::memory_errors::MemoryError;
Expand Down
2 changes: 1 addition & 1 deletion src/vm/errors/exec_scope_errors.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(feature = "std")]
use thiserror::Error;
#[cfg(all(not(feature = "std"), feature = "alloc"))]
#[cfg(not(feature = "std"))]
use thiserror_no_std::Error;

#[derive(Eq, Hash, PartialEq, Debug, Error)]
Expand Down
2 changes: 1 addition & 1 deletion src/vm/errors/hint_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::stdlib::prelude::*;

#[cfg(feature = "std")]
use thiserror::Error;
#[cfg(all(not(feature = "std"), feature = "alloc"))]
#[cfg(not(feature = "std"))]
use thiserror_no_std::Error;

use felt::Felt252;
Expand Down
2 changes: 1 addition & 1 deletion src/vm/errors/memory_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::stdlib::prelude::*;

#[cfg(feature = "std")]
use thiserror::Error;
#[cfg(all(not(feature = "std"), feature = "alloc"))]
#[cfg(not(feature = "std"))]
use thiserror_no_std::Error;

use felt::Felt252;
Expand Down
2 changes: 1 addition & 1 deletion src/vm/errors/runner_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::stdlib::{collections::HashSet, prelude::*};

#[cfg(feature = "std")]
use thiserror::Error;
#[cfg(all(not(feature = "std"), feature = "alloc"))]
#[cfg(not(feature = "std"))]
use thiserror_no_std::Error;

use super::memory_errors::MemoryError;
Expand Down
2 changes: 1 addition & 1 deletion src/vm/errors/trace_errors.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(feature = "std")]
use thiserror::Error;
#[cfg(all(not(feature = "std"), feature = "alloc"))]
#[cfg(not(feature = "std"))]
use thiserror_no_std::Error;

use crate::vm::errors::memory_errors::MemoryError;
Expand Down
2 changes: 1 addition & 1 deletion src/vm/errors/vm_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::stdlib::prelude::*;

#[cfg(feature = "std")]
use thiserror::Error;
#[cfg(all(not(feature = "std"), feature = "alloc"))]
#[cfg(not(feature = "std"))]
use thiserror_no_std::Error;

use crate::{
Expand Down
14 changes: 7 additions & 7 deletions src/vm/errors/vm_exception.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::stdlib::{

#[cfg(feature = "std")]
use thiserror::Error;
#[cfg(all(not(feature = "std"), feature = "alloc"))]
#[cfg(not(feature = "std"))]
use thiserror_no_std::Error;

use crate::{
Expand Down Expand Up @@ -239,7 +239,7 @@ impl Location {
)
}

#[cfg(all(not(feature = "std"), feature = "alloc"))]
#[cfg(not(feature = "std"))]
pub fn to_string_with_content(&self, message: &str) -> String {
self.to_string(message)
}
Expand Down Expand Up @@ -640,7 +640,7 @@ mod test {

#[cfg(all(feature = "std"))]
let expected_traceback = String::from("Cairo traceback (most recent call last):\ncairo_programs/bad_programs/bad_dict_update.cairo:10:5: (pc=0:34)\n dict_update{dict_ptr=my_dict}(key=2, prev_value=3, new_value=4);\n ^*************************************************************^\n");
#[cfg(all(not(feature = "std"), feature = "alloc"))]
#[cfg(not(feature = "std"))]
let expected_traceback = String::from("Cairo traceback (most recent call last):\ncairo_programs/bad_programs/bad_dict_update.cairo:10:5: (pc=0:34)\n");

let mut hint_processor = BuiltinHintProcessor::new_empty();
Expand Down Expand Up @@ -674,7 +674,7 @@ cairo_programs/bad_programs/bad_usort.cairo:64:5: (pc=0:60)
verify_multiplicity(multiplicity=multiplicity, input_len=input_len, input=input, value=value);
^*******************************************************************************************^
";
#[cfg(all(not(feature = "std"), feature = "alloc"))]
#[cfg(not(feature = "std"))]
let expected_traceback = r"Cairo traceback (most recent call last):
cairo_programs/bad_programs/bad_usort.cairo:91:48: (pc=0:97)
cairo_programs/bad_programs/bad_usort.cairo:36:5: (pc=0:30)
Expand Down Expand Up @@ -732,7 +732,7 @@ cairo_programs/bad_programs/bad_usort.cairo:64:5: (pc=0:60)

#[cfg(feature = "std")]
let expected_message = "cairo_programs/bad_programs/bad_usort.cairo:5:1: Error at pc=0:75:\nfunc usort{range_check_ptr}(input_len: felt, input: felt*) -> (\n^";
#[cfg(all(not(feature = "std"), feature = "alloc"))]
#[cfg(not(feature = "std"))]
let expected_message = "cairo_programs/bad_programs/bad_usort.cairo:5:1: Error at pc=0:75:";

assert_eq!(
Expand Down Expand Up @@ -824,7 +824,7 @@ cairo_programs/bad_programs/bad_range_check.cairo:11:5: (pc=0:6)
check_range(num - 1);
^******************^
"#;
#[cfg(all(not(feature = "std"), feature = "alloc"))]
#[cfg(not(feature = "std"))]
let expected_error_string = r#"Error message: Failed range-check
cairo_programs/bad_programs/bad_range_check.cairo:5:9: Error at pc=0:0:
An ASSERT_EQ instruction failed: 4 != 5.
Expand Down Expand Up @@ -871,7 +871,7 @@ cairo_programs/bad_programs/bad_usort.cairo:64:5: (pc=0:60)
verify_multiplicity(multiplicity=multiplicity, input_len=input_len, input=input, value=value);
^*******************************************************************************************^
"#;
#[cfg(all(not(feature = "std"), feature = "alloc"))]
#[cfg(not(feature = "std"))]
let expected_error_string = r#"cairo_programs/bad_programs/bad_usort.cairo:79:5: Error at pc=0:75:
Got an exception while executing a hint: unexpected verify multiplicity fail: positions length != 0
Cairo traceback (most recent call last):
Expand Down
3 changes: 0 additions & 3 deletions src/vm/runners/builtin_runner/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ use felt::Felt252;
use num_integer::div_ceil;
use starknet_crypto::{poseidon_permute_comp, FieldElement};

#[cfg(all(not(feature = "std"), feature = "alloc"))]
use alloc::vec::Vec;

use super::POSEIDON_BUILTIN_NAME;

#[derive(Debug, Clone)]
Expand Down
2 changes: 1 addition & 1 deletion src/vm/runners/builtin_runner/segment_arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
vm::vm_memory::memory_segments::MemorySegmentManager,
};

#[cfg(all(not(feature = "std"), feature = "alloc"))]
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

use super::SEGMENT_ARENA_BUILTIN_NAME;
Expand Down
15 changes: 0 additions & 15 deletions src/with_alloc.rs

This file was deleted.

19 changes: 18 additions & 1 deletion src/without_std.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Inspired by Substrate sp-std crate
// see https://github.com/paritytech/substrate/blob/master/primitives/std/without_std.rs

#[macro_use]
pub extern crate alloc;

pub mod without_std {
pub use core::any;
pub use core::borrow;
pub use core::cell;
pub use core::clone;
pub use core::cmp;
Expand All @@ -21,4 +23,19 @@ pub mod without_std {
pub use core::slice;
pub use core::str;
pub use core::time;

pub use alloc::boxed;
pub use alloc::rc;
pub use alloc::string;
pub use alloc::sync;
pub use alloc::vec;

pub mod collections {
pub use hashbrown::{HashMap, HashSet};
}

pub mod borrow {
pub use alloc::borrow::*;
pub use core::borrow::*;
}
}

0 comments on commit b834af1

Please sign in to comment.