Skip to content

Commit

Permalink
Cairo v0.12.3a0 (pre).
Browse files Browse the repository at this point in the history
  • Loading branch information
liorgold2 committed Oct 23, 2023
1 parent 27a157d commit 9e6e0e9
Show file tree
Hide file tree
Showing 65 changed files with 2,108 additions and 462 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.12.2)
[github](https://github.com/starkware-libs/cairo-lang/releases/tag/v0.12.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.12.2.zip .
> docker cp ${container_id}:/app/cairo-lang-0.12.3.zip .
> docker rm -v ${container_id}
```

8 changes: 8 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ load("@rules_python//python:pip.bzl", "pip_parse")

pip_parse(
name = "cpython_reqs",
extra_pip_args = [
"--retries=10",
"--timeout=300",
],
python_interpreter_target = interpreter,
requirements_lock = "//scripts:requirements.txt",
)
Expand All @@ -83,6 +87,10 @@ install_deps()

pip_parse(
name = "pypy_reqs",
extra_pip_args = [
"--retries=10",
"--timeout=300",
],
python_interpreter_target = "@pypy3.9//:bin/pypy3",
requirements_lock = "//scripts:pypy-requirements.txt",
)
Expand Down
8 changes: 3 additions & 5 deletions src/services/external_api/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ py_library(
"utils.py",
],
visibility = ["//visibility:public"],
deps = [
],
deps = [],
)

py_library(
Expand All @@ -25,7 +24,7 @@ py_library(
] + SERVICES_EXTERNAL_API_LIB_ADDITIONAL_FILES,
visibility = ["//visibility:public"],
deps = [
"services_external_api_utils_lib",
":services_external_api_utils_lib",
"//src/starkware/starkware_utils:starkware_dataclasses_field_utils_lib",
"//src/starkware/python:starkware_python_utils_lib",
requirement("aiohttp"),
Expand All @@ -38,6 +37,5 @@ py_library(
"eth_gas_constants.py",
],
visibility = ["//visibility:public"],
deps = [
],
deps = [],
)
13 changes: 10 additions & 3 deletions src/services/external_api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import ssl
from abc import abstractmethod
from http import HTTPStatus
from typing import Any, Dict, List, NamedTuple, Optional, Sequence, Union
from typing import Any, Dict, List, Mapping, NamedTuple, Optional, Sequence, Union
from urllib.parse import urljoin

import aiohttp
Expand Down Expand Up @@ -159,7 +159,11 @@ def _prepare_data(
"""

async def _send_request(
self, send_method: str, uri: str, data: Optional[FlexibleJsonObject] = None
self,
send_method: str,
uri: str,
data: Optional[FlexibleJsonObject] = None,
params: Optional[Mapping[str, str]] = None,
) -> str:
"""
Sends an HTTP request to the target URI.
Expand All @@ -182,7 +186,10 @@ async def _send_request(
async with aiohttp.TCPConnector(ssl=self.ssl_context) as connector:
async with aiohttp.ClientSession(connector=connector) as session:
async with session.request(
method=send_method, url=url, data=self._prepare_data(data=data)
method=send_method,
url=url,
data=self._prepare_data(data=data),
params=params,
) as response:
return await self._parse_response(
request_url=url,
Expand Down
2 changes: 2 additions & 0 deletions src/starkware/cairo/common/cairo_secp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ cairo_library(
name = "cairo_secp256k1",
srcs = [
"bigint.cairo",
"bigint3.cairo",
"constants.cairo",
"ec.cairo",
"ec_point.cairo",
"field.cairo",
"signature.cairo",
],
Expand Down
20 changes: 1 addition & 19 deletions src/starkware/cairo/common/cairo_secp/bigint.cairo
Original file line number Diff line number Diff line change
@@ -1,27 +1,9 @@
from starkware.cairo.common.cairo_secp.bigint3 import BigInt3, UnreducedBigInt3
from starkware.cairo.common.cairo_secp.constants import BASE
from starkware.cairo.common.math import assert_nn, assert_nn_le, unsigned_div_rem
from starkware.cairo.common.math_cmp import RC_BOUND
from starkware.cairo.common.uint256 import Uint256

// Represents a big integer defined by:
// d0 + BASE * d1 + BASE**2 * d2.
// Note that the limbs (d_i) are NOT restricted to the range [0, BASE) and in particular they
// can be negative.
// In most cases this is used to represent a secp256k1 field element.
struct UnreducedBigInt3 {
d0: felt,
d1: felt,
d2: felt,
}

// Same as UnreducedBigInt3, except that d0, d1 and d2 must be in the range [0, 3 * BASE).
// In most cases this is used to represent a secp256k1 field element.
struct BigInt3 {
d0: felt,
d1: felt,
d2: felt,
}

// Represents a big integer defined by:
// sum_i(BASE**i * d_i).
// Note that the limbs (d_i) are NOT restricted to the range [0, BASE) and in particular they
Expand Down
26 changes: 26 additions & 0 deletions src/starkware/cairo/common/cairo_secp/bigint3.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Represents a big integer defined by:
// d0 + BASE * d1 + BASE**2 * d2.
// Note that the limbs (d_i) are NOT restricted to the range [0, BASE) and in particular they
// can be negative.
// In most cases this is used to represent a Secp256k1 or Secp256r1 field element.
struct UnreducedBigInt3 {
d0: felt,
d1: felt,
d2: felt,
}

// Same as UnreducedBigInt3, except that d0, d1 and d2 satisfy the bounds of
// nondet_bigint3 or are the difference of two values satisfying those bounds.
// In most cases this is used to represent a Secp256k1 or Secp256r1 field element.
struct BigInt3 {
d0: felt,
d1: felt,
d2: felt,
}

// Same as BigInt3, except the bounds on d0, d1 and d2 are twice as large.
struct SumBigInt3 {
d0: felt,
d1: felt,
d2: felt,
}
20 changes: 7 additions & 13 deletions src/starkware/cairo/common/cairo_secp/ec.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from starkware.cairo.common.cairo_secp.bigint import BigInt3, UnreducedBigInt3, nondet_bigint3
from starkware.cairo.common.cairo_secp.bigint import nondet_bigint3
from starkware.cairo.common.cairo_secp.bigint3 import BigInt3, SumBigInt3, UnreducedBigInt3
from starkware.cairo.common.cairo_secp.ec_point import EcPoint
from starkware.cairo.common.cairo_secp.field import (
is_zero,
unreduced_mul,
Expand All @@ -7,14 +9,6 @@ from starkware.cairo.common.cairo_secp.field import (
)
from starkware.cairo.common.uint256 import Uint256

// Represents a point on the secp256k1 elliptic curve.
// The zero point is represented as a point with x = 0 (there is no point on the curve with a zero
// x value).
struct EcPoint {
x: BigInt3,
y: BigInt3,
}

// Computes the negation of a point on the elliptic curve, which is a point with the same x value
// and the negation of the y value. If the point is the zero point, returns the zero point.
//
Expand Down Expand Up @@ -252,7 +246,7 @@ func fast_ec_add{range_check_ptr}(point0: EcPoint, point1: EcPoint) -> (res: EcP

// Same as fast_ec_add, except that the cases point0 = +/-point1 are supported.
func ec_add{range_check_ptr}(point0: EcPoint, point1: EcPoint) -> (res: EcPoint) {
let x_diff = BigInt3(
let x_diff = SumBigInt3(
d0=point0.x.d0 - point1.x.d0, d1=point0.x.d1 - point1.x.d1, d2=point0.x.d2 - point1.x.d2
);
let (same_x: felt) = is_zero(x_diff);
Expand All @@ -263,7 +257,7 @@ func ec_add{range_check_ptr}(point0: EcPoint, point1: EcPoint) -> (res: EcPoint)

// We have point0.x = point1.x. This implies point0.y = +/-point1.y.
// Check whether point0.y = -point1.y.
let y_sum = BigInt3(
let y_sum = SumBigInt3(
d0=point0.y.d0 + point1.y.d0, d1=point0.y.d1 + point1.y.d1, d2=point0.y.d2 + point1.y.d2
);
let (opposite_y: felt) = is_zero(y_sum);
Expand All @@ -278,14 +272,14 @@ func ec_add{range_check_ptr}(point0: EcPoint, point1: EcPoint) -> (res: EcPoint)
}
}

// Given a scalar, an integer m in the range [0, 250), and a point on the elliptic curve, point,
// Given (1) an integer m in the range [0, 250), (2) a scalar, and (3) a point on the curve,
// verifies that 0 <= scalar < 2**m and returns (2**m * point, scalar * point).
func ec_mul_inner{range_check_ptr}(point: EcPoint, scalar: felt, m: felt) -> (
pow2: EcPoint, res: EcPoint
) {
if (m == 0) {
with_attr error_message("Too large scalar") {
scalar = 0;
assert scalar = 0;
}
let ZERO_POINT = EcPoint(BigInt3(0, 0, 0), BigInt3(0, 0, 0));
return (pow2=point, res=ZERO_POINT);
Expand Down
10 changes: 10 additions & 0 deletions src/starkware/cairo/common/cairo_secp/ec_point.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from starkware.cairo.common.cairo_secp.bigint3 import BigInt3

// Represents a point on the secp256k1 elliptic curve.
// The zero point is represented as a point with x = 0 (there is no point on the curve with a zero
// x value).
// x and y satisfy the bounds of nondet_bigint3 for the relevant curve.
struct EcPoint {
x: BigInt3,
y: BigInt3,
}
9 changes: 6 additions & 3 deletions src/starkware/cairo/common/cairo_secp/field.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from starkware.cairo.common.cairo_secp.bigint import BigInt3, UnreducedBigInt3, nondet_bigint3
from starkware.cairo.common.cairo_secp.bigint import nondet_bigint3
from starkware.cairo.common.cairo_secp.bigint3 import BigInt3, SumBigInt3, UnreducedBigInt3
from starkware.cairo.common.cairo_secp.constants import BASE, P0, P1, P2, SECP_REM
from starkware.cairo.common.math import assert_nn_le

Expand Down Expand Up @@ -97,7 +98,7 @@ func verify_zero{range_check_ptr}(val: UnreducedBigInt3) {
//
// Completeness assumption: x's limbs are in the range (-BASE, 2*BASE).
// Soundness assumption: x's limbs are in the range (-2**107.49, 2**107.49).
func is_zero{range_check_ptr}(x: BigInt3) -> (res: felt) {
func is_zero{range_check_ptr}(x: SumBigInt3) -> (res: felt) {
%{
from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack

Expand All @@ -115,7 +116,9 @@ func is_zero{range_check_ptr}(x: BigInt3) -> (res: felt) {
value = x_inv = div_mod(1, x, SECP_P)
%}
let (x_inv) = nondet_bigint3();
let (x_x_inv) = unreduced_mul(x, x_inv);
// Note that we pass `SumBigInt3` to `unreduced_mul` so the bounds on
// `x_x_inv` are (-2**211.18, 2**211.18).
let (x_x_inv) = unreduced_mul(BigInt3(d0=x.d0, d1=x.d1, d2=x.d2), 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));
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 @@ -3,15 +3,15 @@ from starkware.cairo.common.cairo_builtins import BitwiseBuiltin
from starkware.cairo.common.cairo_keccak.keccak import cairo_keccak_uint256s_bigend, finalize_keccak
from starkware.cairo.common.cairo_secp.bigint import (
BASE,
BigInt3,
UnreducedBigInt3,
bigint_mul,
bigint_to_uint256,
nondet_bigint3,
uint256_to_bigint,
)
from starkware.cairo.common.cairo_secp.bigint3 import BigInt3, UnreducedBigInt3
from starkware.cairo.common.cairo_secp.constants import BETA, N0, N1, N2
from starkware.cairo.common.cairo_secp.ec import EcPoint, ec_add, ec_mul, ec_negate
from starkware.cairo.common.cairo_secp.ec import ec_add, ec_mul, ec_negate
from starkware.cairo.common.cairo_secp.ec_point import EcPoint
from starkware.cairo.common.cairo_secp.field import (
is_zero,
reduce,
Expand Down
1 change: 1 addition & 0 deletions src/starkware/cairo/common/secp256r1/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ cairo_library(
srcs = [
"bigint.cairo",
"constants.cairo",
"ec.cairo",
"field.cairo",
],
visibility = ["//visibility:public"],
Expand Down
2 changes: 1 addition & 1 deletion src/starkware/cairo/common/secp256r1/bigint.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from starkware.cairo.common.cairo_secp.bigint import BigInt3
from starkware.cairo.common.cairo_secp.bigint3 import BigInt3
from starkware.cairo.common.secp256r1.constants import BASE, D2_BOUND

// Returns a BigInt3 instance whose value is controlled by a prover hint.
Expand Down
23 changes: 20 additions & 3 deletions src/starkware/cairo/common/secp256r1/constants.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ const SECP_REM0 = 1;
const SECP_REM1 = -(2 ** 10);
const SECP_REM2 = 0xffffffff00000;

// The following constants represent the size of the secp256r1 field:
// p = P0 + BASE * P1 + BASE**2 * P2.
const P0 = 0x3fffffffffffffffffffff;
const P1 = 0x3ff;
const P2 = 0xffffffff0000000100000;

// Curve alpha and beta.
const ALPHA = -3;

// Beta = 0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b.
const BETA0 = 0x13b0f63bce3c3e27d2604b;
const BETA1 = 0x3555da621af194741ac331;
const BETA2 = 0x5ac635d8aa3a93e7b3ebb;

// Constants for unreduced_mul/sqr.
// See unreduced_mul for more detail.

Expand All @@ -27,10 +41,13 @@ const BASE3_MOD_P2 = 2 ** 54 - 2 ** 22;
const BASE3_MOD_P1 = -(2 ** 12);
const BASE3_MOD_P0 = 4;

// BASE4_MOD_P2 || BASE4_MOD_P1 || BASE4_MOD_P0 =
// (BASE4_MOD_P2 || BASE4_MOD_P1 || BASE4_MOD_P0) + p =
// (2**(86*4)) mod p =
// 255 * 2**248 - 2 ** 224 + 2**184 + 254 * 2**184 - 2**152 + 2 ** 96 + 2**88 + 2**56 - 1 =
// (-2**76 - 2**12) * 2**(86*2) - (-2**66 + 4) * 2**86 + 2**56.
// (-2**76 - 2**12) * 2**(86*2) + (-2**66 + 4) * 2**86 + 2**56 + p.
const BASE4_MOD_P2 = (-(2 ** 76)) - 2 ** 12;
const BASE4_MOD_P1 = (-(2 ** 66)) + 4;
const BASE4_MOD_P0 = 2 ** 56;

// The high and low uint128 parts of SECP256r1_PRIME.
const SECP_PRIME_HIGH = 0xffffffff000000010000000000000000;
const SECP_PRIME_LOW = 0xffffffffffffffffffffffff;
Loading

0 comments on commit 9e6e0e9

Please sign in to comment.