Skip to content

Commit

Permalink
RTDX: Fix timings of type coercion for crc32
Browse files Browse the repository at this point in the history
  • Loading branch information
Karthik99999 committed Apr 12, 2024
1 parent cbead01 commit 17b971e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/lib/generators/rtdx/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export function deserialize(password: string): RescueData | RevivalData {

const bits = symbolsToBits(sanitized);
const charcode = bits.map((b) => Data.charmap[b]).join('');
const revive = crc32(charcode) & 0x3fffffff;
const revive = Number(crc32(charcode) & 0x3fffffffn);

const data: RescueData = {
password,
Expand Down
9 changes: 4 additions & 5 deletions src/lib/generators/rtdx/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,12 @@ export function checksum(code: number[]) {
return sum;
}

export function crc32(charcode: string): number {
const bytes = Array.from(new TextEncoder().encode(charcode)).map(BigInt);
export function crc32(bytes: string): bigint {
let sum = 0xffffffffn;
for (const byte of bytes) {
sum = BigInt(Data.crc32table[Number((sum & 0xffn) ^ byte)]) ^ (sum >> 8n);
for (const byte of new TextEncoder().encode(bytes)) {
sum = BigInt(Data.crc32table[Number(sum & 0xffn) ^ byte]) ^ (sum >> 8n);
}
return Number(sum ^ 0xffffffffn);
return sum ^ 0xffffffffn;
}

/**
Expand Down

0 comments on commit 17b971e

Please sign in to comment.