Skip to content

Commit

Permalink
hydra: Add G1Point to Cairo struct util
Browse files Browse the repository at this point in the history
  • Loading branch information
feltroidprime committed Jun 20, 2024
1 parent 1d78e7e commit e533494
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 4 additions & 1 deletion hydra/definitions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from hydra.algebra import Polynomial, BaseField, PyFelt, ModuloCircuitElement
from hydra.hints.io import bigint_split
from hydra.hints.io import bigint_split, int_to_u384

from starkware.python.math_utils import ec_safe_mult, EcInfinity, ec_safe_add
from dataclasses import dataclass
Expand Down Expand Up @@ -301,6 +301,9 @@ def __post_init__(self):
if not self.is_on_curve():
raise ValueError(f"Point {self} is not on the curve")

def to_cairo_1(self) -> str:
return f"G1Point{{x: {int_to_u384(self.x)}, y: {int_to_u384(self.y)}}};"

def is_on_curve(self) -> bool:
"""
Check if the point is on the curve using the curve equation y^2 = x^3 + ax + b.
Expand Down
5 changes: 5 additions & 0 deletions hydra/hints/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ def bigint_split(x: int, n_limbs: int, base: int):
return coeffs[::-1]


def int_to_u384(x: int) -> str:
limbs = bigint_split(x, 4, 2**96)
return f"u384{{limb0:{limbs[0]}, limb1:{limbs[1]}, limb2:{limbs[2]}, limb3:{limbs[3]}}}"


def bigint_pack(x: object, n_limbs: int, base: int) -> int:
val = 0
for i in range(n_limbs):
Expand Down

0 comments on commit e533494

Please sign in to comment.