Skip to content

Commit

Permalink
update standard, lint, more tests for hash/sha256
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Aug 8, 2023
1 parent 5cdd1dc commit dbca8ed
Show file tree
Hide file tree
Showing 19 changed files with 879 additions and 1,404 deletions.
1 change: 0 additions & 1 deletion binary.test.js
@@ -1,4 +1,3 @@

import * as binary from './binary.js'
import * as t from './testing.js'

Expand Down
1 change: 0 additions & 1 deletion cache.js
@@ -1,4 +1,3 @@

/* eslint-env browser */

/**
Expand Down
1 change: 0 additions & 1 deletion cache.test.js
@@ -1,4 +1,3 @@

import * as t from './testing.js'
import * as cache from './cache.js'
import * as promise from './promise.js'
Expand Down
25 changes: 14 additions & 11 deletions component.js
Expand Up @@ -57,14 +57,15 @@ export class Lib0Component extends HTMLElement {
}

/**
* @param {S} state
* @param {boolean} [forceStateUpdate] Force that the state is rerendered even if state didn't change
* @param {S} _state
* @param {boolean} [_forceStateUpdate] Force that the state is rerendered even if state didn't change
*/
setState (state, forceStateUpdate = true) {}
setState (_state, _forceStateUpdate = true) {}

/**
* @param {any} stateUpdate
* @param {any} _stateUpdate
*/
updateState (stateUpdate) { }
updateState (_stateUpdate) { }
}

/**
Expand Down Expand Up @@ -135,12 +136,14 @@ export const createComponent = (name, { template, style = '', state: defaultStat
for (const key in attrs) {
normalizedAttrs[string.fromCamelCase(key, '-')] = key
}
const templateElement = template ? /** @type {HTMLTemplateElement} */ (dom.parseElement(`
<template>
<style>${style}</style>
${template}
</template>
`)) : null
const templateElement = template
? /** @type {HTMLTemplateElement} */ (dom.parseElement(`
<template>
<style>${style}</style>
${template}
</template>
`))
: null

class _Lib0Component extends HTMLElement {
/**
Expand Down
1 change: 0 additions & 1 deletion environment.test.js
@@ -1 +0,0 @@

33 changes: 16 additions & 17 deletions hash/sha256.fallback.js
Expand Up @@ -6,7 +6,6 @@
*/

import * as binary from '../binary.js'
import * as math from '../math.js'

/**
* @param {number} w - a 32bit uint
Expand Down Expand Up @@ -75,15 +74,15 @@ class Hasher {
constructor () {
const buf = new ArrayBuffer(64 + 64 * 4)
// Init working variables using a single arraybuffer
this.H = new Uint32Array(buf, 0, 8)
this.H.set(HINIT)
this._H = new Uint32Array(buf, 0, 8)
this._H.set(HINIT)
// "Message schedule" - a working variable
this.W = new Uint32Array(buf, 64, 64)
this._W = new Uint32Array(buf, 64, 64)
}

_updateHash () {
const H = this.H
const W = this.W
const H = this._H
const W = this._W
for (let t = 16; t < 64; t++) {
W[t] = sigma1to256(W[t - 2]) + W[t - 7] + sigma0to256(W[t - 15]) + W[t - 16]
}
Expand Down Expand Up @@ -126,41 +125,41 @@ class Hasher {
// write data in big endianess
let j = 0
for (; j < 16 && i + 3 < data.length; j++) {
this.W[j] = data[i++] << 24 | data[i++] << 16 | data[i++] << 8 | data[i++]
this._W[j] = data[i++] << 24 | data[i++] << 16 | data[i++] << 8 | data[i++]
}
if (i % 64 !== 0) { // there is still room to write partial content and the ending bit.
this.W.fill(0, j, 16)
this._W.fill(0, j, 16)
while (i < data.length) {
this.W[j] |= data[i] << ((3 - (i % 4)) * 8)
this._W[j] |= data[i] << ((3 - (i % 4)) * 8)
i++
}
this.W[j] |= binary.BIT8 << ((3 - (i % 4)) * 8)
this._W[j] |= binary.BIT8 << ((3 - (i % 4)) * 8)
}
this._updateHash()
}
// same check as earlier - the ending bit has been written
const isPaddedWith1 = i % 64 !== 0
this.W.fill(0, 0, 16)
this._W.fill(0, 0, 16)
let j = 0
for (; i < data.length; j++) {
for (let ci = 3; ci >= 0 && i < data.length; ci--) {
this.W[j] |= data[i++] << (ci * 8)
this._W[j] |= data[i++] << (ci * 8)
}
}
// Write padding of the message. See 5.1.2.
if (!isPaddedWith1) {
this.W[j - (i % 4 === 0 ? 0 : 1)] |= binary.BIT8 << ((3 - (i % 4)) * 8)
this._W[j - (i % 4 === 0 ? 0 : 1)] |= binary.BIT8 << ((3 - (i % 4)) * 8)
}
// write length of message (size in bits) as 64 bit uint
// @todo test that this works correctly
this.W[14] = data.byteLength / binary.BIT30 // same as data.byteLength >>> 30 - but works on floats
this.W[15] = data.byteLength * 8
this._W[14] = data.byteLength / binary.BIT30 // same as data.byteLength >>> 30 - but works on floats
this._W[15] = data.byteLength * 8
this._updateHash()
// correct H endianness to use big endiannes and return a Uint8Array
const dv = new Uint8Array(32)
for (let i = 0; i < this.H.length; i++) {
for (let i = 0; i < this._H.length; i++) {
for (let ci = 0; ci < 4; ci++) {
dv[i * 4 + ci] = this.H[i] >>> (3 - ci) * 8
dv[i * 4 + ci] = this._H[i] >>> (3 - ci) * 8
}
}
return dv
Expand Down
16 changes: 10 additions & 6 deletions hash/sha256.test.js
Expand Up @@ -7,7 +7,6 @@ import * as webcrypto from 'lib0/webcrypto'
import * as promise from '../promise.js'
import * as env from '../environment.js'
import * as array from '../array.js'
import * as binary from '../binary.js'
import * as f from '../function.js'

/**
Expand Down Expand Up @@ -47,13 +46,11 @@ export const testSha256Basics = async _tc => {
*/
export const testLargeValue = async _tc => {
t.skip(!t.extensive)
const BS = binary.BIT30
const BS = 100 * 1000 * 1000
const data = prng.uint8Array(prng.create(42), BS)
let resNode = buffer.fromBase64('WZK5ZK68FVhGoTXZY0XrU9wcfTHsqmJZukf1ULEAD+s=')
let resNode = buffer.fromBase64('81m7UtkH2s9J3E33Bw5kRMuC5zvktgZ64SfzenbV5Lw=')
let resLib0
t.measureTime(`[lib0] Hash message of size ${BS}`, () => {
resLib0 = sha256.hash(data)
})
let resWebcrypto
if (env.isNode) {
const sha256Node = await import('./sha256.node.js')
t.measureTime(`[node] Hash message of size ${BS}`, () => {
Expand All @@ -65,7 +62,14 @@ export const testLargeValue = async _tc => {
t.compare(res, resNode, 'Precomputed result should be the same')
})
}
t.measureTime(`[lib0] Hash message of size ${BS}`, () => {
resLib0 = sha256.hash(data)
})
await t.measureTimeAsync(`[webcrypto] Hash message of size ${BS}`, async () => {
resWebcrypto = new Uint8Array(await webcrypto.subtle.digest('SHA-256', data))
})
t.compare(resLib0, resNode)
t.compare(resLib0, resWebcrypto)
}

/**
Expand Down
32 changes: 16 additions & 16 deletions logging.common.js
Expand Up @@ -56,21 +56,21 @@ export const createModuleLogger = (_print, moduleName) => {
return !doLogging
? func.nop
: (...args) => {
const timeNow = time.getUnixTime()
const timeDiff = timeNow - lastLoggingTime
lastLoggingTime = timeNow
_print(
color,
moduleName,
UNCOLOR,
...args.map((arg) =>
(typeof arg === 'string' || typeof arg === 'symbol')
? arg
: JSON.stringify(arg)
),
color,
' +' + timeDiff + 'ms'
)
}
const timeNow = time.getUnixTime()
const timeDiff = timeNow - lastLoggingTime
lastLoggingTime = timeNow
_print(
color,
moduleName,
UNCOLOR,
...args.map((arg) =>
(typeof arg === 'string' || typeof arg === 'symbol')
? arg
: JSON.stringify(arg)
),
color,
' +' + timeDiff + 'ms'
)
}
}
/* c8 ignore stop */
1 change: 0 additions & 1 deletion logging.test.js
@@ -1,4 +1,3 @@

import * as log from 'lib0/logging'

export const testLogging = () => {
Expand Down
1 change: 1 addition & 0 deletions object.js
Expand Up @@ -70,6 +70,7 @@ export const some = (obj, f) => {
* @param {Object|undefined} obj
*/
export const isEmpty = obj => {
// eslint-disable-next-line
for (const _k in obj) {
return false
}
Expand Down

0 comments on commit dbca8ed

Please sign in to comment.