Skip to content

Commit

Permalink
use bigint to compare hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
i582 committed Feb 4, 2025
1 parent 451e53a commit 4b58cbc
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/test/e2e-emulated/intrinsics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,16 +263,17 @@ describe("intrinsics", () => {
expect(await contract.getGetCrc32_4()).toBe(0n);
});

const checkSha256 = async (input: string) => {
const expected = sha256_sync(input).toString("hex");
if (expected.startsWith("0")) {
// Since TVM calculates sha256 as an Int, if the resulting hash starts with zero,
// that zero will be truncated in the TVM result, so we skip such cases.
return;
}
const bufferToBigInt = (buffer: Buffer): bigint => {
return buffer.reduce(
(acc, byte) => (acc << BigInt(8)) | BigInt(byte),
BigInt(0),
);
};

const checkSha256 = async (input: string) => {
const expected = bufferToBigInt(sha256_sync(input));
const actual = await contract.getGetHashLongRuntime(input);
expect(actual.toString(16)).toEqual(expected);
expect(actual.toString(16)).toEqual(expected.toString(16));
};

const generateString = (length: number): string => {
Expand Down

0 comments on commit 4b58cbc

Please sign in to comment.