class Ed25519FieldElement implements Serializable {

  public Ed25519EncodedFieldElement encode() {
    // Step 1:
    final Ed25519FieldElement g = this.modP();
    final int[] gValues = g.getRaw();
    final int h0 = gValues[0];
    final int h1 = gValues[1];
    final int h2 = gValues[2];
    final int h3 = gValues[3];
    final int h4 = gValues[4];
    final int h5 = gValues[5];
    final int h6 = gValues[6];
    final int h7 = gValues[7];
    final int h8 = gValues[8];
    final int h9 = gValues[9];

    // Step 2:
    final byte[] s = new byte[32];
    s[0] = (byte) (h0);
    s[1] = (byte) (h0 >> 8);
    s[2] = (byte) (h0 >> 16);
    s[3] = (byte) ((h0 >> 24) | (h1 << 2));
    s[4] = (byte) (h1 >> 6);
    s[5] = (byte) (h1 >> 14);
    s[6] = (byte) ((h1 >> 22) | (h2 << 3));
    s[7] = (byte) (h2 >> 5);
    s[8] = (byte) (h2 >> 13);
    s[9] = (byte) ((h2 >> 21) | (h3 << 5));
    s[10] = (byte) (h3 >> 3);
    s[11] = (byte) (h3 >> 11);
    s[12] = (byte) ((h3 >> 19) | (h4 << 6));
    s[13] = (byte) (h4 >> 2);
    s[14] = (byte) (h4 >> 10);
    s[15] = (byte) (h4 >> 18);
    s[16] = (byte) (h5);
    s[17] = (byte) (h5 >> 8);
    s[18] = (byte) (h5 >> 16);
    s[19] = (byte) ((h5 >> 24) | (h6 << 1));
    s[20] = (byte) (h6 >> 7);
    s[21] = (byte) (h6 >> 15);
    s[22] = (byte) ((h6 >> 23) | (h7 << 3));
    s[23] = (byte) (h7 >> 5);
    s[24] = (byte) (h7 >> 13);
    s[25] = (byte) ((h7 >> 21) | (h8 << 4));
    s[26] = (byte) (h8 >> 4);
    s[27] = (byte) (h8 >> 12);
    s[28] = (byte) ((h8 >> 20) | (h9 << 6));
    s[29] = (byte) (h9 >> 2);
    s[30] = (byte) (h9 >> 10);
    s[31] = (byte) (h9 >> 18);

    return new Ed25519EncodedFieldElement(s);
  }

  public  Ed25519FieldElement(final int[] values);

}