Add wrapper for BouncyCastle's BigInteger.
authorCryptoManiac <balthazar.ad@gmail.com>
Wed, 9 Sep 2015 08:23:56 +0000 (11:23 +0300)
committerCryptoManiac <balthazar.ad@gmail.com>
Wed, 9 Sep 2015 08:23:56 +0000 (11:23 +0300)
Novacoin/BigNum.cs [new file with mode: 0644]
Novacoin/Novacoin.csproj

diff --git a/Novacoin/BigNum.cs b/Novacoin/BigNum.cs
new file mode 100644 (file)
index 0000000..e80d26d
--- /dev/null
@@ -0,0 +1,135 @@
+\feffusing System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+using Org.BouncyCastle.Math;
+
+namespace Novacoin
+{
+    /// <summary>
+    /// Wrapper for bouncycastle's Biginteger class.
+    /// </summary>
+    public class BigNum
+    {
+        /// <summary>
+        /// Internal coby of Biginteger object.
+        /// </summary>
+        private BigInteger bn;
+
+        #region Constructors
+        public BigNum(BigInteger bnValue)
+        {
+            bn = bnValue;
+        }
+
+        public BigNum(byte[] dataBytes)
+        {
+            bn = new BigInteger(dataBytes);
+        }
+
+        public BigNum(ulong ulongValue)
+        {
+            bn = new BigInteger(BitConverter.GetBytes(ulongValue));
+        }
+
+        public BigNum(uint256 uint256Value)
+        {
+            bn = new BigInteger(uint256Value);
+        }
+        #endregion
+
+        #region Basic arithmetics
+        public static BigNum operator +(BigNum a, ulong b)
+        {
+            var bnValueToAdd = new BigInteger(BitConverter.GetBytes(b));
+            return new BigNum(a.bn.Add(bnValueToAdd));
+        }
+
+        public static BigNum operator -(BigNum a, ulong b)
+        {
+            var bnValueToSubstract = new BigInteger(BitConverter.GetBytes(b));
+            return new BigNum(a.bn.Subtract(bnValueToSubstract));
+        }
+
+        public static BigNum operator +(BigNum a, uint256 b)
+        {
+            var bnValueToAdd = new BigInteger(b);
+            return new BigNum(a.bn.Add(bnValueToAdd));
+        }
+
+        public static BigNum operator -(BigNum a, uint256 b)
+        {
+            var bnValueToSubstract = new BigInteger(b);
+            return new BigNum(a.bn.Subtract(bnValueToSubstract));
+        }
+
+        public static BigNum operator +(BigNum a, BigNum b)
+        {
+            return new BigNum(a.bn.Add(b.bn));
+        }
+
+        public static BigNum operator -(BigNum a, BigNum b)
+        {
+            return new BigNum(a.bn.Subtract(b.bn));
+        }
+
+        public static BigNum operator /(BigNum a, ulong b)
+        {
+            var bnDivider = new BigInteger(BitConverter.GetBytes(b));
+            return new BigNum(a.bn.Divide(bnDivider));
+        }
+
+        public static BigNum operator /(BigNum a, uint256 b)
+        {
+            var bnDivider = new BigInteger(b);
+            return new BigNum(a.bn.Divide(bnDivider));
+        }
+
+        public static BigNum operator /(BigNum a, BigNum b)
+        {
+            return new BigNum(a.bn.Divide(b.bn));
+        }
+
+        public static BigNum operator *(BigNum a, ulong b)
+        {
+            var bnMultiplier = new BigInteger(BitConverter.GetBytes(b));
+            return new BigNum(a.bn.Multiply(bnMultiplier));
+        }
+
+        public static BigNum operator *(BigNum a, uint256 b)
+        {
+            var bnMultiplier = new BigInteger(b);
+            return new BigNum(a.bn.Multiply(bnMultiplier));
+        }
+
+        public static BigNum operator *(BigNum a, BigNum b)
+        {
+            return new BigNum(a.bn.Multiply(b.bn));
+        }
+        #endregion
+
+        #region Cast operators
+        public static implicit operator BigNum(BigInteger bnValue)
+        {
+            return new BigNum(bnValue);
+        }
+
+        public static implicit operator BigNum(ulong ulongValue)
+        {
+            return new BigNum(ulongValue);
+        }
+
+        public static implicit operator BigNum(uint256 uint256Value)
+        {
+            return new BigNum(uint256Value);
+        }
+
+        public static implicit operator ulong (BigNum a)
+        {
+            return (ulong)a.bn.LongValue;
+        }
+        #endregion
+    }
+}
index b056308..8348912 100644 (file)
   <ItemGroup>
     <Compile Include="AddressTools.cs" />
     <Compile Include="base_uint.cs" />
+    <Compile Include="BigNum.cs" />
     <Compile Include="BignumHelper.cs" />
     <Compile Include="Checkpoints.cs" />
     <Compile Include="DatabaseObjects.cs" />