Missing helpers (#24)

* Detach buffers by sending to an empty message channel

* Move helpers out

* fix import

* export helpers

* Try browser testing

* messagechannel check

* xvfb-run --auto-servernum npm run test-browser fails weirdly on ci, removing

Co-authored-by: Mathias Buus <mathiasbuus@gmail.com>
This commit is contained in:
Emil Bay 2020-08-12 15:39:14 +02:00 committed by GitHub
parent e4693065fd
commit a338ae9f9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 66 additions and 23 deletions

View File

@ -1,3 +1,18 @@
language: node_js
node_js:
- lts/*
env:
global:
- MOZ_HEADLESS=1
services:
- xvfb
addons:
firefox: latest
chrome: stable
cache:
npm: false
script:
- npm test
- xvfb-run --auto-servernum npm run test-browser -- --browser chrome
- xvfb-run --auto-servernum npm run test-browser -- --browser firefox

View File

@ -1,11 +1,7 @@
/* eslint-disable camelcase */
const assert = require('nanoassert')
module.exports = {
crypto_verify_16,
crypto_verify_32,
sodium_memcmp,
sodium_is_zero
crypto_verify_32
}
function vn (x, xi, y, yi, n) {
@ -14,6 +10,11 @@ function vn (x, xi, y, yi, n) {
return (1 & ((d - 1) >>> 8)) - 1
}
// Make non enumerable as this is an internal function
Object.defineProperty(module.exports, 'vn', {
value: vn
})
function crypto_verify_16 (x, xi, y, yi) {
return vn(x, xi, y, yi, 16)
}
@ -21,15 +22,3 @@ function crypto_verify_16 (x, xi, y, yi) {
function crypto_verify_32 (x, xi, y, yi) {
return vn(x, xi, y, yi, 32)
}
function sodium_memcmp (a, b) {
assert(a.byteLength === b.byteLength, 'buffers must be the same size')
return vn(a, 0, b, 0, a.byteLength) === 0
}
function sodium_is_zero (arr) {
var d = 0
for (let i = 0; i < arr.length; i++) d |= arr[i]
return d === 0
}

31
helpers.js Normal file
View File

@ -0,0 +1,31 @@
/* eslint-disable camelcase */
const assert = require('nanoassert')
const { vn } = require('./crypto_verify')
function sodium_increment (n) {
const nlen = n.byteLength
var c = 1
for (var i = 0; i < nlen; i++) {
c += n[i]
n[i] = c
c >>= 8
}
}
function sodium_memcmp (a, b) {
assert(a.byteLength === b.byteLength, 'buffers must be the same size')
return vn(a, 0, b, 0, a.byteLength) === 0
}
function sodium_is_zero (arr) {
var d = 0
for (let i = 0; i < arr.length; i++) d |= arr[i]
return d === 0
}
module.exports = {
sodium_increment,
sodium_memcmp,
sodium_is_zero
}

View File

@ -8,7 +8,10 @@
// Implementation derived from TweetNaCl version 20140427.
// See for details: http://tweetnacl.cr.yp.to/
forward(require('./randombytes'))
forward(require('./memory'))
forward(require('./helpers'))
forward(require('./crypto_verify'))
forward(require('./crypto_box'))
forward(require('./crypto_generichash'))
forward(require('./crypto_hash'))
@ -23,8 +26,6 @@ forward(require('./crypto_shorthash'))
forward(require('./crypto_sign'))
forward(require('./crypto_stream'))
forward(require('./crypto_stream_chacha20'))
forward(require('./crypto_verify'))
forward(require('./randombytes'))
function forward (submodule) {
Object.keys(submodule).forEach(function (prop) {

View File

@ -1,14 +1,23 @@
/* eslint-disable camelcase */
var MessageChannel = global.MessageChannel
if (MessageChannel == null) ({ MessageChannel } = require('worker' + '_threads'))
function sodium_malloc (n) {
return new Uint8Array(n)
}
const sink = new MessageChannel()
function sodium_free (n) {
sodium_memzero(n)
sink.port1.postMessage(n.buffer, [n.buffer])
}
function sodium_memzero (arr) {
arr.fill(0)
}
module.exports = {
sodium_malloc,
sodium_free,
sodium_memzero
}

View File

@ -13,7 +13,6 @@
"xsalsa20": "^1.0.0"
},
"devDependencies": {
"browser-run": "^7.0.2",
"browserify": "^16.5.1",
"sodium-test": "^0.9.0",
"standard": "^14.3.4",
@ -31,10 +30,9 @@
"crypto": "crypto"
},
"scripts": {
"browser": "browserify test.js | browser-run",
"browser-manual": "browserify test.js | tape-run",
"pretest": "standard",
"test": "node test.js"
"test": "node test.js",
"test-browser": "browserify test.js | tape-run"
},
"repository": {
"type": "git",