Skip to content

Instantly share code, notes, and snippets.

View keybase.md

Keybase proof

I hereby claim:

  • I am gitzhou on github.
  • I am cnaaron67 (https://keybase.io/cnaaron67) on keybase.
  • I have a public key ASAOAgz885E7g1x6QrHSfT94q5QvYaIRLqZmhMyeKJuoKgo

To claim this, I am signing this object:

View signed_magnitude.py
from typing import Literal
def unsigned_to_bytes(num: int, byteorder: Literal['big', 'little'] = 'big') -> bytes:
"""
convert an unsigned int to the least number of bytes as possible.
"""
# (0).bit_length() == 0
return num.to_bytes((num.bit_length() + 7) // 8 or 1, byteorder)
@gitzhou
gitzhou / rfc6979.py
Created Feb 1, 2022
RFC6979 Python demo implementation
View rfc6979.py
import hashlib
import hmac
import math
from typing import Union, Literal
def unsigned_to_bytes(num: int, byteorder: Literal['big', 'little'] = 'big') -> bytes:
"""
convert an unsigned int to the least number of bytes as possible.
"""
View rabin.py
import hashlib
def hash_to_int(x: bytes) -> int:
hx = hashlib.sha256(x).digest()
for i in range(11):
hx += hashlib.sha256(hx).digest()
return int.from_bytes(hx, 'little')
View sign_message.py
from meta import int_to_varint, public_key_to_address, address_to_public_key_hash, public_key_hash
from modular_inverse import modular_multiplicative_inverse
from ec_point_operation import curve, add, scalar_multiply
from sign import hash_to_int, sign_recoverable, verify_signature
from base64 import b64encode, b64decode
def message_bytes(message: str) -> bytes:
msg_bytes = message.encode('utf-8')
return int_to_varint(len(msg_bytes)) + msg_bytes
View sign_transaction.py
from ec_point_operation import curve, scalar_multiply
from meta import int_to_varint, address_to_public_key_hash, build_locking_script, deserialize_signature, serialize_signature, serialize_public_key
from crypto import double_sha256
from sign import verify_signature, sign
from collections import namedtuple
from binascii import unhexlify, hexlify
VERSION = 0x01.to_bytes(4, 'little')
SEQUENCE = 0xffffffff.to_bytes(4, byteorder='little')
LOCK_TIME = 0x00.to_bytes(4, byteorder='little')
View sign.py
from ec_point_operation import curve, add, scalar_multiply
from modular_inverse import modular_multiplicative_inverse
from crypto import double_sha256
import random
def hash_to_int(message: bytes) -> int:
"""Calculate the bitcoin double-sha256 hash of the message, return as an integer"""
h = double_sha256(message)
return int.from_bytes(h, byteorder='big')
View ec_point_operation.py
import collections
from modular_inverse import modular_multiplicative_inverse
EllipticCurve = collections.namedtuple('EllipticCurve', 'name p a b g n h')
curve = EllipticCurve(
name='Secp256k1',
p=0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f,
a=0,
b=7,
View modular_inverse.py
def extended_euclid_gcd(a: int, b: int) -> list:
"""
Returns [gcd(a, b), x, y] where ax + by = gcd(a, b)
"""
s, old_s = 0, 1
t, old_t = 1, 0
r, old_r = b, a
while r != 0:
quotient = old_r // r
old_r, r = r, old_r - quotient * r
View crypto.py
import hashlib
from binascii import hexlify
def sha256(payload: bytes) -> bytes:
return hashlib.sha256(payload).digest()
def double_sha256(payload: bytes) -> bytes:
return sha256(sha256(payload))