Add wrapper for BouncyCastle's BigInteger.
[NovacoinLibrary.git] / Novacoin / BigNum.cs
1 \feffusing System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 using Org.BouncyCastle.Math;
8
9 namespace Novacoin
10 {
11     /// <summary>
12     /// Wrapper for bouncycastle's Biginteger class.
13     /// </summary>
14     public class BigNum
15     {
16         /// <summary>
17         /// Internal coby of Biginteger object.
18         /// </summary>
19         private BigInteger bn;
20
21         #region Constructors
22         public BigNum(BigInteger bnValue)
23         {
24             bn = bnValue;
25         }
26
27         public BigNum(byte[] dataBytes)
28         {
29             bn = new BigInteger(dataBytes);
30         }
31
32         public BigNum(ulong ulongValue)
33         {
34             bn = new BigInteger(BitConverter.GetBytes(ulongValue));
35         }
36
37         public BigNum(uint256 uint256Value)
38         {
39             bn = new BigInteger(uint256Value);
40         }
41         #endregion
42
43         #region Basic arithmetics
44         public static BigNum operator +(BigNum a, ulong b)
45         {
46             var bnValueToAdd = new BigInteger(BitConverter.GetBytes(b));
47             return new BigNum(a.bn.Add(bnValueToAdd));
48         }
49
50         public static BigNum operator -(BigNum a, ulong b)
51         {
52             var bnValueToSubstract = new BigInteger(BitConverter.GetBytes(b));
53             return new BigNum(a.bn.Subtract(bnValueToSubstract));
54         }
55
56         public static BigNum operator +(BigNum a, uint256 b)
57         {
58             var bnValueToAdd = new BigInteger(b);
59             return new BigNum(a.bn.Add(bnValueToAdd));
60         }
61
62         public static BigNum operator -(BigNum a, uint256 b)
63         {
64             var bnValueToSubstract = new BigInteger(b);
65             return new BigNum(a.bn.Subtract(bnValueToSubstract));
66         }
67
68         public static BigNum operator +(BigNum a, BigNum b)
69         {
70             return new BigNum(a.bn.Add(b.bn));
71         }
72
73         public static BigNum operator -(BigNum a, BigNum b)
74         {
75             return new BigNum(a.bn.Subtract(b.bn));
76         }
77
78         public static BigNum operator /(BigNum a, ulong b)
79         {
80             var bnDivider = new BigInteger(BitConverter.GetBytes(b));
81             return new BigNum(a.bn.Divide(bnDivider));
82         }
83
84         public static BigNum operator /(BigNum a, uint256 b)
85         {
86             var bnDivider = new BigInteger(b);
87             return new BigNum(a.bn.Divide(bnDivider));
88         }
89
90         public static BigNum operator /(BigNum a, BigNum b)
91         {
92             return new BigNum(a.bn.Divide(b.bn));
93         }
94
95         public static BigNum operator *(BigNum a, ulong b)
96         {
97             var bnMultiplier = new BigInteger(BitConverter.GetBytes(b));
98             return new BigNum(a.bn.Multiply(bnMultiplier));
99         }
100
101         public static BigNum operator *(BigNum a, uint256 b)
102         {
103             var bnMultiplier = new BigInteger(b);
104             return new BigNum(a.bn.Multiply(bnMultiplier));
105         }
106
107         public static BigNum operator *(BigNum a, BigNum b)
108         {
109             return new BigNum(a.bn.Multiply(b.bn));
110         }
111         #endregion
112
113         #region Cast operators
114         public static implicit operator BigNum(BigInteger bnValue)
115         {
116             return new BigNum(bnValue);
117         }
118
119         public static implicit operator BigNum(ulong ulongValue)
120         {
121             return new BigNum(ulongValue);
122         }
123
124         public static implicit operator BigNum(uint256 uint256Value)
125         {
126             return new BigNum(uint256Value);
127         }
128
129         public static implicit operator ulong (BigNum a)
130         {
131             return (ulong)a.bn.LongValue;
132         }
133         #endregion
134     }
135 }