add
public static byte[] add(int maxbytes,
byte[]... arrays)
Add a variable number of byte arrays of variable size.
This is useful for operations involving positive numbers
encoded as byte arrays, where the output is going to
be modulo a power of two where the power is divisible by 8
(so 2**8, which is what can be rendered in hex in a byte).
We are exploiting the fact that x modulo y, where x is
positive and y is a power of two can reduce down to (x & (y-1)),
and we are also avoiding BigInteger's nasty habit of putting
unneeded sign bytes out when you ask it to do toByteArray() or
you have to construct a BigInteger from bytes. We have no need
of expensive BigInteger.mod operations for numbers that are powers
of 2 that end up also being powers of 2**8.
// V=(V+H+C+reseed_counter)mod2**440
BigInteger accumulator = new BigInteger(1, V);
accumulator = accumulator.add(new BigInteger(1, H));
accumulator = accumulator.add(new BigInteger(1, C));
byte[] reseedB = new byte[4];
msbf4((int)reseed_counter, reseedB, 0);
accumulator = accumulator.add(new BigInteger(1, reseedB));
accumulator = accumulator.mod(MOD2_440);
byte[] vprime = stripOffSignByte(accumulator.toByteArray());
int vprimelen = vprime.length;
if (vprimelen > MIN_SEED_BYTES) {
System.arraycopy(vprime, vprimelen-MIN_SEED_BYTES, V, 0, V.length);
vprime = null;
} else
V = vprime;
becomes simply
V = ByteAdder.add(55, V, H, C, reseedB);
- Parameters:
maxbytes
- the number of bytes desired in the output arrayarrays
- variable number of byte arrays of variable size
- Returns:
- a byte array of the specified length, with the sum of the input