Skip to content

Commit

Permalink
Cairo v0.10.3 (pre).
Browse files Browse the repository at this point in the history
  • Loading branch information
liorgold2 committed Nov 29, 2022
1 parent 9889fbd commit 60233af
Show file tree
Hide file tree
Showing 80 changed files with 3,722 additions and 1,426 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ We recommend starting from [Setting up the environment](https://cairo-lang.org/d
# Installation instructions

You should be able to download the python package zip file directly from
[github](https://github.com/starkware-libs/cairo-lang/releases/tag/v0.10.2)
[github](https://github.com/starkware-libs/cairo-lang/releases/tag/v0.10.3)
and install it using ``pip``.
See [Setting up the environment](https://cairo-lang.org/docs/quickstart.html).

Expand Down Expand Up @@ -54,7 +54,7 @@ Once the docker image is built, you can fetch the python package zip file using:

```bash
> container_id=$(docker create cairo)
> docker cp ${container_id}:/app/cairo-lang-0.10.2.zip .
> docker cp ${container_id}:/app/cairo-lang-0.10.3.zip .
> docker rm -v ${container_id}
```

3 changes: 1 addition & 2 deletions src/cmake_utils/gen_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,9 @@ def main():

# Prepare an empty virtual environment in the background.
# --symlinks prefers symlinks of copying.
# --without-pip installs a completely empty venv, with no pip.
# --clear clears the old venv if exists.
venv_proc = subprocess.Popen(
[python_exec, "-m", "venv", "--symlinks", "--without-pip", "--clear", args.venv_dir]
[python_exec, "-m", "venv", "--symlinks", "--clear", args.venv_dir]
)

# Find all libraries.
Expand Down
6 changes: 2 additions & 4 deletions src/demo/amm_demo/amm.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ func modify_account{range_check_ptr}(state: AmmState, account_id, diff_a, diff_b
let (local old_account: Account*) = dict_read{dict_ptr=account_dict_end}(key=account_id);

// Compute the new account values.
tempvar new_token_a_balance = (
old_account.token_a_balance + diff_a);
tempvar new_token_b_balance = (
old_account.token_b_balance + diff_b);
tempvar new_token_a_balance = (old_account.token_a_balance + diff_a);
tempvar new_token_b_balance = (old_account.token_b_balance + diff_b);

// Verify that the new balances are positive.
assert_nn_le(new_token_a_balance, MAX_BALANCE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ func execute_task{builtin_ptrs: BuiltinData*, self_range_check_ptr}(
ecdsa=input_builtin_ptrs.ecdsa,
bitwise=input_builtin_ptrs.bitwise,
ec_op=input_builtin_ptrs.ec_op,
keccak=input_builtin_ptrs.keccak);
keccak=input_builtin_ptrs.keccak,
);

// Call select_input_builtins to get the relevant input builtin pointers for the task.
select_input_builtins(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ func run_simple_bootloader{
ecdsa=ecdsa_ptr,
bitwise=bitwise_ptr,
ec_op=ec_op_ptr,
keccak=keccak_ptr);
keccak=keccak_ptr,
);

// A struct containing the encoding of each builtin.
local builtin_encodings: BuiltinData = BuiltinData(
Expand All @@ -56,10 +57,12 @@ func run_simple_bootloader{
ecdsa='ecdsa',
bitwise='bitwise',
ec_op='ec_op',
keccak='keccak');
keccak='keccak',
);

local builtin_instance_sizes: BuiltinData = BuiltinData(
output=1, pedersen=3, range_check=1, ecdsa=2, bitwise=5, ec_op=7, keccak=16);
output=1, pedersen=3, range_check=1, ecdsa=2, bitwise=5, ec_op=7, keccak=16
);

// Call execute_tasks.
let (__fp__, _) = get_fp_and_pc();
Expand Down
15 changes: 9 additions & 6 deletions src/starkware/cairo/common/cairo_blake2s/packed_blake2s.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ from starkware.cairo.common.registers import get_fp_and_pc

const N_PACKED_INSTANCES = 7;
const ALL_ONES = 2 ** 251 - 1;
const SHIFTS = 1 + 2 ** 35 + 2 ** (35 * 2) + 2 ** (35 * 3) + 2 ** (35 * 4) + 2 ** (35 * 5) +
2 ** (35 * 6);
const SHIFTS = (
1 + 2 ** 35 + 2 ** (35 * 2) + 2 ** (35 * 3) + 2 ** (35 * 4) + 2 ** (35 * 5) + 2 ** (35 * 6)
);

func mix{bitwise_ptr: BitwiseBuiltin*}(a: felt, b: felt, c: felt, d: felt, m0: felt, m1: felt) -> (
a: felt, b: felt, c: felt, d: felt
Expand All @@ -27,8 +28,9 @@ func mix{bitwise_ptr: BitwiseBuiltin*}(a: felt, b: felt, c: felt, d: felt, m0: f
tempvar a_xor_d = bitwise_ptr[0].x_xor_y;
assert bitwise_ptr[1].x = a_xor_d;
assert bitwise_ptr[1].y = SHIFTS * (2 ** 32 - 2 ** 16);
tempvar d = (2 ** (32 - 16)) * a_xor_d +
(1 / 2 ** 16 - 2 ** (32 - 16)) * bitwise_ptr[1].x_and_y;
tempvar d = (
(2 ** (32 - 16)) * a_xor_d + (1 / 2 ** 16 - 2 ** (32 - 16)) * bitwise_ptr[1].x_and_y
);
let bitwise_ptr = bitwise_ptr + 2 * BitwiseBuiltin.SIZE;

// c = (c + d) % 2**32.
Expand All @@ -43,8 +45,9 @@ func mix{bitwise_ptr: BitwiseBuiltin*}(a: felt, b: felt, c: felt, d: felt, m0: f
tempvar b_xor_c = bitwise_ptr[0].x_xor_y;
assert bitwise_ptr[1].x = b_xor_c;
assert bitwise_ptr[1].y = SHIFTS * (2 ** 32 - 2 ** 12);
tempvar b = (2 ** (32 - 12)) * b_xor_c +
(1 / 2 ** 12 - 2 ** (32 - 12)) * bitwise_ptr[1].x_and_y;
tempvar b = (
(2 ** (32 - 12)) * b_xor_c + (1 / 2 ** 12 - 2 ** (32 - 12)) * bitwise_ptr[1].x_and_y
);
let bitwise_ptr = bitwise_ptr + 2 * BitwiseBuiltin.SIZE;

// a = (a + b + m1) % 2**32.
Expand Down
47 changes: 26 additions & 21 deletions src/starkware/cairo/common/cairo_secp/ec.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ func ec_negate{range_check_ptr}(point: EcPoint) -> (point: EcPoint) {
let (minus_y) = nondet_bigint3();
verify_zero(
UnreducedBigInt3(
d0=minus_y.d0 + point.y.d0,
d1=minus_y.d1 + point.y.d1,
d2=minus_y.d2 + point.y.d2),
d0=minus_y.d0 + point.y.d0, d1=minus_y.d1 + point.y.d1, d2=minus_y.d2 + point.y.d2
),
);

return (point=EcPoint(x=point.x, y=minus_y));
Expand Down Expand Up @@ -70,9 +69,10 @@ func compute_doubling_slope{range_check_ptr}(point: EcPoint) -> (slope: BigInt3)

verify_zero(
UnreducedBigInt3(
d0=3 * x_sqr.d0 - 2 * slope_y.d0,
d1=3 * x_sqr.d1 - 2 * slope_y.d1,
d2=3 * x_sqr.d2 - 2 * slope_y.d2),
d0=3 * x_sqr.d0 - 2 * slope_y.d0,
d1=3 * x_sqr.d1 - 2 * slope_y.d1,
d2=3 * x_sqr.d2 - 2 * slope_y.d2,
),
);

return (slope=slope);
Expand Down Expand Up @@ -111,9 +111,10 @@ func compute_slope{range_check_ptr}(point0: EcPoint, point1: EcPoint) -> (slope:

verify_zero(
UnreducedBigInt3(
d0=x_diff_slope.d0 - point0.y.d0 + point1.y.d0,
d1=x_diff_slope.d1 - point0.y.d1 + point1.y.d1,
d2=x_diff_slope.d2 - point0.y.d2 + point1.y.d2),
d0=x_diff_slope.d0 - point0.y.d0 + point1.y.d0,
d1=x_diff_slope.d1 - point0.y.d1 + point1.y.d1,
d2=x_diff_slope.d2 - point0.y.d2 + point1.y.d2,
),
);

return (slope=slope);
Expand Down Expand Up @@ -155,9 +156,10 @@ func ec_double{range_check_ptr}(point: EcPoint) -> (res: EcPoint) {

verify_zero(
UnreducedBigInt3(
d0=slope_sqr.d0 - new_x.d0 - 2 * point.x.d0,
d1=slope_sqr.d1 - new_x.d1 - 2 * point.x.d1,
d2=slope_sqr.d2 - new_x.d2 - 2 * point.x.d2),
d0=slope_sqr.d0 - new_x.d0 - 2 * point.x.d0,
d1=slope_sqr.d1 - new_x.d1 - 2 * point.x.d1,
d2=slope_sqr.d2 - new_x.d2 - 2 * point.x.d2,
),
);

let (x_diff_slope: UnreducedBigInt3) = unreduced_mul(
Expand All @@ -166,9 +168,10 @@ func ec_double{range_check_ptr}(point: EcPoint) -> (res: EcPoint) {

verify_zero(
UnreducedBigInt3(
d0=x_diff_slope.d0 - point.y.d0 - new_y.d0,
d1=x_diff_slope.d1 - point.y.d1 - new_y.d1,
d2=x_diff_slope.d2 - point.y.d2 - new_y.d2),
d0=x_diff_slope.d0 - point.y.d0 - new_y.d0,
d1=x_diff_slope.d1 - point.y.d1 - new_y.d1,
d2=x_diff_slope.d2 - point.y.d2 - new_y.d2,
),
);

return (res=EcPoint(new_x, new_y));
Expand Down Expand Up @@ -224,9 +227,10 @@ func fast_ec_add{range_check_ptr}(point0: EcPoint, point1: EcPoint) -> (res: EcP

verify_zero(
UnreducedBigInt3(
d0=slope_sqr.d0 - new_x.d0 - point0.x.d0 - point1.x.d0,
d1=slope_sqr.d1 - new_x.d1 - point0.x.d1 - point1.x.d1,
d2=slope_sqr.d2 - new_x.d2 - point0.x.d2 - point1.x.d2),
d0=slope_sqr.d0 - new_x.d0 - point0.x.d0 - point1.x.d0,
d1=slope_sqr.d1 - new_x.d1 - point0.x.d1 - point1.x.d1,
d2=slope_sqr.d2 - new_x.d2 - point0.x.d2 - point1.x.d2,
),
);

let (x_diff_slope: UnreducedBigInt3) = unreduced_mul(
Expand All @@ -236,9 +240,10 @@ func fast_ec_add{range_check_ptr}(point0: EcPoint, point1: EcPoint) -> (res: EcP

verify_zero(
UnreducedBigInt3(
d0=x_diff_slope.d0 - point0.y.d0 - new_y.d0,
d1=x_diff_slope.d1 - point0.y.d1 - new_y.d1,
d2=x_diff_slope.d2 - point0.y.d2 - new_y.d2),
d0=x_diff_slope.d0 - point0.y.d0 - new_y.d0,
d1=x_diff_slope.d1 - point0.y.d1 - new_y.d1,
d2=x_diff_slope.d2 - point0.y.d2 - new_y.d2,
),
);

return (res=EcPoint(new_x, new_y));
Expand Down
10 changes: 2 additions & 8 deletions src/starkware/cairo/common/cairo_secp/field.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,7 @@ func is_zero{range_check_ptr}(x: BigInt3) -> (res: felt) {
let (x_x_inv) = unreduced_mul(x, x_inv);

// Check that x * x_inv = 1 to verify that x != 0.
verify_zero(UnreducedBigInt3(
d0=x_x_inv.d0 - 1,
d1=x_x_inv.d1,
d2=x_x_inv.d2));
verify_zero(UnreducedBigInt3(d0=x_x_inv.d0 - 1, d1=x_x_inv.d1, d2=x_x_inv.d2));
return (res=0);
}

Expand All @@ -137,10 +134,7 @@ func reduce{range_check_ptr}(x: UnreducedBigInt3) -> (reduced_x: BigInt3) {
let (reduced_x: BigInt3) = nondet_bigint3();

verify_zero(
UnreducedBigInt3(
d0=x.d0 - reduced_x.d0,
d1=x.d1 - reduced_x.d1,
d2=x.d2 - reduced_x.d2),
UnreducedBigInt3(d0=x.d0 - reduced_x.d0, d1=x.d1 - reduced_x.d1, d2=x.d2 - reduced_x.d2)
);
return (reduced_x=reduced_x);
}
Expand Down
6 changes: 3 additions & 3 deletions src/starkware/cairo/common/cairo_secp/signature.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ func get_point_from_x{range_check_ptr}(x: BigInt3, v: felt) -> (point: EcPoint)
// Check that y_square = x_cube + BETA.
verify_zero(
UnreducedBigInt3(
d0=x_cube.d0 + BETA - y_square.d0,
d1=x_cube.d1 - y_square.d1,
d2=x_cube.d2 - y_square.d2,
d0=x_cube.d0 + BETA - y_square.d0,
d1=x_cube.d1 - y_square.d1,
d2=x_cube.d2 - y_square.d2,
),
);

Expand Down
8 changes: 4 additions & 4 deletions src/starkware/cairo/common/hash_chain.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ func hash_chain{hash_ptr: HashBuiltin*}(data_ptr: felt*) -> (hash: felt) {
tempvar data_ptr_end = data_ptr + data_length;
// Prepare the loop_frame for the first iteration of the hash_loop.
tempvar loop_frame = LoopLocals(
data_ptr=data_ptr_end,
hash_ptr=hash_ptr,
cur_hash=[data_ptr_end]);
data_ptr=data_ptr_end, hash_ptr=hash_ptr, cur_hash=[data_ptr_end]
);

hash_loop:
let curr_frame = cast(ap - LoopLocals.SIZE, LoopLocals*);
Expand All @@ -35,7 +34,8 @@ func hash_chain{hash_ptr: HashBuiltin*}(data_ptr: felt*) -> (hash: felt) {
tempvar next_frame = LoopLocals(
data_ptr=curr_frame.data_ptr - 1,
hash_ptr=curr_frame.hash_ptr + HashBuiltin.SIZE,
cur_hash=current_hash.result);
cur_hash=current_hash.result,
);

// Update n_elements_to_hash and loop accordingly. Note that the hash is calculated backwards.
n_elements_to_hash = next_frame.data_ptr - data_ptr;
Expand Down
2 changes: 1 addition & 1 deletion src/starkware/cairo/lang/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.10.2
0.10.3a0
2 changes: 2 additions & 0 deletions src/starkware/cairo/lang/compiler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ python_lib(cairo_compile_lib
ast/module.py
ast/node.py
ast/notes.py
ast/parentheses_expr_wrapper.py
ast/particle.py
ast/rvalue.py
ast/types.py
ast/visitor.py
Expand Down
6 changes: 4 additions & 2 deletions src/starkware/cairo/lang/compiler/ast/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
full_python_test(cairo_compile_formatting_utils_test
full_python_test(cairo_compile_formatting_test
PREFIX starkware/cairo/lang/compiler/ast
PYTHON ${PYTHON_COMMAND}
TESTED_MODULES starkware/cairo/lang/compiler/ast

FILES
formatting_utils_test.py
parentheses_expr_wrapper_test.py
particle_test.py

LIBS
cairo_compile_lib
cairo_compile_test_utils_lib
pip_pytest
)
5 changes: 3 additions & 2 deletions src/starkware/cairo/lang/compiler/ast/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from starkware.cairo.lang.compiler.ast.formatting_utils import LocationField
from starkware.cairo.lang.compiler.ast.node import AstNode
from starkware.cairo.lang.compiler.ast.notes import NoteListField, Notes
from starkware.cairo.lang.compiler.ast.particle import ParticleList
from starkware.cairo.lang.compiler.ast.types import TypedIdentifier
from starkware.cairo.lang.compiler.error_handling import Location

Expand All @@ -14,10 +15,10 @@ class IdentifierList(AstNode):
notes: List[Notes] = NoteListField # type: ignore
location: Optional[Location] = LocationField

def get_particles(self):
def get_particles(self) -> ParticleList:
for note in self.notes:
note.assert_no_comments()
return [x.to_particle() for x in self.identifiers]
return ParticleList(elements=[x.to_particle() for x in self.identifiers])

def get_children(self) -> Sequence[Optional[AstNode]]:
return self.identifiers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
ExprNewOperator,
ExprOperator,
ExprParentheses,
ExprPow,
ExprSubscript,
)

Expand All @@ -18,6 +19,8 @@ def remove_parentheses(expr):
return remove_parentheses(expr.val)
if isinstance(expr, ExprOperator):
return ExprOperator(a=remove_parentheses(expr.a), op=expr.op, b=remove_parentheses(expr.b))
if isinstance(expr, ExprPow):
return ExprPow(a=remove_parentheses(expr.a), b=remove_parentheses(expr.b))
if isinstance(expr, ExprAddressOf):
return ExprAddressOf(expr=remove_parentheses(expr.expr))
if isinstance(expr, ExprNeg):
Expand Down
8 changes: 2 additions & 6 deletions src/starkware/cairo/lang/compiler/ast/bool_expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@
from typing import Optional, Sequence

from starkware.cairo.lang.compiler.ast.expr import Expression
from starkware.cairo.lang.compiler.ast.formatting_utils import (
LocationField,
Particle,
ParticleList,
SingleParticle,
)
from starkware.cairo.lang.compiler.ast.formatting_utils import LocationField
from starkware.cairo.lang.compiler.ast.node import AstNode
from starkware.cairo.lang.compiler.ast.notes import Notes, NotesField
from starkware.cairo.lang.compiler.ast.particle import Particle, ParticleList, SingleParticle
from starkware.cairo.lang.compiler.error_handling import Location


Expand Down
18 changes: 9 additions & 9 deletions src/starkware/cairo/lang/compiler/ast/cairo_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
from enum import Enum, auto
from typing import List, Optional, Sequence

from starkware.cairo.lang.compiler.ast.formatting_utils import (
LocationField,
from starkware.cairo.lang.compiler.ast.formatting_utils import LocationField
from starkware.cairo.lang.compiler.ast.node import AstNode
from starkware.cairo.lang.compiler.ast.notes import Notes
from starkware.cairo.lang.compiler.ast.particle import (
Particle,
SeparatedParticleList,
SingleParticle,
)
from starkware.cairo.lang.compiler.ast.node import AstNode
from starkware.cairo.lang.compiler.ast.notes import Notes
from starkware.cairo.lang.compiler.error_handling import Location
from starkware.cairo.lang.compiler.scoped_name import ScopedName

Expand Down Expand Up @@ -179,14 +179,14 @@ def assert_no_comments(self):
for note in self.notes:
note.assert_no_comments()

def get_particles(self) -> List[Particle]:
self.assert_no_comments()
return [member.to_particle() for member in self.members]

def to_particle(self) -> Particle:
self.assert_no_comments()
has_trailing_comma = len(self.members) == 1 and self.members[0].name is None
return SeparatedParticleList(
elements=self.get_particles(), start="(", end=")", trailing_separator=has_trailing_comma
elements=[member.to_particle() for member in self.members],
start="(",
end=")",
trailing_separator=has_trailing_comma,
)

def get_children(self) -> Sequence[Optional[AstNode]]:
Expand Down
Loading

0 comments on commit 60233af

Please sign in to comment.