kuboon
3/29/2019 - 8:45 AM

chaos_rev.rb

N=32

BYTE = false

x0=[0]*N
x1=[0]*N
x2=[0]*N
a=[0]*N


random=Random.new(1234)

def rp(a)
  (0..(N-1)).each { |i|
    a[i]=i
  }

  (0..(N-3)).each { |i|
    # rand from i+1 to N-1
    j = (rand(0..256) % (N - 1 - i)) + i + 1
    # swap a[i] and a[j]
    x = a[j]
    a[j] = a[i]
    a[i] = x
  }
  if (a[N - 1] == N - 1) 
    a[N - 1] = a[N - 2]
    a[N - 2] = N - 1
  end

end

w=[0]*N
ww=[0]*N
b=[0]*N

if !BYTE
  b=[0]*16+[1]*16
else
  (0..(N-1)).each{ |i|
    b[i]=random.rand(0..0xffffffffffffffff)
    }
end

rp(x0)
rp(x1)
print x0
print x1
(0..(N-1)).each { |i|
  x2[x0[i]]=i
}
j=0

file=open("rand.dat","wb")

while(j<1000000)
  (0..(N-1)).each { |i|
    ww[i]=b[x1[i]]
    b[i]^=ww[i]
  }
  if !BYTE
    aa=0
    (0..3).each { |k|
      (k*8..k*8+8-1).each { |i|
        aa=aa<<1
        aa=aa^b[i]
      }
    }
    file.write [aa].pack "i*"
  else 
    file.write b.pack("C*")    
  end
  (0..(N-1)).each { |i|
    w[i]=x0[x1[x2[i]]]
  }
  (0..(N-1)).each { |i|
    x1[i]=w[i]
  }
  j=j+1
end
file.close