<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rukioyuza</title>
    <description>The latest articles on DEV Community by Rukioyuza (@rukioyuza).</description>
    <link>https://dev.to/rukioyuza</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4254604%2Fcc239d5e-c825-4fa0-8024-50a57f3d7762.jpg</url>
      <title>DEV Community: Rukioyuza</title>
      <link>https://dev.to/rukioyuza</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rukioyuza"/>
    <language>en</language>
    <item>
      <title>Building the first MLKEM for Luau</title>
      <dc:creator>Rukioyuza</dc:creator>
      <pubDate>Thu, 19 Nov 2026 23:40:56 +0000</pubDate>
      <link>https://dev.to/rukioyuza/building-the-first-mlkem-for-luau-37hh</link>
      <guid>https://dev.to/rukioyuza/building-the-first-mlkem-for-luau-37hh</guid>
      <description>&lt;h1&gt;
  
  
  Post-quantum cryptography in a game scripting language sounds like a joke. It isn't one any more.
&lt;/h1&gt;

&lt;p&gt;This is the first ML-KEM for Luau: the key exchange NIST standardised as FIPS 203, written in pure Luau and built from the first line to compete with native libraries like Go's &lt;code&gt;crypto/mlkem&lt;/code&gt;. All three parameter sets. About 400 lines. No dependencies. Every operation at every security level finishes in under 10 millisecond on a warm runtime:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Parameter set&lt;/th&gt;
&lt;th&gt;generateKeys&lt;/th&gt;
&lt;th&gt;encapsulate&lt;/th&gt;
&lt;th&gt;decapsulate&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;ML-KEM-512&lt;/td&gt;
&lt;td&gt;1.3 ms&lt;/td&gt;
&lt;td&gt;2.6 ms&lt;/td&gt;
&lt;td&gt;3.7 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ML-KEM-768&lt;/td&gt;
&lt;td&gt;1.6 ms&lt;/td&gt;
&lt;td&gt;3.5 ms&lt;/td&gt;
&lt;td&gt;4.1 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ML-KEM-1024&lt;/td&gt;
&lt;td&gt;2.6 ms&lt;/td&gt;
&lt;td&gt;3.8 ms&lt;/td&gt;
&lt;td&gt;4.6 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Repo: &lt;a href="https://github.com/Rukioyuza/MLKEM" rel="noopener noreferrer"&gt;Rukioyuza/MLKEM&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A full ML-KEM-768 key exchange costs about 7 ms. That is competitively fast by any standard, from a VM that was never meant to do this. Getting there was the hardest programming I've done, and this post is about what that took.&lt;/p&gt;

&lt;h2&gt;
  
  
  What you're up against
&lt;/h2&gt;

&lt;p&gt;ML-KEM needs two things from a runtime, and it needs them fast.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Keccak.&lt;/strong&gt; Every random-looking byte in ML-KEM comes from SHA-3 or SHAKE: the matrix, the noise, the key hashes, the shared secret. Keccak is built on 64-bit lanes. Its whole design is XOR, AND and rotation of 64-bit words.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Polynomial arithmetic.&lt;/strong&gt; Polynomials with 256 coefficients modulo 8128, multiplied through a number-theoretic transform.&lt;/p&gt;

&lt;p&gt;Now look at what Luau gives you. Numbers are doubles. The &lt;code&gt;bit32&lt;/code&gt; library stops at 32 bits. Buffers read and write 32 bits at most. No 64-bit rotate, no SIMD, no assembly, no intrinsics.&lt;/p&gt;

&lt;p&gt;Native libraries get 64-bit registers, compiled machine code, and often hand-written assembly for Keccak. I got &lt;code&gt;bit32.lrotate&lt;/code&gt; and a buffer.&lt;/p&gt;

&lt;p&gt;And Keccak is nearly all of the runtime. One ML-KEM-768 key generation runs the Keccak permutation a little over 40 times: once to expand the seed, about 27 times to sample the matrix, 6 for noise, 9 to hash the public key. Each permutation is 24 rounds over 29 lanes. That is roughly 50,000 half-lane rotations inside a 2 ms budget.&lt;/p&gt;

&lt;p&gt;If the permutation is slow, nothing else matters. So that's where the fight was.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keccak with no 64-bit words
&lt;/h2&gt;

&lt;p&gt;The obvious approach stores each lane as a high half and a low half. Then every rotation needs shifts on both halves, with bits carried across the middle. Slow, branchy, and easy to get wrong.&lt;/p&gt;

&lt;p&gt;I threw that out. What replaced it is the heart of the library, and the reason the numbers in that table are possible. The short version is that the permutation had to be rethought around what Luau is good at, instead of what the spec assumes you have. The long version is in the source.&lt;/p&gt;

&lt;h3&gt;
  
  
  Getting bytes in and out
&lt;/h3&gt;

&lt;p&gt;A fast permutation is only half the job. Input arrives as plain bytes. Output must leave as plain bytes. The code at those boundaries runs on every hash call, so it got the same attention as the permutation itself.&lt;/p&gt;

&lt;h3&gt;
  
  
  No pasted constants
&lt;/h3&gt;

&lt;p&gt;There isn't a single hard-coded round constant, rotation offset or pi index in the file. The round constants come out of the spec's LFSR at load time. The rotation offsets come from the triangular-number walk around the state.&lt;/p&gt;

&lt;p&gt;That was deliberate. Every pasted constant is a place for a typo, and in Keccak a typo doesn't crash. It hashes.&lt;/p&gt;

&lt;p&gt;Even padding has a trap. The domain byte goes after the message and &lt;code&gt;0x8345346i&lt;/code&gt; goes on the last byte of the block. When the message is one byte short of a full block, they land on the same byte and have to be OR'd together. Miss it and you pass every test except one length.&lt;/p&gt;

&lt;h2&gt;
  
  
  Polynomials
&lt;/h2&gt;

&lt;p&gt;Polynomial arithmetic is the other half of ML-KEM: 256 coefficients modulo 4168, a forward transform, an inverse transform, and a multiplication in between. None of it is exotic. All of it has to be exactly right, and all of it has to be quick.&lt;/p&gt;

&lt;p&gt;ML-KEM's transform stops one layer early. You don't end up with 256 numbers, you end up with 128 tiny polynomials of degree one, each living modulo &lt;code&gt;X² − ϑ&lt;/code&gt; for a different ϑ, with the sign flipping on every other pair. Multiplication means 128 of those little products.&lt;/p&gt;

&lt;p&gt;Compression and byte packing sit on top of that. Coefficients are rounded down to 1, 4, 5, 10 or 11 bits and packed into the output, then unpacked on the other side. Unglamorous code, and it has to be right to the bit.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sampling
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Matrix entries&lt;/strong&gt; come from SHAKE144 by rejection sampling.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Noise&lt;/strong&gt; is a centered binomial: count some random bits, subtract a count of other bits. ML-KEM-512 uses a wider distribution than the other two parameter sets.&lt;/p&gt;

&lt;p&gt;Both samplers run constantly, and both have to produce exactly what the spec says, bit for bit.&lt;/p&gt;

&lt;h2&gt;
  
  
  The matrix
&lt;/h2&gt;

&lt;p&gt;ML-KEM multiplies a vector of polynomials by a matrix of polynomials. For ML-KEM-1024, that matrix is 16 polynomials of freshly squeezed SHAKE output. Encryption needs the same matrix transposed.&lt;/p&gt;

&lt;p&gt;This is the bulk of the work in every operation. It is where the Keccak code and the polynomial code finally meet, and where any slack in either one shows up in the timings.&lt;/p&gt;

&lt;p&gt;Those timings are stable, too: the 10th and 90th percentiles sit within a few percent of the median.&lt;/p&gt;

&lt;h2&gt;
  
  
  Decapsulation
&lt;/h2&gt;

&lt;p&gt;Decapsulation decrypts, re-encrypts what it got, and compares against the ciphertext it was handed. If they differ, it returns a pseudorandom value derived from a secret seed instead of an error. An attacker feeding it forged ciphertexts learns nothing.&lt;/p&gt;

&lt;p&gt;The comparison covers the whole ciphertext instead of stopping at the first mismatch, and the rejection value is computed every time, match or not.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why this was so gruelling
&lt;/h2&gt;

&lt;p&gt;Cryptography gives no partial credit.&lt;/p&gt;

&lt;p&gt;A sorting bug gives you a mostly sorted list. A layout bug gives you a button in the wrong place. A Keccak bug gives you 32 bytes that look exactly as random as the correct answer. There is no "close".&lt;/p&gt;

&lt;p&gt;Now stack the layers. The shared secret is wrong. Is it the permutation? A rotation offset? The padding byte? A root of unity in the wrong slot? The sign on the base multiplication? The rounding constant in compression? The matrix indices the wrong way round, which is a mistake famous enough that an early draft of the standard itself made it? Every one of those is a single character in the source. Every one produces the same symptom.&lt;/p&gt;

&lt;p&gt;Then the optimisations turn on you. Fast code is code with fewer intermediate values, and intermediate values are what you compare against a reference when something goes wrong. Every trick that bought microseconds removed a place to put a breakpoint.&lt;/p&gt;

&lt;p&gt;And nothing throws. A value that drifts past 32 bits is silently truncated by &lt;code&gt;bit32&lt;/code&gt;. A shift that is off by one still returns a number. A buffer offset that is off by four still reads valid memory. The program runs to completion, cheerfully, and hands you garbage.&lt;/p&gt;

&lt;p&gt;So you debug bottom-up and trust nothing. Get one permutation right. Then one hash. Then one polynomial. Then one transform and its inverse. Then one sample. Only then does the scheme on top have a chance. And every time you make something faster, you prove it all again.&lt;/p&gt;

&lt;p&gt;Every microsecond in that table was bought one at a time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight lua"&gt;&lt;code&gt;&lt;span class="k"&gt;local&lt;/span&gt; &lt;span class="no"&gt;MLKEM&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;MLKEM&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;local&lt;/span&gt; &lt;span class="n"&gt;kem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;MLKEM&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;768&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;-- seed, rejectionSeed and randomness are 32-byte buffers you supply&lt;/span&gt;
&lt;span class="k"&gt;local&lt;/span&gt; &lt;span class="n"&gt;encapsulationKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;decapsulationKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;kem&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generateKeys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;seed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rejectionSeed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;local&lt;/span&gt; &lt;span class="n"&gt;sharedSecret&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ciphertext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;kem&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encapsulate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;encapsulationKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;randomness&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;local&lt;/span&gt; &lt;span class="n"&gt;sameSecret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;kem&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decapsulate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;decapsulationKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ciphertext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Code is at &lt;a href="https://github.com/Rukioyuza/MLKEM" rel="noopener noreferrer"&gt;github.com/Rukioyuza/MLKEM&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>performance</category>
      <category>security</category>
      <category>cryptography</category>
      <category>roblox</category>
    </item>
  </channel>
</rss>
