cnfy-uint 0.2.3

Zero-dependency 256-bit unsigned integer arithmetic for cryptographic applications
Documentation
//! Precomputed tables for [`KnuthD`] reciprocal estimation.

/// MG10 lookup table for initial reciprocal approximation (Algorithm 3).
///
/// Entry `i` approximates `floor((2^19 - 3ยท2^8) / (i + 1))` for `d9` values
/// 256..512, providing ~10 bits of precision for the Newton-Raphson seed
/// used in [`KnuthD::reciprocal_1`](super::KnuthD::reciprocal_1).
pub(super) static RECIP_TABLE: [u16; 256] = [
    2045, 2037, 2029, 2021, 2013, 2005, 1998, 1990, 1983, 1975, 1968, 1960, 1953, 1946, 1938, 1931,
    1924, 1917, 1910, 1903, 1896, 1889, 1883, 1876, 1869, 1863, 1856, 1849, 1843, 1836, 1830, 1824,
    1817, 1811, 1805, 1799, 1792, 1786, 1780, 1774, 1768, 1762, 1756, 1750, 1745, 1739, 1733, 1727,
    1722, 1716, 1710, 1705, 1699, 1694, 1688, 1683, 1677, 1672, 1667, 1661, 1656, 1651, 1646, 1641,
    1636, 1630, 1625, 1620, 1615, 1610, 1605, 1600, 1596, 1591, 1586, 1581, 1576, 1572, 1567, 1562,
    1558, 1553, 1548, 1544, 1539, 1535, 1530, 1526, 1521, 1517, 1513, 1508, 1504, 1500, 1495, 1491,
    1487, 1483, 1478, 1474, 1470, 1466, 1462, 1458, 1454, 1450, 1446, 1442, 1438, 1434, 1430, 1426,
    1422, 1418, 1414, 1411, 1407, 1403, 1399, 1396, 1392, 1388, 1384, 1381, 1377, 1374, 1370, 1366,
    1363, 1359, 1356, 1352, 1349, 1345, 1342, 1338, 1335, 1332, 1328, 1325, 1322, 1318, 1315, 1312,
    1308, 1305, 1302, 1299, 1295, 1292, 1289, 1286, 1283, 1280, 1276, 1273, 1270, 1267, 1264, 1261,
    1258, 1255, 1252, 1249, 1246, 1243, 1240, 1237, 1234, 1231, 1228, 1226, 1223, 1220, 1217, 1214,
    1211, 1209, 1206, 1203, 1200, 1197, 1195, 1192, 1189, 1187, 1184, 1181, 1179, 1176, 1173, 1171,
    1168, 1165, 1163, 1160, 1158, 1155, 1153, 1150, 1148, 1145, 1143, 1140, 1138, 1135, 1133, 1130,
    1128, 1125, 1123, 1121, 1118, 1116, 1113, 1111, 1109, 1106, 1104, 1102, 1099, 1097, 1095, 1092,
    1090, 1088, 1086, 1083, 1081, 1079, 1077, 1074, 1072, 1070, 1068, 1066, 1064, 1061, 1059, 1057,
    1055, 1053, 1051, 1049, 1047, 1044, 1042, 1040, 1038, 1036, 1034, 1032, 1030, 1028, 1026, 1024,
];

#[cfg(test)]
mod ai_tests {
    use super::*;

    /// Table has exactly 256 entries.
    #[test]
    fn table_length() {
        assert_eq!(RECIP_TABLE.len(), 256);
    }

    /// First entry matches floor((2^19 - 3*2^8) / 256).
    #[test]
    fn first_entry() {
        let expected = ((1u64 << 19) - 3 * (1u64 << 8)) / 256;
        assert_eq!(RECIP_TABLE[0] as u64, expected);
    }

    /// Last entry is 1024.
    #[test]
    fn last_entry() {
        assert_eq!(RECIP_TABLE[255], 1024);
    }

    /// Entries are monotonically non-increasing.
    #[test]
    fn monotonic_decreasing() {
        for i in 1..256 {
            assert!(
                RECIP_TABLE[i] <= RECIP_TABLE[i - 1],
                "RECIP_TABLE[{i}]={} > RECIP_TABLE[{}]={}",
                RECIP_TABLE[i],
                i - 1,
                RECIP_TABLE[i - 1],
            );
        }
    }
}