diff --git a/fe-test.js b/fe-test.js index 1b7e723..5230f5c 100644 --- a/fe-test.js +++ b/fe-test.js @@ -1,6 +1,6 @@ const ec = require('./fe25519_25.js') const sodium = require('./') -const wasm = require('./fe25519_25/fe25519_invert')({ +const invert = require('./fe25519_25/fe25519_invert')({ imports: { debug: { log (...args) { @@ -13,7 +13,7 @@ const wasm = require('./fe25519_25/fe25519_invert')({ } } }) -const wasm2 = require('./fe25519_25/fe25519_pow22523')({ +const pow = require('./fe25519_25/fe25519_pow22523')({ imports: { debug: { log (...args) { @@ -39,10 +39,10 @@ var r = ec.ge3() function wasm_inv (h, f) { var buf = Buffer.from(f.buffer) - wasm.memory.set(buf) - wasm.exports.fe25519_invert(40, 0) + invert.memory.set(buf) + invert.exports.fe25519_invert(40, 0) - buf = Buffer.from(wasm.memory.slice(40, 80)) + buf = Buffer.from(invert.memory.slice(40, 80)) for (let i = 0; i < 10; i++) { h[i] = buf.readUInt32LE(4 * i) } @@ -51,10 +51,10 @@ function wasm_inv (h, f) { function wasm_pow (h, f) { var buf = Buffer.from(f.buffer) - wasm2.memory.set(buf) - wasm2.exports.fe25519_pow22523(40, 0) + pow.memory.set(buf) + pow.exports.fe25519_pow22523(40, 0) - buf = Buffer.from(wasm2.memory.slice(40, 80)) + buf = Buffer.from(pow.memory.slice(40, 80)) for (let i = 0; i < 10; i++) { h[i] = buf.readUInt32LE(4 * i) } @@ -171,65 +171,63 @@ console.timeEnd('standard') ec.fe25519_tobytes(res, b) console.log('tess :', res.toString('hex')) -console.log(wasm.buffer.length) - -console.time('pure wasm') +console.time('pure invert') for (let i = 0; i < 10000; i++) wasm_pow(b, a) -console.timeEnd('pure wasm') +console.timeEnd('pure invert') ec.fe25519_tobytes(res, b) console.log('tess :', res.toString('hex')) -// ec.fe25519_pow22523(a, a) -// ec.fe25519_tobytes(res, a) -// console.log('fe_p25:', res.toString('hex')) +ec.fe25519_pow22523(a, a) +ec.fe25519_tobytes(res, a) +console.log('fe_p25:', res.toString('hex')) -// ec.fe25519_cneg(a, a, 1) -// ec.fe25519_tobytes(res, a) -// console.log('fe_cng:', res.toString('hex')) +ec.fe25519_cneg(a, a, 1) +ec.fe25519_tobytes(res, a) +console.log('fe_cng:', res.toString('hex')) -// ec.sc25519_mul(res, an, bn) -// console.log('sc_mul:', res.toString('hex')) +ec.sc25519_mul(res, an, bn) +console.log('sc_mul:', res.toString('hex')) -// ec.sc25519_muladd(res, an, bn, cn) -// console.log('sc_mad:', res.toString('hex')) +ec.sc25519_muladd(res, an, bn, cn) +console.log('sc_mad:', res.toString('hex')) -// ec.sc25519_reduce(s) -// console.log('sc_red:', s.subarray(0, 32).toString('hex')) +ec.sc25519_reduce(s) +console.log('sc_red:', s.subarray(0, 32).toString('hex')) -// ec.sc25519_invert(res, cn) -// console.log('sc_inv:', res.toString('hex')) +ec.sc25519_invert(res, cn) +console.log('sc_inv:', res.toString('hex')) -// ec.ge25519_mont_to_ed(g, c, a, b) -// ec.fe25519_tobytes(res, g) -// console.log('g_m2ex:', res.toString('hex')) -// ec.fe25519_tobytes(res, c) -// console.log('g_m2ey:', res.toString('hex')) +ec.ge25519_mont_to_ed(g, c, a, b) +ec.fe25519_tobytes(res, g) +console.log('g_m2ex:', res.toString('hex')) +ec.fe25519_tobytes(res, c) +console.log('g_m2ey:', res.toString('hex')) -// ec.ge25519_frombytes(ge, p) -// ec.ge25519_p3_tobytes(res, ge) -// console.log("p :", res.toString('hex')) +ec.ge25519_frombytes(ge, p) +ec.ge25519_p3_tobytes(res, ge) +console.log("p :", res.toString('hex')) -// ec.ge25519_mul_l(gf, ge) -// ec.ge25519_p3_tobytes(res, gf) -// console.log("mul_l :", res.toString('hex')) +ec.ge25519_mul_l(gf, ge) +ec.ge25519_p3_tobytes(res, gf) +console.log("mul_l :", res.toString('hex')) -// ec.ge25519_scalarmult_base(gf, cn) -// ec.ge25519_p3_tobytes(res, gf) -// console.log("smultb:", res.toString('hex')) +ec.ge25519_scalarmult_base(gf, cn) +ec.ge25519_p3_tobytes(res, gf) +console.log("smultb:", res.toString('hex')) -// ec.ge25519_scalarmult(ge, bn, gf) -// ec.ge25519_p3_tobytes(res, ge) -// console.log("smult :", res.toString('hex')) +ec.ge25519_scalarmult(ge, bn, gf) +ec.ge25519_p3_tobytes(res, ge) +console.log("smult :", res.toString('hex')) -// ec.ge25519_double_scalarmult_vartime(gf, an, ge, bn) -// ec.ge25519_p3_tobytes(res, gf) -// console.log("smdbl :", res.toString('hex')) +ec.ge25519_double_scalarmult_vartime(gf, an, ge, bn) +ec.ge25519_p3_tobytes(res, gf) +console.log("smdbl :", res.toString('hex')) -// ec.ge25519_frombytes_negate_vartime(gf, pk_test) -// ec.ge25519_p3_tobytes(res, gf) -// console.log("smdbl :", res.toString('hex')) +ec.ge25519_frombytes_negate_vartime(gf, pk_test) +ec.ge25519_p3_tobytes(res, gf) +console.log("smdbl :", res.toString('hex')) -// console.log('canon :', ec.sc25519_is_canonical(bn)) +console.log('canon :', ec.sc25519_is_canonical(bn)) ///////////////////////////////////////////////////// diff --git a/fe25519_25.js b/fe25519_25.js index 6a0c5a5..0e167d9 100644 --- a/fe25519_25.js +++ b/fe25519_25.js @@ -1,43 +1,54 @@ const assert = require('nanoassert') const sodium = require('./') -const wasm = require('./fe25519_25/mult.js')({ + +const debug = { + log (...args) { + console.log(...args.map(int => (int >>> 0).toString(16).padStart(8, '0'))) + }, + log_tee (arg) { + console.log((arg >>> 0).toString(16).padStart(8, '0')) + return arg + } +} + +const wasm_mul = require('./fe25519_25/fe25519_mul')({ + imports: { debug } +}) +const wasm_sq = require('./fe25519_25/fe25519_sq')({ + imports: { debug } +}) +const wasm_invert = require('./fe25519_25/fe25519_invert')({ + imports: { debug } +}) +const wasm_pow = require('./fe25519_25/fe25519_pow22523')({ + imports: { debug } +}) + +const tbl = new WebAssembly.Table({ initial: 2, element: "anyfunc" }) + +const wasm_sc_red = require('./fe25519_25/sc_reduce')({ imports: { - debug: { - log (...args) { - console.log(...args.map(int => (int >>> 0).toString(16).padStart(8, '0'))) - }, - log_tee (arg) { - console.log((arg >>> 0).toString(16).padStart(8, '0')) - return arg - } + debug, + js: { + table: tbl } } }) -const wasm3 = require('./fe25519_25/fe25519_invert')({ +const wasm_sc_mul = require('./fe25519_25/sc25519_mul')({ imports: { - debug: { - log (...args) { - console.log(...args.map(int => (int >>> 0).toString(16).padStart(8, '0'))) - }, - log_tee (arg) { - console.log((arg >>> 0).toString(16).padStart(8, '0')) - return arg - } + debug, + js: { + table: tbl } } }) -const wasm2 = require('./fe25519_25/fe25519_pow22523')({ +const wasm_sc_muladd = require('./fe25519_25/sc25519_muladd')({ imports: { - debug: { - log (...args) { - console.log(...args.map(int => (int >>> 0).toString(16).padStart(8, '0'))) - }, - log_tee (arg) { - console.log((arg >>> 0).toString(16).padStart(8, '0')) - return arg - } + debug, + js: { + table: tbl } } }) @@ -45,10 +56,10 @@ const wasm2 = require('./fe25519_25/fe25519_pow22523')({ function fe25519_invert (h, f) { var buf = Buffer.from(f.buffer) - wasm3.memory.set(buf) - wasm3.exports.fe25519_invert(40, 0) + wasm_invert.memory.set(buf) + wasm_invert.exports.fe25519_invert(40, 0) - buf = Buffer.from(wasm3.memory.slice(40, 80)) + buf = Buffer.from(wasm_invert.memory.slice(40, 80)) for (let i = 0; i < 10; i++) { h[i] = buf.readUInt32LE(4 * i) } @@ -57,16 +68,15 @@ function fe25519_invert (h, f) { function fe25519_pow22523 (h, f) { var buf = Buffer.from(f.buffer) - wasm2.memory.set(buf) - wasm2.exports.fe25519_pow22523(40, 0) + wasm_pow.memory.set(buf) + wasm_pow.exports.fe25519_pow22523(40, 0) - buf = Buffer.from(wasm2.memory.slice(40, 80)) + buf = Buffer.from(wasm_pow.memory.slice(40, 80)) for (let i = 0; i < 10; i++) { h[i] = buf.readUInt32LE(4 * i) } } -console.log(wasm.buffer.byteLength) const base = require('./fe25519_25/base.json').map(a => a.map(b => ge2(b))) const printbuf =Buffer.alloc(32) @@ -147,14 +157,11 @@ const ed25519_sqrtam2 = fe25519([ ]) function print_ge (g, n = 4) { - console.log('__________\n') for (let i = 0; i < n; i++) for (let j = 0; j <10; j++) console.log(`g[${i}][${j}]:`, signedInt(g[i][j]).toString(16).padStart(8, '0')) } function print_fe (f) { for (let j = 0; j <10; j++) console.log(`f[${j}]:`, signedInt(f[j]).toString(16).padStart(8, '0')) - console.log('__________\n') - console.log('__________\n') } function fe25519 (arr) { @@ -774,11 +781,11 @@ function fe25519_mul (h, f, g) { var fbuf = Buffer.from(f.buffer) var gbuf = Buffer.from(g.buffer) - wasm.memory.set(fbuf) - wasm.memory.set(gbuf, 40) - wasm.exports.fe25519_mul(80, 0, 40) + wasm_mul.memory.set(fbuf) + wasm_mul.memory.set(gbuf, 40) + wasm_mul.exports.fe25519_mul(80, 0, 40) - buf = Buffer.from(wasm.memory.slice(80, 120)) + buf = Buffer.from(wasm_mul.memory.slice(80, 120)) for (let i = 0; i < 10; i++) { h[i] = buf.readUInt32LE(4 * i) } @@ -801,10 +808,10 @@ function fe25519_sq (h, f, log) { var buf = Buffer.from(f.buffer) - wasm.memory.set(buf) - wasm.exports.fe25519_sq(40, 0, 0) + wasm_sq.memory.set(buf) + wasm_sq.exports.sq(40, 0, 0) - buf = Buffer.from(wasm.memory.slice(40, 80)) + buf = Buffer.from(wasm_sq.memory.slice(40, 80)) for (let i = 0; i < 10; i++) { h[i] = buf.readUInt32LE(4 * i) } @@ -827,10 +834,10 @@ function fe25519_sq2 (h, f) { var buf = Buffer.from(f.buffer) - wasm.memory.set(buf) - wasm.exports.fe25519_sq(40, 0, 1) + wasm_sq.memory.set(buf) + wasm_sq.exports.sq(40, 0, 1) - buf = Buffer.from(wasm.memory.slice(40, 80)) + buf = Buffer.from(wasm_sq.memory.slice(40, 80)) for (let i = 0; i < 10; i++) { h[i] = buf.readUInt32LE(4 * i) } @@ -2093,12 +2100,12 @@ function sc25519_mul (s, a, b) { const abuf = new Uint8Array(_a.buffer) const bbuf = new Uint8Array(_b.buffer) - wasm.memory.set(abuf, 0) - wasm.memory.set(bbuf, 48) + wasm_sc_mul.memory.set(abuf, 0) + wasm_sc_mul.memory.set(bbuf, 48) - wasm.exports.sc25519_mul(96, 0, 48) + wasm_sc_mul.exports.sc25519_mul(96, 0, 48) - s.set(wasm.memory.slice(96, 128)) + s.set(wasm_sc_mul.memory.slice(96, 128)) } function sc25519_muladd (s, a, b, c) { @@ -2154,13 +2161,13 @@ function sc25519_muladd (s, a, b, c) { const bbuf = new Uint8Array(_b.buffer) const cbuf = new Uint8Array(_c.buffer) - wasm.memory.set(abuf, 0) - wasm.memory.set(bbuf, 48) - wasm.memory.set(cbuf, 96) + wasm_sc_muladd.memory.set(abuf, 0) + wasm_sc_muladd.memory.set(bbuf, 48) + wasm_sc_muladd.memory.set(cbuf, 96) - wasm.exports.sc25519_muladd(144, 0, 48, 96) + wasm_sc_muladd.exports.sc25519_muladd(144, 0, 48, 96) - s.set(wasm.memory.slice(144, 176)) + s.set(wasm_sc_muladd.memory.slice(144, 176)) } /* @@ -2303,12 +2310,12 @@ function sc25519_reduce (s) { _s[23] = load_4(s, 60) >>> 3 var sbuf = Buffer.from(_s.buffer) - wasm.memory.set(sbuf, 0) + wasm_sc_red.memory.set(sbuf, 0) - wasm.exports.sc25519_reduce(0) + wasm_sc_red.exports.sc25519_reduce(0) s.fill(0) - s.set(wasm.memory.slice(0, 32)) + s.set(wasm_sc_red.memory.slice(0, 32)) } function sc25519_is_canonical (s) { diff --git a/fe25519_25/fe25519_invert.js b/fe25519_25/fe25519_invert.js new file mode 100644 index 0000000..7da55b9 --- /dev/null +++ b/fe25519_25/fe25519_invert.js @@ -0,0 +1,61 @@ + +module.exports = loadWebAssembly + +loadWebAssembly.supported = typeof WebAssembly !== 'undefined' + +function loadWebAssembly (opts) { + if (!loadWebAssembly.supported) return null + + var imp = opts && opts.imports + var wasm = toUint8Array('AGFzbQEAAAABQQpgAX8AYAF/AX9gAn9/AGABfQBgAX0BfWABfABgAXwBfGABfgBgAX4BfmAUfn5+fn5+fn5+fn5+fn5+fn5+fn4AAmEHBWRlYnVnA2xvZwAABWRlYnVnB2xvZ190ZWUAAQVkZWJ1ZwNsb2cAAgVkZWJ1ZwNsb2cAAwVkZWJ1Zwdsb2dfdGVlAAQFZGVidWcDbG9nAAUFZGVidWcHbG9nX3RlZQAGAwUEBwgJAgUDAQABBjMKfgFCAAt+AUIAC34BQgALfgFCAAt+AUIAC34BQgALfgFCAAt+AUIAC34BQgALfgFCAAsHGwIGbWVtb3J5AgAOZmUyNTUxOV9pbnZlcnQACgrDHQQNACAAQiCHpyAApxACCwkAIAAQByAADwuzDwF8fiAAp6whACABp6whASACp6whAiADp6whAyAEp6whBCAFp6whBSAGp6whBiAHp6whByAIp6whCCAJp6whCSAKp6whCiALp6whCyAMp6whDCANp6whDSAOp6whDiAPp6whDyAQp6whECARp6whESASp6whEiATp6whE0ITIAt+IAtCgICAgAiDQgGGQhN+fSEeQhMgDH4gDEKAgICACINCAYZCE359IR9CEyANfiANQoCAgIAIg0IBhkITfn0hIEITIA5+IA5CgICAgAiDQgGGQhN+fSEhQhMgD34gD0KAgICACINCAYZCE359ISJCEyAQfiAQQoCAgIAIg0IBhkITfn0hI0ITIBF+IBFCgICAgAiDQgGGQhN+fSEkQhMgEn4gEkKAgICACINCAYZCE359ISVCEyATfiATQoCAgIAIg0IBhkITfn0hJkICIAF+IAFCgICAgAiDQgGGQgJ+fSEnQgIgA34gA0KAgICACINCAYZCAn59IShCAiAFfiAFQoCAgIAIg0IBhkICfn0hKUICIAd+IAdCgICAgAiDQgGGQgJ+fSEqQgIgCX4gCUKAgICACINCAYZCAn59ISsgHqesIR4gH6esIR8gIKesISAgIaesISEgIqesISIgI6esISMgJKesISQgJaesISUgJqesISYgJ6esIScgKKesISggKaesISkgKqesISogK6esISsgACAKfiEsIAAgC34hLSAAIAx+IS4gACANfiEvIAAgDn4hMCAAIA9+ITEgACAQfiEyIAAgEX4hMyAAIBJ+ITQgACATfiE1IAEgCn4hNiAnIAt+ITcgASAMfiE4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BIAxBAWohDCMAIwEjAiMDIwQjBSMGIwcjCCMJIwAjASMCIwMjBCMFIwYjByMIIwkQCQwACwtBACEMIA4gESAUIBcgGiAdICAgIyAmICkjACMBIwIjAyMEIwUjBiMHIwgjCRAJIwAhDiMBIREjAiEUIwMhFyMEIRojBSEdIwYhICMHISMjCCEmIwkhKQJAA0BBMiAMRg0BIAxBAWohDCMAIwEjAiMDIwQjBSMGIwcjCCMJIwAjASMCIwMjBCMFIwYjByMIIwkQCQwACwtBACEMIA4gESAUIBcgGiAdICAgIyAmICkjACMBIwIjAyMEIwUjBiMHIwgjCRAJIwAhDyMBIRIjAiEVIwMhGCMEIRsjBSEeIwYhISMHISQjCCEnIwkhKgJAA0BB5AAgDEYNASAMQQFqIQwjACMBIwIjAyMEIwUjBiMHIwgjCSMAIwEjAiMDIwQjBSMGIwcjCCMJEAkMAAsLQQAhDCAPIBIgFSAYIBsgHiAhICQgJyAqIwAjASMCIwMjBCMFIwYjByMIIwkQCQJAA0BBMiAMRg0BIAxBAWohDCMAIwEjAiMDIwQjBSMGIwcjCCMJIwAjASMCIwMjBCMFIwYjByMIIwkQCQwACwtBACEMIA4gESAUIBcgGiAdICAgIyAmICkjACMBIwIjAyMEIwUjBiMHIwgjCRAJAkADQEEFIAxGDQEgDEEBaiEMIwAjASMCIwMjBCMFIwYjByMIIwkjACMBIwIjAyMEIwUjBiMHIwgjCRAJDAALCyANIBAgEyAWIBkgHCAfICIgJSAoIwAjASMCIwMjBCMFIwYjByMIIwkQCSAAIwA+AgAgACMBPgIEIAAjAj4CCCAAIwM+AgwgACMEPgIQIAAjBT4CFCAAIwY+AhggACMHPgIcIAAjCD4CICAAIwk+AiQL') + var ready = null + + var mod = { + buffer: wasm, + memory: null, + exports: null, + realloc: realloc, + onload: onload + } + + onload(function () {}) + + return mod + + function realloc (size) { + mod.exports.memory.grow(Math.max(0, Math.ceil(Math.abs(size - mod.memory.length) / 65536))) + mod.memory = new Uint8Array(mod.exports.memory.buffer) + } + + function onload (cb) { + if (mod.exports) return cb() + + if (ready) { + ready.then(cb.bind(null, null)).catch(cb) + return + } + + try { + if (opts && opts.async) throw new Error('async') + setup({instance: new WebAssembly.Instance(new WebAssembly.Module(wasm), imp)}) + } catch (err) { + ready = WebAssembly.instantiate(wasm, imp).then(setup) + } + + onload(cb) + } + + function setup (w) { + mod.exports = w.instance.exports + mod.memory = mod.exports.memory && mod.exports.memory.buffer && new Uint8Array(mod.exports.memory.buffer) + } +} + +function toUint8Array (s) { + if (typeof atob === 'function') return new Uint8Array(atob(s).split('').map(charCodeAt)) + return (require('buf' + 'fer').Buffer).from(s, 'base64') +} + +function charCodeAt (c) { + return c.charCodeAt(0) +} diff --git a/fe25519_25/fe25519_mul.js b/fe25519_25/fe25519_mul.js new file mode 100644 index 0000000..f53b10c --- /dev/null +++ b/fe25519_25/fe25519_mul.js @@ -0,0 +1,62 @@ + +module.exports = loadWebAssembly + +loadWebAssembly.supported = typeof WebAssembly !== 'undefined' + +function loadWebAssembly (opts) { + if (!loadWebAssembly.supported) return null + + var imp = opts && opts.imports +console.log(imp) + var wasm = toUint8Array('AGFzbQEAAAABMApgAX8AYAF/AX9gAn9/AGABfQBgAX0BfWABfABgAXwBfGABfgBgAX4BfmADf39/AAJhBwVkZWJ1ZwNsb2cAAAVkZWJ1Zwdsb2dfdGVlAAEFZGVidWcDbG9nAAIFZGVidWcDbG9nAAMFZGVidWcHbG9nX3RlZQAEBWRlYnVnA2xvZwAFBWRlYnVnB2xvZ190ZWUABgMEAwcICQUDAQABBxgCBm1lbW9yeQIAC2ZlMjU1MTlfbXVsAAkKuxEDDQAgAEIgh6cgAKcQAgsJACAAEAcgAA8LoBEBmgF+IAE1AgAhAyABNQIEIQQgATUCCCEFIAE1AgwhBiABNQIQIQcgATUCFCEIIAE1AhghCSABNQIcIQogATUCICELIAE1AiQhDCACNQIAIQ0gAjUCBCEOIAI1AgghDyACNQIMIRAgAjUCECERIAI1AhQhEiACNQIYIRMgAjUCHCEUIAI1Ai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+fSEXIBtCAUIZhnxCGochJSAcICV8IRwgGyAlQgFCGoZ+fSEbIBhCAUIYhnxCGYchIiAZICJ8IRkgGCAiQgFCGYZ+fSEYIBxCAUIYhnxCGYchJiAdICZ8IR0gHCAmQgFCGYZ+fSEcIBlCAUIZhnxCGochIyAaICN8IRogGSAjQgFCGoZ+fSEZIB1CAUIZhnxCGochJyAeICd8IR4gHSAnQgFCGoZ+fSEdIBpCAUIYhnxCGYchJCAbICR8IRsgGiAkQgFCGYZ+fSEaIB5CAUIYhnxCGYchKCAfICh8IR8gHiAoQgFCGYZ+fSEeIBtCAUIZhnxCGochJSAcICV8IRwgGyAlQgFCGoZ+fSEbIB9CAUIZhnxCGochKSAgICl8ISAgHyApQgFCGoZ+fSEfICBCAUIYhnxCGYchKiAXICpCE358IRcgICAqQgFCGYZ+fSEgIBdCAUIZhnxCGochISAYICF8IRggFyAhQgFCGoZ+fSEXIAAgFz4CACAAIBg+AgQgACAZPgIIIAAgGj4CDCAAIBs+AhAgACAcPgIUIAAgHT4CGCAAIB4+AhwgACAfPgIgIAAgID4CJAs=') + var ready = null + + var mod = { + buffer: wasm, + memory: null, + exports: null, + realloc: realloc, + onload: onload + } + + onload(function () {}) + + return mod + + function realloc (size) { + mod.exports.memory.grow(Math.max(0, Math.ceil(Math.abs(size - mod.memory.length) / 65536))) + mod.memory = new Uint8Array(mod.exports.memory.buffer) + } + + function onload (cb) { + if (mod.exports) return cb() + + if (ready) { + ready.then(cb.bind(null, null)).catch(cb) + return + } + + try { + if (opts && opts.async) throw new Error('async') + setup({instance: new WebAssembly.Instance(new WebAssembly.Module(wasm), imp)}) + } catch (err) { + ready = WebAssembly.instantiate(wasm, imp).then(setup) + } + + onload(cb) + } + + function setup (w) { + mod.exports = w.instance.exports + mod.memory = mod.exports.memory && mod.exports.memory.buffer && new Uint8Array(mod.exports.memory.buffer) + } +} + +function toUint8Array (s) { + if (typeof atob === 'function') return new Uint8Array(atob(s).split('').map(charCodeAt)) + return (require('buf' + 'fer').Buffer).from(s, 'base64') +} + +function charCodeAt (c) { + return c.charCodeAt(0) +} diff --git a/fe25519_25/fe25519_mul.wat b/fe25519_25/fe25519_mul.wat index 7638c38..998d4ba 100644 --- a/fe25519_25/fe25519_mul.wat +++ b/fe25519_25/fe25519_mul.wat @@ -213,27 +213,27 @@ (set_local $g8 (i64.load32_u offset=32 (get_local $g))) (set_local $g9 (i64.load32_u offset=36 (get_local $g))) - (set_local $f0 (i64.extend32_s (get_local $f0))) - (set_local $f1 (i64.extend32_s (get_local $f1))) - (set_local $f2 (i64.extend32_s (get_local $f2))) - (set_local $f3 (i64.extend32_s (get_local $f3))) - (set_local $f4 (i64.extend32_s (get_local $f4))) - (set_local $f5 (i64.extend32_s (get_local $f5))) - (set_local $f6 (i64.extend32_s (get_local $f6))) - (set_local $f7 (i64.extend32_s (get_local $f7))) - (set_local $f8 (i64.extend32_s (get_local $f8))) - (set_local $f9 (i64.extend32_s (get_local $f9))) + (set_local $f0 (i64.extend_s/i32 (i32.wrap/i64 (get_local $f0)))) + (set_local $f1 (i64.extend_s/i32 (i32.wrap/i64 (get_local $f1)))) + (set_local $f2 (i64.extend_s/i32 (i32.wrap/i64 (get_local $f2)))) + (set_local $f3 (i64.extend_s/i32 (i32.wrap/i64 (get_local $f3)))) + (set_local $f4 (i64.extend_s/i32 (i32.wrap/i64 (get_local $f4)))) + (set_local $f5 (i64.extend_s/i32 (i32.wrap/i64 (get_local $f5)))) + (set_local $f6 (i64.extend_s/i32 (i32.wrap/i64 (get_local $f6)))) + (set_local $f7 (i64.extend_s/i32 (i32.wrap/i64 (get_local $f7)))) + (set_local $f8 (i64.extend_s/i32 (i32.wrap/i64 (get_local $f8)))) + (set_local $f9 (i64.extend_s/i32 (i32.wrap/i64 (get_local $f9)))) - (set_local $g0 (i64.extend32_s (get_local $g0))) - (set_local $g1 (i64.extend32_s (get_local $g1))) - (set_local $g2 (i64.extend32_s (get_local $g2))) - (set_local $g3 (i64.extend32_s (get_local $g3))) - (set_local $g4 (i64.extend32_s (get_local $g4))) - (set_local $g5 (i64.extend32_s (get_local $g5))) - (set_local $g6 (i64.extend32_s (get_local $g6))) - (set_local $g7 (i64.extend32_s (get_local $g7))) - (set_local $g8 (i64.extend32_s (get_local $g8))) - (set_local $g9 (i64.extend32_s (get_local $g9))) + (set_local $g0 (i64.extend_s/i32 (i32.wrap/i64 (get_local $g0)))) + (set_local $g1 (i64.extend_s/i32 (i32.wrap/i64 (get_local $g1)))) + (set_local $g2 (i64.extend_s/i32 (i32.wrap/i64 (get_local $g2)))) + (set_local $g3 (i64.extend_s/i32 (i32.wrap/i64 (get_local $g3)))) + (set_local $g4 (i64.extend_s/i32 (i32.wrap/i64 (get_local $g4)))) + (set_local $g5 (i64.extend_s/i32 (i32.wrap/i64 (get_local $g5)))) + (set_local $g6 (i64.extend_s/i32 (i32.wrap/i64 (get_local $g6)))) + (set_local $g7 (i64.extend_s/i32 (i32.wrap/i64 (get_local $g7)))) + (set_local $g8 (i64.extend_s/i32 (i32.wrap/i64 (get_local $g8)))) + (set_local $g9 (i64.extend_s/i32 (i32.wrap/i64 (get_local $g9)))) (set_local $g1_19 (i64.sub (i64.mul (i64.const 19) (get_local $g1)) (i64.mul (i64.shl (i64.and (get_local $g1) (i64.const 0x80000000)) (i64.const 1)) (i64.const 19)))) (set_local $g2_19 (i64.sub (i64.mul (i64.const 19) (get_local $g2)) (i64.mul (i64.shl (i64.and (get_local $g2) (i64.const 0x80000000)) (i64.const 1)) (i64.const 19)))) @@ -251,21 +251,21 @@ (set_local $f7_2 (i64.sub (i64.mul (i64.const 2) (get_local $f7)) (i64.mul (i64.shl (i64.and (get_local $f7) (i64.const 0x80000000)) (i64.const 1)) (i64.const 2)))) (set_local $f9_2 (i64.sub (i64.mul (i64.const 2) (get_local $f9)) (i64.mul (i64.shl (i64.and (get_local $f9) (i64.const 0x80000000)) (i64.const 1)) (i64.const 2)))) - (set_local $g1_19 (i64.extend32_s (get_local $g1_19))) - (set_local $g2_19 (i64.extend32_s (get_local $g2_19))) - (set_local $g3_19 (i64.extend32_s (get_local $g3_19))) - (set_local $g4_19 (i64.extend32_s (get_local $g4_19))) - (set_local $g5_19 (i64.extend32_s (get_local $g5_19))) - (set_local $g6_19 (i64.extend32_s (get_local $g6_19))) - (set_local $g7_19 (i64.extend32_s (get_local $g7_19))) - (set_local $g8_19 (i64.extend32_s (get_local $g8_19))) - (set_local $g9_19 (i64.extend32_s (get_local $g9_19))) + (set_local $g1_19 (i64.extend_s/i32 (i32.wrap/i64 (get_local $g1_19)))) + (set_local $g2_19 (i64.extend_s/i32 (i32.wrap/i64 (get_local $g2_19)))) + (set_local $g3_19 (i64.extend_s/i32 (i32.wrap/i64 (get_local $g3_19)))) + (set_local $g4_19 (i64.extend_s/i32 (i32.wrap/i64 (get_local $g4_19)))) + (set_local $g5_19 (i64.extend_s/i32 (i32.wrap/i64 (get_local $g5_19)))) + (set_local $g6_19 (i64.extend_s/i32 (i32.wrap/i64 (get_local $g6_19)))) + (set_local $g7_19 (i64.extend_s/i32 (i32.wrap/i64 (get_local $g7_19)))) + (set_local $g8_19 (i64.extend_s/i32 (i32.wrap/i64 (get_local $g8_19)))) + (set_local $g9_19 (i64.extend_s/i32 (i32.wrap/i64 (get_local $g9_19)))) - (set_local $f1_2 (i64.extend32_s (get_local $f1_2))) - (set_local $f3_2 (i64.extend32_s (get_local $f3_2))) - (set_local $f5_2 (i64.extend32_s (get_local $f5_2))) - (set_local $f7_2 (i64.extend32_s (get_local $f7_2))) - (set_local $f9_2 (i64.extend32_s (get_local $f9_2))) + (set_local $f1_2 (i64.extend_s/i32 (i32.wrap/i64 (get_local $f1_2)))) + (set_local $f3_2 (i64.extend_s/i32 (i32.wrap/i64 (get_local $f3_2)))) + (set_local $f5_2 (i64.extend_s/i32 (i32.wrap/i64 (get_local $f5_2)))) + (set_local $f7_2 (i64.extend_s/i32 (i32.wrap/i64 (get_local $f7_2)))) + (set_local $f9_2 (i64.extend_s/i32 (i32.wrap/i64 (get_local $f9_2)))) (set_local $f0g0 (i64.mul (get_local $f0 ) (get_local $g0))) (set_local $f0g1 (i64.mul (get_local $f0 ) (get_local $g1))) diff --git a/fe25519_25/fe25519_pow22523.js b/fe25519_25/fe25519_pow22523.js new file mode 100644 index 0000000..17a08cd --- /dev/null +++ b/fe25519_25/fe25519_pow22523.js @@ -0,0 +1,61 @@ + +module.exports = loadWebAssembly + +loadWebAssembly.supported = typeof WebAssembly !== 'undefined' + +function loadWebAssembly (opts) { + if (!loadWebAssembly.supported) return null + + var imp = opts && opts.imports + var wasm = toUint8Array('AGFzbQEAAAABQQpgAX8AYAF/AX9gAn9/AGABfQBgAX0BfWABfABgAXwBfGABfgBgAX4BfmAUfn5+fn5+fn5+fn5+fn5+fn5+fn4AAmEHBWRlYnVnA2xvZwAABWRlYnVnB2xvZ190ZWUAAQVkZWJ1ZwNsb2cAAgVkZWJ1ZwNsb2cAAwVkZWJ1Zwdsb2dfdGVlAAQFZGVidWcDbG9nAAUFZGVidWcHbG9nX3RlZQAGAwUEBwgJAgUDAQABBjMKfgFCAAt+AUIAC34BQgALfgFCAAt+AUIAC34BQgALfgFCAAt+AUIAC34BQgALfgFCAAsHHQIGbWVtb3J5AgAQZmUyNTUxOV9wb3cyMjUyMwAKCocdBA0AIABCIIenIACnEAILCQAgABAHIAAPC7MPAXx+IACnrCEAIAGnrCEBIAKnrCECIAOnrCEDIASnrCEEIAWnrCEFIAanrCEGIAenrCEHIAinrCEIIAmnrCEJIAqnrCEKIAunrCELIAynrCEMIA2nrCENIA6nrCEOIA+nrCEPIBCnrCEQIBGnrCERIBKnrCESIBOnrCETQhMgC34gC0KAgICACINCAYZCE359IR5CEyAMfiAMQoCAgIAIg0IBhkITfn0hH0ITIA1+IA1CgICAgAiDQgGGQhN+fSEgQhMgDn4gDkKAgICACINCAYZCE359ISFCEyAPfiAPQoCAgIAIg0IBhkITfn0hIkITIBB+IBBCgICAgAiDQgGGQhN+fSEjQhMgEX4gEUKAgICACINCAYZCE359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wkjACMBIwIjAyMEIwUjBiMHIwgjCRAJIAIgAyAEIAUgBiAHIAggCSAKIAsjACMBIwIjAyMEIwUjBiMHIwgjCRAJIAAjAD4CACAAIwE+AgQgACMCPgIIIAAjAz4CDCAAIwQ+AhAgACMFPgIUIAAjBj4CGCAAIwc+AhwgACMIPgIgIAAjCT4CJAs=') + var ready = null + + var mod = { + buffer: wasm, + memory: null, + exports: null, + realloc: realloc, + onload: onload + } + + onload(function () {}) + + return mod + + function realloc (size) { + mod.exports.memory.grow(Math.max(0, Math.ceil(Math.abs(size - mod.memory.length) / 65536))) + mod.memory = new Uint8Array(mod.exports.memory.buffer) + } + + function onload (cb) { + if (mod.exports) return cb() + + if (ready) { + ready.then(cb.bind(null, null)).catch(cb) + return + } + + try { + if (opts && opts.async) throw new Error('async') + setup({instance: new WebAssembly.Instance(new WebAssembly.Module(wasm), imp)}) + } catch (err) { + ready = WebAssembly.instantiate(wasm, imp).then(setup) + } + + onload(cb) + } + + function setup (w) { + mod.exports = w.instance.exports + mod.memory = mod.exports.memory && mod.exports.memory.buffer && new Uint8Array(mod.exports.memory.buffer) + } +} + +function toUint8Array (s) { + if (typeof atob === 'function') return new Uint8Array(atob(s).split('').map(charCodeAt)) + return (require('buf' + 'fer').Buffer).from(s, 'base64') +} + +function charCodeAt (c) { + return c.charCodeAt(0) +} diff --git a/fe25519_25/fe25519_sq.js b/fe25519_25/fe25519_sq.js new file mode 100644 index 0000000..d93d91a --- /dev/null +++ b/fe25519_25/fe25519_sq.js @@ -0,0 +1,61 @@ + +module.exports = loadWebAssembly + +loadWebAssembly.supported = typeof WebAssembly !== 'undefined' + +function loadWebAssembly (opts) { + if (!loadWebAssembly.supported) return null + + var imp = opts && opts.imports + var wasm = toUint8Array('AGFzbQEAAAABCAFgBH9/f38AAwIBAAUDAQABBw8CBm1lbW9yeQIAAnNxAAAKzwoBzAoCAX9jfiABNQIAIQYgATUCBCEHIAE1AgghCCABNQIMIQkgATUCECEKIAE1AhQhCyABNQI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8IRYgFyAXfCEXIBggGHwhGCAZIBl8IRkLIBBCAUIZhnxCGochGiARIBp8IREgECAaQgFCGoZ+fSEQIBRCAUIZhnxCGochHiAVIB58IRUgFCAeQgFCGoZ+fSEUIBFCAUIYhnxCGYchGyASIBt8IRIgESAbQgFCGYZ+fSERIBVCAUIYhnxCGYchHyAWIB98IRYgFSAfQgFCGYZ+fSEVIBJCAUIZhnxCGochHCATIBx8IRMgEiAcQgFCGoZ+fSESIBZCAUIZhnxCGochICAXICB8IRcgFiAgQgFCGoZ+fSEWIBNCAUIYhnxCGYchHSAUIB18IRQgEyAdQgFCGYZ+fSETIBdCAUIYhnxCGYchISAYICF8IRggFyAhQgFCGYZ+fSEXIBRCAUIZhnxCGochHiAVIB58IRUgFCAeQgFCGoZ+fSEUIBhCAUIZhnxCGochIiAZICJ8IRkgGCAiQgFCGoZ+fSEYIBlCAUIYhnxCGYchIyAQICNCE358IRAgGSAjQgFCGYZ+fSEZIBBCAUIZhnxCGochGiARIBp8IREgECAaQgFCGoZ+fSEQIAAgED4CACAAIBE+AgQgACASPgIIIAAgEz4CDCAAIBQ+AhAgACAVPgIUIAAgFj4CGCAAIBc+AhwgACAYPgIgIAAgGT4CJAs=') + var ready = null + + var mod = { + buffer: wasm, + memory: null, + exports: null, + realloc: realloc, + onload: onload + } + + onload(function () {}) + + return mod + + function realloc (size) { + mod.exports.memory.grow(Math.max(0, Math.ceil(Math.abs(size - mod.memory.length) / 65536))) + mod.memory = new Uint8Array(mod.exports.memory.buffer) + } + + function onload (cb) { + if (mod.exports) return cb() + + if (ready) { + ready.then(cb.bind(null, null)).catch(cb) + return + } + + try { + if (opts && opts.async) throw new Error('async') + setup({instance: new WebAssembly.Instance(new WebAssembly.Module(wasm), imp)}) + } catch (err) { + ready = WebAssembly.instantiate(wasm, imp).then(setup) + } + + onload(cb) + } + + function setup (w) { + mod.exports = w.instance.exports + mod.memory = mod.exports.memory && mod.exports.memory.buffer && new Uint8Array(mod.exports.memory.buffer) + } +} + +function toUint8Array (s) { + if (typeof atob === 'function') return new Uint8Array(atob(s).split('').map(charCodeAt)) + return (require('buf' + 'fer').Buffer).from(s, 'base64') +} + +function charCodeAt (c) { + return c.charCodeAt(0) +} diff --git a/fe25519_25/fe25519_sq.wat b/fe25519_25/fe25519_sq.wat index 867c675..2e14cf0 100644 --- a/fe25519_25/fe25519_sq.wat +++ b/fe25519_25/fe25519_sq.wat @@ -3,6 +3,7 @@ (export "memory" (memory $0)) (func $sq (export "sq") (param $h i32) (param $f i32) (param $double i32) (param $repeat i32) + (local $count i32) (local $tmp i64) (local $f0 i64) @@ -122,16 +123,16 @@ (block $end (loop $again - (set_local $f0 (i64.extend32_s (get_local $f0))) - (set_local $f1 (i64.extend32_s (get_local $f1))) - (set_local $f2 (i64.extend32_s (get_local $f2))) - (set_local $f3 (i64.extend32_s (get_local $f3))) - (set_local $f4 (i64.extend32_s (get_local $f4))) - (set_local $f5 (i64.extend32_s (get_local $f5))) - (set_local $f6 (i64.extend32_s (get_local $f6))) - (set_local $f7 (i64.extend32_s (get_local $f7))) - (set_local $f8 (i64.extend32_s (get_local $f8))) - (set_local $f9 (i64.extend32_s (get_local $f9))) + (set_local $f0 (i64.extend_s/i32 (i32.wrap/i64 (get_local $f0)))) + (set_local $f1 (i64.extend_s/i32 (i32.wrap/i64 (get_local $f1)))) + (set_local $f2 (i64.extend_s/i32 (i32.wrap/i64 (get_local $f2)))) + (set_local $f3 (i64.extend_s/i32 (i32.wrap/i64 (get_local $f3)))) + (set_local $f4 (i64.extend_s/i32 (i32.wrap/i64 (get_local $f4)))) + (set_local $f5 (i64.extend_s/i32 (i32.wrap/i64 (get_local $f5)))) + (set_local $f6 (i64.extend_s/i32 (i32.wrap/i64 (get_local $f6)))) + (set_local $f7 (i64.extend_s/i32 (i32.wrap/i64 (get_local $f7)))) + (set_local $f8 (i64.extend_s/i32 (i32.wrap/i64 (get_local $f8)))) + (set_local $f9 (i64.extend_s/i32 (i32.wrap/i64 (get_local $f9)))) (set_local $f0_2 (i64.mul (i64.const 2 (get_local $f0)))) (set_local $f1_2 (i64.mul (i64.const 2 (get_local $f1)))) diff --git a/fe25519_25/sc25519_mul.js b/fe25519_25/sc25519_mul.js new file mode 100644 index 0000000..81a0206 --- /dev/null +++ b/fe25519_25/sc25519_mul.js @@ -0,0 +1,61 @@ + +module.exports = loadWebAssembly + +loadWebAssembly.supported = typeof WebAssembly !== 'undefined' + +function loadWebAssembly (opts) { + if (!loadWebAssembly.supported) return null + + var imp = opts && opts.imports + var wasm = toUint8Array('AGFzbQEAAAABIwJgGX9+fn5+fn5+fn5+fn5+fn5+fn5+fn5+fn4AYAN/f38AAg4BAmpzBXRhYmxlAXAAAQMCAQEFAwEAAQcYAgZtZW1vcnkCAAtzYzI1NTE5X211bAAACugPAeUPAUh+IAE1AgAhBCABNQIEIQUgATUCCCEGIAE1AgwhByABNQIQIQggATUCFCEJIAE1AhghCiABNQIcIQsgATUCICEMIAE1AiQhDSABNQIoIQ4gATUCLCEPIAI1AgAhECACNQIEIREgAjUCCCESIAI1AgwhEyACNQIQIRQgAjUCFCEVIAI1AhghFiACNQIcIRcgAjUCICEYIAI1AiQhGSACNQIoIRogAjUCLCEbIASnrCEEIAWnrCEFIAanrCEGIAenrCEHIAinrCEIIAmnrCEJIAqnrCEKIAunrCELIAynrCEMIA2nrCENIA6nrCEOIA+nrCEPIBCnrCEQIBGnrCERIBKnrCESIBOnrCETIBSnrCEUIBWnrCEVIBanrCEWIBenrCEXIBinrCEYIBmnrCEZIBqnrCEaIBunrCEbIAQgEH4hHCAEIBF+IAUgEH58IR0gBCASfiAFIBF+IAYgEH58fCEeIAQgE34gBSASfiAGIBF+IAcgEH58fHwhHyAEIBR+IAUgE34gBiASfiAHIBF+IAggEH58fHx8ISAgBCAVfiAFIBR+IAYgE34gByASfiAIIBF+IAkgEH58fHx8fCEhIAQgFn4gBSAVfiAGIBR+IAcgE34gCCASfiAJIBF+IAogEH58fHx8fHwhIiAEIBd+IAUgFn4gBi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CAUIUhnxCFYchOiAjIDp8ISMgIiA6QgFCFYZ+fSEiICRCAUIUhnxCFYchPCAlIDx8ISUgJCA8QgFCFYZ+fSEkICZCAUIUhnxCFYchPiAnID58IScgJiA+QgFCFYZ+fSEmIChCAUIUhnxCFYchQCApIEB8ISkgKCBAQgFCFYZ+fSEoICpCAUIUhnxCFYchQiArIEJ8ISsgKiBCQgFCFYZ+fSEqICxCAUIUhnxCFYchRCAtIER8IS0gLCBEQgFCFYZ+fSEsIC5CAUIUhnxCFYchRiAvIEZ8IS8gLiBGQgFCFYZ+fSEuIDBCAUIUhnxCFYchSCAxIEh8ITEgMCBIQgFCFYZ+fSEwIDJCAUIUhnxCFYchSiAzIEp8ITMgMiBKQgFCFYZ+fSEyIB1CAUIUhnxCFYchNSAeIDV8IR4gHSA1QgFCFYZ+fSEdIB9CAUIUhnxCFYchNyAgIDd8ISAgHyA3QgFCFYZ+fSEfICFCAUIUhnxCFYchOSAiIDl8ISIgISA5QgFCFYZ+fSEhICNCAUIUhnxCFYchOyAkIDt8ISQgIyA7QgFCFYZ+fSEjICVCAUIUhnxCFYchPSAmID18ISYgJSA9QgFCFYZ+fSElICdCAUIUhnxCFYchPyAoID98ISggJyA/QgFCFYZ+fSEnIClCAUIUhnxCFYchQSAqIEF8ISogKSBBQgFCFYZ+fSEpICtCAUIUhnxCFYchQyAsIEN8ISwgKyBDQgFCFYZ+fSErIC1CAUIUhnxCFYchRSAuIEV8IS4gLSBFQgFCFYZ+fSEtIC9CAUIUhnxCFYchRyAwIEd8ITAgLyBHQgFCFYZ+fSEvIDFCAUIUhnxCFYchSSAyIEl8ITIgMSBJQgFCFYZ+fSExIAAgHCAdIB4gHyAgICEgIiAjICQgJSAmICcgKCApICogKyAsIC0gLiAvIDAgMSAyIDNBABEAAAs=') + var ready = null + + var mod = { + buffer: wasm, + memory: null, + exports: null, + realloc: realloc, + onload: onload + } + + onload(function () {}) + + return mod + + function realloc (size) { + mod.exports.memory.grow(Math.max(0, Math.ceil(Math.abs(size - mod.memory.length) / 65536))) + mod.memory = new Uint8Array(mod.exports.memory.buffer) + } + + function onload (cb) { + if (mod.exports) return cb() + + if (ready) { + ready.then(cb.bind(null, null)).catch(cb) + return + } + + try { + if (opts && opts.async) throw new Error('async') + setup({instance: new WebAssembly.Instance(new WebAssembly.Module(wasm), imp)}) + } catch (err) { + ready = WebAssembly.instantiate(wasm, imp).then(setup) + } + + onload(cb) + } + + function setup (w) { + mod.exports = w.instance.exports + mod.memory = mod.exports.memory && mod.exports.memory.buffer && new Uint8Array(mod.exports.memory.buffer) + } +} + +function toUint8Array (s) { + if (typeof atob === 'function') return new Uint8Array(atob(s).split('').map(charCodeAt)) + return (require('buf' + 'fer').Buffer).from(s, 'base64') +} + +function charCodeAt (c) { + return c.charCodeAt(0) +} diff --git a/fe25519_25/sc25519_mul.wat b/fe25519_25/sc25519_mul.wat index 7a9f2c7..29ced76 100644 --- a/fe25519_25/sc25519_mul.wat +++ b/fe25519_25/sc25519_mul.wat @@ -1,4 +1,32 @@ (module + (import "js" "table" (table 1 anyfunc)) + (type $to_void (func + (param $s i32) + (param $s0 i64) + (param $s1 i64) + (param $s2 i64) + (param $s3 i64) + (param $s4 i64) + (param $s5 i64) + (param $s6 i64) + (param $s7 i64) + (param $s8 i64) + (param $s9 i64) + (param $s10 i64) + (param $s11 i64) + (param $s12 i64) + (param $s13 i64) + (param $s14 i64) + (param $s15 i64) + (param $s16 i64) + (param $s17 i64) + (param $s18 i64) + (param $s19 i64) + (param $s20 i64) + (param $s21 i64) + (param $s22 i64) + (param $s23 i64))) + (memory $0 1) (export "memory" (memory $0)) @@ -106,31 +134,31 @@ (set_local $b10 (i64.load32_u offset=40 (get_local $b))) (set_local $b11 (i64.load32_u offset=44 (get_local $b))) - (set_local $a0 (i64.extend_s/i32 (get_local $a0))) - (set_local $a1 (i64.extend_s/i32 (get_local $a1))) - (set_local $a2 (i64.extend_s/i32 (get_local $a2))) - (set_local $a3 (i64.extend_s/i32 (get_local $a3))) - (set_local $a4 (i64.extend_s/i32 (get_local $a4))) - (set_local $a5 (i64.extend_s/i32 (get_local $a5))) - (set_local $a6 (i64.extend_s/i32 (get_local $a6))) - (set_local $a7 (i64.extend_s/i32 (get_local $a7))) - (set_local $a8 (i64.extend_s/i32 (get_local $a8))) - (set_local $a9 (i64.extend_s/i32 (get_local $a9))) - (set_local $a10 (i64.extend_s/i32 (get_local $a10))) - (set_local $a11 (i64.extend_s/i32 (get_local $a11))) + (set_local $a0 (i64.extend_s/i32 (i32.wrap/i64 (get_local $a0)))) + (set_local $a1 (i64.extend_s/i32 (i32.wrap/i64 (get_local $a1)))) + (set_local $a2 (i64.extend_s/i32 (i32.wrap/i64 (get_local $a2)))) + (set_local $a3 (i64.extend_s/i32 (i32.wrap/i64 (get_local $a3)))) + (set_local $a4 (i64.extend_s/i32 (i32.wrap/i64 (get_local $a4)))) + (set_local $a5 (i64.extend_s/i32 (i32.wrap/i64 (get_local $a5)))) + (set_local $a6 (i64.extend_s/i32 (i32.wrap/i64 (get_local $a6)))) + (set_local $a7 (i64.extend_s/i32 (i32.wrap/i64 (get_local $a7)))) + (set_local $a8 (i64.extend_s/i32 (i32.wrap/i64 (get_local $a8)))) + (set_local $a9 (i64.extend_s/i32 (i32.wrap/i64 (get_local $a9)))) + (set_local $a10 (i64.extend_s/i32 (i32.wrap/i64 (get_local $a10)))) + (set_local $a11 (i64.extend_s/i32 (i32.wrap/i64 (get_local $a11)))) - (set_local $b0 (i64.extend_s/i32 (get_local $b0))) - (set_local $b1 (i64.extend_s/i32 (get_local $b1))) - (set_local $b2 (i64.extend_s/i32 (get_local $b2))) - (set_local $b3 (i64.extend_s/i32 (get_local $b3))) - (set_local $b4 (i64.extend_s/i32 (get_local $b4))) - (set_local $b5 (i64.extend_s/i32 (get_local $b5))) - (set_local $b6 (i64.extend_s/i32 (get_local $b6))) - (set_local $b7 (i64.extend_s/i32 (get_local $b7))) - (set_local $b8 (i64.extend_s/i32 (get_local $b8))) - (set_local $b9 (i64.extend_s/i32 (get_local $b9))) - (set_local $b10 (i64.extend_s/i32 (get_local $b10))) - (set_local $b11 (i64.extend_s/i32 (get_local $b11))) + (set_local $b0 (i64.extend_s/i32 (i32.wrap/i64 (get_local $b0)))) + (set_local $b1 (i64.extend_s/i32 (i32.wrap/i64 (get_local $b1)))) + (set_local $b2 (i64.extend_s/i32 (i32.wrap/i64 (get_local $b2)))) + (set_local $b3 (i64.extend_s/i32 (i32.wrap/i64 (get_local $b3)))) + (set_local $b4 (i64.extend_s/i32 (i32.wrap/i64 (get_local $b4)))) + (set_local $b5 (i64.extend_s/i32 (i32.wrap/i64 (get_local $b5)))) + (set_local $b6 (i64.extend_s/i32 (i32.wrap/i64 (get_local $b6)))) + (set_local $b7 (i64.extend_s/i32 (i32.wrap/i64 (get_local $b7)))) + (set_local $b8 (i64.extend_s/i32 (i32.wrap/i64 (get_local $b8)))) + (set_local $b9 (i64.extend_s/i32 (i32.wrap/i64 (get_local $b9)))) + (set_local $b10 (i64.extend_s/i32 (i32.wrap/i64 (get_local $b10)))) + (set_local $b11 (i64.extend_s/i32 (i32.wrap/i64 (get_local $b11)))) (set_local $s0 (i64.mul (get_local $a0) (get_local $b0))) (set_local $s1 (i64.add (i64.mul (get_local $a0) (get_local $b1 )) (i64.mul (get_local $a1) (get_local $b0)))) @@ -274,4 +302,5 @@ (get_local $s21) (get_local $s22) (get_local $s23) - (call $sc_reduce))) + (i32.const 0) + (call_indirect (type $to_void)))) diff --git a/fe25519_25/sc25519_muladd.js b/fe25519_25/sc25519_muladd.js new file mode 100644 index 0000000..dabdf1e --- /dev/null +++ b/fe25519_25/sc25519_muladd.js @@ -0,0 +1,61 @@ + +module.exports = loadWebAssembly + +loadWebAssembly.supported = typeof WebAssembly !== 'undefined' + +function loadWebAssembly (opts) { + if (!loadWebAssembly.supported) return null + + var imp = opts && opts.imports + var wasm = toUint8Array('AGFzbQEAAAABJAJgGX9+fn5+fn5+fn5+fn5+fn5+fn5+fn5+fn4AYAR/f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sgNCBLfCE0IDMgS0IBQhWGfn0hMyA1QgFCFIZ8QhWHIU0gNiBNfCE2IDUgTUIBQhWGfn0hNSA3QgFCFIZ8QhWHIU8gOCBPfCE4IDcgT0IBQhWGfn0hNyA5QgFCFIZ8QhWHIVEgOiBRfCE6IDkgUUIBQhWGfn0hOSA7QgFCFIZ8QhWHIVMgPCBTfCE8IDsgU0IBQhWGfn0hOyA9QgFCFIZ8QhWHIVUgPiBVfCE+ID0gVUIBQhWGfn0hPSA/QgFCFIZ8QhWHIVcgQCBXfCFAID8gV0IBQhWGfn0hPyAqQgFCFIZ8QhWHIUIgKyBCfCErICogQkIBQhWGfn0hKiAsQgFCFIZ8QhWHIUQgLSBEfCEtICwgREIBQhWGfn0hLCAuQgFCFIZ8QhWHIUYgLyBGfCEvIC4gRkIBQhWGfn0hLiAwQgFCFIZ8QhWHIUggMSBIfCExIDAgSEIBQhWGfn0hMCAyQgFCFIZ8QhWHIUogMyBKfCEzIDIgSkIBQhWGfn0hMiA0QgFCFIZ8QhWHIUwgNSBMfCE1IDQgTEIBQhWGfn0hNCA2QgFCFIZ8QhWHIU4gNyBOfCE3IDYgTkIBQhWGfn0hNiA4QgFCFIZ8QhWHIVAgOSBQfCE5IDggUEIBQhWGfn0hOCA6QgFCFIZ8QhWHIVIgOyBSfCE7IDogUkIBQhWGfn0hOiA8QgFCFIZ8QhWHIVQgPSBUfCE9IDwgVEIBQhWGfn0hPCA+QgFCFIZ8QhWHIVYgPyBWfCE/ID4gVkIBQhWGfn0hPiAAICkgKiArICwgLSAuIC8gMCAxIDIgMyA0IDUgNiA3IDggOSA6IDsgPCA9ID4gPyBAQQARAAAL') + var ready = null + + var mod = { + buffer: wasm, + memory: null, + exports: null, + realloc: realloc, + onload: onload + } + + onload(function () {}) + + return mod + + function realloc (size) { + mod.exports.memory.grow(Math.max(0, Math.ceil(Math.abs(size - mod.memory.length) / 65536))) + mod.memory = new Uint8Array(mod.exports.memory.buffer) + } + + function onload (cb) { + if (mod.exports) return cb() + + if (ready) { + ready.then(cb.bind(null, null)).catch(cb) + return + } + + try { + if (opts && opts.async) throw new Error('async') + setup({instance: new WebAssembly.Instance(new WebAssembly.Module(wasm), imp)}) + } catch (err) { + ready = WebAssembly.instantiate(wasm, imp).then(setup) + } + + onload(cb) + } + + function setup (w) { + mod.exports = w.instance.exports + mod.memory = mod.exports.memory && mod.exports.memory.buffer && new Uint8Array(mod.exports.memory.buffer) + } +} + +function toUint8Array (s) { + if (typeof atob === 'function') return new Uint8Array(atob(s).split('').map(charCodeAt)) + return (require('buf' + 'fer').Buffer).from(s, 'base64') +} + +function charCodeAt (c) { + return c.charCodeAt(0) +} diff --git a/fe25519_25/sc25519_muladd.wat b/fe25519_25/sc25519_muladd.wat index 47e4ece..2519898 100644 --- a/fe25519_25/sc25519_muladd.wat +++ b/fe25519_25/sc25519_muladd.wat @@ -1,4 +1,32 @@ (module + (import "js" "table" (table 1 anyfunc)) + (type $to_void (func + (param $s i32) + (param $s0 i64) + (param $s1 i64) + (param $s2 i64) + (param $s3 i64) + (param $s4 i64) + (param $s5 i64) + (param $s6 i64) + (param $s7 i64) + (param $s8 i64) + (param $s9 i64) + (param $s10 i64) + (param $s11 i64) + (param $s12 i64) + (param $s13 i64) + (param $s14 i64) + (param $s15 i64) + (param $s16 i64) + (param $s17 i64) + (param $s18 i64) + (param $s19 i64) + (param $s20 i64) + (param $s21 i64) + (param $s22 i64) + (param $s23 i64))) + (memory $0 1) (export "memory" (memory $0)) @@ -132,44 +160,44 @@ (set_local $c10 (i64.load32_u offset=40 (get_local $c))) (set_local $c11 (i64.load32_u offset=44 (get_local $c))) - (set_local $a0 (i64.extend32_s (get_local $a0))) - (set_local $a1 (i64.extend32_s (get_local $a1))) - (set_local $a2 (i64.extend32_s (get_local $a2))) - (set_local $a3 (i64.extend32_s (get_local $a3))) - (set_local $a4 (i64.extend32_s (get_local $a4))) - (set_local $a5 (i64.extend32_s (get_local $a5))) - (set_local $a6 (i64.extend32_s (get_local $a6))) - (set_local $a7 (i64.extend32_s (get_local $a7))) - (set_local $a8 (i64.extend32_s (get_local $a8))) - (set_local $a9 (i64.extend32_s (get_local $a9))) - (set_local $a10 (i64.extend32_s (get_local $a10))) - (set_local $a11 (i64.extend32_s (get_local $a11))) + (set_local $a0 (i64.extend_s/i32 (i32.wrap/i64 (get_local $a0)))) + (set_local $a1 (i64.extend_s/i32 (i32.wrap/i64 (get_local $a1)))) + (set_local $a2 (i64.extend_s/i32 (i32.wrap/i64 (get_local $a2)))) + (set_local $a3 (i64.extend_s/i32 (i32.wrap/i64 (get_local $a3)))) + (set_local $a4 (i64.extend_s/i32 (i32.wrap/i64 (get_local $a4)))) + (set_local $a5 (i64.extend_s/i32 (i32.wrap/i64 (get_local $a5)))) + (set_local $a6 (i64.extend_s/i32 (i32.wrap/i64 (get_local $a6)))) + (set_local $a7 (i64.extend_s/i32 (i32.wrap/i64 (get_local $a7)))) + (set_local $a8 (i64.extend_s/i32 (i32.wrap/i64 (get_local $a8)))) + (set_local $a9 (i64.extend_s/i32 (i32.wrap/i64 (get_local $a9)))) + (set_local $a10 (i64.extend_s/i32 (i32.wrap/i64 (get_local $a10)))) + (set_local $a11 (i64.extend_s/i32 (i32.wrap/i64 (get_local $a11)))) - (set_local $b0 (i64.extend32_s (get_local $b0))) - (set_local $b1 (i64.extend32_s (get_local $b1))) - (set_local $b2 (i64.extend32_s (get_local $b2))) - (set_local $b3 (i64.extend32_s (get_local $b3))) - (set_local $b4 (i64.extend32_s (get_local $b4))) - (set_local $b5 (i64.extend32_s (get_local $b5))) - (set_local $b6 (i64.extend32_s (get_local $b6))) - (set_local $b7 (i64.extend32_s (get_local $b7))) - (set_local $b8 (i64.extend32_s (get_local $b8))) - (set_local $b9 (i64.extend32_s (get_local $b9))) - (set_local $b10 (i64.extend32_s (get_local $b10))) - (set_local $b11 (i64.extend32_s (get_local $b11))) + (set_local $b0 (i64.extend_s/i32 (i32.wrap/i64 (get_local $b0)))) + (set_local $b1 (i64.extend_s/i32 (i32.wrap/i64 (get_local $b1)))) + (set_local $b2 (i64.extend_s/i32 (i32.wrap/i64 (get_local $b2)))) + (set_local $b3 (i64.extend_s/i32 (i32.wrap/i64 (get_local $b3)))) + (set_local $b4 (i64.extend_s/i32 (i32.wrap/i64 (get_local $b4)))) + (set_local $b5 (i64.extend_s/i32 (i32.wrap/i64 (get_local $b5)))) + (set_local $b6 (i64.extend_s/i32 (i32.wrap/i64 (get_local $b6)))) + (set_local $b7 (i64.extend_s/i32 (i32.wrap/i64 (get_local $b7)))) + (set_local $b8 (i64.extend_s/i32 (i32.wrap/i64 (get_local $b8)))) + (set_local $b9 (i64.extend_s/i32 (i32.wrap/i64 (get_local $b9)))) + (set_local $b10 (i64.extend_s/i32 (i32.wrap/i64 (get_local $b10)))) + (set_local $b11 (i64.extend_s/i32 (i32.wrap/i64 (get_local $b11)))) - (set_local $c0 (i64.extend32_s (get_local $c0))) - (set_local $c1 (i64.extend32_s (get_local $c1))) - (set_local $c2 (i64.extend32_s (get_local $c2))) - (set_local $c3 (i64.extend32_s (get_local $c3))) - (set_local $c4 (i64.extend32_s (get_local $c4))) - (set_local $c5 (i64.extend32_s (get_local $c5))) - (set_local $c6 (i64.extend32_s (get_local $c6))) - (set_local $c7 (i64.extend32_s (get_local $c7))) - (set_local $c8 (i64.extend32_s (get_local $c8))) - (set_local $c9 (i64.extend32_s (get_local $c9))) - (set_local $c10 (i64.extend32_s (get_local $c10))) - (set_local $c11 (i64.extend32_s (get_local $c11))) + (set_local $c0 (i64.extend_s/i32 (i32.wrap/i64 (get_local $c0)))) + (set_local $c1 (i64.extend_s/i32 (i32.wrap/i64 (get_local $c1)))) + (set_local $c2 (i64.extend_s/i32 (i32.wrap/i64 (get_local $c2)))) + (set_local $c3 (i64.extend_s/i32 (i32.wrap/i64 (get_local $c3)))) + (set_local $c4 (i64.extend_s/i32 (i32.wrap/i64 (get_local $c4)))) + (set_local $c5 (i64.extend_s/i32 (i32.wrap/i64 (get_local $c5)))) + (set_local $c6 (i64.extend_s/i32 (i32.wrap/i64 (get_local $c6)))) + (set_local $c7 (i64.extend_s/i32 (i32.wrap/i64 (get_local $c7)))) + (set_local $c8 (i64.extend_s/i32 (i32.wrap/i64 (get_local $c8)))) + (set_local $c9 (i64.extend_s/i32 (i32.wrap/i64 (get_local $c9)))) + (set_local $c10 (i64.extend_s/i32 (i32.wrap/i64 (get_local $c10)))) + (set_local $c11 (i64.extend_s/i32 (i32.wrap/i64 (get_local $c11)))) (set_local $s0 (i64.add (get_local $c0 ) (i64.mul (get_local $a0) (get_local $b0)))) (set_local $s1 (i64.add (get_local $c1 ) (i64.add (i64.mul (get_local $a0) (get_local $b1 )) (i64.mul (get_local $a1) (get_local $b0))))) @@ -313,4 +341,5 @@ (get_local $s21) (get_local $s22) (get_local $s23) - (call $sc_reduce))) + (i32.const 0) + (call_indirect (type $to_void)))) diff --git a/fe25519_25/sc_reduce.js b/fe25519_25/sc_reduce.js new file mode 100644 index 0000000..462fe1b --- /dev/null +++ b/fe25519_25/sc_reduce.js @@ -0,0 +1,61 @@ + +module.exports = loadWebAssembly + +loadWebAssembly.supported = typeof WebAssembly !== 'undefined' + +function loadWebAssembly (opts) { + if (!loadWebAssembly.supported) return null + + var imp = opts && opts.imports + var wasm = toUint8Array('AGFzbQEAAAABIQJgGX9+fn5+fn5+fn5+fn5+fn5+fn5+fn5+fn4AYAF/AAIOAQJqcwV0YWJsZQFwAAEDAwIAAQUDAQABBycDBm1lbW9yeQIACXNjX3JlZHVjZQAADnNjMjU1MTlfcmVkdWNlAAEJBwEAQQALAQAK+hcC+BYBEX4gDCAYQpPYKH58IQwgDSAYQpjaHH58IQ0gDiAYQuf2J358IQ4gDyAYQq3zPH59IQ8gECAYQtGrCH58IRAgESAYQv3eKX59IREgCyAXQpPYKH58IQsgDCAXQpjaHH58IQwgDSAXQuf2J358IQ0gDiAXQq3zPH59IQ4gDyAXQtGrCH58IQ8gECAXQv3eKX59IRAgCiAWQpPYKH58IQogCyAWQpjaHH58IQsgDCAWQuf2J358IQwgDSAWQq3zPH59IQ0gDiAWQtGrCH58IQ4gDyAWQv3eKX59IQ8gCSAVQpPYKH58IQkgCiAVQpjaHH58IQogCyAVQuf2J358IQsgDCAVQq3zPH59IQwgDSAVQtGrCH58IQ0gDiAVQv3eKX59IQ4gCCAUQpPYKH58IQggCSAUQpjaHH58IQkgCiAUQuf2J358IQogCyAUQq3zPH59IQsgDCAUQtGrCH58IQwgDSAUQv3eKX59IQ0gByATQpPYKH58IQcgCCATQpjaHH58IQggCSATQuf2J358IQkgCiATQq3zPH59IQogCyATQtGrCH58IQsgDCATQv3eKX59IQwgB0IBQhSGfEIVhyEfIAggH3whCCAHIB9CAUIVhn59IQcgCUIBQhSGfEIVhyEhIAogIXwhCiAJICFCAUIVhn59IQkgC0IBQhSGfEIVhyEjIAwgI3whDCALICNCAUIVhn59IQsgDUIBQhSGfEIVhyElIA4gJXwhDiANICVCAUIVhn59IQ0gD0IBQhSGfEIVhyEnIBAgJ3whECAPICdCAUIVhn59IQ8gEUIBQhSGfEIVhyEpIBIgKXwhEiARIClCAUIVhn59IREgCEIBQhSGfEIVhyEgIAkgIHwhCSAIICBCAUIVhn59IQggCkIBQhSGfEIVhyEiIAsgInwhCyAKICJCAUIVhn59IQogDEIBQhSGfEIVhyEkIA0gJHwhDSAMICRCAUIVhn59IQwgDkIBQhSGfEIVhyEmIA8gJnwhDyAOICZCAUIVhn59IQ4gEEIBQhSGfEIVhyEoIBEgKHwhESAQIChCAUIVhn59IRAgBiASQpPYKH58IQYgByASQpjaHH58IQcgCCASQuf2J358IQggCSASQq3zPH59IQkgCiASQtGrCH58IQogCyASQv3eKX59IQsgBSARQpPYKH58IQUgBiARQpjaHH58IQYgByARQuf2J358IQcgCCARQq3zPH59IQggCSARQtGrCH58IQkgCiARQv3eKX59IQogBCAQQpPYKH58IQQgBSAQQpjaHH58IQUgBiAQQuf2J358IQYgByAQQq3zPH59IQcgCCAQQtGrCH58IQggCSAQQv3eKX59IQk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AGQimGIAdCwAB+QjiGhEKAgICAgICAgH+DhDcDCCAAIAdCAodC/wGDIAdCAodCgP4Dg4QgB0IChyAIQgh+QhCGhEKAgPwHg4QgCEIThkKAgID4D4OEIAhCE4ZCgICAgPAfg4QgCUIohkKAgICAgOA/g4QgCUIohkKAgICAgIDA/wCDhCAJQiiGIApCIH5COIaEQoCAgICAgICAf4OENwMQIAAgCkIDh0L/AYMgCkIDh0KA/gODhCAKQgOHIAtCBH5CEIaEQoCA/AeDhCALQhKGQoCAgPgPg4QgC0IShiAMQoABfkIghoRCgICAgPAfg4QgDEInhkKAgICAgOA/g4QgDEInhkKAgICAgIDA/wCDhCAMQieGQoCAgICAgICAf4OENwMYC34AIAAgADUCACAANQIEIAA1AgggADUCDCAANQIQIAA1AhQgADUCGCAANQIcIAA1AiAgADUCJCAANQIoIAA1AiwgADUCMCAANQI0IAA1AjggADUCPCAANQJAIAA1AkQgADUCSCAANQJMIAA1AlAgADUCVCAANQJYIAA1AlwQAAs=') + var ready = null + + var mod = { + buffer: wasm, + memory: null, + exports: null, + realloc: realloc, + onload: onload + } + + onload(function () {}) + + return mod + + function realloc (size) { + mod.exports.memory.grow(Math.max(0, Math.ceil(Math.abs(size - mod.memory.length) / 65536))) + mod.memory = new Uint8Array(mod.exports.memory.buffer) + } + + function onload (cb) { + if (mod.exports) return cb() + + if (ready) { + ready.then(cb.bind(null, null)).catch(cb) + return + } + + try { + if (opts && opts.async) throw new Error('async') + setup({instance: new WebAssembly.Instance(new WebAssembly.Module(wasm), imp)}) + } catch (err) { + ready = WebAssembly.instantiate(wasm, imp).then(setup) + } + + onload(cb) + } + + function setup (w) { + mod.exports = w.instance.exports + mod.memory = mod.exports.memory && mod.exports.memory.buffer && new Uint8Array(mod.exports.memory.buffer) + } +} + +function toUint8Array (s) { + if (typeof atob === 'function') return new Uint8Array(atob(s).split('').map(charCodeAt)) + return (require('buf' + 'fer').Buffer).from(s, 'base64') +} + +function charCodeAt (c) { + return c.charCodeAt(0) +} diff --git a/fe25519_25/sc_reduce.wat b/fe25519_25/sc_reduce.wat index 188e8fc..0c06093 100644 --- a/fe25519_25/sc_reduce.wat +++ b/fe25519_25/sc_reduce.wat @@ -1,9 +1,12 @@ (module + (import "js" "table" (table 1 anyfunc)) + (elem (i32.const 0) $sc_reduce) + (memory $0 1) (export "memory" (memory $0)) - + (func $sc_reduce (export "sc_reduce") - (param $ptr i32) + (param $s i32) (param $s0 i64) (param $s1 i64) @@ -30,6 +33,24 @@ (param $s22 i64) (param $s23 i64) + (local $carry0 i64) + (local $carry1 i64) + (local $carry2 i64) + (local $carry3 i64) + (local $carry4 i64) + (local $carry5 i64) + (local $carry6 i64) + (local $carry7 i64) + (local $carry8 i64) + (local $carry9 i64) + (local $carry10 i64) + (local $carry11 i64) + (local $carry12 i64) + (local $carry13 i64) + (local $carry14 i64) + (local $carry15 i64) + (local $carry16 i64) + (set_local $s11 (i64.add (get_local $s11) (i64.mul (get_local $s23) (i64.const 666643)))) (set_local $s12 (i64.add (get_local $s12) (i64.mul (get_local $s23) (i64.const 470296)))) (set_local $s13 (i64.add (get_local $s13) (i64.mul (get_local $s23) (i64.const 654183))))