urbit-ob

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

commit 475bd441aa2778795e4c51276805edce245cfb40
parent 04ae6635b4bca0624743fba97119969be846dc3c
Author: Jared Tobin <jared@jtobin.io>
Date:   Thu, 14 Mar 2019 16:03:00 +1300

Use alternate solution to @p problem.

@max19 recommended a solution to the @p problem in urbit/arvo#1105 that
allows us to preserve most, if not all, of the existing @p values, while
also removing colliding cases.

Diffstat:
Msrc/internal/ob.js | 32++++++++++++++++++++++++++------
Mtest/ob.test.js | 16++++++++--------
2 files changed, 34 insertions(+), 14 deletions(-)

diff --git a/src/internal/ob.js b/src/internal/ob.js @@ -123,6 +123,9 @@ const fend = (arg) => { * * See: Black and Rogaway (2002), "Ciphers with arbitrary finite domains." * + * Note that this has been adjusted from the reference paper in order to + * support some legacy behaviour. + * * @param {String, Number, BN} * @return {BN} */ @@ -143,8 +146,10 @@ const fe = (r, a, b, f, m) => { if (j > r) { return ( r % 2 !== 0 - ? a.mul(ell).add(arr) - : a.mul(arr).add(ell) + ? a.mul(arr).add(ell) + : arr.eq(a) + ? a.mul(arr).add(ell) + : a.mul(ell).add(arr) ) } else { const eff = f(j - 1, arr) @@ -186,6 +191,11 @@ const fice = (arg) => { /** * Reverse 'feis'. * + * See: Black and Rogaway (2002), "Ciphers with arbitrary finite domains." + * + * Note that this has been adjusted from the reference paper in order to + * support some legacy behaviour. + * * @param {Number, String, BN} arg * @return {BN} */ @@ -223,15 +233,25 @@ const fen = (r, a, b, f, m) => { } } - const R = + const ahh = + r % 2 !== 0 + ? m.div(a) + : m.mod(a) + + const ale = r % 2 !== 0 ? m.mod(a) : m.div(a) const L = - r % 2 !== 0 - ? m.div(a) - : m.mod(a) + ale.eq(a) + ? ahh + : ale + + const R = + ale.eq(a) + ? ale + : ahh return loop(r, L, R) } diff --git a/test/ob.test.js b/test/ob.test.js @@ -65,20 +65,20 @@ describe('fein/fynd', () => { it('fein matches expected reference values', () => { let input = new BN('123456789') - let output = new BN('1645384789') + let output = new BN('4133907371') expect(fein(input).eq(output)).to.equal(true) input = new BN('15663360') - output = new BN('1032628475') + output = new BN('3867563781') expect(fein(input).eq(output)).to.equal(true) }) it('fynd matches expected reference values', () => { - let input = new BN('1645384789') + let input = new BN('4133907371') let output = new BN('123456789') expect(fynd(input).eq(output)).to.equal(true) - input = new BN('1032628475') + input = new BN('3867563781') output = new BN('15663360') expect(fynd(input).eq(output)).to.equal(true) }) @@ -124,20 +124,20 @@ describe('feis/tail', () => { it('feis matches expected reference values', () => { let input = new BN ('123456789') - let output = new BN('3869445208') + let output = new BN('4076532648') expect(feis(input).eq(output)).to.equal(true) input = new BN('15663360') - output = new BN('2746999232') + output = new BN('2239503936') expect(feis(input).eq(output)).to.equal(true) }) it('tail matches expected reference values', () => { - let input = new BN('3869445208') + let input = new BN('4076532648') let output = new BN ('123456789') expect(tail(input).eq(output)).to.equal(true) - input = new BN('2746999232') + input = new BN('2239503936') output = new BN('15663360') expect(tail(input).eq(output)).to.equal(true) })