Repository navigation
Constant-time AES, GHASH, POLYVAL and tag comparison - #52
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
this reply written by agent mycroft@luna:
Builds on #51 (AES-128-GCM), now merged.
What changed
Cipher/AES.lean):subBytesnow runs the Boyar–Peralta depth-16 S-box circuit (128 gates, 34 of them AND), bitsliced across the state with one byte per lane of aUInt64bit plane.mixColumnsdoubles with mask arithmetic,xtime x = (x <<< 1) ^^^ (0x1b &&& (0 - (x >>> 7))). The key schedule's SubWord goes through the samesubBytes. The table versions stay as the specification:sbox,subBytesSpec,mixColumnsSpec,expandKeySpec,expandKey128Spec,encryptBlockSpec. AEZ's imports ofsubBytes,shiftRowsandmixColumnsare unchanged.Cipher/GHash.lean): the hash state and key are now twoUInt64words. Algorithm 1 is still bit-serial, but masks replace its branches, and blocks are loaded and stored without going throughNat. The oldNatcode ismul/ghashSpec.Cipher/Polyval.lean): the same treatment, with the double-width product in four words, the carry-less multiply by masks, and the 128 halvings indivX128masked. The old code ispolyvalSpec.Util/ConstantTime.lean):ctEqORs together the XOR of every byte pair over the full length and tests the result once at the end.decryptin AES-GCM, AES-GCM-SIV and ChaCha20-Poly1305 now uses it. Theirdecrypt_encrypt/decrypt_soundlaws still hold.Proofs
Nothing uses
sorryornative_decide, and there are no new axioms.scripts/check-axioms.shpasses on AES, GHash, Polyval, ConstantTime, AESGCM, AESGCMSIV and ChaChaPoly: every theorem depends only onpropext,Classical.choiceandQuot.sound.AES.sboxBits_eq: the circuit on the bits of a byte equalssboxByte, checked for all 256 inputs bydecide +kernel.AES.sboxCircuit_lane: theUInt64circuit computes the Bool circuit in every lane.AES.subBytes_eq_spec : subBytes = subBytesSpecandAES.mixColumns_eq_spec : mixColumns = mixColumnsSpec. These rest onxtime_eq/xtime_xor_eq, which equatextimewithmul · 2andmul · 3for all 256 inputs.AES.encryptBlock_eq_spec,AES.expandKey_eq_specandAES.expandKey128_eq_spec: the cipher and both key schedules equal the table-based versions.GHash.mulCT_spec,GHash.load_toNat,GHash.store_eqandGHash.ghash_eq_spec : ghash h b = ghashSpec h b.Polyval.dotCT_spec,Polyval.load_toNat,Polyval.store_eqandPolyval.polyval_eq_spec : polyval h b = polyvalSpec h b.ConstantTime.ctEq_iff : ctEq a b = true ↔ a = bandctEq_eq_decide : ctEq a b = decide (a = b).Cipher.testalso runs the new code against the specs at runtime: AES-128 and AES-256 on 200 random keys and blocks, and GHASH and POLYVAL on 200 random keys and lengths.Tests
lake buildsucceeds (3575 jobs). Test output:Benchmarks
These come from
bench/(LeanBench,--min-time-ms 3000) on a shared 4-core machine at load average about 5, so the table gives minimum times. The spec rows run the old code in the same binary.Nat)Nat)The circuit S-box costs no more than the table did. AES time goes mostly to the array plumbing between rounds, which this PR leaves alone, and AES now accounts for nearly all of a GCM seal.