-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathproof_of_possession.py
More file actions
53 lines (39 loc) · 1.86 KB
/
Copy pathproof_of_possession.py
File metadata and controls
53 lines (39 loc) · 1.86 KB
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
import sys
import os
from os.path import dirname
sys.path.append(os.path.join(dirname(dirname(__file__)), 'thirdparty/bls-signatures/python-impl'))
from schemes import PopSchemeMPL, PrivateKey as BLSSchemePrivateKey
from crypto.identity.bls_private_key import BLSPrivateKey
class ProofOfPossession:
@staticmethod
def derive_bls_private_key(passphrase: str) -> bytes:
"""Derives a BLS private key (32 bytes) from a BIP-39 mnemonic."""
return BLSPrivateKey.from_passphrase(passphrase).private_key
@classmethod
def derive_bls_public_key(cls, passphrase: str) -> str:
"""Derives the BLS12-381 G1 public key from a mnemonic. Returns hex (96 chars)."""
sk = BLSSchemePrivateKey.from_bytes(cls.derive_bls_private_key(passphrase))
return bytes(sk.get_g1()).hex()
@classmethod
def build_proof_of_possession(cls, private_key_bytes: bytes) -> dict:
"""Builds proof of possession for a given private key.
Args:
private_key_bytes: 32-byte BLS private key
Returns:
dict with 'pk' (hex, 48 bytes G1) and 'pop' (hex, 96 bytes G2)
Raises:
ValueError: if the key is not exactly 32 bytes or is the zero scalar
"""
if int.from_bytes(private_key_bytes, 'big') == 0:
raise ValueError('BLS secret key must not be zero')
sk = BLSSchemePrivateKey.from_bytes(private_key_bytes)
pk = bytes(sk.get_g1()).hex()
pop = bytes(PopSchemeMPL.pop_prove(sk)).hex()
return {'pk': pk, 'pop': pop}
@classmethod
def from_passphrase(cls, passphrase: str) -> dict:
"""Convenience: derives private key from mnemonic and builds PoP.
Returns:
dict with 'pk' (hex, 48 bytes G1) and 'pop' (hex, 96 bytes G2)
"""
return cls.build_proof_of_possession(cls.derive_bls_private_key(passphrase))