Esempio n. 1
0
def unit_ops():
    result = 0
    messages = []
    seed = 129873918231
    for o in (0, 12, 24, 36, 48):
        print("\nOffset:", o)
        for i in range(1 << 12):
            if i % (1 << 8) == (1 << 8) - 1:
                print(i / (1 << 12), end='\r')
            x = (i << o)
            if (x != anarchy.flop(anarchy.flop(x))):
                messages.append("flop @ {}".format(x))
                result += 1
            if (x != anarchy.rev_scramble(anarchy.scramble(x))):
                messages.append("scramble @ {}".format(x))
                result += 1
            if (x != anarchy.prng(anarchy.rev_prng(x, seed), seed)):
                messages.append("prng @ {}".format(x))
                result += 1
            r = x
            p = x
            for j, y in enumerate(TEST_VALUES):
                if (x != anarchy.rev_swirl(anarchy.swirl(x, y), y)):
                    messages.append("swirl @ {}, {}".format(x, y))
                    result += 1

                if (x != anarchy.fold(anarchy.fold(x, y), y)):
                    messages.append("fold @ {}, {}".format(x, y))
                    result += 1

                if (r != anarchy.scramble(anarchy.rev_scramble(r))):
                    messages.append("chained scramble @ {}".format(r))
                    result += 1

                r = anarchy.rev_scramble(r)
                if (p != anarchy.rev_prng(anarchy.prng(p, seed), seed)):
                    messages.append("chained prng @ {}".format(p))
                    result += 1

                p = anarchy.prng(p, seed)

    if result != 0:
        print("unit_ops failures:")
        for m in messages:
            print(m)

    return result
Esempio n. 2
0
def demo_prng():
  """
  Creates images that demonstrate the PRNG functionality and compare it against
  the built-in irreversible PRNG.
  """
  seed = 2318230981
  random.seed(seed)
  prev_a = 472938
  prev_b = builtin_next()
  xy = {}
  a_seq = PIL.Image.new("RGB", (100, 100))
  b_seq = PIL.Image.new("RGB", (100, 100))
  a_map = {}
  a_map_max = 0
  b_map = {}
  b_map_max = 0
  for x in range(100):
    for y in range(100):
      next_a = anarchy.prng(prev_a, seed)
      c_x = round(prev_a / MAX_RANDOM * 99)
      c_y = round(next_a / MAX_RANDOM * 99)
      if c_x not in a_map:
        a_map[c_x] = {}
      if c_y not in a_map[c_x]:
        a_map[c_x][c_y] = 0
      else:
        a_map[c_x][c_y] += 1
        if a_map[c_x][c_y] > a_map_max:
          a_map_max = a_map[c_x][c_y]
      prev_a = next_a
      brightness = round(next_a / MAX_RANDOM * 255)
      a_seq.putpixel((x, y), (brightness,)*3)

      next_b = builtin_next()
      c_x = round(prev_b / MAX_RANDOM * 99)
      c_y = round(next_b / MAX_RANDOM * 99)
      if (c_x < 0 or c_x >= 100 or c_y < 0 or c_y >= 100):
        print(prev_b, next_b, c_x, c_y)
      if c_x not in b_map:
        b_map[c_x] = {}
      if c_y not in b_map[c_x]:
        b_map[c_x][c_y] = 0
      else:
        b_map[c_x][c_y] += 1
        if b_map[c_x][c_y] > b_map_max:
          b_map_max = b_map[c_x][c_y]
      prev_b = next_b
      brightness = round(next_b / MAX_RANDOM * 255)
      b_seq.putpixel((x, y), (brightness,)*3)

  a_coords = PIL.Image.new("RGB", (100, 100))
  b_coords = PIL.Image.new("RGB", (100, 100))
  for kx in a_map:
    for ky in a_map[kx]:
      val = round(a_map[kx][ky] / a_map_max * 255)
      a_coords.putpixel((kx, ky), (val,)*3)

  for kx in b_map:
    for ky in b_map[kx]:
      val = round(b_map[kx][ky] / b_map_max * 255)
      b_coords.putpixel((kx, ky), (val,)*3)

  a_seq.save(os.path.join("demos", "rng_seq_anarchy.png"), format="png")
  b_seq.save(os.path.join("demos", "rng_seq_builtin.png"), format="png")
  a_coords.save(os.path.join("demos", "rng_coords_anarchy.png"), format="png")
  b_coords.save(os.path.join("demos", "rng_coords_builtin.png"), format="png")
Esempio n. 3
0
 "scramble": [
     [
         anarchy.scramble(anarchy.rev_swirl(0x03040610 | 0x40004001, 1)),
         0x40004001
     ],
     [
         anarchy.rev_scramble(0x40004001),
         anarchy.rev_swirl(0x03040610 | 0x40004001, 1)
     ],
     [anarchy.rev_scramble(anarchy.scramble(17)), 17],
     [anarchy.rev_scramble(anarchy.scramble(8493489)), 8493489],
     [anarchy.scramble(anarchy.rev_scramble(8493489)), 8493489],
 ],
 "prng": [
     # TODO: Verify these!
     [anarchy.prng(489348, 373891), 18107188676709054266],
     [anarchy.rev_prng(1766311112, 373891), 14566213110237565854],
     [anarchy.prng(0, 0), 15132939213242511212],
     [anarchy.rev_prng(15132939213242511212, 0), 0],
     [anarchy.prng(anarchy.rev_prng(1782, 39823), 39823), 1782],
     [anarchy.rev_prng(anarchy.prng(1782, 39823), 39823), 1782],
 ],
 "lfsr": [
     # TODO: Verify these!
     [anarchy.lfsr(489348), 244674],
     [anarchy.lfsr(1766932808), 883466404],
 ],
 "udist": [
     # TODO: Verify and/or fix these!
     [anarchy.udist(0), 0.39901845521416135],
     [anarchy.udist(8329801), 0.9082340390686318],
Esempio n. 4
0
"""
Empirically tests the period of the prng function with several seeds.
"""

import sys

import anarchy

seed = 9823098
starting_value = 0

seen = set()
rng = starting_value
period = 0
while rng not in seen:
    seen.add(rng)
    rng = anarchy.prng(rng, seed)
    period += 1
    if period % 1000000 == 0:
        print('.', end="")
        sys.stdout.flush()
print("Seed {} starting from {}: period is {}".format(seed, starting_value,
                                                      period))