天道酬勤,学无止境

bignum

将大数提高到大幂并对其进行大量修改?(Raising large number to large power and mod it by a large number?)

问题 我被困在可能很简单的问题上。 我得到了 3 个大数(A、B、C),所有整数,我需要执行以下操作:A 到 B 的幂并将结果乘以 C,然后检查结果是否等于 1。这是我的代码: double power = fmod((pow((double)A,(double)B)),(double)C); if (power != 1){ printf("Something!\n"); } 它不起作用(我尝试了小数字,例如 17 由 28 提供动力并由 29 进行模块化)。 对此有什么建议吗? 回答1 试试这个(为了避免算术溢出): unsigned long long power = 1; A %= C; while (B > 0) { power = (power * A) % C; B--; } 您可以通过以下方式进一步提高运行时性能: unsigned long long power = 1; A %= C; while (B > 0) { if (B & 1) power = (power * A) % C; B >>= 1; A = (A * A) % C; } 回答2 Double 的最小和最大尺寸分别为 -1.7*10^308 和 1.7*10^308。 如果你需要更大的,你可以尝试 long long。 不知道你为什么使用 fmod。 但这应该做你想做的。 double

2021-06-12 23:06:24    分类:技术分享    c   bignum

Openssl, Invalid arguments ' Candidates are: int BN_set_word(bignum_st *, ?) '

I am using OpenSSL for a cuda project. I just imported all the project from win to linux (Eclipse) I solved all the dependencies except this annoying error: Invalid arguments ' Candidates are: int BN_set_word(bignum_st *, ?) ' for this line: BN_set_word(two, 2); and the function itself says in the bn.h int BN_set_word(BIGNUM *a, BN_ULONG w); Where BN_ULONG is defined as: #define BN_ULONG unsigned long Neither it works if I do something like unsigned long q = 2; BN_set_word(two, q); Because it returns Invalid arguments ' Candidates are: int BN_set_word(bignum_st *, ?) ' or BN_ULONG q = 2; BN

2021-06-12 11:53:49    分类:问答    c++   c   openssl   bignum

Restore a number from several its remainders (chinese remainder theorem)

I have a long integer number, but it is stored not in decimal form, but as set of remainders. So, I have not the N number, but set of such remainders: r_1 = N % 2147483743 r_2 = N % 2147483713 r_3 = N % 2147483693 r_4 = N % 2147483659 r_5 = N % 2147483647 r_6 = N % 2147483629 I know, that N is less than multiplication of these primes, so chinese remainder theorem does work here ( http://en.wikipedia.org/wiki/Chinese_remainder_theorem ). How can I restore N in decimal, if I have this 6 remainders? The wonderful will be any program to do this (C/C+GMP/C++/perl/java/bc). For example, what minimal

2021-06-10 04:23:08    分类:问答    math   gmp   bignum   arbitrary-precision   chinese-remainder-theorem

Convert really big number from binary to decimal and print it

I know how to convert binary to decimal. I know at least 2 methods: table and power ;-) I want to convert binary to decimal and print this decimal. Moreover, I'm not interested in this `decimal'; I want just to print it. But, as I wrote above, I know only 2 methods to convert binary to decimal and both of them required addition. So, I'm computing some value for 1 or 0 in binary and add it to the remembered value. This is a thin place. I have a really-really big number (1 and 64 zeros). While converting I need to place some intermediate result in some 'variable'. In C, I have an `int' type

2021-06-09 21:09:06    分类:问答    c   binary   decimal   bignum   bcd

Convert a big number given as a string to an OpenSSL BIGNUM

I am trying to convert a string p_str representing a big integer to a BIGNUM p using the OpenSSL library. #include <stdio.h> #include <openssl/bn.h> int main () { /* I shortened the integer */ unsigned char *p_str = "82019154470699086128524248488673846867876336512717"; BIGNUM *p = BN_bin2bn(p_str, sizeof(p_str), NULL); BN_print_fp(stdout, p); puts(""); BN_free(p); return 0; } Compiled it with: gcc -Wall -Wextra -g -o convert convert.c -lcrypto But, when I execute it, I get the following result: 3832303139313534

2021-06-02 11:22:45    分类:问答    c   openssl   bignum

How to serialize the GMP mpf type?

It seems that GMP provides only string serialization of the mpf (floating point) type: mpf_get_str(), mpf_class::get_str() The mpz (integer) type has an additional interface for raw bytes: mpz_out_raw() http://gmplib.org/manual/Function-Index.html Am I missing something? Does anyone know of another library that can serialize GMP floats? Does anyone know of another bignum lib that offers robust serialization? Edit: I'd be happy with serializing MPFR's mpfr_t, as well, which similarly only seems to offer string output: http://www.mpfr.org/mpfr-current/mpfr.html#Function-Index

2021-06-02 10:45:34    分类:问答    c++   c   serialization   bignum   gmp

custom data type in C

I am working with cryptography and need to use some really large numbers. I am also using the new Intel instruction for carryless multiplication that requires m128i data type which is done by loading it with a function that takes in floating point data as its arguments. I need to store 2^1223 integer and then square it and store that value as well. I know I can use the GMP library but I think it would be faster to create two data types that both store values like 2^1224 and 2^2448. It will have less overhead.I am going to using karatsuba to multiply the numbers so the only operation I need to

2021-06-01 21:06:51    分类:问答    c   types   bignum

SHA256 Hash results different across Android & iOS for Big numbers

I'm trying to Hash a BigInteger/BigNum and I'm getting different results in Android/iOS. I need to get the same Hash result so that both the apps work as per the SRP protocol. On closer inspection it is working fine for positive numbers but not working for negative numbers (first nibble greater than 7). Not sure which one is correct and which one is to be adjusted to match with the other. Android: void hashBigInteger(String s) { try { BigInteger a = new BigInteger(s, 16); MessageDigest sha = MessageDigest.getInstance("SHA-256"); byte[] b = a.toByteArray(); sha.update(b, 0, b.length); byte[]

2021-05-25 04:36:59    分类:问答    android   ios   hash   biginteger   bignum

Emacs / elisp中的bignum(bignum in emacs/elisp)

问题 emacs是否支持不适合整数的大数? 如果可以,我该如何使用它们? 回答1 Emacs Lispers对Emacs缺乏bignum处理感到沮丧: calc.el提供了非常好的bignum功能。 calc.el是GNU Emacs发行版的一部分。 有关可用功能,请参见其源代码。 您可以通过键入Mx quick-calc立即开始使用它。 您可能还需要检查bigint.el软件包,这是用于处理bignum的非标准轻量级实现。 回答2 Emacs 27.1原生支持bignums(请参阅Emacs的NEWS文件): ** Emacs Lisp整数现在可以是任意大小。 Emacs使用GNU多精度(GMP)库来支持整数,该整数的大小太大以至于无法原生支持。 本机支持的整数称为“ fixnums”,较大的整数为“ bignums”。 新的谓词“ bignump”和“ fixnump”可用于区分这两种类型的整数。 现在,bignums有意义的所有算术,比较和逻辑(也称为“按位”)操作都支持fixnums和bignums。 但是,请注意,与fixnums不同,bignums不会与'eq'相等,而必须使用'eql'。 (当然,在数字上用'='进行比较都可以。) 由于较大的bignum占用大量内存,因此Emacs限制了允许Lisp程序创建的最大bignum的大小。 新变量'integer-width

2021-05-17 22:12:27    分类:技术分享    elisp   biginteger   bignum

JavaScript中的大整数(更多2 ^ 53-1)(Large integers in javascript (more the 2^53-1))

问题 在javascript中使用大整数进行操作的一般原则是什么? 像在bigint的库中一样? 我自己怎么做? 回答1 您可以看一下此实现。 您可能还会发现其他实现很有用。 回答2 我过去使用的另一个选项是通过jsonp将这些操作传递给计算服务器。 如果您使用的是如此大量的数据,则可能需要提高性能和精度,这可以为您提供帮助。

2021-05-09 02:41:55    分类:技术分享    javascript   biginteger   bignum