BNSharp 1.0.0
dotnet add package BNSharp --version 1.0.0
NuGet\Install-Package BNSharp -Version 1.0.0
<PackageReference Include="BNSharp" Version="1.0.0" />
paket add BNSharp --version 1.0.0
#r "nuget: BNSharp, 1.0.0"
// Install BNSharp as a Cake Addin #addin nuget:?package=BNSharp&version=1.0.0 // Install BNSharp as a Cake Tool #tool nuget:?package=BNSharp&version=1.0.0
BNSharp
BigNum in C#. Port of bn.js. Public domain.
Documentation
- Overview
- Installation
- Notation
- Instructions
- Fast reduction
- System requirements
- Development and testing
- Contributors
Overview
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.
Installation
You can install BNSharp via NuGet:
package manager:
$ PM> Install-Package BNSharp
NET CLI:
$ dotnet add package BNSharp
Notation
Prefixes
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
Postfixes
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.
Examples
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
Instructions
Prefixes/postfixes are put in parens at the end of the line. endian
- could be
either LittleEndian
or BigEndian
.
Utilities
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
Arithmetics
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
Bit operations
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
)
Reduction
a.Gcd(b)
- GCDa.Egcd(b)
- Extended GCD results ({ A: ..., B: ..., Gcd: ... }
)a.Invm(b)
- inversea
modulob
Fast reduction
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.
Reduction context
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)
.
Converting numbers
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();
Red instructions
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
Number Size
Optimized for elliptic curves that work with 256-bit numbers. There is no limitation on the size of the numbers.
System requirements
BNSharp supports:
- Net 6
Development and testing
Make sure to rebuild projects every time you change code for testing.
Testing
To run tests:
$ dotnet test
Contributors
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 265 | 6/21/2022 |