staking-deposit-cli/eth2deposit/key_handling/key_derivation/tree.py
Hsiao-Wei Wang 96200d76ae
Package reorg (#10)
* Move tests to /tests

* Rename `src` to `eth2deposit`

* Fix path

* Fix path and move eth2deposit/utils/credentials.py -> eth2deposit/credentials.py

* Update Makefile

* Kick CI cache

* Fix package setup

* Fix path back

* PR feedback from Carl
2020-05-08 21:34:09 +08:00

54 lines
1.6 KiB
Python

from eth2deposit.utils.crypto import (
HKDF,
SHA256,
)
from py_ecc.optimized_bls12_381 import curve_order as bls_curve_order
from typing import List
def _flip_bits_256(input: int) -> int:
"""
Flips 256 bits worth of `input`.
"""
return input ^ (2**256 - 1)
def _IKM_to_lamport_SK(*, IKM: bytes, salt: bytes) -> List[bytes]:
"""
Derives the lamport SK for a given `IKM` and `salt`.
"""
OKM = HKDF(salt=salt, IKM=IKM, L=8160)
lamport_SK = [OKM[i: i + 32] for i in range(0, 8160, 32)]
return lamport_SK
def _parent_SK_to_lamport_PK(*, parent_SK: int, index: int) -> bytes:
"""
Derives the `index`th child's lamport PK from the `parent_SK`.
"""
salt = index.to_bytes(4, byteorder='big')
IKM = parent_SK.to_bytes(32, byteorder='big')
lamport_0 = _IKM_to_lamport_SK(IKM=IKM, salt=salt)
not_IKM = _flip_bits_256(parent_SK).to_bytes(32, byteorder='big')
lamport_1 = _IKM_to_lamport_SK(IKM=not_IKM, salt=salt)
lamport_SKs = lamport_0 + lamport_1
lamport_PKs = [SHA256(sk) for sk in lamport_SKs]
compressed_PK = SHA256(b''.join(lamport_PKs))
return compressed_PK
def _HKDF_mod_r(*, IKM: bytes) -> int:
okm = HKDF(salt=b'BLS-SIG-KEYGEN-SALT-', IKM=IKM, L=48)
return int.from_bytes(okm, byteorder='big') % bls_curve_order
def derive_child_SK(*, parent_SK: int, index: int) -> int:
assert(index >= 0 and index < 2**32)
lamport_PK = _parent_SK_to_lamport_PK(parent_SK=parent_SK, index=index)
return _HKDF_mod_r(IKM=lamport_PK)
def derive_master_SK(seed: bytes) -> int:
assert(len(seed) >= 16)
return _HKDF_mod_r(IKM=seed)