| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
package net.i2p.crypto.eddsa.math.ed25519; |
| 13 |
|
| 14 |
import net.i2p.crypto.eddsa.math.*; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
public class Ed25519LittleEndianEncoding extends Encoding { |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public byte[] encode(FieldElement x) { |
| 79 |
int[] h = ((Ed25519FieldElement)x).t; |
| 80 |
int h0 = h[0]; |
| 81 |
int h1 = h[1]; |
| 82 |
int h2 = h[2]; |
| 83 |
int h3 = h[3]; |
| 84 |
int h4 = h[4]; |
| 85 |
int h5 = h[5]; |
| 86 |
int h6 = h[6]; |
| 87 |
int h7 = h[7]; |
| 88 |
int h8 = h[8]; |
| 89 |
int h9 = h[9]; |
| 90 |
int q; |
| 91 |
int carry0; |
| 92 |
int carry1; |
| 93 |
int carry2; |
| 94 |
int carry3; |
| 95 |
int carry4; |
| 96 |
int carry5; |
| 97 |
int carry6; |
| 98 |
int carry7; |
| 99 |
int carry8; |
| 100 |
int carry9; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
q = (19 * h9 + (1 << 24)) >> 25; |
| 105 |
q = (h0 + q) >> 26; |
| 106 |
q = (h1 + q) >> 25; |
| 107 |
q = (h2 + q) >> 26; |
| 108 |
q = (h3 + q) >> 25; |
| 109 |
q = (h4 + q) >> 26; |
| 110 |
q = (h5 + q) >> 25; |
| 111 |
q = (h6 + q) >> 26; |
| 112 |
q = (h7 + q) >> 25; |
| 113 |
q = (h8 + q) >> 26; |
| 114 |
q = (h9 + q) >> 25; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
h0 += 19 * q; |
| 119 |
|
| 120 |
carry0 = h0 >> 26; h1 += carry0; h0 -= carry0 << 26; |
| 121 |
carry1 = h1 >> 25; h2 += carry1; h1 -= carry1 << 25; |
| 122 |
carry2 = h2 >> 26; h3 += carry2; h2 -= carry2 << 26; |
| 123 |
carry3 = h3 >> 25; h4 += carry3; h3 -= carry3 << 25; |
| 124 |
carry4 = h4 >> 26; h5 += carry4; h4 -= carry4 << 26; |
| 125 |
carry5 = h5 >> 25; h6 += carry5; h5 -= carry5 << 25; |
| 126 |
carry6 = h6 >> 26; h7 += carry6; h6 -= carry6 << 26; |
| 127 |
carry7 = h7 >> 25; h8 += carry7; h7 -= carry7 << 25; |
| 128 |
carry8 = h8 >> 26; h9 += carry8; h8 -= carry8 << 26; |
| 129 |
carry9 = h9 >> 25; h9 -= carry9 << 25; |
| 130 |
|
| 131 |
|
| 132 |
byte[] s = new byte[32]; |
| 133 |
s[0] = (byte) h0; |
| 134 |
s[1] = (byte) (h0 >> 8); |
| 135 |
s[2] = (byte) (h0 >> 16); |
| 136 |
s[3] = (byte) ((h0 >> 24) | (h1 << 2)); |
| 137 |
s[4] = (byte) (h1 >> 6); |
| 138 |
s[5] = (byte) (h1 >> 14); |
| 139 |
s[6] = (byte) ((h1 >> 22) | (h2 << 3)); |
| 140 |
s[7] = (byte) (h2 >> 5); |
| 141 |
s[8] = (byte) (h2 >> 13); |
| 142 |
s[9] = (byte) ((h2 >> 21) | (h3 << 5)); |
| 143 |
s[10] = (byte) (h3 >> 3); |
| 144 |
s[11] = (byte) (h3 >> 11); |
| 145 |
s[12] = (byte) ((h3 >> 19) | (h4 << 6)); |
| 146 |
s[13] = (byte) (h4 >> 2); |
| 147 |
s[14] = (byte) (h4 >> 10); |
| 148 |
s[15] = (byte) (h4 >> 18); |
| 149 |
s[16] = (byte) h5; |
| 150 |
s[17] = (byte) (h5 >> 8); |
| 151 |
s[18] = (byte) (h5 >> 16); |
| 152 |
s[19] = (byte) ((h5 >> 24) | (h6 << 1)); |
| 153 |
s[20] = (byte) (h6 >> 7); |
| 154 |
s[21] = (byte) (h6 >> 15); |
| 155 |
s[22] = (byte) ((h6 >> 23) | (h7 << 3)); |
| 156 |
s[23] = (byte) (h7 >> 5); |
| 157 |
s[24] = (byte) (h7 >> 13); |
| 158 |
s[25] = (byte) ((h7 >> 21) | (h8 << 4)); |
| 159 |
s[26] = (byte) (h8 >> 4); |
| 160 |
s[27] = (byte) (h8 >> 12); |
| 161 |
s[28] = (byte) ((h8 >> 20) | (h9 << 6)); |
| 162 |
s[29] = (byte) (h9 >> 2); |
| 163 |
s[30] = (byte) (h9 >> 10); |
| 164 |
s[31] = (byte) (h9 >> 18); |
| 165 |
return s; |
| 166 |
} |
| 167 |
|
| 168 |
static int load_3(byte[] in, int offset) { |
| 169 |
int result = in[offset++] & 0xff; |
| 170 |
result |= (in[offset++] & 0xff) << 8; |
| 171 |
result |= (in[offset] & 0xff) << 16; |
| 172 |
return result; |
| 173 |
} |
| 174 |
|
| 175 |
static long load_4(byte[] in, int offset) { |
| 176 |
int result = in[offset++] & 0xff; |
| 177 |
result |= (in[offset++] & 0xff) << 8; |
| 178 |
result |= (in[offset++] & 0xff) << 16; |
| 179 |
result |= in[offset] << 24; |
| 180 |
return ((long)result) & 0xffffffffL; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public FieldElement decode(byte[] in) { |
| 190 |
long h0 = load_4(in, 0); |
| 191 |
long h1 = load_3(in, 4) << 6; |
| 192 |
long h2 = load_3(in, 7) << 5; |
| 193 |
long h3 = load_3(in, 10) << 3; |
| 194 |
long h4 = load_3(in, 13) << 2; |
| 195 |
long h5 = load_4(in, 16); |
| 196 |
long h6 = load_3(in, 20) << 7; |
| 197 |
long h7 = load_3(in, 23) << 5; |
| 198 |
long h8 = load_3(in, 26) << 4; |
| 199 |
long h9 = (load_3(in, 29) & 0x7FFFFF) << 2; |
| 200 |
long carry0; |
| 201 |
long carry1; |
| 202 |
long carry2; |
| 203 |
long carry3; |
| 204 |
long carry4; |
| 205 |
long carry5; |
| 206 |
long carry6; |
| 207 |
long carry7; |
| 208 |
long carry8; |
| 209 |
long carry9; |
| 210 |
|
| 211 |
|
| 212 |
carry9 = (h9 + (long) (1<<24)) >> 25; h0 += carry9 * 19; h9 -= carry9 << 25; |
| 213 |
carry1 = (h1 + (long) (1<<24)) >> 25; h2 += carry1; h1 -= carry1 << 25; |
| 214 |
carry3 = (h3 + (long) (1<<24)) >> 25; h4 += carry3; h3 -= carry3 << 25; |
| 215 |
carry5 = (h5 + (long) (1<<24)) >> 25; h6 += carry5; h5 -= carry5 << 25; |
| 216 |
carry7 = (h7 + (long) (1<<24)) >> 25; h8 += carry7; h7 -= carry7 << 25; |
| 217 |
|
| 218 |
carry0 = (h0 + (long) (1<<25)) >> 26; h1 += carry0; h0 -= carry0 << 26; |
| 219 |
carry2 = (h2 + (long) (1<<25)) >> 26; h3 += carry2; h2 -= carry2 << 26; |
| 220 |
carry4 = (h4 + (long) (1<<25)) >> 26; h5 += carry4; h4 -= carry4 << 26; |
| 221 |
carry6 = (h6 + (long) (1<<25)) >> 26; h7 += carry6; h6 -= carry6 << 26; |
| 222 |
carry8 = (h8 + (long) (1<<25)) >> 26; h9 += carry8; h8 -= carry8 << 26; |
| 223 |
|
| 224 |
int[] h = new int[10]; |
| 225 |
h[0] = (int) h0; |
| 226 |
h[1] = (int) h1; |
| 227 |
h[2] = (int) h2; |
| 228 |
h[3] = (int) h3; |
| 229 |
h[4] = (int) h4; |
| 230 |
h[5] = (int) h5; |
| 231 |
h[6] = (int) h6; |
| 232 |
h[7] = (int) h7; |
| 233 |
h[8] = (int) h8; |
| 234 |
h[9] = (int) h9; |
| 235 |
return new Ed25519FieldElement(f, h); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
public boolean isNegative(FieldElement x) { |
| 252 |
byte[] s = encode(x); |
| 253 |
return (s[0] & 1) != 0; |
| 254 |
} |
| 255 |
|
| 256 |
} |