Use signed 64 bit integers for better compatibility.
[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 : IComparable<BigNum>, IEquatable<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(long longValue)
38         {
39             bn = new BigInteger(Math.Sign(longValue), BitConverter.GetBytes(longValue));
40         }
41
42         public BigNum(uint256 uint256Value)
43         {
44             bn = new BigInteger(uint256Value);
45         }
46         #endregion
47
48         #region Basic arithmetics
49         public static BigNum operator +(BigNum a, ulong b)
50         {
51             var bnValueToAdd = new BigInteger(BitConverter.GetBytes(b));
52             return new BigNum(a.bn.Add(bnValueToAdd));
53         }
54
55         public static BigNum operator +(BigNum a, long b)
56         {
57             var bnValueToAdd = new BigInteger(BitConverter.GetBytes(b));
58             return new BigNum(a.bn.Add(bnValueToAdd));
59         }
60
61         public static BigNum operator -(BigNum a, ulong b)
62         {
63             var bnValueToSubstract = new BigInteger(BitConverter.GetBytes(b));
64             return new BigNum(a.bn.Subtract(bnValueToSubstract));
65         }
66         public static BigNum operator -(BigNum a, long b)
67         {
68             var bnValueToSubstract = new BigInteger(BitConverter.GetBytes(b));
69             return new BigNum(a.bn.Subtract(bnValueToSubstract));
70         }
71
72         public static BigNum operator +(BigNum a, uint256 b)
73         {
74             var bnValueToAdd = new BigInteger(b);
75             return new BigNum(a.bn.Add(bnValueToAdd));
76         }
77
78         public static BigNum operator -(BigNum a, uint256 b)
79         {
80             var bnValueToSubstract = new BigInteger(b);
81             return new BigNum(a.bn.Subtract(bnValueToSubstract));
82         }
83
84         public static BigNum operator +(BigNum a, BigNum b)
85         {
86             return new BigNum(a.bn.Add(b.bn));
87         }
88
89         public static BigNum operator -(BigNum a, BigNum b)
90         {
91             return new BigNum(a.bn.Subtract(b.bn));
92         }
93
94         public static BigNum operator /(BigNum a, ulong b)
95         {
96             var bnDivider = new BigInteger(BitConverter.GetBytes(b));
97             return new BigNum(a.bn.Divide(bnDivider));
98         }
99
100         public static BigNum operator /(BigNum a, long b)
101         {
102             var bnDivider = new BigInteger(BitConverter.GetBytes(b));
103             return new BigNum(a.bn.Divide(bnDivider));
104         }
105
106         public static BigNum operator /(BigNum a, uint256 b)
107         {
108             var bnDivider = new BigInteger(b);
109             return new BigNum(a.bn.Divide(bnDivider));
110         }
111
112         public static BigNum operator /(BigNum a, BigNum b)
113         {
114             return new BigNum(a.bn.Divide(b.bn));
115         }
116
117         public static BigNum operator *(BigNum a, ulong b)
118         {
119             var bnMultiplier = new BigInteger(BitConverter.GetBytes(b));
120             return new BigNum(a.bn.Multiply(bnMultiplier));
121         }
122
123         public static BigNum operator *(BigNum a, long b)
124         {
125             var bnMultiplier = new BigInteger(BitConverter.GetBytes(b));
126             return new BigNum(a.bn.Multiply(bnMultiplier));
127         }
128
129         public static BigNum operator *(BigNum a, uint256 b)
130         {
131             var bnMultiplier = new BigInteger(b);
132             return new BigNum(a.bn.Multiply(bnMultiplier));
133         }
134
135         public static BigNum operator *(BigNum a, BigNum b)
136         {
137             return new BigNum(a.bn.Multiply(b.bn));
138         }
139         #endregion
140
141         #region Comparison operations
142         public static bool operator <(BigNum a, BigNum b)
143         {
144             return a.bn.CompareTo(b.bn) < 0;
145         }
146         public static bool operator <=(BigNum a, BigNum b)
147         {
148             return a.bn.CompareTo(b.bn) <= 0;
149         }
150
151         public static bool operator >(BigNum a, BigNum b)
152         {
153             return a.bn.CompareTo(b.bn) > 0;
154         }
155
156         public static bool operator >=(BigNum a, BigNum b)
157         {
158             return a.bn.CompareTo(b.bn) >= 0;
159         }
160
161         public static bool operator ==(BigNum a, BigNum b)
162         {
163             return a.bn.CompareTo(b.bn) == 0;
164         }
165
166         public static bool operator !=(BigNum a, BigNum b)
167         {
168             return a.bn.CompareTo(b.bn) != 0;
169         }
170
171         #endregion
172
173         #region Cast operators
174         public static implicit operator BigNum(BigInteger bnValue)
175         {
176             return new BigNum(bnValue);
177         }
178
179         public static implicit operator BigNum(ulong ulongValue)
180         {
181             return new BigNum(ulongValue);
182         }
183
184         public static implicit operator BigNum(long ulongValue)
185         {
186             return new BigNum(ulongValue);
187         }
188
189         public static implicit operator BigNum(uint256 uint256Value)
190         {
191             return new BigNum(uint256Value);
192         }
193
194         public static implicit operator ulong (BigNum a)
195         {
196             return (ulong)a.bn.LongValue;
197         }
198
199         public static implicit operator long (BigNum a)
200         {
201             return a.bn.LongValue;
202         }
203
204         public int CompareTo(BigNum other)
205         {
206             return bn.CompareTo(other.bn);
207         }
208
209         public bool Equals(BigNum other)
210         {
211             if (object.ReferenceEquals(other, null))
212             {
213                 return false;
214             }
215
216             return this == other;
217         }
218
219         public override int GetHashCode()
220         {
221             return bn.GetHashCode();
222         }
223
224         public override bool Equals(object obj)
225         {
226             if (object.ReferenceEquals(obj, null))
227             {
228                 return false;
229             }
230
231             return this == (obj as BigNum);
232         }
233
234         #endregion
235     }
236 }