Skip to content

Commit 49110f4

Browse files
committed
move sqrt algs to an appendix
1 parent 8195ccf commit 49110f4

File tree

1 file changed

+155
-155
lines changed

1 file changed

+155
-155
lines changed

draft-irtf-cfrg-hash-to-curve.md

+155-155
Original file line numberDiff line numberDiff line change
@@ -1242,7 +1242,7 @@ is_square(x) := { True, if x^((q - 1) / 2) is 0 or 1 in F;
12421242
of the result.
12431243

12441244
The preferred way of computing square roots is to fix a deterministic
1245-
algorithm particular to F. We give several algorithms in {{sqrt-variants}}.
1245+
algorithm particular to F. We give several algorithms in {{appx-sqrt}}.
12461246
Regardless of the method chosen, the sqrt function should be implemented
12471247
in a way that resists timing side channels, i.e., in constant time.
12481248

@@ -1354,160 +1354,6 @@ Steps:
13541354
6. return CMOV(sign, 1, sign == 0) // regard x == 0 as positive
13551355
~~~
13561356

1357-
## sqrt variants {#sqrt-variants}
1358-
1359-
This section defines special-purpose sqrt functions for the three most common cases,
1360-
p = 3 mod 4, p = 5 mod 8, and p = 9 mod 16.
1361-
In addition, it gives a generic constant-time algorithm that works for any prime modulus.
1362-
1363-
### p = 3 mod 4 {#sqrt-3mod4}
1364-
1365-
~~~
1366-
sqrt_3mod4(x)
1367-
1368-
Parameters:
1369-
- F, a finite field of characteristic p and order q = p^m.
1370-
- p, the characteristic of F (see immediately above).
1371-
- m, the extension degree of F, m >= 1 (see immediately above).
1372-
1373-
Input: x, an element of F.
1374-
Output: s, an element of F such that (s^2) == x.
1375-
1376-
Constants:
1377-
1. c1 = (q + 1) / 4 // Integer arithmetic
1378-
1379-
Procedure:
1380-
1. return x^c1
1381-
~~~
1382-
1383-
### p = 5 mod 8 {#sqrt-5mod8}
1384-
1385-
~~~
1386-
sqrt_5mod8(x)
1387-
1388-
Parameters:
1389-
- F, a finite field of characteristic p and order q = p^m.
1390-
- p, the characteristic of F (see immediately above).
1391-
- m, the extension degree of F, m >= 1 (see immediately above).
1392-
1393-
Input: x, an element of F.
1394-
Output: s, an element of F such that (s^2) == x.
1395-
1396-
Constants:
1397-
1. c1 = sqrt(-1) in F, i.e., (c1^2) == -1 in F
1398-
2. c2 = (q + 3) / 8 // Integer arithmetic
1399-
1400-
Procedure:
1401-
1. t1 = x^c2
1402-
2. e = (t1^2) == x
1403-
3. s = CMOV(t1 * c1, t1, e)
1404-
3. return s
1405-
~~~
1406-
1407-
### p = 9 mod 16 {#sqrt-9mod16}
1408-
1409-
Note that this case also applies to GF(p^2) when p = 3 mod 8.
1410-
{{AR13}} and {{S85}} describe methods that work for other field extensions.
1411-
1412-
~~~
1413-
sqrt_9mod16(x)
1414-
1415-
Parameters:
1416-
- F, a finite field of characteristic p and order q = p^m.
1417-
- p, the characteristic of F (see immediately above).
1418-
- m, the extension degree of F, m >= 1 (see immediately above).
1419-
1420-
Input: x, an element of F.
1421-
Output: s, an element of F such that (s^2) == x.
1422-
1423-
Constants:
1424-
1. c1 = sqrt(-1) in F, i.e., (c1^2) == -1 in F
1425-
2. c2 = sqrt(c1) in F, i.e., (c2^2) == c1 in F
1426-
3. c3 = sqrt(-c1) in F, i.e., (c3^2) == -c1 in F
1427-
4. c4 = (q + 7) / 16 // Integer arithmetic
1428-
1429-
Procedure:
1430-
1. t1 = x^c4
1431-
2. t2 = c1 * t1
1432-
3. t3 = c2 * t1
1433-
4. t4 = c3 * t1
1434-
5. e1 = (t2^2) == x
1435-
6. e2 = (t3^2) == x
1436-
7. t1 = CMOV(t1, t2, e1) // select t2 if (t2^2) == x
1437-
8. t2 = CMOV(t4, t3, e2) // select t3 if (t3^2) == x
1438-
9. e3 = (t2^2) == x
1439-
10. s = CMOV(t1, t2, e3) // select the sqrt from t1 and t2
1440-
11. return s
1441-
~~~
1442-
1443-
### Constant-time Tonelli-Shanks algorithm {#sqrt-ts}
1444-
1445-
This algorithm is a constant-time version of the classic Tonelli-Shanks algorithm
1446-
({{C93}}, Algorithm 1.5.1) due to Sean Bowe, Jack Grigg, and Eirik Ogilvie-Wigley,
1447-
adapted and optimized by Michael Scott.
1448-
1449-
This algorithm applies to GF(p) for any p.
1450-
Note, however, that the special-purpose algorithms given in the prior sections are
1451-
faster, when they apply.
1452-
1453-
~~~
1454-
sqrt_ts_ct(x)
1455-
1456-
Parameters:
1457-
- F, a finite field of order p
1458-
- p, the characteristic of F (see immediately above)
1459-
1460-
Input x, an element of F.
1461-
Output: r, an element of F such that (r^2) == 2.
1462-
1463-
Constants (see discussion below):
1464-
1. c1, the largest integer such that 2^c1 divides p - 1.
1465-
2. c2 = (p - 1) / (2^c1) // integer arithmetic
1466-
3. c3 = (c2 - 1) / 2 // integer arithmetic
1467-
4. c4, a non-square value in F
1468-
5. c5 = c4^c2 in F
1469-
1470-
Procedure:
1471-
1. r = x^c3
1472-
2. t = r * r * x
1473-
3. r = r * x
1474-
4. b = t
1475-
5. c = c5
1476-
6. for k in (m, m - 1, ..., 2):
1477-
7. for j in (1, 2, ..., k - 1):
1478-
8. b = b * b
1479-
9. b_is_good = b != 1
1480-
10. tmp = r * c
1481-
11. r = CMOV(r, tmp, e)
1482-
12. c = c * c
1483-
13. tmp = t * c
1484-
14. t = CMOV(t, tmp, e)
1485-
15. b = t
1486-
16. return r
1487-
~~~
1488-
1489-
The constants used in this procedure can be computed as follows:
1490-
1491-
~~~
1492-
precompute_ts(p)
1493-
1494-
Input: p, a prime
1495-
Output: the required constants c1, ..., c5
1496-
1497-
Procedure:
1498-
1. c1 = 0
1499-
2. c2 = p - 1
1500-
3. while c2 is even:
1501-
4. c2 = c2 / 2 // integer arithmetic
1502-
5. c1 = c1 + 1
1503-
6. c3 = (c2 - 1) / 2 // integer arithmetic
1504-
7. c4 = 1
1505-
8. while c4 is square mod p:
1506-
9. c4 = c4 + 1
1507-
10. c5 = c4^c2 mod p
1508-
11. return (c1, c2, c3, c4, c5)
1509-
~~~
1510-
15111357
# Hashing to a Finite Field {#hashtobase}
15121358

15131359
The hash\_to\_base function hashes a string msg of any length into an element of a
@@ -3359,3 +3205,157 @@ def find_z_ell2(F):
33593205
return Z_cand
33603206
ctr += 1
33613207
~~~
3208+
3209+
# sqrt functions {#appx-sqrt}
3210+
3211+
This section defines special-purpose sqrt functions for the three most common cases,
3212+
p = 3 mod 4, p = 5 mod 8, and p = 9 mod 16.
3213+
In addition, it gives a generic constant-time algorithm that works for any prime modulus.
3214+
3215+
## p = 3 mod 4 {#sqrt-3mod4}
3216+
3217+
~~~
3218+
sqrt_3mod4(x)
3219+
3220+
Parameters:
3221+
- F, a finite field of characteristic p and order q = p^m.
3222+
- p, the characteristic of F (see immediately above).
3223+
- m, the extension degree of F, m >= 1 (see immediately above).
3224+
3225+
Input: x, an element of F.
3226+
Output: s, an element of F such that (s^2) == x.
3227+
3228+
Constants:
3229+
1. c1 = (q + 1) / 4 // Integer arithmetic
3230+
3231+
Procedure:
3232+
1. return x^c1
3233+
~~~
3234+
3235+
## p = 5 mod 8 {#sqrt-5mod8}
3236+
3237+
~~~
3238+
sqrt_5mod8(x)
3239+
3240+
Parameters:
3241+
- F, a finite field of characteristic p and order q = p^m.
3242+
- p, the characteristic of F (see immediately above).
3243+
- m, the extension degree of F, m >= 1 (see immediately above).
3244+
3245+
Input: x, an element of F.
3246+
Output: s, an element of F such that (s^2) == x.
3247+
3248+
Constants:
3249+
1. c1 = sqrt(-1) in F, i.e., (c1^2) == -1 in F
3250+
2. c2 = (q + 3) / 8 // Integer arithmetic
3251+
3252+
Procedure:
3253+
1. t1 = x^c2
3254+
2. e = (t1^2) == x
3255+
3. s = CMOV(t1 * c1, t1, e)
3256+
3. return s
3257+
~~~
3258+
3259+
## p = 9 mod 16 {#sqrt-9mod16}
3260+
3261+
Note that this case also applies to GF(p^2) when p = 3 mod 8.
3262+
{{AR13}} and {{S85}} describe methods that work for other field extensions.
3263+
3264+
~~~
3265+
sqrt_9mod16(x)
3266+
3267+
Parameters:
3268+
- F, a finite field of characteristic p and order q = p^m.
3269+
- p, the characteristic of F (see immediately above).
3270+
- m, the extension degree of F, m >= 1 (see immediately above).
3271+
3272+
Input: x, an element of F.
3273+
Output: s, an element of F such that (s^2) == x.
3274+
3275+
Constants:
3276+
1. c1 = sqrt(-1) in F, i.e., (c1^2) == -1 in F
3277+
2. c2 = sqrt(c1) in F, i.e., (c2^2) == c1 in F
3278+
3. c3 = sqrt(-c1) in F, i.e., (c3^2) == -c1 in F
3279+
4. c4 = (q + 7) / 16 // Integer arithmetic
3280+
3281+
Procedure:
3282+
1. t1 = x^c4
3283+
2. t2 = c1 * t1
3284+
3. t3 = c2 * t1
3285+
4. t4 = c3 * t1
3286+
5. e1 = (t2^2) == x
3287+
6. e2 = (t3^2) == x
3288+
7. t1 = CMOV(t1, t2, e1) // select t2 if (t2^2) == x
3289+
8. t2 = CMOV(t4, t3, e2) // select t3 if (t3^2) == x
3290+
9. e3 = (t2^2) == x
3291+
10. s = CMOV(t1, t2, e3) // select the sqrt from t1 and t2
3292+
11. return s
3293+
~~~
3294+
3295+
## Constant-time Tonelli-Shanks algorithm {#sqrt-ts}
3296+
3297+
This algorithm is a constant-time version of the classic Tonelli-Shanks algorithm
3298+
({{C93}}, Algorithm 1.5.1) due to Sean Bowe, Jack Grigg, and Eirik Ogilvie-Wigley,
3299+
adapted and optimized by Michael Scott.
3300+
3301+
This algorithm applies to GF(p) for any p.
3302+
Note, however, that the special-purpose algorithms given in the prior sections are
3303+
faster, when they apply.
3304+
3305+
~~~
3306+
sqrt_ts_ct(x)
3307+
3308+
Parameters:
3309+
- F, a finite field of order p
3310+
- p, the characteristic of F (see immediately above)
3311+
3312+
Input x, an element of F.
3313+
Output: r, an element of F such that (r^2) == 2.
3314+
3315+
Constants (see discussion below):
3316+
1. c1, the largest integer such that 2^c1 divides p - 1.
3317+
2. c2 = (p - 1) / (2^c1) // integer arithmetic
3318+
3. c3 = (c2 - 1) / 2 // integer arithmetic
3319+
4. c4, a non-square value in F
3320+
5. c5 = c4^c2 in F
3321+
3322+
Procedure:
3323+
1. r = x^c3
3324+
2. t = r * r * x
3325+
3. r = r * x
3326+
4. b = t
3327+
5. c = c5
3328+
6. for k in (m, m - 1, ..., 2):
3329+
7. for j in (1, 2, ..., k - 1):
3330+
8. b = b * b
3331+
9. b_is_good = b != 1
3332+
10. tmp = r * c
3333+
11. r = CMOV(r, tmp, e)
3334+
12. c = c * c
3335+
13. tmp = t * c
3336+
14. t = CMOV(t, tmp, e)
3337+
15. b = t
3338+
16. return r
3339+
~~~
3340+
3341+
The constants used in this procedure can be computed as follows:
3342+
3343+
~~~
3344+
precompute_ts(p)
3345+
3346+
Input: p, a prime
3347+
Output: the required constants c1, ..., c5
3348+
3349+
Procedure:
3350+
1. c1 = 0
3351+
2. c2 = p - 1
3352+
3. while c2 is even:
3353+
4. c2 = c2 / 2 // integer arithmetic
3354+
5. c1 = c1 + 1
3355+
6. c3 = (c2 - 1) / 2 // integer arithmetic
3356+
7. c4 = 1
3357+
8. while c4 is square mod p:
3358+
9. c4 = c4 + 1
3359+
10. c5 = c4^c2 mod p
3360+
11. return (c1, c2, c3, c4, c5)
3361+
~~~

0 commit comments

Comments
 (0)