peterolson / BigInteger.js

An arbitrary length integer library for Javascript
The Unlicense
1.12k stars 187 forks source link

Problems with bigInterger #140

Closed ying2025 closed 6 years ago

ying2025 commented 6 years ago
var hexn = "EEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C" +
                "9C256576D674DF7496EA81D3383B4813D692C6E0E0D5D8E250B98BE4" +
                "8E495C1D6089DAD15DC7D7B46154D6B6CE8EF4AD69B15D4982559B29" +
                "7BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA9A" +
                "FD5138FE8376435B9FC61D2FC0EB06E3";
N = Str2Bytes(hexn);
var srv = new Array(20);
SetBytes(srv,N)
function SetBytes(b, buf) {
    var z = new Array(buf.length)
    var k = 0, s = 0;
    console.log(bigInterger(N));
    var d = 0;
    // console.log(d);
    var _S = 0;
    for (var i = buf.length; i > 0; i--) {
        d |= buf[i-1] << s;
        s += 8;
        if (s == _S++ * 8) {
            z[k] = d;
            k++;
            s = 0;
            d = 0;
        }
        console.log(buf[i], s, d);
    }
    if (k < z.length) {
        z[k] = d;
    }
    return z
}
function Str2Bytes(str) {  
    var pos = 0;  
    var len = str.length;  
    if(len%2 != 0) {  
       return null;   
    }  
    len /= 2;  
    var hexA = new Array();  
    for(var i=0; i<len; i++) {  
       var s = str.substr(pos, 2);  
       var v = parseInt(s, 16);  
       hexA.push(v);  
       pos += 2;  
    }  
    return hexA;  
} 

[ 238,
  175,
  10,
  185,
  173,
  179,
  141,
  214,
  156,
  51,
  248,
  10,
  250,
  143,
  197,
  232,
  96,
  114,
  97,
  135,
  117,
  255,
  60,
  11,
  158,
  162,
  49,
  76,
  156,
  37,
  101,
  118,
  214,
  116,
  223,
  116,
  150,
  234,
  129,
  211,
  56,
  59,
  72,
  19,
  214,
  146,
  198,
  224,
  224,
  213,
  216,
  226,
  80,
  185,
  139,
  228,
  142,
  73,
  92,
  29,
  96,
  137,
  218,
  209,
  93,
  199,
  215,
  180,
  97,
  84,
  214,
  182,
  206,
  142,
  244,
  173,
  105,
  177,
  93,
  73,
  130,
  85,
  155,
  41,
  123,
  207,
  24,
  133,
  197,
  41,
  245,
  102,
  102,
  14,
  87,
  236,
  104,
  237,
  188,
  60,
  ... 28 more items ]
undefined 8 227
227 16 1763
6 24 15402723
235 32 -1058339101
192 40 -1058339089
47 48 -1058332689
29 56 -1058070545
198 64 -537976849
159 72 -537976833
91 80 -537960449
67 88 -536911873
118 96 -536911873
131 104 -536911873
254 112 -536903681
56 120 -536903681
81 128 -32769
253 136 -32769
154 144 -1
170 152 -1
110 160 -1
151 168 -1
244 176 -1
203 184 -1
212 192 -1
47 200 -1
192 208 -1
108 216 -1
114 224 -1
5 232 -1
60 240 -1
188 248 -1
237 256 -1
104 264 -1
236 272 -1
87 280 -1
14 288 -1
102 296 -1
102 304 -1
245 312 -1
41 320 -1
197 328 -1
133 336 -1
24 344 -1
207 352 -1
123 360 -1
41 368 -1
155 376 -1
85 384 -1
130 392 -1
73 400 -1
93 408 -1
177 416 -1
105 424 -1
173 432 -1
244 440 -1
142 448 -1
206 456 -1
182 464 -1
214 472 -1
84 480 -1
97 488 -1
180 496 -1
215 504 -1
199 512 -1
93 520 -1
209 528 -1
218 536 -1
137 544 -1
96 552 -1
29 560 -1
92 568 -1
73 576 -1
142 584 -1
228 592 -1
139 600 -1
185 608 -1
80 616 -1
226 624 -1
216 632 -1
213 640 -1
224 648 -1
224 656 -1
198 664 -1
146 672 -1
214 680 -1
19 688 -1
72 696 -1
59 704 -1
56 712 -1
211 720 -1
129 728 -1
234 736 -1
150 744 -1
116 752 -1
223 760 -1
116 768 -1
214 776 -1
118 784 -1
101 792 -1
37 800 -1
156 808 -1
76 816 -1
49 824 -1
162 832 -1
158 840 -1
11 848 -1
60 856 -1
255 864 -1
117 872 -1
135 880 -1
97 888 -1
114 896 -1
96 904 -1
232 912 -1
197 920 -1
143 928 -1
250 936 -1
10 944 -1
248 952 -1
51 960 -1
156 968 -1
214 976 -1
141 984 -1
179 992 -1
173 1000 -1
185 1008 -1
10 1016 -1
175 1024 -1
[Finished in 0.2s]
ying2025 commented 6 years ago

Why it overflowed?

peterolson commented 6 years ago

Sorry, it's unclear to me what you are asking. Could you explain what the purpose of the code is, and point out what exactly it's doing that is not expected?

ying2025 commented 6 years ago

Why it appear -1058339101 in this line "235 32 -1058339101" ?

peterolson commented 6 years ago

I don't know exactly what you are trying to do with your code, but this line is invalid:

console.log(bigInterger(N));

Firstly, bigInterger is not spelled correctly. The function should be named bigInt.

Second, N is an array of integers. An array is not a valid argument to pass into bigInt. You can only pass in a string, a number, or another bigInt.

Since this log statement is the only place where you are using the bigInteger function, any other issues in the code are not caused by this library.