-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinorg.js
373 lines (331 loc) · 9.9 KB
/
binorg.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
103
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// const varint = require('varint')
export const randomBytes = n => {
const buf = new Uint8Array(n)
globalThis.crypto.getRandomValues(buf)
return buf
}
/*
* Sizes where (bits - 1) is divisble by 3
* really any 3 * n + 1 size goes but let's
* minimize litter bytes in cyberspace
*
> Array.from(new Array(32)).map((_,i)=>1<<i).filter(i => i % 3 === 1).map(i => i>>3)
[ 2, 16bit Cryptoling
8, 64bit Hashmob
32, 256bit Player
128, 1024bit Raid-boss
512,
2048,
8192 ]
*/
/* eslint-disable no-multi-spaces */
/*
* Chosen multiple of 3 configurations
*/
export const NIBBLING = 2 // 7bit
export const TWOBYTER = 5 // 16bit
export const HASHCHAR = 21 // 64bit
export const CRYPTORG = 85 // 256bit
export const CRYPTOLISK = 341 // 1024bit
/* eslint-enable no-multi-spaces */
const noBuf = {
allocUnsafe (s) { return Array.from(new Array(s)) },
alloc (s) { return allocUnsafe.map(() => 0) }
}
const { alloc, allocUnsafe } = (typeof Buffer !== 'undefined') ? Buffer : noBuf
export const DEF_SIZE = 8
/*
function countLeadingZeroes (p, SIZE = DEF_SIZE) {
let n = 0
for (let i = 0; i < SIZE; i++) {
let b = p[i]
for (let j = 0; j < 8; j++) {
if (b & 1) return n
n++
b >>= 1
}
}
}
*/
/*
export function inspect (x, SIZE = DEF_SIZE) {
console.debug('Inspecting Cryptoling', x.hexSlice())
console.log('DNA Sequence:', _toStr(x))
const str = strengthOf(x)
const agl = agilityOf(x)
const intl = intelligenceOf(x)
let cls = 'Villager'
const maxStat = (SIZE * 8 - ((SIZE * 8) % 3))
const avg = maxStat >> 1
if (str > avg && agl > avg && intl > avg) cls = 'Archvillager'
else if (str > avg && agl > avg) cls = 'Monk'
else if (agl > avg && intl > avg) cls = 'Marksman'
else if (str > avg && intl > avg) cls = 'Spellsword'
else if (str > avg) cls = 'Warrior'
else if (agl > avg) cls = 'Rogue'
else if (intl > avg) cls = 'Mage'
console.table({
CLASS: cls,
LIFE: lifeOf(x),
STR: str,
AGL: agl,
INT: intl
})
}
*/
export class Binorg {
constructor (mulitpleOf3, ...crdtMems) {
this.m3 = mulitpleOf3
this.nb = mulitpleOf3 * 3 + 1
this.dna = crdtMems[0] || roll(this.nb)
this.exp = crdtMems[1] || [1] // empty lvl 0
}
get lvl () {
const z = countTrailingZeroes(this.exp) +
this.nb - (this.exp.length << 3)
return this.m3 * 3 - z
}
get life () {
const damage = countTrailingZeroes(this.dna) +
this.nb - (this.dna.length << 3)
return this.m3 * 3 - damage
}
// Holy trinity
get pwr () { return this._extractAttr(0b110, 3) }
get agl () { return this._extractAttr(0b101, 3) }
get wis () { return this._extractAttr(0b011, 3) }
damage (points) {
if (points < 1) return
while (points--) downShift(this.dna)
}
progress (parity) {
const x = this.exp
const o = upShift(x, parity)
if (o) { // create new byte when bit overflows
if (Array.isArray(x)) x[x.length] = 1
else { // resize buffer
this.exp = alloc(roundByte(this.m3 * 3 + 1))
for (let i = 0; i < x.length; i++) this.exp[i] = x[i]
this.exp[x.length] = 1
}
}
}
// return a list of n trailing zeros of each pad.
get clock () {
// TODO: figure out how to do this for dynamic list of shiftregisters.
// Currently the problem is knowing if it a shrinking or expanding
// register
return [
this.m3 * 3 - this.life, // Same as 'accumulated damage'
this.lvl
]
}
_binorgInspect () {
const clzzName = Object.getPrototypeOf(this).constructor.name
let str = `${clzzName}[${this.clock}] {\n`
str += ` Size: ${this.m3 * 3 + 1}bit\n`
str += ` LVL: ${this.lvl} HP ${this.life}\n`
str += ` PWR: ${this.pwr} AGL: ${this.agl} WIS: ${this.wis}\n`
str += ` DNA: ${binstr(this.dna)}\n`
str += ` EXP: ${binstr(this.exp)}\n`
str += '}\n'
return str
}
_extractAttr (mask, d) {
let i = 0
let o = 0
mapOverlap([this.dna, this.exp], ([d, x]) => {
const b = d ^ x ^ ((mask >> (i++ % 3)) & 1)
if (b) o++
return b
})
return o
}
// Class is next to useless without theese methods.
static get decode () { return decode }
static get encode () { return encode }
static get encodingLength () { return encodingLength }
}
// Round bits upwards to closet byte
export function roundByte (b) { return (b >> 3) + (b % 8 ? 1 : 0) } // = Math.ceil(b / 8)
/*
* Treats buffer as a series of latched 8bit shift-registers
* shifts all bits 1 step from low to high.
* _____________
* input -> | 0 1 0 1 1 1 | -> return overflow
* -------------
* Low High
*/
export function upShift (x, inp = 0) {
let c = inp ? 1 : 0
for (let i = 0; i < x.length; i++) {
const nc = (x[i] >> 7) & 1
x[i] = (x[i] << 1) | c
c = nc
}
return c
}
/*
* Opposite of upShift, shifts all bits
* 1 step towards low.
* _____________
* output <- | 0 1 0 1 1 1 | <- input
* -------------
* Low High
*/
export function downShift (x, inp = 0) {
let i = x.length
let c = (inp ? 1 : 0) << 7
while (i--) {
const nc = (x[i] & 1) << 7
x[i] = c | x[i] >> 1
c = nc
}
return c ? 1 : 0
}
export function insertLifeMarker (o, bits) {
const r = bits % 8 ? bits % 8 : 8
const size = o.length
o[size - 1] = o[size - 1] & // Fix last byte
((1 << r) - 1) | // Mask after LIFE marker
(1 << (r - 1)) // Insert LIFE marker
return o
}
export function roll (bits, generator = randomBytes) {
const size = roundByte(bits)
const o = generator(size)
const r = bits % 8 ? bits % 8 : 8
o[size - 1] = o[size - 1] & // Fix last byte
((1 << r) - 1) | // Mask after LIFE marker
(1 << (r - 1)) // Insert LIFE marker
return o
}
export function countTrailingZeroes (x) {
let i = x.length
let n = 0 // vBits - (x.length << 3)
while (i--) {
for (let j = 7; j >= 0; j--) {
if (x[i] & (1 << j)) return n
n++
}
}
return n
}
export function countOnes (p) {
let out = 0
for (let i = 0; i < p.length; i++) {
let b = p[i]
while (b) {
if (b & 1) out++
b >>= 1
}
}
return out
}
// Higher order function to align multiple registers
// and iterate the bits within the overlap.
//
// Operational zone
// Start bit _____
// * | | | |
// A: 0 0 0 0 1 1 0 1 0 1 1
// B: 0 0 1 0 1 1 1 1 1 1 0
// C: 0 0 0 1 0 0 1 0
// HI ^ LOW
// idx: 0 1 2 3
//
export function mapOverlap (registers, process) {
process = process || (() => {})
const o = registers.map(() => 0)
const found = registers.map(() => 0)
const outputs = registers.map(() => 0)
let sync = false
const result = []
let cancel = false
while (!cancel) {
for (let r = 0; r < registers.length; r++) {
if (!sync && found[r]) continue // wait-state reached
const reg = registers[r]
const bitIdx = o[r]++
const byteIdx = reg.length - 1 - (bitIdx >> 3)
// return function as soon as first buffer end is reached.
if (byteIdx < 0) return result
const bit = (reg[byteIdx] >> (7 - (bitIdx % 8))) & 1
if (!found[r]) {
if (bit) found[r] = true
continue
}
outputs[r] = bit
}
if (!sync) {
sync = found.reduce((c, n) => c && n, true)
continue
}
const z = process(outputs, () => { cancel = true })
if (upShift(result, z)) result[result.length] = 1
}
return result
}
export function encodingLength (org) {
if (!(org instanceof Binorg)) throw new Error('NotABinorgError')
let tally = varint.encodingLength(org.m3)
const dnaLen = org.dna.length - (countTrailingZeroes(org.dna) >> 3)
tally += dnaLen + varint.encodingLength(dnaLen)
const xpLen = org.exp.length - (countTrailingZeroes(org.exp) >> 3)
tally += xpLen + varint.encodingLength(xpLen)
return tally
}
export function encode (org, buffer, offset = 0) {
if (!(org instanceof Binorg)) throw new Error('NotABinorgError')
if (!buffer) buffer = allocUnsafe(encodingLength(org) + offset)
const start = offset
varint.encode(org.m3, buffer, offset)
offset += varint.encode.bytes
const dnaLen = org.dna.length - (countTrailingZeroes(org.dna) >> 3)
varint.encode(dnaLen, buffer, offset)
offset += varint.encode.bytes
for (let i = 0; i < dnaLen; i++) buffer[offset + i] = org.dna[i]
offset += dnaLen
const xpLen = org.exp.length - (countTrailingZeroes(org.exp) >> 3)
varint.encode(xpLen, buffer, offset)
offset += varint.encode.bytes
for (let i = 0; i < xpLen; i++) buffer[offset + i] = org.exp[i]
offset += xpLen
encode.bytes = offset - start
return buffer
}
export function decode (buffer, offset = 0, end, Clss = Binorg) {
if (!buffer) throw new Error('First argument must be bufferish')
end = end || buffer.length
const start = offset
const m3 = varint.decode(buffer, offset)
offset += varint.decode.bytes
const dnaLen = varint.decode(buffer, offset)
offset += varint.decode.bytes
const dna = allocUnsafe(dnaLen)
if (dnaLen + offset > end) throw new RangeError('Buffer underflow')
for (let i = 0; i < dnaLen; i++) dna[i] = buffer[offset + i]
offset += dnaLen
const xpLen = varint.decode(buffer, offset)
offset += varint.decode.bytes
if (xpLen + offset > end) throw new RangeError('Buffer underflow')
const xp = allocUnsafe(xpLen)
for (let i = 0; i < xpLen; i++) xp[i] = buffer[offset + i]
offset += xpLen
decode.bytes = offset - start
// TODO: 3.0 this should return { m: 21, pads: [dna, xp, ...] }
// Each pad is a tiny single-bit writer. single bit hypercore...
// make a rant/desc about this idea somewhere else.
return new Clss(m3, dna, xp) // TODO: maybe just return {m3, dna, xp}
}
export function binstr (x, cap) {
cap = cap || x.length * 8
let str = ''
for (let i = 0; i < x.length; i++) {
for (let j = 0; j < 8; j++) {
if (cap === i * 8 + j) str += '|'
str += x[i] & (1 << j) ? '1' : '0'
}
}
return str
}