Created
February 1, 2022 22:04
-
-
Save gitzhou/87ae44da4d8681c5da7272152f4498a6 to your computer and use it in GitHub Desktop.
RFC6979 Python demo implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
""" | |
return num.to_bytes((num.bit_length() + 7) // 8 or 1, byteorder) | |
def bytes_to_bits(octets: Union[str, bytes]) -> str: | |
""" | |
convert bytes to binary 0/1 string | |
""" | |
b: bytes = octets if isinstance(octets, bytes) else bytes.fromhex(octets) | |
bits: str = bin(int.from_bytes(b, 'big'))[2:] | |
if len(bits) < len(b) * 8: | |
bits = '0' * (len(b) * 8 - len(bits)) + bits | |
return bits | |
def bits_to_bytes(bits: str) -> bytes: | |
""" | |
convert binary 0/1 string to the least number of bytes | |
""" | |
return unsigned_to_bytes(int(bits, 2)) | |
def _hmac(key: bytes, data: bytes, h=hashlib.sha256) -> bytes: | |
return hmac.new(key, data, h).digest() | |
class Rfc6979: | |
""" | |
https://datatracker.ietf.org/doc/html/rfc6979 | |
""" | |
def __init__(self, q: int): | |
# q is the size of the group (group order) | |
self.q: int = q | |
# the binary length of q. | |
# q_len is the smallest integer such that q is less than 2^q_len. | |
q_bits: str = bytes_to_bits(unsigned_to_bytes(q)).lstrip('0') | |
self.q_len: int = len(q_bits) | |
# r_len is equal to q_len, rounded up to the next multiple of | |
# 8 (if q_len is already a multiple of 8, then r_len equals q_len; | |
# otherwise, r_len is slightly larger, up to q_len+7 | |
self.r_len: int = 8 * math.ceil(self.q_len / 8) | |
# 2.3.2. Bit String to Integer | |
def bits_to_int(self, bits: str) -> int: | |
b_len: int = len(bits) | |
if self.q_len <= b_len: | |
bits = bits[:self.q_len] | |
else: | |
bits = (self.q_len - b_len) * '0' + bits | |
return int.from_bytes(bits_to_bytes(bits), 'big') | |
# 2.3.3. Integer to Octet String | |
def int_to_octets(self, num: int) -> bytes: | |
assert num < 2 ** self.q_len | |
return num.to_bytes(self.r_len // 8, 'big') | |
# 2.3.4. Bit String to Octet String | |
def bits_to_octets(self, bits: str) -> bytes: | |
return self.int_to_octets(self.bits_to_int(bits) % self.q) | |
# 3.2. Generation of k | |
def generate_k(self, x: int, m: bytes, h=hashlib.sha256) -> int: | |
"""Generation of k | |
:param x: private key | |
:param m: message | |
:param h: a hash name suitable for hashlib.new(). *OR* | |
a hashlib constructor returning a new hash object. *OR* | |
a module supporting PEP 247. | |
:return: deterministic "random" k | |
""" | |
assert h | |
# step a | |
h1: str = bytes_to_bits(h(m).digest()) | |
h_len: int = len(h1) | |
# step b | |
v: bytes = b'\x01' * math.ceil(h_len / 8) | |
# step c | |
_k: bytes = b'\x00' * math.ceil(h_len / 8) | |
# step d | |
_k = _hmac(_k, v + b'\x00' + self.int_to_octets(x) + self.bits_to_octets(h1), h) | |
# step e | |
v = _hmac(_k, v, h) | |
# step f | |
_k = _hmac(_k, v + b'\x01' + self.int_to_octets(x) + self.bits_to_octets(h1), h) | |
# step g | |
v = _hmac(_k, v, h) | |
while True: | |
# step h.1 | |
t: bytes = b'' | |
# step h.2 | |
while len(t) * 8 < self.q_len: | |
v = _hmac(_k, v, h) | |
t += v | |
# step h.3 | |
k: int = self.bits_to_int(bytes_to_bits(t)) | |
if 0 < k < self.q: | |
# k is good | |
return k | |
_k = _hmac(_k, v + b'\x00', h) | |
v = _hmac(_k, v, h) | |
if __name__ == '__main__': | |
test_vectors = [ | |
# | |
# q, x, m, h, expected k | |
# | |
# A.1.2.Generation of k | |
( | |
0x4000000000000000000020108A2E0CC0D99F8A5EF, | |
0x09A4D6792295A7F730FC3F2B49CBC0F62E862272F, | |
'sample'.encode('utf-8'), | |
hashlib.sha256, | |
0x23AF4074C90A02B3FE61D286D5C87F425E6BDD81B, | |
), | |
# A.2.1. DSA, 1024 Bits | |
( | |
0x996F967F6C8E388D9E28D01E205FBA957A5698B1, | |
0x411602CB19A6CCC34494D79D98EF1E7ED5AF25F7, | |
'sample'.encode('utf-8'), | |
hashlib.sha1, | |
0x7BDB6B0FF756E1BB5D53583EF979082F9AD5BD5B, | |
), | |
( | |
0x996F967F6C8E388D9E28D01E205FBA957A5698B1, | |
0x411602CB19A6CCC34494D79D98EF1E7ED5AF25F7, | |
'sample'.encode('utf-8'), | |
hashlib.sha224, | |
0x562097C06782D60C3037BA7BE104774344687649, | |
), | |
( | |
0x996F967F6C8E388D9E28D01E205FBA957A5698B1, | |
0x411602CB19A6CCC34494D79D98EF1E7ED5AF25F7, | |
'sample'.encode('utf-8'), | |
hashlib.sha256, | |
0x519BA0546D0C39202A7D34D7DFA5E760B318BCFB, | |
), | |
( | |
0x996F967F6C8E388D9E28D01E205FBA957A5698B1, | |
0x411602CB19A6CCC34494D79D98EF1E7ED5AF25F7, | |
'sample'.encode('utf-8'), | |
hashlib.sha384, | |
0x95897CD7BBB944AA932DBC579C1C09EB6FCFC595, | |
), | |
( | |
0x996F967F6C8E388D9E28D01E205FBA957A5698B1, | |
0x411602CB19A6CCC34494D79D98EF1E7ED5AF25F7, | |
'sample'.encode('utf-8'), | |
hashlib.sha512, | |
0x09ECE7CA27D0F5A4DD4E556C9DF1D21D28104F8B, | |
), | |
( | |
0x996F967F6C8E388D9E28D01E205FBA957A5698B1, | |
0x411602CB19A6CCC34494D79D98EF1E7ED5AF25F7, | |
'test'.encode('utf-8'), | |
hashlib.sha1, | |
0x5C842DF4F9E344EE09F056838B42C7A17F4A6433, | |
), | |
( | |
0x996F967F6C8E388D9E28D01E205FBA957A5698B1, | |
0x411602CB19A6CCC34494D79D98EF1E7ED5AF25F7, | |
'test'.encode('utf-8'), | |
hashlib.sha224, | |
0x4598B8EFC1A53BC8AECD58D1ABBB0C0C71E67297, | |
), | |
( | |
0x996F967F6C8E388D9E28D01E205FBA957A5698B1, | |
0x411602CB19A6CCC34494D79D98EF1E7ED5AF25F7, | |
'test'.encode('utf-8'), | |
hashlib.sha256, | |
0x5A67592E8128E03A417B0484410FB72C0B630E1A, | |
), | |
( | |
0x996F967F6C8E388D9E28D01E205FBA957A5698B1, | |
0x411602CB19A6CCC34494D79D98EF1E7ED5AF25F7, | |
'test'.encode('utf-8'), | |
hashlib.sha384, | |
0x220156B761F6CA5E6C9F1B9CF9C24BE25F98CD89, | |
), | |
( | |
0x996F967F6C8E388D9E28D01E205FBA957A5698B1, | |
0x411602CB19A6CCC34494D79D98EF1E7ED5AF25F7, | |
'test'.encode('utf-8'), | |
hashlib.sha512, | |
0x65D2C2EEB175E370F28C75BFCDC028D22C7DBE9C, | |
), | |
# A.2.2. DSA, 2048 Bits | |
( | |
0xF2C3119374CE76C9356990B465374A17F23F9ED35089BD969F61C6DDE9998C1F, | |
0x69C7548C21D0DFEA6B9A51C9EAD4E27C33D3B3F180316E5BCAB92C933F0E4DBC, | |
'sample'.encode('utf-8'), | |
hashlib.sha1, | |
0x888FA6F7738A41BDC9846466ABDB8174C0338250AE50CE955CA16230F9CBD53E, | |
), | |
( | |
0xF2C3119374CE76C9356990B465374A17F23F9ED35089BD969F61C6DDE9998C1F, | |
0x69C7548C21D0DFEA6B9A51C9EAD4E27C33D3B3F180316E5BCAB92C933F0E4DBC, | |
'sample'.encode('utf-8'), | |
hashlib.sha224, | |
0xBC372967702082E1AA4FCE892209F71AE4AD25A6DFD869334E6F153BD0C4D806, | |
), | |
( | |
0xF2C3119374CE76C9356990B465374A17F23F9ED35089BD969F61C6DDE9998C1F, | |
0x69C7548C21D0DFEA6B9A51C9EAD4E27C33D3B3F180316E5BCAB92C933F0E4DBC, | |
'sample'.encode('utf-8'), | |
hashlib.sha256, | |
0x8926A27C40484216F052F4427CFD5647338B7B3939BC6573AF4333569D597C52, | |
), | |
( | |
0xF2C3119374CE76C9356990B465374A17F23F9ED35089BD969F61C6DDE9998C1F, | |
0x69C7548C21D0DFEA6B9A51C9EAD4E27C33D3B3F180316E5BCAB92C933F0E4DBC, | |
'sample'.encode('utf-8'), | |
hashlib.sha384, | |
0xC345D5AB3DA0A5BCB7EC8F8FB7A7E96069E03B206371EF7D83E39068EC564920, | |
), | |
( | |
0xF2C3119374CE76C9356990B465374A17F23F9ED35089BD969F61C6DDE9998C1F, | |
0x69C7548C21D0DFEA6B9A51C9EAD4E27C33D3B3F180316E5BCAB92C933F0E4DBC, | |
'sample'.encode('utf-8'), | |
hashlib.sha512, | |
0x5A12994431785485B3F5F067221517791B85A597B7A9436995C89ED0374668FC, | |
), | |
( | |
0xF2C3119374CE76C9356990B465374A17F23F9ED35089BD969F61C6DDE9998C1F, | |
0x69C7548C21D0DFEA6B9A51C9EAD4E27C33D3B3F180316E5BCAB92C933F0E4DBC, | |
'test'.encode('utf-8'), | |
hashlib.sha1, | |
0x6EEA486F9D41A037B2C640BC5645694FF8FF4B98D066A25F76BE641CCB24BA4F, | |
), | |
( | |
0xF2C3119374CE76C9356990B465374A17F23F9ED35089BD969F61C6DDE9998C1F, | |
0x69C7548C21D0DFEA6B9A51C9EAD4E27C33D3B3F180316E5BCAB92C933F0E4DBC, | |
'test'.encode('utf-8'), | |
hashlib.sha224, | |
0x06BD4C05ED74719106223BE33F2D95DA6B3B541DAD7BFBD7AC508213B6DA6670, | |
), | |
( | |
0xF2C3119374CE76C9356990B465374A17F23F9ED35089BD969F61C6DDE9998C1F, | |
0x69C7548C21D0DFEA6B9A51C9EAD4E27C33D3B3F180316E5BCAB92C933F0E4DBC, | |
'test'.encode('utf-8'), | |
hashlib.sha256, | |
0x1D6CE6DDA1C5D37307839CD03AB0A5CBB18E60D800937D67DFB4479AAC8DEAD7, | |
), | |
( | |
0xF2C3119374CE76C9356990B465374A17F23F9ED35089BD969F61C6DDE9998C1F, | |
0x69C7548C21D0DFEA6B9A51C9EAD4E27C33D3B3F180316E5BCAB92C933F0E4DBC, | |
'test'.encode('utf-8'), | |
hashlib.sha384, | |
0x206E61F73DBE1B2DC8BE736B22B079E9DACD974DB00EEBBC5B64CAD39CF9F91C, | |
), | |
( | |
0xF2C3119374CE76C9356990B465374A17F23F9ED35089BD969F61C6DDE9998C1F, | |
0x69C7548C21D0DFEA6B9A51C9EAD4E27C33D3B3F180316E5BCAB92C933F0E4DBC, | |
'test'.encode('utf-8'), | |
hashlib.sha512, | |
0xAFF1651E4CD6036D57AA8B2A05CCF1A9D5A40166340ECBBDC55BE10B568AA0AA, | |
), | |
# A.2.3. ECDSA, 192 Bits (Prime Field) | |
( | |
0xFFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831, | |
0x6FAB034934E4C0FC9AE67F5B5659A9D7D1FEFD187EE09FD4, | |
'sample'.encode('utf-8'), | |
hashlib.sha1, | |
0x37D7CA00D2C7B0E5E412AC03BD44BA837FDD5B28CD3B0021, | |
), | |
( | |
0xFFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831, | |
0x6FAB034934E4C0FC9AE67F5B5659A9D7D1FEFD187EE09FD4, | |
'sample'.encode('utf-8'), | |
hashlib.sha224, | |
0x4381526B3FC1E7128F202E194505592F01D5FF4C5AF015D8, | |
), | |
( | |
0xFFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831, | |
0x6FAB034934E4C0FC9AE67F5B5659A9D7D1FEFD187EE09FD4, | |
'sample'.encode('utf-8'), | |
hashlib.sha256, | |
0x32B1B6D7D42A05CB449065727A84804FB1A3E34D8F261496, | |
), | |
( | |
0xFFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831, | |
0x6FAB034934E4C0FC9AE67F5B5659A9D7D1FEFD187EE09FD4, | |
'sample'.encode('utf-8'), | |
hashlib.sha384, | |
0x4730005C4FCB01834C063A7B6760096DBE284B8252EF4311, | |
), | |
( | |
0xFFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831, | |
0x6FAB034934E4C0FC9AE67F5B5659A9D7D1FEFD187EE09FD4, | |
'sample'.encode('utf-8'), | |
hashlib.sha512, | |
0xA2AC7AB055E4F20692D49209544C203A7D1F2C0BFBC75DB1, | |
), | |
( | |
0xFFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831, | |
0x6FAB034934E4C0FC9AE67F5B5659A9D7D1FEFD187EE09FD4, | |
'test'.encode('utf-8'), | |
hashlib.sha1, | |
0xD9CF9C3D3297D3260773A1DA7418DB5537AB8DD93DE7FA25, | |
), | |
( | |
0xFFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831, | |
0x6FAB034934E4C0FC9AE67F5B5659A9D7D1FEFD187EE09FD4, | |
'test'.encode('utf-8'), | |
hashlib.sha224, | |
0xF5DC805F76EF851800700CCE82E7B98D8911B7D510059FBE, | |
), | |
( | |
0xFFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831, | |
0x6FAB034934E4C0FC9AE67F5B5659A9D7D1FEFD187EE09FD4, | |
'test'.encode('utf-8'), | |
hashlib.sha256, | |
0x5C4CE89CF56D9E7C77C8585339B006B97B5F0680B4306C6C, | |
), | |
( | |
0xFFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831, | |
0x6FAB034934E4C0FC9AE67F5B5659A9D7D1FEFD187EE09FD4, | |
'test'.encode('utf-8'), | |
hashlib.sha384, | |
0x5AFEFB5D3393261B828DB6C91FBC68C230727B030C975693, | |
), | |
( | |
0xFFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831, | |
0x6FAB034934E4C0FC9AE67F5B5659A9D7D1FEFD187EE09FD4, | |
'test'.encode('utf-8'), | |
hashlib.sha512, | |
0x0758753A5254759C7CFBAD2E2D9B0792EEE44136C9480527, | |
), | |
# A.2.4. ECDSA, 224 Bits (Prime Field) | |
( | |
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D, | |
0xF220266E1105BFE3083E03EC7A3A654651F45E37167E88600BF257C1, | |
'sample'.encode('utf-8'), | |
hashlib.sha1, | |
0x7EEFADD91110D8DE6C2C470831387C50D3357F7F4D477054B8B426BC, | |
), | |
( | |
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D, | |
0xF220266E1105BFE3083E03EC7A3A654651F45E37167E88600BF257C1, | |
'sample'.encode('utf-8'), | |
hashlib.sha224, | |
0xC1D1F2F10881088301880506805FEB4825FE09ACB6816C36991AA06D, | |
), | |
( | |
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D, | |
0xF220266E1105BFE3083E03EC7A3A654651F45E37167E88600BF257C1, | |
'sample'.encode('utf-8'), | |
hashlib.sha256, | |
0xAD3029E0278F80643DE33917CE6908C70A8FF50A411F06E41DEDFCDC, | |
), | |
( | |
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D, | |
0xF220266E1105BFE3083E03EC7A3A654651F45E37167E88600BF257C1, | |
'sample'.encode('utf-8'), | |
hashlib.sha384, | |
0x52B40F5A9D3D13040F494E83D3906C6079F29981035C7BD51E5CAC40, | |
), | |
( | |
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D, | |
0xF220266E1105BFE3083E03EC7A3A654651F45E37167E88600BF257C1, | |
'sample'.encode('utf-8'), | |
hashlib.sha512, | |
0x9DB103FFEDEDF9CFDBA05184F925400C1653B8501BAB89CEA0FBEC14, | |
), | |
( | |
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D, | |
0xF220266E1105BFE3083E03EC7A3A654651F45E37167E88600BF257C1, | |
'test'.encode('utf-8'), | |
hashlib.sha1, | |
0x2519178F82C3F0E4F87ED5883A4E114E5B7A6E374043D8EFD329C253, | |
), | |
( | |
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D, | |
0xF220266E1105BFE3083E03EC7A3A654651F45E37167E88600BF257C1, | |
'test'.encode('utf-8'), | |
hashlib.sha224, | |
0xDF8B38D40DCA3E077D0AC520BF56B6D565134D9B5F2EAE0D34900524, | |
), | |
( | |
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D, | |
0xF220266E1105BFE3083E03EC7A3A654651F45E37167E88600BF257C1, | |
'test'.encode('utf-8'), | |
hashlib.sha256, | |
0xFF86F57924DA248D6E44E8154EB69F0AE2AEBAEE9931D0B5A969F904, | |
), | |
( | |
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D, | |
0xF220266E1105BFE3083E03EC7A3A654651F45E37167E88600BF257C1, | |
'test'.encode('utf-8'), | |
hashlib.sha384, | |
0x7046742B839478C1B5BD31DB2E862AD868E1A45C863585B5F22BDC2D, | |
), | |
( | |
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D, | |
0xF220266E1105BFE3083E03EC7A3A654651F45E37167E88600BF257C1, | |
'test'.encode('utf-8'), | |
hashlib.sha512, | |
0xE39C2AA4EA6BE2306C72126D40ED77BF9739BB4D6EF2BBB1DCB6169D, | |
), | |
# A.2.5. ECDSA, 256 Bits (Prime Field) | |
( | |
0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551, | |
0xC9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721, | |
'sample'.encode('utf-8'), | |
hashlib.sha1, | |
0x882905F1227FD620FBF2ABF21244F0BA83D0DC3A9103DBBEE43A1FB858109DB4, | |
), | |
( | |
0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551, | |
0xC9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721, | |
'sample'.encode('utf-8'), | |
hashlib.sha224, | |
0x103F90EE9DC52E5E7FB5132B7033C63066D194321491862059967C715985D473, | |
), | |
( | |
0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551, | |
0xC9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721, | |
'sample'.encode('utf-8'), | |
hashlib.sha256, | |
0xA6E3C57DD01ABE90086538398355DD4C3B17AA873382B0F24D6129493D8AAD60, | |
), | |
( | |
0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551, | |
0xC9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721, | |
'sample'.encode('utf-8'), | |
hashlib.sha384, | |
0x09F634B188CEFD98E7EC88B1AA9852D734D0BC272F7D2A47DECC6EBEB375AAD4, | |
), | |
( | |
0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551, | |
0xC9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721, | |
'sample'.encode('utf-8'), | |
hashlib.sha512, | |
0x5FA81C63109BADB88C1F367B47DA606DA28CAD69AA22C4FE6AD7DF73A7173AA5, | |
), | |
( | |
0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551, | |
0xC9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721, | |
'test'.encode('utf-8'), | |
hashlib.sha1, | |
0x8C9520267C55D6B980DF741E56B4ADEE114D84FBFA2E62137954164028632A2E, | |
), | |
( | |
0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551, | |
0xC9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721, | |
'test'.encode('utf-8'), | |
hashlib.sha224, | |
0x669F4426F2688B8BE0DB3A6BD1989BDAEFFF84B649EEB84F3DD26080F667FAA7, | |
), | |
( | |
0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551, | |
0xC9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721, | |
'test'.encode('utf-8'), | |
hashlib.sha256, | |
0xD16B6AE827F17175E040871A1C7EC3500192C4C92677336EC2537ACAEE0008E0, | |
), | |
( | |
0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551, | |
0xC9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721, | |
'test'.encode('utf-8'), | |
hashlib.sha384, | |
0x16AEFFA357260B04B1DD199693960740066C1A8F3E8EDD79070AA914D361B3B8, | |
), | |
( | |
0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551, | |
0xC9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721, | |
'test'.encode('utf-8'), | |
hashlib.sha512, | |
0x6915D11632ACA3C40D5D51C08DAF9C555933819548784480E93499000D9F0B7F, | |
), | |
# A.2.6. ECDSA, 384 Bits (Prime Field) | |
( | |
0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551, | |
0xC9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721, | |
'sample'.encode('utf-8'), | |
hashlib.sha1, | |
0x882905F1227FD620FBF2ABF21244F0BA83D0DC3A9103DBBEE43A1FB858109DB4, | |
), | |
( | |
0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551, | |
0xC9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721, | |
'sample'.encode('utf-8'), | |
hashlib.sha224, | |
0x103F90EE9DC52E5E7FB5132B7033C63066D194321491862059967C715985D473, | |
), | |
( | |
0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551, | |
0xC9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721, | |
'sample'.encode('utf-8'), | |
hashlib.sha256, | |
0xA6E3C57DD01ABE90086538398355DD4C3B17AA873382B0F24D6129493D8AAD60, | |
), | |
( | |
0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551, | |
0xC9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721, | |
'sample'.encode('utf-8'), | |
hashlib.sha384, | |
0x09F634B188CEFD98E7EC88B1AA9852D734D0BC272F7D2A47DECC6EBEB375AAD4, | |
), | |
( | |
0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551, | |
0xC9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721, | |
'sample'.encode('utf-8'), | |
hashlib.sha512, | |
0x5FA81C63109BADB88C1F367B47DA606DA28CAD69AA22C4FE6AD7DF73A7173AA5, | |
), | |
( | |
0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551, | |
0xC9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721, | |
'test'.encode('utf-8'), | |
hashlib.sha1, | |
0x8C9520267C55D6B980DF741E56B4ADEE114D84FBFA2E62137954164028632A2E, | |
), | |
( | |
0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551, | |
0xC9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721, | |
'test'.encode('utf-8'), | |
hashlib.sha224, | |
0x669F4426F2688B8BE0DB3A6BD1989BDAEFFF84B649EEB84F3DD26080F667FAA7, | |
), | |
( | |
0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551, | |
0xC9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721, | |
'test'.encode('utf-8'), | |
hashlib.sha256, | |
0xD16B6AE827F17175E040871A1C7EC3500192C4C92677336EC2537ACAEE0008E0, | |
), | |
( | |
0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551, | |
0xC9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721, | |
'test'.encode('utf-8'), | |
hashlib.sha384, | |
0x16AEFFA357260B04B1DD199693960740066C1A8F3E8EDD79070AA914D361B3B8, | |
), | |
( | |
0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551, | |
0xC9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721, | |
'test'.encode('utf-8'), | |
hashlib.sha512, | |
0x6915D11632ACA3C40D5D51C08DAF9C555933819548784480E93499000D9F0B7F, | |
), | |
# A.2.6. ECDSA, 384 Bits (Prime Field) | |
( | |
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973, | |
0x6B9D3DAD2E1B8C1C05B19875B6659F4DE23C3B667BF297BA9AA47740787137D896D5724E4C70A825F872C9EA60D2EDF5, | |
'sample'.encode('utf-8'), | |
hashlib.sha1, | |
0x4471EF7518BB2C7C20F62EAE1C387AD0C5E8E470995DB4ACF694466E6AB096630F29E5938D25106C3C340045A2DB01A7, | |
), | |
( | |
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973, | |
0x6B9D3DAD2E1B8C1C05B19875B6659F4DE23C3B667BF297BA9AA47740787137D896D5724E4C70A825F872C9EA60D2EDF5, | |
'sample'.encode('utf-8'), | |
hashlib.sha224, | |
0xA4E4D2F0E729EB786B31FC20AD5D849E304450E0AE8E3E341134A5C1AFA03CAB8083EE4E3C45B06A5899EA56C51B5879, | |
), | |
( | |
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973, | |
0x6B9D3DAD2E1B8C1C05B19875B6659F4DE23C3B667BF297BA9AA47740787137D896D5724E4C70A825F872C9EA60D2EDF5, | |
'sample'.encode('utf-8'), | |
hashlib.sha256, | |
0x180AE9F9AEC5438A44BC159A1FCB277C7BE54FA20E7CF404B490650A8ACC414E375572342863C899F9F2EDF9747A9B60, | |
), | |
( | |
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973, | |
0x6B9D3DAD2E1B8C1C05B19875B6659F4DE23C3B667BF297BA9AA47740787137D896D5724E4C70A825F872C9EA60D2EDF5, | |
'sample'.encode('utf-8'), | |
hashlib.sha384, | |
0x94ED910D1A099DAD3254E9242AE85ABDE4BA15168EAF0CA87A555FD56D10FBCA2907E3E83BA95368623B8C4686915CF9, | |
), | |
( | |
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973, | |
0x6B9D3DAD2E1B8C1C05B19875B6659F4DE23C3B667BF297BA9AA47740787137D896D5724E4C70A825F872C9EA60D2EDF5, | |
'sample'.encode('utf-8'), | |
hashlib.sha512, | |
0x92FC3C7183A883E24216D1141F1A8976C5B0DD797DFA597E3D7B32198BD35331A4E966532593A52980D0E3AAA5E10EC3, | |
), | |
( | |
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973, | |
0x6B9D3DAD2E1B8C1C05B19875B6659F4DE23C3B667BF297BA9AA47740787137D896D5724E4C70A825F872C9EA60D2EDF5, | |
'test'.encode('utf-8'), | |
hashlib.sha1, | |
0x66CC2C8F4D303FC962E5FF6A27BD79F84EC812DDAE58CF5243B64A4AD8094D47EC3727F3A3C186C15054492E30698497, | |
), | |
( | |
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973, | |
0x6B9D3DAD2E1B8C1C05B19875B6659F4DE23C3B667BF297BA9AA47740787137D896D5724E4C70A825F872C9EA60D2EDF5, | |
'test'.encode('utf-8'), | |
hashlib.sha224, | |
0x18FA39DB95AA5F561F30FA3591DC59C0FA3653A80DAFFA0B48D1A4C6DFCBFF6E3D33BE4DC5EB8886A8ECD093F2935726, | |
), | |
( | |
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973, | |
0x6B9D3DAD2E1B8C1C05B19875B6659F4DE23C3B667BF297BA9AA47740787137D896D5724E4C70A825F872C9EA60D2EDF5, | |
'test'.encode('utf-8'), | |
hashlib.sha256, | |
0x0CFAC37587532347DC3389FDC98286BBA8C73807285B184C83E62E26C401C0FAA48DD070BA79921A3457ABFF2D630AD7, | |
), | |
( | |
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973, | |
0x6B9D3DAD2E1B8C1C05B19875B6659F4DE23C3B667BF297BA9AA47740787137D896D5724E4C70A825F872C9EA60D2EDF5, | |
'test'.encode('utf-8'), | |
hashlib.sha384, | |
0x015EE46A5BF88773ED9123A5AB0807962D193719503C527B031B4C2D225092ADA71F4A459BC0DA98ADB95837DB8312EA, | |
), | |
( | |
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973, | |
0x6B9D3DAD2E1B8C1C05B19875B6659F4DE23C3B667BF297BA9AA47740787137D896D5724E4C70A825F872C9EA60D2EDF5, | |
'test'.encode('utf-8'), | |
hashlib.sha512, | |
0x3780C4F67CB15518B6ACAE34C9F83568D2E12E47DEAB6C50A4E4EE5319D1E8CE0E2CC8A136036DC4B9C00E6888F66B6C, | |
), | |
# A.2.7. ECDSA, 521 Bits (Prime Field) | |
( | |
0x1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5C9B8899C47AEBB6FB71E91386409, | |
0x0FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75CAA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83538, | |
'sample'.encode('utf-8'), | |
hashlib.sha1, | |
0x089C071B419E1C2820962321787258469511958E80582E95D8378E0C2CCDB3CB42BEDE42F50E3FA3C71F5A76724281D31D9C89F0F91FC1BE4918DB1C03A5838D0F9, | |
), | |
( | |
0x1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5C9B8899C47AEBB6FB71E91386409, | |
0x0FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75CAA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83538, | |
'sample'.encode('utf-8'), | |
hashlib.sha224, | |
0x121415EC2CD7726330A61F7F3FA5DE14BE9436019C4DB8CB4041F3B54CF31BE0493EE3F427FB906393D895A19C9523F3A1D54BB8702BD4AA9C99DAB2597B92113F3, | |
), | |
( | |
0x1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5C9B8899C47AEBB6FB71E91386409, | |
0x0FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75CAA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83538, | |
'sample'.encode('utf-8'), | |
hashlib.sha256, | |
0x0EDF38AFCAAECAB4383358B34D67C9F2216C8382AAEA44A3DAD5FDC9C32575761793FEF24EB0FC276DFC4F6E3EC476752F043CF01415387470BCBD8678ED2C7E1A0, | |
), | |
( | |
0x1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5C9B8899C47AEBB6FB71E91386409, | |
0x0FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75CAA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83538, | |
'sample'.encode('utf-8'), | |
hashlib.sha384, | |
0x1546A108BC23A15D6F21872F7DED661FA8431DDBD922D0DCDB77CC878C8553FFAD064C95A920A750AC9137E527390D2D92F153E66196966EA554D9ADFCB109C4211, | |
), | |
( | |
0x1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5C9B8899C47AEBB6FB71E91386409, | |
0x0FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75CAA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83538, | |
'sample'.encode('utf-8'), | |
hashlib.sha512, | |
0x1DAE2EA071F8110DC26882D4D5EAE0621A3256FC8847FB9022E2B7D28E6F10198B1574FDD03A9053C08A1854A168AA5A57470EC97DD5CE090124EF52A2F7ECBFFD3, | |
), | |
( | |
0x1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5C9B8899C47AEBB6FB71E91386409, | |
0x0FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75CAA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83538, | |
'test'.encode('utf-8'), | |
hashlib.sha1, | |
0x0BB9F2BF4FE1038CCF4DABD7139A56F6FD8BB1386561BD3C6A4FC818B20DF5DDBA80795A947107A1AB9D12DAA615B1ADE4F7A9DC05E8E6311150F47F5C57CE8B222, | |
), | |
( | |
0x1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5C9B8899C47AEBB6FB71E91386409, | |
0x0FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75CAA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83538, | |
'test'.encode('utf-8'), | |
hashlib.sha224, | |
0x040D09FCF3C8A5F62CF4FB223CBBB2B9937F6B0577C27020A99602C25A01136987E452988781484EDBBCF1C47E554E7FC901BC3085E5206D9F619CFF07E73D6F706, | |
), | |
( | |
0x1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5C9B8899C47AEBB6FB71E91386409, | |
0x0FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75CAA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83538, | |
'test'.encode('utf-8'), | |
hashlib.sha256, | |
0x01DE74955EFAABC4C4F17F8E84D881D1310B5392D7700275F82F145C61E843841AF09035BF7A6210F5A431A6A9E81C9323354A9E69135D44EBD2FCAA7731B909258, | |
), | |
( | |
0x1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5C9B8899C47AEBB6FB71E91386409, | |
0x0FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75CAA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83538, | |
'test'.encode('utf-8'), | |
hashlib.sha384, | |
0x1F1FC4A349A7DA9A9E116BFDD055DC08E78252FF8E23AC276AC88B1770AE0B5DCEB1ED14A4916B769A523CE1E90BA22846AF11DF8B300C38818F713DADD85DE0C88, | |
), | |
( | |
0x1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5C9B8899C47AEBB6FB71E91386409, | |
0x0FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75CAA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83538, | |
'test'.encode('utf-8'), | |
hashlib.sha512, | |
0x16200813020EC986863BEDFC1B121F605C1215645018AEA1A7B215A564DE9EB1B38A67AA1128B80CE391C4FB71187654AAA3431027BFC7F395766CA988C964DC56D, | |
), | |
# | |
# ... ... | |
# | |
] | |
for case in test_vectors: | |
assert Rfc6979(q=case[0]).generate_k(x=case[1], m=case[2], h=case[3]) == case[4] | |
print('passed') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://datatracker.ietf.org/doc/html/rfc6979