cryptonerdcn

cryptonerdcn

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 (publicly available in two weeks with Starknet 0.11.0). 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 certain point in time, and the created snapshot cannot be further modified.

The main purpose of this type is to create a reference to the object, even if the object is not copyable (note: in Cairo, copy refers to the trait in Rust, which is different from the general concept of copy, see here). In this case, @ is the snap operation, and * is the desnap operation, which retrieves the snapshot object.

The official code is a bit confusing, 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 students 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 (such as the implementation here).

Neg is short for Negative, which means taking the negative (such as the implementation here) or taking the inverse. Not means taking the negation.

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

My Twitter:

image

https://twitter.com/cryptonerdcn

載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。