2020-03-10 21:31:01 +03:00
|
|
|
module big
|
2019-11-07 22:04:18 +03:00
|
|
|
|
|
|
|
// Wrapper for https://github.com/kokke/tiny-bignum-c
|
2021-04-19 19:01:47 +03:00
|
|
|
#flag -I @VEXEROOT/thirdparty/bignum
|
|
|
|
#flag @VEXEROOT/thirdparty/bignum/bn.o
|
2019-11-07 22:04:18 +03:00
|
|
|
#include "bn.h"
|
2021-02-01 16:50:41 +03:00
|
|
|
|
2020-05-18 22:38:06 +03:00
|
|
|
struct C.bn {
|
2020-12-22 09:24:41 +03:00
|
|
|
mut:
|
2019-11-07 22:04:18 +03:00
|
|
|
array [32]u32
|
|
|
|
}
|
|
|
|
|
2020-12-14 12:53:18 +03:00
|
|
|
// Big unsigned integer number.
|
2020-05-18 22:38:06 +03:00
|
|
|
type Number = C.bn
|
|
|
|
|
2020-12-14 12:53:18 +03:00
|
|
|
fn C.bignum_init(n &Number)
|
|
|
|
|
|
|
|
fn C.bignum_from_int(n &Number, i u64)
|
|
|
|
|
|
|
|
fn C.bignum_to_int(n &Number) int
|
|
|
|
|
2021-04-14 12:47:24 +03:00
|
|
|
fn C.bignum_from_string(n &Number, s &char, nbytes int)
|
2020-12-14 12:53:18 +03:00
|
|
|
|
2021-04-14 12:47:24 +03:00
|
|
|
fn C.bignum_to_string(n &Number, s &char, maxsize int)
|
2020-12-14 12:53:18 +03:00
|
|
|
|
|
|
|
// c = a + b
|
|
|
|
fn C.bignum_add(a &Number, b &Number, c &Number)
|
|
|
|
|
|
|
|
// c = a - b
|
|
|
|
fn C.bignum_sub(a &Number, b &Number, c &Number)
|
|
|
|
|
|
|
|
// c = a * b
|
|
|
|
fn C.bignum_mul(a &Number, b &Number, c &Number)
|
|
|
|
|
|
|
|
// c = a / b
|
|
|
|
fn C.bignum_div(a &Number, b &Number, c &Number)
|
|
|
|
|
|
|
|
// c = a % b
|
|
|
|
fn C.bignum_mod(a &Number, b &Number, c &Number)
|
|
|
|
|
|
|
|
// c = a/b d=a%b
|
|
|
|
fn C.bignum_divmod(a &Number, b &Number, c &Number, d &Number)
|
|
|
|
|
|
|
|
// c = a & b
|
|
|
|
fn C.bignum_and(a &Number, b &Number, c &Number)
|
|
|
|
|
|
|
|
// c = a | b
|
|
|
|
fn C.bignum_or(a &Number, b &Number, c &Number)
|
|
|
|
|
|
|
|
// c = a xor b
|
|
|
|
fn C.bignum_xor(a &Number, b &Number, c &Number)
|
|
|
|
|
|
|
|
// b = a << nbits
|
|
|
|
fn C.bignum_lshift(a &Number, b &Number, nbits int)
|
|
|
|
|
|
|
|
// b = a >> nbits
|
|
|
|
fn C.bignum_rshift(a &Number, b &Number, nbits int)
|
|
|
|
|
|
|
|
fn C.bignum_cmp(a &Number, b &Number) int
|
|
|
|
|
|
|
|
fn C.bignum_is_zero(a &Number) int
|
|
|
|
|
|
|
|
// n++
|
2019-11-07 22:04:18 +03:00
|
|
|
fn C.bignum_inc(n &Number)
|
2020-12-14 12:53:18 +03:00
|
|
|
|
|
|
|
// n--
|
2019-11-07 22:04:18 +03:00
|
|
|
fn C.bignum_dec(n &Number)
|
|
|
|
|
2020-12-14 12:53:18 +03:00
|
|
|
// c = a ^ b
|
|
|
|
fn C.bignum_pow(a &Number, b &Number, c &Number)
|
|
|
|
|
|
|
|
// b = integer_square_root_of(a)
|
|
|
|
fn C.bignum_isqrt(a &Number, b &Number)
|
|
|
|
|
|
|
|
// copy src number to dst number
|
|
|
|
fn C.bignum_assign(dst &Number, src &Number)
|
|
|
|
|
2020-12-22 10:44:59 +03:00
|
|
|
// new returns a bignum, initialized to 0
|
2020-03-10 21:31:01 +03:00
|
|
|
pub fn new() Number {
|
2019-11-07 22:04:18 +03:00
|
|
|
return Number{}
|
|
|
|
}
|
2020-12-14 12:53:18 +03:00
|
|
|
|
2020-12-22 10:44:59 +03:00
|
|
|
// conversion actions to/from big numbers:
|
|
|
|
// from_int converts an ordinary int number `i` to big.Number
|
2019-11-07 22:04:18 +03:00
|
|
|
pub fn from_int(i int) Number {
|
|
|
|
n := Number{}
|
2020-12-14 12:53:18 +03:00
|
|
|
C.bignum_from_int(&n, i)
|
2019-11-07 22:04:18 +03:00
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2020-12-22 10:44:59 +03:00
|
|
|
// from_u64 converts an ordinary u64 number `u` to big.Number
|
2019-11-07 22:04:18 +03:00
|
|
|
pub fn from_u64(u u64) Number {
|
|
|
|
n := Number{}
|
2020-12-14 12:53:18 +03:00
|
|
|
C.bignum_from_int(&n, u)
|
2019-11-07 22:04:18 +03:00
|
|
|
return n
|
|
|
|
}
|
2020-12-14 12:53:18 +03:00
|
|
|
|
2020-12-22 10:44:59 +03:00
|
|
|
// from_hex_string converts a hex string to big.Number
|
|
|
|
pub fn from_hex_string(input string) Number {
|
2020-12-16 04:23:02 +03:00
|
|
|
mut s := input.trim_prefix('0x')
|
|
|
|
if s.len == 0 {
|
|
|
|
s = '0'
|
|
|
|
}
|
|
|
|
padding := '0'.repeat((8 - s.len % 8) % 8)
|
|
|
|
s = padding + s
|
2019-11-07 22:04:18 +03:00
|
|
|
n := Number{}
|
2021-04-14 12:47:24 +03:00
|
|
|
C.bignum_from_string(&n, &char(s.str), s.len)
|
2019-11-07 22:04:18 +03:00
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2020-12-22 10:44:59 +03:00
|
|
|
// from_string converts a decimal string to big.Number
|
|
|
|
pub fn from_string(input string) Number {
|
|
|
|
mut n := from_int(0)
|
|
|
|
for _, c in input {
|
|
|
|
d := from_int(int(c - `0`))
|
2021-01-25 12:26:20 +03:00
|
|
|
n = (n * big.ten) + d
|
2020-12-22 10:44:59 +03:00
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
// .int() converts (a small) big.Number `n` to an ordinary integer.
|
2021-07-20 15:08:32 +03:00
|
|
|
pub fn (n &Number) int() int {
|
|
|
|
r := C.bignum_to_int(n)
|
2019-11-07 22:04:18 +03:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2020-12-14 12:53:18 +03:00
|
|
|
const (
|
|
|
|
ten = from_int(10)
|
|
|
|
)
|
|
|
|
|
2020-12-22 10:44:59 +03:00
|
|
|
// .str returns a decimal representation of the big unsigned integer number n.
|
2021-07-20 15:08:32 +03:00
|
|
|
pub fn (n &Number) str() string {
|
2020-12-14 12:53:18 +03:00
|
|
|
if n.is_zero() {
|
|
|
|
return '0'
|
|
|
|
}
|
|
|
|
mut digits := []byte{}
|
|
|
|
mut x := n.clone()
|
|
|
|
div := Number{}
|
|
|
|
for !x.is_zero() {
|
2021-01-25 12:26:20 +03:00
|
|
|
mod := divmod(&x, &big.ten, &div)
|
2020-12-14 12:53:18 +03:00
|
|
|
digits << byte(mod.int()) + `0`
|
|
|
|
x = div
|
|
|
|
}
|
|
|
|
return digits.reverse().bytestr()
|
2019-11-07 22:04:18 +03:00
|
|
|
}
|
|
|
|
|
2020-12-22 10:44:59 +03:00
|
|
|
// .hexstr returns a hexadecimal representation of the bignum `n`
|
2021-07-20 15:08:32 +03:00
|
|
|
pub fn (n &Number) hexstr() string {
|
2020-08-28 09:03:22 +03:00
|
|
|
mut buf := [8192]byte{}
|
2021-02-26 02:28:47 +03:00
|
|
|
mut s := ''
|
|
|
|
unsafe {
|
|
|
|
bp := &buf[0]
|
|
|
|
// NB: C.bignum_to_string(), returns the HEXADECIMAL representation of the bignum n
|
2021-07-20 15:08:32 +03:00
|
|
|
C.bignum_to_string(n, &char(bp), 8192)
|
2021-02-26 02:28:47 +03:00
|
|
|
s = tos_clone(bp)
|
|
|
|
}
|
2020-12-14 12:53:18 +03:00
|
|
|
if s.len == 0 {
|
|
|
|
return '0'
|
|
|
|
}
|
2019-11-07 22:04:18 +03:00
|
|
|
return s
|
|
|
|
}
|
2020-12-14 12:53:18 +03:00
|
|
|
|
|
|
|
// //////////////////////////////////////////////////////////
|
2019-11-07 22:04:18 +03:00
|
|
|
// overloaded ops for the numbers:
|
2021-07-20 15:08:32 +03:00
|
|
|
pub fn (a &Number) + (b &Number) Number {
|
2019-11-07 22:04:18 +03:00
|
|
|
c := Number{}
|
2021-07-20 15:08:32 +03:00
|
|
|
C.bignum_add(a, b, &c)
|
2019-11-07 22:04:18 +03:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2021-07-20 15:08:32 +03:00
|
|
|
pub fn (a &Number) - (b &Number) Number {
|
2019-11-07 22:04:18 +03:00
|
|
|
c := Number{}
|
2021-07-20 15:08:32 +03:00
|
|
|
C.bignum_sub(a, b, &c)
|
2019-11-07 22:04:18 +03:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2021-07-20 15:08:32 +03:00
|
|
|
pub fn (a &Number) * (b &Number) Number {
|
2019-11-07 22:04:18 +03:00
|
|
|
c := Number{}
|
2021-07-20 15:08:32 +03:00
|
|
|
C.bignum_mul(a, b, &c)
|
2019-11-07 22:04:18 +03:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2021-07-20 15:08:32 +03:00
|
|
|
pub fn (a &Number) / (b &Number) Number {
|
2019-11-07 22:04:18 +03:00
|
|
|
c := Number{}
|
2021-07-20 15:08:32 +03:00
|
|
|
C.bignum_div(a, b, &c)
|
2019-11-07 22:04:18 +03:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2021-07-20 15:08:32 +03:00
|
|
|
pub fn (a &Number) % (b &Number) Number {
|
2019-11-07 22:04:18 +03:00
|
|
|
c := Number{}
|
2021-07-20 15:08:32 +03:00
|
|
|
C.bignum_mod(a, b, &c)
|
2019-11-07 22:04:18 +03:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-12-14 12:53:18 +03:00
|
|
|
pub fn divmod(a &Number, b &Number, c &Number) Number {
|
2019-11-07 22:04:18 +03:00
|
|
|
d := Number{}
|
2020-12-14 12:53:18 +03:00
|
|
|
C.bignum_divmod(a, b, c, &d)
|
2019-11-07 22:04:18 +03:00
|
|
|
return d
|
|
|
|
}
|
2020-12-14 12:53:18 +03:00
|
|
|
|
|
|
|
// //////////////////////////////////////////////////////////
|
2021-07-20 15:08:32 +03:00
|
|
|
pub fn cmp(a &Number, b &Number) int {
|
|
|
|
return C.bignum_cmp(a, b)
|
2019-11-07 22:04:18 +03:00
|
|
|
}
|
2020-12-14 12:53:18 +03:00
|
|
|
|
2021-07-20 15:08:32 +03:00
|
|
|
pub fn (a &Number) is_zero() bool {
|
|
|
|
return C.bignum_is_zero(a) != 0
|
2019-11-07 22:04:18 +03:00
|
|
|
}
|
2020-12-14 12:53:18 +03:00
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut a Number) inc() {
|
2021-07-20 15:08:32 +03:00
|
|
|
C.bignum_inc(&a)
|
2019-11-07 22:04:18 +03:00
|
|
|
}
|
2020-12-14 12:53:18 +03:00
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut a Number) dec() {
|
2021-07-20 15:08:32 +03:00
|
|
|
C.bignum_dec(&a)
|
2019-11-07 22:04:18 +03:00
|
|
|
}
|
2020-12-14 12:53:18 +03:00
|
|
|
|
2021-07-20 15:08:32 +03:00
|
|
|
pub fn pow(a &Number, b &Number) Number {
|
2019-11-07 22:04:18 +03:00
|
|
|
c := Number{}
|
2021-07-20 15:08:32 +03:00
|
|
|
C.bignum_pow(a, b, &c)
|
2019-11-07 22:04:18 +03:00
|
|
|
return c
|
|
|
|
}
|
2020-12-14 12:53:18 +03:00
|
|
|
|
2021-07-20 15:08:32 +03:00
|
|
|
pub fn (a &Number) isqrt() Number {
|
2019-11-07 22:04:18 +03:00
|
|
|
b := Number{}
|
2021-07-20 15:08:32 +03:00
|
|
|
C.bignum_isqrt(a, &b)
|
2019-11-07 22:04:18 +03:00
|
|
|
return b
|
|
|
|
}
|
2020-12-14 12:53:18 +03:00
|
|
|
|
|
|
|
// //////////////////////////////////////////////////////////
|
2021-07-20 15:08:32 +03:00
|
|
|
pub fn b_and(a &Number, b &Number) Number {
|
2019-11-07 22:04:18 +03:00
|
|
|
c := Number{}
|
2021-07-20 15:08:32 +03:00
|
|
|
C.bignum_and(a, b, &c)
|
2019-11-07 22:04:18 +03:00
|
|
|
return c
|
|
|
|
}
|
2020-12-14 12:53:18 +03:00
|
|
|
|
2021-07-20 15:08:32 +03:00
|
|
|
pub fn b_or(a &Number, b &Number) Number {
|
2019-11-07 22:04:18 +03:00
|
|
|
c := Number{}
|
2021-07-20 15:08:32 +03:00
|
|
|
C.bignum_or(a, b, &c)
|
2019-11-07 22:04:18 +03:00
|
|
|
return c
|
|
|
|
}
|
2020-12-14 12:53:18 +03:00
|
|
|
|
2021-07-20 15:08:32 +03:00
|
|
|
pub fn b_xor(a &Number, b &Number) Number {
|
2019-11-07 22:04:18 +03:00
|
|
|
c := Number{}
|
2021-07-20 15:08:32 +03:00
|
|
|
C.bignum_xor(a, b, &c)
|
2019-11-07 22:04:18 +03:00
|
|
|
return c
|
|
|
|
}
|
2020-12-14 12:53:18 +03:00
|
|
|
|
2021-07-20 15:08:32 +03:00
|
|
|
pub fn (a &Number) lshift(nbits int) Number {
|
2019-11-07 22:04:18 +03:00
|
|
|
b := Number{}
|
2021-07-20 15:08:32 +03:00
|
|
|
C.bignum_lshift(a, &b, nbits)
|
2019-11-07 22:04:18 +03:00
|
|
|
return b
|
|
|
|
}
|
2020-12-14 12:53:18 +03:00
|
|
|
|
2021-07-20 15:08:32 +03:00
|
|
|
pub fn (a &Number) rshift(nbits int) Number {
|
2019-11-07 22:04:18 +03:00
|
|
|
b := Number{}
|
2021-07-20 15:08:32 +03:00
|
|
|
C.bignum_rshift(a, &b, nbits)
|
2019-11-07 22:04:18 +03:00
|
|
|
return b
|
|
|
|
}
|
2020-12-14 12:53:18 +03:00
|
|
|
|
2021-07-20 15:08:32 +03:00
|
|
|
pub fn (a &Number) clone() Number {
|
2019-11-07 22:04:18 +03:00
|
|
|
b := Number{}
|
2021-07-20 15:08:32 +03:00
|
|
|
C.bignum_assign(&b, a)
|
2019-11-07 22:04:18 +03:00
|
|
|
return b
|
|
|
|
}
|
2020-12-14 12:53:18 +03:00
|
|
|
|
|
|
|
// //////////////////////////////////////////////////////////
|
2021-07-20 15:08:32 +03:00
|
|
|
pub fn factorial(nn &Number) Number {
|
2019-11-07 22:04:18 +03:00
|
|
|
mut n := nn.clone()
|
|
|
|
mut a := nn.clone()
|
|
|
|
n.dec()
|
2020-12-14 12:53:18 +03:00
|
|
|
mut i := 1
|
2019-11-07 22:04:18 +03:00
|
|
|
for !n.is_zero() {
|
|
|
|
res := a * n
|
|
|
|
n.dec()
|
|
|
|
a = res
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2020-03-10 16:40:30 +03:00
|
|
|
pub fn fact(n int) Number {
|
2020-12-14 12:53:18 +03:00
|
|
|
return factorial(from_int(n))
|
2019-11-07 22:04:18 +03:00
|
|
|
}
|
2021-07-19 16:42:37 +03:00
|
|
|
|
|
|
|
// bytes returns an array of the bytes for the number `n`,
|
|
|
|
// in little endian format, where .bytes()[0] is the least
|
|
|
|
// significant byte. The result is NOT trimmed, and will contain 0s, even
|
|
|
|
// after the significant bytes.
|
|
|
|
// This method is faster than .bytes_trimmed(), but may be less convenient.
|
|
|
|
// Example: assert big.from_int(1).bytes()[0] == byte(0x01)
|
|
|
|
// Example: assert big.from_int(1024).bytes()[1] == byte(0x04)
|
|
|
|
// Example: assert big.from_int(1048576).bytes()[2] == byte(0x10)
|
2021-07-20 15:08:32 +03:00
|
|
|
pub fn (n &Number) bytes() []byte {
|
2021-07-19 16:42:37 +03:00
|
|
|
mut res := []byte{len: 128, init: 0}
|
2021-07-20 15:08:32 +03:00
|
|
|
unsafe { C.memcpy(res.data, n, 128) }
|
2021-07-19 16:42:37 +03:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// bytes_trimmed returns an array of the bytes for the number `n`,
|
|
|
|
// in little endian format, where .bytes_trimmed()[0] is the least
|
|
|
|
// significant byte. The result is trimmed, so that *the last* byte
|
|
|
|
// of the result is also the the last meaningfull byte, != 0 .
|
|
|
|
// Example: assert big.from_int(1).bytes_trimmed() == [byte(0x01)]
|
|
|
|
// Example: assert big.from_int(1024).bytes_trimmed() == [byte(0x00), 0x04]
|
|
|
|
// Example: assert big.from_int(1048576).bytes_trimmed() == [byte(0x00), 0x00, 0x10]
|
2021-07-20 15:08:32 +03:00
|
|
|
pub fn (n &Number) bytes_trimmed() []byte {
|
2021-07-19 16:42:37 +03:00
|
|
|
mut res := []byte{len: 128, init: 0}
|
2021-07-20 15:08:32 +03:00
|
|
|
unsafe { C.memcpy(res.data, n, 128) }
|
2021-07-19 16:42:37 +03:00
|
|
|
mut non_zero_idx := 127
|
|
|
|
for ; non_zero_idx >= 0; non_zero_idx-- {
|
|
|
|
if res[non_zero_idx] != 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res.trim(non_zero_idx + 1)
|
|
|
|
return res
|
|
|
|
}
|