2020-03-10 19:31:01 +01:00
|
|
|
module big
|
2019-11-07 20:04:18 +01:00
|
|
|
|
|
|
|
// Wrapper for https://github.com/kokke/tiny-bignum-c
|
2020-04-10 12:09:04 +02:00
|
|
|
#flag -I @VROOT/thirdparty/bignum
|
|
|
|
#flag @VROOT/thirdparty/bignum/bn.o
|
2019-11-07 20:04:18 +01:00
|
|
|
#include "bn.h"
|
2021-02-01 14:50:41 +01:00
|
|
|
|
2020-05-18 21:38:06 +02:00
|
|
|
struct C.bn {
|
2020-12-22 07:24:41 +01:00
|
|
|
mut:
|
2019-11-07 20:04:18 +01:00
|
|
|
array [32]u32
|
|
|
|
}
|
|
|
|
|
2020-12-14 10:53:18 +01:00
|
|
|
// Big unsigned integer number.
|
2020-05-18 21:38:06 +02:00
|
|
|
type Number = C.bn
|
|
|
|
|
2020-12-14 10:53:18 +01:00
|
|
|
fn C.bignum_init(n &Number)
|
|
|
|
|
|
|
|
fn C.bignum_from_int(n &Number, i u64)
|
|
|
|
|
|
|
|
fn C.bignum_to_int(n &Number) int
|
|
|
|
|
|
|
|
fn C.bignum_from_string(n &Number, s byteptr, nbytes int)
|
|
|
|
|
|
|
|
fn C.bignum_to_string(n &Number, s byteptr, maxsize int)
|
|
|
|
|
|
|
|
// 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 20:04:18 +01:00
|
|
|
fn C.bignum_inc(n &Number)
|
2020-12-14 10:53:18 +01:00
|
|
|
|
|
|
|
// n--
|
2019-11-07 20:04:18 +01:00
|
|
|
fn C.bignum_dec(n &Number)
|
|
|
|
|
2020-12-14 10:53:18 +01: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 08:44:59 +01:00
|
|
|
// new returns a bignum, initialized to 0
|
2020-03-10 19:31:01 +01:00
|
|
|
pub fn new() Number {
|
2019-11-07 20:04:18 +01:00
|
|
|
return Number{}
|
|
|
|
}
|
2020-12-14 10:53:18 +01:00
|
|
|
|
2020-12-22 08:44:59 +01:00
|
|
|
// conversion actions to/from big numbers:
|
|
|
|
// from_int converts an ordinary int number `i` to big.Number
|
2019-11-07 20:04:18 +01:00
|
|
|
pub fn from_int(i int) Number {
|
|
|
|
n := Number{}
|
2020-12-14 10:53:18 +01:00
|
|
|
C.bignum_from_int(&n, i)
|
2019-11-07 20:04:18 +01:00
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2020-12-22 08:44:59 +01:00
|
|
|
// from_u64 converts an ordinary u64 number `u` to big.Number
|
2019-11-07 20:04:18 +01:00
|
|
|
pub fn from_u64(u u64) Number {
|
|
|
|
n := Number{}
|
2020-12-14 10:53:18 +01:00
|
|
|
C.bignum_from_int(&n, u)
|
2019-11-07 20:04:18 +01:00
|
|
|
return n
|
|
|
|
}
|
2020-12-14 10:53:18 +01:00
|
|
|
|
2020-12-22 08:44:59 +01:00
|
|
|
// from_hex_string converts a hex string to big.Number
|
|
|
|
pub fn from_hex_string(input string) Number {
|
2020-12-16 02:23:02 +01: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 20:04:18 +01:00
|
|
|
n := Number{}
|
|
|
|
C.bignum_from_string(&n, s.str, s.len)
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2020-12-22 08:44:59 +01: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 10:26:20 +01:00
|
|
|
n = (n * big.ten) + d
|
2020-12-22 08:44:59 +01:00
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
// .int() converts (a small) big.Number `n` to an ordinary integer.
|
2019-11-07 20:04:18 +01:00
|
|
|
pub fn (n Number) int() int {
|
|
|
|
r := C.bignum_to_int(&n)
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2020-12-14 10:53:18 +01:00
|
|
|
const (
|
|
|
|
ten = from_int(10)
|
|
|
|
)
|
|
|
|
|
2020-12-22 08:44:59 +01:00
|
|
|
// .str returns a decimal representation of the big unsigned integer number n.
|
2019-11-07 20:04:18 +01:00
|
|
|
pub fn (n Number) str() string {
|
2020-12-14 10:53:18 +01:00
|
|
|
if n.is_zero() {
|
|
|
|
return '0'
|
|
|
|
}
|
|
|
|
mut digits := []byte{}
|
|
|
|
mut x := n.clone()
|
|
|
|
div := Number{}
|
|
|
|
for !x.is_zero() {
|
2021-01-25 10:26:20 +01:00
|
|
|
mod := divmod(&x, &big.ten, &div)
|
2020-12-14 10:53:18 +01:00
|
|
|
digits << byte(mod.int()) + `0`
|
|
|
|
x = div
|
|
|
|
}
|
|
|
|
return digits.reverse().bytestr()
|
2019-11-07 20:04:18 +01:00
|
|
|
}
|
|
|
|
|
2020-12-22 08:44:59 +01:00
|
|
|
// .hexstr returns a hexadecimal representation of the bignum `n`
|
2019-11-07 20:04:18 +01:00
|
|
|
pub fn (n Number) hexstr() string {
|
2020-08-28 08:03:22 +02:00
|
|
|
mut buf := [8192]byte{}
|
2020-12-22 08:44:59 +01:00
|
|
|
// NB: C.bignum_to_string(), returns the HEXADECIMAL representation of the bignum n
|
2020-12-14 10:53:18 +01:00
|
|
|
C.bignum_to_string(&n, buf, 8192)
|
|
|
|
s := tos_clone(buf)
|
|
|
|
if s.len == 0 {
|
|
|
|
return '0'
|
|
|
|
}
|
2019-11-07 20:04:18 +01:00
|
|
|
return s
|
|
|
|
}
|
2020-12-14 10:53:18 +01:00
|
|
|
|
|
|
|
// //////////////////////////////////////////////////////////
|
2019-11-07 20:04:18 +01:00
|
|
|
// overloaded ops for the numbers:
|
2020-12-22 08:27:50 +01:00
|
|
|
pub fn (a Number) + (b Number) Number {
|
2019-11-07 20:04:18 +01:00
|
|
|
c := Number{}
|
|
|
|
C.bignum_add(&a, &b, &c)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-12-22 08:27:50 +01:00
|
|
|
pub fn (a Number) - (b Number) Number {
|
2019-11-07 20:04:18 +01:00
|
|
|
c := Number{}
|
|
|
|
C.bignum_sub(&a, &b, &c)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-12-22 08:27:50 +01:00
|
|
|
pub fn (a Number) * (b Number) Number {
|
2019-11-07 20:04:18 +01:00
|
|
|
c := Number{}
|
|
|
|
C.bignum_mul(&a, &b, &c)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-12-22 08:27:50 +01:00
|
|
|
pub fn (a Number) / (b Number) Number {
|
2019-11-07 20:04:18 +01:00
|
|
|
c := Number{}
|
|
|
|
C.bignum_div(&a, &b, &c)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-12-22 08:27:50 +01:00
|
|
|
pub fn (a Number) % (b Number) Number {
|
2019-11-07 20:04:18 +01:00
|
|
|
c := Number{}
|
|
|
|
C.bignum_mod(&a, &b, &c)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-12-14 10:53:18 +01:00
|
|
|
pub fn divmod(a &Number, b &Number, c &Number) Number {
|
2019-11-07 20:04:18 +01:00
|
|
|
d := Number{}
|
2020-12-14 10:53:18 +01:00
|
|
|
C.bignum_divmod(a, b, c, &d)
|
2019-11-07 20:04:18 +01:00
|
|
|
return d
|
|
|
|
}
|
2020-12-14 10:53:18 +01:00
|
|
|
|
|
|
|
// //////////////////////////////////////////////////////////
|
2019-11-07 20:04:18 +01:00
|
|
|
pub fn cmp(a Number, b Number) int {
|
2020-12-14 10:53:18 +01:00
|
|
|
return C.bignum_cmp(&a, &b)
|
2019-11-07 20:04:18 +01:00
|
|
|
}
|
2020-12-14 10:53:18 +01:00
|
|
|
|
2019-11-07 20:04:18 +01:00
|
|
|
pub fn (a Number) is_zero() bool {
|
2020-03-10 15:47:18 +01:00
|
|
|
return C.bignum_is_zero(&a) != 0
|
2019-11-07 20:04:18 +01:00
|
|
|
}
|
2020-12-14 10:53:18 +01:00
|
|
|
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut a Number) inc() {
|
2019-11-07 20:04:18 +01:00
|
|
|
C.bignum_inc(a)
|
|
|
|
}
|
2020-12-14 10:53:18 +01:00
|
|
|
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut a Number) dec() {
|
2019-11-07 20:04:18 +01:00
|
|
|
C.bignum_dec(a)
|
|
|
|
}
|
2020-12-14 10:53:18 +01:00
|
|
|
|
2019-11-07 20:04:18 +01:00
|
|
|
pub fn pow(a Number, b Number) Number {
|
|
|
|
c := Number{}
|
2020-12-14 10:53:18 +01:00
|
|
|
C.bignum_pow(&a, &b, &c)
|
2019-11-07 20:04:18 +01:00
|
|
|
return c
|
|
|
|
}
|
2020-12-14 10:53:18 +01:00
|
|
|
|
2019-11-07 20:04:18 +01:00
|
|
|
pub fn (a Number) isqrt() Number {
|
|
|
|
b := Number{}
|
2020-12-14 10:53:18 +01:00
|
|
|
C.bignum_isqrt(&a, &b)
|
2019-11-07 20:04:18 +01:00
|
|
|
return b
|
|
|
|
}
|
2020-12-14 10:53:18 +01:00
|
|
|
|
|
|
|
// //////////////////////////////////////////////////////////
|
2019-11-07 20:04:18 +01:00
|
|
|
pub fn b_and(a Number, b Number) Number {
|
|
|
|
c := Number{}
|
2020-12-14 10:53:18 +01:00
|
|
|
C.bignum_and(&a, &b, &c)
|
2019-11-07 20:04:18 +01:00
|
|
|
return c
|
|
|
|
}
|
2020-12-14 10:53:18 +01:00
|
|
|
|
2019-11-07 20:04:18 +01:00
|
|
|
pub fn b_or(a Number, b Number) Number {
|
|
|
|
c := Number{}
|
2020-12-14 10:53:18 +01:00
|
|
|
C.bignum_or(&a, &b, &c)
|
2019-11-07 20:04:18 +01:00
|
|
|
return c
|
|
|
|
}
|
2020-12-14 10:53:18 +01:00
|
|
|
|
2019-11-07 20:04:18 +01:00
|
|
|
pub fn b_xor(a Number, b Number) Number {
|
|
|
|
c := Number{}
|
2020-12-14 10:53:18 +01:00
|
|
|
C.bignum_xor(&a, &b, &c)
|
2019-11-07 20:04:18 +01:00
|
|
|
return c
|
|
|
|
}
|
2020-12-14 10:53:18 +01:00
|
|
|
|
2019-11-07 20:04:18 +01:00
|
|
|
pub fn (a Number) lshift(nbits int) Number {
|
|
|
|
b := Number{}
|
2020-12-14 10:53:18 +01:00
|
|
|
C.bignum_lshift(&a, &b, nbits)
|
2019-11-07 20:04:18 +01:00
|
|
|
return b
|
|
|
|
}
|
2020-12-14 10:53:18 +01:00
|
|
|
|
2019-11-07 20:04:18 +01:00
|
|
|
pub fn (a Number) rshift(nbits int) Number {
|
|
|
|
b := Number{}
|
2020-12-14 10:53:18 +01:00
|
|
|
C.bignum_rshift(&a, &b, nbits)
|
2019-11-07 20:04:18 +01:00
|
|
|
return b
|
|
|
|
}
|
2020-12-14 10:53:18 +01:00
|
|
|
|
2019-11-07 20:04:18 +01:00
|
|
|
pub fn (a Number) clone() Number {
|
|
|
|
b := Number{}
|
2020-12-14 10:53:18 +01:00
|
|
|
C.bignum_assign(&b, &a)
|
2019-11-07 20:04:18 +01:00
|
|
|
return b
|
|
|
|
}
|
2020-12-14 10:53:18 +01:00
|
|
|
|
|
|
|
// //////////////////////////////////////////////////////////
|
2020-03-10 14:40:30 +01:00
|
|
|
pub fn factorial(nn Number) Number {
|
2019-11-07 20:04:18 +01:00
|
|
|
mut n := nn.clone()
|
|
|
|
mut a := nn.clone()
|
|
|
|
n.dec()
|
2020-12-14 10:53:18 +01:00
|
|
|
mut i := 1
|
2019-11-07 20:04:18 +01:00
|
|
|
for !n.is_zero() {
|
|
|
|
res := a * n
|
|
|
|
n.dec()
|
|
|
|
a = res
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2020-03-10 14:40:30 +01:00
|
|
|
pub fn fact(n int) Number {
|
2020-12-14 10:53:18 +01:00
|
|
|
return factorial(from_int(n))
|
2019-11-07 20:04:18 +01:00
|
|
|
}
|