我有一些使用libgmp的代码.在某些时候,用户可以请求非常大数量的阶乘.不幸的是,这导致libgmp引发中止信号.
例如,以下代码:
#include <cmath>
#include <gmp.h>
#include <iostream>
int main() {
mpz_t result;
mpz_init(result);
mpz_fac_ui(result, 20922789888000);
std::cout << mpz_get_si(result) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
结果是:
$ ./test
gmp: overflow in mpz type
Aborted
Run Code Online (Sandbox Code Playgroud)
显然,产生的数字真的很大.反正是否比中止更优雅地处理错误.这是一个基于GUI的应用程序,它中止是处理此类问题的最不可取的方法.
我需要GMP在我正在使用的iphone程序中使用,但不知道从哪里开始.我知道我必须为设备构建一个版本,并为模拟器构建一个版本,但这就像我所知道的那样.我试过环顾四周但却找不到多少.
有没有人在这里成功建立GMP,iphone这将指导我完成这个过程?
我看到为iOS构建GMP但它对我不起作用.我以为我使用以下方法成功构建了它:
./configure CC="/Developer/Platforms/iPhoneOS.platform/Developer/usr/llvm-gcc-4.2/bin/llvm-gcc-4.2" CXX="/Developer/Platforms/iPhoneOS.platform/Developer/usr/llvm-gcc-4.2/bin/llvm-g++-4.2" CPP="/Developer/Platforms/iPhoneOS.platform/Developer/usr/llvm-gcc-4.2/bin/llvm-gcc-4.2 -E" LD="$IOS/usr/bin/ld" CPPFLAGS="-arch armv7 -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk -miphoneos-version-min=4.2" AR="$IOS/usr/bin/ar" NM="$IOS/usr/bin/nm" NMEDIT="$IOS/usr/bin/nmedit" LIBTOOL="$IOS/usr/bin/libtool" LIPO="$IOS/usr/bin/lipo" OTOOL="$IOS/usr/bin/otool" RANLIB="$IOS/usr/bin/ranlib" STRIP="$IOS/usr/bin/strip" --disable-shared --enable-static --prefix=$IOS/usr/local --host=none-apple-darwin10
然后进行安装.
在/usr/local/lib我有libgmp.a.但是当我拖到xcode编译时它告诉我:
ignoring file /Users/daniel/Desktop/libgmp.a, file was built for archive which is not the architecture being linked (armv7) (由于某种原因,我放入桌面,然后拖入我的xcode项目)
这让我疯了.有人可以帮忙吗?请记住,使用这些工具我是一个完整的菜鸟.
假设我得到了帮助,我可以做到这一点.我怎么去把它变成了一个框架,将在一个工作armv6,armv7和simulator(i386)?
提前致谢.
担
更新:其他人在这里遇到问题的方法是我如何解决它.
首先交叉编译GMP 3次,将架构设置为armv6.armv7和i386分别.构建arm版本时,将标志-DNO_ASM添加到CPPFLAGS.
一起找到三个.a文件的LIPO
使用gmp.g和gmpgxx.h将.a文件导入XCode.
完成
要编译我自己的 gcc,我需要gmp。因此,我下载了 gmp 的 tarball,并通过常规的配置、制作和安装步骤安装了它。然而,之后,我无法通过输入“which gmp”在系统中看到任何 gmp。所以我的问题是 gmp 隐藏在哪里?我使用的是 CentOS 5.6。
好的,我可以在/usr/local/include和/usr/local/lib中看到gmp的头文件和库文件了。我使用过--with-gmp-include=/usr/local/include --with-gmp-lib=/usr/local/lib,但 gcc 的配置仍然抱怨找不到 gmp。这里发生了什么?
所以根据我之前提出的问题,我下载并设置了boost.我有这个代码:
#include <stdlib.h>
#include <boost\multiprecision\gmp.hpp>
using namespace std;
using namespace boost::multiprecision;
void main() {
mpz_int N(567014094304930933548155069494723691156768423655208899778686163624192868328194365094673392756508907687565332345345678900976543567890976543565789054335678097654680986564323567890876532456890775646780976543556789054367890765435689876545898876587907876535976565578907654538790878656543687656543467898786565457897675645657689756456578656456768654657898865567689656890795587907654678798765787897865654657897654678965465786867278762795432151914451557727529104757415030674806148138138281214236089749601911974949125689884222023119844272122501649909415937);
}
Run Code Online (Sandbox Code Playgroud)
但是当我编译它时说
IntelliSense: integer constant is too large
Run Code Online (Sandbox Code Playgroud)
如果mpz_int不是我应该使用的,那么我应该使用什么来提升大量的int?
我正在编写一个使用Paillier Cryptosystem来加密矩阵的程序.要加密50 x 50矩阵,大约需要12秒!考虑到我打算加密大小为5000 x 5000及以上的矩阵,这是痛苦的.在Xcode上分析程序我发现这个paillier_get_rand_devurandom()是罪魁祸首.
这是调用跟踪快照:

这是此特定Paillier C库函数的源代码
void paillier_get_rand_devurandom( void* buf, int len )
{
paillier_get_rand_file(buf, len, "/dev/urandom");
}
void paillier_get_rand_file( void* buf, int len, char* file )
{
FILE* fp;
void* p;
fp = fopen(file, "r");
p = buf;
while( len )
{
size_t s;
s = fread(p, 1, len, fp);
p += s;
len -= s;
}
fclose(fp);
}
Run Code Online (Sandbox Code Playgroud)
如果是的话,
<http://www.en.wikipedia.org/wiki/Paillier_cryptosystem>
Paillier Cryptosytem C library : <http://acsc.cs.utexas.edu/libpaillier/>
Run Code Online (Sandbox Code Playgroud)
我已经读过使用dev/random的随机数生成很慢而使用dev/urandom则更快.在我的情况下,两者都同样缓慢.这个随机数生成能否更快?
编辑:这是一个例子
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include<paillier.h> …Run Code Online (Sandbox Code Playgroud) 我正在尝试为arm64构建一个用于iOS的C库(GMP 6.0.0).我正在使用下面的调用运行configure脚本(使用xcrun --find找到编译器).
./configure \
CC="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" \
CPP="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -E" \
CPPFLAGS="-target arm64-apple-darwin -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.1.sdk/ -miphoneos-version-min=7.0" \
--host=aarch64-apple-darwin
Run Code Online (Sandbox Code Playgroud)
然而,这在以下行失败("长期可靠性测试1"):
checking compiler /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -O2 -pedantic -target arm64-apple-darwin -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.1.sdk/ -miphoneos-version-min=7.0... no, long long reliability test 1
configure: error: could not find a working compiler, see config.log for details
Run Code Online (Sandbox Code Playgroud)
完整的config.log 在这里可用.它显示了长期可靠性测试编译的多个警告和错误,包括以下内容:
conftest.c:9:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
f(){static const struct{t1 n;t1 src[9];t1 want[9];}d[]={{1,{0},{1}},};t1 got[9];int i;
^
conftest.c:10:44: error: implicit declaration of function 'h' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
for(i=0;i<1;i++){if(e(got,got,9,d[i].n)==0)h();g(i,d[i].src,d[i].n,got,d[i].want,9);if(d[i].n)h();}} …Run Code Online (Sandbox Code Playgroud) 我在 64 位系统上运行 Arch Linux。
我尝试遵循以下说明:
# download gmp, unpack
cd $NDK_ROOT/sources
wget ftp://ftp.gmplib.org/pub/gmp-4.2.4/gmp-4.2.4.tar.bz2
tar -jxvf gmp-4.2.4.tar.bz2
mv gmp-4.2.4 gmp
cd gmp
CFLAGS=-UHAVE_LOCALE_H ./configure --host=arm-unknown-linux --prefix=$SYSROOT/usr
make
make install
Run Code Online (Sandbox Code Playgroud)
在这里找到;但是,我收到错误(此时我不知道所有输出是否相关。如果太多,我深表歉意):
[jav@jav gmp]$ CFLAGS=-UHAVE_LOCALE_H ./configure --host=arm-unknown-linux --prefix=$SYSROOT/usr
configure: WARNING: If you wanted to set the --build type, don't use --host.
If a cross compiler is detected then cross compile mode will be used.
checking build system type... x86_64-unknown-linux-gnu
checking host system type... arm-unknown-linux-gnu
checking for a BSD-compatible install... /usr/bin/install -c …Run Code Online (Sandbox Code Playgroud) 我遇到了这段代码.
typedef __mpz_struct MP_INT;
typedef __mpz_struct mpz_t[1];
Run Code Online (Sandbox Code Playgroud)
这里的struct __mpz_struct是一个结构,它被定义为单个元素的数组.我理解这是一个通过C中的引用传递的技巧.然后mpz_t被用作声明变量的类型并将它们作为参数传递.此外,还有一个评论
/*
MP_INT*, MP_RAT* and MP_FLOAT* are used because they don't have side-effects
of single-element arrays mp*_t
*/
Run Code Online (Sandbox Code Playgroud)
他们在谈论什么样的副作用?
我使用这个程序来存储一个 mpz 值但是当我添加一个 0 ( 400000000000000000000000000000000000000 而不是 40000000000000000000000000000000000000000000000000000000000000000000000000000000000000
free(): 在 tcache 2 中检测到双空闲
中止(核心转储)
#include <iostream>
#include <gmpxx.h>
#include <vector>
using namespace std;
int main(const int argc, const char * const argv[])
{
char *str= (char*)malloc(sizeof(char)*1024);
mpz_class l;
l=40000000000000000000000000000000000000_mpz;
mpz_set_str(l.get_mpz_t(), str, 10);
cout<<endl<<str;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
是否有可能存储大量数字?
谢谢
该包的当前版本gmp不支持集合操作,例如intersect、setdiff等。我正在使用数字序列进行一些工作(有关示例,请参阅OEIS)并且需要处理大整数的大集合。我目前一直坚持使用各种循环来生成所需的差异或交叉点;虽然我可能可以生成编译的(Rccp 等)代码,但我希望在现有的函数和包中找到一种方法R。