indutny / bn.js

BigNum in pure javascript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bug in setn

wbl opened this issue · comments

Example transcript:
`$node
Welcome to Node.js v14.13.0.
Type ".help" for more information.

var BN = require('bn.js')
undefined
var a = new BN('03cf24d034379e619ee0af9f633d1eb61aff399abb9eb91081c7fb8af7c171bb', 16)
undefined
a.toString(16, 16)
'03cf24d034379e619ee0af9f633d1eb61aff399abb9eb91081c7fb8af7c171bb'
a.testn(252)
false
b = a.setn(252)
BN {
negative: 0,
words: [
63009211, 33481405,
59836444, 40562298,
35323705, 13584301,
34273782, 41518715,
13644855, 62409,
0
],
length: 10,
red: null
}
b.toString(16,16)
'03cf24d034379e619ee0af9f633d1eb61aff399abb9eb91081c7fb8af7c171bb'
a
BN {
negative: 0,
words: [
63009211, 33481405,
59836444, 40562298,
35323705, 13584301,
34273782, 41518715,
13644855, 62409,
0
],
length: 10,
red: null
}
a.toString(16,16)
'03cf24d034379e619ee0af9f633d1eb61aff399abb9eb91081c7fb8af7c171bb'
a.eq(b)
true
b.testn(252)
false
b.setn(252)
BN {
negative: 0,
words: [
63009211, 33481405,
59836444, 40562298,
35323705, 13584301,
34273782, 41518715,
13644855, 62409,
0
],
length: 10,
red: null
}
b.setn(252).testn(252)
false

`

Apologies for the terrible formatting: I can't quite figure out how to get a literal block.

Sorry, looks like docs are wrong. setn also requires a value to which bit should be set.
Working example:

const a = new BN('03cf24d034379e619ee0af9f633d1eb61aff399abb9eb91081c7fb8af7c171bb', 16)
console.log(a.bitLength())
console.log(a.testn(252))
a.setn(252, 1)
console.log(a.bitLength())
console.log(a.testn(252))

Output:

250
false
253
true