LongValue() public method

public LongValue ( ) : long
return long
Ejemplo n.º 1
0
        private static BigInteger GetNearby(BigInteger significand, int binExp, int offset)
        {
            int        nExtraBits = 1;
            int        nDec       = (int)Math.Round(3.0 + (64 + nExtraBits) * Math.Log10(2.0));
            BigInteger newFrac    = (significand << nExtraBits).Add(new BigInteger(offset));

            int gg = 64 + nExtraBits - binExp - 1;

            decimal bd = new decimal(newFrac.LongValue());

            if (gg > 0)
            {
                bd = bd / (new decimal((BigInteger.ONE << gg).LongValue()));
            }
            else
            {
                BigInteger frac = newFrac;
                while (frac.BitLength() + binExp < 180)
                {
                    frac = frac * (BigInteger.TEN);
                }
                int binaryExp = binExp - newFrac.BitLength() + frac.BitLength();

                bd = new decimal((frac >> (frac.BitLength() - binaryExp - 1)).LongValue());
            }

            /*int excessPrecision = bd.Precision() - nDec;
             * if (excessPrecision > 0)
             * {
             *  bd = bd.SetScale(bd.Scale() - excessPrecision, BigDecimal.ROUND_HALF_UP);
             * }
             * return bd.unscaledValue();*/
            throw new NotImplementedException();
        }
Ejemplo n.º 2
0
        private static BigInteger findRandomGenerator(BigInteger order, BigInteger modulo, Rng random)
        {
            BigInteger one = new BigInteger(1);
            BigInteger aux = modulo - new BigInteger(1);
            BigInteger t   = aux % order;
            BigInteger generator;

            if (t.LongValue() != 0)
            {
                return(null);
            }

            t = aux / order;

            while (true)
            {
                generator = new BigInteger();
                generator.genRandomBits(modulo.bitCount(), random);
                generator = generator % modulo;
                generator = generator.modPow(t, modulo);
                if (generator != one)
                {
                    break;
                }
            }

            aux = generator.modPow(order, modulo);

            if (aux != one)
            {
                return(null);
            }

            return(generator);
        }
Ejemplo n.º 3
0
        public static ulong[] Encrypt(ulong data, ulong pkey)
        {
            var        k = new BigInteger(new[] { PositiveRandom(), PositiveRandom() });
            BigInteger c1 = B.ModPow(k, M), c2 = new BigInteger(pkey).ModPow(k, M);
            BigInteger encrypted = (c2 * new BigInteger(data)) % M;

            return(new[] { (ulong)c1.LongValue(), (ulong)encrypted.LongValue() });
        }
Ejemplo n.º 4
0
 /**
  * Get this number as a long, or rather truncate all but the least
  * significant 64 bits from the 2's complement representation of this number
  * and return them as a long.
  *
  * @return the value of this number, as a long.
  */
 public long longValue()
 {
     if (!BigIntegerIsNull(bigVal))
     {
         return(bigVal.LongValue());
     }
     else
     {
         return(val);
     }
 }
Ejemplo n.º 5
0
        public static ulong Decrypt(ulong[] data)
        {
            if (data.Length != 2)
            {
                throw new ArgumentException("data should have a length of 2");
            }

            BigInteger c1 = new BigInteger(data[0]), c2 = new BigInteger(data[1]);
            BigInteger res = (c1.ModPow(Program.PrivateKey, M).modInverse(M) * c2) % M;

            return((ulong)res.LongValue());
        }
Ejemplo n.º 6
0
        public void TestDecrement()
        {
            BigInteger a, b;
            var        rand = new Random();

            // Basic tests
            for (long i = -512; i <= 512; i++)
            {
                var j = i;
                a = new BigInteger(i);

                a--;
                j--;

                Assert.AreEqual(j, a.LongValue());
            }

            // Random tests
            for (long i = -512; i <= 512; i++)
            {
                var j = (long)rand.Next();
                a = new BigInteger(j);

                a--;
                j--;

                Assert.AreEqual(j, a.LongValue());
            }

            // Real big integers test
            a = new BigInteger("52934751979537459082347598273459823452034579283475783465927501645982739085609348562783618963874623908273490519845620", 10);
            b = new BigInteger("52934751979537459082347598273459823452034579283475783465927501645982739085609348562783618963874623908273490519845619", 10);
            a--;
            Assert.AreEqual(a, b);

            a = new BigInteger("1259234750923760235470196347826357167634528763452673645928635987263458926493875623485628756782", 10);
            b = new BigInteger("1259234750923760235470196347826357167634528763452673645928635987263458926493875623485628756781", 10);
            a--;
            Assert.AreEqual(a, b);

            a = new BigInteger("1234567890863223489876543234789876543262873568236458726358723876283465876529836582658263487528793562984365", 10);
            b = new BigInteger("1234567890863223489876543234789876543262873568236458726358723876283465876529836582658263487528793562984364", 10);
            a--;
            Assert.AreEqual(a, b);

            a = new BigInteger("1239846123879561238975328976356178347616234781692361897356917865802346578234675432312123467989789", 10);
            b = new BigInteger("1239846123879561238975328976356178347616234781692361897356917865802346578234675432312123467989788", 10);
            a--;
            Assert.AreEqual(a, b);

            // TODO Test negative really big integers
        }
Ejemplo n.º 7
0
 /**
  * Create an Erlang integer from the given value.
  *
  * @param val
  *                the long value to use.
  */
 public OtpErlangLong(BigInteger v)
 {
     if (v == null)
     {
         throw new NullReferenceException();
     }
     if (v.bitCount() < 64)
     {
         val = v.LongValue();
     }
     else
     {
         bigVal = v;
     }
 }
Ejemplo n.º 8
0
    public long ReconstructData(SharedData[] shares)
    {
        BigInteger[] nominators = new BigInteger[numNecessaryParts];

        for (int i = 0; i < numNecessaryParts; i++)
        {
            nominators[i] = 1;

            for (int j = 0; j < numNecessaryParts; j++)
            {
                if (i != j)
                {
                    BigInteger inv = new BigInteger(shares[i].xi) - new BigInteger(shares[j].xi);
                    inv = inv % modP;
                    if (inv < 0)
                    {
                        while (inv <= 0)
                        {
                            inv += modP;
                        }
                        inv = inv.modInverse(modP);
                    }
                    else if (inv != 1)
                    {
                        inv = inv.modInverse(modP);
                    }

                    nominators[i] = (nominators[i] * (new BigInteger(shares[j].xi) * inv)) % modP;
                }
            }
        }

        for (int i = 0; i < numNecessaryParts; i++)
        {
            nominators[i] = (nominators[i] * new BigInteger(shares[i].yi)) % modP;
        }

        BigInteger nominator = new BigInteger(0);

        for (int i = 0; i < numNecessaryParts; i++)
        {
            nominator = (nominator + nominators[i]) % modP;
        }

        return(nominator.LongValue());
    }
Ejemplo n.º 9
0
        public static Guid DecryptGuid(ulong[] data)
        {
            if (data.Length != 5)
            {
                throw new ArgumentException("data should have a length of 5");
            }

            var c1  = new BigInteger(data[0]);
            var res = new byte[16];

            for (int i = 1; i < 5; i++)
            {
                var        c2        = new BigInteger(data[i]);
                BigInteger part      = (c1.ModPow(Program.PrivateKey, M).modInverse(M) * c2) % M;
                byte[]     partBytes = BitConverter.GetBytes((uint)part.LongValue());
                partBytes.CopyTo(res, (i - 1) * 4);
            }
            return(new Guid(res));
        }
Ejemplo n.º 10
0
        public static ulong[] Encrypt(Guid data, ulong pkey)
        {
            var        k = new BigInteger(new[] { PositiveRandom(), PositiveRandom() });
            BigInteger c1 = B.ModPow(k, M), c2 = new BigInteger(pkey).ModPow(k, M);

            byte[]     bytes      = data.ToByteArray();
            BigInteger encrypted1 = (c2 * new BigInteger(BitConverter.ToUInt32(bytes, 0))) % M;
            BigInteger encrypted2 = (c2 * new BigInteger(BitConverter.ToUInt32(bytes, 4))) % M;
            BigInteger encrypted3 = (c2 * new BigInteger(BitConverter.ToUInt32(bytes, 8))) % M;
            BigInteger encrypted4 = (c2 * new BigInteger(BitConverter.ToUInt32(bytes, 12))) % M;

            return(new[]
            {
                (ulong)c1.LongValue(),
                (ulong)encrypted1.LongValue(),
                (ulong)encrypted2.LongValue(),
                (ulong)encrypted3.LongValue(),
                (ulong)encrypted4.LongValue()
            });
        }
Ejemplo n.º 11
0
        public void write_big_integer(BigInteger v)
        {
            if (v.bitCount() < 64)
            {
                this.write_long(v.LongValue(), true);
                return;
            }
            int signum = (v > (BigInteger)0) ? 1 : (v < (BigInteger)0) ? -1 : 0;

            if (signum < 0)
            {
                v = -v;
            }
            byte[] magnitude = v.getBytes();
            int    n         = magnitude.Length;

            // Reverse the array to make it little endian.
            for (int i = 0, j = n; i < j--; i++)
            {
                // Swap [i] with [j]
                byte b = magnitude[i];
                magnitude[i] = magnitude[j];
                magnitude[j] = b;
            }
            if ((n & 0xFF) == n)
            {
                write1(OtpExternal.smallBigTag);
                write1(n); // length
            }
            else
            {
                write1(OtpExternal.largeBigTag);
                write4BE(n);            // length
            }
            write1(signum < 0 ? 1 : 0); // sign
            // Write the array
            writeN(magnitude);
        }
Ejemplo n.º 12
0
        public void WriteBigInteger(BigInteger v)
        {
            if (v.bitCount() < 64)
            {
                WriteLong(v.LongValue(), true);
                return;
            }

            int signum = (v > 0) ? 1 : (v < 0) ? -1 : 0;

            if (signum < 0)
            {
                v = -v;
            }

            byte[] magnitude = v.getBytes();
            int    n         = magnitude.Length;

            // Reverse the array to make it little endian.
            for (int i = 0, j = n; i < j--; i++)
            {
                (magnitude[i], magnitude[j]) = (magnitude[j], magnitude[i]);
            }

            if ((n & 0xFF) == n)
            {
                Write1(OtpExternal.smallBigTag);
                Write1(n); // length
            }
            else
            {
                Write1(OtpExternal.largeBigTag);
                Write4BE(n);            // length
            }
            Write1(signum < 0 ? 1 : 0); // sign
            // Write the array
            WriteN(magnitude);
        }
Ejemplo n.º 13
0
    public long ReconstructData(SharedData[] shares)
    {
        BigInteger[] nominators = new BigInteger[numNecessaryParts];

        for (int i = 0; i < numNecessaryParts; i++)
        {
            nominators[i] = 1;

            for (int j = 0; j < numNecessaryParts; j++)
            {
                if (i != j)
                {
                    BigInteger inv = new BigInteger(shares[i].xi) - new BigInteger(shares[j].xi);
                    inv = inv % modP;
                    if (inv < 0)
                    {
                        while (inv <= 0)
                            inv += modP;
                        inv = inv.modInverse(modP);
                    }
                    else if (inv != 1)
                    {
                        inv = inv.modInverse(modP);
                    }

                    nominators[i] = (nominators[i] * (new BigInteger(shares[j].xi) * inv)) % modP;
                }
            }

        }

        for (int i = 0; i < numNecessaryParts; i++)
        {
            nominators[i] = (nominators[i] * new BigInteger(shares[i].yi)) % modP;
        }

        BigInteger nominator = new BigInteger(0);

        for (int i = 0; i < numNecessaryParts; i++)
        {
            nominator = (nominator + nominators[i]) % modP;
        }

        return nominator.LongValue();
    }