BigInt
- class cometa.common.bigint.BigInt(ptr)[source]
Bases:
objectRepresents 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:
- 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:
- classmethod from_bytes(data, order=ByteOrder.BIG_ENDIAN)[source]
Creates a BigInt from a byte array.
- 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:
- 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.
- 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:
- 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:
- 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:
- 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
- __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
- __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
- __divmod__(other)[source]
Computes the quotient and remainder of two BigInt values. Returns a tuple (quotient, remainder).
- __pow__(exponent, modulus=None)[source]
Raises BigInt to a power, optionally modulo another BigInt.
- __lshift__(bits)[source]
Left shift the BigInt by the specified number of bits.
- Parameters:
bits (int)
- Return type:
- __rshift__(bits)[source]
Right shift the BigInt by the specified number of bits.
- Parameters:
bits (int)
- Return type:
- __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
- __hash__ = None