<commit_before># -*- coding: utf-8 -*-
"""
Created on Thu Nov  9 11:00:55 2017

@author: Leonardo
"""

# deterministic generation of k following rfc6979
# https://tools.ietf.org/html/rfc6979#section-3.2
# code adapted from:
# https://github.com/AntonKueltz/fastecdsa/blob/master/fastecdsa/util.py

from hashlib import sha256
from ECsecp256k1 import ec
from struct import pack
from binascii import hexlify
from hmac import new as hmac_new

default_hasher = sha256
default_hash_digest_size = 32

qlen = len(bin(ec.order)) - 2  # -2 for the leading '0b'
rlen = ((qlen + 7) // 8) * 8

def bits2int(b):
  i = int(hexlify(b), 16)
  blen = len(b) * 8
  if blen > qlen:
    i >>= (blen - qlen)
  return i

def int2octets(x):
  octets = b''
  while x > 0:
    octets = pack('=B', (0xff & x)) + octets
    x >>= 8
  padding = b'\x00' * (rlen // 8 - len(octets))
  return padding + octets

def bits2octets(b):
  z1 = bits2int(b)  # -2 for the leading '0b'
  z2 = z1 % ec.order
  return int2octets(z2)

def check_hash_digest(m, hash_digest_size=default_hash_digest_size):
  """check that m is a bytes message with correct length
  """
  assert type(m) == bytes and len(m) == hash_digest_size, "m must be bytes with correct bytes length"

def rfc6979(prv, m, hasher=default_hasher):
  assert type(prv) == int and 0 < prv and prv < ec.order, "invalid prv"
  check_hash_digest(m)
  return rfc6979_raw(prv, m, hasher)

def rfc6979_raw(prv, m, hasher=default_hasher):
  hash_size = len(m)
  prv_and_m = int2octets(prv) + bits2octets(m)
  v = b'\x01' * hash_size
  k = b'\x00' * hash_size
  k = hmac_new(k, v + b'\x00' + prv_and_m, hasher).digest()
  v = hmac_new(k, v, hasher).digest()
  k = hmac_new(k, v + b'\x01' + prv_and_m, hasher).digest()
  v = hmac_new(k, v, hasher).digest()
  while True:
    t = b''
    while len(t) * 8 < qlen:
      v = hmac_new(k, v, hasher).digest()
      t = t + v
    nonce = bits2int(t)
    if nonce >= 1 and nonce < ec.order:
      # here it should be checked that nonce do not yields a invalid signature
      # but then I should put the signature generation here
      return nonce
    k = hmac_new(k, v + b'\x00', hasher).digest()
    v = hmac_new(k, v, hasher).digest()

"""
def deterministic_k(prv, msg, hasher = sha256):
  assert type(prv) == int and 0 < prv and prv < ec.order, "invalid prv"
  if type(msg) == str: msg = msg.encode()
  assert type(msg) == bytes
  hashmsg = hasher(msg)
  return deterministic_k_raw(prv, hashmsg, hasher)

def deterministic_k_raw(prv, m, hasher=default_hasher):
  hash_size = m.digest_size
  m = m.digest()
  prv_and_m = int2octets(prv) + bits2octets(m)
  v = b'\x01' * hash_size
  k = b'\x00' * hash_size
  k = hmac_new(k, v + b'\x00' + prv_and_m, hasher).digest()
  v = hmac_new(k, v, hasher).digest()
  k = hmac_new(k, v + b'\x01' + prv_and_m, hasher).digest()
  v = hmac_new(k, v, hasher).digest()
  while True:
    t = b''
    while len(t) * 8 < qlen:
      v = hmac_new(k, v, hasher).digest()
      t = t + v
    nonce = bits2int(t)
    if nonce >= 1 and nonce < ec.order:
      # here it should be checked that nonce do not yields a invalid signature
      # but then I should put the signature generation here
      return nonce
    k = hmac_new(k, v + b'\x00', hasher).digest()
    v = hmac_new(k, v, hasher).digest()
"""<commit_msg>updated commented code (even if must be removed)<commit_after># -*- coding: utf-8 -*-
"""
Created on Thu Nov  9 11:00:55 2017

@author: Leonardo
"""

# deterministic generation of k following rfc6979
# https://tools.ietf.org/html/rfc6979#section-3.2
# code adapted from:
# https://github.com/AntonKueltz/fastecdsa/blob/master/fastecdsa/util.py

from hashlib import sha256
from ECsecp256k1 import ec
from struct import pack
from binascii import hexlify
from hmac import new as hmac_new

default_hasher = sha256
default_hash_digest_size = 32

qlen = len(bin(ec.order)) - 2  # -2 for the leading '0b'
rlen = ((qlen + 7) // 8) * 8

def bits2int(b):
  i = int(hexlify(b), 16)
  blen = len(b) * 8
  if blen > qlen:
    i >>= (blen - qlen)
  return i

def int2octets(x):
  octets = b''
  while x > 0:
    octets = pack('=B', (0xff & x)) + octets
    x >>= 8
  padding = b'\x00' * (rlen // 8 - len(octets))
  return padding + octets

def bits2octets(b):
  z1 = bits2int(b)  # -2 for the leading '0b'
  z2 = z1 % ec.order
  return int2octets(z2)

def check_hash_digest(m, hash_digest_size=default_hash_digest_size):
  """check that m is a bytes message with correct length
  """
  assert type(m) == bytes and len(m) == hash_digest_size, "m must be bytes with correct bytes length"

def rfc6979(prv, m, hasher=default_hasher):
  assert type(prv) == int and 0 < prv and prv < ec.order, "invalid prv"
  check_hash_digest(m)
  return rfc6979_raw(prv, m, hasher)

def rfc6979_raw(prv, m, hasher=default_hasher):
  hash_size = len(m)
  prv_and_m = int2octets(prv) + bits2octets(m)
  v = b'\x01' * hash_size
  k = b'\x00' * hash_size
  k = hmac_new(k, v + b'\x00' + prv_and_m, hasher).digest()
  v = hmac_new(k, v, hasher).digest()
  k = hmac_new(k, v + b'\x01' + prv_and_m, hasher).digest()
  v = hmac_new(k, v, hasher).digest()
  while True:
    t = b''
    while len(t) * 8 < qlen:
      v = hmac_new(k, v, hasher).digest()
      t = t + v
    nonce = bits2int(t)
    if nonce >= 1 and nonce < ec.order:
      # here it should be checked that nonce do not yields a invalid signature
      # but then I should put the signature generation here
      return nonce
    k = hmac_new(k, v + b'\x00', hasher).digest()
    v = hmac_new(k, v, hasher).digest()

"""
def rfc6979(prv, msg, hasher = sha256):
  assert type(prv) == int and 0 < prv and prv < ec.order, "invalid prv"
  if type(msg) == str: msg = msg.encode()
  assert type(msg) == bytes
  hashmsg = hasher(msg)
  return deterministic_k_raw(prv, hashmsg, hasher)

def rfc6979_raw(prv, m, hasher=default_hasher):
  hash_size = m.digest_size
  m = m.digest()
  prv_and_m = int2octets(prv) + bits2octets(m)
  v = b'\x01' * hash_size
  k = b'\x00' * hash_size
  k = hmac_new(k, v + b'\x00' + prv_and_m, hasher).digest()
  v = hmac_new(k, v, hasher).digest()
  k = hmac_new(k, v + b'\x01' + prv_and_m, hasher).digest()
  v = hmac_new(k, v, hasher).digest()
  while True:
    t = b''
    while len(t) * 8 < qlen:
      v = hmac_new(k, v, hasher).digest()
      t = t + v
    nonce = bits2int(t)
    if nonce >= 1 and nonce < ec.order:
      # here it should be checked that nonce do not yields a invalid signature
      # but then I should put the signature generation here
      return nonce
    k = hmac_new(k, v + b'\x00', hasher).digest()
    v = hmac_new(k, v, hasher).digest()
"""
{'fraction_non_alphanumeric': 0.13994257216261147, 'fraction_numerical': 0.03354994710593925, 'mean_word_length': 3.2696774193548386, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4967010', 'n_tokens_mistral': 2814, 'n_tokens_neox': 2670, 'n_words': 757}