Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pre commit hooks and setup #145

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/hydra.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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:
Expand Down
17 changes: 17 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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: 24.4.2
hooks:
- id: black
- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
- id: isort
args: ["--profile", "black"]
25 changes: 12 additions & 13 deletions hydra/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = []
Expand All @@ -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 = []
Expand Down
6 changes: 4 additions & 2 deletions hydra/hints/multi_miller_witness.py
Original file line number Diff line number Diff line change
@@ -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]:
"""
Expand Down
17 changes: 9 additions & 8 deletions hydra/modulo_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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::<CI<{start_index+i}>> {{}}, CE::<CI<{start_index+i+1}>> {{}}, CE::<CI<{start_index+i+2}>> {{}});\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::<CI<{start_index+i}>> {{}}, CE::<CI<{start_index+i+1}>> {{}});\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::<CI<{start_index+i}>> {{}};\n"
Expand Down Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions hydra/precompiled_circuits/honk.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,17 @@ 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)
return (c.x, c.y)


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)
Expand All @@ -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)):
Expand Down
21 changes: 20 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,23 @@ profile = "black"
[tool.pytest.ini_options]
cache_dir = "build/.pytest_cache"
testpaths = ["tests/hydra"]
# addopts = "--tb=short --showlocals"
# 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
)/
'''
5 changes: 4 additions & 1 deletion tools/make/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ tabulate
pyarrow
maturin
python-dotenv
pytest
pytest
black==24.4.2
pre-commit
web3==5.31.0
3 changes: 3 additions & 0 deletions tools/make/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 5 additions & 4 deletions tools/starknet/e2e_tests_writer/msm.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -247,7 +246,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()

Expand Down
Loading