Skip to content

Commit

Permalink
Merge pull request #8 from petscheit/feat/g1_point_decompression
Browse files Browse the repository at this point in the history
Feat/g1 point decompression
  • Loading branch information
petscheit authored Dec 18, 2024
2 parents 13e3111 + 27c57fb commit e49f8f5
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/wasm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:
run: |
sudo apt-get update
sudo apt-get install -y \
libnss3 libxss1 libasound2 libatk-bridge2.0-0 libatk1.0-0 \
libnss3 libxss1 libasound2t64 libatk-bridge2.0-0 libatk1.0-0 \
libcups2 libxcomposite1 libxdamage1 libxrandr2 libgbm1 \
libpango-1.0-0 libpangocairo-1.0-0 libpangoft2-1.0-0 libgtk-3-0
- name: Download package file as artifact
Expand Down
56 changes: 56 additions & 0 deletions hydra/garaga/precompiled_circuits/ec.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,62 @@ def _derive_point_from_x(
return (rhs, grhs, should_be_rhs, should_be_grhs, rhs_sqrt)


class DeriveG1PointFromX(ModuloCircuit):
"""
A class to derive the y-coordinate of a G1 point on an elliptic curve given the x-coordinate.
This class is a specialized ModuloCircuit that uses the curve parameters to compute the y-coordinate
from a given x-coordinate, ensuring that the point lies on the curve. The s_bit can be extracted from the compressed G1 point.
"""

def __init__(self, name: str, curve_id: int, compilation_mode: int = 0):
super().__init__(
name=name,
curve_id=curve_id,
generic_circuit=True,
compilation_mode=compilation_mode,
)
self.curve = CURVES[curve_id]

def derive_y_from_x(
self,
b: ModuloCircuitElement,
x: ModuloCircuitElement,
s_bit: ModuloCircuitElement, # S bit to determine y-coordinate
) -> ModuloCircuitElement:
"""
Derive the y-coordinate from the given x-coordinate on the elliptic curve.
:param x: The x-coordinate as a ModuloCircuitElement.
:param s_bit: A bit to select which y-coordinate to use (0 for smaller, 1 for larger).
:return: The y-coordinate as a ModuloCircuitElement.
:raises AssertionError: If the x-coordinate does not lie on the curve.
"""
# y^2 = x^3 + b
x3 = self.mul(x, self.mul(x, x))
rhs = self.add(x3, b)

# Ensure rhs is a quadratic residue
assert is_quad_residue(rhs.value, self.field.p), "x coordinate is not on curve"

# Compute both possible y values
y1 = self.field(sqrt_mod_p(rhs.value, self.field.p))
y2 = self.field.p - y1 # Negative of y1

# Select y based on s_bit - use larger value if s_bit=1, smaller value if s_bit=0
y = y2 if (y1 < y2) == s_bit.value else y1

y_coord = self.write_element(
y,
WriteOps.WITNESS,
)

# Validate the y-coordinate
self.mul_and_assert(y_coord, y_coord, rhs)

return y_coord


class ECIPCircuits(ModuloCircuit):
def __init__(
self,
Expand Down
2 changes: 1 addition & 1 deletion tools/npm/garaga_ts/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# - It will update the code generated under src/wasm/pkg
# Usage: docker compose up --build && docker compose down

FROM rust:1.80.1
FROM rust:1.81.0

RUN cargo install wasm-pack

Expand Down
1 change: 1 addition & 0 deletions tools/npm/garaga_ts/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

services:
app:
platform: linux/amd64
build: .
volumes:
- "../../..:/garaga"
2 changes: 1 addition & 1 deletion tools/npm/garaga_ts/src/wasm/pkg/garaga_rs_bg.wasm.js

Large diffs are not rendered by default.

0 comments on commit e49f8f5

Please sign in to comment.