Skip to content

Commit 7a3334b

Browse files
committed
fix: use viem for bytes32/bigint conversion
1 parent db5c568 commit 7a3334b

5 files changed

Lines changed: 12 additions & 30 deletions

File tree

packages/rln/src/proof.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@ describe("RLN Proof Integration Tests", function () {
7777
BytesUtils.bytes32FromBigInt(element, "little")
7878
),
7979
proofElementIndexes.map((index) =>
80-
BytesUtils.writeUIntLE(new Uint8Array(1), index, 0, 1)
80+
BytesUtils.writeUintLE(new Uint8Array(1), index, 0, 1)
8181
),
8282
Number(rateLimit),
8383
0
8484
);
8585

8686
const isValid = rlnInstance.zerokit.verifyRLNProof(
87-
BytesUtils.writeUIntLE(new Uint8Array(8), testMessage.length, 0, 8),
87+
BytesUtils.writeUintLE(new Uint8Array(8), testMessage.length, 0, 8),
8888
testMessage,
8989
proof,
9090
[BytesUtils.bytes32FromBigInt(merkleRoot, "little")]

packages/rln/src/utils/bytes.ts

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { bytesToBigInt, numberToBytes } from "viem";
12
export class BytesUtils {
23
/**
34
* Concatenate Uint8Arrays
@@ -32,21 +33,13 @@ export class BytesUtils {
3233
return 0n;
3334
}
3435

35-
// Create a copy to avoid modifying the original array
3636
const workingBytes = new Uint8Array(bytes);
3737

38-
// Reverse bytes if input is little-endian to work with big-endian internally
3938
if (inputEndianness === "little") {
4039
workingBytes.reverse();
4140
}
4241

43-
// Convert to BigInt
44-
let result = 0n;
45-
for (let i = 0; i < workingBytes.length; i++) {
46-
result = (result << 8n) | BigInt(workingBytes[i]);
47-
}
48-
49-
return result;
42+
return bytesToBigInt(workingBytes, { size: 32 });
5043
}
5144

5245
/**
@@ -69,18 +62,7 @@ export class BytesUtils {
6962
);
7063
}
7164

72-
if (value === 0n) {
73-
return new Uint8Array(32);
74-
}
75-
76-
const result = new Uint8Array(32);
77-
let workingValue = value;
78-
79-
// Extract bytes in big-endian order
80-
for (let i = 31; i >= 0; i--) {
81-
result[i] = Number(workingValue & 0xffn);
82-
workingValue = workingValue >> 8n;
83-
}
65+
const result = numberToBytes(value, { size: 32 });
8466

8567
// If we need little-endian output, reverse the array
8668
if (outputEndianness === "little") {
@@ -93,7 +75,7 @@ export class BytesUtils {
9375
/**
9476
* Writes an unsigned integer to a buffer in little-endian format
9577
*/
96-
public static writeUIntLE(
78+
public static writeUintLE(
9779
buf: Uint8Array,
9880
value: number,
9981
offset: number,

packages/rln/src/utils/epoch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function dateToEpoch(
1717
}
1818

1919
export function epochIntToBytes(epoch: number): Uint8Array {
20-
return BytesUtils.writeUIntLE(new Uint8Array(32), epoch, 0, 32);
20+
return BytesUtils.writeUintLE(new Uint8Array(32), epoch, 0, 32);
2121
}
2222

2323
export function epochBytesToInt(bytes: Uint8Array): number {

packages/rln/src/utils/hash.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { hash, poseidonHash as poseidon } from "@waku/zerokit-rln-wasm-utils";
33
import { BytesUtils } from "./bytes.js";
44

55
export function poseidonHash(...input: Array<Uint8Array>): Uint8Array {
6-
const inputLen = BytesUtils.writeUIntLE(
6+
const inputLen = BytesUtils.writeUintLE(
77
new Uint8Array(8),
88
input.length,
99
0,

packages/rln/src/zerokit.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ export class Zerokit {
5252
sha256(this.rlnIdentifier)
5353
);
5454
const pathElementsBytes = new Uint8Array(8 + pathElements.length * 32);
55-
BytesUtils.writeUIntLE(pathElementsBytes, pathElements.length, 0, 8);
55+
BytesUtils.writeUintLE(pathElementsBytes, pathElements.length, 0, 8);
5656
for (let i = 0; i < pathElements.length; i++) {
5757
// We assume that the path elements are already in little-endian format
5858
pathElementsBytes.set(pathElements[i], 8 + i * 32);
5959
}
6060
const identityPathIndexBytes = new Uint8Array(
6161
8 + identityPathIndex.length * 1
6262
);
63-
BytesUtils.writeUIntLE(
63+
BytesUtils.writeUintLE(
6464
identityPathIndexBytes,
6565
identityPathIndex.length,
6666
0,
@@ -73,8 +73,8 @@ export class Zerokit {
7373
const x = sha256(msg);
7474
return BytesUtils.concatenate(
7575
idSecretHash,
76-
BytesUtils.writeUIntLE(new Uint8Array(32), rateLimit, 0, 32),
77-
BytesUtils.writeUIntLE(new Uint8Array(32), messageId, 0, 32),
76+
BytesUtils.writeUintLE(new Uint8Array(32), rateLimit, 0, 32),
77+
BytesUtils.writeUintLE(new Uint8Array(32), messageId, 0, 32),
7878
pathElementsBytes,
7979
identityPathIndexBytes,
8080
x,

0 commit comments

Comments
 (0)