- Zig 100%
| src | added legendre method | |
| .gitignore | initial commit | |
| build.zig | initial commit | |
| build.zig.zon | initial commit | |
| README.md | added README | |
Big Integer Package for Zig
A neither feature complete nor bug free (at least it's very unlikely) big integer package written in Zig.
Memory for integers is always dynamically allocated and must be freed using deinit.
Integer De-/ Serialization
Integers from Strings
To create a new integer from a string one can use the fromStringAlloc function.
consti=tryBigInt.fromStringAlloc("0xaabbccdd",0,allocator);deferi.deinit(std.testing.allocator);The first argument is a string representing an integer. Currently, only binary (0b), octal (0o) and hexadecimal (0x) are supported. The second argument is the base. Use 0 if the base should be derived from the string. The third argument is an allocator used to allocate the memory required to store the integer in memory.
Integers to Strings
To serialize a BigInt into a string, use the formatAlloc method.
conststr=tryi.formatAlloc(16,allocator);deferallocator.free(str);The first argument is the base that should be used to format the integer string and the second argument is an allocator used to allocated the memory required for the string.
Arithmetic Operations
Addition
vari1=tryBigInt.fromStringAlloc("-0xd7b3f9b6be6d0ba69561e026d1a71713f000da0eebcdf648d2f3651614f37f04bf",0,allocator);deferi1.deinit(allocator);vari2=tryBigInt.fromStringAlloc("0xd5612187a8fd23f35e21b1f7d63e224244c37658d6e020657c494a59d52e69b1355ee2b3ec44397b67",0,allocator);deferi2.deinit(allocator);// "0xd5612187a8fd23f2866db84117d1169baf619632053909518c48704ae9607368626b7d9dd750ba76a8"consti3=tryi1.add(i2,std.testing.allocator);deferi3.deinit(allocator);Subtraction
vari1=tryBigInt.fromStringAlloc("0xc9f2c9cd04674edea3fffffff",0,allocator);deferi1.deinit(allocator);vari2=tryBigInt.fromStringAlloc("0xab54a98ceb1f0ad2",0,allocator);deferi2.deinit(allocator);// "0xc9f2c9ccf9b20445d54e0f52d"consti3=tryi1.sub(i2,std.testing.allocator);deferi3.deinit(allocator);Multiplication
vari1=tryBigInt.fromStringAlloc("0xd7b3f9b6be6d0ba69561e026d1a71713f000da0eebcdf648d2f3651614f37f04bf",0,allocator);deferi1.deinit(allocator);vari2=tryBigInt.fromStringAlloc("0xd7b3f9b6be6d0ba69561e026d1a71713f000da0eebcdf648d2f3651614f37f04bf",0,allocator);deferi2.deinit(allocator);// "0xb5bfcbf81cfe76f38b3bf893a9c64ec5e31c53f13bfd68c9dcdf25ce3dd5f99ac8b58dd6b4b1a3c0c687c26e1bf4ded1cbee18ee138e28d61a2920f9f3e04f988681"consti3=tryi1.mul(i2,std.testing.allocator);deferi3.deinit(allocator);Division
Division with Remainder
Compared to most other methods divrem will return a tuple consisting of the result of the division and the remainder.
vari1=tryBigInt.fromStringAlloc("0x9f4f2726179a224501d762422c946590d9bb54a98ceb1f0ad2",0,allocator);deferi1.deinit(allocator);vari2=tryBigInt.fromStringAlloc("0x9f4f2726179a224501d762422c946590d90fffffffffffffff",0,allocator);deferi2.deinit(allocator);constres,constrem=tryi1.divrem(i2,std.testing.allocator);deferres.deinit(allocator);// "0x1"deferrem.deinit(allocator);// "0xab54a98ceb1f0ad3"Remainder
To calculate only the remainder use the remainder method.
vari1=tryBigInt.fromStringAlloc("0x9f4f2726179a224501d762422c946590d9bb54a98ceb1f0ad2",0,allocator);deferi1.deinit(allocator);vari2=tryBigInt.fromStringAlloc("0x9f4f2726179a224501d762422c946590d90fffffffffffffff",0,allocator);deferi2.deinit(allocator);constrem=tryi1.remainder(i2,std.testing.allocator);deferrem.deinit(allocator);// "0xab54a98ceb1f0ad3"Modulo
To calculate n mod m where m is the module use the mod method.
varn=tryBigInt.fromStringAlloc("-0xab54a98ceb1f0ad2",0,allocator);defern.deinit(allocator);varm=tryBigInt.fromStringAlloc("0xc9f2c9cd04674edea3fffffff",0,allocator);deferm.deinit(allocator);consta=tryn.mod(m,std.testing.allocator);defera.deinit(allocator);// "0xc9f2c9ccf9b20445d54e0f52d"Multiplicative Inverse
One can calculate the multiplicative inverse d of a value e where e * d = 1 (mod m) using the mulinv method.
It is important to note that this method will only succeed if e and m do not share a common divisor, i.e. the method will always succeed if one chooses a prime for m.
vare=tryBigInt.fromStringAlloc("0x13cb6d5a698890d16746620b38ad51b169dfde8cd89c474c14a98248f794bcba",0,allocator);defere.deinit(allocator);varm=tryBigInt.fromStringAlloc("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f",0,allocator);deferm.deinit(allocator);constd=trye.mulinv(m,std.testing.allocator);deferd.deinit(allocator);// "0xf38c3f1da477ede38da56515070d2a6d2d0ff4ae146125caf3115b1e8ded63f"// e * d = 1 (mod m)