Skip to content

Commit

Permalink
chore: corelib v2.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
shramee committed Oct 31, 2023
1 parent 5d9a7a0 commit 429e743
Show file tree
Hide file tree
Showing 45 changed files with 1,189 additions and 570 deletions.
6 changes: 0 additions & 6 deletions corelib/Scarb.lock

This file was deleted.

2 changes: 1 addition & 1 deletion corelib/Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "core"
version = "2.2.0"
version = "2.3.0"

# 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
30 changes: 11 additions & 19 deletions corelib/src/array.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl ArrayIndex<T> of IndexView<Array<T>, usize, @T> {
}
}

impl ArraySerde<T, impl TSerde: Serde<T>, impl TDrop: Drop<T>> of Serde<Array<T>> {
impl ArraySerde<T, +Serde<T>, +Drop<T>> of Serde<Array<T>> {
fn serialize(self: @Array<T>, ref output: Array<felt252>) {
self.len().serialize(ref output);
serialize_array_helper(self.span(), ref output);
Expand All @@ -93,9 +93,7 @@ impl ArraySerde<T, impl TSerde: Serde<T>, impl TDrop: Drop<T>> of Serde<Array<T>
}
}

fn serialize_array_helper<T, impl TSerde: Serde<T>, impl TDrop: Drop<T>>(
mut input: Span<T>, ref output: Array<felt252>
) {
fn serialize_array_helper<T, +Serde<T>, +Drop<T>>(mut input: Span<T>, ref output: Array<felt252>) {
match input.pop_front() {
Option::Some(value) => {
value.serialize(ref output);
Expand All @@ -105,13 +103,13 @@ fn serialize_array_helper<T, impl TSerde: Serde<T>, impl TDrop: Drop<T>>(
}
}

fn deserialize_array_helper<T, impl TSerde: Serde<T>, impl TDrop: Drop<T>>(
fn deserialize_array_helper<T, +Serde<T>, +Drop<T>>(
ref serialized: Span<felt252>, mut curr_output: Array<T>, remaining: felt252
) -> Option<Array<T>> {
if remaining == 0 {
return Option::Some(curr_output);
}
curr_output.append(TSerde::deserialize(ref serialized)?);
curr_output.append(Serde::deserialize(ref serialized)?);
deserialize_array_helper(ref serialized, curr_output, remaining - 1)
}

Expand All @@ -123,7 +121,7 @@ struct Span<T> {
impl SpanCopy<T> of Copy<Span<T>>;
impl SpanDrop<T> of Drop<Span<T>>;

impl SpanSerde<T, impl TSerde: Serde<T>, impl TDrop: Drop<T>> of Serde<Span<T>> {
impl SpanSerde<T, +Serde<T>, +Drop<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 Down Expand Up @@ -188,25 +186,21 @@ impl SpanIndex<T> of IndexView<Span<T>, usize, @T> {
}

// TODO(spapini): Remove TDrop. It is necessary to get rid of response in case of panic.
impl ArrayTCloneImpl<T, impl TClone: Clone<T>, impl TDrop: Drop<T>> of Clone<Array<T>> {
impl ArrayTCloneImpl<T, +Clone<T>, +Drop<T>> of Clone<Array<T>> {
fn clone(self: @Array<T>) -> Array<T> {
let mut response = array_new();
let mut span = self.span();
loop {
match span.pop_front() {
Option::Some(v) => {
response.append(TClone::clone(v));
},
Option::None => {
break ();
},
Option::Some(v) => { response.append(v.clone()); },
Option::None => { break (); },
};
};
response
}
}

impl ArrayPartialEq<T, impl PartialEqImpl: PartialEq<T>> of PartialEq<Array<T>> {
impl ArrayPartialEq<T, +PartialEq<T>> of PartialEq<Array<T>> {
fn eq(lhs: @Array<T>, rhs: @Array<T>) -> bool {
lhs.span() == rhs.span()
}
Expand All @@ -215,7 +209,7 @@ impl ArrayPartialEq<T, impl PartialEqImpl: PartialEq<T>> of PartialEq<Array<T>>
}
}

impl SpanPartialEq<T, impl PartialEqImpl: PartialEq<T>> of PartialEq<Span<T>> {
impl SpanPartialEq<T, +PartialEq<T>> of PartialEq<Span<T>> {
fn eq(lhs: @Span<T>, rhs: @Span<T>) -> bool {
if (*lhs).len() != (*rhs).len() {
return false;
Expand All @@ -229,9 +223,7 @@ impl SpanPartialEq<T, impl PartialEqImpl: PartialEq<T>> of PartialEq<Span<T>> {
break false;
}
},
Option::None => {
break true;
},
Option::None => { break true; },
};
}
}
Expand Down
28 changes: 9 additions & 19 deletions corelib/src/byte_array.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ struct ByteArray {
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 }
Expand Down Expand Up @@ -90,12 +92,8 @@ impl ByteArrayImpl of ByteArrayTrait {
if self.pending_word_len == 0 {
loop {
match other_data.pop_front() {
Option::Some(current_word) => {
self.data.append(*current_word);
},
Option::None => {
break;
}
Option::Some(current_word) => { self.data.append(*current_word); },
Option::None => { break; }
};
};
self.pending_word = *other.pending_word;
Expand All @@ -111,9 +109,7 @@ impl ByteArrayImpl of ByteArrayTrait {
Option::Some(current_word) => {
self.append_split_index_16((*current_word).into());
},
Option::None => {
break;
}
Option::None => { break; }
};
};
} else if self.pending_word_len < BYTES_IN_U128 {
Expand All @@ -125,9 +121,7 @@ impl ByteArrayImpl of ByteArrayTrait {
(*current_word).into(), self.pending_word_len
);
},
Option::None => {
break;
}
Option::None => { break; }
};
};
} else {
Expand All @@ -140,9 +134,7 @@ impl ByteArrayImpl of ByteArrayTrait {
(*current_word).into(), self.pending_word_len
);
},
Option::None => {
break;
}
Option::None => { break; }
};
};
}
Expand Down Expand Up @@ -223,9 +215,7 @@ impl ByteArrayImpl of ByteArrayTrait {
Option::Some(current_word) => {
result.append_word_rev((*current_word).into(), BYTES_IN_BYTES31);
},
Option::None => {
break;
}
Option::None => { break; }
};
};
result
Expand Down Expand Up @@ -356,7 +346,7 @@ impl ByteArrayImpl of ByteArrayTrait {
}
}

impl U128Add of Add<ByteArray> {
impl ByteArrayAdd of Add<ByteArray> {
#[inline]
fn add(lhs: ByteArray, rhs: ByteArray) -> ByteArray {
ByteArrayTrait::concat(@lhs, @rhs)
Expand Down
2 changes: 1 addition & 1 deletion corelib/src/clone.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ trait Clone<T> {
fn clone(self: @T) -> T;
}

impl TCopyClone<T, impl TCopy: Copy<T>> of Clone<T> {
impl TCopyClone<T, +Copy<T>> of Clone<T> {
fn clone(self: @T) -> T {
*self
}
Expand Down
8 changes: 2 additions & 6 deletions corelib/src/cmp.cairo
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
fn min<T, impl TPartialOrd: PartialOrd<T>, impl DropT: Drop<T>, impl CopyT: Copy<T>>(
a: T, b: T
) -> T {
fn min<T, +PartialOrd<T>, +Drop<T>, +Copy<T>>(a: T, b: T) -> T {
if a > b {
return b;
}
a
}

fn max<T, impl TPartialOrd: PartialOrd<T>, impl DropT: Drop<T>, impl CopyT: Copy<T>>(
a: T, b: T
) -> T {
fn max<T, +PartialOrd<T>, +Drop<T>, +Copy<T>>(a: T, b: T) -> T {
if a > b {
return a;
}
Expand Down
30 changes: 30 additions & 0 deletions corelib/src/debug.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,36 @@ impl U256PrintImpl of PrintTrait<u256> {
}
}

impl I8PrintImpl of PrintTrait<i8> {
fn print(self: i8) {
Into::<_, felt252>::into(self).print();
}
}

impl I16PrintImpl of PrintTrait<i16> {
fn print(self: i16) {
Into::<_, felt252>::into(self).print();
}
}

impl I32PrintImpl of PrintTrait<i32> {
fn print(self: i32) {
Into::<_, felt252>::into(self).print();
}
}

impl I64PrintImpl of PrintTrait<i64> {
fn print(self: i64) {
Into::<_, felt252>::into(self).print();
}
}

impl I128PrintImpl of PrintTrait<i128> {
fn print(self: i128) {
Into::<_, felt252>::into(self).print();
}
}

impl ArrayGenericPrintImpl of PrintTrait<Array<felt252>> {
fn print(mut self: Array<felt252>) {
print(self);
Expand Down
29 changes: 11 additions & 18 deletions corelib/src/dict.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use traits::{Index, Default};
extern type Felt252Dict<T>;
extern type SquashedFelt252Dict<T>;
extern type Felt252DictEntry<T>;
impl SquashedFelt252DictDrop<T, impl TDrop: Drop<T>> of Drop<SquashedFelt252Dict<T>>;
impl SquashedFelt252DictDrop<T, +Drop<T>> of Drop<SquashedFelt252Dict<T>>;

extern fn felt252_dict_new<T>() -> Felt252Dict<T> implicits(SegmentArena) nopanic;

Expand All @@ -28,23 +28,23 @@ trait Felt252DictTrait<T> {
/// Inserts the given value for the given key.
///
/// Requires the `Destruct` trait, as the previous value is dropped.
fn insert<impl TDestruct: Destruct<T>>(ref self: Felt252Dict<T>, key: felt252, value: T);
fn insert<+Destruct<T>>(ref self: Felt252Dict<T>, key: felt252, value: T);
/// Returns a copy of the value at the given key.
///
/// Requires the `Copy` trait.
fn get<impl TCopy: Copy<T>>(ref self: Felt252Dict<T>, key: felt252) -> T;
fn get<+Copy<T>>(ref self: Felt252Dict<T>, key: felt252) -> T;
fn squash(self: Felt252Dict<T>) -> SquashedFelt252Dict<T> nopanic;
fn entry(self: Felt252Dict<T>, key: felt252) -> (Felt252DictEntry<T>, T) nopanic;
}
impl Felt252DictImpl<T, impl TDefault: Felt252DictValue<T>> of Felt252DictTrait<T> {
impl Felt252DictImpl<T, +Felt252DictValue<T>> of Felt252DictTrait<T> {
#[inline]
fn insert<impl TDestruct: Destruct<T>>(ref self: Felt252Dict<T>, key: felt252, value: T) {
fn insert<+Destruct<T>>(ref self: Felt252Dict<T>, key: felt252, value: T) {
let (entry, _prev_value) = felt252_dict_entry_get(self, key);
self = felt252_dict_entry_finalize(entry, value);
}

#[inline]
fn get<impl TCopy: Copy<T>>(ref self: Felt252Dict<T>, key: felt252) -> T {
fn get<+Copy<T>>(ref self: Felt252Dict<T>, key: felt252) -> T {
let (entry, prev_value) = felt252_dict_entry_get(self, key);
let return_value = prev_value;
self = felt252_dict_entry_finalize(entry, prev_value);
Expand All @@ -66,7 +66,7 @@ trait Felt252DictEntryTrait<T> {
fn finalize(self: Felt252DictEntry<T>, new_value: T) -> Felt252Dict<T>;
}

impl Felt252DictEntryImpl<T, impl TDefault: Felt252DictValue<T>> of Felt252DictEntryTrait<T> {
impl Felt252DictEntryImpl<T, +Felt252DictValue<T>> of Felt252DictEntryTrait<T> {
#[inline(always)]
fn finalize(self: Felt252DictEntry<T>, new_value: T) -> Felt252Dict<T> {
felt252_dict_entry_finalize(self, new_value)
Expand All @@ -80,29 +80,22 @@ impl Felt252DictDefault<T> of Default<Felt252Dict<T>> {
}
}

impl Felt252DictDestruct<
T, impl TDrop: Drop<T>, impl TDefault: Felt252DictValue<T>
> of Destruct<Felt252Dict<T>> {
impl Felt252DictDestruct<T, +Drop<T>, +Felt252DictValue<T>> of Destruct<Felt252Dict<T>> {
#[inline(always)]
fn destruct(self: Felt252Dict<T>) nopanic {
self.squash();
}
}

impl Felt252DictEntryDestruct<
T, impl TDrop: Drop<T>, impl TDefault: Felt252DictValue<T>
> of Destruct<Felt252DictEntry<T>> {
impl Felt252DictEntryDestruct<T, +Drop<T>, +Felt252DictValue<T>> of Destruct<Felt252DictEntry<T>> {
#[inline(always)]
fn destruct(self: Felt252DictEntry::<T>) nopanic {
felt252_dict_entry_finalize(self, TDefault::zero_default());
felt252_dict_entry_finalize(self, Felt252DictValue::zero_default());
}
}

impl Felt252DictIndex<
T,
impl TDictImpl: Felt252DictTrait<T>,
impl TCopy: Copy<T>,
impl EntryDestruct: Destruct<Felt252DictEntry<T>>
T, +Felt252DictTrait<T>, +Copy<T>, +Destruct<Felt252DictEntry<T>>
> of Index<Felt252Dict<T>, felt252, T> {
#[inline(always)]
fn index(ref self: Felt252Dict<T>, index: felt252) -> T {
Expand Down
11 changes: 4 additions & 7 deletions corelib/src/ec.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,11 @@ impl EcPointAdd of Add<EcPoint> {
fn add(lhs: EcPoint, rhs: EcPoint) -> EcPoint {
let lhs_nz = match lhs.try_into() {
Option::Some(pt) => pt,
Option::None => {
return rhs;
},
Option::None => { return rhs; },
};
let rhs_nz = match rhs.try_into() {
Option::Some(pt) => pt,
Option::None => {
return lhs;
},
Option::None => { return lhs; },
};
let mut state = ec_state_init();
state.add(lhs_nz);
Expand All @@ -169,7 +165,8 @@ impl EcPointAddEq of AddEq<EcPoint> {
impl EcPointSub of Sub<EcPoint> {
/// Computes the difference between two points on the curve.
fn sub(lhs: EcPoint, rhs: EcPoint) -> EcPoint {
match rhs.try_into() {
let nz_point: Option<NonZero<EcPoint>> = rhs.try_into();
match nz_point {
Option::Some(_) => {},
Option::None => {
// lhs - 0 = lhs.
Expand Down
Loading

0 comments on commit 429e743

Please sign in to comment.