Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a different PRNG proposal #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 76 additions & 8 deletions blockies/blockies.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from itertools import islice
import colorsys
import ctypes
from hashlib import sha256
from itertools import cycle

LOWER = chr(9604)
RESET = '\033[39;49m'
Expand Down Expand Up @@ -55,13 +57,44 @@ def rand(self):
return result


class SHA256PRNG(object):
"""Alternative PRNG that iterates over the SHA256 sum of the lower-cased
`seed`.

Rationale: This is much simpler to implement across languages/platforms and can be expected
to be resistant against visual collisions on single-byte mismatches.

For example with the XORshiftPRNG

0x00112266bcfc3ce2a72a7211fff9ef200e30178a
0x00112266bcfc3ce2a72a7211fff9ef200e30178b
0x00112266bcfc3ce2a72a7211fff9ef200e30178c
0x00112266bcfc3ce2a72a7211fff9ef200e30178d
0x00112266bcfc3ce2a72a7211fff9ef200e30178e
0x00112266bcfc3ce2a72a7211fff9ef200e30178f

will all yield the same color scheme, whereas SHA256PRNG will create very different sequences
(and hence color schemes) for the above.
"""

def __init__(self, seed: str):
if seed[:2] == '0x':
seed = seed[2:]
self.randseed = sha256(seed.lower().encode('utf-8')).digest()
self.sequence = cycle(i / 256. for i in self.randseed)

def rand(self):
return next(self.sequence)


def sanity_check():
# Sanity check (test that RNG has same result as js version)
first_rand = 0.2292061443440616
seed = '0xfadc801b8b7ff0030f36ba700359d30bb12786e4'
test = XORshiftPRNG(seed)
assert test.rand() == first_rand


sanity_check()


Expand Down Expand Up @@ -98,6 +131,28 @@ def hls(self):
return (self.hue, self.light, self.saturation)


def createPyramidData(size, prng):
width = size
height = size
mirrorWidth = math.floor(width / 2)
rowBits = [i + 1 for i in list(range(height // 2)) + list(reversed(range(height // 2)))]
data = []
for y in range(height):
row = []
for x in range(rowBits[y]):
row.append(math.floor(prng.rand() * 2.3))
for x in range(mirrorWidth - rowBits[y]):
row.append(-1)
row = list(reversed(row))
r = row[0:mirrorWidth]
r.reverse()
row.extend(r)

for i in range(len(row)):
data.append(row[i])
return data


def createImageData(size, prng):
width = size # Only support square icons for now
height = size
Expand All @@ -122,17 +177,20 @@ def createImageData(size, prng):


class Options(object):
def __init__(self,

def __init__(
self,
seed=None,
size=8,
color=None,
bgcolor=None,
spotcolor=None
spotcolor=None,
prng=XORshiftPRNG
):
self.seed = seed or hex(
math.floor((random.random() * math.pow(10, 16)))
)
self.prng = XORshiftPRNG(seed)
self.prng = prng(seed)
self.size = size
self.color = color or Color.createColor(self.prng)
self.bgcolor = bgcolor or Color.createColor(self.prng)
Expand All @@ -149,15 +207,16 @@ def draw_two(first, second):
return first + second + RESET


def renderANSI(opts, _print=False):
imageData = createImageData(opts.size, opts.prng)
def renderANSI(opts, _print=False, data_function=createImageData):
imageData = data_function(opts.size, opts.prng)
width = opts.size
rowsData = iter(
[imageData[i: i + width] for i in range(0, len(imageData), width)]
)
rows = []
odd, even = list(islice(rowsData, 2))
color_by_field = {
-1: (0, 0, 0),
0: opts.bgcolor.numeric_rgb,
1: opts.color.numeric_rgb,
2: opts.spotcolor.numeric_rgb,
Expand All @@ -179,11 +238,18 @@ def renderANSI(opts, _print=False):
return rows


def create_blockie(seed, _print=False):
def create_blockie(seed, _print=False, prng=XORshiftPRNG):
"""main method: creates a blockie with standard
options from `seed` and optionally print it to stdout."""
opts = Options(seed)
return renderANSI(opts, _print=_print)
opts = Options(seed, prng=prng, size=8)
return renderANSI(opts, _print=_print, data_function=createImageData)


def create_blockie_v2(seed, _print=False, prng=SHA256PRNG):
"""v2 version method: create a "pyramie" with standard
options from `seed` and optionally print it to stdout."""
opts = Options(seed, prng=prng, size=10)
return renderANSI(opts, _print=_print, data_function=createPyramidData)


def main():
Expand All @@ -193,10 +259,12 @@ def main():
from blockies.vanity import vanity
for a in vanity:
create_blockie(a, True)
create_blockie_v2(a, True, prng=SHA256PRNG)
print(a)
else:
seed = sys.argv[1].lower()
create_blockie(seed, True)
create_blockie_v2(seed, True, prng=SHA256PRNG)


if __name__ == '__main__':
Expand Down