Skip to content

Commit

Permalink
updating to scarb 2.5.3
Browse files Browse the repository at this point in the history
  • Loading branch information
shramee committed Feb 10, 2024
1 parent a848288 commit 71b8b7e
Show file tree
Hide file tree
Showing 88 changed files with 4,699 additions and 2,013 deletions.
1,702 changes: 1,202 additions & 500 deletions Cargo.lock

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ glob = "0.3.0"
cairo-felt = "0.8.2"

# Cairo runner dependencies
cairo-lang-test-runner = {git = "https://github.com/starkware-libs/cairo", rev = "bf91adecc5a1cb2ced041ba383d7b7c38dd2fa7f"}
cairo-lang-test-plugin = {git = "https://github.com/starkware-libs/cairo", rev = "bf91adecc5a1cb2ced041ba383d7b7c38dd2fa7f"}
cairo-lang-runner = {git = "https://github.com/starkware-libs/cairo", rev = "bf91adecc5a1cb2ced041ba383d7b7c38dd2fa7f"}
cairo-lang-sierra = {git = "https://github.com/starkware-libs/cairo", rev = "bf91adecc5a1cb2ced041ba383d7b7c38dd2fa7f"}
scarb = { git = "https://github.com/software-mansion/scarb", version = "2.3.0" }
scarb-ui = { git = "https://github.com/software-mansion/scarb", version = "0.1.0" }
cairo-lang-test-runner = "2.5.3"
cairo-lang-test-plugin = "2.5.3"
cairo-lang-runner = "2.5.3"
cairo-lang-sierra = "2.5.3"
scarb = { git = "https://github.com/software-mansion/scarb", version = "2.5.3" }
scarb-ui = { git = "https://github.com/software-mansion/scarb", version = "0.1.3" }

anyhow = "1.0.66"
ark-ff = "0.4.0-alpha.7"
Expand Down
2 changes: 1 addition & 1 deletion corelib/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ version = 1

[[package]]
name = "core"
version = "2.3.0"
version = "2.4.1"
3 changes: 2 additions & 1 deletion corelib/Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[package]
name = "core"
version = "2.3.0"
version = "2.5.3"
edition = "2023_11"

# NOTE: This is non-public, unstable Scarb's field, which instructs resolver that this package does not
# depend on `core`, which is only true for this particular package. Nobody else should use it.
Expand Down
6 changes: 6 additions & 0 deletions corelib/cairo_project.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
[crate_roots]
core = "src"

[config.global]
edition = "2023_11"

[config.global.experimental_features]
negative_impls = true
65 changes: 51 additions & 14 deletions corelib/src/array.cairo
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use traits::IndexView;
use core::traits::IndexView;

use box::BoxTrait;
use gas::withdraw_gas;
use option::OptionTrait;
use serde::Serde;
use core::box::BoxTrait;
use core::gas::withdraw_gas;
use core::option::OptionTrait;
use core::serde::Serde;
use core::metaprogramming::TypeEqual;

#[derive(Drop)]
extern type Array<T>;
pub extern type Array<T>;

extern fn array_new<T>() -> Array<T> nopanic;
extern fn array_append<T>(ref arr: Array<T>, value: T) nopanic;
Expand All @@ -24,15 +25,24 @@ extern fn array_slice<T>(
extern fn array_len<T>(arr: @Array<T>) -> usize nopanic;

#[generate_trait]
impl ArrayImpl<T> of ArrayTrait<T> {
pub impl ArrayImpl<T> of ArrayTrait<T> {
#[inline(always)]
fn new() -> Array<T> {
array_new()
}
#[inline(always)]
fn append(ref self: Array<T>, value: T) {
fn append(ref self: Array<T>, value: T) nopanic {
array_append(ref self, value)
}
fn append_span<+Clone<T>, +Drop<T>>(ref self: Array<T>, mut span: Span<T>) {
match span.pop_front() {
Option::Some(current) => {
self.append(current.clone());
self.append_span(span);
},
Option::None => {}
};
}
#[inline(always)]
fn pop_front(ref self: Array<T>) -> Option<T> nopanic {
match array_pop_front(ref self) {
Expand All @@ -55,14 +65,21 @@ impl ArrayImpl<T> of ArrayTrait<T> {
array_at(self, index).unbox()
}
#[inline(always)]
#[must_use]
fn len(self: @Array<T>) -> usize {
array_len(self)
}
#[inline(always)]
#[must_use]
fn is_empty(self: @Array<T>) -> bool {
self.len() == 0_usize
let mut snapshot = self;
match array_snapshot_pop_front(ref snapshot) {
Option::Some(_) => false,
Option::None => true,
}
}
#[inline(always)]
#[must_use]
fn span(self: @Array<T>) -> Span<T> {
Span { snapshot: self }
}
Expand Down Expand Up @@ -114,14 +131,28 @@ fn deserialize_array_helper<T, +Serde<T>, +Drop<T>>(
}

// Span.
struct Span<T> {
pub struct Span<T> {
snapshot: @Array<T>
}

impl SpanCopy<T> of Copy<Span<T>>;
impl SpanDrop<T> of Drop<Span<T>>;

impl SpanSerde<T, +Serde<T>, +Drop<T>> of Serde<Span<T>> {
impl SpanFelt252Serde of Serde<Span<felt252>> {
fn serialize(self: @Span<felt252>, ref output: Array<felt252>) {
(*self).len().serialize(ref output);
serialize_array_helper(*self, ref output)
}

fn deserialize(ref serialized: Span<felt252>) -> Option<Span<felt252>> {
let length: u32 = (*serialized.pop_front()?).try_into()?;
let res = serialized.slice(0, length);
serialized = serialized.slice(length, serialized.len() - length);
Option::Some(res)
}
}

impl SpanSerde<T, +Serde<T>, +Drop<T>, -TypeEqual<felt252, T>> of Serde<Span<T>> {
fn serialize(self: @Span<T>, ref output: Array<felt252>) {
(*self).len().serialize(ref output);
serialize_array_helper(*self, ref output)
Expand All @@ -135,7 +166,7 @@ impl SpanSerde<T, +Serde<T>, +Drop<T>> of Serde<Span<T>> {
}

#[generate_trait]
impl SpanImpl<T> of SpanTrait<T> {
pub impl SpanImpl<T> of SpanTrait<T> {
#[inline(always)]
fn pop_front(ref self: Span<T>) -> Option<@T> {
let mut snapshot = self.snapshot;
Expand Down Expand Up @@ -169,16 +200,22 @@ impl SpanImpl<T> of SpanTrait<T> {
Span { snapshot: array_slice(self.snapshot, start, length).expect('Index out of bounds') }
}
#[inline(always)]
#[must_use]
fn len(self: Span<T>) -> usize {
array_len(self.snapshot)
}
#[inline(always)]
#[must_use]
fn is_empty(self: Span<T>) -> bool {
self.len() == 0_usize
let mut snapshot = self.snapshot;
match array_snapshot_pop_front(ref snapshot) {
Option::Some(_) => false,
Option::None => true,
}
}
}

impl SpanIndex<T> of IndexView<Span<T>, usize, @T> {
pub impl SpanIndex<T> of IndexView<Span<T>, usize, @T> {
#[inline(always)]
fn index(self: @Span<T>, index: usize) -> @T {
array_at(*self.snapshot, index).unbox()
Expand Down
19 changes: 19 additions & 0 deletions corelib/src/boolean.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#[generate_trait]
pub impl BoolImpl<T, +Drop<T>> of BoolTrait<T> {
/// Returns `Some(t)` if the `bool` is `true`, or `None` otherwise.
///
/// # Examples
///
/// ```
/// assert(false.then_some(0) == Option::None);
/// assert(true.then_some(0) == Option::Some(0));
/// ```
#[inline(always)]
fn then_some(self: bool, t: T) -> Option<T> nopanic {
if self {
Option::Some(t)
} else {
Option::None
}
}
}
18 changes: 16 additions & 2 deletions corelib/src/box.cairo
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
#[derive(Copy, Drop)]
extern type Box<T>;
pub extern type Box<T>;

// These functions are only exposed in the corelib through the trait below since calling them
// directly with tuples panics due to auto unpacking of the tuple.
// TODO(Gil): Expose in the core lib when the described behaviour is fixed.
extern fn into_box<T>(value: T) -> Box<T> nopanic;
extern fn unbox<T>(box: Box<T>) -> T nopanic;
extern fn box_forward_snapshot<T>(value: @Box<T>) -> Box<@T> nopanic;

#[generate_trait]
impl BoxImpl<T> of BoxTrait<T> {
pub impl BoxImpl<T> of BoxTrait<T> {
#[inline(always)]
#[must_use]
fn new(value: T) -> Box<T> nopanic {
into_box(value)
}
#[inline(always)]
#[must_use]
fn unbox(self: Box<T>) -> T nopanic {
unbox(self)
}
#[must_use]
fn as_snapshot(self: @Box<T>) -> Box<@T> nopanic {
box_forward_snapshot(self)
}
}

impl BoxDebug<T, impl TDebug: core::fmt::Debug<T>> of core::fmt::Debug<Box<T>> {
fn fmt(self: @Box<T>, ref f: core::fmt::Formatter) -> Result<(), core::fmt::Error> {
write!(f, "&")?;
TDebug::fmt(self.as_snapshot().unbox(), ref f)
}
}
54 changes: 25 additions & 29 deletions corelib/src/byte_array.cairo
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
use array::{ArrayTrait, SpanTrait};
use bytes_31::{
use core::array::{ArrayTrait, SpanTrait};
use core::bytes_31::{
BYTES_IN_BYTES31, Bytes31Trait, one_shift_left_bytes_felt252, one_shift_left_bytes_u128,
POW_2_128, POW_2_8, U128IntoBytes31, U8IntoBytes31
};
use clone::Clone;
use cmp::min;
use integer::{u128_safe_divmod, U32TryIntoNonZero};
use option::OptionTrait;
use traits::{Into, TryInto};
use zeroable::NonZeroIntoImpl;

use core::clone::Clone;
use core::cmp::min;
use core::integer::{u128_safe_divmod, U32TryIntoNonZero};
use core::option::OptionTrait;
use core::traits::{Into, TryInto};
use core::serde::Serde;
use core::zeroable::NonZeroIntoImpl;

/// A magic constant for identifying serialization of ByteArrays. An array of felt252s with this
/// magic as one of the felt252s indicates that right after it you should expect a serialized
/// ByteArray. This is currently used mainly for prints and panics.
pub(crate) const BYTE_ARRAY_MAGIC: felt252 =
0x46a6158a16a947e5916b2a2ca68501a45e93d7110e81aa2d6438b1c57c879a3;
const BYTES_IN_U128: usize = 16;
// TODO(yuval): change to `BYTES_IN_BYTES31 - 1` once consteval_int supports non-literals.
const BYTES_IN_BYTES31_MINUS_ONE: usize = consteval_int!(31 - 1);

// TODO(yuval): don't allow creation of invalid ByteArray?
#[derive(Drop, Clone, PartialEq)]
struct ByteArray {
#[derive(Drop, Clone, PartialEq, Serde, Default)]
pub struct ByteArray {
// Full "words" of 31 bytes each. The first byte of each word in the byte array
// is the most significant byte in the word.
data: Array<bytes31>,
pub(crate) data: Array<bytes31>,
// This felt252 actually represents a bytes31, with < 31 bytes.
// It is represented as a felt252 to improve performance of building the byte array.
// The number of bytes in here is specified in `pending_word_len`.
// The first byte is the most significant byte among the `pending_word_len` bytes in the word.
pending_word: felt252,
pub(crate) pending_word: felt252,
// Should be in range [0, 30].
pending_word_len: usize,
pub(crate) pending_word_len: usize,
}

impl ByteArrayStringLiteral of string::StringLiteral<ByteArray>;

impl ByteArrayDefault of Default<ByteArray> {
fn default() -> ByteArray {
ByteArray { data: Default::default(), pending_word: 0, pending_word_len: 0 }
}
}
pub(crate) impl ByteArrayStringLiteral of core::string::StringLiteral<ByteArray>;

#[generate_trait]
impl ByteArrayImpl of ByteArrayTrait {
pub impl ByteArrayImpl of ByteArrayTrait {
// TODO(yuval): add a `new` function for initialization.

// Appends a single word of `len` bytes to the end of the ByteArray.
Expand Down Expand Up @@ -90,12 +90,7 @@ impl ByteArrayImpl of ByteArrayTrait {
let mut other_data = other.data.span();

if self.pending_word_len == 0 {
loop {
match other_data.pop_front() {
Option::Some(current_word) => { self.data.append(*current_word); },
Option::None => { break; }
};
};
self.data.append_span(other_data);
self.pending_word = *other.pending_word;
self.pending_word_len = *other.pending_word_len;
return;
Expand Down Expand Up @@ -172,6 +167,7 @@ impl ByteArrayImpl of ByteArrayTrait {
self.pending_word_len = 0;
}

#[must_use]
fn len(self: @ByteArray) -> usize {
self.data.len() * BYTES_IN_BYTES31.into() + (*self.pending_word_len).into()
}
Expand Down Expand Up @@ -359,7 +355,7 @@ impl ByteArrayAddEq of AddEq<ByteArray> {
}
}

impl ByteArrayIndexView of IndexView<ByteArray, usize, u8> {
pub(crate) impl ByteArrayIndexView of IndexView<ByteArray, usize, u8> {
fn index(self: @ByteArray, index: usize) -> u8 {
self.at(index).expect('Index out of bounds')
}
Expand Down
Loading

0 comments on commit 71b8b7e

Please sign in to comment.