Written by Meowing Cat on 5/30/2024, 2:17:00 PM
BigCat is a library that allows you to work with big numbers in GDScript.
Hello, Cat is here! Today I want to introduce you to BigCat, a big number library for Godot. BigCat is a library that allows you to work with big numbers in GDScript.
![]() | BigCat is a library for working with big numbers in Godot. (Written in GDScript.) |
It can do arithmetic operations, comparisons, and conversions between big numbers, strings and different base representations. It also can do things that we need for cryptography, like modular exponentiation, modular inverse, and prime number generation.
BigCat.ATOMIC_BITS
, BigCat.set_atomic_bits(bits)
)I tried to make it fast and efficient as much as possibleā¦ But it is slow for random prime generation for big numbers (actually because of the primality test). Iāll try to improve it in the future.
However, 128-bit random prime generation is taking an āacceptableā time, for 256-bit it is slower but still acceptable. For more, it is being more and more slower.
Clone BigCat repository into your project directory and youāll have the BigCat
module.
BigCat.BigNumber
is the main class that youāll be using. Hereās a simple example:
extends Node
func _ready():
var a = BigCat.BigNumber.from_uint(812387138271)
var b = BigCat.BigNumber.from_string("091283091823908109238109382091823091")
var c = a.add(b)
print(str(c))
BigCat uses an atomic scalar vector to store the big numbers and allows you to set the size of the atomic scalar.
BigNumber.ATOMIC_BITS: int
, BigCat.set_atomic_bits(p_bits)
The number of bits in the atomic scalar. Default is 30
.
Important Always use
BigCat.set_atomic_bits(bits)
to set this. It will re-calculate other atomic values too.
# Set the atomic bits to 8
BigNumber.set_atomic_bits(8)
# Set the atomic bits to 30 (default and maximum)
BigNumber.set_atomic_bits(30)
Important
2^30 = 1073741824
scalars, because more than that overflows the integer limit during scalar operations. All atomic scalar operations that BigCat does are done with 30-bit integers. It doesnāt do something likescalar1 ^ scalar2
, so there is no a logarithmic crazy result that can overflow the integer limit, so 30-bit scalars it wonāt overflow the integer limit. (Godot Engine rolls over from zero when the integer limit is exceeded.) (I have an idea to avoid this, but I think it would not increase the performance for some reasons like it will mostly overflow for most of scalar operations.)
You can read the article How to implement RSA in GDScript with BigCat to see how to implement RSA in GDScript using BigCat.
You can find more information and examples in the BigCat repository on GitHub.
Thank you for reading. Thatās all for today! I hope you find BigCat useful for your projects. If you have any questions or suggestions, feel free to leave a comment below.
Meow! š¾