X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2Fuint256.cs;h=606acf0139f71a40e4df8a10ee4ca0e47990bf74;hb=14dcc623463c612772aa4a5dc20497d8661e3d0b;hp=60e570d1393b34f7ca7cd562e920f4db61df55b7;hpb=384257c389f95406f5287d84e2d449f541db512f;p=NovacoinLibrary.git diff --git a/Novacoin/uint256.cs b/Novacoin/uint256.cs index 60e570d..606acf0 100644 --- a/Novacoin/uint256.cs +++ b/Novacoin/uint256.cs @@ -18,6 +18,7 @@ using System; using System.Diagnostics.Contracts; +using System.Linq; namespace Novacoin { @@ -268,6 +269,83 @@ namespace Novacoin return result; } + + public static uint256 operator *(uint256 a, uint b) + { + var result = new uint256(); + + ulong c = 0; + uint i = 0; + + do + { + c += a.pn[i] * (ulong)b; + result.pn[i] = (uint)c; + c >>= 32; + } while (++i < result.nWidth); + + return result; + } + + public static uint operator %(uint256 a, uint b) + { + ulong r = 0; + int i = a.nWidth; + + while (i-- > 0) + { + r <<= 32; + r |= a.pn[i]; + r %= b; + } + + return (uint)r; + } + + public static uint256 operator /(uint256 a, uint256 b) + { + if (b.bits <= 32) + { + return a / b.Low32; + } + + uint256 result = new uint256(); + + uint[] quotient; + uint[] remainder_value; + + int m = a.bits / 32 + (a.bits % 32 != 0 ? 1 : 0); + int n = b.bits / 32 + (b.bits % 32 != 0 ? 1 : 0); + + BignumHelper.DivModUnsigned(a.pn.Take(m).ToArray(), b.pn.Take(n).ToArray(), out quotient, out remainder_value); + + quotient.CopyTo(result.pn, 0); + + return result; + } + + public static uint256 operator %(uint256 a, uint256 b) + { + if (b.bits <= 32) + { + return a % b.Low32; + } + + uint256 result = new uint256(); + + uint[] quotient; + uint[] remainder_value; + + int m = a.bits / 32 + (a.bits % 32 != 0 ? 1 : 0); + int n = b.bits / 32 + (b.bits % 32 != 0 ? 1 : 0); + + BignumHelper.DivModUnsigned(a.pn.Take(m).ToArray(), b.pn.Take(n).ToArray(), out quotient, out remainder_value); + + remainder_value.CopyTo(result.pn, 0); + + return result; + + } #endregion #region Shift