nethermindeth / int256 Goto Github PK
View Code? Open in Web Editor NEWA 256-bit integer implementation for .NET
License: MIT License
A 256-bit integer implementation for .NET
License: MIT License
At first let's look to contructor from byte[]
in litle endian mode:
u0 = BinaryPrimitives.ReadUInt64LittleEndian(bytes.Slice(0, 8));
u1 = BinaryPrimitives.ReadUInt64LittleEndian(bytes.Slice(8, 8));
u2 = BinaryPrimitives.ReadUInt64LittleEndian(bytes.Slice(16, 8));
u3 = BinaryPrimitives.ReadUInt64LittleEndian(bytes.Slice(24, 8));
And now to ToLittleEndian
itself:
BinaryPrimitives.WriteUInt64LittleEndian(target.Slice(0, 8), u3);
BinaryPrimitives.WriteUInt64LittleEndian(target.Slice(8, 8), u2);
BinaryPrimitives.WriteUInt64LittleEndian(target.Slice(16, 8), u1);
BinaryPrimitives.WriteUInt64LittleEndian(target.Slice(24, 8), u0);
In constuctor we read u0
from first 8 bytes, but in ToLittleEndian
we write in first 8 bytes u3
value.
So if we trying make UInt256 from ToLittleEndian we get different value.
[Test]
public virtual void CheckToLittleEndian()
{
var v = UInt256.Parse("0xfb5764948f29b38f6f4456eff35caab6741b5a19292862ff4d376a570cd473d7");
var v2 = new UInt256(v.ToLittleEndian());
v2.Should().Be(v);
}
After it get out from preview features.
https://docs.microsoft.com/en-us/dotnet/api/system.inumber-1?view=net-7.0
Issue can be easily tested with this snippet:
double d = 1E+20d;
UInt256 u = (UInt256)d;
Console.WriteLine(u); // 0
When NM client parses something like a Dictionary from JSON where input keys look like: "0x0000000000000000000000000000000000000000000000000000000000000000"
we currently fail.
This leads to dirty hacks like re-implementing dictionary parser like they do here with explicit (UInt256)BigInteger.Parse(skey.Substring(2), NumberStyles.HexNumber);
in it.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.