Java Code Examples for java.math.BigInteger#clearBit()

The following examples show how to use java.math.BigInteger#clearBit() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ECFieldF2m.java    From Java8CN with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an elliptic curve characteristic 2 finite
 * field which has 2^{@code m} elements with
 * polynomial basis.
 * The reduction polynomial for this field is based
 * on {@code rp} whose i-th bit corresponds to
 * the i-th coefficient of the reduction polynomial.<p>
 * Note: A valid reduction polynomial is either a
 * trinomial (X^{@code m} + X^{@code k} + 1
 * with {@code m} &gt; {@code k} &gt;= 1) or a
 * pentanomial (X^{@code m} + X^{@code k3}
 * + X^{@code k2} + X^{@code k1} + 1 with
 * {@code m} &gt; {@code k3} &gt; {@code k2}
 * &gt; {@code k1} &gt;= 1).
 * @param m with 2^{@code m} being the number of elements.
 * @param rp the BigInteger whose i-th bit corresponds to
 * the i-th coefficient of the reduction polynomial.
 * @exception NullPointerException if {@code rp} is null.
 * @exception IllegalArgumentException if {@code m}
 * is not positive, or {@code rp} does not represent
 * a valid reduction polynomial.
 */
public ECFieldF2m(int m, BigInteger rp) {
    // check m and rp
    this.m = m;
    this.rp = rp;
    if (m <= 0) {
        throw new IllegalArgumentException("m is not positive");
    }
    int bitCount = this.rp.bitCount();
    if (!this.rp.testBit(0) || !this.rp.testBit(m) ||
        ((bitCount != 3) && (bitCount != 5))) {
        throw new IllegalArgumentException
            ("rp does not represent a valid reduction polynomial");
    }
    // convert rp into ks
    BigInteger temp = this.rp.clearBit(0).clearBit(m);
    this.ks = new int[bitCount-2];
    for (int i = this.ks.length-1; i >= 0; i--) {
        int index = temp.getLowestSetBit();
        this.ks[i] = index;
        temp = temp.clearBit(index);
    }
}
 
Example 2
Source File: ECFieldF2m.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates an elliptic curve characteristic 2 finite
 * field which has 2^{@code m} elements with
 * polynomial basis.
 * The reduction polynomial for this field is based
 * on {@code rp} whose i-th bit corresponds to
 * the i-th coefficient of the reduction polynomial.<p>
 * Note: A valid reduction polynomial is either a
 * trinomial (X^{@code m} + X^{@code k} + 1
 * with {@code m} &gt; {@code k} &gt;= 1) or a
 * pentanomial (X^{@code m} + X^{@code k3}
 * + X^{@code k2} + X^{@code k1} + 1 with
 * {@code m} &gt; {@code k3} &gt; {@code k2}
 * &gt; {@code k1} &gt;= 1).
 * @param m with 2^{@code m} being the number of elements.
 * @param rp the BigInteger whose i-th bit corresponds to
 * the i-th coefficient of the reduction polynomial.
 * @exception NullPointerException if {@code rp} is null.
 * @exception IllegalArgumentException if {@code m}
 * is not positive, or {@code rp} does not represent
 * a valid reduction polynomial.
 */
public ECFieldF2m(int m, BigInteger rp) {
    // check m and rp
    this.m = m;
    this.rp = rp;
    if (m <= 0) {
        throw new IllegalArgumentException("m is not positive");
    }
    int bitCount = this.rp.bitCount();
    if (!this.rp.testBit(0) || !this.rp.testBit(m) ||
        ((bitCount != 3) && (bitCount != 5))) {
        throw new IllegalArgumentException
            ("rp does not represent a valid reduction polynomial");
    }
    // convert rp into ks
    BigInteger temp = this.rp.clearBit(0).clearBit(m);
    this.ks = new int[bitCount-2];
    for (int i = this.ks.length-1; i >= 0; i--) {
        int index = temp.getLowestSetBit();
        this.ks[i] = index;
        temp = temp.clearBit(index);
    }
}
 
Example 3
Source File: ECFieldF2m.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an elliptic curve characteristic 2 finite
 * field which has 2^{@code m} elements with
 * polynomial basis.
 * The reduction polynomial for this field is based
 * on {@code rp} whose i-th bit corresponds to
 * the i-th coefficient of the reduction polynomial.<p>
 * Note: A valid reduction polynomial is either a
 * trinomial (X^{@code m} + X^{@code k} + 1
 * with {@code m} &gt; {@code k} &gt;= 1) or a
 * pentanomial (X^{@code m} + X^{@code k3}
 * + X^{@code k2} + X^{@code k1} + 1 with
 * {@code m} &gt; {@code k3} &gt; {@code k2}
 * &gt; {@code k1} &gt;= 1).
 * @param m with 2^{@code m} being the number of elements.
 * @param rp the BigInteger whose i-th bit corresponds to
 * the i-th coefficient of the reduction polynomial.
 * @exception NullPointerException if {@code rp} is null.
 * @exception IllegalArgumentException if {@code m}
 * is not positive, or {@code rp} does not represent
 * a valid reduction polynomial.
 */
public ECFieldF2m(int m, BigInteger rp) {
    // check m and rp
    this.m = m;
    this.rp = rp;
    if (m <= 0) {
        throw new IllegalArgumentException("m is not positive");
    }
    int bitCount = this.rp.bitCount();
    if (!this.rp.testBit(0) || !this.rp.testBit(m) ||
        ((bitCount != 3) && (bitCount != 5))) {
        throw new IllegalArgumentException
            ("rp does not represent a valid reduction polynomial");
    }
    // convert rp into ks
    BigInteger temp = this.rp.clearBit(0).clearBit(m);
    this.ks = new int[bitCount-2];
    for (int i = this.ks.length-1; i >= 0; i--) {
        int index = temp.getLowestSetBit();
        this.ks[i] = index;
        temp = temp.clearBit(index);
    }
}
 
Example 4
Source File: NetworkSpace.java    From android with GNU General Public License v3.0 6 votes vote down vote up
private BigInteger getMaskedAddress(boolean one) {
    BigInteger numAddress = netAddress;

    int numBits;
    if (isV4) {
        numBits = 32 - networkMask;
    } else {
        numBits = 128 - networkMask;
    }

    for (int i = 0; i < numBits; i++) {
        if (one)
            numAddress = numAddress.setBit(i);
        else
            numAddress = numAddress.clearBit(i);
    }
    return numAddress;
}
 
Example 5
Source File: ECFieldF2m.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates an elliptic curve characteristic 2 finite
 * field which has 2^{@code m} elements with
 * polynomial basis.
 * The reduction polynomial for this field is based
 * on {@code rp} whose i-th bit corresponds to
 * the i-th coefficient of the reduction polynomial.<p>
 * Note: A valid reduction polynomial is either a
 * trinomial (X^{@code m} + X^{@code k} + 1
 * with {@code m} &gt; {@code k} &gt;= 1) or a
 * pentanomial (X^{@code m} + X^{@code k3}
 * + X^{@code k2} + X^{@code k1} + 1 with
 * {@code m} &gt; {@code k3} &gt; {@code k2}
 * &gt; {@code k1} &gt;= 1).
 * @param m with 2^{@code m} being the number of elements.
 * @param rp the BigInteger whose i-th bit corresponds to
 * the i-th coefficient of the reduction polynomial.
 * @exception NullPointerException if {@code rp} is null.
 * @exception IllegalArgumentException if {@code m}
 * is not positive, or {@code rp} does not represent
 * a valid reduction polynomial.
 */
public ECFieldF2m(int m, BigInteger rp) {
    // check m and rp
    this.m = m;
    this.rp = rp;
    if (m <= 0) {
        throw new IllegalArgumentException("m is not positive");
    }
    int bitCount = this.rp.bitCount();
    if (!this.rp.testBit(0) || !this.rp.testBit(m) ||
        ((bitCount != 3) && (bitCount != 5))) {
        throw new IllegalArgumentException
            ("rp does not represent a valid reduction polynomial");
    }
    // convert rp into ks
    BigInteger temp = this.rp.clearBit(0).clearBit(m);
    this.ks = new int[bitCount-2];
    for (int i = this.ks.length-1; i >= 0; i--) {
        int index = temp.getLowestSetBit();
        this.ks[i] = index;
        temp = temp.clearBit(index);
    }
}
 
Example 6
Source File: ECFieldF2m.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates an elliptic curve characteristic 2 finite
 * field which has 2^{@code m} elements with
 * polynomial basis.
 * The reduction polynomial for this field is based
 * on {@code rp} whose i-th bit corresponds to
 * the i-th coefficient of the reduction polynomial.<p>
 * Note: A valid reduction polynomial is either a
 * trinomial (X^{@code m} + X^{@code k} + 1
 * with {@code m} &gt; {@code k} &gt;= 1) or a
 * pentanomial (X^{@code m} + X^{@code k3}
 * + X^{@code k2} + X^{@code k1} + 1 with
 * {@code m} &gt; {@code k3} &gt; {@code k2}
 * &gt; {@code k1} &gt;= 1).
 * @param m with 2^{@code m} being the number of elements.
 * @param rp the BigInteger whose i-th bit corresponds to
 * the i-th coefficient of the reduction polynomial.
 * @exception NullPointerException if {@code rp} is null.
 * @exception IllegalArgumentException if {@code m}
 * is not positive, or {@code rp} does not represent
 * a valid reduction polynomial.
 */
public ECFieldF2m(int m, BigInteger rp) {
    // check m and rp
    this.m = m;
    this.rp = rp;
    if (m <= 0) {
        throw new IllegalArgumentException("m is not positive");
    }
    int bitCount = this.rp.bitCount();
    if (!this.rp.testBit(0) || !this.rp.testBit(m) ||
        ((bitCount != 3) && (bitCount != 5))) {
        throw new IllegalArgumentException
            ("rp does not represent a valid reduction polynomial");
    }
    // convert rp into ks
    BigInteger temp = this.rp.clearBit(0).clearBit(m);
    this.ks = new int[bitCount-2];
    for (int i = this.ks.length-1; i >= 0; i--) {
        int index = temp.getLowestSetBit();
        this.ks[i] = index;
        temp = temp.clearBit(index);
    }
}
 
Example 7
Source File: NetworkSpace.java    From bitmask_android with GNU General Public License v3.0 6 votes vote down vote up
private BigInteger getMaskedAddress(boolean one) {
    BigInteger numAddress = netAddress;

    int numBits;
    if (isV4) {
        numBits = 32 - networkMask;
    } else {
        numBits = 128 - networkMask;
    }

    for (int i = 0; i < numBits; i++) {
        if (one)
            numAddress = numAddress.setBit(i);
        else
            numAddress = numAddress.clearBit(i);
    }
    return numAddress;
}
 
Example 8
Source File: NetworkSpace.java    From EasyVPN-Free with GNU General Public License v3.0 6 votes vote down vote up
private BigInteger getMaskedAddress(boolean one) {
    BigInteger numAddress = netAddress;

    int numBits;
    if (isV4) {
        numBits = 32 - networkMask;
    } else {
        numBits = 128 - networkMask;
    }

    for (int i = 0; i < numBits; i++) {
        if (one)
            numAddress = numAddress.setBit(i);
        else
            numAddress = numAddress.clearBit(i);
    }
    return numAddress;
}
 
Example 9
Source File: ECFieldF2m.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an elliptic curve characteristic 2 finite
 * field which has 2^{@code m} elements with
 * polynomial basis.
 * The reduction polynomial for this field is based
 * on {@code rp} whose i-th bit corresponds to
 * the i-th coefficient of the reduction polynomial.<p>
 * Note: A valid reduction polynomial is either a
 * trinomial (X^{@code m} + X^{@code k} + 1
 * with {@code m} &gt; {@code k} &gt;= 1) or a
 * pentanomial (X^{@code m} + X^{@code k3}
 * + X^{@code k2} + X^{@code k1} + 1 with
 * {@code m} &gt; {@code k3} &gt; {@code k2}
 * &gt; {@code k1} &gt;= 1).
 * @param m with 2^{@code m} being the number of elements.
 * @param rp the BigInteger whose i-th bit corresponds to
 * the i-th coefficient of the reduction polynomial.
 * @exception NullPointerException if {@code rp} is null.
 * @exception IllegalArgumentException if {@code m}
 * is not positive, or {@code rp} does not represent
 * a valid reduction polynomial.
 */
public ECFieldF2m(int m, BigInteger rp) {
    // check m and rp
    this.m = m;
    this.rp = rp;
    if (m <= 0) {
        throw new IllegalArgumentException("m is not positive");
    }
    int bitCount = this.rp.bitCount();
    if (!this.rp.testBit(0) || !this.rp.testBit(m) ||
        ((bitCount != 3) && (bitCount != 5))) {
        throw new IllegalArgumentException
            ("rp does not represent a valid reduction polynomial");
    }
    // convert rp into ks
    BigInteger temp = this.rp.clearBit(0).clearBit(m);
    this.ks = new int[bitCount-2];
    for (int i = this.ks.length-1; i >= 0; i--) {
        int index = temp.getLowestSetBit();
        this.ks[i] = index;
        temp = temp.clearBit(index);
    }
}
 
Example 10
Source File: ECFieldF2m.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates an elliptic curve characteristic 2 finite
 * field which has 2^{@code m} elements with
 * polynomial basis.
 * The reduction polynomial for this field is based
 * on {@code rp} whose i-th bit corresponds to
 * the i-th coefficient of the reduction polynomial.<p>
 * Note: A valid reduction polynomial is either a
 * trinomial (X^{@code m} + X^{@code k} + 1
 * with {@code m} &gt; {@code k} &gt;= 1) or a
 * pentanomial (X^{@code m} + X^{@code k3}
 * + X^{@code k2} + X^{@code k1} + 1 with
 * {@code m} &gt; {@code k3} &gt; {@code k2}
 * &gt; {@code k1} &gt;= 1).
 * @param m with 2^{@code m} being the number of elements.
 * @param rp the BigInteger whose i-th bit corresponds to
 * the i-th coefficient of the reduction polynomial.
 * @exception NullPointerException if {@code rp} is null.
 * @exception IllegalArgumentException if {@code m}
 * is not positive, or {@code rp} does not represent
 * a valid reduction polynomial.
 */
public ECFieldF2m(int m, BigInteger rp) {
    // check m and rp
    this.m = m;
    this.rp = rp;
    if (m <= 0) {
        throw new IllegalArgumentException("m is not positive");
    }
    int bitCount = this.rp.bitCount();
    if (!this.rp.testBit(0) || !this.rp.testBit(m) ||
        ((bitCount != 3) && (bitCount != 5))) {
        throw new IllegalArgumentException
            ("rp does not represent a valid reduction polynomial");
    }
    // convert rp into ks
    BigInteger temp = this.rp.clearBit(0).clearBit(m);
    this.ks = new int[bitCount-2];
    for (int i = this.ks.length-1; i >= 0; i--) {
        int index = temp.getLowestSetBit();
        this.ks[i] = index;
        temp = temp.clearBit(index);
    }
}
 
Example 11
Source File: ECFieldF2m.java    From jdk-1.7-annotated with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an elliptic curve characteristic 2 finite
 * field which has 2^<code>m</code> elements with
 * polynomial basis.
 * The reduction polynomial for this field is based
 * on <code>rp</code> whose i-th bit correspondes to
 * the i-th coefficient of the reduction polynomial.<p>
 * Note: A valid reduction polynomial is either a
 * trinomial (X^<code>m</code> + X^<code>k</code> + 1
 * with <code>m</code> > <code>k</code> >= 1) or a
 * pentanomial (X^<code>m</code> + X^<code>k3</code>
 * + X^<code>k2</code> + X^<code>k1</code> + 1 with
 * <code>m</code> > <code>k3</code> > <code>k2</code>
 * > <code>k1</code> >= 1).
 * @param m with 2^<code>m</code> being the number of elements.
 * @param rp the BigInteger whose i-th bit corresponds to
 * the i-th coefficient of the reduction polynomial.
 * @exception NullPointerException if <code>rp</code> is null.
 * @exception IllegalArgumentException if <code>m</code>
 * is not positive, or <code>rp</code> does not represent
 * a valid reduction polynomial.
 */
public ECFieldF2m(int m, BigInteger rp) {
    // check m and rp
    this.m = m;
    this.rp = rp;
    if (m <= 0) {
        throw new IllegalArgumentException("m is not positive");
    }
    int bitCount = this.rp.bitCount();
    if (!this.rp.testBit(0) || !this.rp.testBit(m) ||
        ((bitCount != 3) && (bitCount != 5))) {
        throw new IllegalArgumentException
            ("rp does not represent a valid reduction polynomial");
    }
    // convert rp into ks
    BigInteger temp = this.rp.clearBit(0).clearBit(m);
    this.ks = new int[bitCount-2];
    for (int i = this.ks.length-1; i >= 0; i--) {
        int index = temp.getLowestSetBit();
        this.ks[i] = index;
        temp = temp.clearBit(index);
    }
}
 
Example 12
Source File: ECFieldF2m.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates an elliptic curve characteristic 2 finite
 * field which has 2^{@code m} elements with
 * polynomial basis.
 * The reduction polynomial for this field is based
 * on {@code rp} whose i-th bit corresponds to
 * the i-th coefficient of the reduction polynomial.<p>
 * Note: A valid reduction polynomial is either a
 * trinomial (X^{@code m} + X^{@code k} + 1
 * with {@code m} &gt; {@code k} &gt;= 1) or a
 * pentanomial (X^{@code m} + X^{@code k3}
 * + X^{@code k2} + X^{@code k1} + 1 with
 * {@code m} &gt; {@code k3} &gt; {@code k2}
 * &gt; {@code k1} &gt;= 1).
 * @param m with 2^{@code m} being the number of elements.
 * @param rp the BigInteger whose i-th bit corresponds to
 * the i-th coefficient of the reduction polynomial.
 * @exception NullPointerException if {@code rp} is null.
 * @exception IllegalArgumentException if {@code m}
 * is not positive, or {@code rp} does not represent
 * a valid reduction polynomial.
 */
public ECFieldF2m(int m, BigInteger rp) {
    // check m and rp
    this.m = m;
    this.rp = rp;
    if (m <= 0) {
        throw new IllegalArgumentException("m is not positive");
    }
    int bitCount = this.rp.bitCount();
    if (!this.rp.testBit(0) || !this.rp.testBit(m) ||
        ((bitCount != 3) && (bitCount != 5))) {
        throw new IllegalArgumentException
            ("rp does not represent a valid reduction polynomial");
    }
    // convert rp into ks
    BigInteger temp = this.rp.clearBit(0).clearBit(m);
    this.ks = new int[bitCount-2];
    for (int i = this.ks.length-1; i >= 0; i--) {
        int index = temp.getLowestSetBit();
        this.ks[i] = index;
        temp = temp.clearBit(index);
    }
}
 
Example 13
Source File: ECFieldF2m.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates an elliptic curve characteristic 2 finite
 * field which has 2^{@code m} elements with
 * polynomial basis.
 * The reduction polynomial for this field is based
 * on {@code rp} whose i-th bit corresponds to
 * the i-th coefficient of the reduction polynomial.<p>
 * Note: A valid reduction polynomial is either a
 * trinomial (X^{@code m} + X^{@code k} + 1
 * with {@code m} &gt; {@code k} &gt;= 1) or a
 * pentanomial (X^{@code m} + X^{@code k3}
 * + X^{@code k2} + X^{@code k1} + 1 with
 * {@code m} &gt; {@code k3} &gt; {@code k2}
 * &gt; {@code k1} &gt;= 1).
 * @param m with 2^{@code m} being the number of elements.
 * @param rp the BigInteger whose i-th bit corresponds to
 * the i-th coefficient of the reduction polynomial.
 * @exception NullPointerException if {@code rp} is null.
 * @exception IllegalArgumentException if {@code m}
 * is not positive, or {@code rp} does not represent
 * a valid reduction polynomial.
 */
public ECFieldF2m(int m, BigInteger rp) {
    // check m and rp
    this.m = m;
    this.rp = rp;
    if (m <= 0) {
        throw new IllegalArgumentException("m is not positive");
    }
    int bitCount = this.rp.bitCount();
    if (!this.rp.testBit(0) || !this.rp.testBit(m) ||
        ((bitCount != 3) && (bitCount != 5))) {
        throw new IllegalArgumentException
            ("rp does not represent a valid reduction polynomial");
    }
    // convert rp into ks
    BigInteger temp = this.rp.clearBit(0).clearBit(m);
    this.ks = new int[bitCount-2];
    for (int i = this.ks.length-1; i >= 0; i--) {
        int index = temp.getLowestSetBit();
        this.ks[i] = index;
        temp = temp.clearBit(index);
    }
}
 
Example 14
Source File: ECFieldF2m.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an elliptic curve characteristic 2 finite
 * field which has 2^{@code m} elements with
 * polynomial basis.
 * The reduction polynomial for this field is based
 * on {@code rp} whose i-th bit corresponds to
 * the i-th coefficient of the reduction polynomial.<p>
 * Note: A valid reduction polynomial is either a
 * trinomial (X^{@code m} + X^{@code k} + 1
 * with {@code m} &gt; {@code k} &gt;= 1) or a
 * pentanomial (X^{@code m} + X^{@code k3}
 * + X^{@code k2} + X^{@code k1} + 1 with
 * {@code m} &gt; {@code k3} &gt; {@code k2}
 * &gt; {@code k1} &gt;= 1).
 * @param m with 2^{@code m} being the number of elements.
 * @param rp the BigInteger whose i-th bit corresponds to
 * the i-th coefficient of the reduction polynomial.
 * @throws    NullPointerException if {@code rp} is null.
 * @throws    IllegalArgumentException if {@code m}
 * is not positive, or {@code rp} does not represent
 * a valid reduction polynomial.
 */
public ECFieldF2m(int m, BigInteger rp) {
    // check m and rp
    this.m = m;
    this.rp = rp;
    if (m <= 0) {
        throw new IllegalArgumentException("m is not positive");
    }
    int bitCount = this.rp.bitCount();
    if (!this.rp.testBit(0) || !this.rp.testBit(m) ||
        ((bitCount != 3) && (bitCount != 5))) {
        throw new IllegalArgumentException
            ("rp does not represent a valid reduction polynomial");
    }
    // convert rp into ks
    BigInteger temp = this.rp.clearBit(0).clearBit(m);
    this.ks = new int[bitCount-2];
    for (int i = this.ks.length-1; i >= 0; i--) {
        int index = temp.getLowestSetBit();
        this.ks[i] = index;
        temp = temp.clearBit(index);
    }
}
 
Example 15
Source File: BigIntegerOperateBitsTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * clearBit(int n) inside a positive number
 */
public void testClearBitPositiveInside4 () {
    byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26};
    int aSign = 1;
    int number = 50;
    byte rBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger result = aNumber.clearBit(number);
    byte resBytes[] = new byte[rBytes.length];
    resBytes = result.toByteArray();
    for(int i = 0; i < resBytes.length; i++) {
        assertTrue(resBytes[i] == rBytes[i]);
    }
    assertEquals("incorrect sign", 1, result.signum());
}
 
Example 16
Source File: BigIntegerOperateBitsTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * clearBit(int n) outside a positive number
 */
public void testClearBitPositiveOutside1() {
    byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26};
    int aSign = 1;
    int number = 150;
    byte rBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger result = aNumber.clearBit(number);
    byte resBytes[] = new byte[rBytes.length];
    resBytes = result.toByteArray();
    for(int i = 0; i < resBytes.length; i++) {
        assertTrue(resBytes[i] == rBytes[i]);
    }
    assertEquals("incorrect sign", 1, result.signum());
}
 
Example 17
Source File: BigIntegerOperateBitsTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * clearBit(int n) outside a positive number
 */
public void testClearBitPositiveOutside2() {
    byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26};
    int aSign = 1;
    int number = 191;
    byte rBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger result = aNumber.clearBit(number);
    byte resBytes[] = new byte[rBytes.length];
    resBytes = result.toByteArray();
    for(int i = 0; i < resBytes.length; i++) {
        assertTrue(resBytes[i] == rBytes[i]);
    }
    assertEquals("incorrect sign", 1, result.signum());
}
 
Example 18
Source File: BigIntegerOperateBitsTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * clearBit(2) in the negative number with all ones in bit representation
 */
public void testClearBitNegativeInside3() {
    String as = "-18446744073709551615";
    int number = 2;
    BigInteger aNumber = new BigInteger(as);
    BigInteger result = aNumber.clearBit(number);
    assertEquals(as, result.toString());
}
 
Example 19
Source File: Tnaf.java    From ripple-lib-java with ISC License 4 votes vote down vote up
/**
 * Computes the <code>&tau;</code>-adic NAF (non-adjacent form) of an
 * element <code>&lambda;</code> of <code><b>Z</b>[&tau;]</code>.
 * @param mu The parameter <code>&mu;</code> of the elliptic curve.
 * @param lambda The element <code>&lambda;</code> of
 * <code><b>Z</b>[&tau;]</code>.
 * @return The <code>&tau;</code>-adic NAF of <code>&lambda;</code>.
 */
public static byte[] tauAdicNaf(byte mu, ZTauElement lambda)
{
    if (!((mu == 1) || (mu == -1)))
    {
        throw new IllegalArgumentException("mu must be 1 or -1");
    }
    
    BigInteger norm = norm(mu, lambda);

    // Ceiling of log2 of the norm 
    int log2Norm = norm.bitLength();

    // If length(TNAF) > 30, then length(TNAF) < log2Norm + 3.52
    int maxLength = log2Norm > 30 ? log2Norm + 4 : 34;

    // The array holding the TNAF
    byte[] u = new byte[maxLength];
    int i = 0;

    // The actual length of the TNAF
    int length = 0;

    BigInteger r0 = lambda.u;
    BigInteger r1 = lambda.v;

    while(!((r0.equals(ECConstants.ZERO)) && (r1.equals(ECConstants.ZERO))))
    {
        // If r0 is odd
        if (r0.testBit(0))
        {
            u[i] = (byte) ECConstants.TWO.subtract((r0.subtract(r1.shiftLeft(1))).mod(ECConstants.FOUR)).intValue();

            // r0 = r0 - u[i]
            if (u[i] == 1)
            {
                r0 = r0.clearBit(0);
            }
            else
            {
                // u[i] == -1
                r0 = r0.add(ECConstants.ONE);
            }
            length = i;
        }
        else
        {
            u[i] = 0;
        }

        BigInteger t = r0;
        BigInteger s = r0.shiftRight(1);
        if (mu == 1)
        {
            r0 = r1.add(s);
        }
        else
        {
            // mu == -1
            r0 = r1.subtract(s);
        }

        r1 = t.shiftRight(1).negate();
        i++;
    }

    length++;

    // Reduce the TNAF array to its actual length
    byte[] tnaf = new byte[length];
    System.arraycopy(u, 0, tnaf, 0, length);
    return tnaf;
}
 
Example 20
Source File: BigIntegerMutatorTest.java    From pitest with Apache License 2.0 4 votes vote down vote up
@Override
BigInteger apply(BigInteger left, BigInteger right) {
  return left.clearBit(right.intValue());
}