BigInt

class cometa.common.bigint.BigInt(ptr)[source]

Bases: object

Represents a large numeric value (arbitrary precision integer).

This class wraps the C cardano_bigint_t type. It implements standard Python math operators (+, -, *, /, etc.), enabling it to be used interchangeably with native Python integers in most contexts.

property is_zero: bool

Returns True if the value is 0.

property sign: int

Returns -1 for negative, 0 for zero, 1 for positive.

property bit_count: int

Returns number of bits differing from sign bit.

property bit_length: int

Returns number of bits required to represent the integer.

classmethod from_int(value)[source]

Creates a BigInt from a Python integer.

Parameters:

value (int) – The integer value.

Return type:

BigInt

classmethod from_string(string, base=10)[source]

Creates a BigInt from a string representation.

Parameters:
  • string (str) – The string representation.

  • base (int) – The numerical base (e.g., 10, 16).

Return type:

BigInt

classmethod from_bytes(data, order=ByteOrder.BIG_ENDIAN)[source]

Creates a BigInt from a byte array.

Parameters:
  • data (bytes) – The raw byte data.

  • order (ByteOrder) – The byte order (Endianness).

Return type:

BigInt

clone()[source]

Creates a deep copy of the BigInt object.

Return type:

BigInt

to_string(base=10)[source]

Converts the BigInt to a string in the specified base.

Parameters:

base (int)

Return type:

str

to_bytes(order=ByteOrder.BIG_ENDIAN)[source]

Converts the BigInt to a byte array.

Parameters:

order (ByteOrder)

Return type:

bytes

pow(exponent)[source]

Raises BigInt to a positive integer power.

Parameters:

exponent (int)

Return type:

BigInt

mod_pow(exponent, modulus)[source]

Modular exponentiation: (self ** exponent) % modulus.

Parameters:
  • exponent (BigInt | int | str)

  • modulus (BigInt | int | str)

Return type:

BigInt

mod_inverse(modulus)[source]

Computes the modular multiplicative inverse.

Parameters:

modulus (BigInt | int | str)

Return type:

BigInt

gcd(other)[source]

Computes the Greatest Common Divisor.

Parameters:

other (BigInt | int | str)

Return type:

BigInt

assign(other)[source]

Assigns the value of another BigInt to this BigInt in-place.

Parameters:

other (BigIntLike) – The BigInt-like value to assign from.

Return type:

None

Note

Despite the C function parameter names, the actual behavior is: cardano_bigint_assign(source, destination) copies FROM source TO destination.

increment()[source]

Increments the BigInt by 1 in-place.

Return type:

None

decrement()[source]

Decrements the BigInt by 1 in-place.

Return type:

None

static max(lhs, rhs)[source]

Returns the maximum of two BigInt values.

Parameters:
  • lhs (BigIntLike) – First value.

  • rhs (BigIntLike) – Second value.

Returns:

The larger of the two values.

Return type:

BigInt

static min(lhs, rhs)[source]

Returns the minimum of two BigInt values.

Parameters:
  • lhs (BigIntLike) – First value.

  • rhs (BigIntLike) – Second value.

Returns:

The smaller of the two values.

Return type:

BigInt

remainder(divisor)[source]

Computes the remainder of division (same as modulus operation).

Parameters:

divisor (BigIntLike) – The divisor.

Returns:

The remainder of self divided by divisor.

Return type:

BigInt

to_int()[source]

Converts the BigInt to a Python int (bounded by int64 range).

Returns:

The integer value (int64 range: -9223372036854775808 to 9223372036854775807).

Return type:

int

Note

If the BigInt value exceeds the int64 range, the result will be truncated or wrapped. For full precision conversion, use int(bigint) or str(bigint) instead.

to_unsigned_int()[source]

Converts the BigInt to a Python int (bounded by uint64 range).

Returns:

The unsigned integer value (uint64 range: 0 to 18446744073709551615).

Return type:

int

Note

If the BigInt value exceeds the uint64 range, the result will be truncated or wrapped. For full precision conversion, use int(bigint) or str(bigint) instead.

test_bit(bit_position)[source]

Checks if the N-th bit is set.

Parameters:

bit_position (int)

Return type:

bool

set_bit(bit_position)[source]

Sets the N-th bit in-place.

Parameters:

bit_position (int)

Return type:

None

clear_bit(bit_position)[source]

Clears the N-th bit in-place.

Parameters:

bit_position (int)

Return type:

None

flip_bit(bit_position)[source]

Flips the N-th bit in-place.

Parameters:

bit_position (int)

Return type:

None

compare(other)[source]

Compares this BigInt with another BigInt-like value.

Parameters:

other (BigInt | int | str)

Return type:

int

__int__()[source]

Converts the BigInt to a Python int.

Return type:

int

__index__()[source]

Allows BigInt to be used as a list index or in functions like hex(), bin(). Example: my_list[big_int] or hex(big_int)

Return type:

int

__str__()[source]

Returns the decimal string representation.

Return type:

str

__repr__()[source]

Returns the official string representation.

Return type:

str

__format__(format_spec)[source]

Allows BigInt to be formatted in f-strings. Example: f”{big_int:x}” -> hexadecimal

Parameters:

format_spec (str)

Return type:

str

__add__(other)[source]

Adds two BigInt values.

Parameters:

other (BigInt | int | str)

Return type:

BigInt

__sub__(other)[source]

Subtracts two BigInt values.

Parameters:

other (BigInt | int | str)

Return type:

BigInt

__mul__(other)[source]

Multiplies two BigInt values.

Parameters:

other (BigInt | int | str)

Return type:

BigInt

__truediv__(other)[source]

Integer division (same as floordiv for BigInt).

Parameters:

other (BigInt | int | str)

Return type:

BigInt

__floordiv__(other)[source]

Integer division (same as truediv for BigInt).

Parameters:

other (BigInt | int | str)

Return type:

BigInt

__mod__(other)[source]

Computes the modulus of two BigInt values.

Parameters:

other (BigInt | int | str)

Return type:

BigInt

__divmod__(other)[source]

Computes the quotient and remainder of two BigInt values. Returns a tuple (quotient, remainder).

Parameters:

other (BigInt | int | str)

Return type:

tuple[BigInt, BigInt]

__pow__(exponent, modulus=None)[source]

Raises BigInt to a power, optionally modulo another BigInt.

Parameters:
  • exponent (int)

  • modulus (BigInt | int | str | None)

Return type:

BigInt

__abs__()[source]

Returns the absolute value of the BigInt.

Return type:

BigInt

__neg__()[source]

Returns the negation of the BigInt.

Return type:

BigInt

__and__(other)[source]

Bitwise AND of two BigInt values.

Parameters:

other (BigInt | int | str)

Return type:

BigInt

__or__(other)[source]

Bitwise OR of two BigInt values.

Parameters:

other (BigInt | int | str)

Return type:

BigInt

__xor__(other)[source]

Bitwise XOR of two BigInt values.

Parameters:

other (BigInt | int | str)

Return type:

BigInt

__invert__()[source]

Bitwise NOT of the BigInt value.

Return type:

BigInt

__lshift__(bits)[source]

Left shift the BigInt by the specified number of bits.

Parameters:

bits (int)

Return type:

BigInt

__rshift__(bits)[source]

Right shift the BigInt by the specified number of bits.

Parameters:

bits (int)

Return type:

BigInt

__eq__(other)[source]

Checks equality between two BigInt values.

Parameters:

other (object)

Return type:

bool

__lt__(other)[source]

Less than comparison.

Parameters:

other (BigInt | int | str)

Return type:

bool

__le__(other)[source]

Less than or equal comparison.

Parameters:

other (BigInt | int | str)

Return type:

bool

__gt__(other)[source]

Greater than comparison.

Parameters:

other (BigInt | int | str)

Return type:

bool

__ge__(other)[source]

Greater than or equal comparison.

Parameters:

other (BigInt | int | str)

Return type:

bool

__init__(ptr)[source]

Internal constructor. Use class methods like from_int, from_string, or from_bytes instead.

Return type:

None

__enter__()[source]

Context manager entry (no-op).

Return type:

BigInt

__exit__(exc_type, exc_val, exc_tb)[source]

Context manager exit (no-op).

Return type:

None

__hash__ = None