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

The following examples show how to use java.math.BigInteger#clearBit() . These examples are extracted from open source projects. 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
/**
 * 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
/**
 * 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 Project: j2objc   File: ECFieldF2m.java    License: 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
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
/**
 * 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 Project: jdk8u60   File: ECFieldF2m.java    License: 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
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
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
/**
 * 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 Project: jdk-1.7-annotated   File: ECFieldF2m.java    License: 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 11
/**
 * 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 12
Source Project: Bytecoder   File: ECFieldF2m.java    License: 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 13
/**
 * 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 Project: Java8CN   File: ECFieldF2m.java    License: 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 15
/**
 * 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 16
/**
 * 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 17
/**
 * 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
/**
 * 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 19
Source Project: ripple-lib-java   File: Tnaf.java    License: 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 Project: pitest   File: BigIntegerMutatorTest.java    License: Apache License 2.0 4 votes vote down vote up
@Override
BigInteger apply(BigInteger left, BigInteger right) {
  return left.clearBit(right.intValue());
}