X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2Fuint256.cs;h=4696b4baafe91090e63093963e000b7ca7559594;hb=832b3920713c9aa7c5b71c474a47c37bbea4ac78;hp=09e1a2c7572be9d8aa2769fe6e007e7919145f10;hpb=584a6e678d645766a522fd5d25e5ae49726f7634;p=NovacoinLibrary.git
diff --git a/Novacoin/uint256.cs b/Novacoin/uint256.cs
index 09e1a2c..4696b4b 100644
--- a/Novacoin/uint256.cs
+++ b/Novacoin/uint256.cs
@@ -1,45 +1,56 @@
-using System;
-using System.Collections.Generic;
+/**
+ * Novacoin classes library
+ * Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
+
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see .
+ */
+
+using System;
using System.Diagnostics.Contracts;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-using System.Numerics;
namespace Novacoin
{
public class uint256 : base_uint
{
- new public readonly int nWidth = 8;
+ #region Access to internal representation
+ new protected int nWidth {
+ get { return base.nWidth; }
+ private set { base.nWidth = value; }
+ }
+ new protected uint[] pn {
+ get { return base.pn; }
+ private set { base.pn = value; }
+ }
+ #endregion
+ #region Constructors
public uint256()
{
- base.nWidth = nWidth;
+ nWidth = 8;
pn = new uint[nWidth];
-
- for (int i = 0; i < nWidth; i++)
- {
- pn[i] = 0;
- }
}
- public uint256(uint256 b)
+ public uint256(uint256 b) : this()
{
- base.nWidth = nWidth;
- pn = new uint[nWidth];
-
for (int i = 0; i < nWidth; i++)
{
pn[i] = b.pn[i];
}
}
-
- public uint256(ulong n)
+ public uint256(ulong n) : this()
{
- base.nWidth = nWidth;
- pn = new uint[nWidth];
-
pn[0] = (uint)n;
pn[1] = (uint)(n >> 32);
for (int i = 2; i < nWidth; i++)
@@ -48,23 +59,35 @@ namespace Novacoin
}
}
- public uint256(byte[] bytes)
+ public uint256(byte[] bytes) : this()
{
Contract.Requires(bytes.Length == 32, "Incorrect array length");
- base.nWidth = nWidth;
pn = Interop.ToUInt32Array(bytes);
}
- public uint256(string hex)
+ public uint256(string hex) : this()
{
Contract.Requires(hex.Length == 64, "Incorrect string");
- base.nWidth = nWidth;
var bytes = Interop.ReverseBytes(Interop.HexToArray(hex));
pn = Interop.ToUInt32Array(bytes);
}
+ #endregion
+ #region Cast operators
+ public static implicit operator uint256(byte[] bytes)
+ {
+ return new uint256(bytes);
+ }
+
+ public static implicit operator uint256(ulong n)
+ {
+ return new uint256(n);
+ }
+ #endregion
+
+ #region Compact representation
///
/// Compact representation of unsigned 256bit numbers.
///
@@ -79,11 +102,11 @@ namespace Novacoin
int nSize = (bits + 7) / 8;
uint nCompact = 0;
if (nSize <= 3)
- nCompact = ((uint)GetLow64()) << 8 * (3 - nSize);
+ nCompact = ((uint)Low64) << 8 * (3 - nSize);
else
{
uint256 bn = this >> 8 * (nSize - 3);
- nCompact = (uint)bn.GetLow64();
+ nCompact = (uint)bn.Low64;
}
if ((nCompact & 0x00800000) != 0)
@@ -120,12 +143,9 @@ namespace Novacoin
pn = i.pn;
}
}
+ #endregion
- private void SetBytes(byte[] bytes)
- {
- pn = Interop.ToUInt32Array(Interop.ReverseBytes(bytes));
- }
-
+ #region Bitwise operations
public static uint256 operator ~(uint256 a)
{
var ret = new uint256();
@@ -136,6 +156,41 @@ namespace Novacoin
return ret;
}
+ public static uint256 operator ^(uint256 a, uint256 b)
+ {
+ var result = new uint256();
+ result.pn = new uint[a.nWidth];
+ for (int i = 0; i < result.nWidth; i++)
+ {
+ result.pn[i] = a.pn[i] ^ b.pn[i];
+ }
+ return result;
+ }
+
+ public static uint256 operator &(uint256 a, uint256 b)
+ {
+ var result = new uint256();
+ result.pn = new uint[a.nWidth];
+ for (int i = 0; i < result.nWidth; i++)
+ {
+ result.pn[i] = a.pn[i] & b.pn[i];
+ }
+ return result;
+ }
+
+ public static uint256 operator |(uint256 a, uint256 b)
+ {
+ var result = new uint256();
+ result.pn = new uint[a.nWidth];
+ for (int i = 0; i < result.nWidth; i++)
+ {
+ result.pn[i] = a.pn[i] | b.pn[i];
+ }
+ return result;
+ }
+ #endregion
+
+ #region Basic arithmetics
public static uint256 operator -(uint256 a)
{
var ret = new uint256();
@@ -147,7 +202,6 @@ namespace Novacoin
return ret;
}
-
public static uint256 operator ++(uint256 a)
{
int i = 0;
@@ -168,16 +222,6 @@ namespace Novacoin
return a;
}
- public static uint256 operator ^(uint256 a, uint256 b)
- {
- var result = new uint256();
- result.pn = new uint[a.nWidth];
- for (int i = 0; i < result.nWidth; i++)
- {
- result.pn[i] = a.pn[i] ^ b.pn[i];
- }
- return result;
- }
public static uint256 operator +(uint256 a, uint256 b)
{
@@ -207,28 +251,58 @@ namespace Novacoin
return a - new uint256(b);
}
- public static uint256 operator &(uint256 a, uint256 b)
+ public static uint256 operator /(uint256 a, uint divisor)
{
var result = new uint256();
- result.pn = new uint[a.nWidth];
- for (int i = 0; i < result.nWidth; i++)
+
+ ulong r = 0;
+ int i = a.nWidth;
+
+ while (i-- > 0)
{
- result.pn[i] = a.pn[i] & b.pn[i];
+ r <<= 32;
+ r |= a.pn[i];
+ result.pn[i] = (uint)(r / divisor);
+ r %= divisor;
}
+
return result;
}
- public static uint256 operator |(uint256 a, uint256 b)
+ public static uint256 operator *(uint256 a, uint multiplier)
{
var result = new uint256();
- result.pn = new uint[a.nWidth];
- for (int i = 0; i < result.nWidth; i++)
+
+ ulong c = 0;
+ uint i = 0;
+
+ do
{
- result.pn[i] = a.pn[i] | b.pn[i];
- }
+ c += a.pn[i] * (ulong)multiplier;
+ result.pn[i] = (uint)c;
+ c >>= 32;
+ } while (++i < result.nWidth);
+
return result;
}
+ public static uint operator %(uint256 a, uint divisor)
+ {
+ ulong r = 0;
+ int i = a.nWidth;
+
+ while (i-- > 0)
+ {
+ r <<= 32;
+ r |= a.pn[i];
+ r %= divisor;
+ }
+
+ return (uint)r;
+ }
+ #endregion
+
+ #region Shift
public static uint256 operator <<(uint256 a, int shift)
{
var result = new uint256();
@@ -272,5 +346,6 @@ namespace Novacoin
return result;
}
+ #endregion
}
}