maths/routines

This module contains mathematical routines.

To convert between integers and big-endian bytearrays, use stdlib’s int.from_bytearray_big_endian and bytearray.from_int_big_endian(n, 0).

Functions

powmod(n: Int, e: Int, q: Int) -> Int

Calculate n to the power of e modulo q using the exponentiation by squaring method. At each multiplication a modulo is calculated, allowing very large n and e values. A negative exponent or a zero modulus returns 0, and a zero exponent returns 1.

Newer Plutus V3 protocol versions provide builtin.exp_mod_integer, which is much cheaper. It fails (instead of returning 0) on a zero modulus, and computes modular inverses for negative exponents.

routines.powmod(3, 2, 5)

base_q(n: Int, q: Int) -> List<Int>

Convert a non-negative integer n into some base q. The digits are returned most significant first. This method should scale with any integer and any logical base. Zero (in any base) and base zero return the empty list. A negative n, or a base of one or less (other than zero), fails.

routines.base_q(123, 7)
Search Document