BNSharp 1.0.0

dotnet add package BNSharp --version 1.0.0
NuGet\Install-Package BNSharp -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BNSharp" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add BNSharp --version 1.0.0
#r "nuget: BNSharp, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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


alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image

BigNum in C#. Port of bn.js. Public domain.



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


$ dotnet add package BNSharp

or download source code.



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 costs
  • u - unsigned, ignore the sign of operands when performing operation, or always return positive value. Second case applies to reduction operations like Mod(). 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 on a and b, storing the result in a
  • a.Umod(b) - reduce a modulo b, returning positive value
  • a.Iushln(13) - shift bits of a 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 number
  • a.ToString(base, length) - convert to base-string and pad with zeroes
  • a.ToNumber() - convert to Number (limited to 53 bits)
  • a.ToJSON() - convert to JSON compatible hex string (alias of ToString(16))
  • a.ToArray(endian, length) - convert to byte Array, and optionally zero pad to length, throwing if already exceeding
  • a.BitLength() - get number of bits occupied
  • a.ZeroBits() - return number of less-significant consequent zero bits (example: 1010000 has 4 zero bits)
  • a.ByteLength() - return number of bytes occupied
  • a.IsNeg() - true if the number is negative
  • a.IsEven() - no comments
  • a.IsOdd() - no comments
  • a.IsZero() - no comments
  • a.Cmp(b) - compare numbers and return -1 (a < b), 0 (a == b), or 1 (a > b) depending on the comparison result (Ucmp, Cmpn)
  • a.Lt(b) - a less than b (n)
  • a.Lte(b) - a less than or equals b (n)
  • a.Gt(b) - a greater than b (n)
  • a.Gte(b) - a greater than or equals b (n)
  • a.Eq(b) - a equals b (n)
  • a.ToTwos(width) - convert to two's complement representation, where width is bit width
  • a.FromTwos(width) - convert from two's complement representation, where width is the bit width
  • BN.IsBN(object) - returns true if the supplied object is a BN instance
  • BN.Max(a, b) - return a if a bigger than b
  • BN.Min(a, b) - return a if a less than b


  • 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) - raise a to the power of b
  • a.Div(b) - divide (Divn, Idivn)
  • a.Mod(b) - reduct (u, n) (but no Umodn)
  • a.Divmod(b) - quotient and modulus obtained by dividing
  • a.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 with Andn in future)
  • a.Xor(b) - xor (i, u, iu)
  • a.Setn(b, value) - set specified bit to value
  • a.Shln(b) - shift left (i, u, iu)
  • a.Shrn(b) - shift right (i, u, iu)
  • a.Testn(b) - test if specified bit is set
  • a.Maskn(b) - clear bits with indexes higher or equal to b (i)
  • a.Bincn(b) - add 1 << b to the number
  • a.Notn(w) - not (for the width specified by w) (i)


  • a.Gcd(b) - GCD
  • a.Egcd(b) - Extended GCD results ({ A: ..., B: ..., Gcd: ... })
  • a.Invm(b) - inverse a modulo b

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.


var red = BN.Red(primeName);

Where primeName is either of these Mersenne Primes:

  • 'k256'
  • 'p224'
  • 'p192'
  • 'p25519'


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 prime
  • a.RedInvm() - modular inverse of the number
  • a.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.


To run tests:

$ dotnet test



Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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 224 6/21/2022