Skip to content

Commit

Permalink
fix: check all features on each crate (lambdaclass#1312)
Browse files Browse the repository at this point in the history
* Remove some `std` uses from cairo 1 hint processor

* Fix smoke job

* Fix ["alloc"] compilation of cairo-felt

* Split check-all-features into one for each crate

* Cache on failure

* Fix cairo-1-hints compilation
  • Loading branch information
MegaRedHand authored and kariy committed Jul 25, 2023
1 parent cad5438 commit 13cf355
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
25 changes: 23 additions & 2 deletions .github/workflows/rust.yml
Expand Up @@ -121,6 +121,9 @@ jobs:

- name: Set up cargo cache
uses: Swatinem/rust-cache@v2
with:
cache-on-failure: true

- name: Install cargo-all-features
uses: taiki-e/install-action@v2
with:
Expand Down Expand Up @@ -157,8 +160,26 @@ jobs:
cairo_programs/**/*.json
key: cairo_1_test_contracts-cache-${{ hashFiles( 'cairo_programs/**/*.cairo' ) }}

- name: Check all features
run: cargo check-all-features --workspace --all-targets
# NOTE: we do this separately because --workspace operates in weird ways
- name: Check all features (felt)
run: |
cd felt
cargo check-all-features
cargo check-all-features --workspace --all-targets
- name: Check all features (vm)
run: |
cd vm
cargo check-all-features
- name: Check all features (CLI)
run: |
cd cairo-vm-cli
cargo check-all-features
- name: Check all features (workspace)
run: |
cargo check-all-features --workspace --all-targets
- name: Check no-std
run: |
Expand Down
4 changes: 2 additions & 2 deletions felt/src/lib_lambdaworks.rs
Expand Up @@ -955,13 +955,13 @@ impl FromPrimitive for Felt252 {

impl fmt::Display for Felt252 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.to_str_radix(10))
write!(f, "{}", self.to_biguint().to_str_radix(10))
}
}

impl fmt::Debug for Felt252 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.to_str_radix(10))
write!(f, "{}", self.to_biguint().to_str_radix(10))
}
}

Expand Down
12 changes: 6 additions & 6 deletions vm/src/hint_processor/cairo_1_hint_processor/hint_processor.rs
Expand Up @@ -23,11 +23,11 @@ use cairo_lang_casm::{
operand::{CellRef, ResOperand},
};
use core::any::Any;
use core::ops::Shl;

use num_bigint::BigUint;
use num_integer::Integer;
use num_traits::{cast::ToPrimitive, Zero};
use std::ops::Shl;

/// Execution scope for constant memory allocation.
struct MemoryExecScope {
Expand Down Expand Up @@ -767,17 +767,17 @@ impl Cairo1HintProcessor {
.map_err(HintError::from)
}

#[allow(unused_variables)]
fn debug_print(
&self,
vm: &mut VirtualMachine,
start: &ResOperand,
end: &ResOperand,
) -> Result<(), HintError> {
let mut curr = as_relocatable(vm, start)?;
let end = as_relocatable(vm, end)?;

#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "std")]
{
let mut curr = as_relocatable(vm, start)?;
let end = as_relocatable(vm, end)?;
while curr != end {
let value = vm.get_integer(curr)?;
if let Some(shortstring) = as_cairo_short_string(&value) {
Expand Down Expand Up @@ -1066,7 +1066,7 @@ impl Cairo1HintProcessor {
if let Some(root) = res.sqrt() {
let root0: BigUint = root.into_bigint().into();
let root1: BigUint = (-root).into_bigint().into();
let root = Felt252::from(std::cmp::min(root0, root1));
let root = Felt252::from(core::cmp::min(root0, root1));
vm.insert_value(cell_ref_to_relocatable(sqrt, vm)?, root)
.map_err(HintError::from)
} else {
Expand Down
Expand Up @@ -4,7 +4,7 @@ use crate::vm::errors::{hint_errors::HintError, vm_errors::VirtualMachineError};
use crate::vm::vm_core::VirtualMachine;
use cairo_lang_casm::operand::{CellRef, DerefOrImmediate, Operation, Register, ResOperand};
use felt::Felt252;
use num_traits::Zero;

/// Extracts a parameter assumed to be a buffer.
pub(crate) fn extract_buffer(buffer: &ResOperand) -> Result<(&CellRef, Felt252), HintError> {
let (cell, base_offset) = match buffer {
Expand Down Expand Up @@ -58,6 +58,7 @@ pub(crate) fn get_ptr(
Ok((vm.get_relocatable(cell_ref_to_relocatable(cell, vm)?)? + offset)?)
}

#[cfg(feature = "std")]
pub(crate) fn as_relocatable(
vm: &mut VirtualMachine,
value: &ResOperand,
Expand Down Expand Up @@ -97,10 +98,15 @@ pub(crate) fn res_operand_get_val(
}
}

#[cfg(feature = "std")]
pub(crate) fn as_cairo_short_string(value: &Felt252) -> Option<String> {
let mut as_string = String::default();
let mut is_end = false;
for byte in value.to_be_bytes().into_iter().skip_while(Zero::is_zero) {
for byte in value
.to_be_bytes()
.into_iter()
.skip_while(num_traits::Zero::is_zero)
{
if byte == 0 {
is_end = true;
} else if is_end || !byte.is_ascii() {
Expand Down

0 comments on commit 13cf355

Please sign in to comment.