Skip to content

Commit

Permalink
Cairo v0.10.3.
Browse files Browse the repository at this point in the history
  • Loading branch information
liorgold2 committed Dec 4, 2022
1 parent 60233af commit de741b9
Show file tree
Hide file tree
Showing 35 changed files with 285 additions and 169 deletions.
6 changes: 2 additions & 4 deletions src/demo/amm_demo/amm.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ func modify_account{range_check_ptr}(state: AmmState, account_id, diff_a, diff_b
// Construct and return the new state with the updated
// 'account_dict_end'.
local new_state: AmmState;
assert new_state.account_dict_start = (
state.account_dict_start);
assert new_state.account_dict_start = (state.account_dict_start);
assert new_state.account_dict_end = account_dict_end;
assert new_state.token_a_balance = state.token_a_balance;
assert new_state.token_b_balance = state.token_b_balance;
Expand Down Expand Up @@ -112,8 +111,7 @@ func swap{range_check_ptr}(state: AmmState, transaction: SwapTransaction*) -> (s

// Update the state.
local new_state: AmmState;
assert new_state.account_dict_start = (
state.account_dict_start);
assert new_state.account_dict_start = (state.account_dict_start);
assert new_state.account_dict_end = state.account_dict_end;
assert new_state.token_a_balance = new_x;
assert new_state.token_b_balance = new_y;
Expand Down
3 changes: 2 additions & 1 deletion src/starkware/cairo/bootloaders/bootloader/bootloader.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,8 @@ func unpack_composite_packed_task{
// Verify task output.
assert [cast(task_output, CairoVerifierOutput*)] = CairoVerifierOutput(
program_hash=bootloader_config.simple_bootloader_program_hash,
output_hash=subtasks_output_hash);
output_hash=subtasks_output_hash,
);
local task_output: felt* = task_output + CairoVerifierOutput.SIZE;

// Call recursively to parse the composite task's subtasks.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ func main{

// Write program_hash and output_hash to output.
assert [cast(output_ptr, CairoVerifierOutput*)] = CairoVerifierOutput(
program_hash=program_hash,
output_hash=output_hash);
program_hash=program_hash, output_hash=output_hash
);
let output_ptr = output_ptr + CairoVerifierOutput.SIZE;

return ();
Expand Down
11 changes: 9 additions & 2 deletions src/starkware/cairo/common/cairo_blake2s/blake2s.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,15 @@ func _pack_ints{range_check_ptr, blake2s_ptr: felt*}(m, packed_values: felt*) {
tempvar x6 = blake2s_ptr[6 * INSTANCE_SIZE];
assert [range_check_ptr + 12] = x6;
assert [range_check_ptr + 13] = MAX_VALUE - x6;
assert packed_values[0] = x0 + 2 ** 35 * x1 + 2 ** (35 * 2) * x2 + 2 ** (35 * 3) * x3 +
2 ** (35 * 4) * x4 + 2 ** (35 * 5) * x5 + 2 ** (35 * 6) * x6;
assert packed_values[0] = (
x0 +
2 ** 35 * x1 +
2 ** (35 * 2) * x2 +
2 ** (35 * 3) * x3 +
2 ** (35 * 4) * x4 +
2 ** (35 * 5) * x5 +
2 ** (35 * 6) * x6
);

tempvar packed_values = packed_values + 1;
tempvar blake2s_ptr = blake2s_ptr + 1;
Expand Down
11 changes: 6 additions & 5 deletions src/starkware/cairo/common/cairo_secp/bigint.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ struct UnreducedBigInt5 {
func bigint_mul(x: BigInt3, y: BigInt3) -> (res: UnreducedBigInt5) {
return (
UnreducedBigInt5(
d0=x.d0 * y.d0,
d1=x.d0 * y.d1 + x.d1 * y.d0,
d2=x.d0 * y.d2 + x.d1 * y.d1 + x.d2 * y.d0,
d3=x.d1 * y.d2 + x.d2 * y.d1,
d4=x.d2 * y.d2),
d0=x.d0 * y.d0,
d1=x.d0 * y.d1 + x.d1 * y.d0,
d2=x.d0 * y.d2 + x.d1 * y.d1 + x.d2 * y.d0,
d3=x.d1 * y.d2 + x.d2 * y.d1,
d4=x.d2 * y.d2,
),
);
}

Expand Down
14 changes: 8 additions & 6 deletions src/starkware/cairo/common/cairo_secp/field.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ func unreduced_mul(a: BigInt3, b: BigInt3) -> (res_low: UnreducedBigInt3) {
// since BASE ** 3 = 4 * SECP_REM (mod secp256k1_prime).
return (
UnreducedBigInt3(
d0=a.d0 * b.d0 + (a.d1 * b.d2 + a.d2 * b.d1) * (4 * SECP_REM),
d1=a.d0 * b.d1 + a.d1 * b.d0 + (a.d2 * b.d2) * (4 * SECP_REM),
d2=a.d0 * b.d2 + a.d1 * b.d1 + a.d2 * b.d0),
d0=a.d0 * b.d0 + (a.d1 * b.d2 + a.d2 * b.d1) * (4 * SECP_REM),
d1=a.d0 * b.d1 + a.d1 * b.d0 + (a.d2 * b.d2) * (4 * SECP_REM),
d2=a.d0 * b.d2 + a.d1 * b.d1 + a.d2 * b.d0,
),
);
}

Expand All @@ -42,9 +43,10 @@ func unreduced_sqr(a: BigInt3) -> (res_low: UnreducedBigInt3) {
tempvar twice_d0 = a.d0 * 2;
return (
UnreducedBigInt3(
d0=a.d0 * a.d0 + (a.d1 * a.d2) * (2 * 4 * SECP_REM),
d1=twice_d0 * a.d1 + (a.d2 * a.d2) * (4 * SECP_REM),
d2=twice_d0 * a.d2 + a.d1 * a.d1),
d0=a.d0 * a.d0 + (a.d1 * a.d2) * (2 * 4 * SECP_REM),
d1=twice_d0 * a.d1 + (a.d2 * a.d2) * (4 * SECP_REM),
d2=twice_d0 * a.d2 + a.d1 * a.d1,
),
);
}

Expand Down
5 changes: 3 additions & 2 deletions src/starkware/cairo/common/cairo_secp/signature.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ func get_generator_point() -> (point: EcPoint) {
// ).
return (
point=EcPoint(
BigInt3(0xe28d959f2815b16f81798, 0xa573a1c2c1c0a6ff36cb7, 0x79be667ef9dcbbac55a06),
BigInt3(0x554199c47d08ffb10d4b8, 0x2ff0384422a3f45ed1229a, 0x483ada7726a3c4655da4f)),
BigInt3(0xe28d959f2815b16f81798, 0xa573a1c2c1c0a6ff36cb7, 0x79be667ef9dcbbac55a06),
BigInt3(0x554199c47d08ffb10d4b8, 0x2ff0384422a3f45ed1229a, 0x483ada7726a3c4655da4f),
),
);
}

Expand Down
6 changes: 4 additions & 2 deletions src/starkware/cairo/common/patricia.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,8 @@ func patricia_update_using_update_constants{hash_ptr: HashBuiltin*, range_check_
let original_siblings = siblings;
let (local globals_prev: ParticiaGlobals*) = alloc();
assert [globals_prev] = ParticiaGlobals(
pow2=patricia_update_constants.globals_pow2, access_offset=DictAccess.prev_value);
pow2=patricia_update_constants.globals_pow2, access_offset=DictAccess.prev_value
);

assert_le(height, MAX_LENGTH);
%{ vm_enter_scope(dict(node=node, **common_args)) %}
Expand All @@ -543,7 +544,8 @@ func patricia_update_using_update_constants{hash_ptr: HashBuiltin*, range_check_
let siblings = original_siblings;
let (local globals_new: ParticiaGlobals*) = alloc();
assert [globals_new] = ParticiaGlobals(
pow2=patricia_update_constants.globals_pow2, access_offset=DictAccess.new_value);
pow2=patricia_update_constants.globals_pow2, access_offset=DictAccess.new_value
);

%{ vm_enter_scope(dict(node=node, **common_args)) %}
with update_ptr, siblings {
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.3a0
0.10.3
65 changes: 39 additions & 26 deletions src/starkware/cairo/lang/compiler/ast/code_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from starkware.cairo.lang.compiler.ast.arguments import IdentifierList
from starkware.cairo.lang.compiler.ast.bool_expr import BoolExpr
from starkware.cairo.lang.compiler.ast.cairo_types import CairoType
from starkware.cairo.lang.compiler.ast.expr import Expression, ExprHint, ExprIdentifier, ExprTuple
from starkware.cairo.lang.compiler.ast.expr import Expression, ExprHint, ExprIdentifier
from starkware.cairo.lang.compiler.ast.formatting_utils import INDENTATION, LocationField
from starkware.cairo.lang.compiler.ast.instructions import InstructionAst
from starkware.cairo.lang.compiler.ast.node import AstNode
Expand Down Expand Up @@ -195,7 +195,17 @@ class CodeElementCompoundAssertEq(CodeElement):
location: Optional[Location] = LocationField

def format(self, allowed_line_length):
return f"assert {self.a.format()} = {self.b.format()};"
a_particles = parenthesize_expression(self.a).get_particles()
b_particles = parenthesize_expression(self.b).get_particles()

a_particles.add_prefix("assert ")
a_particles.add_suffix(" = ")
a_particles.add_suffix(b_particles.pop_prefix())
b_particles.add_suffix(";")

return code_particles_in_lines(
particles=a_particles + b_particles, allowed_line_length=allowed_line_length
)

def get_children(self) -> Sequence[Optional[AstNode]]:
return [self.a, self.b]
Expand All @@ -208,7 +218,17 @@ class CodeElementStaticAssert(CodeElement):
location: Optional[Location] = LocationField

def format(self, allowed_line_length):
return f"static_assert {self.a.format()} == {self.b.format()};"
a_particles = parenthesize_expression(self.a).get_particles()
b_particles = parenthesize_expression(self.b).get_particles()

a_particles.add_prefix("static_assert ")
a_particles.add_suffix(" == ")
a_particles.add_suffix(b_particles.pop_prefix())
b_particles.add_suffix(";")

return code_particles_in_lines(
particles=a_particles + b_particles, allowed_line_length=allowed_line_length
)

def get_children(self) -> Sequence[Optional[AstNode]]:
return [self.a, self.b]
Expand All @@ -225,27 +245,14 @@ class CodeElementReturn(CodeElement):
location: Optional[Location] = LocationField

def format(self, allowed_line_length):
if isinstance(self.expr, ExprTuple):
self.expr.members.assert_no_comments()
args = self.expr.members.args
expr_codes = [arg.format() for arg in args]

trailing_comma = self.expr.members.has_trailing_comma and len(args) > 0

particles = ParticleList(
elements=[
"return (",
SeparatedParticleList(
elements=expr_codes, end=");", trailing_separator=trailing_comma
),
]
)

return code_particles_in_lines(
particles=particles, allowed_line_length=allowed_line_length, one_per_line=True
)

return "return " + self.expr.format() + ";"
particles = parenthesize_expression(self.expr).get_particles()
particles.add_prefix("return ")
particles.add_suffix(";")
return code_particles_in_lines(
particles=particles,
allowed_line_length=allowed_line_length,
one_per_line=True,
)

def get_children(self) -> Sequence[Optional[AstNode]]:
return [self.expr]
Expand Down Expand Up @@ -288,10 +295,16 @@ class CodeElementFuncCall(CodeElement):
func_call: RvalueFuncCall

def get_particles(self) -> ParticleList:
return self.func_call.get_particles()
particles = self.func_call.get_particles()
particles.add_suffix(";")
return particles

def format(self, allowed_line_length):
return self.func_call.format_ex(allowed_line_length, semicolon=True)
return code_particles_in_lines(
particles=self.get_particles(),
allowed_line_length=allowed_line_length,
one_per_line=True,
)

def get_children(self) -> Sequence[Optional[AstNode]]:
return [self.func_call]
Expand Down
40 changes: 36 additions & 4 deletions src/starkware/cairo/lang/compiler/ast/particle.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ def is_splitable(self) -> bool:
Returns True if and only if the particle can be split into multiple lines.
"""

@abstractmethod
def is_empty(self) -> bool:
"""
Returns True if and only if the particle contains only empty strings.
"""

@abstractmethod
def add_prefix(self, prefix: str):
"""
Expand Down Expand Up @@ -162,6 +168,9 @@ def __str__(self):
def is_splitable(self) -> bool:
return False

def is_empty(self) -> bool:
return self.text == ""

def add_prefix(self, prefix: str):
self.text = prefix + self.text

Expand Down Expand Up @@ -202,23 +211,38 @@ def __str__(self):
def is_splitable(self) -> bool:
return len(self.elements) > 0

def is_empty(self) -> bool:
return all(particle.is_empty() for particle in self.elements)

def add_prefix(self, prefix: str):
assert len(self.elements) > 0
if prefix == "":
return
if len(self.elements) == 0:
self.elements.append(SingleParticle(text=""))
self.elements[0].add_prefix(prefix)

def add_suffix(self, suffix: str):
assert len(self.elements) > 0
if suffix == "":
return
if len(self.elements) == 0:
self.elements.append(SingleParticle(text=""))
self.elements[-1].add_suffix(suffix)

def pop_prefix(self) -> str:
if len(self.elements) == 0:
return ""
return self.elements[0].pop_prefix()
prefix = self.elements[0].pop_prefix()
if self.elements[0].is_empty():
del self.elements[0]
return prefix

def pop_suffix(self) -> str:
if len(self.elements) == 0:
return ""
return self.elements[-1].pop_suffix()
suffix = self.elements[-1].pop_suffix()
if self.elements[-1].is_empty():
del self.elements[-1]
return suffix

def add_to_builder(self, builder: ParticleLineBuilder, suffix: str = ""):
for i, particle in enumerate(self.elements):
Expand Down Expand Up @@ -272,6 +296,14 @@ def __str__(self):
def is_splitable(self) -> bool:
return len(self.elements) > 0

def is_empty(self) -> bool:
return (
all(particle.is_empty() for particle in self.elements)
and self.start == ""
and self.end == ""
and self.separator == ""
)

def add_prefix(self, prefix: str):
self.start = prefix + self.start

Expand Down
9 changes: 5 additions & 4 deletions src/starkware/cairo/lang/compiler/ast/rvalue.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,13 @@ def get_particles(self) -> ParticleList:
particles = self.func_ident.get_particles()

if self.implicit_arguments is not None:
particles.add_suffix("{")
particles.append(self.implicit_arguments.to_particle(end="}("))
start = particles.pop_suffix() + "{"
particles.append(self.implicit_arguments.to_particle(start=start, end="}("))
particles.append(self.arguments.to_particle(end=")"))
else:
particles.add_suffix("(")
start = particles.pop_suffix() + "("
particles.append(self.arguments.to_particle(start=start, end=")"))

particles.append(self.arguments.to_particle(end=")"))
return particles

def format(self, allowed_line_length):
Expand Down
21 changes: 18 additions & 3 deletions src/starkware/cairo/lang/compiler/ast_objects_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def test_file_format():
assert x * z + x = y + y;
static_assert ap + (3 + 7) + ap == fp;
let () = foo();
return (1, [fp], [ap + 3],);
return (1, [fp], [ap + 3]);
fibonacci(a=3, b=[fp + 1]);
[ap - 1] = [fp]; // This is a comment.
Expand Down Expand Up @@ -415,7 +415,7 @@ def test_return_splitting():
return (a, b, c, foo, bar,
variable_name_which_is_way_too_long_but_has_to_be_supported, g);
"""
after = """\
after_not_one_per_line = """\
return (
a,
b,
Expand All @@ -424,9 +424,24 @@ def test_return_splitting():
bar,
variable_name_which_is_way_too_long_but_has_to_be_supported,
g);
"""

after_one_per_line = """\
return (
a,
b,
c,
foo,
bar,
variable_name_which_is_way_too_long_but_has_to_be_supported,
g,
);
"""
with set_one_item_per_line(False):
assert parse_file(before).format(allowed_line_length=20) == after
assert parse_file(before).format(allowed_line_length=20) == after_not_one_per_line

with set_one_item_per_line(True):
assert parse_file(before).format(allowed_line_length=20) == after_one_per_line


def test_func_arg_ret_splitting():
Expand Down
Loading

0 comments on commit de741b9

Please sign in to comment.