def serialize_signature(self): result = bytearray() # Append challenges as bytes challenges = BitVector(size=0) for i in self.__signature.challenges: if (i == 0): challenges = challenges + BitVector(bitlist=[0, 0]) if (i == 1): challenges = challenges + BitVector(bitlist=[1, 0]) if (i == 2): challenges = challenges + BitVector(bitlist=[0, 1]) diff = 8 - (challenges.length() % 8) challenges = challenges + BitVector(intVal=0, size=diff) result.extend(bytes.fromhex(challenges.get_bitvector_in_hex())) # Append salt as bytes result.extend( bytes.fromhex(self.__signature.salt.get_bitvector_in_hex())) # Append all proofs for t in range(self.mpc_rounds): challenge_value = self.__signature.challenges[t] result.extend(self.__signature.proofs[t].view_3_commit) result.extend( bytes.fromhex(self.__signature.proofs[t].transcript. get_bitvector_in_hex())) result.extend( bytes.fromhex( self.__signature.proofs[t].seed_1.get_bitvector_in_hex())) result.extend( bytes.fromhex( self.__signature.proofs[t].seed_2.get_bitvector_in_hex())) if (challenge_value == 1 or challenge_value == 2): result.extend( bytes.fromhex(self.__signature.proofs[t].i_share. get_bitvector_in_hex())) self.__signature_ser = result
def sha512(msg): def sigma_0(x): return (x[:] >> 1) ^ (x[:] >> 8) ^ (x[:].shift_right(7)) def sigma_1(x): return (x[:] >> 19) ^ (x[:] >> 61) ^ (x[:].shift_right(6)) def gamma_0(x): return (x[:] >> 28) ^ (x[:] >> 34) ^ (x[:] >> 39) def gamma_1(x): return (x[:] >> 14) ^ (x[:] >> 18) ^ (x[:] >> 41) def Ch(e, f, g): return (e & f) ^ (~e & g) def Maj(a, b, c): return (a & b) ^ (a & c) ^ (b & c) def T1(h, ch, g1, w, k): return (int(h) + int(ch) + int(g1) + int(w) + int(k)) % 2**64 def T2(a, maj): return (int(a) + int(maj)) % 2**64 iv_hexstrings = ('6a09e667f3bcc908', 'bb67ae8584caa73b', '3c6ef372fe94f82b', 'a54ff53a5f1d36f1', '510e527fade682d1', '9b05688c2b3e6c1f', '1f83d9abfb41bd6b', '5be0cd19137e2179') hb = [BitVector(hexstring=h) for h in iv_hexstrings] # hashbuffer msg_block_bv = BitVector(textstring=msg) msg_length = msg_block_bv.length() msg_length_bv = BitVector(intVal=msg_length, size=128) zero_num = (896 - (msg_length + 1)) % 1024 pad_string = '1' + zero_num * '0' msg_padding_bv = BitVector(bitstring=pad_string) msg_block = msg_block_bv + msg_padding_bv + msg_length_bv words = [None] * 80 for n in xrange(0, msg_block.length(), 1024): # Word schedule block = msg_block[n:n + 1024] words[0:16] = [block[i:i + 64] for i in xrange(0, 1024, 64)] for i in xrange(16, 80): val = (int(words[i - 16]) + int(sigma_0(words[i - 15])) + int(words[i - 7]) + int(sigma_1(words[i - 2]))) % (2**64) words[i] = BitVector(intVal=val, size=64) # Compression function a, b, c, d, e, f, g, h = hb[0], hb[1], hb[2], hb[3], hb[4], hb[5], hb[ 6], hb[7] for i in xrange(80): t1 = T1(h, Ch(e, f, g), gamma_1(e), words[i], k512[i]) t2 = T2(gamma_0(a), Maj(a, b, c)) h = g g = f f = e e = BitVector(intVal=(int(d) + t1) % (2**64), size=64) d = c c = b b = a a = BitVector(intVal=(t1 + t2) % (2**64), size=64) # Intermediate hash values hb[0] = BitVector(intVal=(int(hb[0]) + int(a)) % (2**64), size=64) hb[1] = BitVector(intVal=(int(hb[1]) + int(b)) % (2**64), size=64) hb[2] = BitVector(intVal=(int(hb[2]) + int(c)) % (2**64), size=64) hb[3] = BitVector(intVal=(int(hb[3]) + int(d)) % (2**64), size=64) hb[4] = BitVector(intVal=(int(hb[4]) + int(e)) % (2**64), size=64) hb[5] = BitVector(intVal=(int(hb[5]) + int(f)) % (2**64), size=64) hb[6] = BitVector(intVal=(int(hb[6]) + int(g)) % (2**64), size=64) hb[7] = BitVector(intVal=(int(hb[7]) + int(h)) % (2**64), size=64) # Final Hash values msg_hash = hb[0] + hb[1] + hb[2] + hb[3] + hb[4] + hb[5] + hb[6] + hb[7] hex_str = msg_hash.getHexStringFromBitVector() return hex_str
class BloomFilter(object): # Return the estimated number of bits needed in a Bloom Filter that # will store numKeys keys, using numHashes hash functions, and that # will have a false positive rate of maxFalsePositive. # See Slide 12 for the math needed to do this. def __bitsNeeded(self, numKeys, numHashes, maxFalsePositive): p = maxFalsePositive n = numKeys d = numHashes # Using equation B to find fe phee = (1 - ((p)**(1 / d))) # Plugging in fe to equation D to find N (aka size of bits needed) N = (d) / (1 - ((phee)**(1 / n))) return int(N) # Casted to int because you can't have a decimal size # Create a Bloom Filter that will store numKeys keys, using # numHashes hash functions, and that will have a false positive # rate of maxFalsePositive. # All attributes must be private. # will need to use __bitsNeeded to figure out how big # of a BitVector will be needed def __init__(self, numKeys, numHashes, maxFalsePositive): self.__n = numKeys self.__d = numHashes self.__p = maxFalsePositive self.__N = self.__bitsNeeded(numKeys, numHashes, maxFalsePositive) self.__bitVector = BitVector(size=self.__N) # accessors def getLen(self): return self.__bitVector.length() def getNumHash(self): return self.__d # insert the specified key into the Bloom Filter. # Doesn't return anything, since an insert into # a Bloom Filter always succeeds! def insert(self, key): # set to default h for the first time through prevBucket = 0 # hash appropriate number of times (numHashes) and set the bits # counting how many get set to 1 for i in range(0, self.__d): bucket = BitHash.BitHash(key, prevBucket) % self.__N if self.__bitVector[bucket] != 1: self.__bitVector[bucket] = 1 # next time we bit hash, we've updated our h # with the previous bucket prevBucket = bucket # Returns True if key MAY have been inserted into the Bloom filter. # Returns False if key definitely hasn't been inserted into the BF. def find(self, key): # set to default h for the first time because of bitHash function # specifications prevBucket = 0 for i in range(0, self.__d): # hashes that number of times, and returns T/F appropriately # if 1, then result = T bucket = BitHash.BitHash(key, prevBucket) % self.__N # check first if self.__bitVector[bucket] != 1: return False # next time we BitHash, we've updated our h # with the previous bucket prevBucket = bucket # otherwise, if all hash indices were 1s, we return true return True # Returns the PROJECTED current false positive rate based on the # ACTUAL current number of bits set in this Bloom Filter. # This is NOT the same thing as trying to use the Bloom Filter and # actually measuring the proportion of false positives that # are actually encountered. def falsePositiveRate(self): # Using equation B fe = (1 - (self.__d / self.__N))**self.__n # Using equation A proportion = (1 - fe)**self.__d return proportion
message = f.readlines()[0] h0 = BitVector(hexstring='6a09e667f3bcc908') h1 = BitVector(hexstring='bb67ae8584caa73b') h2 = BitVector(hexstring='3c6ef372fe94f82b') h3 = BitVector(hexstring='a54ff53a5f1d36f1') h4 = BitVector(hexstring='510e527fade682d1') h5 = BitVector(hexstring='9b05688c2b3e6c1f') h6 = BitVector(hexstring='1f83d9abfb41bd6b') h7 = BitVector(hexstring='5be0cd19137e2179') mod64 = int(BitVector(hexstring='FFFFFFFFFFFFFFFF')) # Setup modeled after SHA-1 in lecture notes bv = BitVector(textstring=message) length = bv.length() bv1 = bv + BitVector(bitstring="1") length1 = bv1.length() howmanyzeros = (896 - length1) % 1024 zerolist = [0] * howmanyzeros bv2 = bv1 + BitVector(bitlist=zerolist) bv3 = BitVector(intVal=length, size=128) bv4 = bv2 + bv3 words = [None] * 80 for n in range(0, bv4.length(), 1024): block = bv4[n:n + 1024] words[0:16] = [block[i:i + 64] for i in range(0, 1024, 64)] for i in range(16, 80): sigma0 = (words[i-15].deep_copy() >> 1) ^ (words[i-15].deep_copy() >> 8) ^ \