Socket
Socket
Sign inDemoInstall

@chainsafe/as-sha256

Package Overview
Dependencies
Maintainers
6
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@chainsafe/as-sha256 - npm Package Compare versions

Comparing version 0.4.2 to 0.5.0

lib/alloc.d.ts

6

lib/hashObject.d.ts

@@ -27,3 +27,7 @@ /**

**/
export declare function byteArrayToHashObject(byteArr: Uint8Array): HashObject;
export declare function byteArrayToHashObject(byteArr: Uint8Array, offset: number): HashObject;
/**
* Same to above but this set result to the output param to save memory.
*/
export declare function byteArrayIntoHashObject(byteArr: Uint8Array, offset: number, output: HashObject): void;
//# sourceMappingURL=hashObject.d.ts.map

114

lib/hashObject.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.byteArrayToHashObject = exports.hashObjectToByteArray = void 0;
exports.byteArrayIntoHashObject = exports.byteArrayToHashObject = exports.hashObjectToByteArray = void 0;
/**

@@ -82,87 +82,95 @@ * Pass 8 numbers in an object and set that to inputArray.

**/
function byteArrayToHashObject(byteArr) {
function byteArrayToHashObject(byteArr, offset) {
const result = {
h0: 0,
h1: 0,
h2: 0,
h3: 0,
h4: 0,
h5: 0,
h6: 0,
h7: 0,
};
byteArrayIntoHashObject(byteArr, offset, result);
return result;
}
exports.byteArrayToHashObject = byteArrayToHashObject;
/**
* Same to above but this set result to the output param to save memory.
*/
function byteArrayIntoHashObject(byteArr, offset, output) {
let tmp = 0;
tmp |= byteArr[3] & 0xff;
tmp |= byteArr[3 + offset] & 0xff;
tmp = tmp << 8;
tmp |= byteArr[2] & 0xff;
tmp |= byteArr[2 + offset] & 0xff;
tmp = tmp << 8;
tmp |= byteArr[1] & 0xff;
tmp |= byteArr[1 + offset] & 0xff;
tmp = tmp << 8;
tmp |= byteArr[0] & 0xff;
const h0 = tmp;
tmp |= byteArr[0 + offset] & 0xff;
output.h0 = tmp;
tmp = 0;
tmp |= byteArr[7] & 0xff;
tmp |= byteArr[7 + offset] & 0xff;
tmp = tmp << 8;
tmp |= byteArr[6] & 0xff;
tmp |= byteArr[6 + offset] & 0xff;
tmp = tmp << 8;
tmp |= byteArr[5] & 0xff;
tmp |= byteArr[5 + offset] & 0xff;
tmp = tmp << 8;
tmp |= byteArr[4] & 0xff;
const h1 = tmp;
tmp |= byteArr[4 + offset] & 0xff;
output.h1 = tmp;
tmp = 0;
tmp |= byteArr[11] & 0xff;
tmp |= byteArr[11 + offset] & 0xff;
tmp = tmp << 8;
tmp |= byteArr[10] & 0xff;
tmp |= byteArr[10 + offset] & 0xff;
tmp = tmp << 8;
tmp |= byteArr[9] & 0xff;
tmp |= byteArr[9 + offset] & 0xff;
tmp = tmp << 8;
tmp |= byteArr[8] & 0xff;
const h2 = tmp;
tmp |= byteArr[8 + offset] & 0xff;
output.h2 = tmp;
tmp = 0;
tmp |= byteArr[15] & 0xff;
tmp |= byteArr[15 + offset] & 0xff;
tmp = tmp << 8;
tmp |= byteArr[14] & 0xff;
tmp |= byteArr[14 + offset] & 0xff;
tmp = tmp << 8;
tmp |= byteArr[13] & 0xff;
tmp |= byteArr[13 + offset] & 0xff;
tmp = tmp << 8;
tmp |= byteArr[12] & 0xff;
const h3 = tmp;
tmp |= byteArr[12 + offset] & 0xff;
output.h3 = tmp;
tmp = 0;
tmp |= byteArr[19] & 0xff;
tmp |= byteArr[19 + offset] & 0xff;
tmp = tmp << 8;
tmp |= byteArr[18] & 0xff;
tmp |= byteArr[18 + offset] & 0xff;
tmp = tmp << 8;
tmp |= byteArr[17] & 0xff;
tmp |= byteArr[17 + offset] & 0xff;
tmp = tmp << 8;
tmp |= byteArr[16] & 0xff;
const h4 = tmp;
tmp |= byteArr[16 + offset] & 0xff;
output.h4 = tmp;
tmp = 0;
tmp |= byteArr[23] & 0xff;
tmp |= byteArr[23 + offset] & 0xff;
tmp = tmp << 8;
tmp |= byteArr[22] & 0xff;
tmp |= byteArr[22 + offset] & 0xff;
tmp = tmp << 8;
tmp |= byteArr[21] & 0xff;
tmp |= byteArr[21 + offset] & 0xff;
tmp = tmp << 8;
tmp |= byteArr[20] & 0xff;
const h5 = tmp;
tmp |= byteArr[20 + offset] & 0xff;
output.h5 = tmp;
tmp = 0;
tmp |= byteArr[27] & 0xff;
tmp |= byteArr[27 + offset] & 0xff;
tmp = tmp << 8;
tmp |= byteArr[26] & 0xff;
tmp |= byteArr[26 + offset] & 0xff;
tmp = tmp << 8;
tmp |= byteArr[25] & 0xff;
tmp |= byteArr[25 + offset] & 0xff;
tmp = tmp << 8;
tmp |= byteArr[24] & 0xff;
const h6 = tmp;
tmp |= byteArr[24 + offset] & 0xff;
output.h6 = tmp;
tmp = 0;
tmp |= byteArr[31] & 0xff;
tmp |= byteArr[31 + offset] & 0xff;
tmp = tmp << 8;
tmp |= byteArr[30] & 0xff;
tmp |= byteArr[30 + offset] & 0xff;
tmp = tmp << 8;
tmp |= byteArr[29] & 0xff;
tmp |= byteArr[29 + offset] & 0xff;
tmp = tmp << 8;
tmp |= byteArr[28] & 0xff;
const h7 = tmp;
return {
h0,
h1,
h2,
h3,
h4,
h5,
h6,
h7,
};
tmp |= byteArr[28 + offset] & 0xff;
output.h7 = tmp;
}
exports.byteArrayToHashObject = byteArrayToHashObject;
exports.byteArrayIntoHashObject = byteArrayIntoHashObject;
//# sourceMappingURL=hashObject.js.map

@@ -1,4 +0,4 @@

import { HashObject, byteArrayToHashObject, hashObjectToByteArray } from "./hashObject";
import { HashObject, byteArrayIntoHashObject, byteArrayToHashObject, hashObjectToByteArray } from "./hashObject";
import SHA256 from "./sha256";
export { HashObject, byteArrayToHashObject, hashObjectToByteArray, SHA256 };
export { HashObject, byteArrayToHashObject, hashObjectToByteArray, byteArrayIntoHashObject, SHA256 };
export declare function digest(data: Uint8Array): Uint8Array;

@@ -14,2 +14,29 @@ export declare function digest64(data: Uint8Array): Uint8Array;

export declare function digest64HashObjects(obj1: HashObject, obj2: HashObject): HashObject;
/**
* Same to above but this set result to the output param to save memory.
*/
export declare function digest64HashObjectsInto(obj1: HashObject, obj2: HashObject, output: HashObject): void;
/**
* Hash 4 Uint8Array objects in parallel, each 64 length as below
*
* Inputs: i0 i1 i2 i3 i4 i5 i6 i7
* \ / \ / \ / \ /
* Outputs: o0 o1 o2 o3
*/
export declare function batchHash4UintArray64s(inputs: Uint8Array[]): Uint8Array[];
/**
* Hash 4 HashObject inputs in parallel
* - Each input (inputs{i}) is 4 bytes which make it 32 bytes
* - Each HashObject input contains 2 HashObjects which is 64 bytes similar to batchHash4UintArray64s
*
* Inputs i0 i1 i2 i3 i4 i5 i6 i7
* \ / \ / \ / \ /
* Outputs o0 o1 o2 o3
* // TODO - batch: support equivalent method to hash into
*/
export declare function batchHash4HashObjectInputs(inputs: HashObject[]): HashObject[];
/**
* Hash an input into preallocated input using batch if possible.
*/
export declare function hashInto(input: Uint8Array, output: Uint8Array): void;
//# sourceMappingURL=index.d.ts.map

@@ -6,5 +6,7 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.digest64HashObjects = exports.digest2Bytes32 = exports.digest64 = exports.digest = exports.SHA256 = exports.hashObjectToByteArray = exports.byteArrayToHashObject = void 0;
exports.hashInto = exports.batchHash4HashObjectInputs = exports.batchHash4UintArray64s = exports.digest64HashObjectsInto = exports.digest64HashObjects = exports.digest2Bytes32 = exports.digest64 = exports.digest = exports.SHA256 = exports.byteArrayIntoHashObject = exports.hashObjectToByteArray = exports.byteArrayToHashObject = void 0;
const alloc_1 = require("./alloc");
const wasm_1 = require("./wasm");
const hashObject_1 = require("./hashObject");
Object.defineProperty(exports, "byteArrayIntoHashObject", { enumerable: true, get: function () { return hashObject_1.byteArrayIntoHashObject; } });
Object.defineProperty(exports, "byteArrayToHashObject", { enumerable: true, get: function () { return hashObject_1.byteArrayToHashObject; } });

@@ -18,3 +20,5 @@ Object.defineProperty(exports, "hashObjectToByteArray", { enumerable: true, get: function () { return hashObject_1.hashObjectToByteArray; } });

const inputUint8Array = new Uint8Array(ctx.memory.buffer, wasmInputValue, ctx.INPUT_LENGTH);
const outputUint8Array = new Uint8Array(ctx.memory.buffer, wasmOutputValue, 32);
const outputUint8Array = new Uint8Array(ctx.memory.buffer, wasmOutputValue, ctx.PARALLEL_FACTOR * 32);
/** output uint8array, length 32, used to easily copy output data */
const outputUint8Array32 = new Uint8Array(ctx.memory.buffer, wasmOutputValue, 32);
const inputUint32Array = new Uint32Array(ctx.memory.buffer, wasmInputValue, ctx.INPUT_LENGTH);

@@ -28,5 +32,3 @@ function digest(data) {

ctx.digest(data.length);
const output = new Uint8Array(32);
output.set(outputUint8Array);
return output;
return allocDigest();
}

@@ -42,5 +44,3 @@ ctx.init();

ctx.digest64(wasmInputValue, wasmOutputValue);
const output = new Uint8Array(32);
output.set(outputUint8Array);
return output;
return allocDigest();
}

@@ -55,5 +55,3 @@ throw new Error("InvalidLengthForDigest64");

ctx.digest64(wasmInputValue, wasmOutputValue);
const output = new Uint8Array(32);
output.set(outputUint8Array);
return output;
return allocDigest();
}

@@ -70,2 +68,20 @@ throw new Error("InvalidLengthForDigest64");

function digest64HashObjects(obj1, obj2) {
const result = {
h0: 0,
h1: 0,
h2: 0,
h3: 0,
h4: 0,
h5: 0,
h6: 0,
h7: 0,
};
digest64HashObjectsInto(obj1, obj2, result);
return result;
}
exports.digest64HashObjects = digest64HashObjects;
/**
* Same to above but this set result to the output param to save memory.
*/
function digest64HashObjectsInto(obj1, obj2, output) {
// TODO: expect obj1 and obj2 as HashObject

@@ -90,5 +106,170 @@ inputUint32Array[0] = obj1.h0;

// extracting numbers from Uint32Array causes more memory
return hashObject_1.byteArrayToHashObject(outputUint8Array);
hashObject_1.byteArrayIntoHashObject(outputUint8Array, 0, output);
}
exports.digest64HashObjects = digest64HashObjects;
exports.digest64HashObjectsInto = digest64HashObjectsInto;
/**
* Hash 4 Uint8Array objects in parallel, each 64 length as below
*
* Inputs: i0 i1 i2 i3 i4 i5 i6 i7
* \ / \ / \ / \ /
* Outputs: o0 o1 o2 o3
*/
function batchHash4UintArray64s(inputs) {
if (inputs.length !== 4) {
throw new Error("Input length must be 4");
}
for (let i = 0; i < 4; i++) {
const input = inputs[i];
if (input == null) {
throw new Error(`Input ${i} is null or undefined`);
}
if (input.length !== 64) {
throw new Error(`Invalid length ${input.length} at input ${i}`);
}
}
// set up input buffer for v128
inputUint8Array.set(inputs[0], 0);
inputUint8Array.set(inputs[1], 64);
inputUint8Array.set(inputs[2], 128);
inputUint8Array.set(inputs[3], 192);
ctx.batchHash4UintArray64s(wasmOutputValue);
const output0 = allocDigest();
const output1 = allocDigestOffset(32);
const output2 = allocDigestOffset(64);
const output3 = allocDigestOffset(96);
return [output0, output1, output2, output3];
}
exports.batchHash4UintArray64s = batchHash4UintArray64s;
/**
* Hash 4 HashObject inputs in parallel
* - Each input (inputs{i}) is 4 bytes which make it 32 bytes
* - Each HashObject input contains 2 HashObjects which is 64 bytes similar to batchHash4UintArray64s
*
* Inputs i0 i1 i2 i3 i4 i5 i6 i7
* \ / \ / \ / \ /
* Outputs o0 o1 o2 o3
* // TODO - batch: support equivalent method to hash into
*/
function batchHash4HashObjectInputs(inputs) {
if (inputs.length !== 8) {
throw new Error("Input length must be 8");
}
// inputUint8Array is 256 bytes
// inputUint32Array is 64 items
// v128 0
inputUint32Array[0] = inputs[0].h0;
inputUint32Array[1] = inputs[2].h0;
inputUint32Array[2] = inputs[4].h0;
inputUint32Array[3] = inputs[6].h0;
// v128 1
inputUint32Array[4] = inputs[0].h1;
inputUint32Array[5] = inputs[2].h1;
inputUint32Array[6] = inputs[4].h1;
inputUint32Array[7] = inputs[6].h1;
// v128 2
inputUint32Array[8] = inputs[0].h2;
inputUint32Array[9] = inputs[2].h2;
inputUint32Array[10] = inputs[4].h2;
inputUint32Array[11] = inputs[6].h2;
// v128 3
inputUint32Array[12] = inputs[0].h3;
inputUint32Array[13] = inputs[2].h3;
inputUint32Array[14] = inputs[4].h3;
inputUint32Array[15] = inputs[6].h3;
// v128 4
inputUint32Array[16] = inputs[0].h4;
inputUint32Array[17] = inputs[2].h4;
inputUint32Array[18] = inputs[4].h4;
inputUint32Array[19] = inputs[6].h4;
// v128 5
inputUint32Array[20] = inputs[0].h5;
inputUint32Array[21] = inputs[2].h5;
inputUint32Array[22] = inputs[4].h5;
inputUint32Array[23] = inputs[6].h5;
// v128 6
inputUint32Array[24] = inputs[0].h6;
inputUint32Array[25] = inputs[2].h6;
inputUint32Array[26] = inputs[4].h6;
inputUint32Array[27] = inputs[6].h6;
// v128 7
inputUint32Array[28] = inputs[0].h7;
inputUint32Array[29] = inputs[2].h7;
inputUint32Array[30] = inputs[4].h7;
inputUint32Array[31] = inputs[6].h7;
// v128 8
inputUint32Array[32] = inputs[1].h0;
inputUint32Array[33] = inputs[3].h0;
inputUint32Array[34] = inputs[5].h0;
inputUint32Array[35] = inputs[7].h0;
// v128 9
inputUint32Array[36] = inputs[1].h1;
inputUint32Array[37] = inputs[3].h1;
inputUint32Array[38] = inputs[5].h1;
inputUint32Array[39] = inputs[7].h1;
// v128 10
inputUint32Array[40] = inputs[1].h2;
inputUint32Array[41] = inputs[3].h2;
inputUint32Array[42] = inputs[5].h2;
inputUint32Array[43] = inputs[7].h2;
// v128 11
inputUint32Array[44] = inputs[1].h3;
inputUint32Array[45] = inputs[3].h3;
inputUint32Array[46] = inputs[5].h3;
inputUint32Array[47] = inputs[7].h3;
// v128 12
inputUint32Array[48] = inputs[1].h4;
inputUint32Array[49] = inputs[3].h4;
inputUint32Array[50] = inputs[5].h4;
inputUint32Array[51] = inputs[7].h4;
// v128 13
inputUint32Array[52] = inputs[1].h5;
inputUint32Array[53] = inputs[3].h5;
inputUint32Array[54] = inputs[5].h5;
inputUint32Array[55] = inputs[7].h5;
// v128 14
inputUint32Array[56] = inputs[1].h6;
inputUint32Array[57] = inputs[3].h6;
inputUint32Array[58] = inputs[5].h6;
inputUint32Array[59] = inputs[7].h6;
// v128 15
inputUint32Array[60] = inputs[1].h7;
inputUint32Array[61] = inputs[3].h7;
inputUint32Array[62] = inputs[5].h7;
inputUint32Array[63] = inputs[7].h7;
ctx.batchHash4HashObjectInputs(wasmOutputValue);
const output0 = hashObject_1.byteArrayToHashObject(outputUint8Array, 0);
const output1 = hashObject_1.byteArrayToHashObject(outputUint8Array, 32);
const output2 = hashObject_1.byteArrayToHashObject(outputUint8Array, 64);
const output3 = hashObject_1.byteArrayToHashObject(outputUint8Array, 96);
return [output0, output1, output2, output3];
}
exports.batchHash4HashObjectInputs = batchHash4HashObjectInputs;
/**
* Hash an input into preallocated input using batch if possible.
*/
function hashInto(input, output) {
if (input.length % 64 !== 0) {
throw new Error(`Invalid input length ${input.length}`);
}
if (input.length !== output.length * 2) {
throw new Error(`Invalid output length ${output.length}`);
}
// for every 64 x 4 = 256 bytes, do the batch hash
const endBatch = Math.floor(input.length / 256);
for (let i = 0; i < endBatch; i++) {
inputUint8Array.set(input.subarray(i * 256, (i + 1) * 256), 0);
ctx.batchHash4UintArray64s(wasmOutputValue);
output.set(outputUint8Array.subarray(0, 128), i * 128);
}
const numHashed = endBatch * 4;
const remainingHash = Math.floor((input.length % 256) / 64);
const inputOffset = numHashed * 64;
const outputOffset = numHashed * 32;
for (let i = 0; i < remainingHash; i++) {
inputUint8Array.set(input.subarray(inputOffset + i * 64, inputOffset + (i + 1) * 64), 0);
ctx.digest64(wasmInputValue, wasmOutputValue);
output.set(outputUint8Array.subarray(0, 32), outputOffset + i * 32);
}
}
exports.hashInto = hashInto;
function update(data) {

@@ -98,3 +279,3 @@ const INPUT_LENGTH = ctx.INPUT_LENGTH;

for (let i = 0; i < data.length; i += INPUT_LENGTH) {
const sliced = data.slice(i, i + INPUT_LENGTH);
const sliced = data.subarray(i, i + INPUT_LENGTH);
inputUint8Array.set(sliced);

@@ -111,6 +292,16 @@ ctx.update(wasmInputValue, sliced.length);

ctx.final(wasmOutputValue);
const output = new Uint8Array(32);
output.set(outputUint8Array);
return output;
return allocDigest();
}
/** allocate memory and copy result */
function allocDigest() {
const out = alloc_1.allocUnsafe(32);
out.set(outputUint8Array32);
return out;
}
/** allocate memory and copy result at offset */
function allocDigestOffset(offset) {
const out = alloc_1.allocUnsafe(32);
out.set(outputUint8Array.subarray(offset, offset + 32));
return out;
}
//# sourceMappingURL=index.js.map
export interface WasmContext {
readonly INPUT_LENGTH: number;
readonly PARALLEL_FACTOR: number;
memory: {

@@ -17,4 +18,6 @@ buffer: ArrayBuffer;

digest64(inPtr: number, outPtr: number): void;
batchHash4UintArray64s(outPtr: number): void;
batchHash4HashObjectInputs(outPtr: number): void;
}
export declare function newInstance(): WasmContext;
//# sourceMappingURL=wasm.d.ts.map
{
"name": "@chainsafe/as-sha256",
"version": "0.4.2",
"version": "0.5.0",
"description": "An AssemblyScript implementation of SHA256",

@@ -31,13 +31,16 @@ "author": "ChainSafe Systems",

"scripts": {
"clean": "rm -rf ./dist",
"lint": "echo 'no linting for this package'",
"check-types": "echo 'no type check for this package'",
"generate": "rm -rf ./dist && node -r ts-node/register ./scripts/codegen.ts",
"build": "yarn asbuild:untouched && yarn asbuild:optimized && yarn build:lib",
"asbuild:untouched": "asc assembly/index.ts -o build/untouched.wasm -t build/untouched.wat --runtime minimal --target debug",
"asbuild:optimized": "asc assembly/index.ts -o build/optimized.wasm -t build/optimized.wat --runtime minimal --target release -O3z --noAssert",
"build:lib": "run -T tsc -p tsconfig.build.json",
"build:web": "run -T webpack --mode production --entry ./index.js --output ./dist/as-sha256.min.js",
"asbuild:untouched": "asc assembly/index.ts -o build/untouched.wasm -t build/untouched.wat --runtime minimal --target debug --enable simd",
"asbuild:optimized": "asc assembly/index.ts -o build/optimized.wasm -t build/optimized.wat --runtime minimal --target release -O3z --noAssert --enable simd",
"build:lib": "tsc -p tsconfig.build.json",
"build:web": "webpack --mode production --entry ./index.js --output ./dist/as-sha256.min.js",
"test": "yarn run test:unit",
"test:unit": "yarn run test:unit:node && yarn run test:unit:browser",
"test:unit:node": "run -T mocha -r ts-node/register test/unit/*.test.ts",
"test:unit:browser": "run -T karma start --single-run --browsers ChromeHeadless,FirefoxHeadless karma.config.js",
"benchmark": "node -r ts-node/register ./node_modules/.bin/benchmark 'test/perf/index.test.ts'",
"test:unit:node": "mocha -r ts-node/register test/unit/*.test.ts",
"test:unit:browser": "karma start --single-run --browsers ChromeHeadless,FirefoxHeadless karma.config.js",
"benchmark": "node -r ts-node/register ./node_modules/.bin/benchmark 'test/perf/*.test.ts'",
"benchmark:local": "yarn benchmark --local",

@@ -51,2 +54,2 @@ "test:ci": "yarn test:as-ci"

}
}
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc