X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2Fuint256.cs;h=c9134010ed604c09ce69335237065ff097fc5c8f;hb=198c3037b62f2c6007dee4c0a8b0fa41e9225c24;hp=dfaebea6de1dc67b9ee2b41a02bf3c9ab2e5c4b8;hpb=8ad3c74259a603dc5f175c34ebe5a740c09ff6ae;p=NovacoinLibrary.git diff --git a/Novacoin/uint256.cs b/Novacoin/uint256.cs index dfaebea..c913401 100644 --- a/Novacoin/uint256.cs +++ b/Novacoin/uint256.cs @@ -18,6 +18,7 @@ using System; using System.Diagnostics.Contracts; +using System.Linq; namespace Novacoin { @@ -251,7 +252,7 @@ namespace Novacoin return a - new uint256(b); } - public static uint256 operator /(uint256 a, uint divisor) + public static uint256 operator /(uint256 a, uint b) { var result = new uint256(); @@ -262,14 +263,14 @@ namespace Novacoin { r <<= 32; r |= a.pn[i]; - result.pn[i] = (uint)(r / divisor); - r %= divisor; + result.pn[i] = (uint)(r / b); + r %= b; } return result; } - public static uint256 operator *(uint256 a, uint multiplier) + public static uint256 operator *(uint256 a, ulong b) { var result = new uint256(); @@ -278,7 +279,7 @@ namespace Novacoin do { - c += a.pn[i] * (ulong)multiplier; + c += a.pn[i] * b; result.pn[i] = (uint)c; c >>= 32; } while (++i < result.nWidth); @@ -286,7 +287,67 @@ namespace Novacoin return result; } - public static uint operator %(uint256 a, uint divisor) + public static uint256 operator *(uint256 a, uint256 b) + { + if (!a || !b) + { + // Multiplication by zero results with zero. + return 0; + } + else if (b.bits <= 32) + { + if (b.pn[0] == 1) + { + // If right is 1 then return left operand value + return a; + } + + return a * b.pn[0]; + } + else if (a.bits <= 32) + { + if (a.pn[0] == 1) + { + // If left is 1 then return right operand value + return b; + } + + return a * b.pn[0]; + } + + int m = a.bits / 32 + (a.bits % 32 != 0 ? 1 : 0); + int n = b.bits / 32 + (b.bits % 32 != 0 ? 1 : 0); + + uint256 result = new uint256(); + + uint[] left = a.pn.Take(m).ToArray(); + uint[] right = b.pn.Take(n).ToArray(); + + for (int i = 0; i < m; ++i) + { + uint ai = left[i]; + int k = i; + + ulong temp = 0; + for (int j = 0; j < n; ++j) + { + temp = temp + ((ulong)ai) * right[j] + result.pn[k]; + result.pn[k++] = (uint)temp; + temp >>= 32; + } + + while (temp != 0) + { + temp += result.pn[k]; + result.pn[k++] = (uint)temp; + temp >>= 32; + } + } + + return result; + } + + public static uint operator %(uint256 a, uint b) { ulong r = 0; int i = a.nWidth; @@ -295,43 +356,57 @@ namespace Novacoin { r <<= 32; r |= a.pn[i]; - r %= divisor; + r %= b; } return (uint)r; } - public static uint256 operator /(uint256 a, uint256 divisor) + public static uint256 operator /(uint256 a, uint256 b) { - if (divisor.bits <= 32) + if (b.bits <= 32) { - return a / divisor.Low32; + return a / b.Low32; } - return Divide(a, divisor)[0]; + 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 divisor) + public static uint256 operator %(uint256 a, uint256 b) { - if (divisor.bits <= 32) + if (b.bits <= 32) { - return a % divisor.Low32; + return a % b.Low32; } - return Divide(a, divisor)[1]; - } - #endregion + uint256 result = new uint256(); - public static uint256[] Divide(uint256 bi1, uint256 bi2) - { - // STUB! + uint[] quotient; + uint[] remainder_value; - uint256[] ret = new uint256[2] { 0, 0 }; + int m = a.bits / 32 + (a.bits % 32 != 0 ? 1 : 0); + int n = b.bits / 32 + (b.bits % 32 != 0 ? 1 : 0); - return ret; - } + 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 public static uint256 operator <<(uint256 a, int shift)