Skip to content

Commit 8411e81

Browse files
more compat
1 parent e1c9c51 commit 8411e81

1 file changed

Lines changed: 253 additions & 3 deletions

File tree

internal_filesystem/lib/secp256k1_compat.py

Lines changed: 253 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ def new(self, type_str):
1818
return bytearray(size)
1919
elif 'size_t *' in type_str:
2020
return [0]
21+
elif type_str == 'secp256k1_pubkey *':
22+
return bytearray(64) # Placeholder for pubkey
23+
elif type_str == 'secp256k1_ecdsa_signature *':
24+
return bytearray(64) # Placeholder for signature
25+
elif type_str == 'secp256k1_ecdsa_recoverable_signature *':
26+
return bytearray(65) # Placeholder for recoverable signature
27+
elif type_str == 'secp256k1_xonly_pubkey *':
28+
return bytearray(32) # Placeholder for xonly pubkey
29+
elif type_str == 'secp256k1_keypair *':
30+
return bytearray(96) # Placeholder for keypair
2131
raise ValueError(f"Unsupported ffi type: {type_str}")
2232

2333
def buffer(self, obj, size=None):
@@ -31,9 +41,8 @@ def memmove(self, dst, src, n):
3141
dst[:n] = src[:n]
3242

3343
def callback(self, signature):
34-
# Simplified decorator to mark functions without setting attributes
3544
def decorator(func):
36-
return func # Return func as-is
45+
return func
3746
return decorator
3847

3948
# Dummy lib class to map to usecp256k1 functions
@@ -46,6 +55,13 @@ class Lib:
4655
def secp256k1_context_create(self, flags):
4756
return object()
4857

58+
def secp256k1_ec_seckey_verify(self, ctx, seckey):
59+
try:
60+
return usecp256k1.usecp256k1_ec_seckey_verify(seckey)
61+
except AttributeError:
62+
# Placeholder until usecp256k1 supports this
63+
raise NotImplementedError("secp256k1_ec_seckey_verify not implemented in usecp256k1")
64+
4965
def secp256k1_ecdsa_signature_serialize_der(self, ctx, output, outputlen, raw_sig):
5066
try:
5167
result = usecp256k1.usecp256k1_ecdsa_signature_serialize_der(raw_sig)
@@ -54,7 +70,241 @@ def secp256k1_ecdsa_signature_serialize_der(self, ctx, output, outputlen, raw_si
5470
output[:len(result)] = result
5571
outputlen[0] = len(result)
5672
return 1
57-
except ValueError:
73+
except (ValueError, AttributeError):
74+
return 0
75+
76+
def secp256k1_ecdsa_signature_parse_der(self, ctx, raw_sig, ser_sig, ser_len):
77+
try:
78+
result = usecp256k1.usecp256k1_ecdsa_signature_parse_der(ser_sig)
79+
if result is None:
80+
return 0
81+
raw_sig[:] = result
82+
return 1
83+
except (ValueError, AttributeError):
84+
return 0
85+
86+
def secp256k1_ecdsa_signature_serialize_compact(self, ctx, output, raw_sig):
87+
try:
88+
result = usecp256k1.usecp256k1_ecdsa_signature_serialize_compact(raw_sig)
89+
if result is None:
90+
return 0
91+
output[:64] = result
92+
return 1
93+
except (ValueError, AttributeError):
94+
return 0
95+
96+
def secp256k1_ecdsa_signature_parse_compact(self, ctx, raw_sig, ser_sig):
97+
try:
98+
result = usecp256k1.usecp256k1_ecdsa_signature_parse_compact(ser_sig)
99+
if result is None:
100+
return 0
101+
raw_sig[:] = result
102+
return 1
103+
except (ValueError, AttributeError):
104+
return 0
105+
106+
def secp256k1_ecdsa_signature_normalize(self, ctx, sigout, raw_sig):
107+
try:
108+
is_normalized = usecp256k1.usecp256k1_ecdsa_signature_normalize(raw_sig)
109+
if sigout != FFI.NULL:
110+
sigout[:] = is_normalized[1] if is_normalized[1] else raw_sig
111+
return is_normalized[0]
112+
except (ValueError, AttributeError):
113+
return 0
114+
115+
def secp256k1_ecdsa_sign(self, ctx, raw_sig, msg32, privkey, nonce_fn, nonce_data):
116+
try:
117+
result = usecp256k1.usecp256k1_ecdsa_sign(msg32, privkey)
118+
if result is None:
119+
return 0
120+
raw_sig[:] = result
121+
return 1
122+
except (ValueError, AttributeError):
123+
return 0
124+
125+
def secp256k1_ecdsa_verify(self, ctx, raw_sig, msg32, pubkey):
126+
try:
127+
return usecp256k1.usecp256k1_ecdsa_verify(raw_sig, msg32, pubkey)
128+
except (ValueError, AttributeError):
129+
return 0
130+
131+
def secp256k1_ecdsa_recoverable_signature_serialize_compact(self, ctx, output, recid, recover_sig):
132+
try:
133+
result, rec_id = usecp256k1.usecp256k1_ecdsa_recoverable_signature_serialize_compact(recover_sig)
134+
if result is None:
135+
return 0
136+
output[:64] = result
137+
recid[0] = rec_id
138+
return 1
139+
except (ValueError, AttributeError):
140+
return 0
141+
142+
def secp256k1_ecdsa_recoverable_signature_parse_compact(self, ctx, recover_sig, ser_sig, rec_id):
143+
try:
144+
result = usecp256k1.usecp256k1_ecdsa_recoverable_signature_parse_compact(ser_sig, rec_id)
145+
if result is None:
146+
return 0
147+
recover_sig[:] = result
148+
return 1
149+
except (ValueError, AttributeError):
150+
return 0
151+
152+
def secp256k1_ecdsa_recoverable_signature_convert(self, ctx, normal_sig, recover_sig):
153+
try:
154+
result = usecp256k1.usecp256k1_ecdsa_recoverable_signature_convert(recover_sig)
155+
if result is None:
156+
return 0
157+
normal_sig[:] = result
158+
return 1
159+
except (ValueError, AttributeError):
160+
return 0
161+
162+
def secp256k1_ecdsa_sign_recoverable(self, ctx, raw_sig, msg32, privkey, nonce_fn, nonce_data):
163+
try:
164+
result = usecp256k1.usecp256k1_ecdsa_sign_recoverable(msg32, privkey)
165+
if result is None:
166+
return 0
167+
raw_sig[:] = result
168+
return 1
169+
except (ValueError, AttributeError):
170+
return 0
171+
172+
def secp256k1_ecdsa_recover(self, ctx, pubkey, recover_sig, msg32):
173+
try:
174+
result = usecp256k1.usecp256k1_ecdsa_recover(recover_sig, msg32)
175+
if result is None:
176+
return 0
177+
pubkey[:] = result
178+
return 1
179+
except (ValueError, AttributeError):
180+
return 0
181+
182+
def secp256k1_schnorrsig_sign_custom(self, ctx, sig64, msg, msg_len, keypair, aux_rand32):
183+
try:
184+
result = usecp256k1.usecp256k1_schnorrsig_sign_custom(msg, keypair)
185+
if result is None:
186+
return 0
187+
sig64[:64] = result
188+
return 1
189+
except (ValueError, AttributeError):
190+
return 0
191+
192+
def secp256k1_schnorrsig_verify(self, ctx, schnorr_sig, msg, msg_len, xonly_pubkey):
193+
try:
194+
return usecp256k1.usecp256k1_schnorrsig_verify(schnorr_sig, msg, xonly_pubkey)
195+
except (ValueError, AttributeError):
196+
return 0
197+
198+
def secp256k1_tagged_sha256(self, ctx, hash32, tag, tag_len, msg, msg_len):
199+
try:
200+
result = usecp256k1.usecp256k1_tagged_sha256(tag, msg)
201+
if result is None:
202+
return 0
203+
hash32[:32] = result
204+
return 1
205+
except (ValueError, AttributeError):
206+
return 0
207+
208+
def secp256k1_ec_pubkey_serialize(self, ctx, output, outlen, pubkey, flags):
209+
try:
210+
result = usecp256k1.usecp256k1_ec_pubkey_serialize(pubkey, flags)
211+
if result is None:
212+
return 0
213+
output[:outlen[0]] = result
214+
return 1
215+
except (ValueError, AttributeError):
216+
return 0
217+
218+
def secp256k1_ec_pubkey_parse(self, ctx, pubkey, pubkey_ser, ser_len):
219+
try:
220+
result = usecp256k1.usecp256k1_ec_pubkey_parse(pubkey_ser)
221+
if result is None:
222+
return 0
223+
pubkey[:] = result
224+
return 1
225+
except (ValueError, AttributeError):
226+
return 0
227+
228+
def secp256k1_ec_pubkey_combine(self, ctx, outpub, pubkeys, n_pubkeys):
229+
try:
230+
result = usecp256k1.usecp256k1_ec_pubkey_combine(pubkeys)
231+
if result is None:
232+
return 0
233+
outpub[:] = result
234+
return 1
235+
except (ValueError, AttributeError):
236+
return 0
237+
238+
def secp256k1_ec_pubkey_tweak_add(self, ctx, pubkey, scalar):
239+
try:
240+
result = usecp256k1.usecp256k1_ec_pubkey_tweak_add(pubkey, scalar)
241+
if result is None:
242+
return 0
243+
pubkey[:] = result
244+
return 1
245+
except (ValueError, AttributeError):
246+
return 0
247+
248+
def secp256k1_ec_pubkey_tweak_mul(self, ctx, pubkey, scalar):
249+
try:
250+
result = usecp256k1.usecp256k1_ec_pubkey_tweak_mul(pubkey, scalar)
251+
if result is None:
252+
return 0
253+
pubkey[:] = result
254+
return 1
255+
except (ValueError, AttributeError):
256+
return 0
257+
258+
def secp256k1_ec_pubkey_create(self, ctx, pubkey, privkey):
259+
try:
260+
result = usecp256k1.usecp256k1_ec_pubkey_create(privkey)
261+
if result is None:
262+
return 0
263+
pubkey[:] = result
264+
return 1
265+
except (ValueError, AttributeError):
266+
return 0
267+
268+
def secp256k1_xonly_pubkey_from_pubkey(self, ctx, xonly_pubkey, pk_parity, pubkey):
269+
try:
270+
result, parity = usecp256k1.usecp256k1_xonly_pubkey_from_pubkey(pubkey)
271+
if result is None:
272+
return 0
273+
xonly_pubkey[:] = result
274+
if pk_parity != FFI.NULL:
275+
pk_parity[0] = parity
276+
return 1
277+
except (ValueError, AttributeError):
278+
return 0
279+
280+
def secp256k1_ec_privkey_tweak_add(self, ctx, privkey, scalar):
281+
try:
282+
result = usecp256k1.usecp256k1_ec_privkey_tweak_add(privkey, scalar)
283+
if result is None:
284+
return 0
285+
privkey[:] = result
286+
return 1
287+
except (ValueError, AttributeError):
288+
return 0
289+
290+
def secp256k1_ec_privkey_tweak_mul(self, ctx, privkey, scalar):
291+
try:
292+
result = usecp256k1.usecp256k1_ec_privkey_tweak_mul(privkey, scalar)
293+
if result is None:
294+
return 0
295+
privkey[:] = result
296+
return 1
297+
except (ValueError, AttributeError):
298+
return 0
299+
300+
def secp256k1_keypair_create(self, ctx, keypair, privkey):
301+
try:
302+
result = usecp256k1.usecp256k1_keypair_create(privkey)
303+
if result is None:
304+
return 0
305+
keypair[:] = result
306+
return 1
307+
except (ValueError, AttributeError):
58308
return 0
59309

60310
def secp256k1_ecdh(self, ctx, output, pubkey, seckey, hashfn=FFI.NULL, hasharg=FFI.NULL):

0 commit comments

Comments
 (0)