|
| 1 | +import sys |
| 2 | +import os |
| 3 | +from os.path import dirname |
| 4 | + |
| 5 | +sys.path.append(os.path.join(dirname(dirname(__file__)), 'thirdparty/bls-signatures/python-impl')) |
| 6 | + |
| 7 | +from schemes import PopSchemeMPL, PrivateKey as BLSSchemePrivateKey |
| 8 | + |
| 9 | +from crypto.identity.bls_private_key import BLSPrivateKey |
| 10 | + |
| 11 | + |
| 12 | +class ProofOfPossession: |
| 13 | + @staticmethod |
| 14 | + def derive_bls_private_key(passphrase: str) -> bytes: |
| 15 | + """Derives a BLS private key (32 bytes) from a BIP-39 mnemonic.""" |
| 16 | + return BLSPrivateKey.from_passphrase(passphrase).private_key |
| 17 | + |
| 18 | + @classmethod |
| 19 | + def derive_bls_public_key(cls, passphrase: str) -> str: |
| 20 | + """Derives the BLS12-381 G1 public key from a mnemonic. Returns hex (96 chars).""" |
| 21 | + sk = BLSSchemePrivateKey.from_bytes(cls.derive_bls_private_key(passphrase)) |
| 22 | + return bytes(sk.get_g1()).hex() |
| 23 | + |
| 24 | + @classmethod |
| 25 | + def build_proof_of_possession(cls, private_key_bytes: bytes) -> dict: |
| 26 | + """Builds proof of possession for a given private key. |
| 27 | +
|
| 28 | + Args: |
| 29 | + private_key_bytes: 32-byte BLS private key |
| 30 | +
|
| 31 | + Returns: |
| 32 | + dict with 'pk' (hex, 48 bytes G1) and 'pop' (hex, 96 bytes G2) |
| 33 | +
|
| 34 | + Raises: |
| 35 | + ValueError: if the key is not exactly 32 bytes or is the zero scalar |
| 36 | + """ |
| 37 | + if int.from_bytes(private_key_bytes, 'big') == 0: |
| 38 | + raise ValueError('BLS secret key must not be zero') |
| 39 | + |
| 40 | + sk = BLSSchemePrivateKey.from_bytes(private_key_bytes) |
| 41 | + pk = bytes(sk.get_g1()).hex() |
| 42 | + pop = bytes(PopSchemeMPL.pop_prove(sk)).hex() |
| 43 | + |
| 44 | + return {'pk': pk, 'pop': pop} |
| 45 | + |
| 46 | + @classmethod |
| 47 | + def from_passphrase(cls, passphrase: str) -> dict: |
| 48 | + """Convenience: derives private key from mnemonic and builds PoP. |
| 49 | +
|
| 50 | + Returns: |
| 51 | + dict with 'pk' (hex, 48 bytes G1) and 'pop' (hex, 96 bytes G2) |
| 52 | + """ |
| 53 | + return cls.build_proof_of_possession(cls.derive_bls_private_key(passphrase)) |
0 commit comments