diff --git a/src/byte_utils.ts b/src/byte_utils.ts deleted file mode 100644 index 2e7e52b..0000000 --- a/src/byte_utils.ts +++ /dev/null @@ -1,63 +0,0 @@ -// Adapted from https://github.com/feross/buffer - -function checkInt( - buf: Uint8Array, - value: number, - offset: number, - ext: number, - max: number, - min: number -): void { - if (value > max || value < min) - throw new RangeError('"value" argument is out of bounds'); - if (offset + ext > buf.length) throw new RangeError("Index out of range"); -} - -export function writeUIntLE( - buf: Uint8Array, - value: number, - offset: number, - byteLength: number, - noAssert?: boolean -): Uint8Array { - value = +value; - offset = offset >>> 0; - byteLength = byteLength >>> 0; - if (!noAssert) { - const maxBytes = Math.pow(2, 8 * byteLength) - 1; - checkInt(buf, value, offset, byteLength, maxBytes, 0); - } - - let mul = 1; - let i = 0; - buf[offset] = value & 0xff; - while (++i < byteLength && (mul *= 0x100)) { - buf[offset + i] = (value / mul) & 0xff; - } - - return buf; -} - -/** - * Transforms Uint8Array into BigInt - * @param array: Uint8Array - * @returns BigInt - */ -export function buildBigIntFromUint8Array(array: Uint8Array): bigint { - const dataView = new DataView(array.buffer); - return dataView.getBigUint64(0, true); -} - -/** - * Fills with zeros to set length - * @param array little endian Uint8Array - * @param length amount to pad - * @returns little endian Uint8Array padded with zeros to set length - */ -export function zeroPadLE(array: Uint8Array, length: number): Uint8Array { - const result = new Uint8Array(length); - for (let i = 0; i < length; i++) { - result[i] = array[i] || 0; - } - return result; -} diff --git a/src/contract/rln_contract.ts b/src/contract/rln_contract.ts index b01ac33..4a8759c 100644 --- a/src/contract/rln_contract.ts +++ b/src/contract/rln_contract.ts @@ -1,7 +1,8 @@ import { hexToBytes } from "@waku/utils/bytes"; import { ethers } from "ethers"; -import { zeroPadLE } from "./byte_utils.js"; +import { zeroPadLE } from "../utils/index.js"; + import { RLN_REGISTRY_ABI, RLN_STORAGE_ABI } from "./constants.js"; import type { DecryptedCredentials } from "./keystore/index.js"; import { type IdentityCredential, RLNInstance } from "./rln.js"; diff --git a/src/rln.ts b/src/rln.ts index 7569d03..f33af94 100644 --- a/src/rln.ts +++ b/src/rln.ts @@ -9,7 +9,6 @@ import init from "@waku/zerokit-rln-wasm"; import * as zerokitRLN from "@waku/zerokit-rln-wasm"; import { ethers } from "ethers"; -import { buildBigIntFromUint8Array, writeUIntLE } from "./byte_utils.js"; import type { RLNDecoder, RLNEncoder } from "./codec.js"; import { createRLNDecoder, createRLNEncoder } from "./codec.js"; import { SEPOLIA_CONTRACT } from "./constants.js"; @@ -22,6 +21,11 @@ import type { } from "./keystore/index.js"; import { KeystoreEntity, Password } from "./keystore/types.js"; import verificationKey from "./resources/verification_key.js"; +import { + buildBigIntFromUint8Array, + poseidonHash, + writeUIntLE, +} from "./utils/index.js"; import { concatenate, extractMetaMaskSigner } from "./utils/index.js"; import * as wc from "./witness_calculator.js"; import { WitnessCalculator } from "./witness_calculator.js"; @@ -147,18 +151,6 @@ export function proofToBytes(p: IRateLimitProof): Uint8Array { ); } -export function poseidonHash(...input: Array): Uint8Array { - const inputLen = writeUIntLE(new Uint8Array(8), input.length, 0, 8); - const lenPrefixedData = concatenate(inputLen, ...input); - return zerokitRLN.poseidonHash(lenPrefixedData); -} - -export function sha256(input: Uint8Array): Uint8Array { - const inputLen = writeUIntLE(new Uint8Array(8), input.length, 0, 8); - const lenPrefixedData = concatenate(inputLen, input); - return zerokitRLN.hash(lenPrefixedData); -} - type StartRLNOptions = { /** * If not set - will extract MetaMask account and get signer from it. diff --git a/src/utils/bytes.ts b/src/utils/bytes.ts index bc00ff0..000488c 100644 --- a/src/utils/bytes.ts +++ b/src/utils/bytes.ts @@ -16,3 +16,66 @@ export function concatenate(...input: Uint8Array[]): Uint8Array { } return result; } + +// Adapted from https://github.com/feross/buffer +function checkInt( + buf: Uint8Array, + value: number, + offset: number, + ext: number, + max: number, + min: number +): void { + if (value > max || value < min) + throw new RangeError('"value" argument is out of bounds'); + if (offset + ext > buf.length) throw new RangeError("Index out of range"); +} + +export function writeUIntLE( + buf: Uint8Array, + value: number, + offset: number, + byteLength: number, + noAssert?: boolean +): Uint8Array { + value = +value; + offset = offset >>> 0; + byteLength = byteLength >>> 0; + if (!noAssert) { + const maxBytes = Math.pow(2, 8 * byteLength) - 1; + checkInt(buf, value, offset, byteLength, maxBytes, 0); + } + + let mul = 1; + let i = 0; + buf[offset] = value & 0xff; + while (++i < byteLength && (mul *= 0x100)) { + buf[offset + i] = (value / mul) & 0xff; + } + + return buf; +} + +/** + * Transforms Uint8Array into BigInt + * @param array: Uint8Array + * @returns BigInt + */ +export function buildBigIntFromUint8Array(array: Uint8Array): bigint { + const dataView = new DataView(array.buffer); + return dataView.getBigUint64(0, true); +} + +/** + * Fills with zeros to set length + * @param array little endian Uint8Array + * @param length amount to pad + * @returns little endian Uint8Array padded with zeros to set length + */ +export function zeroPadLE(array: Uint8Array, length: number): Uint8Array { + const result = new Uint8Array(length); + for (let i = 0; i < length; i++) { + result[i] = array[i] || 0; + } + return result; +} diff --git a/src/utils/hash.ts b/src/utils/hash.ts new file mode 100644 index 0000000..78422e2 --- /dev/null +++ b/src/utils/hash.ts @@ -0,0 +1,15 @@ +import * as zerokitRLN from "@waku/zerokit-rln-wasm"; + +import { concatenate, writeUIntLE } from "./bytes.js"; + +export function poseidonHash(...input: Array): Uint8Array { + const inputLen = writeUIntLE(new Uint8Array(8), input.length, 0, 8); + const lenPrefixedData = concatenate(inputLen, ...input); + return zerokitRLN.poseidonHash(lenPrefixedData); +} + +export function sha256(input: Uint8Array): Uint8Array { + const inputLen = writeUIntLE(new Uint8Array(8), input.length, 0, 8); + const lenPrefixedData = concatenate(inputLen, input); + return zerokitRLN.hash(lenPrefixedData); +} diff --git a/src/utils/index.ts b/src/utils/index.ts index 2aebf9e..f0faaab 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,2 +1,8 @@ export { extractMetaMaskSigner } from "./metamask.js"; -export { concatenate } from "./bytes.js"; +export { + concatenate, + writeUIntLE, + buildBigIntFromUint8Array, + zeroPadLE, +} from "./bytes.js"; +export { sha256, poseidonHash } from "./hash.js";