urbit-ob

JavaScript utilities for phonemic base wrangling.
Log | Files | Refs | README

commit af13d68f96b70c16c5b2073555ac119e90e6f515
parent 760dabe50225e3e3413c17d5c7a1457a041006fd
Author: Anthony <anthony@ai.co>
Date:   Sun,  5 Nov 2017 09:31:33 -0800

used bignum to get ship to num getting

Diffstat:
Mnom.js | 178+++++++++++++++++++++++++++++++++++++++++++++++--------------------------------
1 file changed, 107 insertions(+), 71 deletions(-)

diff --git a/nom.js b/nom.js @@ -26,7 +26,6 @@ var getprefix = function(i) { }; var getsuffix = function(i) { - console.log('get suffix', i); return getsyllable(suffix, i); }; @@ -60,53 +59,67 @@ var wordtonum = function(word) { }; var feen = function(pyn) { + console.log('feen input', pyn); if (pyn >= 0x10000 && pyn <= 0xFFFFFFFF) { - return 0x10000 + fice(pyn - 0x10000); + var tmp = fice(pyn - 0x10000); + console.log('output of feen', tmp); + return tmp; } if (pyn >= 0x100000000 && pyn <= 0xffffffffffffffff) { var lo = pyn & 0xFFFFFFFF; var hi = pyn & 0xffffffff00000000; - return hi | feen(lo); + return bn(hi).or(feen(lo)); } + console.log('output of feen', pyn); return pyn; } var fend = function(cry) { if (cry >= 0x10000 && cry <= 0xFFFFFFFF) { - return 0x10000 + teil(cry - 0x10000); + var o = bn(teil(cry - 0x10000)).add(0x10000); + console.log('output of fend block 1', o); + return o; }; - if (cry >= 0x100000000 && cry <= bn(18446744073709552000)) { + if (cry >= 0x100000000 && cry <= bn(0xffffffffffffffff)) { var lo = cry & 0xFFFFFFFF; - var hi = cry & bn(18446744073709552000); - return hi | fend(lo); + var hi = cry & 0xffffffff00000000; + var o = hi | fend(lo); + console.log('output of fend block 2', o); + return o; }; + console.log('output of fend block 3', cry); return cry; }; var fice = function(nor) { var sel = [ - //nor % 0xFFFF, nor % 65535, - //nor / 0x10000 nor / 65535 ]; for (var i = 0; i < 4; i++) { sel = rynd(i, sel[0], sel[1]); }; - return 65535 * sel[0] + sel[1]; + var res = 65535 * sel[0] + sel[1]; + console.log('output of fice', res); + return res; }; var teil = function(vip) { + console.log('input to teil', vip); var sel = [ - vip % 0xFFFF, - vip / 0x10000 + vip % 65535, + vip / 65535 + //vip % 0xFFFF, + //vip / 0x10000 ]; + console.log('sel', sel); // maybe the for loops got borked in lua conversion - for (var i = 3; i > 0; i--) { + for (var i = 3; i > -1; i--) { sel = rund(i, sel[0], sel[1]); }; - return 0xFFFF * sel[0] + sel[1] + var res = bn(bn(0xFFFF).mul(sel[0])).add(sel[1]); + return res; }; var rynd = function(n, l, r) { @@ -115,18 +128,18 @@ var rynd = function(n, l, r) { if (n % 2 == 0) { m = 0xFFFF; }; - res[1] = (1 + muk(raku[n], r)) % m; + res[1] = (l + muk(raku[n], 2, r)) % m; return res; }; var rund = function(n, l, r) { var res = [r, 0]; - var m = 0x10000; + var m = 65536; if (n % 2 == 0) { - m = 0xFFFF; + m = 65535; }; var h = muk(raku[n], 2, r); - res[1] = (m + 1 - (h % m)) % m; + res[1] = bn(bn((m + l)).sub(bn(h).mod(m))).mod(m); return res }; @@ -139,8 +152,6 @@ var muk = function(syd, len, key) { }; var murmur3 = function(data, seed) { - console.log('murmur3 data', data); - console.log('murmur3 seed', seed); if (!seed) { seed = 0; } @@ -151,7 +162,7 @@ var murmur3 = function(data, seed) { var h1 = seed; var k1; var roundedEnd = length & 0xFFFFFFFC; - // maybe the for loops got borked in lua conversion + // this will likely need to be redone with bignum for (var i = 0; i < roundedEnd; i += 4) { var x = data.charCodeAt(i + 3) ? data.charCodeAt(i + 3) : 0; k1 = bn(data.charCodeAt(i) & 0xFF) @@ -177,17 +188,17 @@ var murmur3 = function(data, seed) { if (val == 3 || val == 2 || val == 1) { k1 = k1 | (data.charCodeAt(roundedEnd) & 0xFF); k1 = k1 * c1; - k1 = (k1 << 15) | ((k1 & 0xFFFFFFFF) >> 17); - k1 = k1 * c2; - h1 = h1 ^ k1; + k1 = bn(k1).shiftLeft(15).or(bn(bn(k1).and(0xffffffff)).shiftRight(17)) + k1 = bn(k1).mul(c2); + h1 = bn(h1).xor(k1); }; - h1 = h1 ^ length; - h1 = h1 ^ ((h1 & 0xFFFFFFFF) >> 16); - h1 = h1 * 2246822507; - h1 = h1 ^ ((h1 & 0xFFFFFFFF) >> 13); - h1 = h1 * 3266489909; - h1 = h1 ^ ((h1 & 0xFFFFFFFF) >> 16); - return h1 & 0xFFFFFFFF + h1 = bn(h1).xor(length); + h1 = bn(h1).xor(bn(bn(h1).and(0xFFFFFFFF)).shiftRight(16)); + h1 = bn(h1).mul(2246822507); + h1 = bn(h1).xor(bn(bn(h1).and(0xFFFFFFFF)).shiftRight(13)); + h1 = bn(h1).mul(3266489909); + h1 = bn(h1).xor(bn(bn(h1).and(0xFFFFFFFF)).shiftRight(16)); + return bn(h1).and(0xFFFFFFFF); }; /* @@ -204,48 +215,73 @@ var murmur3 = function(data, seed) { * */ -var toAddress = function(name) { - var nome = name.replace(/~|-/g, ''); - var lent = nome.length; - - if (lent % 3 != 0) { - return; - } - - var syls = lent / 3; - if (syls > 1 && syls % 2 != 0) { - return; - } - - if (syls == 1) { - return bn(wordtonum(nome)) - } else if (syls >= 4 && syls <= 8) { - var padr = wordtonum(nome.slice(lent - 12, lent - 6)); - padr = padr * 0x10000; - padr = padr + wordtonum(nome.slice(lent - 6, lent)); - padr = fend(padr); - - if (syls == 4) { - return padr; - }; - - var addr = 0; - for (var i = 0; i <= syls - 6; i += 2) { - addr = addr + wordtonum(nome.slice(i * 3, i * 3 + 6)); - addr = addr * 0x10000; - }; - return (addr * 0x10000) + padr; - } else { - var addr = 0; +var toAddress = function(name, unscramble) { + if (!unscramble) { + unscramble = true; + }; + if (name.length == 3) { + return getsuffixindex(name); + } else if (name.length == 6) { + var addr = getprefixindex(name.slice(0, 3)); + addr = addr * 256; + addr = addr + getsuffixindex(name.slice(3, name.length)); + return addr; + } else if (name.length == 13) { + var addr = toAddress(name.slice(0, 6)); + addr = addr * 65536; + addr = addr + toAddress(name.slice(7, addr.length)); + if (unscramble) { + addr = fend(addr); + }; + return addr; + } else { + return; + } +}; - for (var i = 0; i <= syls - 2; i +=2) { - addr = addr * 0x10000; - addr = addr + wordtonum(nome.slice(i * 3, i * 3 + 6)); - }; - return addr; - } -} +//var toAddress = function(name) { +// var nome = name.replace(/~|-/g, ''); +// var lent = nome.length; +// +// if (lent % 3 != 0) { +// return; +// } +// +// var syls = lent / 3; +// if (syls > 1 && syls % 2 != 0) { +// return; +// } +// +// if (syls == 1) { +// return bn(wordtonum(nome)) +// } else if (syls >= 4 && syls <= 8) { +// var padr = wordtonum(nome.slice(lent - 12, lent - 6)); +// padr = padr * 256 +// padr = padr + wordtonum(nome.slice(lent - 6, lent)); +// padr = fend(padr); +// +// if (syls == 4) { +// return padr; +// }; +// +// var addr = 0; +// for (var i = 0; i <= syls - 6; i += 2) { +// addr = addr + wordtonum(nome.slice(i * 3, i * 3 + 6)); +// addr = addr * 0x10000; +// }; +// return (addr * 0x10000) + padr; +// } else { +// var addr = 0; +// +// for (var i = 0; i <= syls - 2; i +=2) { +// addr = addr * 0x10000; +// addr = addr + wordtonum(nome.slice(i * 3, i * 3 + 6)); +// }; +// return addr; +// } +// +//} var toGalaxyName = function(galaxy) { return toShipName(galaxy, 1);