mirror of
https://github.com/bitcoin/bips.git
synced 2026-03-16 15:55:37 +00:00
git-subtree-dir: bip-0352/secp256k1lab git-subtree-split: 44dc4bd893b8f03e621585e3bf255253e0e0fbfb
25 lines
645 B
Python
25 lines
645 B
Python
import hashlib
|
|
|
|
|
|
# This implementation can be sped up by storing the midstate after hashing
|
|
# tag_hash instead of rehashing it all the time.
|
|
def tagged_hash(tag: str, msg: bytes) -> bytes:
|
|
tag_hash = hashlib.sha256(tag.encode()).digest()
|
|
return hashlib.sha256(tag_hash + tag_hash + msg).digest()
|
|
|
|
|
|
def bytes_from_int(x: int) -> bytes:
|
|
return x.to_bytes(32, byteorder="big")
|
|
|
|
|
|
def xor_bytes(b0: bytes, b1: bytes) -> bytes:
|
|
return bytes(x ^ y for (x, y) in zip(b0, b1))
|
|
|
|
|
|
def int_from_bytes(b: bytes) -> int:
|
|
return int.from_bytes(b, byteorder="big")
|
|
|
|
|
|
def hash_sha256(b: bytes) -> bytes:
|
|
return hashlib.sha256(b).digest()
|