Skip to content

Commit 02e29f4

Browse files
Replace ECIP by GLV/FakeGLV (#387)
1 parent a3b806b commit 02e29f4

File tree

94 files changed

+12631
-26260
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+12631
-26260
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ venv
1515
*.idea
1616
*.secrets
1717
*.pb.gz
18+
.hypothesis/
19+
*.go
1820

1921
tools/garaga_rs/target/
2022
tools/make/requirements.txt

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hydra/garaga/definitions.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ class WeierstrassCurve:
124124
Gx: int # x-coordinate of the generator point
125125
Gy: int # y-coordinate of the generator point
126126
swu_params: SWUParams
127+
eigen_value: int | None # Endomorphism eigenvalue
128+
third_root_of_unity: int | None # Endomorphism image
129+
130+
def is_endomorphism_available(self) -> bool:
131+
return self.eigen_value is not None and self.third_root_of_unity is not None
127132

128133
def to_cairo_zero(self) -> str:
129134
code = f"namespace {self.cairo_zero_namespace_name} {{\n"
@@ -187,13 +192,17 @@ def __init__(
187192
Gx: int,
188193
Gy: int,
189194
swu_params: SWUParams,
195+
eigen_value: int | None = None,
196+
third_root_of_unity: int | None = None,
190197
):
191198
assert a_twisted != 0 and d_twisted != 0 and a_twisted != d_twisted
192199
# Set attributes
193200
object.__setattr__(self, "d_twisted", d_twisted)
194201
object.__setattr__(self, "a_twisted", a_twisted)
195202
object.__setattr__(self, "p", p)
196203
object.__setattr__(self, "swu_params", swu_params)
204+
object.__setattr__(self, "eigen_value", eigen_value)
205+
object.__setattr__(self, "third_root_of_unity", third_root_of_unity)
197206
# Calculate Weierstrass parameters
198207
a = (
199208
-1
@@ -220,6 +229,8 @@ def __init__(
220229
fp_generator,
221230
*(self.to_weierstrass(Gx, Gy)),
222231
swu_params,
232+
eigen_value,
233+
third_root_of_unity,
223234
)
224235

225236
def to_weierstrass(self, x_twisted, y_twisted):
@@ -356,6 +367,8 @@ def bit(value, index):
356367
0x90689D0585FF075EC9E99AD690C3395BC4B313370B38EF355ACDADCD122975B,
357368
),
358369
swu_params=None,
370+
eigen_value=0xB3C4D79D41A917585BFC41088D8DAAA78B17EA66B99C90DD,
371+
third_root_of_unity=0x59E26BCEA0D48BACD4F263F1ACDB5C4F5763473177FFFFFE,
359372
),
360373
BLS12_381_ID: PairingCurve(
361374
cairo_zero_namespace_name="bls",
@@ -406,6 +419,8 @@ def bit(value, index):
406419
B=0x12E2908D11688030018B12E8753EEE3B2016C1F0F24F4070A0B9C14FCEF35EF55A23215A316CEAA5D1CC48E98E172BE0,
407420
Z=11,
408421
),
422+
eigen_value=0xAC45A4010001A40200000000FFFFFFFF,
423+
third_root_of_unity=0x1A0111EA397FE699EC02408663D4DE85AA0D857D89759AD4897D29650FB85F9B409427EB4F49FFFD8BFD00000000AAAC,
409424
),
410425
SECP256K1_ID: WeierstrassCurve(
411426
cairo_zero_namespace_name="secp256k1",
@@ -419,6 +434,8 @@ def bit(value, index):
419434
Gx=0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798,
420435
Gy=0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8,
421436
swu_params=None,
437+
eigen_value=0x5363AD4CC05C30E0A5261C028812645A122E22EA20816678DF02967C1B23BD72,
438+
third_root_of_unity=0x7AE96A2B657C07106E64479EAC3434E99CF0497512F58995C1396C28719501EE,
422439
),
423440
SECP256R1_ID: WeierstrassCurve(
424441
cairo_zero_namespace_name="secp256r1",
@@ -432,6 +449,8 @@ def bit(value, index):
432449
Gx=0x6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296,
433450
Gy=0x4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5,
434451
swu_params=None,
452+
eigen_value=None,
453+
third_root_of_unity=None,
435454
),
436455
ED25519_ID: TwistedEdwardsCurve(
437456
cairo_zero_namespace_name="ED25519", # See https://neuromancer.sk/std/other/Ed25519
@@ -445,6 +464,8 @@ def bit(value, index):
445464
Gx=0x216936D3CD6E53FEC0A4E231FDD6DC5C692CC7609525A7B2C9562D608F25D51A,
446465
Gy=0x6666666666666666666666666666666666666666666666666666666666666658,
447466
swu_params=None,
467+
eigen_value=None,
468+
third_root_of_unity=None,
448469
),
449470
GRUMPKIN_ID: WeierstrassCurve(
450471
cairo_zero_namespace_name="grumpkin",
@@ -458,6 +479,8 @@ def bit(value, index):
458479
Gx=0x1,
459480
Gy=0x2CF135E7506A45D632D270D45F1181294833FC48D823F272C,
460481
swu_params=None,
482+
eigen_value=None,
483+
third_root_of_unity=None,
461484
),
462485
}
463486

@@ -1293,6 +1316,37 @@ def recode_naf_bits(lst):
12931316
if __name__ == "__main__":
12941317
r = recode_naf_bits(jy00(6 * 0x44E992B44A6909F1 + 2)[2:])
12951318
print(r, len(r))
1319+
from garaga.hints.io import int_to_u384
12961320

12971321
# bls = [int(x) for x in bin(0xD201000000010000)[2:]][2:]
12981322
# recode_naf_bits(bls)
1323+
1324+
def print_nbits_and_nG_glv_fake_glv():
1325+
for curve_id in CURVES:
1326+
curve: WeierstrassCurve = CURVES[curve_id]
1327+
if curve.is_endomorphism_available():
1328+
nbits = curve.n.bit_length() // 4 + 9
1329+
print(
1330+
f"Curve {curve_id}: {nbits}, {G1Point.get_nG(CurveID(curve_id), 2 ** (nbits - 1)).to_cairo_1()}"
1331+
)
1332+
1333+
print_nbits_and_nG_glv_fake_glv()
1334+
1335+
def print_min_one_order():
1336+
for curve_id in CURVES:
1337+
curve: WeierstrassCurve = CURVES[curve_id]
1338+
print(f"Curve {curve_id}: min_one_order: {int_to_u384(-1 % curve.n)}")
1339+
1340+
print_min_one_order()
1341+
1342+
print()
1343+
1344+
def print_third_root_of_unity():
1345+
for curve_id in CURVES:
1346+
curve: WeierstrassCurve = CURVES[curve_id]
1347+
if curve.is_endomorphism_available():
1348+
print(
1349+
f"Curve {curve_id}: third_root_of_unity: {int_to_u384(curve.third_root_of_unity)}"
1350+
)
1351+
1352+
print_third_root_of_unity()

0 commit comments

Comments
 (0)