Skip to content

Commit

Permalink
refactor: remove the crc-32 library from dependencies (#1565)
Browse files Browse the repository at this point in the history
  • Loading branch information
Danil42Russia authored Jan 31, 2025
1 parent 45f3e35 commit be96b46
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
"@tonstudio/parser-runtime": "^0.0.1",
"blockstore-core": "1.0.5",
"change-case": "^4.1.2",
"crc-32": "1.2.2",
"ipfs-unixfs-importer": "9.0.10",
"json-bigint": "^1.0.0",
"ohm-js": "^17.1.0",
Expand Down
4 changes: 2 additions & 2 deletions src/optimizer/interpreter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Address, beginCell, BitString, Cell, toNano } from "@ton/core";
import { paddedBufferToBits } from "@ton/core/dist/boc/utils/paddedBits";
import * as crc32 from "crc-32";
import { crc32 } from "../utils/crc32";
import * as A from "../ast/ast";
import { evalConstantExpression } from "./constEval";
import { CompilerContext } from "../context/context";
Expand Down Expand Up @@ -1377,7 +1377,7 @@ export class Interpreter {
this.interpretExpression(ast.args[0]!),
);
return this.util.makeNumberLiteral(
BigInt(crc32.str(str.value) >>> 0),
BigInt(crc32(str.value) >>> 0),
ast.loc,
); // >>> 0 converts to unsigned
}
Expand Down
12 changes: 12 additions & 0 deletions src/utils/crc32.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { crc32 } from "./crc32";

describe("crc32", () => {
it("crc32 is correctly calculated from the string", () => {
expect(crc32("")).toBe(0);
expect(crc32("Hello Tact")).toBe(-1612685692);
expect(crc32("Привет Tact")).toBe(-1470995533);
expect(crc32("👋 Tact")).toBe(1855222621);
expect(crc32("\u0000")).toBe(-771559539);
expect(crc32("⚡")).toBe(2136484914);
});
});
28 changes: 28 additions & 0 deletions src/utils/crc32.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function makeCRC32Table(polynomial: number): Int32Array {
let c: number;
const table = new Int32Array(256);
for (let n = 0; n < table.length; n++) {
c = n;
for (let k = 0; k < 8; k++) {
c = c & 1 ? (c >>> 1) ^ polynomial : c >>> 1;
}
table[n] = c;
}
return table;
}

// Reversed polynomial of ISO3309 CRC32
const CRC32C_TABLE = makeCRC32Table(0xedb88320);

export function crc32(data: string | Uint8Array): number {
if (typeof data === "string") {
data = new TextEncoder().encode(data);
}

let crc = 0xffffffff;
for (const byte of data) {
crc = CRC32C_TABLE[(crc ^ byte) & 0xff]! ^ (crc >>> 8);
}

return crc ^ 0xffffffff;
}
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2028,11 +2028,6 @@ core-util-is@^1.0.3:
resolved "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz"
integrity sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==

crc-32@1.2.2:
version "1.2.2"
resolved "https://registry.npmjs.org/crc-32/-/crc-32-1.2.2.tgz#3cad35a934b8bf71f25ca524b6da51fb7eace2ff"
integrity sha512-ROmzCKrTnOwybPcJApAA6WBWij23HVfGVNKqqrZpuyZOHqK2CwHSvpGuyt/UNNvaIjEd8X5IFGp4Mh+Ie1IHJQ==

create-jest@^29.7.0:
version "29.7.0"
resolved "https://registry.npmjs.org/create-jest/-/create-jest-29.7.0.tgz#a355c5b3cb1e1af02ba177fe7afd7feee49a5320"
Expand Down

0 comments on commit be96b46

Please sign in to comment.