@iden3/js-crypto
Advanced tools
Comparing version 1.0.0-beta.0 to 1.0.0-beta.1
@@ -59,4 +59,9 @@ "use strict"; | ||
} | ||
// HashBytes returns a sponge hash of a msg byte slice split into blocks of 31 bytes | ||
static hashBytes(msg) { | ||
const inputs = new Array(SPONGE_INPUTS).fill(BigInt(0)); | ||
return Poseidon.hashBytesX(msg, SPONGE_INPUTS); | ||
} | ||
// hashBytesX returns a sponge hash of a msg byte slice split into blocks of 31 bytes | ||
static hashBytesX(msg, frameSize) { | ||
const inputs = new Array(frameSize).fill(BigInt(0)); | ||
let dirty = false; | ||
@@ -68,8 +73,8 @@ let hash; | ||
inputs[k] = ff_1.utils.beBuff2int(msg.slice(SPONGE_CHUNK_SIZE * i, SPONGE_CHUNK_SIZE * (i + 1))); | ||
if (k === SPONGE_INPUTS - 1) { | ||
if (k === frameSize - 1) { | ||
hash = Poseidon.hash(inputs); | ||
dirty = false; | ||
inputs[0] = hash.valueOf(); | ||
inputs[0] = hash; | ||
inputs.fill(BigInt(0), 1, SPONGE_CHUNK_SIZE); | ||
for (let j = 1; j < SPONGE_INPUTS; j += 1) { | ||
for (let j = 1; j < frameSize; j += 1) { | ||
inputs[j] = BigInt(0); | ||
@@ -96,5 +101,37 @@ } | ||
} | ||
// @ts-ignore: if we reach here then hash should be assigned value | ||
return hash.valueOf(); | ||
return hash; | ||
} | ||
// SpongeHashX returns a sponge hash of inputs using Poseidon with configurable frame size | ||
static spongeHashX(inputs, frameSize) { | ||
if (frameSize < 2 || frameSize > 16) { | ||
throw new Error('incorrect frame size'); | ||
} | ||
// not used frame default to zero | ||
let frame = new Array(frameSize).fill(BigInt(0)); | ||
let dirty = false; | ||
let hash; | ||
let k = 0; | ||
for (let i = 0; i < inputs.length; i++) { | ||
dirty = true; | ||
frame[k] = inputs[i]; | ||
if (k === frameSize - 1) { | ||
hash = this.hash(frame); | ||
dirty = false; | ||
frame = new Array(frameSize).fill(BigInt(0)); | ||
frame[0] = hash; | ||
k = 1; | ||
} | ||
else { | ||
k++; | ||
} | ||
} | ||
if (dirty) { | ||
// we haven't hashed something in the main sponge loop and need to do hash here | ||
hash = this.hash(frame); | ||
} | ||
if (!hash) { | ||
throw new Error('hash is undefined'); | ||
} | ||
return hash; | ||
} | ||
} | ||
@@ -101,0 +138,0 @@ exports.Poseidon = Poseidon; |
@@ -53,4 +53,9 @@ import { F1Field, Scalar, utils } from '../ff'; | ||
} | ||
// HashBytes returns a sponge hash of a msg byte slice split into blocks of 31 bytes | ||
static hashBytes(msg) { | ||
const inputs = new Array(SPONGE_INPUTS).fill(BigInt(0)); | ||
return Poseidon.hashBytesX(msg, SPONGE_INPUTS); | ||
} | ||
// hashBytesX returns a sponge hash of a msg byte slice split into blocks of 31 bytes | ||
static hashBytesX(msg, frameSize) { | ||
const inputs = new Array(frameSize).fill(BigInt(0)); | ||
let dirty = false; | ||
@@ -62,8 +67,8 @@ let hash; | ||
inputs[k] = utils.beBuff2int(msg.slice(SPONGE_CHUNK_SIZE * i, SPONGE_CHUNK_SIZE * (i + 1))); | ||
if (k === SPONGE_INPUTS - 1) { | ||
if (k === frameSize - 1) { | ||
hash = Poseidon.hash(inputs); | ||
dirty = false; | ||
inputs[0] = hash.valueOf(); | ||
inputs[0] = hash; | ||
inputs.fill(BigInt(0), 1, SPONGE_CHUNK_SIZE); | ||
for (let j = 1; j < SPONGE_INPUTS; j += 1) { | ||
for (let j = 1; j < frameSize; j += 1) { | ||
inputs[j] = BigInt(0); | ||
@@ -90,5 +95,37 @@ } | ||
} | ||
// @ts-ignore: if we reach here then hash should be assigned value | ||
return hash.valueOf(); | ||
return hash; | ||
} | ||
// SpongeHashX returns a sponge hash of inputs using Poseidon with configurable frame size | ||
static spongeHashX(inputs, frameSize) { | ||
if (frameSize < 2 || frameSize > 16) { | ||
throw new Error('incorrect frame size'); | ||
} | ||
// not used frame default to zero | ||
let frame = new Array(frameSize).fill(BigInt(0)); | ||
let dirty = false; | ||
let hash; | ||
let k = 0; | ||
for (let i = 0; i < inputs.length; i++) { | ||
dirty = true; | ||
frame[k] = inputs[i]; | ||
if (k === frameSize - 1) { | ||
hash = this.hash(frame); | ||
dirty = false; | ||
frame = new Array(frameSize).fill(BigInt(0)); | ||
frame[0] = hash; | ||
k = 1; | ||
} | ||
else { | ||
k++; | ||
} | ||
} | ||
if (dirty) { | ||
// we haven't hashed something in the main sponge loop and need to do hash here | ||
hash = this.hash(frame); | ||
} | ||
if (!hash) { | ||
throw new Error('hash is undefined'); | ||
} | ||
return hash; | ||
} | ||
} | ||
@@ -95,0 +132,0 @@ Poseidon.F = F; |
@@ -6,3 +6,5 @@ export declare const OPT: any; | ||
static hashBytes(msg: Uint8Array): bigint; | ||
static hashBytesX(msg: Uint8Array, frameSize: number): bigint; | ||
static spongeHashX(inputs: bigint[], frameSize: number): bigint; | ||
} | ||
export declare const poseidon: typeof Poseidon; |
{ | ||
"name": "@iden3/js-crypto", | ||
"version": "1.0.0-beta.0", | ||
"version": "1.0.0-beta.1", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs/index.js", |
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
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
11394350
102207