Debian Codegraph @

ext/ed25519_jruby/net/i2p/crypto/eddsa/math/ed25519/Ed25519LittleEndianEncoding.java

raw symbols SCIP
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.ed25519;
13
14 import net.i2p.crypto.eddsa.math.*;
15
16 /**
17 * Helper class for encoding/decoding from/to the 32 byte representation.
18 * <p>
19 * Reviewed/commented by Bloody Rookie ([email protected])
20 */
21 public class Ed25519LittleEndianEncoding extends Encoding {
22 /**
23 * Encodes a given field element in its 32 byte representation. This is done in two steps:
24 * <ol>
25 * <li>Reduce the value of the field element modulo $p$.
26 * <li>Convert the field element to the 32 byte representation.
27 * </ol><p>
28 * The idea for the modulo $p$ reduction algorithm is as follows:
29 * </p>
30 * <h2>Assumption:</h2>
31 * <ul>
32 * <li>$p = 2^{255} - 19$
33 * <li>$h = h_0 + 2^{25} * h_1 + 2^{(26+25)} * h_2 + \dots + 2^{230} * h_9$ where $0 \le |h_i| \lt 2^{27}$ for all $i=0,\dots,9$.
34 * <li>$h \cong r \mod p$, i.e. $h = r + q * p$ for some suitable $0 \le r \lt p$ and an integer $q$.
35 * </ul><p>
36 * Then $q = [2^{-255} * (h + 19 * 2^{-25} * h_9 + 1/2)]$ where $[x] = floor(x)$.
37 * </p>
38 * <h2>Proof:</h2>
39 * <p>
40 * We begin with some very raw estimation for the bounds of some expressions:
41 * <p>
42 * $$
43 * \begin{equation}
44 * |h| \lt 2^{230} * 2^{30} = 2^{260} \Rightarrow |r + q * p| \lt 2^{260} \Rightarrow |q| \lt 2^{10}. \\
45 * \Rightarrow -1/4 \le a := 19^2 * 2^{-255} * q \lt 1/4. \\
46 * |h - 2^{230} * h_9| = |h_0 + \dots + 2^{204} * h_8| \lt 2^{204} * 2^{30} = 2^{234}. \\
47 * \Rightarrow -1/4 \le b := 19 * 2^{-255} * (h - 2^{230} * h_9) \lt 1/4
48 * \end{equation}
49 * $$
50 * <p>
51 * Therefore $0 \lt 1/2 - a - b \lt 1$.
52 * <p>
53 * Set $x := r + 19 * 2^{-255} * r + 1/2 - a - b$. Then:
54 * <p>
55 * $$
56 * 0 \le x \lt 255 - 20 + 19 + 1 = 2^{255} \\
57 * \Rightarrow 0 \le 2^{-255} * x \lt 1.
58 * $$
59 * <p>
60 * Since $q$ is an integer we have
61 * <p>
62 * $$
63 * [q + 2^{-255} * x] = q \quad (1)
64 * $$
65 * <p>
66 * Have a closer look at $x$:
67 * <p>
68 * $$
69 * \begin{align}
70 * x &amp;= h - q * (2^{255} - 19) + 19 * 2^{-255} * (h - q * (2^{255} - 19)) + 1/2 - 19^2 * 2^{-255} * q - 19 * 2^{-255} * (h - 2^{230} * h_9) \\
71 * &amp;= h - q * 2^{255} + 19 * q + 19 * 2^{-255} * h - 19 * q + 19^2 * 2^{-255} * q + 1/2 - 19^2 * 2^{-255} * q - 19 * 2^{-255} * h + 19 * 2^{-25} * h_9 \\
72 * &amp;= h + 19 * 2^{-25} * h_9 + 1/2 - q^{255}.
73 * \end{align}
74 * $$
75 * <p>
76 * Inserting the expression for $x$ into $(1)$ we get the desired expression for $q$.
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 // Step 1:
103 // Calculate q
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 // r = h - q * p = h - 2^255 * q + 19 * q
117 // First add 19 * q then discard the bit 255
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 // Step 2 (straight forward conversion):
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 * Decodes a given field element in its 10 byte $2^{25.5}$ representation.
185 *
186 * @param in The 32 byte representation.
187 * @return The field element in its $2^{25.5}$ bit representation.
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 // Remember: 2^255 congruent 19 modulo p
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 * Is the FieldElement negative in this encoding?
240 * <p>
241 * Return true if $x$ is in $\{1,3,5,\dots,q-2\}$<br>
242 * Return false if $x$ is in $\{0,2,4,\dots,q-1\}$
243 * <p>
244 * Preconditions:
245 * </p><ul>
246 * <li>$|x|$ bounded by $1.1*2^{26},1.1*2^{25},1.1*2^{26},1.1*2^{25}$, etc.
247 * </ul>
248 *
249 * @return true if $x$ is in $\{1,3,5,\dots,q-2\}$, false otherwise.
250 */
251 public boolean isNegative(FieldElement x) {
252 byte[] s = encode(x);
253 return (s[0] & 1) != 0;
254 }
255
256 }