-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathchash.py
69 lines (43 loc) · 1.34 KB
/
chash.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from buidl._libsec import ffi, lib
GLOBAL_CTX = ffi.gc(
lib.secp256k1_context_create(
lib.SECP256K1_CONTEXT_SIGN | lib.SECP256K1_CONTEXT_VERIFY
),
lib.secp256k1_context_destroy,
)
def tagged_hash(tag, msg):
result = ffi.new("unsigned char [32]")
tag_length = len(tag)
msg_length = len(msg)
if not lib.secp256k1_tagged_sha256(
GLOBAL_CTX,
result,
tag,
tag_length,
msg,
msg_length,
):
raise RuntimeError("libsecp256k1 tagged hash problem")
return bytes(ffi.buffer(result, 32))
def hash_aux(msg):
return tagged_hash(b"BIP0340/aux", msg)
def hash_challenge(msg):
return tagged_hash(b"BIP0340/challenge", msg)
def hash_keyaggcoef(msg):
return tagged_hash(b"KeyAgg coefficient", msg)
def hash_keyagglist(msg):
return tagged_hash(b"KeyAgg list", msg)
def hash_musignonce(msg):
return tagged_hash(b"MuSig/noncecoef", msg)
def hash_nonce(msg):
return tagged_hash(b"BIP0340/nonce", msg)
def hash_tapbranch(msg):
return tagged_hash(b"TapBranch", msg)
def hash_tapleaf(msg):
return tagged_hash(b"TapLeaf", msg)
def hash_tapsighash(msg):
return tagged_hash(b"TapSighash", msg)
def hash_taptweak(msg):
return tagged_hash(b"TapTweak", msg)
def hash_bip322message(msg):
return tagged_hash(b"BIP0322-signed-message",msg)