mirror of
https://github.com/logos-messaging/js-waku.git
synced 2026-01-05 23:33:08 +00:00
fix: use viem for bytes32/bigint conversion
This commit is contained in:
parent
a20fac086e
commit
ed996117d8
@ -77,14 +77,14 @@ describe("RLN Proof Integration Tests", function () {
|
|||||||
BytesUtils.bytes32FromBigInt(element, "little")
|
BytesUtils.bytes32FromBigInt(element, "little")
|
||||||
),
|
),
|
||||||
proofElementIndexes.map((index) =>
|
proofElementIndexes.map((index) =>
|
||||||
BytesUtils.writeUIntLE(new Uint8Array(1), index, 0, 1)
|
BytesUtils.writeUintLE(new Uint8Array(1), index, 0, 1)
|
||||||
),
|
),
|
||||||
Number(rateLimit),
|
Number(rateLimit),
|
||||||
0
|
0
|
||||||
);
|
);
|
||||||
|
|
||||||
const isValid = rlnInstance.zerokit.verifyRLNProof(
|
const isValid = rlnInstance.zerokit.verifyRLNProof(
|
||||||
BytesUtils.writeUIntLE(new Uint8Array(8), testMessage.length, 0, 8),
|
BytesUtils.writeUintLE(new Uint8Array(8), testMessage.length, 0, 8),
|
||||||
testMessage,
|
testMessage,
|
||||||
proof,
|
proof,
|
||||||
[BytesUtils.bytes32FromBigInt(merkleRoot, "little")]
|
[BytesUtils.bytes32FromBigInt(merkleRoot, "little")]
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
import { bytesToBigInt, numberToBytes } from "viem";
|
||||||
export class BytesUtils {
|
export class BytesUtils {
|
||||||
/**
|
/**
|
||||||
* Concatenate Uint8Arrays
|
* Concatenate Uint8Arrays
|
||||||
@ -32,21 +33,13 @@ export class BytesUtils {
|
|||||||
return 0n;
|
return 0n;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a copy to avoid modifying the original array
|
|
||||||
const workingBytes = new Uint8Array(bytes);
|
const workingBytes = new Uint8Array(bytes);
|
||||||
|
|
||||||
// Reverse bytes if input is little-endian to work with big-endian internally
|
|
||||||
if (inputEndianness === "little") {
|
if (inputEndianness === "little") {
|
||||||
workingBytes.reverse();
|
workingBytes.reverse();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert to BigInt
|
return bytesToBigInt(workingBytes, { size: 32 });
|
||||||
let result = 0n;
|
|
||||||
for (let i = 0; i < workingBytes.length; i++) {
|
|
||||||
result = (result << 8n) | BigInt(workingBytes[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -69,18 +62,7 @@ export class BytesUtils {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (value === 0n) {
|
const result = numberToBytes(value, { size: 32 });
|
||||||
return new Uint8Array(32);
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = new Uint8Array(32);
|
|
||||||
let workingValue = value;
|
|
||||||
|
|
||||||
// Extract bytes in big-endian order
|
|
||||||
for (let i = 31; i >= 0; i--) {
|
|
||||||
result[i] = Number(workingValue & 0xffn);
|
|
||||||
workingValue = workingValue >> 8n;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we need little-endian output, reverse the array
|
// If we need little-endian output, reverse the array
|
||||||
if (outputEndianness === "little") {
|
if (outputEndianness === "little") {
|
||||||
@ -93,7 +75,7 @@ export class BytesUtils {
|
|||||||
/**
|
/**
|
||||||
* Writes an unsigned integer to a buffer in little-endian format
|
* Writes an unsigned integer to a buffer in little-endian format
|
||||||
*/
|
*/
|
||||||
public static writeUIntLE(
|
public static writeUintLE(
|
||||||
buf: Uint8Array,
|
buf: Uint8Array,
|
||||||
value: number,
|
value: number,
|
||||||
offset: number,
|
offset: number,
|
||||||
|
|||||||
@ -17,7 +17,7 @@ export function dateToEpoch(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function epochIntToBytes(epoch: number): Uint8Array {
|
export function epochIntToBytes(epoch: number): Uint8Array {
|
||||||
return BytesUtils.writeUIntLE(new Uint8Array(32), epoch, 0, 32);
|
return BytesUtils.writeUintLE(new Uint8Array(32), epoch, 0, 32);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function epochBytesToInt(bytes: Uint8Array): number {
|
export function epochBytesToInt(bytes: Uint8Array): number {
|
||||||
|
|||||||
@ -3,7 +3,7 @@ import { hash, poseidonHash as poseidon } from "@waku/zerokit-rln-wasm-utils";
|
|||||||
import { BytesUtils } from "./bytes.js";
|
import { BytesUtils } from "./bytes.js";
|
||||||
|
|
||||||
export function poseidonHash(...input: Array<Uint8Array>): Uint8Array {
|
export function poseidonHash(...input: Array<Uint8Array>): Uint8Array {
|
||||||
const inputLen = BytesUtils.writeUIntLE(
|
const inputLen = BytesUtils.writeUintLE(
|
||||||
new Uint8Array(8),
|
new Uint8Array(8),
|
||||||
input.length,
|
input.length,
|
||||||
0,
|
0,
|
||||||
|
|||||||
@ -52,7 +52,7 @@ export class Zerokit {
|
|||||||
sha256(this.rlnIdentifier)
|
sha256(this.rlnIdentifier)
|
||||||
);
|
);
|
||||||
const pathElementsBytes = new Uint8Array(8 + pathElements.length * 32);
|
const pathElementsBytes = new Uint8Array(8 + pathElements.length * 32);
|
||||||
BytesUtils.writeUIntLE(pathElementsBytes, pathElements.length, 0, 8);
|
BytesUtils.writeUintLE(pathElementsBytes, pathElements.length, 0, 8);
|
||||||
for (let i = 0; i < pathElements.length; i++) {
|
for (let i = 0; i < pathElements.length; i++) {
|
||||||
// We assume that the path elements are already in little-endian format
|
// We assume that the path elements are already in little-endian format
|
||||||
pathElementsBytes.set(pathElements[i], 8 + i * 32);
|
pathElementsBytes.set(pathElements[i], 8 + i * 32);
|
||||||
@ -60,7 +60,7 @@ export class Zerokit {
|
|||||||
const identityPathIndexBytes = new Uint8Array(
|
const identityPathIndexBytes = new Uint8Array(
|
||||||
8 + identityPathIndex.length * 1
|
8 + identityPathIndex.length * 1
|
||||||
);
|
);
|
||||||
BytesUtils.writeUIntLE(
|
BytesUtils.writeUintLE(
|
||||||
identityPathIndexBytes,
|
identityPathIndexBytes,
|
||||||
identityPathIndex.length,
|
identityPathIndex.length,
|
||||||
0,
|
0,
|
||||||
@ -73,8 +73,8 @@ export class Zerokit {
|
|||||||
const x = sha256(msg);
|
const x = sha256(msg);
|
||||||
return BytesUtils.concatenate(
|
return BytesUtils.concatenate(
|
||||||
idSecretHash,
|
idSecretHash,
|
||||||
BytesUtils.writeUIntLE(new Uint8Array(32), rateLimit, 0, 32),
|
BytesUtils.writeUintLE(new Uint8Array(32), rateLimit, 0, 32),
|
||||||
BytesUtils.writeUIntLE(new Uint8Array(32), messageId, 0, 32),
|
BytesUtils.writeUintLE(new Uint8Array(32), messageId, 0, 32),
|
||||||
pathElementsBytes,
|
pathElementsBytes,
|
||||||
identityPathIndexBytes,
|
identityPathIndexBytes,
|
||||||
x,
|
x,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user