JavaScript library to manipulate big integers. Based on jsbn
made by Tom Wu
Official documentation:
JavaScript Big Integer library based upon Tom Wu's work.
License: Other
JavaScript library to manipulate big integers. Based on jsbn
made by Tom Wu
Official documentation:
Can we add isBigInteger? As a work-around, what do you recommend?
See pointbiz/bitaddress.org#132
The same issue exists in this repository.
There was some discussion about changing this to be backed by a Buffer
. I think that'll actually be slower since operations will then be preformed on a 8 bit numbers as opposed to what it uses now.
By analyzing the code, it does appear to in fact take advantage of JavaScript's capability of handling integers up to 53 bits. So porting to JavaScript native typed arrays may actually introduce a performance penalty.
Maybe there may be a way to leverage some of the concepts of asm.js? Not sure. It's worth looking at potential changes and benchmarking them.
Could be improved: http://cryptocoinjs.com/modules/misc/bigi/
File on Github: https://github.com/cryptocoinjs/cryptocoinjs.com/blob/gh-pages/markdown/modules/misc/bigi.md
Having a big integer, this is the string representation (10 radix): "61112663125671126633722436054102056730607837166180410453588415302234407842306"
The method toDERInteger()
which convert to the bigendian bytes would return:
[0, -121, 28, -118, -93, -127, -101, 97, 127, 69, -4, -15, 127, -126, 42, -104, -89, 8, 44, 66, 30, 73, -84, -5, -54, -93, -87, 83, -28, 113, 30, 50, 2]
But the same method from pointycastle returns:
https://github.com/PointyCastle/pointycastle/blob/master/lib/src/utils.dart#L21
unsigned:
[135, 28, 138, 163, 129, 155, 97, 127, 69, 252, 241, 127, 130, 42, 152, 167, 8, 44, 66, 30, 73, 172, 251, 202, 163, 169, 83, 228, 113, 30, 50, 2]
convert to signed:
[-121, 28, -118, -93, -127, -101, 97, 127, 69, -4, -15, 127, -126, 42, -104, -89, 8, 44, 66, 30, 73, -84, -5, -54, -93, -87, 83, -28, 113, 30, 50, 2]
Wondering what is the leading 0...
I have added bigi flow definitions here
(not really an issue, just wanting to let you know)
Seems like there are issues in converting buffers to BigInteger instances.
Below you can see the sample code example and the output of it in node.js environment.
'use strict';
const BigInteger = require('bigi');
const bigNegativeNumberString = '-12345678900';
const bigIRepresentation = new BigInteger(bigNegativeNumberString);
console.log([
bigIRepresentation.toString(),
BigInteger.fromBuffer(bigIRepresentation.toBuffer()).toString(),
BigInteger.fromHex(bigIRepresentation.toHex()).toString()
]);
Output:
[ '-12345678900', '1087165948876', '1087165948876' ]
The toByteArrayUnsigned() seems broken:
b=BigInteger('1234567890',10)
b.toByteArrayUnsigned()
results in: [73, -106, 2, -46] while there should be no negative numbers.
In fact, it gives the same result than toByteArray()
I have a private key of type buffer.
In my local env, when running gatsby build
the line BigInteger.fromBuffer(privateKey)
console logs this:
BigInteger {0: 5218218, 1: 6291014, 2: 34656281, 3: 1776116, 4: 53518743, 5: 10808093, 6: 62725491, 7: 63913863, 8: 50275223, 9: 1979384, t: 10, s: 0}
When I am in production, run with gatsby build
, the same exact line outputs this:
n {0: 5218218, 1: 6291014, 2: 34656281, 3: 1776116, 4: 53518743, 5: 10808093, 6: 62725491, 7: 63913863, 8: 50275223, 9: 1979384, t: 10, s: 0}
so the ecsda signature fails, saying it expects a BigInteger
and is receiving type n
Until we have a constructor: #18
... or a fromInt
method, what is the best way to create a BigInteger from a number (even one with a decimal place)?
Here is my work-around, but it produced a number that was much too large in the second case:
BigInteger("11").toString() == "11"
BigInteger("1.1").toString() == "11"
Both lines return true
TypeError: Cannot read property 'nextBytes' of undefined
at BigInteger.bnpFromNumber [as fromNumber] (node_modules/bigi/lib/bigi.js:775:6)
at new BigInteger (node_modules/bigi/lib/bigi.js:7:36)
Is it meant to be supported?
It appears the Miller-Rabin test implemented in bigi always chooses the base from the set of primes < 1000, however it is possible to construct a composite that appears prime relative to any fixed set of witnesses, and the paper https://math.dartmouth.edu/~carlp/PDF/reliable.pdf has a proof that there exist integers with no small witnesses at all.
The best fix is probably to choose the witnesses randomly from the range [2,n)
which avoids the problem entirely.
I see byteCount method is listed in the document. But when I tried to use it, it throws error:
Object has no method 'byteCount'
Thanks :)
It would be nice if you could initialize bigi with an intereger like so
var bi = require('bigi');
var aNumber = bi(9);
Currently if you try this you get
typeError: Cannot call method 'nextBytes' of undefined
at BigInteger.bnpFromNumber [as fromNumber] (/home/null/code/bigi/lib/bigi.js:775:7)
at new BigInteger (/home/null/code/bigi/lib/bigi.js:7:36)
at BigInteger (/home/null/code/bigi/lib/bigi.js:4:12)
For reference: cryptocoinjs/ecurve#25
@dcousens thoughts?
Code is like this below
const num1 = 726
const BigIntegerOne = bigi.fromHex(toHex(num1))
function toHex(num) {
let hex = num.toString(16)
return hex
}
When I used the method 'fromHex', I found that it thow an error, "Incomplete hex".
So, I don't know what does it mean.
Could you please tell me the reason why you handle this error message.
Hope see you response in few time.
Thank you.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.