Skip to content

Instantly share code, notes, and snippets.

@operator-DD3
Created December 12, 2017 06:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save operator-DD3/45c2c1e15a8cb252fa4923f1651910c9 to your computer and use it in GitHub Desktop.
Save operator-DD3/45c2c1e15a8cb252fa4923f1651910c9 to your computer and use it in GitHub Desktop.
Spritz Encryption Routines
S = {}
i,j,k,z,a,v=0,0,0,0,0,0
w=1
N=2^9 --2^8 or 8bits
D=math.ceil(math.sqrt(N))
print("N:",N)
print("D:",D)
function InitializeState(N)
i,j,k,z,a=0,0,0,0,0
w=1
for v = 0, N-1 do
S[v] = v
end
end
function Absorb(I)
for v = 0, #I-1 do
AbsorbByte(I[v])
end
end
function AbsorbByte(b)
AbsorbNibble(LOW(b))
AbsorbNibble(HIGH(b))
end
function LOW(b)
return b % D
end
function HIGH(b)
return math.floor(b / D)
end
function AbsorbNibble(x)
if a == math.floor(N/2) then
Shuffle()
end
--Swap(S[a], S[math.floor(N/2) + x])
Swap(S, a, math.floor(N/2)+x)
a = (a + 1) % N
end
function Swap(array, index1, index2)
array[index1], array[index2] = array[index2], array[index1]
end
function AbsorbStop()
if a == math.floor(N/2) then
Shuffle()
end
a = (a + 1) % N
end
function Shuffle()
Whip(2*N)
Crush()
Whip(2*N)
Crush()
Whip(2*N)
a = 0
end
function Whip(r)
for v = 0, r-1 do
Update()
end
repeat
w = (w + 1) % N
until gcd(w, N) == 1
end
function gcd(a,b)
if b ~= 0 then
return gcd(b, a % b)
else
return math.abs(a)
end
end
function Crush()
for v = 0, math.floor(N/2)-1 do
if S[v] > S[(N - 1 - v) % N] then
--Swap(S[v], S[N - 1 - v])
Swap(S, v, (N - 1 - v) % N)
end
end
end
function Squeeze(r)
if a > 0 then
Shuffle()
end
P=nil;P={};
for v = 0, r-1 do
P[v] = Drip()
end
return P
end
function Drip()
if a > 0 then
Shuffle()
end
Update()
return Output()
end
function Update()
i = (i + w) % N
j = (k + S[(j + S[i]) % N]) % N
k = (i + k + S[j]) % N
Swap(S,i,j)
end
function Output()
--z = S[j + S[i + S[z + k]]]
z = S[(j + S[(i + S[(z + k) % N]) % N]) % N]
--print("OUTPUT\tz:",z)
return z
end
InitializeState(N)
print(table.concat(Squeeze(D),", "))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment