Skip to content

Replace BigInt in the 32-bit Poly1305 and make the final reduction constant-time - #43

Merged
dipu-bd merged 1 commit into
masterfrom
ccr-3e9bf447-zsxg7p-poly1305
Nov 29, 2026
Merged

dipu-bd merged 1 commit into
masterfrom
ccr-3e9bf447-zsxg7p-poly1305

Conversation

@dipu-bd

@dipu-bd dipu-bd commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Web Poly1305 (poly1305_32bit.dart): BigInt arithmetic is replaced with the 16-bit layout of poly1305-donna (also used by tweetnacl-js).
    • r and the accumulator are each held as ten 13-bit limbs.
    • Every partial product stays below 2^30 and every column sum below 2^35, so all arithmetic is exact within JavaScript's 53-bit integer range.
    • Carries out of those sums use ~/ rather than >>>, because the sums can exceed 32 bits.
    • The class name, constructor, key validation and buffering are unchanged.
  • Constant-time final reduction, both implementations: the final h mod p step now picks between h and h - p with a mask instead of an if. Its timing no longer depends on the accumulated value. The 32-bit code is written that way from the start; poly1305_64bit.dart previously used a branch there. This only touches $finalize, so it adds a few operations per message, not per block.
  • AGENTS.md: lists the new differential test.

The branch is a single commit on top of master.

Performance (web)

Node, dart2js -O2, same script before and after. Single runs in a cloud container, not the reference machine.

Message size Before After Speedup
16 B 1.64 MB/s 21.1 MB/s 13x
64 B 3.42 MB/s 54.5 MB/s 16x
1 KiB 5.06 MB/s 126 MB/s 25x
1 MiB 5.23 MB/s 142 MB/s 27x

cipherlib's Poly1305-based AEADs (ChaCha20-Poly1305, XSalsa20-Poly1305) should get faster on the web once it picks up a hashlib release with this change.

Tests

  • Edge cases (test/poly1305_test.dart, all platforms): 9 new vectors aimed at the final reduction and carry handling.
  • Differential (test/poly1305_32bit_test.dart, vm-only): the 32-bit code against the 64-bit code. It covers 300 random keys (16- and 32-byte) and keys saturated with single byte values, each at 14 message lengths up to 4099 bytes, plus a reset check. All matched.
  • 64-bit change: I compared the new 64-bit code with the previous version on 18,000 inputs, including saturated ones that reach the h ≥ p path. The output is identical. This was a one-off check and isn't committed.

Verification

  • dart format --output=none --set-exit-if-changed . makes no changes
  • dart analyze --fatal-infos reports no issues
  • dart test -p vm: all tests pass
  • dart test -p node: all tests pass
  • dart test -P wasm: all tests pass
  • dart test -p chrome and the integration smoke test also passed on the same code.

No CHANGELOG entry: the version heading is left to the maintainer. Suggested bullets:

  • "Speed up Poly1305 on the web 13–27x by replacing BigInt with 13-bit limb arithmetic"
  • "Make the Poly1305 final reduction constant-time"

🤖 Generated with Claude Code

https://claude.ai/code/session_01KmuCbukmnH9zJJPoxX3tGo

Base automatically changed from ccr-3e9bf447-zsxg7p-uint64-sha512-keccak to master November 28, 2026 22:24
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

Files with missing lines Coverage Δ
lib/src/algorithms/poly1305/poly1305_32bit.dart 99.61% <100.00%> (ø)
lib/src/algorithms/poly1305/poly1305_64bit.dart 100.00% <100.00%> (ø)

…constant-time

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KmuCbukmnH9zJJPoxX3tGo
@dipu-bd
dipu-bd force-pushed the ccr-3e9bf447-zsxg7p-poly1305 branch from 8a445e9 to 7798ca6 Compare November 29, 2026 20:34
@dipu-bd
dipu-bd merged commit bc6b75b into master Nov 29, 2026
20 checks passed
@dipu-bd
dipu-bd deleted the ccr-3e9bf447-zsxg7p-poly1305 branch November 29, 2026 20:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants