bignum

Rails: Storing a 256 bit checksum as binary in database

夙愿已清 提交于 2019-12-11 03:35:18
问题 I'm trying to store a SHA-2 256 bit checksum in a column: create_table :checksums do |t| t.binary :value, :null => false, :limit => 32 end I'm storing in the value like so: c = Checksum.new big_num = Digest::SHA2.new.update("some string to be checksum'd").hexdigest.to_i(16) c.value = big_num On the assignment of big_num to c.value I get: NoMethodError: undefined method `gsub' for #<Bignum:0x00000001ea48f8> Anybody know what I'm doing wrong? 回答1: If you're going to be storing your SHA2 in a

long <-> str binary conversion

这一生的挚爱 提交于 2019-12-08 20:30:21
问题 Is there any lib that convert very long numbers to string just copying the data? These one-liners are too slow: def xlong(s): return sum([ord(c) << e*8 for e,c in enumerate(s)]) def xstr(x): return chr(x&255) + xstr(x >> 8) if x else '' print xlong('abcd'*1024) % 666 print xstr(13**666) 回答1: You want the struct module. packed = struct.pack('l', 123456) assert struct.unpack('l', packed)[0] == 123456 回答2: How about from binascii import hexlify, unhexlify def xstr(x): hex = '%x' % x return

Why does division become faster with very large numbers

允我心安 提交于 2019-12-07 18:01:48
问题 I wanted to see how constant the division operation is, so I ran this code import time def di(n): n/101 i = 10 while i < 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000: start = time.clock() di(i) end = time.clock() print("On " + str(i) + " " + str(end-start)) i *= 10000 And this was the output I got On 10 5.98714047756e-06 On 100000 4.7041818038e-06 On 1000000000 2.56591734753e-06 On 10000000000000 2.99357023878e-06 On 100000000000000000 2

Accessing the highest digits of large numbers from Python long

≯℡__Kan透↙ 提交于 2019-12-07 07:49:30
问题 I'm working with numbers with tens of thousands of digits in python. The long type works beautifully in performing math on these numbers, however I'm unable to access the highest digits of these numbers in a sufficiently fast way. Note that I don't know exactly how many digits the number contains. The "highest digits" refers to the digits in the most significant place, the lowest digits can be accessed quickly using modulus. I can think of two ways to access these digits in python but they're

Division of a big number of 100 digits stored as string

送分小仙女□ 提交于 2019-12-07 05:04:56
问题 I have a 100 digit number stored as string. I want to divide this number with an integer less than 10. How do I efficiently divide a big integer stored as a string with an integer? 回答1: You can check the big integer library. You can use this library in a C++ program to do arithmetic on integers of size limited only by your computer's memory. The library provides BigUnsigned and BigInteger classes that represent nonnegative integers and signed integers, respectively. Most of the C++ arithmetic

RangeError: bignum too big to convert into `long'

大城市里の小女人 提交于 2019-12-06 14:38:34
num = "0000001000000000011000000000000010010011000011110000000000000000" for n in 0...num.length temp = num[n] dec = dec + temp*(2**(num.length - n - 1)) end puts dec When i am running this code in irb the following error message is the output. and when i compiled the same logic in python it is working absolutely fine. I have Googled "RangeError: bignum too big to convert into `long': but didn't find the relevant answer. Please help me :( Thanks in Advance. RangeError: bignum too big to convert into long' from (irb):4:in *' from (irb):4:in block in irb_binding' from (irb):2:in each' from (irb)

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

不想你离开。 提交于 2019-12-06 13:01:08
问题 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

How can I compute double factorials in Perl?

对着背影说爱祢 提交于 2019-12-06 04:32:02
Given Wikipedia's discussion of Double Factorial s, can anyone suggest where I might find a bignum version of this for Perl, or else suggest how it might be written? Perl will handle whatever your C compiler can handle, for anything bigger you should be using Math::BigInt . I would recommend you read perlnumber . A definition for the double factorial (in perl golf): sub f{$_[0]&&$_[0]>=2?$_[0]*f($_[0]-2):1} Here are lots of alternative approaches to implementing Fast Factorial Functions . The Poor Man's algorithm may be a good choice for you, as it uses no Big-Integer library and can be easily

How to seed the PRNG for BN_generate_prime

◇◆丶佛笑我妖孽 提交于 2019-12-06 03:45:39
问题 I have not been able to find an answer as to what is used to generate the primes with BN_generate_prime in openssl/bn.h. Also, how would I seed whatever PRNG that this function uses? Separate question but relevant to my code (I'm writing a program to generate RSA key pairs): how would I check if the high order bit is set in a BIGNUM? Say I generate a 512 bit prime. Would I use BN_is_bit_set(prime, 512)? Thanks 回答1: BN_generate_prime is a deprecated function, says here. Also, it is defined in

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

你离开我真会死。 提交于 2019-12-06 03:32:24
问题 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