Ported from the Jan. 13, 2016 C version at https://github.com/Kleshni/Elligator-2 Note: That code was completely rewritten May 8, 2017 and is now much more complex. No apparent license.
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final I2PAppContextThe app context.private static final BigIntegerThe curve constant A.private static final longThe curve constant A as a long.private static final CurveThe Curve25519 curve.private static final booleanDeveloper switch disabling the mapping.private static final BigIntegerThe exponent (p-1)/2 for the Legendre check.private static final BigIntegerThe exponent (p-1)/4 for the square root.private static final BigIntegerThe exponent (p+3)/8 for the square root.private static final BigIntegerLittleEndianEncodingEncoder of field elements to and from bytes.private static final FieldThe field the mapping works in.private static final BigIntegerThe inverse of u in the field.private static final BigIntegerThe negated curve constant -A.private static final BigIntegerThe Curve25519 field prime.private static final intBytes in a field element representative.private static final EdDSANamedCurveSpecThe named curve the field comes from.private static final BigIntegerA square root of -1 in the field.private static final BigIntegerThe value two, for exponent math.private static final BigIntegerA non-square multiplier chosen for the mapping.Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionstatic PublicKeydecode(byte[] representative) From javascript version documentation:static PublicKeydecode(AtomicBoolean alternative, byte[] representative) From javascript version documentation:byte[]Use for on-the-wire.private static byte[]Output will look like 254 random bits.private static intLegendre symbol of a modulo the field prime p, computed as a^((p-1)/2) mod p.private static BigIntegerComputes the square root in the field.
Field Details
_context
The app context.p
The Curve25519 field prime.divide_plus_p_3_8
The exponent (p+3)/8 for the square root.divide_minus_p_1_2
The exponent (p-1)/2 for the Legendre check.divide_minus_p_1_4
The exponent (p-1)/4 for the square root.square_root_negative_1
A square root of -1 in the field.A_INT
A
The curve constant A.negative_A
The negated curve constant -A.u
A non-square multiplier chosen for the mapping.inverted_u
The inverse of u in the field.TWO
The value two, for exponent math.REPRESENTATIVE_LENGTH
private static final int REPRESENTATIVE_LENGTHBytes in a field element representative.- See Also:
SPEC
The named curve the field comes from.CURVE
The Curve25519 curve.FIELD
The field the mapping works in.ENCODING
Encoder of field elements to and from bytes.DISABLE
private static final boolean DISABLEDeveloper switch disabling the mapping.- See Also:
Constructor Details
Elligator2
Create an Elligator2 instance.- Parameters:
ctx- the I2P app context
Method Details
encode
Use for on-the-wire. Don't use for unit tests as output will be randomized based on the 'alternative' and the high bits. There are eight possible encodings for any point. Output will look like 256 random bits.- Parameters:
point- the public key to encode- Returns:
- "representative", little endian or null on failure
encode
Output will look like 254 random bits. High two bits of highBits will be ORed in.From javascript version documentation:
The algorithm can return two different values for a single x coordinate if it's not 0. Which one to return is determined by y coordinate. Since Curve25519 doesn't use y due to optimizations, you should specify a Boolean value as the second argument of the function. It should be unpredictable, because it's recoverable from the representative.
- Parameters:
point- the public key whose x coordinate is convertedalternative- picks which of the two possible roots to encodehighBits- High two bits will be ORed into rv[31]- Returns:
- "representative", little endian or null on failure
decode
From javascript version documentation:Returns an array with the point and the second argument of the corresponding call to the `encode` function. It's also able to return null if the representative is invalid (there are only 10 invalid representatives).
- Parameters:
representative- the encoded data, little endian, 32 bytes WILL BE MODIFIED by masking byte 31- Returns:
- x or null on failure
decode
From javascript version documentation:Returns an array with the point and the second argument of the corresponding call to the `encode` function. It's also able to return null if the representative is invalid (there are only 10 invalid representatives).
- Parameters:
alternative- out parameter, or null if you don't carerepresentative- the encoded data, little endian, 32 bytes; WILL BE MODIFIED by masking byte 31- Returns:
- x or null on failure
square_root
Computes the square root in the field.- Parameters:
x- the value to take the root of- Returns:
- the root, x if there is none
legendre
Legendre symbol of a modulo the field prime p, computed as a^((p-1)/2) mod p.https://gmplib.org/manual/Number-Theoretic-Functions.html https://en.wikipedia.org/wiki/Legendre_symbol
- Parameters:
a- must already be mod(p)- Returns:
- -1/0/1