From: CryptoManiac Date: Wed, 9 Sep 2015 08:23:56 +0000 (+0300) Subject: Add wrapper for BouncyCastle's BigInteger. X-Git-Url: https://git.novaco.in/?p=NovacoinLibrary.git;a=commitdiff_plain;h=713afd692008f0973ce05ab4218c488c3305a362 Add wrapper for BouncyCastle's BigInteger. --- diff --git a/Novacoin/BigNum.cs b/Novacoin/BigNum.cs new file mode 100644 index 0000000..e80d26d --- /dev/null +++ b/Novacoin/BigNum.cs @@ -0,0 +1,135 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Org.BouncyCastle.Math; + +namespace Novacoin +{ + /// + /// Wrapper for bouncycastle's Biginteger class. + /// + public class BigNum + { + /// + /// Internal coby of Biginteger object. + /// + 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 + } +} diff --git a/Novacoin/Novacoin.csproj b/Novacoin/Novacoin.csproj index b056308..8348912 100644 --- a/Novacoin/Novacoin.csproj +++ b/Novacoin/Novacoin.csproj @@ -108,6 +108,7 @@ +