Nethermind.Numerics.Int256 1.12.0

dotnet add package Nethermind.Numerics.Int256 --version 1.12.0
                    
NuGet\Install-Package Nethermind.Numerics.Int256 -Version 1.12.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="Nethermind.Numerics.Int256" Version="1.12.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Nethermind.Numerics.Int256" Version="1.12.0" />
                    
Directory.Packages.props
<PackageReference Include="Nethermind.Numerics.Int256" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Nethermind.Numerics.Int256 --version 1.12.0
                    
#r "nuget: Nethermind.Numerics.Int256, 1.12.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.
#:package Nethermind.Numerics.Int256@1.12.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Nethermind.Numerics.Int256&version=1.12.0
                    
Install as a Cake Addin
#tool nuget:?package=Nethermind.Numerics.Int256&version=1.12.0
                    
Install as a Cake Tool

Int256

Tests / Publish Nethermind.Numerics.Int256

High-performance 256-bit arithmetic for .NET.

Int256 provides UInt256 and Int256: unsigned and signed 256-bit value types used by the Nethermind Ethereum execution client. Bring the arithmetic behind EVM execution to your own .NET applications, from blockchain tooling to binary protocols and numeric workloads that need up to 256 bits.

Why Int256?

  • 256 bits, stored inline. Readonly structs with a 32-byte representation. Core arithmetic works directly on fixed-width limbs without allocating a backing array for each result.
  • Full-width modular arithmetic. UInt256.AddMod and UInt256.MultiplyMod preserve the intermediate sum or product before reduction, including products up to 512 bits wide.
  • Optimized for the hardware you run. Specialized x64 and ARM64 paths use hardware intrinsics where available, with software fallbacks. A separate RISC-V zkVM build tunes execution for guest workloads.
  • Made for hot paths. in/out arithmetic APIs and span-based byte conversion let callers control copies and buffers.
  • Correctness you can inspect. Tests compare arithmetic against System.Numerics.BigInteger, and CI exercises Windows, Linux, macOS, ARM64, and configurations with hardware intrinsics disabled.

Get started

dotnet add package Nethermind.Numerics.Int256
using System;
using Nethermind.Int256;

UInt256 amount = UInt256.Parse("1000000000000000000");
UInt256 total = amount * 3UL;
Console.WriteLine(total); // 3000000000000000000

Int256 delta = -42L;
Console.WriteLine(delta + (Int256)10L); // -32

// Reduce the full 512-bit product, without truncating it to 256 bits first.
UInt256 modulus = 97UL;
UInt256.MultiplyMod(UInt256.MaxValue, UInt256.MaxValue, modulus, out UInt256 remainder);
Console.WriteLine(remainder); // 11

// Write directly into a caller-owned buffer and read it back.
Span<byte> bytes = stackalloc byte[32];
total.ToBigEndian(bytes);
UInt256 decoded = new(bytes, isBigEndian: true);
Console.WriteLine(decoded == total); // True

Choose the right arithmetic

Need API
Unsigned values from 0 to 2²⁵⁶ − 1 UInt256
Signed values from −2²⁵⁵ to 2²⁵⁵ − 1 Int256
Detect unsigned overflow or underflow UInt256.AddOverflow / UInt256.SubtractUnderflow
Modular addition, multiplication, or exponentiation AddMod, MultiplyMod, ExpMod
Read or write binary values Byte-span constructor, ToBigEndian(Span<byte>), ToLittleEndian(Span<byte>)
Interoperate with arbitrary-precision code Explicit conversions to and from BigInteger
Write code generic over number types INumber<T> and IBinaryInteger<T>, with CreateChecked, CreateSaturating, and CreateTruncating

The arithmetic has explicit fixed-width semantics. For UInt256:

  • + and * retain the low 256 bits on overflow.
  • The - operator throws on underflow; Subtract returns the wrapped result, while SubtractUnderflow also reports whether underflow occurred.
  • Division by zero throws DivideByZeroException.
  • AddMod and MultiplyMod throw for a zero modulus, and (a * b) % m can differ from MultiplyMod(a, b, m, out result).

Int256 operators wrap in two's complement, including Int256.MinValue / -1, which gives MinValue as the EVM's SDIV does.

Divide truncates toward zero and Mod takes the sign of the dividend. Both throw DivideByZeroException for a zero divisor.

In a checked context, or a project built with CheckForOverflowUnderflow, the operators that would otherwise wrap throw OverflowException instead, as they do for long and Int128. So does the explicit conversion of a negative Int256 to UInt256.

Code written against INumber<T> or IBinaryInteger<T> gets the same semantics. Shifts follow EVM rules there too: a count of 256 or more gives zero (or the sign fill for Int256 >>) rather than being masked to the width.

Both types are ISpanFormattable and IUtf8SpanFormattable. The D, G, X, and B formats are written directly from the limbs, as UInt128 and Int128 write them, without allocating when formatting into a span; a negative Int256 shows all 256 bits under X and B. The culture formats (N, C, E, F, P, G with a precision, and custom formats) go through BigInteger in the standard build and throw FormatException in the RISC-V zkVM build, which keeps BigInteger out of guest images.

See UInt256 and Int256 for the full API.

Performance

The BenchmarkDotNet suite includes comparisons against BigInteger for arithmetic and modular operations, plus targeted benchmarks for operand widths, comparisons, and byte conversion.

Run the suite on your hardware, or select an operation:

dotnet run -c Release --project src/Nethermind.Int256.Benchmark -- --list flat
dotnet run -c Release --project src/Nethermind.Int256.Benchmark -- --filter '*MultiplyModUnsigned*'

Results depend on operand size, CPU instruction support, and runtime version.

RISC-V zkVM variant

The NuGet package includes standard and RISC-V zkVM implementations with the same assembly identity, selected at build time. The standard implementation is the default. For RISC-V zkVM guest workloads, set EnableZkEvm in your project or a shared Directory.Build.props:

<PropertyGroup>
  <EnableZkEvm>true</EnableZkEvm>
</PropertyGroup>

A guest whose zkVM has Zbb's rev8 and performs misaligned word accesses natively can switch big-endian conversion to a word load plus rev8 by stubbing UInt256.get_HasByteReverse to true in its ILC substitutions:

<linker>
  <assembly fullname="Nethermind.Int256">
    <type fullname="Nethermind.Int256.UInt256">
      <method signature="System.Boolean get_HasByteReverse()" body="stub" value="true" />
    </type>
  </assembly>
</linker>

Pass the file to ILC from the guest project:

<ItemGroup>
  <IlcArg Include="--substitution:$(MSBuildProjectDirectory)/ILLink.Substitutions.xml" />
</ItemGroup>

ILC ignores a file without the <linker> root and warns IL2009 if the method is not found.

Build and contribute

Use the SDK specified in global.json. From the repository root:

dotnet build src/Nethermind.Int256/Nethermind.Int256.csproj -c Release
dotnet test --project src/Nethermind.Int256.Tests/Nethermind.Int256.Tests.csproj -c Release

Bug reports, regression tests, and performance improvements are welcome. For arithmetic bugs, include the operands, operation, expected result, and actual result. For optimizations, include before/after benchmarks with the hardware, runtime version, commit, and benchmark command, plus coverage for boundary values.

License

This project is licensed under the MIT license.

Portions of UInt256 incorporate material from uint256, used under BSD-3-Clause. See THIRD-PARTY-NOTICES for the full text.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

NuGet packages (8)

Showing the top 5 NuGet packages that depend on Nethermind.Numerics.Int256:

Package Downloads
NBip32Fast.NistP256

Blazing fast NistP256 (SecP256r1) bip-32 keypath derivation subpackage

Gnosis.Circles.Nethermind.Plugin.Common

Common utilities and interfaces for Circles indexing

Gnosis.Circles.Nethermind.Plugin.CirclesV2.LBP

Liquidity bootstrap pool indexing for Circles V2

Gnosis.Circles.Nethermind.Plugin.CirclesV2.Erc20Lift

ERC20 lift indexing for Circles V2

Gnosis.Circles.Nethermind.Plugin.CirclesV2.StandardTreasury

Standard treasury indexing for Circles V2

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on Nethermind.Numerics.Int256:

Repository Stars
NethermindEth/nethermind
A robust, high-performance execution client for Ethereum node operators.
Version Downloads Last Updated
1.12.0 132,902 10/20/2026
1.11.0 705,594 10/6/2026
1.10.0 207,410 10/5/2026
1.9.0 1,558,899 9/11/2026
1.8.0 97,441 9/10/2026
1.7.0 344,468 9/3/2026
1.6.0 716,378 7/31/2026
1.5.0 4,246,166 2/23/2026
1.4.0 182,501 1/5/2026
1.3.6 485,780 5/15/2025
1.3.5 31,482 4/29/2025
1.3.4 75,874 3/17/2025
1.3.3 128,155 1/16/2025
1.3.2 13,287 1/7/2025
1.3.1 336 1/6/2025 1.3.1 is deprecated because it has critical bugs.
1.3.0 18,906 12/23/2024
1.2.0 422,072 1/23/2024
1.1.1 85,202 11/1/2023
1.1.0 189,375 1/20/2023
1.0.0 131,025 8/18/2022