research/mimc_stark/utils.py

26 lines
951 B
Python
Raw Normal View History

2018-07-10 12:49:25 +00:00
from merkle_tree import blake
# Get the set of powers of R, until but not including when the powers
# loop back to 1
def get_power_cycle(r, modulus):
o = [1, r]
while o[-1] != 1:
o.append((o[-1] * r) % modulus)
return o[:-1]
# Extract pseudorandom indices from entropy
2018-07-11 15:46:21 +00:00
def get_pseudorandom_indices(seed, modulus, count, exclude_multiples_of=0):
2018-07-10 12:49:25 +00:00
assert modulus < 2**24
data = seed
while len(data) < 4 * count:
data += blake(data[-32:])
2018-07-11 15:46:21 +00:00
if exclude_multiples_of == 0:
return [int.from_bytes(data[i: i+4], 'big') % modulus for i in range(0, count * 4, 4)]
else:
real_modulus = modulus * (exclude_multiples_of - 1) // exclude_multiples_of
o = [int.from_bytes(data[i: i+4], 'big') % real_modulus for i in range(0, count * 4, 4)]
return [x+1+x//(exclude_multiples_of-1) for x in o]
2018-07-11 19:01:28 +00:00
def is_a_power_of_2(x):
return True if x==1 else False if x%2 else is_a_power_of_2(x//2)