cryptonerdcn

cryptonerdcn

Interpretation of New Features in Cairo 1.0-Alpha.3

image

This article is a submission to @StarknetAstroCN.

Starkware announced Cairo 1.0-alpha.3 on February 21st, getting closer to the official launch of Cairo 1.0 testnet (Starknet 0.11.0 public release in two weeks). Let's take a look at the new features in this release.

Snapshot is a new type (although it has been widely used before this version) that creates a reference to an object at a specific point in time, and the created snapshot cannot be further modified.

The main purpose of this type is to create a reference to an object, even if the object is not copyable (note: copying in Cairo, i.e., the copy trait, is based on the concept of Rust and is not the same as the general notion of copy, see here). In this case, "@" is the snap operation, and "*" is the desnap operation, which retrieves the snapshot object.

The official code provided is a bit confusing, so let me explain:

image

The first line here is actually written in Cairo's array source code.

To test it, you only need to test the following code:

use array::ArrayTrait;

fn main() -> felt {
    let mut arr = ArrayTrait::new();
    arr.append(10);
    let x = *arr.at(0_usize);
    return x;
}

This will output something like this in your terminal:

Run completed successfully, returning [10]

For those who are still not clear, run the following code:

use array::ArrayTrait;

extern fn print(message: Array::<felt>) nopanic;

fn main() -> felt {
    let mut arr = ArrayTrait::new();
    arr.append(10);
    let xx = arr.at(0_usize);
    let d = arr.pop_front();
    drop(d);
    arr.append(11);
    print(arr);
    let x = *xx;
    return x;
}

You will see the following output:

'
 ' (raw: 11), 
Run completed successfully, returning [10]

As you can see, even though the array becomes [11], the output is still 10.

Added ec_point_zero, ec_point_is_zero, ec_state_finalize.

+=, -=, *=, /=, %=

Into, TryInto, Neg, Not operators.

Among them, Into is responsible for type conversion. TryInto is also responsible for type conversion, but allows conversion to fail and returns an Option (as implemented here).

Neg is short for Negative, which means taking the negative (as implemented here) or taking the inverse. Not means taking the logical NOT.

https://github.com/starkware-libs/cairo/blob/e53053e787fb5585d09d2012335613db5407eda6/corelib/src/integer.cairo

My Twitter:

image

https://twitter.com/cryptonerdcn

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.