bignum

Is there a good way to optimize the multiplication of two BigNums?

拜拜、爱过 提交于 2021-02-07 19:46:02
问题 I have a class BigNum : struct BigNum{ vector <int> digits; BigNum(vector <int> data){ for(int item : data){d.push_back(item);} } int get_digit(size_t index){ return (index >= d.size() ? 0 : d[index]); } }; and I'm trying to write code to multiply two BigNum s. Currently, I've been using the traditional method of multiplication, which is multiplying the first number by each digit of the other and adding it to a running total. Here's my code: BigNum add(BigNum a, BigNum b){ // traditional

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

五迷三道 提交于 2021-02-07 02:58:04
问题 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

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

家住魔仙堡 提交于 2021-02-07 02:55:26
问题 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

Adding floats with gmp gives “correct” results, sort of

偶尔善良 提交于 2020-01-16 00:48:06
问题 In the code below I use mpf_add to add the string representation of two floating values. What I don't understand at this point is why 2.2 + 3.2 = 5.39999999999999999999999999999999999999 . I would have thought that gmp was smart enough to give 5.4 . What am I not comprehending about how gmp does floats? (BTW, when I first wrote this I wasn't sure how to insert a decimal point, thus the plus/minus digit stuff at the end) BSTR __stdcall FBIGSUM(BSTR p1, BSTR p2 ) { USES_CONVERSION; F(n1); F(n2)

What is a convenient base for a bignum library & primality testing algorithm?

折月煮酒 提交于 2020-01-15 06:56:46
问题 I am to program the Solovay-Strassen primality test presented in the original paper on RSA. Additionally I will need to write a small bignum library, and so when searching for a convenient representation for bignum I came across this specification: struct { int sign; int size; int *tab; } bignum; I will also be writing a multiplication routine using the Karatsuba method. So, for my question: What base would be convenient to store integer data in the bignum struct? Note: I am not allowed to

How can I sprintf a big number in Perl?

回眸只為那壹抹淺笑 提交于 2020-01-04 03:54:33
问题 On a Windows 32-bit platform I have to read some numbers that, this was unexpected, can have values as big as 99,999,999,999, but no more. Trying to sprintf("%011d", $myNum) them outputs an overflow: -2147483648. I cannot use the BigInt module because in this case I should deeply change the code. I cannot manage the format as string, sprintf("%011s", $numero) , because the minus sign is incorrectly handled. How can I manage this? Could pack/unpack be of some help? 回答1: Try formatting it as a

Convert Hex to Decimal when no datatype can hold the full number

谁说胖子不能爱 提交于 2020-01-03 03:15:35
问题 I am working with a PIC microprocessor, in C. It's a 16F, so it can't hold integers larger than 32bits (unsigned int32 is the largest datasize available) From a reader, I receive a 5 byte ID code. To transmit it, I have to encoded to BCD, digit by digit. I can't sprint it to a string, as it is larger that the data size, and can't process it. I can't divide it because no operation is defined for it. I can't figure out any solution possible, does anyone have dealt with this before? EDIT: I

How can I printf a Perl bignum without losing precision?

落爺英雄遲暮 提交于 2020-01-01 16:28:47
问题 #!/usr/bin/perl use strict; use warnings; my $s = "1234567890.123456789"; { no bignum; printf "bignum==%s\n", bignum::in_effect() // 0; my $x = $s; printf "%29s\n", $x; printf "%29.9f\n\n", $x; } { use bignum; printf "bignum==%s\n", bignum::in_effect() // 0; my $x = $s; printf "%29s\n", $x; printf "%29.9f\n\n", $x; } My Perl's printf (ActiveState v5.10.1 built for darwin-thread-multi-2level) using the %f conversion doesn't honor my value past the 1e-6 digit, even when using bignum: $ t.pl

How can I printf a Perl bignum without losing precision?

痞子三分冷 提交于 2020-01-01 16:27:05
问题 #!/usr/bin/perl use strict; use warnings; my $s = "1234567890.123456789"; { no bignum; printf "bignum==%s\n", bignum::in_effect() // 0; my $x = $s; printf "%29s\n", $x; printf "%29.9f\n\n", $x; } { use bignum; printf "bignum==%s\n", bignum::in_effect() // 0; my $x = $s; printf "%29s\n", $x; printf "%29.9f\n\n", $x; } My Perl's printf (ActiveState v5.10.1 built for darwin-thread-multi-2level) using the %f conversion doesn't honor my value past the 1e-6 digit, even when using bignum: $ t.pl

Convert really big number from binary to decimal and print it

与世无争的帅哥 提交于 2020-01-01 11:35:29
问题 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