cryptonerdcn

cryptonerdcn

Cairo 1.0-Alpha.3 新增特性解讀

image

This article is a submission to @StarknetAstroCN.

Starkware announced Cairo 1.0-alpha.3 on February 21. The launch of Cairo 1.0 on the testnet (Starknet 0.11.0 public release in two weeks) is getting closer and closer. 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. 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: In Cairo, copying, i.e., the copy trait, is based on the concept of Rust and is different from the general concept of copy, see here). The "@" symbol represents the snap operation, and the "*" symbol represents 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 those who still don't understand, 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.

The Into operator is responsible for type conversion. The TryInto operator also handles 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 logical negation.

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

My Twitter:

image

https://twitter.com/cryptonerdcn

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