@chainsafe/as-sha256
Advanced tools
Comparing version 0.5.0 to 0.6.0
@@ -9,2 +9,1 @@ /** | ||
export {}; | ||
//# sourceMappingURL=alloc.d.ts.map |
@@ -1,4 +0,1 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.allocUnsafe = void 0; | ||
const isNode = typeof process !== "undefined" && process.versions != null && process.versions.node != null; | ||
@@ -10,3 +7,3 @@ /** | ||
*/ | ||
exports.allocUnsafe = isNode ? _allocUnsafeNode : _allocUnsafe; | ||
export const allocUnsafe = isNode ? _allocUnsafeNode : _allocUnsafe; | ||
function _allocUnsafe(size = 0) { | ||
@@ -13,0 +10,0 @@ return new Uint8Array(size); |
@@ -32,2 +32,1 @@ /** | ||
export declare function byteArrayIntoHashObject(byteArr: Uint8Array, offset: number, output: HashObject): void; | ||
//# sourceMappingURL=hashObject.d.ts.map |
@@ -1,4 +0,1 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.byteArrayIntoHashObject = exports.byteArrayToHashObject = exports.hashObjectToByteArray = void 0; | ||
/** | ||
@@ -9,3 +6,3 @@ * Pass 8 numbers in an object and set that to inputArray. | ||
**/ | ||
function hashObjectToByteArray(obj, byteArr, offset) { | ||
export function hashObjectToByteArray(obj, byteArr, offset) { | ||
let tmp = obj.h0; | ||
@@ -76,3 +73,2 @@ byteArr[0 + offset] = tmp & 0xff; | ||
} | ||
exports.hashObjectToByteArray = hashObjectToByteArray; | ||
/** | ||
@@ -84,3 +80,3 @@ * Parse outputArray into an object of 8 numbers. | ||
**/ | ||
function byteArrayToHashObject(byteArr, offset) { | ||
export function byteArrayToHashObject(byteArr, offset) { | ||
const result = { | ||
@@ -99,7 +95,6 @@ h0: 0, | ||
} | ||
exports.byteArrayToHashObject = byteArrayToHashObject; | ||
/** | ||
* Same to above but this set result to the output param to save memory. | ||
*/ | ||
function byteArrayIntoHashObject(byteArr, offset, output) { | ||
export function byteArrayIntoHashObject(byteArr, offset, output) { | ||
let tmp = 0; | ||
@@ -178,3 +173,2 @@ tmp |= byteArr[3 + offset] & 0xff; | ||
} | ||
exports.byteArrayIntoHashObject = byteArrayIntoHashObject; | ||
//# sourceMappingURL=hashObject.js.map |
@@ -1,4 +0,7 @@ | ||
import { HashObject, byteArrayIntoHashObject, byteArrayToHashObject, hashObjectToByteArray } from "./hashObject"; | ||
import SHA256 from "./sha256"; | ||
import type { HashObject } from "./hashObject.js"; | ||
import { byteArrayIntoHashObject, byteArrayToHashObject, hashObjectToByteArray } from "./hashObject.js"; | ||
import SHA256 from "./sha256.js"; | ||
export { HashObject, byteArrayToHashObject, hashObjectToByteArray, byteArrayIntoHashObject, SHA256 }; | ||
export declare let simdEnabled: boolean; | ||
export declare function reinitializeInstance(useSimd?: boolean): void; | ||
export declare function digest(data: Uint8Array): Uint8Array; | ||
@@ -41,2 +44,1 @@ export declare function digest64(data: Uint8Array): Uint8Array; | ||
export declare function hashInto(input: Uint8Array, output: Uint8Array): void; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,24 +0,27 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
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; } }); | ||
Object.defineProperty(exports, "hashObjectToByteArray", { enumerable: true, get: function () { return hashObject_1.hashObjectToByteArray; } }); | ||
const sha256_1 = __importDefault(require("./sha256")); | ||
exports.SHA256 = sha256_1.default; | ||
const ctx = wasm_1.newInstance(); | ||
const wasmInputValue = ctx.input.value; | ||
const wasmOutputValue = ctx.output.value; | ||
const inputUint8Array = new Uint8Array(ctx.memory.buffer, wasmInputValue, ctx.INPUT_LENGTH); | ||
const outputUint8Array = new Uint8Array(ctx.memory.buffer, wasmOutputValue, ctx.PARALLEL_FACTOR * 32); | ||
import { allocUnsafe } from "./alloc.js"; | ||
import { newInstance } from "./wasm.js"; | ||
import { byteArrayIntoHashObject, byteArrayToHashObject, hashObjectToByteArray } from "./hashObject.js"; | ||
import SHA256 from "./sha256.js"; | ||
export { byteArrayToHashObject, hashObjectToByteArray, byteArrayIntoHashObject, SHA256 }; | ||
let ctx; | ||
export let simdEnabled; | ||
let wasmInputValue; | ||
let wasmOutputValue; | ||
let inputUint8Array; | ||
let outputUint8Array; | ||
/** 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); | ||
function digest(data) { | ||
let outputUint8Array32; | ||
let inputUint32Array; | ||
export function reinitializeInstance(useSimd) { | ||
ctx = newInstance(useSimd); | ||
simdEnabled = Boolean(ctx.HAS_SIMD.valueOf()); | ||
wasmInputValue = ctx.input.value; | ||
wasmOutputValue = ctx.output.value; | ||
inputUint8Array = new Uint8Array(ctx.memory.buffer, wasmInputValue, ctx.INPUT_LENGTH); | ||
outputUint8Array = new Uint8Array(ctx.memory.buffer, wasmOutputValue, ctx.PARALLEL_FACTOR * 32); | ||
outputUint8Array32 = new Uint8Array(ctx.memory.buffer, wasmOutputValue, 32); | ||
inputUint32Array = new Uint32Array(ctx.memory.buffer, wasmInputValue, ctx.INPUT_LENGTH); | ||
} | ||
reinitializeInstance(); | ||
export function digest(data) { | ||
if (data.length === 64) { | ||
@@ -36,4 +39,3 @@ return digest64(data); | ||
} | ||
exports.digest = digest; | ||
function digest64(data) { | ||
export function digest64(data) { | ||
if (data.length === 64) { | ||
@@ -46,4 +48,3 @@ inputUint8Array.set(data); | ||
} | ||
exports.digest64 = digest64; | ||
function digest2Bytes32(bytes1, bytes2) { | ||
export function digest2Bytes32(bytes1, bytes2) { | ||
if (bytes1.length === 32 && bytes2.length === 32) { | ||
@@ -57,3 +58,2 @@ inputUint8Array.set(bytes1); | ||
} | ||
exports.digest2Bytes32 = digest2Bytes32; | ||
/** | ||
@@ -65,3 +65,3 @@ * Digest 2 objects, each has 8 properties from h0 to h7. | ||
*/ | ||
function digest64HashObjects(obj1, obj2) { | ||
export function digest64HashObjects(obj1, obj2) { | ||
const result = { | ||
@@ -80,7 +80,6 @@ h0: 0, | ||
} | ||
exports.digest64HashObjects = digest64HashObjects; | ||
/** | ||
* Same to above but this set result to the output param to save memory. | ||
*/ | ||
function digest64HashObjectsInto(obj1, obj2, output) { | ||
export function digest64HashObjectsInto(obj1, obj2, output) { | ||
// TODO: expect obj1 and obj2 as HashObject | ||
@@ -105,5 +104,4 @@ inputUint32Array[0] = obj1.h0; | ||
// extracting numbers from Uint32Array causes more memory | ||
hashObject_1.byteArrayIntoHashObject(outputUint8Array, 0, output); | ||
byteArrayIntoHashObject(outputUint8Array, 0, output); | ||
} | ||
exports.digest64HashObjectsInto = digest64HashObjectsInto; | ||
/** | ||
@@ -116,3 +114,3 @@ * Hash 4 Uint8Array objects in parallel, each 64 length as below | ||
*/ | ||
function batchHash4UintArray64s(inputs) { | ||
export function batchHash4UintArray64s(inputs) { | ||
if (inputs.length !== 4) { | ||
@@ -142,3 +140,2 @@ throw new Error("Input length must be 4"); | ||
} | ||
exports.batchHash4UintArray64s = batchHash4UintArray64s; | ||
/** | ||
@@ -154,3 +151,3 @@ * Hash 4 HashObject inputs in parallel | ||
*/ | ||
function batchHash4HashObjectInputs(inputs) { | ||
export function batchHash4HashObjectInputs(inputs) { | ||
if (inputs.length !== 8) { | ||
@@ -242,13 +239,12 @@ throw new Error("Input length must be 8"); | ||
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); | ||
const output0 = byteArrayToHashObject(outputUint8Array, 0); | ||
const output1 = byteArrayToHashObject(outputUint8Array, 32); | ||
const output2 = byteArrayToHashObject(outputUint8Array, 64); | ||
const output3 = 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) { | ||
export function hashInto(input, output) { | ||
if (input.length % 64 !== 0) { | ||
@@ -277,3 +273,2 @@ throw new Error(`Invalid input length ${input.length}`); | ||
} | ||
exports.hashInto = hashInto; | ||
function update(data) { | ||
@@ -297,11 +292,15 @@ const INPUT_LENGTH = ctx.INPUT_LENGTH; | ||
} | ||
/** allocate memory and copy result */ | ||
/** | ||
* allocate memory and copy result | ||
*/ | ||
function allocDigest() { | ||
const out = alloc_1.allocUnsafe(32); | ||
const out = allocUnsafe(32); | ||
out.set(outputUint8Array32); | ||
return out; | ||
} | ||
/** allocate memory and copy result at offset */ | ||
/** | ||
* allocate memory and copy result at offset | ||
*/ | ||
function allocDigestOffset(offset) { | ||
const out = alloc_1.allocUnsafe(32); | ||
const out = allocUnsafe(32); | ||
out.set(outputUint8Array.subarray(offset, offset + 32)); | ||
@@ -308,0 +307,0 @@ return out; |
@@ -1,2 +0,2 @@ | ||
import { WasmContext } from "./wasm"; | ||
import { WasmContext } from "./wasm.js"; | ||
/** | ||
@@ -16,2 +16,1 @@ * Class based SHA256 | ||
} | ||
//# sourceMappingURL=sha256.d.ts.map |
@@ -1,10 +0,13 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const wasm_1 = require("./wasm"); | ||
import { newInstance } from "./wasm.js"; | ||
/** | ||
* Class based SHA256 | ||
*/ | ||
class SHA256 { | ||
export default class SHA256 { | ||
ctx; | ||
wasmInputValue; | ||
wasmOutputValue; | ||
uint8InputArray; | ||
uint8OutputArray; | ||
constructor() { | ||
this.ctx = wasm_1.newInstance(); | ||
this.ctx = newInstance(); | ||
this.wasmInputValue = this.ctx.input.value; | ||
@@ -41,3 +44,2 @@ this.wasmOutputValue = this.ctx.output.value; | ||
} | ||
exports.default = SHA256; | ||
//# sourceMappingURL=sha256.js.map |
export interface WasmContext { | ||
readonly HAS_SIMD: boolean; | ||
readonly PARALLEL_FACTOR: number; | ||
readonly INPUT_LENGTH: number; | ||
readonly PARALLEL_FACTOR: number; | ||
memory: { | ||
@@ -21,3 +22,2 @@ buffer: ArrayBuffer; | ||
} | ||
export declare function newInstance(): WasmContext; | ||
//# sourceMappingURL=wasm.d.ts.map | ||
export declare function newInstance(useSimd?: boolean): WasmContext; |
@@ -1,6 +0,3 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.newInstance = void 0; | ||
const wasmCode_1 = require("./wasmCode"); | ||
const _module = new WebAssembly.Module(wasmCode_1.wasmCode); | ||
import { wasmCode } from "./wasmCode.js"; | ||
import { wasmSimdCode } from "./wasmSimdCode.js"; | ||
const importObj = { | ||
@@ -14,6 +11,8 @@ env: { | ||
}; | ||
function newInstance() { | ||
return new WebAssembly.Instance(_module, importObj).exports; | ||
export function newInstance(useSimd) { | ||
const enableSimd = useSimd !== undefined ? useSimd : WebAssembly.validate(wasmSimdCode); | ||
return enableSimd | ||
? new WebAssembly.Instance(new WebAssembly.Module(wasmSimdCode), importObj).exports | ||
: new WebAssembly.Instance(new WebAssembly.Module(wasmCode), importObj).exports; | ||
} | ||
exports.newInstance = newInstance; | ||
//# sourceMappingURL=wasm.js.map |
export declare const wasmCode: Uint8Array; | ||
//# sourceMappingURL=wasmCode.d.ts.map |
{ | ||
"name": "@chainsafe/as-sha256", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"description": "An AssemblyScript implementation of SHA256", | ||
@@ -8,20 +8,13 @@ "author": "ChainSafe Systems", | ||
"bugs": { | ||
"url": "https://github.com/ChainSafe/as-sha256/issues" | ||
"url": "https://github.com/ChainSafe/ssz/issues" | ||
}, | ||
"homepage": "https://github.com/ChainSafe/as-sha256#readme", | ||
"homepage": "https://github.com/ChainSafe/ssz/tree/master/packages/as-sha256/#readme", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/chainsafe/as-sha256.git" | ||
"url": "git+https://github.com/chainsafe/ssz.git" | ||
}, | ||
"main": "lib/index.js", | ||
"typesVersions": { | ||
"*": { | ||
"*": [ | ||
"*", | ||
"lib/*", | ||
"lib/*/index" | ||
] | ||
} | ||
}, | ||
"types": "lib/index.d.ts", | ||
"type": "module", | ||
"main": "./lib/cjs/index.js", | ||
"module": "./lib/index.js", | ||
"types": "./lib/index.d.ts", | ||
"files": [ | ||
@@ -34,13 +27,20 @@ "lib", | ||
"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 --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", | ||
"check-types": "tsc --noEmit", | ||
"generate": "rm -rf ./dist && node --loader ts-node/esm ./scripts/codegen.ts", | ||
"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", | ||
"asbuild": "yarn asbuild:untouched && yarn asbuild:optimized", | ||
"asbuild:simd:untouched": "asc assembly/index.simd.ts -o build/untouched.simd.wasm -t build/untouched.simd.wat --runtime minimal --target debug --enable simd", | ||
"asbuild:simd:optimized": "asc assembly/index.simd.ts -o build/optimized.simd.wasm -t build/optimized.simd.wat --runtime minimal --target release -O3z --noAssert --enable simd", | ||
"asbuild:simd": "yarn asbuild:simd:untouched && yarn asbuild:simd:optimized", | ||
"build:esm": "tsc -p tsconfig.build.esm.json", | ||
"build:cjs": "tsc -p tsconfig.build.cjs.json && echo '{\"type\": \"commonjs\"}' > ./lib/cjs/package.json", | ||
"build:types": "tsc -p tsconfig.build.types.json", | ||
"build:web": "webpack --mode production --entry ./index.js --output ./dist/as-sha256.min.js", | ||
"build": "yarn asbuild && yarn asbuild:simd && yarn generate && yarn build:cjs && yarn build:esm && yarn build:types", | ||
"test": "yarn run test:unit", | ||
"test:unit": "yarn run test:unit:node && yarn run test:unit:browser", | ||
"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", | ||
"test:unit": "vitest run --dir test/unit/", | ||
"test:browsers": "yarn test:browsers:chrome && yarn test:browsers:firefox", | ||
"test:browsers:chrome": "vitest run --browser chrome --config ./vitest.browser.config.ts --dir test/unit", | ||
"test:browsers:firefox": "vitest run --browser firefox --config ./vitest.browser.config.ts --dir test/unit", | ||
"benchmark": "node -r ts-node/register ./node_modules/.bin/benchmark 'test/perf/*.test.ts'", | ||
@@ -55,2 +55,2 @@ "benchmark:local": "yarn benchmark --local", | ||
} | ||
} | ||
} |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
1668244
75
2056
0
Yes