Exemple #1
0
        /// <summary>
        /// Convert a list of Numeric Compacted codewords from Base 900 to Base 10.
        /// EXAMPLE
        /// Encode the fifteen digit numeric string 000213298174000
        /// Prefix the numeric string with a 1 and set the initial value of
        /// t = 1 000 213 298 174 000
        /// Calculate codeword 0
        /// d0 = 1 000 213 298 174 000 mod 900 = 200
        ///
        /// t = 1 000 213 298 174 000 div 900 = 1 111 348 109 082
        /// Calculate codeword 1
        /// d1 = 1 111 348 109 082 mod 900 = 282
        ///
        /// t = 1 111 348 109 082 div 900 = 1 234 831 232
        /// Calculate codeword 2
        /// d2 = 1 234 831 232 mod 900 = 632
        ///
        /// t = 1 234 831 232 div 900 = 1 372 034
        /// Calculate codeword 3
        /// d3 = 1 372 034 mod 900 = 434
        ///
        /// t = 1 372 034 div 900 = 1 524
        /// Calculate codeword 4
        /// d4 = 1 524 mod 900 = 624
        ///
        /// t = 1 524 div 900 = 1
        /// Calculate codeword 5
        /// d5 = 1 mod 900 = 1
        /// t = 1 div 900 = 0
        /// Codeword sequence is: 1, 624, 434, 632, 282, 200
        ///
        /// Decode the above codewords involves
        ///   1 x 900 power of 5 + 624 x 900 power of 4 + 434 x 900 power of 3 +
        /// 632 x 900 power of 2 + 282 x 900 power of 1 + 200 x 900 power of 0 = 1000213298174000
        ///
        /// Remove leading 1 =>  Result is 000213298174000
        /// <param name="codewords">The array of codewords</param>
        /// <param name="count">The number of codewords</param>
        /// <returns>The decoded string representing the Numeric data.</returns>
        /// </summary>
        private static String decodeBase900toBase10(int[] codewords, int count)
        {
#if SILVERLIGHT4 || SILVERLIGHT5 || NET40 || NET45 || NETFX_CORE || NETSTANDARD
            BigInteger result = BigInteger.Zero;
            for (int i = 0; i < count; i++)
            {
                result = BigInteger.Add(result, BigInteger.Multiply(EXP900[count - i - 1], new BigInteger(codewords[i])));
            }
            String resultString = result.ToString();
            if (resultString[0] != '1')
            {
                return(null);
            }
            return(resultString.Substring(1));
#else
            BigInteger result = BigInteger.Zero;
            for (int i = 0; i < count; i++)
            {
                result = BigInteger.Addition(result, BigInteger.Multiplication(EXP900[count - i - 1], new BigInteger(codewords[i])));
            }
            String resultString = result.ToString();
            if (resultString[0] != '1')
            {
                return(null);
            }
            return(resultString.Substring(1));
#endif
        }
Exemple #2
0
        private IPrivateKey Child(byte[] offset)
        {
            var buffer = ToPublicKey().ToBuffer();
            var data   = buffer.ToArray();

            buffer.Dispose();
            offset = data.Concat(offset);
            data.Clear();
            var hash = SHA256.Create().HashAndDispose(offset);

            offset.Clear();
            var c = BigInteger.FromBuffer(hash);

            hash.Clear();
            if (c.CompareTo(Curve.SecP256k1.N) >= 0)
            {
                throw new InvalidOperationException("Child offset went out of bounds, try again");
            }
            var derived = d.Addition(c); //.Modulo(Curve.SecP256k1.N);

            c.Dispose();
            if (derived.Sign == 0)
            {
                throw new InvalidOperationException("Child offset derived to an invalid key, try again");
            }
            return(new PrivateKey(derived));
        }
Exemple #3
0
        public static void RunSpeedTests()
        {
            BigInteger a = null;
            BigInteger b = null;

            //speed test
            Random rnd     = new Random();
            string output  = "";
            string output2 = "";

            Stopwatch sw = new Stopwatch();

            sw.Start();

            for (int i = 0; i < 99999; i++)
            {
                int operation = rnd.Next(1, 5);

                long c1 = rnd.Next(int.MinValue, int.MaxValue);
                long c2 = rnd.Next(int.MinValue, int.MaxValue);

                a = new BigInteger(c1.ToString());
                b = new BigInteger(c2.ToString());

                if (operation == 1)
                {
                    output  = a.Addition(b).ToString();
                    output2 = (c1 + c2).ToString();
                    Tests.Assert(output == output2, "SpeedTest failed");
                }

                if (operation == 2)
                {
                    output  = a.Subtraction(b).ToString();
                    output2 = (c1 - c2).ToString();
                    Tests.Assert(output == output2, "SpeedTest failed");
                }

                if (operation == 3)
                {
                    output  = a.Multiplication(b).ToString();
                    output2 = (c1 * c2).ToString();
                    Tests.Assert(output == output2, "SpeedTest failed");
                }

                if (operation == 4)
                {
                    output  = a.Division(b).Division().ToString();
                    output2 = (c1 / c2).ToString();
                    Tests.Assert(output == output2, "SpeedTest failed");
                }
            }

            sw.Stop();
            Tests.Assert(sw.Elapsed.Seconds <= 2, "speed test take to long");
        }
Exemple #4
0
        public Curve(BigInteger p, BigInteger a, BigInteger b, BigInteger gX, BigInteger gY, BigInteger n, BigInteger h)
        {
            this.p = p;
            this.a = a;
            this.b = b;
            this.n = n;
            this.h = h;

            g        = Point.FromAffine(this, gX, gY);
            infinity = new Point(this, null, null, BigInteger.Zero.Clone());

            // result caching
            pOverFour = p.Addition(BigInteger.One).ShiftRight(2);
        }
Exemple #5
0
        public static void RunTests()
        {
            string     output = "";
            BigInteger a      = null;
            BigInteger b      = null;

            /////////////////////////////////////////////////

            a = new BigInteger("1234");
            b = new BigInteger("1234");
            Tests.Assert(a.Compare(b) == 0, "Comparion failed");

            a = new BigInteger("1234");
            b = new BigInteger("1200");
            Tests.Assert(a.Compare(b) == 1, "Comparion failed");

            a = new BigInteger("1200");
            b = new BigInteger("1234");
            Tests.Assert(a.Compare(b) == -1, "Comparion failed");

            a = new BigInteger("123456");
            b = new BigInteger("1234");
            Tests.Assert(a.Compare(b) == 1, "Comparion failed");

            a = new BigInteger("123456");
            b = new BigInteger("-1234");
            Tests.Assert(a.Compare(b) == 1, "Comparion failed");

            a = new BigInteger("-123456");
            b = new BigInteger("1234");
            Tests.Assert(a.Compare(b) == -1, "Comparion failed");

            a = new BigInteger("-123456");
            b = new BigInteger("-1234");
            Tests.Assert(a.Compare(b) == -1, "Comparion failed");

            /////////////////////////////////////////////////


            a = new BigInteger("1234");
            b = new BigInteger("1234");
            Tests.Assert(a.IsEqual(b) == true, "Comparion failed");

            a = new BigInteger("1234");
            b = new BigInteger("1234567");
            Tests.Assert(a.IsSmaller(b) == true, "Comparion failed");

            a = new BigInteger("1234567");
            b = new BigInteger("1234");
            Tests.Assert(a.IsGreater(b) == true, "Comparion failed");

            a = new BigInteger("1234");
            b = new BigInteger("1234567");
            Tests.Assert(a.IsSmallerOrEqual(b) == true, "Comparion failed");

            a = new BigInteger("1234567");
            b = new BigInteger("1234");
            Tests.Assert(a.IsGreaterOrEqual(b) == true, "Comparion failed");


            /////////////////////////////////////////////////

            a = new BigInteger("10");
            Tests.Assert(a.IsInt() == true, "Check type failed");

            a = new BigInteger("10000000000001000000000000000");
            Tests.Assert(a.IsInt() == false, "Check type failed");

            a = new BigInteger("-10000000000001000000000000000");
            Tests.Assert(a.IsInt() == false, "Check type failed");

            a = new BigInteger("1231231");
            Tests.Assert(a.AsInt() == 1231231, "Convert to int failed");

            a = new BigInteger("-1231231");
            Tests.Assert(a.AsInt() == -1231231, "Convert to int failed");

            a = new BigInteger("1231231");
            Tests.Assert(a.AsLong() == 1231231, "Convert to long failed");

            a = new BigInteger("-1231231");
            Tests.Assert(a.AsLong() == -1231231, "Convert to long failed");

            /////////////////////////////////////////////////

            a      = new BigInteger("10000000000001000000000000000");
            b      = new BigInteger("1000");
            output = a.Addition(b).ToString();
            Tests.Assert(output == "10000000000001000000000001000", "Addition failed");

            a      = new BigInteger("10");
            b      = new BigInteger("5");
            output = a.Addition(b).ToString();
            Tests.Assert(output == "15", "Addition failed");

            a      = new BigInteger("5");
            b      = new BigInteger("10");
            output = a.Addition(b).ToString();
            Tests.Assert(output == "15", "Addition failed");

            a      = new BigInteger("-5");
            b      = new BigInteger("10");
            output = a.Addition(b).ToString();
            Tests.Assert(output == "5", "Addition failed");

            a      = new BigInteger("5");
            b      = new BigInteger("-10");
            output = a.Addition(b).ToString();
            Tests.Assert(output == "-5", "Addition failed");

            a      = new BigInteger("-5");
            b      = new BigInteger("-10");
            output = a.Addition(b).ToString();
            Tests.Assert(output == "-15", "Addition failed");

            a      = new BigInteger("-5");
            b      = new BigInteger("5");
            output = a.Addition(b).ToString();
            Tests.Assert(output == "0", "Addition failed");

            a      = new BigInteger("5");
            b      = new BigInteger("-5");
            output = a.Addition(b).ToString();
            Tests.Assert(output == "0", "Addition failed");

            /////////////////////////////////////////////////

            a      = new BigInteger("10");
            b      = new BigInteger("5");
            output = a.Subtraction(b).ToString();
            Tests.Assert(output == "5", "Subtraction failed");

            a      = new BigInteger("5");
            b      = new BigInteger("10");
            output = a.Subtraction(b).ToString();
            Tests.Assert(output == "-5", "Subtraction failed");

            a      = new BigInteger("-5");
            b      = new BigInteger("10");
            output = a.Subtraction(b).ToString();
            Tests.Assert(output == "-15", "Subtraction failed");

            a      = new BigInteger("5");
            b      = new BigInteger("-10");
            output = a.Subtraction(b).ToString();
            Tests.Assert(output == "15", "Subtraction failed");

            a      = new BigInteger("-5");
            b      = new BigInteger("-10");
            output = a.Subtraction(b).ToString();
            Tests.Assert(output == "5", "Subtraction failed");

            a      = new BigInteger("5");
            b      = new BigInteger("5");
            output = a.Subtraction(b).ToString();
            Tests.Assert(output == "0", "Subtraction failed");

            a      = new BigInteger("-5");
            b      = new BigInteger("-5");
            output = a.Subtraction(b).ToString();
            Tests.Assert(output == "0", "Subtraction failed");

            /////////////////////////////////////////////////

            a      = new BigInteger("10");
            b      = new BigInteger("5");
            output = a.Multiplication(b).ToString();
            Tests.Assert(output == "50", "Multiplication failed");

            a      = new BigInteger("5");
            b      = new BigInteger("10");
            output = a.Multiplication(b).ToString();
            Tests.Assert(output == "50", "Multiplication failed");

            a      = new BigInteger("-5");
            b      = new BigInteger("10");
            output = a.Multiplication(b).ToString();
            Tests.Assert(output == "-50", "Multiplication failed");

            a      = new BigInteger("10");
            b      = new BigInteger("-5");
            output = a.Multiplication(b).ToString();
            Tests.Assert(output == "-50", "Multiplication failed");

            a      = new BigInteger("5");
            b      = new BigInteger("-10");
            output = a.Multiplication(b).ToString();
            Tests.Assert(output == "-50", "Multiplication failed");

            a      = new BigInteger("-10");
            b      = new BigInteger("5");
            output = a.Multiplication(b).ToString();
            Tests.Assert(output == "-50", "Multiplication failed");

            a      = new BigInteger("-5");
            b      = new BigInteger("-10");
            output = a.Multiplication(b).ToString();
            Tests.Assert(output == "50", "Multiplication failed");

            a      = new BigInteger("-10");
            b      = new BigInteger("-5");
            output = a.Multiplication(b).ToString();
            Tests.Assert(output == "50", "Multiplication failed");

            /////////////////////////////////////////////////

            a      = new BigInteger("11025");
            b      = new BigInteger("105");
            output = a.Division(b).ToString();
            Tests.Assert(output == "105|0", "Division failed");

            a      = new BigInteger("15");
            b      = new BigInteger("5");
            output = a.Division(b).ToString();
            Tests.Assert(output == "3|0", "Division failed");

            a      = new BigInteger("42435");
            b      = new BigInteger("123");
            output = a.Division(b).ToString();
            Tests.Assert(output == "345|0", "Division failed");

            a      = new BigInteger("-42435");
            b      = new BigInteger("123");
            output = a.Division(b).ToString();
            Tests.Assert(output == "-345|0", "Division failed");

            a      = new BigInteger("42435");
            b      = new BigInteger("-123");
            output = a.Division(b).ToString();
            Tests.Assert(output == "-345|0", "Division failed");

            a      = new BigInteger("-42435");
            b      = new BigInteger("-123");
            output = a.Division(b).ToString();
            Tests.Assert(output == "345|0", "Division failed");

            a      = new BigInteger("42438");
            b      = new BigInteger("123");
            output = a.Division(b).ToString();
            Tests.Assert(output == "345|3", "Division failed");

            a      = new BigInteger("214312342134123412341234");
            b      = new BigInteger("21312432134");
            output = a.Division(b).ToString();
            Tests.Assert(output == "10055743088665|11055180124", "Division failed");

            a      = new BigInteger("42438");
            b      = new BigInteger("123");
            output = a.Modulo(b).ToString();
            Tests.Assert(output == "3", "Division failed");

            a      = new BigInteger("42557");
            b      = new BigInteger("123");
            output = a.Modulo(b).ToString();
            Tests.Assert(output == "122", "Division failed");
        }