abs() public method

public abs ( ) : BigInteger
return BigInteger
示例#1
0
        /** @see BigInteger#doubleValue() */
        protected internal static double bigInteger2Double(BigInteger val)
        {
            // val.bitLength() < 64
            if ((val.numberLength < 2) ||
                ((val.numberLength == 2) && (val.digits[1] > 0)))
            {
                return(val.longValue());
            }
            // val.bitLength() >= 33 * 32 > 1024
            if (val.numberLength > 32)
            {
                return((val.sign > 0) ? java.lang.Double.POSITIVE_INFINITY
                        : java.lang.Double.NEGATIVE_INFINITY);
            }
            int  bitLen   = val.abs().bitLength();
            long exponent = bitLen - 1;
            int  delta    = bitLen - 54;
            // We need 54 top bits from this, the 53th bit is always 1 in lVal.
            long lVal = val.abs().shiftRight(delta).longValue();

            /*
             * Take 53 bits from lVal to mantissa. The least significant bit is
             * needed for rounding.
             */
            long mantissa = lVal & 0x1FFFFFFFFFFFFFL;

            if (exponent == 1023)
            {
                if (mantissa == 0X1FFFFFFFFFFFFFL)
                {
                    return((val.sign > 0) ? java.lang.Double.POSITIVE_INFINITY
                            : java.lang.Double.NEGATIVE_INFINITY);
                }
                if (mantissa == 0x1FFFFFFFFFFFFEL)
                {
                    return((val.sign > 0) ? java.lang.Double.MAX_VALUE : -java.lang.Double.MAX_VALUE);
                }
            }
            // Round the mantissa
            if (((mantissa & 1) == 1) &&
                (((mantissa & 2) == 2) || BitLevel.nonZeroDroppedBits(delta,
                                                                      val.digits)))
            {
                mantissa += 2;
            }
            mantissa >>= 1; // drop the rounding bit
            long resSign = (long)((val.sign < 0) ? 0x8000000000000000L : 0);

            exponent = ((1023 + exponent) << 52) & 0x7FF0000000000000L;
            long result = resSign | exponent | mantissa;

            return(java.lang.Double.longBitsToDouble(result));
        }
示例#2
0
 /** @see BigInteger#doubleValue() */
 protected internal static double bigInteger2Double(BigInteger val)
 {
     // val.bitLength() < 64
     if ((val.numberLength < 2)
             || ((val.numberLength == 2) && (val.digits[1] > 0))) {
         return val.longValue();
     }
     // val.bitLength() >= 33 * 32 > 1024
     if (val.numberLength > 32) {
         return ((val.sign > 0) ? java.lang.Double.POSITIVE_INFINITY
                 : java.lang.Double.NEGATIVE_INFINITY);
     }
     int bitLen = val.abs().bitLength();
     long exponent = bitLen - 1;
     int delta = bitLen - 54;
     // We need 54 top bits from this, the 53th bit is always 1 in lVal.
     long lVal = val.abs().shiftRight(delta).longValue();
     /*
      * Take 53 bits from lVal to mantissa. The least significant bit is
      * needed for rounding.
      */
     long mantissa = lVal & 0x1FFFFFFFFFFFFFL;
     if (exponent == 1023) {
         if (mantissa == 0X1FFFFFFFFFFFFFL) {
             return ((val.sign > 0) ? java.lang.Double.POSITIVE_INFINITY
                     : java.lang.Double.NEGATIVE_INFINITY);
         }
         if (mantissa == 0x1FFFFFFFFFFFFEL) {
             return ((val.sign > 0) ? java.lang.Double.MAX_VALUE : -java.lang.Double.MAX_VALUE);
         }
     }
     // Round the mantissa
     if (((mantissa & 1) == 1)
             && (((mantissa & 2) == 2) || BitLevel.nonZeroDroppedBits(delta,
                     val.digits))) {
         mantissa += 2;
     }
     mantissa >>= 1; // drop the rounding bit
     long resSign = (long) ((val.sign < 0) ? 0x8000000000000000L : 0);
     exponent = ((1023 + exponent) << 52) & 0x7FF0000000000000L;
     long result = resSign | exponent | mantissa;
     return java.lang.Double.longBitsToDouble(result);
 }
示例#3
0
        public void TestAbs()
        {
            BigInteger bi;
            int        val;
            Random     rand = new Random();

            for (int i = 0; i < 100; i++)
            {
                val = rand.Next(Int32.MinValue, Int32.MaxValue);
                bi  = new BigInteger(val);
                Assert.AreEqual(Math.Abs(val), bi.abs());
            }

            bi = new BigInteger("4809238490238509385094809584086094850909458309580485093485093485094809580945809458340324342343242342343242", 10);
            Assert.AreEqual("4809238490238509385094809584086094850909458309580485093485093485094809580945809458340324342343242342343242", bi.abs().ToString());

            bi = new BigInteger();
            Assert.AreEqual(0, bi.abs());

            bi = new BigInteger("-38265236482749823794237948792386482364236462846234623862368236423764236624762384762384623862376482364823", 10);
            Assert.AreEqual("38265236482749823794237948792386482364236462846234623862368236423764236624762384762384623862376482364823", bi.abs().ToString());
        }
示例#4
0
    // secret shares an input based on n,r parameters
    public SharedData[] ShareData(long secret)
    {
        BigInteger[] coefficients = new BigInteger[numNecessaryParts - 1];
        BigInteger[] toReturn_p   = new BigInteger[numAllParts];

        for (int i = 0; i < numNecessaryParts - 1; i++)
        {
            BigInteger a = new BigInteger();
            a.genRandomBits(64, new Random());
            a = a.abs() % modP;
            coefficients[i] = a;
        }

        for (long i = 0; i < numAllParts; i++)
        {
            toReturn_p[i] = 0;

            for (long j = 0; j < numNecessaryParts - 1; j++)
            {
                BigInteger tmp = new BigInteger((long)(Math.Pow(i + 1, j + 1)));

                toReturn_p[i] = (toReturn_p[i] + (BigInteger)(coefficients[j] * tmp)) % modP;
            }
        }

        SharedData[] toReturn = new SharedData[numAllParts];

        for (int i = 0; i < numAllParts; i++)
        {
            toReturn_p[i]  = (toReturn_p[i] + new BigInteger(secret)) % modP;
            toReturn[i].yi = toReturn_p[i].LongValue();
            toReturn[i].xi = i + 1;
        }

        return(toReturn);
    }
示例#5
0
        /** @see BigInteger#toString(int) */
        protected internal static String bigInteger2String(BigInteger val, int radix)
        {
            int sign         = val.sign;
            int numberLength = val.numberLength;

            int [] digits = val.digits;

            if (sign == 0)
            {
                return("0"); //$NON-NLS-1$
            }
            if (numberLength == 1)
            {
                int  highDigit = digits[numberLength - 1];
                long v         = highDigit & 0xFFFFFFFFL;
                if (sign < 0)
                {
                    v = -v;
                }
                return(java.lang.Long.toString(v, radix));
            }
            if ((radix == 10) || (radix < java.lang.Character.MIN_RADIX) ||
                (radix > java.lang.Character.MAX_RADIX))
            {
                return(val.toString());
            }
            double bitsForRadixDigit;

            bitsForRadixDigit = java.lang.Math.log(radix) / java.lang.Math.log(2);
            int resLengthInChars = (int)(val.abs().bitLength() / bitsForRadixDigit + ((sign < 0) ? 1
                    : 0)) + 1;

            char [] result      = new char[resLengthInChars];
            int     currentChar = resLengthInChars;
            int     resDigit;

            if (radix != 16)
            {
                int [] temp = new int[numberLength];
                java.lang.SystemJ.arraycopy(digits, 0, temp, 0, numberLength);
                int tempLen     = numberLength;
                int charsPerInt = digitFitInInt[radix];
                int i;
                // get the maximal power of radix that fits in int
                int bigRadix = bigRadices[radix - 2];
                while (true)
                {
                    // divide the array of digits by bigRadix and convert remainders
                    // to characters collecting them in the char array
                    resDigit = Division.divideArrayByInt(temp, temp, tempLen,
                                                         bigRadix);
                    int previous = currentChar;
                    do
                    {
                        result[--currentChar] = java.lang.Character.forDigit(
                            resDigit % radix, radix);
                    } while (((resDigit /= radix) != 0) && (currentChar != 0));
                    int delta = charsPerInt - previous + currentChar;
                    for (i = 0; i < delta && currentChar > 0; i++)
                    {
                        result[--currentChar] = '0';
                    }
                    for (i = tempLen - 1; (i > 0) && (temp[i] == 0); i--)
                    {
                        ;
                    }
                    tempLen = i + 1;
                    if ((tempLen == 1) && (temp[0] == 0))   // the quotient is 0
                    {
                        break;
                    }
                }
            }
            else
            {
                // radix == 16
                for (int i = 0; i < numberLength; i++)
                {
                    for (int j = 0; (j < 8) && (currentChar > 0); j++)
                    {
                        resDigit = digits[i] >> (j << 2) & 0xf;
                        result[--currentChar] = java.lang.Character.forDigit(resDigit, 16);
                    }
                }
            }
            while (result[currentChar] == '0')
            {
                currentChar++;
            }
            if (sign == -1)
            {
                result[--currentChar] = '-';
            }
            return(new String(result, currentChar, resLengthInChars - currentChar));
        }
示例#6
0
        /** @see BigInteger#toString(int) */
        protected internal static String bigInteger2String(BigInteger val, int radix)
        {
            int sign = val.sign;
            int numberLength = val.numberLength;
            int []digits = val.digits;

            if (sign == 0) {
                return "0"; //$NON-NLS-1$
            }
            if (numberLength == 1) {
                int highDigit = digits[numberLength - 1];
                long v = highDigit & 0xFFFFFFFFL;
                if (sign < 0) {
                    v = -v;
                }
                return java.lang.Long.toString(v, radix);
            }
            if ((radix == 10) || (radix < java.lang.Character.MIN_RADIX)
                    || (radix > java.lang.Character.MAX_RADIX)) {
                return val.toString();
            }
            double bitsForRadixDigit;
            bitsForRadixDigit = java.lang.Math.log(radix) / java.lang.Math.log(2);
            int resLengthInChars = (int) (val.abs().bitLength() / bitsForRadixDigit + ((sign < 0) ? 1
                    : 0)) + 1;

            char []result = new char[resLengthInChars];
            int currentChar = resLengthInChars;
            int resDigit;
            if (radix != 16) {
                int []temp = new int[numberLength];
                java.lang.SystemJ.arraycopy(digits, 0, temp, 0, numberLength);
                int tempLen = numberLength;
                int charsPerInt = digitFitInInt[radix];
                int i;
                // get the maximal power of radix that fits in int
                int bigRadix = bigRadices[radix - 2];
                while (true) {
                    // divide the array of digits by bigRadix and convert remainders
                    // to characters collecting them in the char array
                    resDigit = Division.divideArrayByInt(temp, temp, tempLen,
                            bigRadix);
                    int previous = currentChar;
                    do {
                        result[--currentChar] = java.lang.Character.forDigit(
                                resDigit % radix, radix);
                    } while (((resDigit /= radix) != 0) && (currentChar != 0));
                    int delta = charsPerInt - previous + currentChar;
                    for (i = 0; i < delta && currentChar > 0; i++) {
                        result[--currentChar] = '0';
                    }
                    for (i = tempLen - 1; (i > 0) && (temp[i] == 0); i--) {
                        ;
                    }
                    tempLen = i + 1;
                    if ((tempLen == 1) && (temp[0] == 0)) { // the quotient is 0
                        break;
                    }
                }
            } else {
                // radix == 16
                for (int i = 0; i < numberLength; i++) {
                    for (int j = 0; (j < 8) && (currentChar > 0); j++) {
                        resDigit = digits[i] >> (j << 2) & 0xf;
                        result[--currentChar] = java.lang.Character.forDigit(resDigit, 16);
                    }
                }
            }
            while (result[currentChar] == '0') {
                currentChar++;
            }
            if (sign == -1) {
                result[--currentChar] = '-';
            }
            return new String(result, currentChar, resLengthInChars - currentChar);
        }
示例#7
0
    // secret shares an input based on n,r parameters
    public SharedData[] ShareData( long secret)
    {
        BigInteger[] coefficients = new BigInteger[numNecessaryParts - 1];
        BigInteger[] toReturn_p = new BigInteger[numAllParts];

        for (int i = 0; i < numNecessaryParts - 1; i++)
        {
            BigInteger a = new BigInteger();
            a.genRandomBits(64, new Random());
            a = a.abs() % modP;
            coefficients[i] = a;

        }

        for (long i = 0; i < numAllParts; i++)
        {
            toReturn_p[i] = 0;

            for (long j = 0; j < numNecessaryParts - 1; j++)
            {

                BigInteger tmp = new BigInteger((long)(Math.Pow(i + 1, j + 1)));

                toReturn_p[i] = (toReturn_p[i] + (BigInteger)(coefficients[j] * tmp)) % modP;
            }

        }

        SharedData[] toReturn = new SharedData[numAllParts];

        for (int i = 0; i < numAllParts; i++)
        {
            toReturn_p[i] = (toReturn_p[i] + new BigInteger(secret)) % modP;
            toReturn[i].yi = toReturn_p[i].LongValue();
            toReturn[i].xi = i + 1;
        }

        return toReturn;
    }