From e533494c544584130bf8fba320993ffb11ad11a8 Mon Sep 17 00:00:00 2001 From: feltroidprime Date: Thu, 20 Jun 2024 12:36:08 +0200 Subject: [PATCH] hydra: Add G1Point to Cairo struct util --- hydra/definitions.py | 5 ++++- hydra/hints/io.py | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/hydra/definitions.py b/hydra/definitions.py index 870cb933..dac20f1b 100644 --- a/hydra/definitions.py +++ b/hydra/definitions.py @@ -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 @@ -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. diff --git a/hydra/hints/io.py b/hydra/hints/io.py index 82549a10..bd4db710 100644 --- a/hydra/hints/io.py +++ b/hydra/hints/io.py @@ -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):