-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scripts for generating events and args
- Loading branch information
Showing
7 changed files
with
354 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
from nostr.key import PublicKey | ||
from garaga import garaga_rs | ||
from garaga.definitions import CurveID, G1Point, CURVES | ||
from hashlib import sha256 | ||
import sys | ||
import json | ||
|
||
TWO128 = 2**128 | ||
|
||
|
||
def generate_args(events: list[dict], target: str) -> list: | ||
args_list = list(map(handle_event, events)) | ||
res = [len(args_list)] + [item for sublist in args_list for item in sublist] | ||
|
||
if target == "cairo-run": | ||
return [res] | ||
elif target == "execute": | ||
return [hex(len(res))] + list(map(hex, res)) | ||
else: | ||
raise NotImplementedError(target) | ||
|
||
|
||
def to_u256(value: int) -> list[int]: | ||
return [value % TWO128, value // TWO128] | ||
|
||
|
||
def hash_challenge(rx: int, px: int, msg: int) -> int: | ||
tagged_hash = sha256(b"BIP0340/challenge").digest() | ||
input = tagged_hash + tagged_hash + rx.to_bytes(32, "big") + px.to_bytes(32, "big") + msg.to_bytes(32, "big") | ||
return int.from_bytes(sha256(input).digest(), "big") | ||
|
||
|
||
def gen_msm_hint(generator_point: G1Point, pk_point: G1Point, s: int, e_neg: int): | ||
return garaga_rs.msm_calldata_builder( | ||
[generator_point.x, generator_point.y, pk_point.x, pk_point.y], | ||
[s, e_neg], | ||
CurveID.SECP256K1.value, | ||
False, # include_digits_decomposition | ||
False, # include_points_and_scalars | ||
False, # serialize_as_pure_felt252_array | ||
False, # risc0_mode | ||
) | ||
|
||
|
||
def derive_point_from_x(x, is_even): | ||
""" | ||
Derive the EC point (x, y) from an x-coordinate on the secp256k1 curve. | ||
:param x: The x-coordinate (integer). | ||
:param is_even: Boolean indicating whether y should be even. | ||
:return: Tuple (x, y) representing the EC point. | ||
""" | ||
p = CURVES[CurveID.SECP256K1.value].p | ||
a = CURVES[CurveID.SECP256K1.value].a | ||
b = CURVES[CurveID.SECP256K1.value].b | ||
|
||
# Calculate y^2 = x^3 + ax + b mod p | ||
y_squared = (pow(x, 3, p) + a * x + b) % p | ||
|
||
# Compute modular square root of y^2 mod p | ||
# Using pow(y_squared, (p+1)//4, p) because p ≡ 3 mod 4 | ||
y = pow(y_squared, (p + 1) // 4, p) | ||
|
||
# Select the correct y based on its parity (even/odd) | ||
if is_even != (y % 2 == 0): | ||
y = p - y | ||
|
||
return (x, y) | ||
|
||
|
||
def handle_event(event: dict) -> dict: | ||
""" | ||
Generate the arguments for the Cairo program from a Nostr event. | ||
""" | ||
pubkey = PublicKey.from_npub(event["nostr_event"]["pubkey"]) | ||
px = int.from_bytes(pubkey.raw_bytes, "big") | ||
_, py = derive_point_from_x(px, is_even=True) | ||
|
||
generator_point = G1Point.get_nG(CurveID.SECP256K1, 1) | ||
pk_point = G1Point(px, py, CurveID.SECP256K1) | ||
|
||
sig = bytes.fromhex(event["nostr_event"]["sig"]) | ||
rx = int.from_bytes(sig[:32], "big") | ||
s = int.from_bytes(sig[32:], "big") | ||
|
||
msg = int.from_bytes(bytes.fromhex(event["nostr_event"]["id"]), "big") | ||
|
||
n = CURVES[CurveID.SECP256K1.value].n | ||
e = hash_challenge(rx, px, msg) | ||
e_neg = -e % n | ||
|
||
msm_hint = gen_msm_hint(generator_point, pk_point, s, e_neg) | ||
|
||
return [ | ||
*to_u256(msg), | ||
*to_u256(px), | ||
*to_u256(py), | ||
*to_u256(rx), | ||
*to_u256(s), | ||
*msm_hint[1:] # remove `include_digits_decomposition` flag | ||
] | ||
|
||
|
||
if __name__ == "__main__": | ||
if len(sys.argv) < 3: | ||
print("Usage: python gen_args.py [--file <path_to_events_file>] [--target <cairo-run|execute>]") | ||
sys.exit(1) | ||
|
||
if sys.argv[1] == "--file": | ||
with open(sys.argv[2], "r") as f: | ||
events = json.load(f) | ||
else: | ||
events = json.load(sys.stdin) | ||
|
||
if sys.argv[3] == "--target": | ||
target = sys.argv[4] | ||
else: | ||
target = "cairo-run" | ||
|
||
args = generate_args(events, target) | ||
print(json.dumps(args, indent=2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from nostr.event import Event | ||
from nostr.key import PrivateKey | ||
import sys | ||
import json | ||
|
||
def create_events(n: int) -> list[Event]: | ||
events = [] | ||
for i in range(n): | ||
private_key = PrivateKey() | ||
pubkey = private_key.public_key.bech32() | ||
event = Event(content=f"Hello Nostr {i}", public_key=pubkey) | ||
private_key.sign_event(event) | ||
events.append({ | ||
"nostr_event": { | ||
"id": event.id, | ||
"pubkey": event.public_key, | ||
"created_at": event.created_at, | ||
"kind": event.kind, | ||
"tags": event.tags, | ||
"content": event.content, | ||
"sig": event.signature | ||
}, | ||
}) | ||
return events | ||
|
||
|
||
if __name__ == "__main__": | ||
n = int(sys.argv[1]) if len(sys.argv) > 1 else 1 | ||
print(json.dumps(create_events(n), indent=2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
nostr | ||
garaga |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,94 @@ | ||
[ | ||
[ | ||
10, | ||
176683710998859414156510166937075682067, | ||
55187569459576321201976262906450820100, | ||
103018626115879340940156672162587808532, | ||
283354871996071695367827140893070637678, | ||
323217734837204111194663387748061068920, | ||
227025623678857638419828685269652196105, | ||
248550569182882525011147985680794684321, | ||
185543153997424577621662074306385317717, | ||
321683181709118817039983274483047669288, | ||
42267025566013959189042772957622143536, | ||
282186115298669849357680718271515504871, | ||
74174979888019477146628241660990578076, | ||
59647002998893167551541406592639228912, | ||
2006891320325329638218477292538078621, | ||
248154017807703263357493592649212850954, | ||
138381720657927309985284544519345575170, | ||
148744588917840615261243811741857149882, | ||
141057244195168382099210133920701844279, | ||
291637480240667569555617862015067958764, | ||
172212438602713169846304280867936812536, | ||
88429217285425834469386952984750135301, | ||
90052632381617690862779709172506843696, | ||
233392539768451385485249988684194332743, | ||
101804211845475116355471365414992813389, | ||
92983453126315756573839316488494459457, | ||
304029299067133961475684666102307849076, | ||
115084866868023324550197233398671227093, | ||
197423518168374585955573128099435834144, | ||
194841870855722186227759052141720851681, | ||
73577160656722067792100540469977161511, | ||
2088651611370386202002342597673718576, | ||
284983010337460821007801615210775795912, | ||
73084152721846261111523505766542303558, | ||
302929169992816324987631466772485047158, | ||
163740857613635257857843842454099133654, | ||
230602618326603246476548111457076398961, | ||
330772702377428670323154472141838142230, | ||
14678431222878906837115936589921444183, | ||
49781217625623672968224497405635330381, | ||
322537656097823987191993233738681635250, | ||
151310156318545578016728589367446683544, | ||
70476916596863831898524842113557855620, | ||
201250895240336970866539305310609928036, | ||
179228454669919827490109329616350333969, | ||
48182982213257889923285056432722851796, | ||
145055406951144615028388819558450611288, | ||
317397708672856206902605444996773346595, | ||
235070250494603010185293028303066833835, | ||
156626351406083014087155295048858249473, | ||
194135234188444802015657376368707850664, | ||
14618163030964347062724469490814573817, | ||
260250062083395098846423086586986987459, | ||
83379332393104322399885644580430438870, | ||
108748044103738873209707679419720982353, | ||
28123546126348923084577046862350787689, | ||
45322461631575825062745449440415915633, | ||
272808406049168894359289587857342757946, | ||
200215864395889144396989066965555611141, | ||
28025254197471244353835872492685275633, | ||
158647387444647495036779217787554847392, | ||
101142118930602222165033063086114221502, | ||
317329501206013545764574548054029599868, | ||
324228049811937954352138425464639346016, | ||
325980195984253045469140785504501301934, | ||
46168404813516151502387599086788265824, | ||
47510472652967433254792195482765073385, | ||
207749091307363032751985061586513009913, | ||
295472677552297121268755157613551039728, | ||
201658199759824870878721787571043829542, | ||
158199959246644099136717980403747113287, | ||
163601168516338991014873144090873564374, | ||
100361580049423788988240629845819539119, | ||
281895563799455251887779182869662140884, | ||
107315608079932796946067580753363820390, | ||
237294227831679206904235037008295761021, | ||
21448701622860211280096664177695857508, | ||
210006471634380156694855553190513986199, | ||
194346227992696867026405517996421438796, | ||
153302712486214439850652939848040274081, | ||
249226732206589138701277795792159629154 | ||
] | ||
] | ||
[ | ||
1, | ||
316728805692154399577501862185318617779, | ||
257879032140084096192261785669741661070, | ||
192665412190732774660665560761415271284, | ||
239947528589859604124319393921573535345, | ||
108828521036272927520402118985660171744, | ||
295032471521177437639943303653942921360, | ||
107993035224931664794018437522410950354, | ||
7470030516050449431494822200369846320, | ||
255043856528719127575241187776623470709, | ||
163878247974765754077853081726250868832, | ||
1269117931205306112497796646512794130913364833233502995882, | ||
2107100646637228472, | ||
250988474796987505092263077732731995207714445750865297316, | ||
13761732054541360511, | ||
1874965804584435998575591247421983100950004323035521870981, | ||
6633676535694854236, | ||
2946362282248449607045072261598964367258174189943832322415, | ||
548223396738322666, | ||
2242513043897659917604559668526979446889690428485864042701, | ||
13967522053078853286, | ||
721540102088333899607116262662416040530993076475194327579, | ||
13338506240705633660, | ||
5, | ||
3742131274051226834490470034738367564731992271149147657557, | ||
9344618707678174642, | ||
2440089479507282535674497019846414219914338449528532553159, | ||
10633427904356444699, | ||
1209858941058730561416900786357439368853086525403266993975, | ||
8785379005227405872, | ||
117811614066551652518697743112344729786721385135247018111, | ||
17679275951475010286, | ||
5648223030770217571686229568912331093147647477949445521360, | ||
16297533348686097498, | ||
6, | ||
4687235591335692582749157655521927453865639247885600534551, | ||
367119714520678003, | ||
3633084393516918058383211137319641055004078265296096445768, | ||
10058615299476782027, | ||
430897929605234202720694860171356702951293371514908026005, | ||
10635288572894033931, | ||
2582439379406123732292735075546930810969497532892145449025, | ||
11229211759811618220, | ||
3136545501491421428972817599431021888642047968828180969093, | ||
10851789577026835309, | ||
1, | ||
0, | ||
6, | ||
3853685632431693441447607370489733269991826584180615578339, | ||
303350180097595560, | ||
5030776792585497757992844744892872333836714929292363872882, | ||
8225194504904176050, | ||
2100363688141736423973714300103162085952817109196881765362, | ||
1073104467656024128, | ||
3165388073924318958368433124694870935775269908737034303014, | ||
865643159154034040, | ||
718312489204038399341917630652530066908603824476002080440, | ||
10489240389944871259, | ||
4753584192985642267413520462389931625361223732194879282458, | ||
7895253328009066784, | ||
9, | ||
1425140462416444260065156472615160096547697512879031177377, | ||
2569838001644746026, | ||
323183813071703353339320268406821720619126079229421973611, | ||
15070074875208819345, | ||
3016285507236639419044864021199496920659053600621536055127, | ||
660043715420031053, | ||
3933006041018516417290934914727443882345055644755695012130, | ||
5184625738363799082, | ||
480495962410145005849776640506128611088992269254405050927, | ||
12234166043826422730, | ||
430897929605234202720694860171356702951293371514908026012, | ||
10635288572894033931, | ||
2582439379406123732292735075546930810969497532892145449025, | ||
11229211759811618220, | ||
3136545501491421428972817599431021888642047968828180969093, | ||
10851789577026835309, | ||
1, | ||
0, | ||
5703066425612722395511761724160851155807686875572385096313, | ||
2585320936149193547, | ||
4, | ||
2428018959741770701567147547004839660425768362357232180313, | ||
4839305780371471489, | ||
4479531526492717469050442872188322447570971008831931078408, | ||
9155870516891554903, | ||
2765983103760271919314185882696588844399923953459023492247, | ||
7572403498964874564, | ||
327582421112812302401547924651534796370938622266761213952, | ||
2447719134557225208 | ||
] | ||
] |
Oops, something went wrong.