/** * Get value as eight bytes in big endian byte order. * @param value the value to convert * @return value as eight bytes in big endian byte order */ public static byte[] getBytes(BigInteger value) { byte[] result = new byte[8]; long val = value.longValue(); result[0] = (byte) ((val & BYTE_MASK)); result[BYTE_1] = (byte) ((val & BYTE_1_MASK) >> BYTE_1_SHIFT); result[BYTE_2] = (byte) ((val & BYTE_2_MASK) >> BYTE_2_SHIFT); result[BYTE_3] = (byte) ((val & BYTE_3_MASK) >> BYTE_3_SHIFT); result[BYTE_4] = (byte) ((val & BYTE_4_MASK) >> BYTE_4_SHIFT); result[BYTE_5] = (byte) ((val & BYTE_5_MASK) >> BYTE_5_SHIFT); result[BYTE_6] = (byte) ((val & BYTE_6_MASK) >> BYTE_6_SHIFT); result[BYTE_7] = (byte) ((val & BYTE_7_MASK) >> BYTE_7_SHIFT); if (value.testBit(LEFTMOST_BIT_SHIFT)) { result[BYTE_7] |= LEFTMOST_BIT; } return result; }
/** * Get value as eight bytes in big endian byte order. * @param value the value to convert * @return value as eight bytes in big endian byte order */ public static byte[] getBytes(final BigInteger value) { final byte[] result = new byte[8]; final long val = value.longValue(); result[0] = (byte) ((val & BYTE_MASK)); result[BYTE_1] = (byte) ((val & BYTE_1_MASK) >> BYTE_1_SHIFT); result[BYTE_2] = (byte) ((val & BYTE_2_MASK) >> BYTE_2_SHIFT); result[BYTE_3] = (byte) ((val & BYTE_3_MASK) >> BYTE_3_SHIFT); result[BYTE_4] = (byte) ((val & BYTE_4_MASK) >> BYTE_4_SHIFT); result[BYTE_5] = (byte) ((val & BYTE_5_MASK) >> BYTE_5_SHIFT); result[BYTE_6] = (byte) ((val & BYTE_6_MASK) >> BYTE_6_SHIFT); result[BYTE_7] = (byte) ((val & BYTE_7_MASK) >> BYTE_7_SHIFT); if (value.testBit(LEFTMOST_BIT_SHIFT)) { result[BYTE_7] |= LEFTMOST_BIT; } return result; }
/** * Get value as eight bytes in big endian byte order. * @param value the value to convert * @return value as eight bytes in big endian byte order */ public static byte[] getBytes(BigInteger value) { byte[] result = new byte[8]; long val = value.longValue(); result[0] = (byte) ((val & BYTE_MASK)); result[BYTE_1] = (byte) ((val & BYTE_1_MASK) >> BYTE_1_SHIFT); result[BYTE_2] = (byte) ((val & BYTE_2_MASK) >> BYTE_2_SHIFT); result[BYTE_3] = (byte) ((val & BYTE_3_MASK) >> BYTE_3_SHIFT); result[BYTE_4] = (byte) ((val & BYTE_4_MASK) >> BYTE_4_SHIFT); result[BYTE_5] = (byte) ((val & BYTE_5_MASK) >> BYTE_5_SHIFT); result[BYTE_6] = (byte) ((val & BYTE_6_MASK) >> BYTE_6_SHIFT); result[BYTE_7] = (byte) ((val & BYTE_7_MASK) >> BYTE_7_SHIFT); if (value.testBit(LEFTMOST_BIT_SHIFT)) { result[BYTE_7] |= LEFTMOST_BIT; } return result; }
@Override public Value divide(Value v) { ValueDecimal dec = (ValueDecimal) v; if (dec.value.signum() == 0) { throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL()); } BigDecimal bd = value.divide(dec.value, value.scale() + DIVIDE_SCALE_ADD, BigDecimal.ROUND_HALF_DOWN); if (bd.signum() == 0) { bd = BigDecimal.ZERO; } else if (bd.scale() > 0) { if (!bd.unscaledValue().testBit(0)) { bd = bd.stripTrailingZeros(); } } return ValueDecimal.get(bd); }
Fp12 cyclotomicExp(BigInteger pow) { Fp12 res = _1; for (int i = pow.bitLength() - 1; i >=0; i--) { res = res.cyclotomicSquared(); if (pow.testBit(i)) { res = res.mul(this); } } return res; }
/** * Raise a BigInteger to a BigInteger power. * * @param k Number to raise. * @param e Exponent (must be positive or zero). * @return k<sup>e</sup> * @throws NotPositiveException if {@code e < 0}. */ public static BigInteger pow(final BigInteger k, BigInteger e) throws NotPositiveException { if (e.compareTo(BigInteger.ZERO) < 0) { throw new NotPositiveException(LocalizedFormats.EXPONENT, e); } BigInteger result = BigInteger.ONE; BigInteger k2p = k; while (!BigInteger.ZERO.equals(e)) { if (e.testBit(0)) { result = result.multiply(k2p); } k2p = k2p.multiply(k2p); e = e.shiftRight(1); } return result; }
private Decimal128(final BigDecimal initialValue, final boolean isNegative) { long localHigh = 0; long localLow = 0; BigDecimal value = clampAndRound(initialValue); long exponent = -value.scale(); if ((exponent < MIN_EXPONENT) || (exponent > MAX_EXPONENT)) { throw new AssertionError("Exponent is out of range for Decimal128 encoding: " + exponent); } if (value.unscaledValue().bitLength() > MAX_BIT_LENGTH) { throw new AssertionError("Unscaled roundedValue is out of range for Decimal128 encoding:" + value.unscaledValue()); } BigInteger significand = value.unscaledValue().abs(); int bitLength = significand.bitLength(); for (int i = 0; i < Math.min(64, bitLength); i++) { if (significand.testBit(i)) { localLow |= 1L << i; } } for (int i = 64; i < bitLength; i++) { if (significand.testBit(i)) { localHigh |= 1L << (i - 64); } } long biasedExponent = exponent + EXPONENT_OFFSET; localHigh |= biasedExponent << 49; if (value.signum() == -1 || isNegative) { localHigh |= SIGN_BIT_MASK; } high = localHigh; low = localLow; }
for (int bytePos = 0; bytePos < 8 && bitPos >= 0; bytePos++, bitPos--) { b <<= 1; if (record.testBit(bitPos)) { b |= 1;
@Override public Value divide(Value v) { ValueDecimal dec = (ValueDecimal) v; if (dec.value.signum() == 0) { throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL()); } BigDecimal bd = value.divide(dec.value, value.scale() + DIVIDE_SCALE_ADD, BigDecimal.ROUND_HALF_DOWN); if (bd.signum() == 0) { bd = BigDecimal.ZERO; } else if (bd.scale() > 0) { if (!bd.unscaledValue().testBit(0)) { String s = bd.toString(); int i = s.length() - 1; while (i >= 0 && s.charAt(i) == '0') { i--; } if (i < s.length() - 1) { s = s.substring(0, i + 1); bd = new BigDecimal(s); } } } return ValueDecimal.get(bd); }
public DataWord signExtend(byte k) { if (0 > k || k > 31) throw new IndexOutOfBoundsException(); byte mask = this.sValue().testBit((k * 8) + 7) ? (byte) 0xff : 0; byte[] newData = this.copyData(); for (int i = 31; i > k; i--) { newData[31 - i] = mask; } return new DataWord(newData); }
/** * Returns a {@code BigInteger} which has the same binary representation * as {@code this} but with the bit at position n set. The result is * equivalent to {@code this | pow(2, n)}. * * <p><b>Implementation Note:</b> Usage of this method is not recommended as * the current implementation is not efficient. * * @param n position where the bit in {@code this} has to be set. * @throws ArithmeticException if {@code n < 0}. */ public BigInteger setBit(int n) { prepareJavaRepresentation(); if (!testBit(n)) { return BitLevel.flipBit(this, n); } else { return this; } }
/** * Returns a {@code BigInteger} which has the same binary representation * as {@code this} but with the bit at position n cleared. The result is * equivalent to {@code this & ~pow(2, n)}. * * <p><b>Implementation Note:</b> Usage of this method is not recommended as * the current implementation is not efficient. * * @param n position where the bit in {@code this} has to be cleared. * @throws ArithmeticException if {@code n < 0}. */ public BigInteger clearBit(int n) { prepareJavaRepresentation(); if (testBit(n)) { return BitLevel.flipBit(this, n); } else { return this; } }
public BN128<T> mul(BigInteger s) { if (s.compareTo(BigInteger.ZERO) == 0) // P * 0 = 0 return zero(); if (isZero()) return this; // 0 * s = 0 BN128<T> res = zero(); for (int i = s.bitLength() - 1; i >= 0; i--) { res = res.dbl(); if (s.testBit(i)) { res = res.add(this); } } return res; }
private static List<EllCoeffs> calcEllCoeffs(BN128G2 base) { List<EllCoeffs> coeffs = new ArrayList<>(); BN128G2 addend = base; // for each bit except most significant one for (int i = LOOP_COUNT.bitLength() - 2; i >=0; i--) { Precomputed doubling = flippedMillerLoopDoubling(addend); addend = doubling.g2; coeffs.add(doubling.coeffs); if (LOOP_COUNT.testBit(i)) { Precomputed addition = flippedMillerLoopMixedAddition(base, addend); addend = addition.g2; coeffs.add(addition.coeffs); } } BN128G2 q1 = base.mulByP(); BN128G2 q2 = q1.mulByP(); q2 = new BN128G2(q2.x, q2.y.negate(), q2.z) ; // q2.y = -q2.y Precomputed addition = flippedMillerLoopMixedAddition(q1, addend); addend = addition.g2; coeffs.add(addition.coeffs); addition = flippedMillerLoopMixedAddition(q2, addend); coeffs.add(addition.coeffs); return coeffs; }
while (!strippedBI.testBit(0)) {
compRem = roundingBehavior( integerAndFraction[0].testBit(0) ? 1 : 0, integerAndFraction[1].signum() * (5 + compRem), mc.getRoundingMode());
@GwtIncompatible // TODO public void testSqrtHalfEven() { for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) { BigInteger halfEven = BigIntegerMath.sqrt(x, HALF_EVEN); // Now figure out what rounding mode we should behave like (it depends if FLOOR was // odd/even). boolean floorWasOdd = BigIntegerMath.sqrt(x, FLOOR).testBit(0); assertEquals(BigIntegerMath.sqrt(x, floorWasOdd ? HALF_UP : HALF_DOWN), halfEven); } }
private static Fp12 millerLoop(BN128G1 g1, BN128G2 g2) { // convert to affine coordinates g1 = g1.toAffine(); g2 = g2.toAffine(); // calculate Ell coefficients List<EllCoeffs> coeffs = calcEllCoeffs(g2); Fp12 f = Fp12._1; int idx = 0; // for each bit except most significant one for (int i = LOOP_COUNT.bitLength() - 2; i >=0; i--) { EllCoeffs c = coeffs.get(idx++); f = f.squared(); f = f.mulBy024(c.ell0, g1.y.mul(c.ellVW), g1.x.mul(c.ellVV)); if (LOOP_COUNT.testBit(i)) { c = coeffs.get(idx++); f = f.mulBy024(c.ell0, g1.y.mul(c.ellVW), g1.x.mul(c.ellVV)); } } EllCoeffs c = coeffs.get(idx++); f = f.mulBy024(c.ell0, g1.y.mul(c.ellVW), g1.x.mul(c.ellVV)); c = coeffs.get(idx); f = f.mulBy024(c.ell0, g1.y.mul(c.ellVW), g1.x.mul(c.ellVV)); return f; }