Meow... BigCat is a big number library for Godot Engine / GDScript.
- 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
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))
Even 256-bit primes take so long time to generate but it is still acceptable. 128-bit is more acceptable.
Single-threaded:
extends Node
func _ready():
var prime = BigCat.BigNumber.generate_prime(128)
print("Prime: ", str(prime))
Multi-threaded:
extends Node
func _ready():
var prime = BigCat.BigNumber.generate_prime_threaded(128, 2, 4)
print("Prime: ", str(prime))
BigCat is doing things... But it is not a cheetah and always be careful with cryptographic implementations for critical data and purposes!
extends Node
func _ready():
BigCat.BigNumber.IS_VERBOSE = true
print("Generating RSA key pair...")
var bits = 128
var p = BigCat.BigNumber.generate_prime(bits)
var q = BigCat.BigNumber.generate_prime(bits)
var n = p.multiply(q)
var phi = p.subtract(BigCat.BigNumber.from_int(1)).multiply(q.subtract(BigCat.BigNumber.from_int(1)))
var e = BigCat.BigNumber.from_int(65537)
var d = e.inverse_modulo(phi)
print("Public Key: (" + str(e) + ", " + str(n) + ")")
print("Private Key: (" + str(d) + ", " + str(n) + ")")
print("Encrypting and Decrypting a message...")
var message = "Hello, World!"
var big = BigCat.BigNumber.from_chars(message.to_ascii_buffer())
var encrypted = big.power_modulo(e, n)
var decrypted = encrypted.power_modulo(d, n)
print("Original Scalar: \t", str(big))
print("Encrypted Scalar: \t", str(encrypted))
print("Decrypted Scalar: \t", str(decrypted))
The number of bits in the atomic scalar. Default is 30
.
Warning
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)
Warning
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.)
Enables/disables verbose outputs. (Default is false
.)
Creates a new BigNumber
object with the given scalar array.
Creates a new BigNumber
object from the given byte array.
Creates a new BigNumber
object from the given signed char
array.
Creates a new BigNumber
object from the given string.
Creates a new BigNumber
object from the given string in the given base.
Creates a new BigNumber
object from the given hexadecimal string.
Creates a new BigNumber
object from the given unsigned integer.
Creates a new BigNumber
object from the given signed integer.
Returns the scalar array of the BigNumber
object.
true
if the BigNumber
object is negative, false
if not.
Byte representation of the BigNumber
object.
Signed char
representation of the BigNumber
object.
String representation of the BigNumber
object.
Conversion Operations:
Returns the byte array representation of the BigNumber
object.
Returns the signed char
array representation of the BigNumber
object.
Returns the string representation of the BigNumber
object. You can do str(big_number)
too.
Returns the string representation of the BigNumber
object in the given base.
Returns the hexadecimal string representation of the BigNumber
object.
Returns the integer representation of the BigNumber
object.
Returns the unsigned integer representation of the BigNumber
object.
Arithmetic Operations:
Returns the sum of the BigNumber
object and the given BigNumber
object.
Returns the sum of the BigNumber
object and the given number.
Returns the sum of the BigNumber
object and the given number.
Returns the difference between the BigNumber
object and the given BigNumber
object.
Returns the difference between the BigNumber
object and the given number.
Returns the difference between the BigNumber
object and the given number.
Returns the product of the BigNumber
object and the given BigNumber
object.
Returns the product of the BigNumber
object and the given number.
Returns the product of the BigNumber
object and the given number.
Returns the quotient of the BigNumber
object and the given BigNumber
object.
Returns the quotient of the BigNumber
object and the given number.
Returns the quotient of the BigNumber
object and the given number.
Returns the power of the BigNumber
object with the given BigNumber
object.
Returns the power of the BigNumber
object with the given number.
Comparison Operations:
true
if the BigNumber
object is less than the given BigNumber
object, false
if not.
true
if the BigNumber
object is greater than the given BigNumber
object, false
if not.
true
if the BigNumber
object is equal to the given BigNumber
object, false
if not.
true
if the BigNumber
object is greater than or equal to the given BigNumber
object, false
if not.
true
if the BigNumber
object is less than or equal to the given BigNumber
object, false
if not.
Modular Arithmetic:
Returns the remainder of the BigNumber
object divided by the given BigNumber
object.
Returns the greatest common divisor of the BigNumber
object and the given BigNumber
object.
Cryptophic Requirements:
Returns the modular exponentiation of the BigNumber
object with the given exponent and modulus.
Returns the modular multiplicative inverse of the BigNumber
object with the given modulus.
Faster inverse modulo for prime numbers.
Random Number Generation:
Returns a random BigNumber
object with the given number of bits.
Returns a random BigNumber
object within the given range.
Returns a random prime BigNumber
object with the given number of bits with the given witness.
static
BigNumber.generate_prime_threaded(p_bits: int, p_witness = 2, p_num_threads = 2) -> BigNumber
Multi-threaded version of BigNumber.generate_prime()
. Spawns p_num_threads
threads and returns the first prime number found.
true
if the BigNumber
object is probably prime with the given witness, false
if not.
Returns the bitwise AND of the BigNumber
object and the given BigNumber
object.
Returns the bitwise OR of the BigNumber
object and the given BigNumber
object.
Returns the bitwise XOR of the BigNumber
object and the given BigNumber
object.
Returns the BigNumber
object shifted left by the given BigNumber
object.
Returns the BigNumber
object shifted right by the given BigNumber
object.
Returns the BigNumber
object shifted left by the given number of bits.
Returns the BigNumber
object shifted right by the given number of bits.
true
if the BigNumber
object is odd, false
if not.
true
if the BigNumber
object is even, false
if not.
true
if the BigNumber
object is zero, false
if not.
You can contribute with commiting to project or developing a plugin. All commits are welcome.
You can find me on Discord and Patreon.
Currencies:
Currency | Address |
---|---|
BTC | bc1qhvlc762kwuzeawedl9a8z0duhs8449nwwc35e2 |
ETH | 0x1D99B2a2D85C34d478dD8519792e82B18f861974 |
USDT | 0x1D99B2a2D85C34d478dD8519792e82B18f861974 |
USDC | 0x1D99B2a2D85C34d478dD8519792e82B18f861974 |
XMR | 88qvS4sfUnLZ7nehFrz3PG1pWovvEgprcUhkmVLaiL8PVAFgfHjspjKPLhWLj3DUcm92rwNQENbJ1ZbvESdukWvh3epBUty |
Copyright (c) 2024, Oğuzhan Eroğlu meowingcate@gmail.com (https://github.com/rohanrhu)
BigCat is licensed under the MIT License. See LICENSE file for more information.