Math
Various math helper functions.
min
fun min(x: Int, y: Int): Int;
Computes and returns the minimum of two Int
values x
and y
.
Usage examples:
min(1, 2); // 1min(2, 2); // 2min(007, 3); // 3min(0x45, 3_0_0); // 69, nice// ↑ ↑// 69 300
max
fun max(x: Int, y: Int): Int;
Computes and returns the maximum of two Int
values x
and y
.
Usage examples:
max(1, 2); // 2max(2, 2); // 2max(007, 3); // 7max(0x45, 3_0_0); // 300// ↑ ↑// 69 300
abs
fun abs(x: Int): Int;
Computes and returns the absolute value of the Int
value x
.
Usage examples:
abs(42); // 42abs(-42); // 42abs(-(-(-42))); // 42
sign
Available since Tact 1.6 (not released yet)fun sign(x: Int): Int;
Computes and returns the sign of the Int
value x
. Produces if the x
is positive, if the x
is negative, and if the x
is .
Usage examples:
sign(42); // 1sign(-42); // -1sign(-(-42)); // 1sign(-(-(-42))); // -1sign(0); // 0
sqrt
Gas-expensive Available since Tact 1.6 (not released yet)fun sqrt(num: Int): Int;
Computes the square root of the Int
value num
. Returns the result rounded to the nearest integer. If there are two equally close integers, rounding is done toward the even one.
Attempts to specify a negative value of num
throw an exception with exit code 5: Integer out of expected range
.
Usage examples:
sqrt(4); // 2sqrt(3); // 2sqrt(2); // 1sqrt(1); // 1sqrt(0); // 0sqrt(-1); // ERROR! Exit code 5: Integer out of expected range
divc
Available since Tact 1.6 (not released yet)fun divc(x: Int, y: Int): Int;
Computes and returns the rounded up result of division of the Int
x
by the Int
y
.
Attempts to divide by y
equal to throw an exception with exit code 4: Integer overflow
.
Usage examples:
divc(4, 2); // 2divc(3, 2); // 2divc(-4, 2); // -2divc(-3, 2); // -1
muldivc
Available since Tact 1.6 (not released yet)fun muldivc(x: Int, y: Int, z: Int): Int;
Computes and returns the rounded up result of (x * y) / z
.
If the value in calculation goes beyond the range from to inclusive, or if there’s an attempt to divide by z
equal to , an exception with exit code 4 is thrown: Integer overflow
.
Usage examples:
muldivc(4, 1, 2); // 2muldivc(3, 1, 2); // 2muldivc(-4, 1, 2); // -2muldivc(-3, 1, 2); // -1muldivc(-3, 0, 2); // 0muldivc(-3, 0, 0); // ERROR! Exit code 4: Integer overflow
mulShiftRight
Available since Tact 1.6 (not released yet)fun mulShiftRight(x: Int, y: Int, z: Int): Int;
Computes and returns the rounded down result of (x * y) / 2^z
. It is a more gas-efficient equivalent of doing the bitwise shift right on the result of multiplication of Int
x
times Int
y
, where Int
z
is the right operand of the shift.
If the value in calculation goes beyond the range from to inclusive, an exception with exit code 4 is thrown: Integer overflow
.
Attempts to specify any value of z
outside the inclusive range from to throw an exception with exit code 5: Integer out of expected range
.
Usage examples:
mulShiftRight(5, 5, 2); // 6mulShiftRight(5, 5, 1); // 12mulShiftRight(5, 5, 0); // 25mulShiftRight(5, 5, -1); // ERROR! Exit code 5: Integer out of expected range
mulShiftRightRound
Available since Tact 1.6 (not released yet)fun mulShiftRightRound(x: Int, y: Int, z: Int): Int;
Similar to mulShiftRight()
, but instead of rounding down, the result value is rounded to the nearest integer. If there are two equally close integers, rounding is done toward the even one.
If the value in calculation goes beyond the range from to inclusive, an exception with exit code 4 is thrown: Integer overflow
.
Attempts to specify any value of z
outside the inclusive range from to throw an exception with exit code 5: Integer out of expected range
.
Usage examples:
mulShiftRightRound(5, 5, 2); // 6mulShiftRightRound(5, 5, 1); // 13mulShiftRightRound(5, 5, 0); // 25mulShiftRightRound(5, 5, -1); // ERROR! Exit code 5: Integer out of expected range
mulShiftRightCeil
Available since Tact 1.6 (not released yet)fun mulShiftRightCeil(x: Int, y: Int, z: Int): Int;
Similar to mulShiftRight()
, but instead of rounding down, the result value is rounded up.
If the value in calculation goes beyond the range from to inclusive, an exception with exit code 4 is thrown: Integer overflow
.
Attempts to specify any value of z
outside the inclusive range from to throw an exception with exit code 5: Integer out of expected range
.
Usage examples:
mulShiftRightCeil(5, 5, 2); // 7mulShiftRightCeil(5, 5, 1); // 13mulShiftRightCeil(5, 5, 0); // 25mulShiftRightCeil(5, 5, -1); // ERROR! Exit code 5: Integer out of expected range
log
fun log(num: Int, base: Int): Int;
Computes and returns the logarithm of a number num
to the base base
. Results are rounded down.
Attempts to specify a non-positive num
value or a base
less than throw an error with exit code 5: Integer out of expected range
.
Usage examples:
log(1000, 10); // 3, as 10^3 is 1000// ↑ ↑ ↑ ↑// num base base num
log(1001, 10); // 3log(999, 10); // 2try { log(-1000, 10); // exit code 5 because of the non-positive num}log(1024, 2); // 10try { log(1024, -2); // exit code 5 because the base is less than 1}
log2
fun log2(num: Int): Int;
Similar to log()
, but sets the base
to .
Attempts to specify a non-positive num
value throw an error with exit code 5: Integer out of expected range
.
Usage example:
log2(1024); // 10, as 2^10 is 1024// ↑ ↑ ↑// num base₂ num
pow
fun pow(base: Int, exp: Int): Int;
Computes and returns the exponentiation involving two numbers: the base
and the exponent (or power) exp
. Exponent exp
must be non-negative, otherwise an error with exit code 5 will be thrown: Integer out of expected range
.
This function tries to resolve constant values in compile-time whenever possible.
Usage examples:
contract Example { // Persistent state variables p23: Int = pow(2, 3); // raises 2 to the 3rd power, which is 8 one: Int = pow(5, 0); // raises 5 to the power 0, which always produces 1 // works at compile-time!
// Internal message receiver receive() { pow(self.p23, self.one + 1); // 64, works at run-time too! try { pow(0, -1); // exit code 5: Integer out of expected range } }}
pow2
fun pow2(exp: Int): Int;
Similar to pow()
, but sets the base
to . Exponent exp
must be non-negative, otherwise an error with exit code 5 will be thrown: Integer out of expected range
.
This function tries to resolve constant values in compile-time whenever possible.
Usage examples:
contract Example { // Persistent state variables p23: Int = pow2(3); // raises 2 to the 3rd power, which is 8 one: Int = pow2(0); // raises 2 to the power 0, which always produces 1 // works at compile-time!
// Internal message receiver, which accepts message ExtMsg receive() { pow2(self.one + 1); // 4, works at run-time too! try { pow(-1); // exit code 5: Integer out of expected range } }}
checkSignature
Gas-expensivefun checkSignature(hash: Int, signature: Slice, public_key: Int): Bool;
Checks the Ed25519 signature
of the -bit unsigned Int
hash
using a public_key
, represented by a -bit unsigned Int
too. The signature must contain at least bits of data, but only the first bits are used.
Returns true
if the signature is valid, false
otherwise.
Usage example:
message ExtMsg { signature: Slice; data: Cell;}
contract Showcase { // Persistent state variables pub: Int as uint256; // public key as an 256-bit unsigned Int
// Constructor function init(), where all the variables are initialized init(pub: Int) { self.pub = pub; // storing the public key upon contract initialization }
// External message receiver, which accepts message ExtMsg external(msg: ExtMsg) { let hash: Int = beginCell().storeRef(msg.data).endCell().hash(); let check: Bool = checkSignature(hash, msg.signature, self.pub); // ---- ------------- -------- // ↑ ↑ ↑ // | | public_key, stored in our contract // | signature, obtained from the received message // hash, calculated using the data from the received message // ... follow-up logic ... }}
checkDataSignature
Gas-expensivefun checkDataSignature(data: Slice, signature: Slice, public_key: Int): Bool;
Checks the Ed25519 signature
of the data
using a public_key
, similar to checkSignature()
. If the bit length of data
is not divisible by , this functions throws an error with exit code 9: Cell underflow
. Verification itself is being done indirectly: on a SHA-256 hash of the data
.
Returns true
if the signature is valid, false
otherwise.
Usage example:
let data: Slice = some_data;let signature: Slice = some_signature;let publicKey: Int = 42;
let check: Bool = checkSignature(data, signature, publicKey);
sha256
fun sha256(data: Slice): Int;fun sha256(data: String): Int;
Computes and returns the SHA-256 hash as a -bit unsigned Int
from a passed Slice
or String
data
.
In case data
is a String
it should have a number of bits divisible by , and in case it’s a Slice
it must also have no references (i.e. only up to bits of data in total). This function tries to resolve constant string values at compile-time whenever possible.
Usage examples:
sha256(beginCell().asSlice());sha256("Hello, world!"); // will be resolved in compile-timesha256(someVariableElsewhere); // will try to resolve at compile-time, // and fallback to run-time evaluation