BigCat is here! Big number library for Godot (GDScript)

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.

What is BigCat?

BigCat, big number library for Godot (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.

Features

  • Infinitely long numbers
  • Dynamically adjustable atomic scalar (BigCat.ATOMIC_BITS, BigCat.set_atomic_bits(bits))
  • Conversions:
    • Conversions between byte arrays and big numbers
    • Conversions between signed char arrays and big numbers (It still doesnā€™t guarantee to give the original ASCII strings.)
    • Conversions between strings (in any base) and big numbers
    • Conversions between big numbers and integers
  • Arithmetic Operations (addition, subtraction, multiplication, division)
  • Comparison Operations (less than, greater than, equal to)
  • Modular Arithmetic
  • Random Number Generation
  • Cryptographic Requirements:
    • Modular Exponentiation
    • Modular Multiplicative Inverse
    • Primality Test
    • Random Prime Generation
    • Dumb Multi-Threaded Prime Generation

Limitations

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.

Installation

Clone BigCat repository into your project directory and youā€™ll have the BigCat module.

Usage

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))

Atomic Scalar Size

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 like scalar1 ^ 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.)

More?

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! šŸ¾