0

LargeInteger doesn't appear to have an equivalent function to BigInteger's and.

Since and(BigInteger val) "Returns a BigInteger whose value is (this & val). (This method returns a negative BigInteger if and only if this and val are both negative.)", I tried to follow this great answer on reproducing testBit with

static LargeInteger and(LargeInteger i, LargeInteger j) {
    return i & j;
}

but the compiler reports

error: bad operand types for binary operator '&'
    return i & j;
             ^

How can BigInteger's and be reproduced to be used on LargeInteger?

Community
  • 1
  • 1
  • Are you really sure that LargeInteger is what you want? It seems like you do a lot of bit level stuff that is not supported there? – Ingo Jan 22 '14 at 17:18
  • @Ingo Thank you for looking Ingo! No, I'm not at all, but the performance of ed25519 is unbearable with `BigInteger`, and I've found that this could be an easy bottleneck to fix by substituting with `LargeInteger`. If, after the code is converted, it works correctly and faster, I will fork and upload the alterations. If `and` is impossible, I can fall back by converting a `BigInteger` to `LargeInteger`, but I'd prefer to remove `BigInteger` completely. https://github.com/k3d3/ed25519-java –  Jan 22 '14 at 17:23

2 Answers2

0

Judging from the documentation, there are methods for converting LargeIntegers into byte arrays, and also to create a LargeInteger from byte arrays. Hence, you can do the following:

convert operands to byte arrays
combine the individual bytes with the operator you want (&, |, ^)
convert resulting byte array back to LargeInteger

Now, correct me if I am wrong, but the original python code seems to only do n & 1. Since you have even() and odd() methods, why not use them? The following identity holds:

 large & 1 = large.odd() ? 1 : 0
Ingo
  • 36,037
  • 5
  • 53
  • 100
  • Thank you Ingo! You are correct, I have no idea since I have no experience with binary. I'm merely trying to substitute `BigInteger` with `LargeInteger` for ed25519. I also have no experience with cryptography, so I'm holding my breath, hoping this works. –  Jan 22 '14 at 17:29
  • @Gracchus, see if I am correct that you only need (&1), and not (x&y) in general. – Ingo Jan 22 '14 at 17:30
  • `convert operands to byte arrays` I can't find such method though (only find the one to convert from byte to LargeInteger). What is the method that you found? – nhahtdh Jan 22 '14 at 17:31
  • 1
    @nhahtdh http://jscience.org/api/org/jscience/mathematics/number/LargeInteger.html#toByteArray%28byte[],%20int%29 – Ingo Jan 22 '14 at 17:33
0

org.jscience.mathematics.number.LargeInteger does not seem to have a similar bit-wise function and (if I have sougth the right class & version).

static LargeInteger and(LargeInteger lhs, LargeInteger rhs) {
     long l = lhs.longValue(); // Low order bits
     long r = rhs.longValue();
     long lo = l & r;

     LargeInteger hi = LargeInteger.ZERO;
     if (lhs.bitLength() > 64 && rhs.bitLength() > 64) {
         hi = and(lhs.shiftRight(64), rhs.shiftRight(64)).shiftLeft(64);
     }
     return hi.plus(lo);
}

Mind, that for a bitwise or the condition needs || instead of &&.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138