From d63386d7a48cd78243b68e780bc287b94b520d35 Mon Sep 17 00:00:00 2001 From: feltroidprime Date: Tue, 13 Aug 2024 10:36:30 +0200 Subject: [PATCH] adds projective_to_affine tool --- hydra/definitions.py | 4 + .../examples/snarkjs_vk_bn254.json | 184 +++++++++--------- .../parsing_utils.py | 16 +- 3 files changed, 106 insertions(+), 98 deletions(-) diff --git a/hydra/definitions.py b/hydra/definitions.py index 90407f29..c1452e22 100644 --- a/hydra/definitions.py +++ b/hydra/definitions.py @@ -49,6 +49,10 @@ class CurveID(Enum): def from_str(s: str) -> "CurveID": return CurveID(CurveID.find_value_in_string(s)) + @property + def p(self) -> int: + return CURVES[self.value].p + @staticmethod def find_value_in_string(s: str) -> int | None: """ diff --git a/tools/starknet/groth16_contract_generator/examples/snarkjs_vk_bn254.json b/tools/starknet/groth16_contract_generator/examples/snarkjs_vk_bn254.json index c690ae2f..24dd8281 100644 --- a/tools/starknet/groth16_contract_generator/examples/snarkjs_vk_bn254.json +++ b/tools/starknet/groth16_contract_generator/examples/snarkjs_vk_bn254.json @@ -1,94 +1,94 @@ { - "protocol": "groth16", - "curve": "bn128", - "nPublic": 1, - "vk_alpha_1": [ - "20491192805390485299153009773594534940189261866228447918068658471970481763042", - "9383485363053290200918347156157836566562967994039712273449902621266178545958", - "1" - ], - "vk_beta_2": [ - [ - "6375614351688725206403948262868962793625744043794305715222011528459656738731", - "4252822878758300859123897981450591353533073413197771768651442665752259397132" - ], - [ - "10505242626370262277552901082094356697409835680220590971873171140371331206856", - "21847035105528745403288232691147584728191162732299865338377159692350059136679" - ], - [ - "1", - "0" - ] - ], - "vk_gamma_2": [ - [ - "10857046999023057135944570762232829481370756359578518086990519993285655852781", - "11559732032986387107991004021392285783925812861821192530917403151452391805634" - ], - [ - "8495653923123431417604973247489272438418190587263600148770280649306958101930", - "4082367875863433681332203403145435568316851327593401208105741076214120093531" - ], - [ - "1", - "0" - ] - ], - "vk_delta_2": [ - [ - "10857046999023057135944570762232829481370756359578518086990519993285655852781", - "11559732032986387107991004021392285783925812861821192530917403151452391805634" - ], - [ - "8495653923123431417604973247489272438418190587263600148770280649306958101930", - "4082367875863433681332203403145435568316851327593401208105741076214120093531" - ], - [ - "1", - "0" - ] - ], - "vk_alphabeta_12": [ - [ - [ - "2029413683389138792403550203267699914886160938906632433982220835551125967885", - "21072700047562757817161031222997517981543347628379360635925549008442030252106" - ], - [ - "5940354580057074848093997050200682056184807770593307860589430076672439820312", - "12156638873931618554171829126792193045421052652279363021382169897324752428276" - ], - [ - "7898200236362823042373859371574133993780991612861777490112507062703164551277", - "7074218545237549455313236346927434013100842096812539264420499035217050630853" - ] - ], - [ - [ - "7077479683546002997211712695946002074877511277312570035766170199895071832130", - "10093483419865920389913245021038182291233451549023025229112148274109565435465" - ], - [ - "4595479056700221319381530156280926371456704509942304414423590385166031118820", - "19831328484489333784475432780421641293929726139240675179672856274388269393268" - ], - [ - "11934129596455521040620786944827826205713621633706285934057045369193958244500", - "8037395052364110730298837004334506829870972346962140206007064471173334027475" - ] - ] - ], - "IC": [ - [ - "3230230166848506278169341429844025995277520944155248223069557471517720414099", - "13103770257244981396389858672913686503786254567452595604017418062281627967708", - "1" - ], - [ - "19371697418061315618343891460787183627139127309393053314424436252400705071207", - "9445383417235588302514232777371752216736256846043789115945856987874292878586", - "1" - ] - ] + "protocol": "groth16", + "curve": "bn128", + "nPublic": 1, + "vk_alpha_1": [ + "20491192805390485299153009773594534940189261866228447918068658471970481763042", + "9383485363053290200918347156157836566562967994039712273449902621266178545958", + "1" + ], + "vk_beta_2": [ + [ + "6375614351688725206403948262868962793625744043794305715222011528459656738731", + "4252822878758300859123897981450591353533073413197771768651442665752259397132" + ], + [ + "10505242626370262277552901082094356697409835680220590971873171140371331206856", + "21847035105528745403288232691147584728191162732299865338377159692350059136679" + ], + [ + "1", + "0" + ] + ], + "vk_gamma_2": [ + [ + "10857046999023057135944570762232829481370756359578518086990519993285655852781", + "11559732032986387107991004021392285783925812861821192530917403151452391805634" + ], + [ + "8495653923123431417604973247489272438418190587263600148770280649306958101930", + "4082367875863433681332203403145435568316851327593401208105741076214120093531" + ], + [ + "1", + "0" + ] + ], + "vk_delta_2": [ + [ + "10857046999023057135944570762232829481370756359578518086990519993285655852781", + "11559732032986387107991004021392285783925812861821192530917403151452391805634" + ], + [ + "8495653923123431417604973247489272438418190587263600148770280649306958101930", + "4082367875863433681332203403145435568316851327593401208105741076214120093531" + ], + [ + "1", + "0" + ] + ], + "vk_alphabeta_12": [ + [ + [ + "2029413683389138792403550203267699914886160938906632433982220835551125967885", + "21072700047562757817161031222997517981543347628379360635925549008442030252106" + ], + [ + "5940354580057074848093997050200682056184807770593307860589430076672439820312", + "12156638873931618554171829126792193045421052652279363021382169897324752428276" + ], + [ + "7898200236362823042373859371574133993780991612861777490112507062703164551277", + "7074218545237549455313236346927434013100842096812539264420499035217050630853" + ] + ], + [ + [ + "7077479683546002997211712695946002074877511277312570035766170199895071832130", + "10093483419865920389913245021038182291233451549023025229112148274109565435465" + ], + [ + "4595479056700221319381530156280926371456704509942304414423590385166031118820", + "19831328484489333784475432780421641293929726139240675179672856274388269393268" + ], + [ + "11934129596455521040620786944827826205713621633706285934057045369193958244500", + "8037395052364110730298837004334506829870972346962140206007064471173334027475" + ] + ] + ], + "IC": [ + [ + "3230230166848506278169341429844025995277520944155248223069557471517720414099", + "13103770257244981396389858672913686503786254567452595604017418062281627967708", + "1" + ], + [ + "19371697418061315618343891460787183627139127309393053314424436252400705071207", + "9445383417235588302514232777371752216736256846043789115945856987874292878586", + "1" + ] + ] } diff --git a/tools/starknet/groth16_contract_generator/parsing_utils.py b/tools/starknet/groth16_contract_generator/parsing_utils.py index 02cfd911..c0a03fa5 100644 --- a/tools/starknet/groth16_contract_generator/parsing_utils.py +++ b/tools/starknet/groth16_contract_generator/parsing_utils.py @@ -44,6 +44,15 @@ def try_parse_g1_point_from_key( return try_parse_g1_point(point, curve_id) +def proj_to_affine(x, y, z, curve_id: CurveID) -> G1Point: + x, y, z = io.to_int(x), io.to_int(y), io.to_int(z) + p = curve_id.p + z = pow(z, -1, p) + x = x * z % p + y = y * z % p + return G1Point(x=x, y=y, curve_id=curve_id) + + def try_parse_g1_point(point: Any, curve_id: CurveID = None) -> G1Point: if isinstance(point, dict): return G1Point( @@ -59,12 +68,7 @@ def try_parse_g1_point(point: Any, curve_id: CurveID = None) -> G1Point: curve_id=curve_id, ) elif len(point) == 3: - assert io.to_int(point[2]) == 1, f"Non standard projective coordinates" - return G1Point( - x=io.to_int(point[0]), - y=io.to_int(point[1]), - curve_id=curve_id, - ) + return proj_to_affine(point[0], point[1], point[2], curve_id) else: raise ValueError(f"Invalid point: {point}") else: