packages feed

crypton-2.3.1 / cbits / s2n / README.md

# s2n-bignumAssembly for the NIST prime curves from AWS's[s2n-bignum](https://github.com/awslabs/s2n-bignum), vendored here.`COMMIT` holds the upstream revision these files came from, and `import.sh`fetches them again.  The files are unmodified: the choice between the twovariants of each routine is made in crypton's own C, not by editing theirs.## Whycrypton's P-256 is portable C, and on the two architectures s2n-bignumcovers, hand-written assembly beats it by a lot.  Measured against crypton'sown code in the same process, agreeing with it on every scalar tried:| | crypton | s2n-bignum || --- | ---: | ---: || Apple M4 | 52.9 us | **20.6** || x86-64 with ADX (EPYC 9V74) | 175.9 | **54.5** || x86-64, ADX not advertised | 156.1 | **59.4** |The third row is the `_alt` path, which is what crypton picks where `CPUID`does not report ADX.  It was measured on a KVM guest whose `CPUID` says sowhile the host underneath runs ADX instructions anyway, so it says what thetwo implementations cost relative to each other on that path, not what anactual pre-Broadwell part would do.Each routine also carries a machine-checked proof in HOL-Light that itcomputes what it says, and is written in a constant-time style.## Licence`Apache-2.0 OR ISC OR MIT-0`, one of which is on every file and all three in`LICENSE`.  crypton is BSD-3, so it takes these under **ISC** -- the MIT-0option would do as well, and the Apache one is the reason OpenSSL's andBoringSSL's `ecp_nistz256`, which is Apache-2.0 only, cannot be used here.## Which variantBoth forms of each routine are vendored, because which one is faster is notthe same question on the two architectures:* **On ARM**, `_alt` is written for parts with high multiplier throughput,  which is what Apple silicon is, and it wins there by 30-40%.  The plain  form is for parts that pipeline `UMULH` less well.  There is no feature bit  for this, so the choice is made at compile time on `__APPLE__`.* **On x86-64**, the plain form uses `MULX`, `ADCX` and `ADOX`, and `_alt` is  the fallback for processors without them.  That *is* a feature bit, so the  choice is made at run time, from `crypton_x86_simd_features()`.## RSA`crypton_powm.c`'s modular exponentiation also borrows from here, but only itsinnermost step and only on x86-64: `bignum_kmul_16_32`, `bignum_ksqr_16_32`,`bignum_kmul_32_64`, `bignum_ksqr_32_64` and `bignum_emontredc_8n` replace theC's Montgomery multiplication and square, and at RSA-3072's 24 limbs the squareis built from `bignum_sqr_8_16` and `bignum_mul_8_16`.  The window, the table and its maskedscan are crypton's throughout.  Sixteen limbs is 1024 bits and thirty-two is2048: the halves a CRT exponentiation works in for RSA-2048 and RSA-4096, andthe whole thing without CRT.  The reduction wants ADX, so the choice is made atrun time like the other x86-64 ones.It is twice the C, because the C cannot form the two carry chains `ADCX` and`ADOX` give.  Measured on an EPYC 7763 at 1024 bits:| | C | s2n-bignum || --- | ---: | ---: || multiplication | 0.4832 us | **0.2479** || square | 0.3984 | **0.1994** |### The Montgomery constant`bignum_montifier` is vendored for **both** architectures.  It is the oneroutine here that is not about a particular curve or a particular size: everymodular exponentiation and every setup in `crypton_ecc.c` starts by computingR^2 mod m, and this computes it.  crypton's C climbs to it by abinary ladder of Montgomery squarings -- log2 of the modulus' bits of them,eleven at 2048 -- and the assembly is between two and three and a half timesfaster, the gap being the carry chains again:| | C | s2n-bignum || --- | ---: | ---: || M4, 1024 bits | 1.853 us | **0.771** || M4, 2048 bits | 5.835 | **2.407** || an Intel x86-64, 1024 bits | 6.145 | **1.995** || an Intel x86-64, 2048 bits | 23.332 | **6.852** |A CRT signature computes it twice, so an RSA-2048 private operation on theEPYC 7763 goes from 613.5 to 606.4 microseconds -- a little over one percent, which is all a constant computed twice out of some two and a halfthousand multiplications can be.  It is worth much more than that to anythingwhose exponent is short, which is why it is here: see `crypton_powm.c`.The two agree bit for bit; `cbits/tests/powm` requires it at six widths, withmoduli that fill their limbs and moduli that do not, and a build without theassembly (`-DCRYPTON_NO_MONTIFIER`) is what it is checked against.**None of the five above is vendored for AArch64**, where they measure levelwith the C.  That took three attempts to establish, and the first two werewrong in opposite directions: a reading when the x86-64 routines went in putthe C 5% ahead, and one on 2026-09-26 put the assembly 4% ahead.  Both werewall-clock times on a machine with other work on it, where a swing of that sizesays nothing.  Measured by thread CPU time, taking the best of twenty-fivebatches and alternating the two binaries with the order swapped, one RSA-2048CRT private operation is 598.9 us through the C and 597.7 through the assembly:two tenths of a per cent, which is nothing.  The two agree on 200 random casesat 1024 and 2048 bits, so both were computing the same thing.  Five files fornothing is not a trade, so the C stays.The gap to OpenSSL on Apple silicon -- about half its speed -- is assembly andnot the C, and no portable C closes it.  BearSSL's `i62`, which is as far asportable C is known to go -- 62-bit limbs in 64-bit words, so that amultiply-accumulate fits an `__int128` with no carry chain at all, and afive-bit window against crypton's four -- was built and measured the same wayon the same machine: 626.4 us against crypton's C at 668.0, a tie.`bignum_emontredc_8n_cdiff` was tried too and is not the answer either: itwants a precomputation this test did not give it, and was slower besides.  Whatthe window is worth, and why it is four and not five, is in`cbits/crypton_powm.c` beside the constant.## Windowsx86-64 Windows takes the assembly as every other x86-64 does.  Two thingsstood in the way, and neither is fixed in a vendored file:* **The ELF directives.**  Each file ends its symbols with `.type` and  `.size`, which the COFF assemblers refuse -- mingw's gas says "junk at end  of line" after a warning, clang says "expected absolute expression".  There is nothing in a PE object for them to say, so on `_WIN32` they are  emptied, as upstream already does for Mach-O.* **The calling convention.**  Each file carries a prologue that moves  Microsoft's argument registers into System V's, under `WINDOWS_ABI`, which  upstream leaves to the build to define.  On `_WIN32` it is defined.Both are done in `x86_att/_internal_s2n_bignum_x86_att.h`, which is crypton'sand not upstream's.  Every vendored file includes that name in quotes, and aquoted include looks first beside the file that asks for it, so this is theone each gets; it hands on to upstream's in `include/` by a relative path.**It must not be removed as an upstream file that went missing**: without itWindows does not assemble.  On Linux and macOS it changes nothing -- theobjects are byte for byte what they were without it.Not `#include_next`, which is the obvious way to hand on: clang, finding thisheader beside the file that asked for it, starts that search from the topand finds this header again, without end.Windows on ARM keeps the C.  The AArch64 files have the same directives andwould want the same header, and there is no runner to check one.## What is not heres2n-bignum has only x86-64 and AArch64, so crypton's C stays and is whatevery other architecture uses.Its `generic` directory holds a complete variable-size bignum library on botharchitectures -- `bignum_montmul`, `bignum_montredc`, `bignum_emontredc`,`bignum_modinv` and a whole `bignum_modexp` -- and only `bignum_montifier` istaken from it.  **The rest was measured on an M4 and is slower than what ishere**, which is why it stays out; they agree with crypton's answers in everycase first, so both were computing the same thing.| nanoseconds, AArch64 | C | s2n-bignum || --- | ---: | ---: || Montgomery multiply, 1024 bits | **171.7** | 227.9 || Montgomery multiply, 2048 bits | **649.6** | 870.3 || Montgomery square, 1024 bits | **157.3** | 202.6 || Montgomery square, 2048 bits | **539.2** | 870.8 |That is the inline assembly above doing its job: four limbs to an iterationwith two carry chains, where a size-parametric routine takes them one at atime.  `bignum_modexp`, which would have replaced `crypton_powm_sec` outright,loses by more again -- 421.0 microseconds against 246.4 at 1024 bits and 3578against 1534 at 2048 -- because it has that multiplication inside it and nowindow, so it pays about twice the multiplications as well.The README there says every function is written in a constant-time style, withone named exception that is none of these, though it also says that unlikefunctional correctness this is not machine-checked.`p384_montjscalarmul` and `p521_jscalarmul`have no affine wrapper upstream; crypton's is in `cbits/p256/p256_s2n.c`'ssibling for those curves.  There is no fixed-base routine for P-384 orP-521 at all, so signing on those curves keeps crypton's comb.