From 0da2cb27b56b13bb858bae2bfeb61ea68ef31377 Mon Sep 17 00:00:00 2001 From: feltroidprime Date: Fri, 9 Aug 2024 12:52:59 +0200 Subject: [PATCH] add pre commit hooks and setup --- .github/workflows/hydra.yml | 4 +++- .pre-commit-config.yaml | 17 +++++++++++++++++ hydra/definitions.py | 25 ++++++++++++------------- hydra/hints/multi_miller_witness.py | 6 ++++-- hydra/modulo_circuit.py | 17 +++++++++-------- hydra/precompiled_circuits/honk.py | 9 ++++++--- pyproject.toml | 21 ++++++++++++++++++++- tools/make/requirements.txt | 3 ++- tools/make/setup.sh | 3 +++ tools/starknet/e2e_tests_writer/msm.py | 13 ++++++++----- 10 files changed, 84 insertions(+), 34 deletions(-) create mode 100644 .pre-commit-config.yaml diff --git a/.github/workflows/hydra.yml b/.github/workflows/hydra.yml index 1897904a4..e44a59e49 100644 --- a/.github/workflows/hydra.yml +++ b/.github/workflows/hydra.yml @@ -17,7 +17,7 @@ jobs: uses: fkirc/skip-duplicate-actions@v5 with: concurrent_skipping: 'same_content_newer' - + test-hydra: needs: pre_job if: needs.pre_job.outputs.should_skip != 'true' @@ -35,6 +35,8 @@ jobs: source venv/bin/activate echo 'export PYTHONPATH="$PWD:$PYTHONPATH"' >> venv/bin/activate pip install -r tools/make/requirements.txt + - name: Check Black formatting + run: source venv/bin/activate && black --check . - name: Set up Rust uses: actions-rs/toolchain@v1 with: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 000000000..1b4ed2a0a --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,17 @@ +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.4.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: check-toml +- repo: https://github.com/psf/black + rev: 23.7.0 + hooks: + - id: black +- repo: https://github.com/pycqa/isort + rev: 5.12.0 + hooks: + - id: isort + args: ["--profile", "black"] diff --git a/hydra/definitions.py b/hydra/definitions.py index 892729fc5..f9731ab77 100644 --- a/hydra/definitions.py +++ b/hydra/definitions.py @@ -3,9 +3,9 @@ from dataclasses import dataclass from enum import Enum from typing import TypeAlias -from fastecdsa import curvemath - +import garaga_rs +from fastecdsa import curvemath from starkware.python.math_utils import EcInfinity, ec_safe_add, ec_safe_mult from hydra.algebra import ( @@ -17,7 +17,6 @@ PyFelt, ) from hydra.hints.io import bigint_split, int_to_u256, int_to_u384 -import garaga_rs N_LIMBS = 4 BASE = 2**96 @@ -843,9 +842,7 @@ def add(self, other: "G2Point") -> "G2Point": c = garaga_rs.g2_add(self.curve_id.value, a, b) return G2Point((c[0], c[1]), (c[2], c[3]), self.curve_id) else: - raise NotImplementedError( - "G2Point.add is not implemented for this curve" - ) + raise NotImplementedError("G2Point.add is not implemented for this curve") def __neg__(self) -> "G2Point": p = CURVES[self.curve_id.value].p @@ -892,9 +889,11 @@ def to_pyfelt_list(self) -> list[PyFelt]: @staticmethod def pair(pairs: list["G1G2Pair"], curve_id: CurveID = None): - from hydra.hints.tower_backup import E12 # avoids cycle + from hydra.hints.tower_backup import E12 # avoids cycle + if curve_id == None: - if len(pairs) == 0: raise ValueError("Unspecified curve") + if len(pairs) == 0: + raise ValueError("Unspecified curve") curve_id = pairs[0].curve_id if curve_id.value in GARAGA_RS_SUPPORTED_CURVES: args = [] @@ -910,15 +909,15 @@ def pair(pairs: list["G1G2Pair"], curve_id: CurveID = None): res = garaga_rs.multi_pairing(curve_id.value, args) return E12(res, curve_id.value) else: - raise NotImplementedError( - "G1G2Pair.pair is not implemented for this curve" - ) + raise NotImplementedError("G1G2Pair.pair is not implemented for this curve") @staticmethod def miller(pairs: list["G1G2Pair"], curve_id: CurveID = None): - from hydra.hints.tower_backup import E12 # avoids cycle + from hydra.hints.tower_backup import E12 # avoids cycle + if curve_id == None: - if len(pairs) == 0: raise ValueError("Unspecified curve") + if len(pairs) == 0: + raise ValueError("Unspecified curve") curve_id = pairs[0].curve_id if curve_id.value in GARAGA_RS_SUPPORTED_CURVES: args = [] diff --git a/hydra/hints/multi_miller_witness.py b/hydra/hints/multi_miller_witness.py index a916b3f6d..06d788fa7 100644 --- a/hydra/hints/multi_miller_witness.py +++ b/hydra/hints/multi_miller_witness.py @@ -1,10 +1,12 @@ import math +import garaga_rs + from hydra.algebra import PyFelt -from hydra.definitions import CURVES, CurveID, G1Point, G2Point, G1G2Pair +from hydra.definitions import CURVES, CurveID, G1G2Pair, G1Point, G2Point from hydra.hints.bls import get_root_and_scaling_factor_bls from hydra.hints.tower_backup import E12 -import garaga_rs + def get_final_exp_witness(curve_id: int, f: E12) -> tuple[E12, E12]: """ diff --git a/hydra/modulo_circuit.py b/hydra/modulo_circuit.py index cb8f79a8d..965046448 100644 --- a/hydra/modulo_circuit.py +++ b/hydra/modulo_circuit.py @@ -477,7 +477,6 @@ def add( b: ModuloCircuitElement, comment: str | None = None, ) -> ModuloCircuitElement: - if a is None and type(b) == ModuloCircuitElement: return b elif b is None and type(a) == ModuloCircuitElement: @@ -874,13 +873,13 @@ def write_cairo1_input_stack( if i + 2 < len_stack: code += f"\t let (in{start_index+i}, in{start_index+i+1}, in{start_index+i+2}) = (CE::> {{}}, CE::> {{}}, CE::> {{}});\n" offset_to_reference_map[offsets[i]] = f"in{start_index+i}" - offset_to_reference_map[offsets[i+1]] = f"in{start_index+i+1}" - offset_to_reference_map[offsets[i+2]] = f"in{start_index+i+2}" + offset_to_reference_map[offsets[i + 1]] = f"in{start_index+i+1}" + offset_to_reference_map[offsets[i + 2]] = f"in{start_index+i+2}" i += 3 elif i + 1 < len_stack: code += f"\t let (in{start_index+i}, in{start_index+i+1}) = (CE::> {{}}, CE::> {{}});\n" offset_to_reference_map[offsets[i]] = f"in{start_index+i}" - offset_to_reference_map[offsets[i+1]] = f"in{start_index+i+1}" + offset_to_reference_map[offsets[i + 1]] = f"in{start_index+i+1}" i += 2 else: code += f"\t let in{start_index+i} = CE::> {{}};\n" @@ -985,10 +984,12 @@ def compile_circuit_cairo_1( code, offset_to_reference_map, start_index = self.write_cairo1_input_stack( WriteOps.CONSTANT, code, {}, 0 ) - code, offset_to_reference_map, commit_start_index = ( - self.write_cairo1_input_stack( - WriteOps.INPUT, code, offset_to_reference_map, start_index - ) + ( + code, + offset_to_reference_map, + commit_start_index, + ) = self.write_cairo1_input_stack( + WriteOps.INPUT, code, offset_to_reference_map, start_index ) code, offset_to_reference_map, commit_end_index = self.write_cairo1_input_stack( WriteOps.COMMIT, code, offset_to_reference_map, commit_start_index diff --git a/hydra/precompiled_circuits/honk.py b/hydra/precompiled_circuits/honk.py index 1dc8ee8ff..77b4df1d1 100644 --- a/hydra/precompiled_circuits/honk.py +++ b/hydra/precompiled_circuits/honk.py @@ -39,7 +39,8 @@ def keccak256(b: bytes) -> bytes: def bn256_add(p1: tuple[int, int], p2: tuple[int, int]) -> tuple[int, int]: - from hydra.definitions import G1Point # shadowed locally + from hydra.definitions import G1Point # shadowed locally + a = G1Point(p1[0], p1[1], CurveID.BN254) b = G1Point(p2[0], p2[1], CurveID.BN254) c = a.add(b) @@ -47,7 +48,8 @@ def bn256_add(p1: tuple[int, int], p2: tuple[int, int]) -> tuple[int, int]: def bn256_scalar_mul(p1: tuple[int, int], scalar: int) -> tuple[int, int]: - from hydra.definitions import G1Point # shadowed locally + from hydra.definitions import G1Point # shadowed locally + a = G1Point(p1[0], p1[1], CurveID.BN254) b = a.scalar_mul(scalar) return (b.x, b.y) @@ -56,7 +58,8 @@ def bn256_scalar_mul(p1: tuple[int, int], scalar: int) -> tuple[int, int]: def bn256_pairing( p1_list: list[tuple[int, int]], p2_list: list[tuple[int, int, int, int]] ) -> bool: - from hydra.definitions import G1Point, G2Point # shadowed locally + from hydra.definitions import G1Point, G2Point # shadowed locally + assert len(p1_list) == len(p2_list) data = [] for i in range(len(p1_list)): diff --git a/pyproject.toml b/pyproject.toml index 12d383dca..6388799a7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,4 +7,23 @@ profile = "black" [tool.pytest.ini_options] cache_dir = "build/.pytest_cache" testpaths = ["tests/hydra"] -# addopts = "--tb=short --showlocals" \ No newline at end of file +# addopts = "--tb=short --showlocals" + +[tool.black] +line-length = 88 +target-version = ['py310'] +include = '\.pyi?$' +extend-exclude = ''' +/( + # directories + \.eggs + | \.git + | \.hg + | \.mypy_cache + | \.tox + | \.venv + | \venv + | build + | dist +)/ +''' diff --git a/tools/make/requirements.txt b/tools/make/requirements.txt index 499efd068..0bd87c6b9 100644 --- a/tools/make/requirements.txt +++ b/tools/make/requirements.txt @@ -6,4 +6,5 @@ tabulate pyarrow maturin python-dotenv -pytest \ No newline at end of file +pytest +black diff --git a/tools/make/setup.sh b/tools/make/setup.sh index 30803241d..20e32c401 100755 --- a/tools/make/setup.sh +++ b/tools/make/setup.sh @@ -47,6 +47,9 @@ echo "PROJECT_ROOT=$PWD" > .env source venv/bin/activate pip install -r tools/make/requirements.txt +# Install the commit hooks (black, isort) +pre-commit install + echo "Applying patch to instances.py..." patch venv/lib/python3.10/site-packages/starkware/cairo/lang/instances.py < tools/make/instances.patch diff --git a/tools/starknet/e2e_tests_writer/msm.py b/tools/starknet/e2e_tests_writer/msm.py index 34e2416cf..4a00ae979 100644 --- a/tools/starknet/e2e_tests_writer/msm.py +++ b/tools/starknet/e2e_tests_writer/msm.py @@ -1,14 +1,13 @@ from dataclasses import dataclass +from functools import lru_cache from hydra import modulo_circuit_structs as structs -from hydra.algebra import PyFelt, FunctionFelt +from hydra.algebra import FunctionFelt, PyFelt from hydra.definitions import STARK, CurveID, G1Point, get_base_field from hydra.hints import ecip, io from hydra.hints.neg_3 import neg_3_base_le from hydra.poseidon_transcript import CairoPoseidonTranscript -from functools import lru_cache - @dataclass(slots=True) class MSMCalldataBuilder: @@ -114,7 +113,9 @@ def build_msm_hints(self) -> tuple[structs.Struct, structs.Struct]: _Q_low, _SumDlogDivLow = ecip.zk_ecip_hint(self.points, scalars_low) _Q_high, _SumDlogDivHigh = ecip.zk_ecip_hint(self.points, scalars_high) - _Q_high_shifted, _SumDlogDivHighShifted = ecip.zk_ecip_hint([_Q_high], [2**128]) + _Q_high_shifted, _SumDlogDivHighShifted = ecip.zk_ecip_hint( + [_Q_high], [2**128] + ) _x_coordinate = self._retrieve_random_x_coordinate( _Q_low, _Q_high, @@ -247,7 +248,9 @@ def _get_input_structs(self): return inputs def to_cairo_1_test(self): - print(f"Generating MSM test for {self.curve_id.name} with {len(self.scalars)} points") + print( + f"Generating MSM test for {self.curve_id.name} with {len(self.scalars)} points" + ) test_name = f"test_msm_{self.curve_id.name}_{len(self.scalars)}P" inputs = self._get_input_structs()