| 1 | /** |
| 2 | * EdDSA-Java by str4d |
| 3 | * |
| 4 | * To the extent possible under law, the person who associated CC0 with |
| 5 | * EdDSA-Java has waived all copyright and related or neighboring rights |
| 6 | * to EdDSA-Java. |
| 7 | * |
| 8 | * You should have received a copy of the CC0 legalcode along with this |
| 9 | * work. If not, see <https://creativecommons.org/publicdomain/zero/1.0/>. |
| 10 | * |
| 11 | */ |
| 12 | package net.i2p.crypto.eddsa.math; |
| 13 | |
| 14 | /** |
| 15 | * Common interface for all $(b-1)$-bit encodings of elements |
| 16 | * of EdDSA finite fields. |
| 17 | * @author str4d |
| 18 | * |
| 19 | */ |
| 20 | public abstract class Encoding { |
| 21 | protected Field f; |
| 22 | |
| 23 | public synchronized void setField(Field f) { |
| 24 | if (this.f != null) |
| 25 | throw new IllegalStateException("already set"); |
| 26 | this.f = f; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Encode a FieldElement in its $(b-1)$-bit encoding. |
| 31 | * @param x the FieldElement to encode |
| 32 | * @return the $(b-1)$-bit encoding of this FieldElement. |
| 33 | */ |
| 34 | public abstract byte[] encode(FieldElement x); |
| 35 | |
| 36 | /** |
| 37 | * Decode a FieldElement from its $(b-1)$-bit encoding. |
| 38 | * The highest bit is masked out. |
| 39 | * @param in the $(b-1)$-bit encoding of a FieldElement. |
| 40 | * @return the FieldElement represented by 'val'. |
| 41 | */ |
| 42 | public abstract FieldElement decode(byte[] in); |
| 43 | |
| 44 | /** |
| 45 | * From the Ed25519 paper:<br> |
| 46 | * $x$ is negative if the $(b-1)$-bit encoding of $x$ is lexicographically larger |
| 47 | * than the $(b-1)$-bit encoding of -x. If $q$ is an odd prime and the encoding |
| 48 | * is the little-endian representation of $\{0, 1,\dots, q-1\}$ then the negative |
| 49 | * elements of $F_q$ are $\{1, 3, 5,\dots, q-2\}$. |
| 50 | * @param x the FieldElement to check |
| 51 | * @return true if negative |
| 52 | */ |
| 53 | public abstract boolean isNegative(FieldElement x); |
| 54 | } |