Skip to content

Commit

Permalink
Fix Mathematics to work under 32 bits
Browse files Browse the repository at this point in the history
  • Loading branch information
Swafox committed Aug 8, 2024
1 parent 88a3660 commit 9e7faa1
Show file tree
Hide file tree
Showing 4 changed files with 3,420 additions and 12 deletions.
15 changes: 9 additions & 6 deletions Mathematics/contracts/mathematics.fc
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@ global int ctx_counter;
return p;
}

;; Modulo operations
;; Modulo operations (32-bit version)
(int) modulo_operations (int xp, int zp) {
int prime = 57896044618658097711785492504343953926634992332820282019728792003956564819949;
(_, int xp+zp*xp-zp) = muldivmod(xp + zp, xp - zp, prime);
return xp+zp*xp-zp;
;; Using a smaller prime that fits in 32 bits
int prime = 4294967291; ;; 2^32 - 5 (a 32-bit prime)

int xp_plus_zp = (xp + zp) % prime;
int xp_minus_zp = (xp - zp + prime) % prime;
return (xp_plus_zp * xp_minus_zp) % prime;
}

() recv_internal(int my_balance, int msg_value, cell in_msg_full, slice in_msg_body) impure {
Expand Down Expand Up @@ -91,8 +94,8 @@ global int ctx_counter;
;; Modulo operations
;; Cookbook link: https://docs.ton.org/develop/func/cookbook#modulo-operations
if (op == 4) {
int xp = in_msg_body~load_uint(256);
int zp = in_msg_body~load_uint(256);
int xp = in_msg_body~load_uint(32);
int zp = in_msg_body~load_uint(32);
ctx_counter += modulo_operations(xp, zp);

save_data();
Expand Down
7 changes: 4 additions & 3 deletions Mathematics/tests/Mathematics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ describe('Mathematics', () => {

console.log('counter before modulo operation', counterBefore);

const xp = BigInt('12345678901234567890');
const zp = BigInt('98765432109876543210');
const xp = 1234567890; // Example 32-bit number
const zp = 987654321; // Example 32-bit number
const prime = 4294967291; // 2^32 - 5 (32-bit prime)

const increaseResult = await mathematics.sendModuloOperations(increaser.getSender(), {
xp,
Expand All @@ -147,6 +148,6 @@ describe('Mathematics', () => {

console.log('counter after modulo operation', counterAfter);

expect(counterAfter).toBeGreaterThan(counterBefore);
expect(counterAfter).toBe(1181121056);
});
});
6 changes: 3 additions & 3 deletions Mathematics/wrappers/Mathematics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ export class Mathematics implements Contract {
provider: ContractProvider,
via: Sender,
opts: {
xp: bigint;
zp: bigint;
xp: number;
zp: number;
value: bigint;
},
) {
await provider.internal(via, {
value: opts.value,
sendMode: SendMode.PAY_GAS_SEPARATELY,
body: beginCell().storeUint(4, 32).storeUint(opts.xp, 256).storeUint(opts.zp, 256).endCell(),
body: beginCell().storeUint(4, 32).storeUint(1, 32).storeUint(opts.xp, 32).storeUint(opts.zp, 32).endCell(),
});
}

Expand Down
Loading

0 comments on commit 9e7faa1

Please sign in to comment.