Module: CheapRandom

Defined in:
lib/cheap_random.rb,
lib/cheap_random/version.rb

Constant Summary collapse

VERSION =
'0.9.4'

Class Method Summary collapse

Class Method Details

.cheap_random3!(is_randomizing, perm, s) ⇒ Object



163
164
165
# File 'lib/cheap_random.rb', line 163

def self.cheap_random3!(is_randomizing, perm, s)
  cheap_random5!(is_randomizing, perm, s, 0, s.length)
end

.cheap_random5!(is_randomizing, startperm, buffer, offset, length) ⇒ Object

length > 0



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/cheap_random.rb', line 137

def self.cheap_random5!(is_randomizing, startperm, buffer, offset, length)
  nextperm = startperm + 'NEXT'
  perm = (' ' * 256) + 'PERM'
  translation = (' ' * 256) + 'TRAN'
  len = length
  off = offset
  while len > 0 do
    bs = next_block_size len
    (0..255).each do |x|
      perm.setbyte x, nextperm.getbyte(x)
    end
    cheap_random7(is_randomizing, perm, nextperm, translation, buffer, off, bs)
    off += bs
    len -= bs
  end
  nextperm[0..255]
end

.cheap_random7(is_randomizing, perm, nextperm, translation, buffer, offset, length) ⇒ Object

is_randomizing is a boolean cheap_random with is_randomizing true, randomizes cheap_random with is_randomizing false, un-randomizes perm is a read only string of length 256 with each byte represented once perm is cheap_random’s seed nextperm is a writeable string of length 256 comes in as a copy of perm it is the next seed for use in chain seeding translation is a buffer needed for perm reversed as a substitution transformation buffer is read as unrandomized text and written as randomized text offset is a pointer into the buffer length is from 1 to 256 it is the number of bytes to process starting at offset in buffer



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cheap_random.rb', line 65

def self.cheap_random7(is_randomizing, perm, nextperm, translation, buffer, offset, length)
  if is_randomizing then
    random_cheap_random(perm, nextperm, buffer, offset, length)
  else
    (0..255).each do |x|
      translation.setbyte perm.getbyte(x), x
    end
    unrandom_cheap_random(perm, nextperm, translation, buffer, offset, length)
  end
  return nil
end

.cheap_seed!(s) ⇒ Object



167
168
169
170
171
172
# File 'lib/cheap_random.rb', line 167

def self.cheap_seed!(s)
  ip = reverse_perm
  result = cheap_random3!(true, ip, s)
  cheap_random3!(false, ip, s)
  result
end

.next_block_size(size) ⇒ Object



130
131
132
133
134
# File 'lib/cheap_random.rb', line 130

def self.next_block_size(size)
  return 256 if size > 511
  return size if size <= 256
  size - (size >> 1)
end

.permute(perm, buffer, offset, length) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cheap_random.rb', line 15

def self.permute(perm, buffer, offset, length)
  disp = 0
  temp = 0
  y = 0
  (0...length).each do |x| 
    while perm.getbyte(y) >= length do
      y += 1
    end
    disp = offset + perm.getbyte(y)
    y += 1
    temp = buffer.getbyte disp
    buffer.setbyte disp, buffer.getbyte(offset + x)
    buffer.setbyte(offset + x, temp)
  end
  nil
end

.random_cheap_random(perm, nextperm, buffer, offset, length) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/cheap_random.rb', line 77

def self.random_cheap_random(perm, nextperm, buffer, offset, length)
  disp = 0
  temp = 0
  y = 0
  (0...length).each do |x| 
    disp = offset + x
    y = buffer.getbyte(disp) ^ perm.getbyte(x)
    y = perm.getbyte((y + x + x + x) & 255)
    buffer.setbyte disp, y
    temp = nextperm.getbyte x
    nextperm.setbyte x, nextperm.getbyte(y)
    nextperm.setbyte y, temp
  end
  y = 0
  (0...length).each do |x| 
    while perm.getbyte(y) >= length do
      y += 1
    end
    disp = offset + perm.getbyte(y)
    y += 1
    temp = buffer.getbyte disp
    buffer.setbyte disp, buffer.getbyte(offset + x)
    buffer.setbyte(offset + x, temp)
  end
  nil
end

.reverse_permObject



155
156
157
158
159
160
161
# File 'lib/cheap_random.rb', line 155

def self.reverse_perm
  s = ' ' * 256
  (0..255).each do |x|
    s.setbyte(x, 255 - x)
  end
  s
end

.subperm(perm, length) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/cheap_random.rb', line 3

def self.subperm(perm, length)
  result = ' ' * length
  idx = 0
  (0..255).each do |x|
    if perm.getbyte(x) < length
      result.setbyte idx, perm.getbyte(x)
      idx += 1
    end
  end
  result
end

.unpermute(perm, buffer, offset, length) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cheap_random.rb', line 32

def self.unpermute(perm, buffer, offset, length)
  disp = 0
  temp = 0
  y = 255
  (1..length).each do |x| 
    while perm.getbyte(y) >= length do
      y -= 1
    end
    disp = offset + perm.getbyte(y)
    y -= 1
    temp = buffer.getbyte disp
    buffer.setbyte disp, buffer.getbyte(offset + length - x)
    buffer.setbyte(offset + length - x, temp)
  end
  nil
end

.unrandom_cheap_random(perm, nextperm, translation, buffer, offset, length) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/cheap_random.rb', line 104

def self.unrandom_cheap_random(perm, nextperm, translation, buffer, offset, length)
  disp = 0
  temp = 0
  y = 255
  (1..length).each do |x| 
    while perm.getbyte(y) >= length do
      y -= 1
    end
    disp = offset + perm.getbyte(y)
    y -= 1
    temp = buffer.getbyte disp
    buffer.setbyte disp, buffer.getbyte(offset + length - x)
    buffer.setbyte(offset + length - x, temp)
  end
  y = 0
  (0...length).each do |x| 
    disp = offset + x
    y = buffer.getbyte disp
    buffer.setbyte(disp, ((translation.getbyte(y) + 768 - x - x - x) & 255) ^ perm.getbyte(x))
    temp = nextperm.getbyte(x)
    nextperm.setbyte x, nextperm.getbyte(y)
    nextperm.setbyte y, temp
  end
  nil
end