urbit-ob

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

commit f3ce19a94bfd03afd372d271d24bf28bbbb8f5cf
parent 2d1518224fcbdd6180c2ae23fcaf65ae13cfde63
Author: Jared Tobin <jared@jtobin.io>
Date:   Fri, 28 Sep 2018 16:18:57 +1200

Merge pull request #4 from urbit/jt-bigint

Support @p for bn.js values
Diffstat:
Mpackage.json | 2+-
Msrc/index.js | 77+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
Mtest/core.test.js | 22++++++++++++++++++++++
3 files changed, 94 insertions(+), 7 deletions(-)

diff --git a/package.json b/package.json @@ -1,6 +1,6 @@ { "name": "ob-js", - "version": "1.1.0", + "version": "1.2.0", "description": "utilities to convert urbit ship names back and forth between @p and @ud", "main": "dist/index.js", "scripts": { diff --git a/src/index.js b/src/index.js @@ -2,7 +2,6 @@ * Utility methods */ -// var bn = require('bignum') const bnjs = require('bn.js') const { reduce, isUndefined, isString, every, map } = require('lodash') @@ -123,16 +122,17 @@ const arr2patp = a => reduce(a, (acc, syl, i) => isEven(i) const feen = pyn => { - const f = 4294967295 if (pyn >= 0x10000 && pyn <= 0xFFFFFFFF) { const tmp = fice(pyn - 0x10000) + 0x10000 return tmp } if (pyn >= 0x100000000 && pyn <= 0xffffffffffffffff) { - const pynBn = new bnjs(pyn) - const lo = pynBn.and(f) - const hi = pynBn.and('18446744069414584000') - return hi.or(feen(lo)).toNumber() + const f = new bnjs('4294967295') + const g = new bnjs('18446744069414584000') + const lo = pyn.and(f) + const hi = pyn.and(g) + let next = new bnjs(feen(lo)) + return hi.or(next) } return pyn } @@ -293,7 +293,68 @@ const murmur3 = (data, seed) => { * */ +const bex = (n) => { + const two = new bnjs(2) + return two.pow(n) +} + +const rsh = (a, b, c) => { + const sub = bex(a).mul(b) + return c.div(bex(sub)) +} + +const met = (a, b, c = new bnjs(0)) => { + const zero = new bnjs(0) + const one = new bnjs(1) + return b.eq(zero) + ? c + : met(a, rsh(a, one, b), c.add(one)) +} + +const end = (a, b, c) => { + return c.mod(bex(bex(a).mul(b))) +} + +const lsh = (a, b, c) => { + const sub = bex(a).mul(b) + return bex(sub).mul(c) +} + +// bignum patp +const patp = (n) => { + const zero = new bnjs(0) + const one = new bnjs(1) + const three = new bnjs(3) + const four = new bnjs(4) + + let sxz = new bnjs(feen(n)) + let dyy = met(four, sxz) + + let loop = (tsxz, timp, trep) => { + let log = end(four, one, tsxz) + let pre = getPrefix(rsh(three, one, log)) + let suf = getSuffix(end(three, one, log)) + let etc = + (timp.mod(four)).eq(zero) + ? timp.eq(zero) + ? '' + : '--' + : '-' + + let res = pre + suf + etc + trep + + return timp.eq(dyy) + ? trep + : loop(rsh(four, one, tsxz), timp.add(one), res) + } + + let dyx = met(three, sxz) + return '~' + + (dyx.lte(one) + ? getSuffix(sxz) + : loop(sxz, zero, '')) +} // returns the class of a ship from it's name const tierOfpatp = name => { @@ -438,6 +499,8 @@ module.exports = { toPlanetName: toPlanetName, isValidName: isValidName, + patp: patp, + _add2patp: _add2patp, _getsuffix: getSuffix, _muk: muk, @@ -461,4 +524,6 @@ module.exports = { _bin2dec: bin2dec, _dec2bin: dec2bin, _syl2bin: syl2bin, + + _arr2patp: arr2patp } diff --git a/test/core.test.js b/test/core.test.js @@ -1,3 +1,4 @@ +const bnjs = require('bn.js'); const ob = require('../src/index.js'); @@ -108,3 +109,24 @@ test('identifies binzod as a star from address', () => { test('identifies poldec-tonteg as a planet from address', () => { expect(ob.tierOfadd(9896704)).toBe('planet'); }); + +test('correctly encodes 0 as @p', () => { + let expected = '~zod'; + expect(ob.patp('0')).toBe(expected); +}); + +test('correctly encodes 4294967295 as @p', () => { + let expected = '~dostec-risfen'; + expect(ob.patp('4294967295')).toBe(expected); +}); + +test('correctly encodes 328256967394537077627 as @p', () => { + let expected = '~dozsyx--halrux-samlep-posmus-ranrux'; + expect(ob.patp('328256967394537077627')).toBe(expected); +}); + +test('correctly encodes a big hex string as @p', () => { + let input = new bnjs('7468697320697320736f6d6520766572792068696768207175616c69747920656e74726f7079', 16); + let expected = '~divmes-davset-holdet--sallun-salpel-taswet-holtex--watmeb-tarlun-picdet-magmes--holter-dacruc-timdet-divtud--holwet-maldut-padpel-sivtud'; + expect(ob.patp(input)).toBe(expected); +});