Example #1
0
  @Override
  public byte[] toBytes() {
    byte[] bytes = value.toByteArray();

    if (bytes.length > field.getLengthInBytes()) {
      // strip the zero prefix
      if (bytes[0] == 0 && bytes.length == field.getLengthInBytes() + 1) {
        // Remove it
        bytes = Arrays.copyOfRange(bytes, 1, bytes.length);
      } else throw new IllegalStateException("result has more than FixedLengthInBytes.");
    } else if (bytes.length < field.getLengthInBytes()) {
      byte[] result = new byte[field.getLengthInBytes()];
      System.arraycopy(bytes, 0, result, field.getLengthInBytes() - bytes.length, bytes.length);
      return result;
    }
    return bytes;
  }
Example #2
0
  public int setFromBytes(byte[] source, int offset) {
    byte[] buffer = Arrays.copyOf(source, offset, field.getLengthInBytes());
    value = new BigInteger(1, buffer).mod(field.order);

    return buffer.length;
  }