def generate_source(self, seed, sourceLength): source = [] rng = LCG(seed) for i in range(sourceLength): unit_nr = rng.next() % len(self.units) unit = copy.deepcopy(self.units[unit_nr]) source.append(unit) return source
def create_image_pw(path, user_id): l = LCG(user_id) buf = b"" for _ in range(128): buf += l.next().to_bytes(16, "big") im = Image.frombytes("1", (128, 128), buf) im.save(joinp(path, "{}.bmp".format(user_id))) return l.next()
def test_assignment_example(self): modulus = 2147483642 multiplier = 450 lcg = LCG(LCGParams(modulus, multiplier)) generator = lcg.generator() for i in range(0, 100): entry = next(generator) self.assertTrue(0 <= entry < modulus)
def break_lcg(user_id): mode = "Lcg" states = [make_bet(mode, user_id, 1, 0)["realNumber"] for _ in range(3)] mod = 2**32 mul = find_mul(states, mod) inc = find_inc(states, mod, mul) lcg = LCG(states[-1], mod, mul, inc) make_bet(mode, user_id, 1, lcg.generate()) make_bet(mode, user_id, 1_000, lcg.generate()) print(make_bet(mode, user_id, 1_000_000, lcg.generate()))
def simulate(self): processed = {} for k in self._data: processed[k] = 0 rg = LCG(int(time.time()), 1664525, 1013904223, 2**32) for i in range(self._trials): rand = rg.binRand() for k in self._data: if rand <= self._data[k]: processed[k] += 1 break return processed
def do_login(): user_id = int(request.form.get("user_id")) pw = int(request.form.get("pw")) lcg = LCG(user_id) for _ in range(128): lcg.next() if lcg.next() == pw: msg = "Login Successful<br/>" session['user_id'] = user_id else: msg = "Wrong credentials!" return render_template(TEMPL_PATH, title="Login!", content=msg)
def test_as_best_as_i_can(self): m1 = 2147483642 a1 = 450 m2 = 2147483423 a2 = 234 lcg1 = LCG(LCGParams(m1, a1)) lcg2 = LCG(LCGParams(m2, a2)) generator = CombinedLCG(lcg1, lcg2).generator() for i in range(0, 100): mynext = next(generator) self.assertTrue(0 < mynext <= m1)
def lcg_test(): seed = int(round(time.time() * 1000)) # these parameters are used in ansi C implementation of LGC true_a = 1103515245 true_c = 12345 true_m = int(math.pow(2, 31)) lcg = LCG(seed, true_a, true_c, true_m) print(recognize_lgc(lcg))
def pyrite_noise(prng_state, noise_state): """ simulates 1 frame of background noise in Pyrite Town """ for i in range(3): prng_state, prn = LCG(prng_state) prn = double_to_single(prn) noise_state[i] = (0.5 * prn) + noise_state[i] if noise_state[i] > 1.0: for j in range(8): prng_state, _ = LCG(prng_state) noise_state[i] -= 1.0 noise_state[i] = double_to_single(noise_state[i]) return prng_state, noise_state
def test_slides_example(self): lcg = LCG(LCGParams(10, 7, 7)) generator = lcg.generator(7) iterations = 6 sequence = [] i = 0 while i < iterations: sequence.append(next(generator)) i += 1 self.assertEqual(sequence[0], 7) self.assertEqual(sequence[1], 6) self.assertEqual(sequence[2], 9) self.assertEqual(sequence[3], 0) self.assertEqual(sequence[4], 7) self.assertEqual(sequence[5], 6)
import matplotlib.pyplot as plt from scipy.stats import chisquare from lcg import LCG from bbs import BBS from ansix917 import ANSIX917 if __name__ == "__main__": rnd_gen = LCG(m=31, a=13, c=0, x0=12) # rnd_gen = BBS(p=383, q=503, s=101355) # k1 = '0110110101101111011011100110000101110010011000110110100001111001' # k2 = '0100011010000111111011011010011001100101101001001100011011110100' # rnd_gen = ANSIX917(v0=1, k1=k1, k2=k2) num_category = 10 f_obs = dict() for i in range(num_category): f_obs[i] = 0 for i in range(1, 1001): x_n = rnd_gen.next() f_obs[x_n % num_category] += 1 f_obs = list(f_obs.values()) print(chisquare(f_obs))
from sha import sha from lcg import LCG import socket message = "Cant be tampered" hashval = sha(message, LCG(123)) s = socket.socket() s.bind(('', 12347)) s.listen(5) while True: c, add = s.accept() c.send(message + '?' + hashval) c.close()
def test_get_modulus(self): lcg = LCG(LCGParams(10, 7, 7)) self.assertEqual(lcg.get_modulus(), 10)
import time from lcg import LCG from Fermat import Fermat from MillerRabin import MillerRabin from xorshift import xorshift if __name__ == '__main__': """ Funcao para gerar numeros primos """ n_bits = [40, 56, 80, 128, 168, 224, 256, 512, 1024, 2048, 4096] n_bits_len = len(n_bits) lcg = LCG(16807) # seed LCG xshif = xorshift(6504) # seed xorshift miller = MillerRabin(5) # 5 iteracoes fermat = Fermat(5) # 5 iteracoes # testanto LCG - MillerRabin print("Gerando possiveis primos LCG - MillerRabin\n") lcg_miller_start_time = time.time() for i in range(0, n_bits_len): while True: n_primo = lcg.generate(1103515245, 12345, n_bits[i]) if miller.test(n_primo): print("Possível número primo ", n_primo, " de tamanho ", n_bits[i], " bits gerado em ", (time.time() - lcg_miller_start_time), "segundos.") break
from sha import sha from lcg import LCG import socket c = socket.socket() c.connect(('localhost', 12347)) message, hashval = c.recv(1000).split('?') if sha(message, LCG(123)) == hashval: print "Integrity maintained" else: print "Message Tampered"
""" prng_state = 0x9187a4a2 noise_state = [int_to_float(0x3f65557e), int_to_float(0x3f5ac27e), int_to_float(0x3ea0a9fc)] while True: prng_state, noise_state = pyrite_noise(prng_state, noise_state) print("predicted prng_state: ", int_to_hex_str(prng_state)) print("predicted noise_state: ", noise_state) input("press ENTER to continue") """ seed = 0xed60636d lcg = LCG(seed) npc = NPC(lcg, float32(4.0), float32(24.0)) for i in range(1, 60 * 60 * 2): npc.step() # create_frame(i, npc.currentX, npc.currentY) if i >= 368: print("step: ", i) print("prng: ", int_to_hex_str(lcg.state)) print("destX: ", single_to_hex_str(npc.destX)) print("destY: ", single_to_hex_str(npc.destY)) print("speed: ", single_to_hex_str(npc.walk_speed)) print("currentX: ", single_to_hex_str(npc.currentX)) print("currentY: ", single_to_hex_str(npc.currentY)) try:
def prng_difference(prng_state, target): difference = 0 while prng_state != target: difference += 1 prng_state, _ = LCG(prng_state) return difference
# # This file is part of a college project for the INE5429 Computer Security # course lectured in Federal University of Santa Catarina. import time from lcg import LCG from blumblumshub import BBS from millerrabin import Millerrabin from fermat import Fermat ## # Funcao main que ira gerar os numeros primos if __name__ == '__main__': tamanhos = [40, 56, 80, 128, 168, 224, 256, 512, 1024, 2048, 4096] #Tamanhos dos numeros que serao gerados pelos geradores lcg = LCG(74573) #semente do LCG miller = Millerrabin(5) #5 iteracoes fermat = Fermat(5) #5 iteracoes outFile = open("numeroPrimos.txt", "wb") #Testando LCG com Miller print "Gerando possiveis numeros primos com LCG e Miller.\n" start_lcg_miller_time = time.time() outFile.write("Possiveis numeros primos gerados por LCG e Miller: \n") for m in tamanhos: while True: numeroPrimo = lcg.gerador(m, 1103515245, 12345) if miller.teste(numeroPrimo): outFile.write(str(numeroPrimo) + "\n") print "Um possivel numero primo de tamanho ", m, " bits foi gerado em ", (
def prng_range(prng_state, n): prg_states = [] for i in range(n): vals.append(prng_state) prng_state, _ = LCG(prng_state) return prng_states
from collections import Counter from lcg import LCG from xorshift import Xorshift from park_miller import Park_miller if __name__ == "__main__": seed = datetime.now().microsecond rounds = 10000000 bits = [40, 56, 80, 128, 168, 224, 256, 512, 1024, 2048, 4096] if argv[1].lower() == "lcg": c = 11 a = 25214903917 for n in bits: m = 2**n start = time() result = LCG(a, c, m) result.generate(seed, rounds) end = time() print((end - start) / rounds) exit(0) if argv[1].lower() == "park_miller" or argv[1].lower() == "pm": g = 48271 for n in bits: m = 2**n - 1 start = time() result = Park_miller(g, m) result.generate(seed, rounds) end = time() print((end - start) / rounds) exit(0)