Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
// t4 -= c * 2^26 -- |t4| <= 2^25
// t5 += c -- |t5| <= 2^63
//
// c = t1 + 2^24 / 2^25 -- |c| <= 2^38
// t1 -= c * 2^25 -- |t1| <= 2^24
// t2 += c -- |t2| <= 2^63
//
// c = t5 + 2^24 / 2^25 -- |c| <= 2^38
// t5 -= c * 2^25 -- |t5| <= 2^24
// t6 += c -- |t6| <= 2^63
//
// c = t2 + 2^25 / 2^26 -- |c| <= 2^37
// t2 -= c * 2^26 -- |t2| <= 2^25 < 1.1 * 2^25 (final t2)
// t3 += c -- |t3| <= 2^63
//
// c = t6 + 2^25 / 2^26 -- |c| <= 2^37
// t6 -= c * 2^26 -- |t6| <= 2^25 < 1.1 * 2^25 (final t6)
// t7 += c -- |t7| <= 2^63
//
// c = t3 + 2^24 / 2^25 -- |c| <= 2^38
// t3 -= c * 2^25 -- |t3| <= 2^24 < 1.1 * 2^24 (final t3)
// t4 += c -- |t4| <= 2^25 + 2^38 < 2^39
//
// c = t7 + 2^24 / 2^25 -- |c| <= 2^38
// t7 -= c * 2^25 -- |t7| <= 2^24 < 1.1 * 2^24 (final t7)
// t8 += c -- |t8| <= 2^63
//
// c = t4 + 2^25 / 2^26 -- |c| <= 2^13
// t4 -= c * 2^26 -- |t4| <= 2^25 < 1.1 * 2^25 (final t4)
// t5 += c -- |t5| <= 2^24 + 2^13 < 1.1 * 2^24 (final t5)
//
// c = t8 + 2^25 / 2^26 -- |c| <= 2^37
// t8 -= c * 2^26 -- |t8| <= 2^25 < 1.1 * 2^25 (final t8)
// t9 += c -- |t9| <= 2^63
//
// c = t9 + 2^24 / 2^25 -- |c| <= 2^38
// t9 -= c * 2^25 -- |t9| <= 2^24 < 1.1 * 2^24 (final t9)
// t0 += c * 19 -- |t0| <= 2^25 + 2^38*19 < 2^44
//
// c = t0 + 2^25 / 2^26 -- |c| <= 2^18
// t0 -= c * 2^26 -- |t0| <= 2^25 < 1.1 * 2^25 (final t0)
// t1 += c -- |t1| <= 2^24 + 2^18 < 1.1 * 2^24 (final t1)
//
// Postcondition
// -------------
// |t0|, |t2|, |t4|, |t6|, |t8| < 1.1 * 2^25
// |t1|, |t3|, |t5|, |t7|, |t9| < 1.1 * 2^24
#define FE_CARRY \
i64 c; \
c = (t0 + ((i64)1<<25)) >> 26; t0 -= c * ((i64)1 << 26); t1 += c; \
c = (t4 + ((i64)1<<25)) >> 26; t4 -= c * ((i64)1 << 26); t5 += c; \
c = (t1 + ((i64)1<<24)) >> 25; t1 -= c * ((i64)1 << 25); t2 += c; \
c = (t5 + ((i64)1<<24)) >> 25; t5 -= c * ((i64)1 << 25); t6 += c; \
c = (t2 + ((i64)1<<25)) >> 26; t2 -= c * ((i64)1 << 26); t3 += c; \
c = (t6 + ((i64)1<<25)) >> 26; t6 -= c * ((i64)1 << 26); t7 += c; \
c = (t3 + ((i64)1<<24)) >> 25; t3 -= c * ((i64)1 << 25); t4 += c; \
c = (t7 + ((i64)1<<24)) >> 25; t7 -= c * ((i64)1 << 25); t8 += c; \
c = (t4 + ((i64)1<<25)) >> 26; t4 -= c * ((i64)1 << 26); t5 += c; \
c = (t8 + ((i64)1<<25)) >> 26; t8 -= c * ((i64)1 << 26); t9 += c; \
c = (t9 + ((i64)1<<24)) >> 25; t9 -= c * ((i64)1 << 25); t0 += c * 19; \
c = (t0 + ((i64)1<<25)) >> 26; t0 -= c * ((i64)1 << 26); t1 += c; \
h[0]=(i32)t0; h[1]=(i32)t1; h[2]=(i32)t2; h[3]=(i32)t3; h[4]=(i32)t4; \
h[5]=(i32)t5; h[6]=(i32)t6; h[7]=(i32)t7; h[8]=(i32)t8; h[9]=(i32)t9
static void fe_frombytes(fe h, const u8 s[32])
{
i64 t0 = load32_le(s); // t0 < 2^32
i64 t1 = load24_le(s + 4) << 6; // t1 < 2^30
i64 t2 = load24_le(s + 7) << 5; // t2 < 2^29
i64 t3 = load24_le(s + 10) << 3; // t3 < 2^27
i64 t4 = load24_le(s + 13) << 2; // t4 < 2^26
i64 t5 = load32_le(s + 16); // t5 < 2^32
i64 t6 = load24_le(s + 20) << 7; // t6 < 2^31
i64 t7 = load24_le(s + 23) << 5; // t7 < 2^29
i64 t8 = load24_le(s + 26) << 4; // t8 < 2^28
i64 t9 = (load24_le(s + 29) & 0x7fffff) << 2; // t9 < 2^25
FE_CARRY; // Carry recondition OK
}
// Precondition
// |h[0]|, |h[2]|, |h[4]|, |h[6]|, |h[8]| < 1.1 * 2^25
// |h[1]|, |h[3]|, |h[5]|, |h[7]|, |h[9]| < 1.1 * 2^24
//
// Therefore, |h| < 2^255-19
// There are two possibilities:
//
// - If h is positive, all we need to do is reduce its individual
// limbs down to their tight positive range.
// - If h is negative, we also need to add 2^255-19 to it.
// Or just remove 19 and chop off any excess bit.
static void fe_tobytes(u8 s[32], const fe h)
{
i32 t[10];
COPY(t, h, 10);
i32 q = (19 * t[9] + (((i32) 1) << 24)) >> 25;
// |t9| < 1.1 * 2^24
// -1.1 * 2^24 < t9 < 1.1 * 2^24
// -21 * 2^24 < 19 * t9 < 21 * 2^24
// -2^29 < 19 * t9 + 2^24 < 2^29
// -2^29 / 2^25 < (19 * t9 + 2^24) / 2^25 < 2^29 / 2^25
// -16 < (19 * t9 + 2^24) / 2^25 < 16
FOR (i, 0, 5) {
q += t[2*i ]; q >>= 26; // q = 0 or -1
q += t[2*i+1]; q >>= 25; // q = 0 or -1
}
// q = 0 iff h >= 0
// q = -1 iff h < 0
// Adding q * 19 to h reduces h to its proper range.
q *= 19; // Shift carry back to the beginning
FOR (i, 0, 5) {
t[i*2 ] += q; q = t[i*2 ] >> 26; t[i*2 ] -= q * ((i32)1 << 26);
t[i*2+1] += q; q = t[i*2+1] >> 25; t[i*2+1] -= q * ((i32)1 << 25);
}
// h is now fully reduced, and q represents the excess bit.
store32_le(s + 0, ((u32)t[0] >> 0) | ((u32)t[1] << 26));
store32_le(s + 4, ((u32)t[1] >> 6) | ((u32)t[2] << 19));
store32_le(s + 8, ((u32)t[2] >> 13) | ((u32)t[3] << 13));
store32_le(s + 12, ((u32)t[3] >> 19) | ((u32)t[4] << 6));
store32_le(s + 16, ((u32)t[5] >> 0) | ((u32)t[6] << 25));
store32_le(s + 20, ((u32)t[6] >> 7) | ((u32)t[7] << 19));
store32_le(s + 24, ((u32)t[7] >> 13) | ((u32)t[8] << 12));
store32_le(s + 28, ((u32)t[8] >> 20) | ((u32)t[9] << 6));
WIPE_BUFFER(t);
}
// Precondition
// -------------
// |f0|, |f2|, |f4|, |f6|, |f8| < 1.65 * 2^26
// |f1|, |f3|, |f5|, |f7|, |f9| < 1.65 * 2^25
//
// |g0|, |g2|, |g4|, |g6|, |g8| < 1.65 * 2^26
// |g1|, |g3|, |g5|, |g7|, |g9| < 1.65 * 2^25
static void fe_mul_small(fe h, const fe f, i32 g)
{
i64 t0 = f[0] * (i64) g; i64 t1 = f[1] * (i64) g;
i64 t2 = f[2] * (i64) g; i64 t3 = f[3] * (i64) g;
i64 t4 = f[4] * (i64) g; i64 t5 = f[5] * (i64) g;
i64 t6 = f[6] * (i64) g; i64 t7 = f[7] * (i64) g;
i64 t8 = f[8] * (i64) g; i64 t9 = f[9] * (i64) g;
// |t0|, |t2|, |t4|, |t6|, |t8| < 1.65 * 2^26 * 2^31 < 2^58
// |t1|, |t3|, |t5|, |t7|, |t9| < 1.65 * 2^25 * 2^31 < 2^57
FE_CARRY; // Carry precondition OK
}
// Precondition
// -------------
// |f0|, |f2|, |f4|, |f6|, |f8| < 1.65 * 2^26
// |f1|, |f3|, |f5|, |f7|, |f9| < 1.65 * 2^25
//
// |g0|, |g2|, |g4|, |g6|, |g8| < 1.65 * 2^26
// |g1|, |g3|, |g5|, |g7|, |g9| < 1.65 * 2^25
static void fe_mul(fe h, const fe f, const fe g)
{
// Everything is unrolled and put in temporary variables.
// We could roll the loop, but that would make curve25519 twice as slow.
i32 f0 = f[0]; i32 f1 = f[1]; i32 f2 = f[2]; i32 f3 = f[3]; i32 f4 = f[4];
i32 f5 = f[5]; i32 f6 = f[6]; i32 f7 = f[7]; i32 f8 = f[8]; i32 f9 = f[9];
i32 g0 = g[0]; i32 g1 = g[1]; i32 g2 = g[2]; i32 g3 = g[3]; i32 g4 = g[4];
i32 g5 = g[5]; i32 g6 = g[6]; i32 g7 = g[7]; i32 g8 = g[8]; i32 g9 = g[9];
i32 F1 = f1*2; i32 F3 = f3*2; i32 F5 = f5*2; i32 F7 = f7*2; i32 F9 = f9*2;
i32 G1 = g1*19; i32 G2 = g2*19; i32 G3 = g3*19;
i32 G4 = g4*19; i32 G5 = g5*19; i32 G6 = g6*19;
i32 G7 = g7*19; i32 G8 = g8*19; i32 G9 = g9*19;
// |F1|, |F3|, |F5|, |F7|, |F9| < 1.65 * 2^26
// |G0|, |G2|, |G4|, |G6|, |G8| < 2^31
// |G1|, |G3|, |G5|, |G7|, |G9| < 2^30
i64 t0 = f0*(i64)g0 + F1*(i64)G9 + f2*(i64)G8 + F3*(i64)G7 + f4*(i64)G6
+ F5*(i64)G5 + f6*(i64)G4 + F7*(i64)G3 + f8*(i64)G2 + F9*(i64)G1;
i64 t1 = f0*(i64)g1 + f1*(i64)g0 + f2*(i64)G9 + f3*(i64)G8 + f4*(i64)G7
+ f5*(i64)G6 + f6*(i64)G5 + f7*(i64)G4 + f8*(i64)G3 + f9*(i64)G2;
i64 t2 = f0*(i64)g2 + F1*(i64)g1 + f2*(i64)g0 + F3*(i64)G9 + f4*(i64)G8
+ F5*(i64)G7 + f6*(i64)G6 + F7*(i64)G5 + f8*(i64)G4 + F9*(i64)G3;
i64 t3 = f0*(i64)g3 + f1*(i64)g2 + f2*(i64)g1 + f3*(i64)g0 + f4*(i64)G9
+ f5*(i64)G8 + f6*(i64)G7 + f7*(i64)G6 + f8*(i64)G5 + f9*(i64)G4;
i64 t4 = f0*(i64)g4 + F1*(i64)g3 + f2*(i64)g2 + F3*(i64)g1 + f4*(i64)g0
+ F5*(i64)G9 + f6*(i64)G8 + F7*(i64)G7 + f8*(i64)G6 + F9*(i64)G5;
i64 t5 = f0*(i64)g5 + f1*(i64)g4 + f2*(i64)g3 + f3*(i64)g2 + f4*(i64)g1
+ f5*(i64)g0 + f6*(i64)G9 + f7*(i64)G8 + f8*(i64)G7 + f9*(i64)G6;
i64 t6 = f0*(i64)g6 + F1*(i64)g5 + f2*(i64)g4 + F3*(i64)g3 + f4*(i64)g2
+ F5*(i64)g1 + f6*(i64)g0 + F7*(i64)G9 + f8*(i64)G8 + F9*(i64)G7;
i64 t7 = f0*(i64)g7 + f1*(i64)g6 + f2*(i64)g5 + f3*(i64)g4 + f4*(i64)g3
+ f5*(i64)g2 + f6*(i64)g1 + f7*(i64)g0 + f8*(i64)G9 + f9*(i64)G8;
i64 t8 = f0*(i64)g8 + F1*(i64)g7 + f2*(i64)g6 + F3*(i64)g5 + f4*(i64)g4
+ F5*(i64)g3 + f6*(i64)g2 + F7*(i64)g1 + f8*(i64)g0 + F9*(i64)G9;
i64 t9 = f0*(i64)g9 + f1*(i64)g8 + f2*(i64)g7 + f3*(i64)g6 + f4*(i64)g5
+ f5*(i64)g4 + f6*(i64)g3 + f7*(i64)g2 + f8*(i64)g1 + f9*(i64)g0;
// t0 < 0.67 * 2^61
// t1 < 0.41 * 2^61
// t2 < 0.52 * 2^61
// t3 < 0.32 * 2^61
// t4 < 0.38 * 2^61
// t5 < 0.22 * 2^61
// t6 < 0.23 * 2^61
// t7 < 0.13 * 2^61
// t8 < 0.09 * 2^61
// t9 < 0.03 * 2^61
FE_CARRY; // Everything below 2^62, Carry precondition OK
}
// Precondition
// -------------
// |f0|, |f2|, |f4|, |f6|, |f8| < 1.65 * 2^26
// |f1|, |f3|, |f5|, |f7|, |f9| < 1.65 * 2^25
//
// Note: we could use fe_mul() for this, but this is significantly faster
static void fe_sq(fe h, const fe f)
{
i32 f0 = f[0]; i32 f1 = f[1]; i32 f2 = f[2]; i32 f3 = f[3]; i32 f4 = f[4];
i32 f5 = f[5]; i32 f6 = f[6]; i32 f7 = f[7]; i32 f8 = f[8]; i32 f9 = f[9];
i32 f0_2 = f0*2; i32 f1_2 = f1*2; i32 f2_2 = f2*2; i32 f3_2 = f3*2;
i32 f4_2 = f4*2; i32 f5_2 = f5*2; i32 f6_2 = f6*2; i32 f7_2 = f7*2;
i32 f5_38 = f5*38; i32 f6_19 = f6*19; i32 f7_38 = f7*38;
i32 f8_19 = f8*19; i32 f9_38 = f9*38;
// |f0_2| , |f2_2| , |f4_2| , |f6_2| , |f8_2| < 1.65 * 2^27
// |f1_2| , |f3_2| , |f5_2| , |f7_2| , |f9_2| < 1.65 * 2^26
// |f5_38|, |f6_19|, |f7_38|, |f8_19|, |f9_38| < 2^31
i64 t0 = f0 *(i64)f0 + f1_2*(i64)f9_38 + f2_2*(i64)f8_19
+ f3_2*(i64)f7_38 + f4_2*(i64)f6_19 + f5 *(i64)f5_38;
i64 t1 = f0_2*(i64)f1 + f2 *(i64)f9_38 + f3_2*(i64)f8_19
+ f4 *(i64)f7_38 + f5_2*(i64)f6_19;
i64 t2 = f0_2*(i64)f2 + f1_2*(i64)f1 + f3_2*(i64)f9_38
+ f4_2*(i64)f8_19 + f5_2*(i64)f7_38 + f6 *(i64)f6_19;
i64 t3 = f0_2*(i64)f3 + f1_2*(i64)f2 + f4 *(i64)f9_38
+ f5_2*(i64)f8_19 + f6 *(i64)f7_38;
i64 t4 = f0_2*(i64)f4 + f1_2*(i64)f3_2 + f2 *(i64)f2
+ f5_2*(i64)f9_38 + f6_2*(i64)f8_19 + f7 *(i64)f7_38;
i64 t5 = f0_2*(i64)f5 + f1_2*(i64)f4 + f2_2*(i64)f3
+ f6 *(i64)f9_38 + f7_2*(i64)f8_19;
i64 t6 = f0_2*(i64)f6 + f1_2*(i64)f5_2 + f2_2*(i64)f4
+ f3_2*(i64)f3 + f7_2*(i64)f9_38 + f8 *(i64)f8_19;
i64 t7 = f0_2*(i64)f7 + f1_2*(i64)f6 + f2_2*(i64)f5
+ f3_2*(i64)f4 + f8 *(i64)f9_38;
i64 t8 = f0_2*(i64)f8 + f1_2*(i64)f7_2 + f2_2*(i64)f6
+ f3_2*(i64)f5_2 + f4 *(i64)f4 + f9 *(i64)f9_38;
i64 t9 = f0_2*(i64)f9 + f1_2*(i64)f8 + f2_2*(i64)f7
+ f3_2*(i64)f6 + f4 *(i64)f5_2;
// t0 < 0.67 * 2^61
// t1 < 0.41 * 2^61
// t2 < 0.52 * 2^61
// t3 < 0.32 * 2^61
// t4 < 0.38 * 2^61
// t5 < 0.22 * 2^61
// t6 < 0.23 * 2^61
// t7 < 0.13 * 2^61
// t8 < 0.09 * 2^61
// t9 < 0.03 * 2^61
FE_CARRY;
}
// h = 2 * (f^2)
//
// Precondition
// -------------
// |f0|, |f2|, |f4|, |f6|, |f8| < 1.65 * 2^26
// |f1|, |f3|, |f5|, |f7|, |f9| < 1.65 * 2^25
//
// Note: we could implement fe_sq2() by copying fe_sq(), multiplying
// each limb by 2, *then* perform the carry. This saves one carry.
// However, doing so with the stated preconditions does not work (t2
// would overflow). There are 3 ways to solve this:
//
// 1. Show that t2 actually never overflows (it really does not).
// 2. Accept an additional carry, at a small lost of performance.
// 3. Make sure the input of fe_sq2() is freshly carried.
//
// SUPERCOP ref10 relies on (1).
// Monocypher chose (2) and (3), mostly to save code.
static void fe_sq2(fe h, const fe f)
{
fe_sq(h, f);
fe_mul_small(h, h, 2);
}
// This could be simplified, but it would be slower
static void fe_pow22523(fe out, const fe z)
{
fe t0, t1, t2;
fe_sq(t0, z);
fe_sq(t1,t0); fe_sq(t1, t1); fe_mul(t1, z, t1);
fe_mul(t0, t0, t1);
fe_sq(t0, t0); fe_mul(t0, t1, t0);
fe_sq(t1, t0); FOR (i, 1, 5) fe_sq(t1, t1); fe_mul(t0, t1, t0);
fe_sq(t1, t0); FOR (i, 1, 10) fe_sq(t1, t1); fe_mul(t1, t1, t0);
fe_sq(t2, t1); FOR (i, 1, 20) fe_sq(t2, t2); fe_mul(t1, t2, t1);
fe_sq(t1, t1); FOR (i, 1, 10) fe_sq(t1, t1); fe_mul(t0, t1, t0);
fe_sq(t1, t0); FOR (i, 1, 50) fe_sq(t1, t1); fe_mul(t1, t1, t0);
fe_sq(t2, t1); FOR (i, 1, 100) fe_sq(t2, t2); fe_mul(t1, t2, t1);
fe_sq(t1, t1); FOR (i, 1, 50) fe_sq(t1, t1); fe_mul(t0, t1, t0);
fe_sq(t0, t0); FOR (i, 1, 2) fe_sq(t0, t0); fe_mul(out, t0, z);
WIPE_BUFFER(t0);
WIPE_BUFFER(t1);
WIPE_BUFFER(t2);
}
// Inverting means multiplying by 2^255 - 21
// 2^255 - 21 = (2^252 - 3) * 8 + 3
// So we reuse the multiplication chain of fe_pow22523
static void fe_invert(fe out, const fe z)
{
fe tmp;
fe_pow22523(tmp, z);
// tmp2^8 * z^3
fe_sq(tmp, tmp); // 0
fe_sq(tmp, tmp); fe_mul(tmp, tmp, z); // 1
fe_sq(tmp, tmp); fe_mul(out, tmp, z); // 1
WIPE_BUFFER(tmp);
}
// Parity check. Returns 0 if even, 1 if odd
static int fe_isodd(const fe f)
{
u8 s[32];
fe_tobytes(s, f);
u8 isodd = s[0] & 1;
WIPE_BUFFER(s);
return isodd;
}
// Returns 1 if equal, 0 if not equal
static int fe_isequal(const fe f, const fe g)
{
u8 fs[32];
u8 gs[32];
fe_tobytes(fs, f);
fe_tobytes(gs, g);
int isdifferent = crypto_verify32(fs, gs);
WIPE_BUFFER(fs);
WIPE_BUFFER(gs);
return 1 + isdifferent;
}
// Inverse square root.
// Returns true if x is a non zero square, false otherwise.
// After the call:
// isr = sqrt(1/x) if x is non-zero square.
// isr = sqrt(sqrt(-1)/x) if x is not a square.
// isr = 0 if x is zero.
// We do not guarantee the sign of the square root.
//
// Notes:
// Let quartic = x^((p-1)/4)
//
// x^((p-1)/2) = chi(x)
// quartic^2 = chi(x)
// quartic = sqrt(chi(x))
// quartic = 1 or -1 or sqrt(-1) or -sqrt(-1)
//
// Note that x is a square if quartic is 1 or -1
// There are 4 cases to consider:
//
// if quartic = 1 (x is a square)
// then x^((p-1)/4) = 1
// x^((p-5)/4) * x = 1
// x^((p-5)/4) = 1/x
// x^((p-5)/8) = sqrt(1/x) or -sqrt(1/x)
//
// if quartic = -1 (x is a square)
// then x^((p-1)/4) = -1
// x^((p-5)/4) * x = -1
// x^((p-5)/4) = -1/x
// x^((p-5)/8) = sqrt(-1) / sqrt(x)
// x^((p-5)/8) * sqrt(-1) = sqrt(-1)^2 / sqrt(x)
// x^((p-5)/8) * sqrt(-1) = -1/sqrt(x)
// x^((p-5)/8) * sqrt(-1) = -sqrt(1/x) or sqrt(1/x)
//
// if quartic = sqrt(-1) (x is not a square)
// then x^((p-1)/4) = sqrt(-1)
// x^((p-5)/4) * x = sqrt(-1)
// x^((p-5)/4) = sqrt(-1)/x
// x^((p-5)/8) = sqrt(sqrt(-1)/x) or -sqrt(sqrt(-1)/x)
//
// Note that the product of two non-squares is always a square:
// For any non-squares a and b, chi(a) = -1 and chi(b) = -1.
// Since chi(x) = x^((p-1)/2), chi(a)*chi(b) = chi(a*b) = 1.
// Therefore a*b is a square.
//
// Since sqrt(-1) and x are both non-squares, their product is a
// square, and we can compute their square root.
//
// if quartic = -sqrt(-1) (x is not a square)
// then x^((p-1)/4) = -sqrt(-1)
// x^((p-5)/4) * x = -sqrt(-1)
// x^((p-5)/4) = -sqrt(-1)/x
// x^((p-5)/8) = sqrt(-sqrt(-1)/x)
// x^((p-5)/8) = sqrt( sqrt(-1)/x) * sqrt(-1)
// x^((p-5)/8) * sqrt(-1) = sqrt( sqrt(-1)/x) * sqrt(-1)^2
// x^((p-5)/8) * sqrt(-1) = sqrt( sqrt(-1)/x) * -1
// x^((p-5)/8) * sqrt(-1) = -sqrt(sqrt(-1)/x) or sqrt(sqrt(-1)/x)
static int invsqrt(fe isr, const fe x)
{
fe check, quartic;
fe_copy(check, x);
fe_pow22523(isr, check);
fe_sq (quartic, isr);
fe_mul(quartic, quartic, check);
fe_1 (check); int p1 = fe_isequal(quartic, check);
fe_neg(check, check ); int m1 = fe_isequal(quartic, check);
fe_neg(check, sqrtm1); int ms = fe_isequal(quartic, check);
fe_mul(check, isr, sqrtm1);
fe_ccopy(isr, check, m1 | ms);
WIPE_BUFFER(quartic);
WIPE_BUFFER(check);
return p1 | m1;
}
// trim a scalar for scalar multiplication
static void trim_scalar(u8 scalar[32])
{
scalar[ 0] &= 248;
scalar[31] &= 127;
scalar[31] |= 64;
}
// cvw: Export the trim_scalar function for external use
void crypto_x25519_clamp(uint8_t scalar[32]) {
trim_scalar(scalar);
}
// get bit from scalar at position i
static int scalar_bit(const u8 s[32], int i)
{
if (i < 0) { return 0; } // handle -1 for sliding windows
return (s[i>>3] >> (i&7)) & 1;
}
///////////////
/// X-25519 /// Taken from SUPERCOP's ref10 implementation.
///////////////
static void scalarmult(u8 q[32], const u8 scalar[32], const u8 p[32],
int nb_bits)
{
// computes the scalar product
fe x1;
fe_frombytes(x1, p);
// computes the actual scalar product (the result is in x2 and z2)
fe x2, z2, x3, z3, t0, t1;
// Montgomery ladder
// In projective coordinates, to avoid divisions: x = X / Z
// We don't care about the y coordinate, it's only 1 bit of information
fe_1(x2); fe_0(z2); // "zero" point
fe_copy(x3, x1); fe_1(z3); // "one" point
int swap = 0;
for (int pos = nb_bits-1; pos >= 0; --pos) {
// constant time conditional swap before ladder step
int b = scalar_bit(scalar, pos);
swap ^= b; // xor trick avoids swapping at the end of the loop
fe_cswap(x2, x3, swap);
fe_cswap(z2, z3, swap);
swap = b; // anticipates one last swap after the loop
// Montgomery ladder step: replaces (P2, P3) by (P2*2, P2+P3)
// with differential addition
fe_sub(t0, x3, z3);
fe_sub(t1, x2, z2);
fe_add(x2, x2, z2);
fe_add(z2, x3, z3);
fe_mul(z3, t0, x2);
fe_mul(z2, z2, t1);
fe_sq (t0, t1 );
fe_sq (t1, x2 );
fe_add(x3, z3, z2);
fe_sub(z2, z3, z2);
fe_mul(x2, t1, t0);
fe_sub(t1, t1, t0);
fe_sq (z2, z2 );
fe_mul_small(z3, t1, 121666);
fe_sq (x3, x3 );
fe_add(t0, t0, z3);
fe_mul(z3, x1, z2);
fe_mul(z2, t1, t0);
}
// last swap is necessary to compensate for the xor trick
// Note: after this swap, P3 == P2 + P1.
fe_cswap(x2, x3, swap);
fe_cswap(z2, z3, swap);
// normalises the coordinates: x == X / Z
fe_invert(z2, z2);
fe_mul(x2, x2, z2);
fe_tobytes(q, x2);
WIPE_BUFFER(x1);
WIPE_BUFFER(x2); WIPE_BUFFER(z2); WIPE_BUFFER(t0);
WIPE_BUFFER(x3); WIPE_BUFFER(z3); WIPE_BUFFER(t1);
}
// cvw: Hacked/kludged this in here to expose the function for BS-SPEKE
void crypto_x25519_scalarmult(uint8_t q[32], const uint8_t scalar[32],
const uint8_t p[32], int nb_bits) {
scalarmult(q, scalar, p, nb_bits);
}
void crypto_x25519(u8 raw_shared_secret[32],
const u8 your_secret_key [32],
const u8 their_public_key [32])
{
// restrict the possible scalar values
u8 e[32];
COPY(e, your_secret_key, 32);
trim_scalar(e);
scalarmult(raw_shared_secret, e, their_public_key, 255);
WIPE_BUFFER(e);
}
void crypto_x25519_public_key(u8 public_key[32],
const u8 secret_key[32])
{
static const u8 base_point[32] = {9};
crypto_x25519(public_key, secret_key, base_point);
}
///////////////////////////
/// Arithmetic modulo L ///
///////////////////////////
static const u32 L[8] = {0x5cf5d3ed, 0x5812631a, 0xa2f79cd6, 0x14def9de,
0x00000000, 0x00000000, 0x00000000, 0x10000000,};
// p = a*b + p
static void multiply(u32 p[16], const u32 a[8], const u32 b[8])
{
FOR (i, 0, 8) {
u64 carry = 0;
FOR (j, 0, 8) {
carry += p[i+j] + (u64)a[i] * b[j];
p[i+j] = (u32)carry;
carry >>= 32;
}
p[i+8] = (u32)carry;
}
}
static int is_above_l(const u32 x[8])
{
// We work with L directly, in a 2's complement encoding
// (-L == ~L + 1)
u64 carry = 1;
FOR (i, 0, 8) {
carry += (u64)x[i] + ~L[i];
carry >>= 32;
}
return carry;
}
// Final reduction modulo L, by conditionally removing L.
// if x < l , then r = x
// if l <= x 2*l, then r = x-l
// otherwise the result will be wrong
static void remove_l(u32 r[8], const u32 x[8])
{
u64 carry = is_above_l(x);
u32 mask = ~(u32)carry + 1; // carry == 0 or 1
FOR (i, 0, 8) {
carry += (u64)x[i] + (~L[i] & mask);
r[i] = (u32)carry;
carry >>= 32;
}
}
// Full reduction modulo L (Barrett reduction)
static void mod_l(u8 reduced[32], const u32 x[16])
{
static const u32 r[9] = {0x0a2c131b,0xed9ce5a3,0x086329a7,0x2106215d,
0xffffffeb,0xffffffff,0xffffffff,0xffffffff,0xf,};
// xr = x * r
u32 xr[25] = {0};
FOR (i, 0, 9) {
u64 carry = 0;
FOR (j, 0, 16) {
carry += xr[i+j] + (u64)r[i] * x[j];
xr[i+j] = (u32)carry;
carry >>= 32;
}
xr[i+16] = (u32)carry;
}
// xr = floor(xr / 2^512) * L
// Since the result is guaranteed to be below 2*L,
// it is enough to only compute the first 256 bits.
// The division is performed by saying xr[i+16]. (16 * 32 = 512)
ZERO(xr, 8);
FOR (i, 0, 8) {
u64 carry = 0;
FOR (j, 0, 8-i) {
carry += xr[i+j] + (u64)xr[i+16] * L[j];
xr[i+j] = (u32)carry;
carry >>= 32;
}
}
// xr = x - xr
u64 carry = 1;
FOR (i, 0, 8) {
carry += (u64)x[i] + ~xr[i];
xr[i] = (u32)carry;
carry >>= 32;
}
// Final reduction modulo L (conditional subtraction)
remove_l(xr, xr);
store32_le_buf(reduced, xr, 8);
WIPE_BUFFER(xr);
}
static void reduce(u8 r[64])
{
u32 x[16];
load32_le_buf(x, r, 16);
mod_l(r, x);
WIPE_BUFFER(x);
}
// r = (a * b) + c
static void mul_add(u8 r[32], const u8 a[32], const u8 b[32], const u8 c[32])
{
u32 A[8]; load32_le_buf(A, a, 8);
u32 B[8]; load32_le_buf(B, b, 8);
u32 p[16];
load32_le_buf(p, c, 8);
ZERO(p + 8, 8);
multiply(p, A, B);
mod_l(r, p);
WIPE_BUFFER(p);
WIPE_BUFFER(A);
WIPE_BUFFER(B);
}
///////////////
/// Ed25519 ///
///////////////
// Point (group element, ge) in a twisted Edwards curve,
// in extended projective coordinates.
// ge : x = X/Z, y = Y/Z, T = XY/Z
// ge_cached : Yp = X+Y, Ym = X-Y, T2 = T*D2
// ge_precomp: Z = 1
typedef struct { fe X; fe Y; fe Z; fe T; } ge;
typedef struct { fe Yp; fe Ym; fe Z; fe T2; } ge_cached;
typedef struct { fe Yp; fe Ym; fe T2; } ge_precomp;
static void ge_zero(ge *p)
{
fe_0(p->X);
fe_1(p->Y);
fe_1(p->Z);
fe_0(p->T);
}
static void ge_tobytes(u8 s[32], const ge *h)
{
fe recip, x, y;
fe_invert(recip, h->Z);
fe_mul(x, h->X, recip);
fe_mul(y, h->Y, recip);
fe_tobytes(s, y);
s[31] ^= fe_isodd(x) << 7;
WIPE_BUFFER(recip);
WIPE_BUFFER(x);
WIPE_BUFFER(y);
}
// h = s, where s is a point encoded in 32 bytes
//
// Variable time! Inputs must not be secret!
// => Use only to *check* signatures.
//
// From the specifications:
// The encoding of s contains y and the sign of x
// x = sqrt((y^2 - 1) / (d*y^2 + 1))
// In extended coordinates:
// X = x, Y = y, Z = 1, T = x*y
//
// Note that num * den is a square iff num / den is a square
// If num * den is not a square, the point was not on the curve.
// From the above:
// Let num = y^2 - 1
// Let den = d*y^2 + 1
// x = sqrt((y^2 - 1) / (d*y^2 + 1))
// x = sqrt(num / den)
// x = sqrt(num^2 / (num * den))
// x = num * sqrt(1 / (num * den))
//
// Therefore, we can just compute:
// num = y^2 - 1
// den = d*y^2 + 1
// isr = invsqrt(num * den) // abort if not square
// x = num * isr
// Finally, negate x if its sign is not as specified.
static int ge_frombytes_vartime(ge *h, const u8 s[32])
{
fe_frombytes(h->Y, s);
fe_1(h->Z);
fe_sq (h->T, h->Y); // t = y^2
fe_mul(h->X, h->T, d ); // x = d*y^2
fe_sub(h->T, h->T, h->Z); // t = y^2 - 1
fe_add(h->X, h->X, h->Z); // x = d*y^2 + 1
fe_mul(h->X, h->T, h->X); // x = (y^2 - 1) * (d*y^2 + 1)
int is_square = invsqrt(h->X, h->X);
if (!is_square) {
return -1; // Not on the curve, abort
}
fe_mul(h->X, h->T, h->X); // x = sqrt((y^2 - 1) / (d*y^2 + 1))
if (fe_isodd(h->X) != (s[31] >> 7)) {
fe_neg(h->X, h->X);
}
fe_mul(h->T, h->X, h->Y);
return 0;
}
static void ge_cache(ge_cached *c, const ge *p)
{
fe_add (c->Yp, p->Y, p->X);
fe_sub (c->Ym, p->Y, p->X);
fe_copy(c->Z , p->Z );
fe_mul (c->T2, p->T, D2 );
}
// Internal buffers are not wiped! Inputs must not be secret!
// => Use only to *check* signatures.
static void ge_add(ge *s, const ge *p, const ge_cached *q)
{
fe a, b;
fe_add(a , p->Y, p->X );
fe_sub(b , p->Y, p->X );
fe_mul(a , a , q->Yp);
fe_mul(b , b , q->Ym);
fe_add(s->Y, a , b );
fe_sub(s->X, a , b );
fe_add(s->Z, p->Z, p->Z );
fe_mul(s->Z, s->Z, q->Z );
fe_mul(s->T, p->T, q->T2);
fe_add(a , s->Z, s->T );
fe_sub(b , s->Z, s->T );
fe_mul(s->T, s->X, s->Y);
fe_mul(s->X, s->X, b );
fe_mul(s->Y, s->Y, a );
fe_mul(s->Z, a , b );
}
// Internal buffers are not wiped! Inputs must not be secret!
// => Use only to *check* signatures.
static void ge_sub(ge *s, const ge *p, const ge_cached *q)
{
ge_cached neg;
fe_copy(neg.Ym, q->Yp);
fe_copy(neg.Yp, q->Ym);
fe_copy(neg.Z , q->Z );
fe_neg (neg.T2, q->T2);
ge_add(s, p, &neg);
}
static void ge_madd(ge *s, const ge *p, const ge_precomp *q, fe a, fe b)
{
fe_add(a , p->Y, p->X );
fe_sub(b , p->Y, p->X );
fe_mul(a , a , q->Yp);
fe_mul(b , b , q->Ym);
fe_add(s->Y, a , b );
fe_sub(s->X, a , b );
fe_add(s->Z, p->Z, p->Z );
fe_mul(s->T, p->T, q->T2);
fe_add(a , s->Z, s->T );
fe_sub(b , s->Z, s->T );
fe_mul(s->T, s->X, s->Y);
fe_mul(s->X, s->X, b );
fe_mul(s->Y, s->Y, a );
fe_mul(s->Z, a , b );
}
static void ge_msub(ge *s, const ge *p, const ge_precomp *q, fe a, fe b)
{
fe_add(a , p->Y, p->X );
fe_sub(b , p->Y, p->X );
fe_mul(a , a , q->Ym);
fe_mul(b , b , q->Yp);
fe_add(s->Y, a , b );
fe_sub(s->X, a , b );
fe_add(s->Z, p->Z, p->Z );
fe_mul(s->T, p->T, q->T2);
fe_sub(a , s->Z, s->T );
fe_add(b , s->Z, s->T );
fe_mul(s->T, s->X, s->Y);
fe_mul(s->X, s->X, b );
fe_mul(s->Y, s->Y, a );
fe_mul(s->Z, a , b );
}
static void ge_double(ge *s, const ge *p, ge *q)
{
fe_sq (q->X, p->X);
fe_sq (q->Y, p->Y);
fe_sq2(q->Z, p->Z);
fe_add(q->T, p->X, p->Y);
fe_sq (s->T, q->T);
fe_add(q->T, q->Y, q->X);
fe_sub(q->Y, q->Y, q->X);
fe_sub(q->X, s->T, q->T);
fe_sub(q->Z, q->Z, q->Y);
fe_mul(s->X, q->X , q->Z);
fe_mul(s->Y, q->T , q->Y);
fe_mul(s->Z, q->Y , q->Z);
fe_mul(s->T, q->X , q->T);
}
// 5-bit signed window in cached format (Niels coordinates, Z=1)
static const ge_precomp b_window[8] = {
{{25967493,-14356035,29566456,3660896,-12694345,
4014787,27544626,-11754271,-6079156,2047605,},
{-12545711,934262,-2722910,3049990,-727428,
9406986,12720692,5043384,19500929,-15469378,},
{-8738181,4489570,9688441,-14785194,10184609,
-12363380,29287919,11864899,-24514362,-4438546,},},
{{15636291,-9688557,24204773,-7912398,616977,
-16685262,27787600,-14772189,28944400,-1550024,},
{16568933,4717097,-11556148,-1102322,15682896,
-11807043,16354577,-11775962,7689662,11199574,},
{30464156,-5976125,-11779434,-15670865,23220365,
15915852,7512774,10017326,-17749093,-9920357,},},
{{10861363,11473154,27284546,1981175,-30064349,
12577861,32867885,14515107,-15438304,10819380,},
{4708026,6336745,20377586,9066809,-11272109,
6594696,-25653668,12483688,-12668491,5581306,},
{19563160,16186464,-29386857,4097519,10237984,
-4348115,28542350,13850243,-23678021,-15815942,},},
{{5153746,9909285,1723747,-2777874,30523605,
5516873,19480852,5230134,-23952439,-15175766,},
{-30269007,-3463509,7665486,10083793,28475525,
1649722,20654025,16520125,30598449,7715701,},
{28881845,14381568,9657904,3680757,-20181635,
7843316,-31400660,1370708,29794553,-1409300,},},
{{-22518993,-6692182,14201702,-8745502,-23510406,
8844726,18474211,-1361450,-13062696,13821877,},
{-6455177,-7839871,3374702,-4740862,-27098617,
-10571707,31655028,-7212327,18853322,-14220951,},
{4566830,-12963868,-28974889,-12240689,-7602672,
-2830569,-8514358,-10431137,2207753,-3209784,},},
{{-25154831,-4185821,29681144,7868801,-6854661,
-9423865,-12437364,-663000,-31111463,-16132436,},
{25576264,-2703214,7349804,-11814844,16472782,
9300885,3844789,15725684,171356,6466918,},
{23103977,13316479,9739013,-16149481,817875,
-15038942,8965339,-14088058,-30714912,16193877,},},
{{-33521811,3180713,-2394130,14003687,-16903474,
-16270840,17238398,4729455,-18074513,9256800,},
{-25182317,-4174131,32336398,5036987,-21236817,
11360617,22616405,9761698,-19827198,630305,},
{-13720693,2639453,-24237460,-7406481,9494427,
-5774029,-6554551,-15960994,-2449256,-14291300,},},
{{-3151181,-5046075,9282714,6866145,-31907062,
-863023,-18940575,15033784,25105118,-7894876,},
{-24326370,15950226,-31801215,-14592823,-11662737,
-5090925,1573892,-2625887,2198790,-15804619,},
{-3099351,10324967,-2241613,7453183,-5446979,
-2735503,-13812022,-16236442,-32461234,-12290683,},},
};
// Incremental sliding windows (left to right)
// Based on Roberto Maria Avanzi[2005]
typedef struct {
i16 next_index; // position of the next signed digit
i8 next_digit; // next signed digit (odd number below 2^window_width)
u8 next_check; // point at which we must check for a new window
} slide_ctx;
static void slide_init(slide_ctx *ctx, const u8 scalar[32])
{
// scalar is guaranteed to be below L, either because we checked (s),
// or because we reduced it modulo L (h_ram). L is under 2^253, so
// so bits 253 to 255 are guaranteed to be zero. No need to test them.
//
// Note however that L is very close to 2^252, so bit 252 is almost
// always zero. If we were to start at bit 251, the tests wouldn't
// catch the off-by-one error (constructing one that does would be
// prohibitively expensive).
//
// We should still check bit 252, though.
int i = 252;
while (i > 0 && scalar_bit(scalar, i) == 0) {
i--;
}
ctx->next_check = (u8)(i + 1);
ctx->next_index = -1;
ctx->next_digit = -1;
}
static int slide_step(slide_ctx *ctx, int width, int i, const u8 scalar[32])
{
if (i == ctx->next_check) {
if (scalar_bit(scalar, i) == scalar_bit(scalar, i - 1)) {
ctx->next_check--;
} else {
// compute digit of next window
int w = MIN(width, i + 1);
int v = -(scalar_bit(scalar, i) << (w-1));
FOR_T (int, j, 0, w-1) {
v += scalar_bit(scalar, i-(w-1)+j) << j;
}
v += scalar_bit(scalar, i-w);
int lsb = v & (~v + 1); // smallest bit of v
int s = ( ((lsb & 0xAA) != 0) // log2(lsb)
| (((lsb & 0xCC) != 0) << 1)
| (((lsb & 0xF0) != 0) << 2));
ctx->next_index = (i16)(i-(w-1)+s);
ctx->next_digit = (i8) (v >> s );
ctx->next_check -= (u8) w;
}
}
return i == ctx->next_index ? ctx->next_digit: 0;
}
#define P_W_WIDTH 3 // Affects the size of the stack
#define B_W_WIDTH 5 // Affects the size of the binary
#define P_W_SIZE (1<<(P_W_WIDTH-2))
// P = [b]B + [p]P, where B is the base point
//
// Variable time! Internal buffers are not wiped! Inputs must not be secret!
// => Use only to *check* signatures.
static void ge_double_scalarmult_vartime(ge *P, const u8 p[32], const u8 b[32])
{
// cache P window for addition
ge_cached cP[P_W_SIZE];
{
ge P2, tmp;
ge_double(&P2, P, &tmp);
ge_cache(&cP[0], P);
FOR (i, 1, P_W_SIZE) {
ge_add(&tmp, &P2, &cP[i-1]);
ge_cache(&cP[i], &tmp);
}
}
// Merged double and add ladder, fused with sliding
slide_ctx p_slide; slide_init(&p_slide, p);
slide_ctx b_slide; slide_init(&b_slide, b);
int i = MAX(p_slide.next_check, b_slide.next_check);
ge *sum = P;
ge_zero(sum);
while (i >= 0) {
ge tmp;
ge_double(sum, sum, &tmp);
int p_digit = slide_step(&p_slide, P_W_WIDTH, i, p);
int b_digit = slide_step(&b_slide, B_W_WIDTH, i, b);
if (p_digit > 0) { ge_add(sum, sum, &cP[ p_digit / 2]); }
if (p_digit < 0) { ge_sub(sum, sum, &cP[-p_digit / 2]); }
fe t1, t2;
if (b_digit > 0) { ge_madd(sum, sum, b_window + b_digit/2, t1, t2); }
if (b_digit < 0) { ge_msub(sum, sum, b_window + -b_digit/2, t1, t2); }
i--;
}
}
// R_check = s[B] - h_ram[pk], where B is the base point
//
// Variable time! Internal buffers are not wiped! Inputs must not be secret!
// => Use only to *check* signatures.
static int ge_r_check(u8 R_check[32], u8 s[32], u8 h_ram[32], u8 pk[32])
{
ge A; // not secret, not wiped
u32 s32[8]; // not secret, not wiped
load32_le_buf(s32, s, 8);
if (ge_frombytes_vartime(&A, pk) || // A = pk
is_above_l(s32)) { // prevent s malleability
return -1;
}
fe_neg(A.X, A.X);
fe_neg(A.T, A.T); // A = -pk
ge_double_scalarmult_vartime(&A, h_ram, s); // A = [s]B - [h_ram]pk
ge_tobytes(R_check, &A); // R_check = A
return 0;
}
// 5-bit signed comb in cached format (Niels coordinates, Z=1)
static const ge_precomp b_comb_low[8] = {
{{-6816601,-2324159,-22559413,124364,18015490,
8373481,19993724,1979872,-18549925,9085059,},
{10306321,403248,14839893,9633706,8463310,
-8354981,-14305673,14668847,26301366,2818560,},
{-22701500,-3210264,-13831292,-2927732,-16326337,
-14016360,12940910,177905,12165515,-2397893,},},
{{-12282262,-7022066,9920413,-3064358,-32147467,
2927790,22392436,-14852487,2719975,16402117,},
{-7236961,-4729776,2685954,-6525055,-24242706,
-15940211,-6238521,14082855,10047669,12228189,},
{-30495588,-12893761,-11161261,3539405,-11502464,
16491580,-27286798,-15030530,-7272871,-15934455,},},
{{17650926,582297,-860412,-187745,-12072900,