Short answer: Poly1305 computes a 16-byte tag under a one-time 32-byte key. The first 16 bytes, with selected bits cleared, are the evaluation point r. The last 16 bytes are the pad s. The message is read in 16-byte chunks. Each chunk gains a trailing 0x01 byte and is added into an accumulator, which is then multiplied by r modulo 2^130 - 5. The tag is the low 128 bits of the accumulator plus s.
What the problem actually asks
A one-time authenticator has to be fast and has to fail if the key is used twice as though it were a long-term MAC key. Poly1305 is a polynomial hash over the prime field 2^130 - 5, plus a one-time pad s. The bit clearing on r keeps the multiplications inside the shape the designers chose. ChaCha20-Poly1305 uses a fresh Poly1305 key for every message, taken from the ChaCha20 keystream. The fragment takes the key as an input. It does not derive that key.
What the algorithm guarantees
The same key and message reproduce the same tag. A receiver adds and multiplies the received message and compares the whole tag. The security statement is for one message per key. Reusing r and s across messages is outside that statement. The fragment checks the RFC 8439 message Cryptographic Forum Research Group.
The working steps
- Read
ras a little-endian integer and keep only the bits in0x0ffffffc0ffffffc0ffffffc0fffffff. - Read
sas a little-endian integer from the last 16 key bytes. - For each 16-byte chunk, append
0x01, add it to the accumulator, and multiply byrmodulo2^130 - 5. - Add
sand output the low 16 bytes.
Approaches that look simpler and fail
| Approach | Why it fails when the same key authenticates two messages |
|---|---|
| Reuse one Poly1305 key for a second message | The construction is one-time. A second message under the same r and s is not the stated authenticator. |
Skip the bit clearing on r |
The published function clears those bits. Leaving them set is a different polynomial. |
Append 0x01 only on the last chunk |
Every chunk, including a partial last chunk, is marked with the extra byte. |
| Use the tag as a cipher | The tag detects a change. It does not hide the message. |
HMAC is a reusable message authentication code built from a hash. Poly1305 is the one-time polynomial tag.
A working example
This Python fragment evaluates the RFC 8439 Poly1305 tag. It prints a8061dc1305136c6c22b8baf0c0127a9.
def poly1305(msg: bytes, key: bytes) -> bytes:
r = int.from_bytes(key[:16], "little") & 0x0FFFFFFC0FFFFFFC0FFFFFFC0FFFFFFF
s = int.from_bytes(key[16:], "little")
acc = 0
prime = (1 << 130) - 5
for off in range(0, len(msg), 16):
block = msg[off : off + 16]
n = int.from_bytes(block + b"\x01", "little")
acc = (acc + n) * r % prime
tag = (acc + s) & ((1 << 128) - 1)
return tag.to_bytes(16, "little")
key = bytes.fromhex("85d6be7857556d337f4452fe42d506a80103808afb0db2fd4abff6af4149f51b")
tag = poly1305(b"Cryptographic Forum Research Group", key)
assert tag.hex() == "a8061dc1305136c6c22b8baf0c0127a9"
print(tag.hex())
Where it is used
Poly1305 is the tag in ChaCha20-Poly1305, the AEAD in TLS, WireGuard, and OpenSSH. Each message gets a fresh one-time key taken from the ChaCha20 keystream. HMAC is the reusable tag built from a hash.
When you can drop this algorithm
Drop Poly1305 where the same 32-byte key would be reused. Keep it where each message gets a fresh key, as it does when that key is taken from a ChaCha20 block.
Why this stays published
We publish this so a search for Poly1305 returns the clamped r, the prime 2^130 - 5, and the RFC tag. Poly1305 evaluates a polynomial modulo 2^130 - 5 at a clamped one-time point and adds a one-time pad to produce a 16-byte tag.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
