@Override public String toString() { return "[Ed25519FieldElement val="+Utils.bytesToHex(toByteArray())+"]"; } }
/** * Constant-time byte[] comparison. * @param b a byte[] * @param c a byte[] * @return 1 if b and c are equal, 0 otherwise. */ public static int equal(byte[] b, byte[] c) { int result = 0; for (int i = 0; i < 32; i++) { result |= b[i] ^ c[i]; } return equal(result, 0); }
/** * Test method for {@link net.i2p.crypto.eddsa.Utils#bit(byte[], int)}. */ @Test public void testBit() { assertThat(Utils.bit(new byte[] {0}, 0), is(0)); assertThat(Utils.bit(new byte[] {8}, 3), is(1)); assertThat(Utils.bit(new byte[] {1, 2, 3}, 9), is(1)); assertThat(Utils.bit(new byte[] {1, 2, 3}, 15), is(0)); assertThat(Utils.bit(new byte[] {1, 2, 3}, 16), is(1)); }
final int bnegative = Utils.negative(b); .cmov(this.precmp[pos][0], Utils.equal(babs, 1)) .cmov(this.precmp[pos][1], Utils.equal(babs, 2)) .cmov(this.precmp[pos][2], Utils.equal(babs, 3)) .cmov(this.precmp[pos][3], Utils.equal(babs, 4)) .cmov(this.precmp[pos][4], Utils.equal(babs, 5)) .cmov(this.precmp[pos][5], Utils.equal(babs, 6)) .cmov(this.precmp[pos][6], Utils.equal(babs, 7)) .cmov(this.precmp[pos][7], Utils.equal(babs, 8));
public TestTuple(String line) { caseNum = ++numCases; String[] x = line.split(":"); seed = Utils.hexToBytes(x[0].substring(0, 64)); pk = Utils.hexToBytes(x[1]); message = Utils.hexToBytes(x[2]); sig = Utils.hexToBytes(x[3].substring(0, 128)); } }
private static String deriveEd25519PrivateKey(final String seed, final int... indexes) { try { return Utils.bytesToHex(SLIP10.deriveEd25519PrivateKey(Utils.hexToBytes(seed), indexes)); } catch (Exception e) { throw new RuntimeException(e); } } }
/** * Test method for {@link net.i2p.crypto.eddsa.Utils#negative(int)}. */ @Test public void testNegative() { assertThat(Utils.negative(0), is(0)); assertThat(Utils.negative(1), is(0)); assertThat(Utils.negative(-1), is(1)); assertThat(Utils.negative(32), is(0)); assertThat(Utils.negative(-100), is(1)); assertThat(Utils.negative(127), is(0)); assertThat(Utils.negative(-255), is(1)); }
/** * Test method for {@link net.i2p.crypto.eddsa.math.bigint.BigIntegerScalarOps#multiplyAndAdd(byte[], byte[], byte[])}. */ @Test public void testMultiplyAndAdd() { // Example from test case 1 byte[] h = Utils.hexToBytes("86eabc8e4c96193d290504e7c600df6cf8d8256131ec2c138a3e7e162e525404"); byte[] a = Utils.hexToBytes("307c83864f2833cb427a2ef1c00a013cfdff2768d980c0a3a520f006904de94f"); byte[] r = Utils.hexToBytes("f38907308c893deaf244787db4af53682249107418afc2edc58f75ac58a07404"); byte[] S = Utils.hexToBytes("5fb8821590a33bacc61e39701cf9b46bd25bf5f0595bbe24655141438e7a100b"); assertThat(scalarOps.multiplyAndAdd(h, a, r), is(equalTo(S))); }
final int bnegative = Utils.negative(b); .cmov(this.precmp[pos][0], Utils.equal(babs, 1)) .cmov(this.precmp[pos][1], Utils.equal(babs, 2)) .cmov(this.precmp[pos][2], Utils.equal(babs, 3)) .cmov(this.precmp[pos][3], Utils.equal(babs, 4)) .cmov(this.precmp[pos][4], Utils.equal(babs, 5)) .cmov(this.precmp[pos][5], Utils.equal(babs, 6)) .cmov(this.precmp[pos][6], Utils.equal(babs, 7)) .cmov(this.precmp[pos][7], Utils.equal(babs, 8));
/** * Gets a value indicating whether or not the field element is non-zero. * * @return 1 if it is non-zero, 0 otherwise. */ public boolean isNonZero() { final byte[] s = toByteArray(); return Utils.equal(s, ZERO) == 0; }
@Test public void hexToBytesReturnsCorrectByteArray() { Assert.assertThat(Utils.hexToBytes(hex1), IsEqual.equalTo(bytes1)); Assert.assertThat(Utils.hexToBytes(hex2), IsEqual.equalTo(bytes2)); Assert.assertThat(Utils.hexToBytes(hex3), IsEqual.equalTo(bytes3)); }
@Test public void bytesToHexReturnsCorrectHexString() { Assert.assertThat(Utils.bytesToHex(bytes1), IsEqual.equalTo(hex1)); Assert.assertThat(Utils.bytesToHex(bytes2), IsEqual.equalTo(hex2)); Assert.assertThat(Utils.bytesToHex(bytes3), IsEqual.equalTo(hex3)); } }
/** * Scalar multiply the group element by the field element. * * @param g The group element. * @param f The field element. * @return The resulting group element. */ public static GroupElement scalarMultiplyGroupElement(final GroupElement g, final FieldElement f) { final byte[] bytes = f.toByteArray(); GroupElement h = curve.getZero(GroupElement.Representation.P3); for (int i=254; i>=0; i--) { h = doubleGroupElement(h); if (Utils.bit(bytes, i) == 1) { h = addGroupElements(h, g); } } return h; }
@Override public boolean equals(Object obj) { if (!(obj instanceof Ed25519FieldElement)) return false; Ed25519FieldElement fe = (Ed25519FieldElement) obj; return 1==Utils.equal(toByteArray(), fe.toByteArray()); }
/** * Test method for {@link net.i2p.crypto.eddsa.math.bigint.BigIntegerScalarOps#reduce(byte[])}. */ @Test public void testReduce() { // Example from test case 1 byte[] r = Utils.hexToBytes("b6b19cd8e0426f5983fa112d89a143aa97dab8bc5deb8d5b6253c928b65272f4044098c2a990039cde5b6a4818df0bfb6e40dc5dee54248032962323e701352d"); assertThat(scalarOps.reduce(r), is(equalTo(Utils.hexToBytes("f38907308c893deaf244787db4af53682249107418afc2edc58f75ac58a07404")))); }
@Override public String toString() { return "[Ed25519FieldElement val="+Utils.bytesToHex(toByteArray())+"]"; } }
if ((x.isNegative() ? 1 : 0) != Utils.bit(s, curve.getField().getb()-1)) { x = x.negate();
/** * Test method for {@link net.i2p.crypto.eddsa.Utils#equal(int, int)}. */ @Test public void testIntEqual() { assertThat(Utils.equal(0, 0), is(1)); assertThat(Utils.equal(1, 1), is(1)); assertThat(Utils.equal(1, 0), is(0)); assertThat(Utils.equal(1, 127), is(0)); assertThat(Utils.equal(-127, 127), is(0)); assertThat(Utils.equal(-42, -42), is(1)); assertThat(Utils.equal(255, 255), is(1)); assertThat(Utils.equal(-255, -256), is(0)); }
/** * Test method for {@link net.i2p.crypto.eddsa.math.bigint.BigIntegerScalarOps#multiplyAndAdd(byte[], byte[], byte[])}. */ @Test public void testMultiplyAndAdd() { ScalarOps sc = new BigIntegerScalarOps(ed25519Field, new BigInteger("5")); assertThat(sc.multiplyAndAdd(new byte[] {7}, new byte[] {2}, new byte[] {5}), is(equalTo(Utils.hexToBytes("0400000000000000000000000000000000000000000000000000000000000000")))); ScalarOps sc2 = new BigIntegerScalarOps(ed25519Field, new BigInteger("7237005577332262213973186563042994240857116359379907606001950938285454250989")); // Example from test case 1 byte[] h = Utils.hexToBytes("86eabc8e4c96193d290504e7c600df6cf8d8256131ec2c138a3e7e162e525404"); byte[] a = Utils.hexToBytes("307c83864f2833cb427a2ef1c00a013cfdff2768d980c0a3a520f006904de94f"); byte[] r = Utils.hexToBytes("f38907308c893deaf244787db4af53682249107418afc2edc58f75ac58a07404"); byte[] S = Utils.hexToBytes("5fb8821590a33bacc61e39701cf9b46bd25bf5f0595bbe24655141438e7a100b"); assertThat(sc2.multiplyAndAdd(h, a, r), is(equalTo(S))); }
/** * Use this method once to generated an "authToken" which is the * combination of a random "user" and the hash of an EdDSA-key. * Persist the authToken somewhere, either in a static final, inside a property etc. * * @return An authToken to be used with the constructor of AirPlayAuth. */ public static String generateNewAuthToken() { String clientId = AuthUtils.randomString(16); return clientId + "@" + net.i2p.crypto.eddsa.Utils.bytesToHex(new KeyPairGenerator().generateKeyPair().getPrivate().getEncoded()); }