diff --git a/crypto_hash.js b/crypto_hash.js index 0886244..77eacbf 100644 --- a/crypto_hash.js +++ b/crypto_hash.js @@ -1,4 +1,4 @@ -const sha512 = require('sha512-wasm') +const sha512 = require('sha512-universal') const assert = require('nanoassert') if (new Uint16Array([1])[0] !== 1) throw new Error('Big endian architecture is not supported.') diff --git a/crypto_hash_sha256.js b/crypto_hash_sha256.js index cbe0543..97139e7 100644 --- a/crypto_hash_sha256.js +++ b/crypto_hash_sha256.js @@ -1,4 +1,4 @@ -const sha256 = require('sha256-wasm') +const sha256 = require('sha256-universal') const assert = require('nanoassert') if (new Uint16Array([1])[0] !== 1) throw new Error('Big endian architecture is not supported.') diff --git a/fe-test.js b/fe-test.js index 64e62ed..1b7e723 100644 --- a/fe-test.js +++ b/fe-test.js @@ -1,5 +1,32 @@ const ec = require('./fe25519_25.js') const sodium = require('./') +const wasm = require('./fe25519_25/fe25519_invert')({ + 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 + } + } + } +}) +const wasm2 = require('./fe25519_25/fe25519_pow22523')({ + 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 + } + } + } +}) + // const crypto = require('crypto') var f = new Int32Array(10) @@ -9,6 +36,30 @@ var h = new Int32Array(10) var a = ec.ge3() 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) + + buf = Buffer.from(wasm.memory.slice(40, 80)) + for (let i = 0; i < 10; i++) { + h[i] = buf.readUInt32LE(4 * i) + } +} + +function wasm_pow (h, f) { + var buf = Buffer.from(f.buffer) + + wasm2.memory.set(buf) + wasm2.exports.fe25519_pow22523(40, 0) + + buf = Buffer.from(wasm2.memory.slice(40, 80)) + for (let i = 0; i < 10; i++) { + h[i] = buf.readUInt32LE(4 * i) + } +} + f[0] = 23983080 a[2][0] = 1 @@ -102,33 +153,31 @@ const gf = ec.ge3() /////////////////////////////////////////// ec.fe25519_frombytes(a, an) ec.fe25519_frombytes(b, bn) + +ec.fe25519_mul(c, b, a) // // console.log('\na __________') // // for (let i = 0; i < 10; i++) console.log(`a${i}:`, signedInt(a[i]).toString(16).padStart(8, '0')) // // console.log('\nb __________') // // for (let i = 0; i < 10; i++) console.log(`b${i}:`, signedInt(b[i]).toString(16).padStart(8, '0')) // ec.fe25519_frombytes(c, bn) -// ec.fe25519_tobytes(res, c) -// console.log('tess :', res.toString('hex')) +ec.fe25519_tobytes(res, c) +console.log('tess :', res.toString('hex')) -ec.fe25519_mul(g, a, b) -ec.fe25519_tobytes(res, g) -console.log('fe_mul:', res.toString('hex')) -// ec.fe25519_sq(g, a) -// ec.fe25519_tobytes(res, g) -// console.log('fe_sq :', res.toString('hex')) +console.time('standard') +for (let i = 0; i < 10000; i++) ec.fe25519_pow22523(b, a) +console.timeEnd('standard') -// ec.fe25519_reduce(g, c) -// ec.fe25519_tobytes(res, g) -// console.log('fe_red:', res.toString('hex')) +ec.fe25519_tobytes(res, b) +console.log('tess :', res.toString('hex')) -// ec.fe25519_sqmul(a, 8734, b) -// ec.fe25519_tobytes(res, a) -// console.log('fe_sqm:', res.toString('hex')) +console.log(wasm.buffer.length) -// ec.fe25519_invert(a, a) -// ec.fe25519_tobytes(res, a) -// console.log('fe_inv:', res.toString('hex')) +console.time('pure wasm') +for (let i = 0; i < 10000; i++) wasm_pow(b, a) +console.timeEnd('pure wasm') +ec.fe25519_tobytes(res, b) +console.log('tess :', res.toString('hex')) // ec.fe25519_pow22523(a, a) // ec.fe25519_tobytes(res, a) @@ -180,7 +229,7 @@ console.log('fe_mul:', res.toString('hex')) // 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 b11bc8a..6a0c5a5 100644 --- a/fe25519_25.js +++ b/fe25519_25.js @@ -14,6 +14,58 @@ const wasm = require('./fe25519_25/mult.js')({ } }) +const wasm3 = require('./fe25519_25/fe25519_invert')({ + 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 + } + } + } +}) + +const wasm2 = require('./fe25519_25/fe25519_pow22523')({ + 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 + } + } + } +}) + +function fe25519_invert (h, f) { + var buf = Buffer.from(f.buffer) + + wasm3.memory.set(buf) + wasm3.exports.fe25519_invert(40, 0) + + buf = Buffer.from(wasm3.memory.slice(40, 80)) + for (let i = 0; i < 10; i++) { + h[i] = buf.readUInt32LE(4 * i) + } +} + +function fe25519_pow22523 (h, f) { + var buf = Buffer.from(f.buffer) + + wasm2.memory.set(buf) + wasm2.exports.fe25519_pow22523(40, 0) + + buf = Buffer.from(wasm2.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) @@ -42,11 +94,10 @@ module.exports = { fe25519_sq, fe25519_sqmul, fe25519_sq2, - fe25519_invert, - fe25519_pow22523, + fe25519_invert: fe25519_invert, + fe25519_pow22523: fe25519_pow22523, fe25519_unchecked_sqrt, fe25519_sqrt, - ge25519_add, ge25519_has_small_order, ge25519_frombytes, ge25519_tobytes, @@ -309,7 +360,6 @@ function fe25519_frombytes (h, s) { var carry8 var carry9 - carry9 = (h9_ + (1 << 8)) >> 9 h9_ -= carry9 * (1 << 9) h0 += carry9 * 19 @@ -345,7 +395,6 @@ function fe25519_frombytes (h, s) { h8_ += carry7 h8 -= carry7 * (1 << 16) - carry0 = (h0_ + (1 << 9)) >>> 10 h0_ -= carry0 * (1 << 10) h1 += carry0 @@ -727,7 +776,7 @@ function fe25519_mul (h, f, g) { wasm.memory.set(fbuf) wasm.memory.set(gbuf, 40) - wasm.exports.fe255219_mul(0, 40) + wasm.exports.fe25519_mul(80, 0, 40) buf = Buffer.from(wasm.memory.slice(80, 120)) for (let i = 0; i < 10; i++) { @@ -753,7 +802,7 @@ function fe25519_sq (h, f, log) { var buf = Buffer.from(f.buffer) wasm.memory.set(buf) - wasm.exports.sq(40, 0, 0) + wasm.exports.fe25519_sq(40, 0, 0) buf = Buffer.from(wasm.memory.slice(40, 80)) for (let i = 0; i < 10; i++) { @@ -779,7 +828,7 @@ function fe25519_sq2 (h, f) { var buf = Buffer.from(f.buffer) wasm.memory.set(buf) - wasm.exports.sq(40, 0, 1) + wasm.exports.fe25519_sq(40, 0, 1) buf = Buffer.from(wasm.memory.slice(40, 80)) for (let i = 0; i < 10; i++) { @@ -802,7 +851,7 @@ function fe25519_sqmul (s, n, a) { * Inversion - returns 0 if z=0 */ -function fe25519_invert (out, z) { +function fe25519_invert_1 (out, z) { check_fe(out) check_fe(z) @@ -862,7 +911,7 @@ function fe25519_invert (out, z) { Power 2^252 - 3 mod 2^255 - 19 */ -function fe25519_pow22523 (out, z) { +function fe25519_pow22523_1 (out, z) { check_fe(out) check_fe(z)