Xinyoumeng233hu's picture
Upload 8 files
229a3ba
raw
history blame
1.34 kB
#@title Colab setup { run: "auto", display-mode: "form" }
#@markdown This downloads some prereqs. It might take a while! You only have to run this cell once.
# !pip install torch==1.13.1 pytorch-transformers==1.1.0 bitarray==1.0.1
import hashlib
import hmac
import numpy as np
class DRBG(object):
def __init__(self, key, seed):
self.key = key
self.val = b'\x01' * 64
self.reseed(seed)
self.byte_index = 0
self.bit_index = 0
def hmac(self, key, val):
return hmac.new(key, val, hashlib.sha512).digest()
def reseed(self, data=b''):
self.key = self.hmac(self.key, self.val + b'\x00' + data)
self.val = self.hmac(self.key, self.val)
if data:
self.key = self.hmac(self.key, self.val + b'\x01' + data)
self.val = self.hmac(self.key, self.val)
def generate_bits(self, n):
xs = np.zeros(n, dtype=bool)
for i in range(0,n):
xs[i] = (self.val[self.byte_index] >> (7 - self.bit_index)) & 1
self.bit_index += 1
if self.bit_index >= 8:
self.bit_index = 0
self.byte_index += 1
if self.byte_index >= 8:
self.byte_index = 0
self.val = self.hmac(self.key, self.val)
self.reseed()
return xs