BigNum in C#. Port of bn.js. Public domain.
- Overview
- Installation
- Notation
- Instructions
- Fast reduction
- System requirements
- Development and testing
- Contributors
The primary goal of this project is to produce a translation of bn.js to C# which is as close as possible to the original implementation.
You can install BNSharp via NuGet:
package manager:
$ PM> Install-Package BNSharp
NET CLI:
$ dotnet add package BNSharp
There are several prefixes to instructions that affect the way they work. Here is the list of them in the order of appearance in the function name:
i
- perform operation in-place, storing the result in the host object (on which the method was invoked). Might be used to avoid number allocation costsu
- unsigned, ignore the sign of operands when performing operation, or always return positive value. Second case applies to reduction operations likeMod()
. In such cases if the result will be negative - modulo will be added to the result to make it positive
n
- the argument of the function must be a plain JavaScript Number. Decimals are not supported.rn
- both argument and return value of the function are plain JavaScript Numbers. Decimals are not supported.
a.Iadd(b)
- perform addition ona
andb
, storing the result ina
a.Umod(b)
- reducea
modulob
, returning positive valuea.Iushln(13)
- shift bits ofa
left by 13
Prefixes/postfixes are put in parens at the end of the line. endian
- could be
either LittleEndian
or BigEndian
.
a.Clone()
- clone numbera.ToString(base, length)
- convert to base-string and pad with zeroesa.ToNumber()
- convert to Number (limited to 53 bits)a.ToJSON()
- convert to JSON compatible hex string (alias ofToString(16)
)a.ToArray(endian, length)
- convert to byteArray
, and optionally zero pad to length, throwing if already exceedinga.BitLength()
- get number of bits occupieda.ZeroBits()
- return number of less-significant consequent zero bits (example:1010000
has 4 zero bits)a.ByteLength()
- return number of bytes occupieda.IsNeg()
- true if the number is negativea.IsEven()
- no commentsa.IsOdd()
- no commentsa.IsZero()
- no commentsa.Cmp(b)
- compare numbers and return-1
(a<
b),0
(a==
b), or1
(a>
b) depending on the comparison result (Ucmp
,Cmpn
)a.Lt(b)
-a
less thanb
(n
)a.Lte(b)
-a
less than or equalsb
(n
)a.Gt(b)
-a
greater thanb
(n
)a.Gte(b)
-a
greater than or equalsb
(n
)a.Eq(b)
-a
equalsb
(n
)a.ToTwos(width)
- convert to two's complement representation, wherewidth
is bit widtha.FromTwos(width)
- convert from two's complement representation, wherewidth
is the bit widthBN.IsBN(object)
- returns true if the suppliedobject
is a BN instanceBN.Max(a, b)
- returna
ifa
bigger thanb
BN.Min(a, b)
- returna
ifa
less thanb
a.Neg()
- negate sign (i
)a.Abs()
- absolute value (i
)a.Add(b)
- addition (i
,n
,in
)a.Sub(b)
- subtraction (i
,n
,in
)a.Mul(b)
- multiply (i
,n
,in
)a.Sqr()
- square (i
)a.Pow(b)
- raisea
to the power ofb
a.Div(b)
- divide (Divn
,Idivn
)a.Mod(b)
- reduct (u
,n
) (but noUmodn
)a.Divmod(b)
- quotient and modulus obtained by dividinga.DivRound(b)
- rounded division
a.Or(b)
- or (i
,u
,iu
)a.And(b)
- and (i
,u
,iu
,Andln
) (NOTE:Andln
is going to be replaced withAndn
in future)a.Xor(b)
- xor (i
,u
,iu
)a.Setn(b, value)
- set specified bit tovalue
a.Shln(b)
- shift left (i
,u
,iu
)a.Shrn(b)
- shift right (i
,u
,iu
)a.Testn(b)
- test if specified bit is seta.Maskn(b)
- clear bits with indexes higher or equal tob
(i
)a.Bincn(b)
- add1 << b
to the numbera.Notn(w)
- not (for the width specified byw
) (i
)
a.Gcd(b)
- GCDa.Egcd(b)
- Extended GCD results ({ A: ..., B: ..., Gcd: ... }
)a.Invm(b)
- inversea
modulob
When doing lots of reductions using the same modulo, it might be beneficial to use some tricks: like Montgomery multiplication, or using special algorithm for Mersenne Prime.
To enable this trick one should create a reduction context:
var red = BN.Red(num);
where num
is just a BN instance.
Or:
var red = BN.Red(primeName);
Where primeName
is either of these Mersenne Primes:
'k256'
'p224'
'p192'
'p25519'
Or:
var red = BN.Mont(num);
To reduce numbers with Montgomery trick. .Mont()
is generally faster than
.Red(num)
, but slower than BN.Red(primeName)
.
Before performing anything in reduction context - numbers should be converted to it. Usually, this means that one should:
- Convert inputs to reducted ones
- Operate on them in reduction context
- Convert outputs back from the reduction context
Here is how one may convert numbers to red
:
var redA = a.ToRed(red);
Where red
is a reduction context created using instructions above
Here is how to convert them back:
var a = redA.FromRed();
Most of the instructions from the very start of this readme have their counterparts in red context:
a.RedAdd(b)
,a.RedIAdd(b)
a.RedSub(b)
,a.RedISub(b)
a.RedShl(num)
a.RedMul(b)
,a.RedIMul(b)
a.RedSqr()
,a.RedISqr()
a.RedSqrt()
- square root modulo reduction context's primea.RedInvm()
- modular inverse of the numbera.RedNeg()
a.RedPow(b)
- modular exponentiation
Optimized for elliptic curves that work with 256-bit numbers. There is no limitation on the size of the numbers.
BNSharp supports:
- Net 6
Make sure to rebuild projects every time you change code for testing.
To run tests:
$ dotnet test