Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Rijndael's S-box is a frequently used operation in AES encryption and decryption. It is typically implemented as a 256-byte lookup table. That's fast, but means you need to enumerate a 256-byte lookup table in your code. I bet someone in this crowd could do it with less code, given the underlying mathematical structure.

Write a function in your favorite language that implements Rijndael's S-box. Shortest code wins.

share|improve this question
Bonus points (upvotes from me) if the resulting function is constant-time (i.e. no data-dependent code paths or array accesses or whatever your language supports). –  Paŭlo Ebermann Oct 4 '11 at 8:08
@PaŭloEbermann array accesses are constant time in many languages (it's adding a (scaled) value to a pointer and dereferencing it, this is why a lookup table is so very fast) –  ratchet freak Oct 4 '11 at 8:58
@ratchetfreak Array accesses are O(1), but the actual access time depends on cache hits or misses, for example, which leads to side-channel attacks on AES. –  Paŭlo Ebermann Oct 4 '11 at 9:01
@PaŭloEbermann, but you can use the shorter code to fill a lookup table, which will then fit in well under a page of memory. –  Peter Taylor Oct 4 '11 at 9:32
@PaŭloEbermann and if the 256-length table is stored along the code (as enum generated at compile time) you nearly guaranteed a cache hit –  ratchet freak Oct 4 '11 at 9:35
show 2 more comments

5 Answers

up vote 2 down vote accepted

Ruby, 161 characters


In order to check the output you can use the following code to print it in tabular form:

S.map{|x|"%02x"%x}.each_slice(16){|l|puts l*' '}
share|improve this answer
add comment

The table can be generated without computing inverses in the finite field GF(256), by using logarithms. It would look like this (Java code, using int to avoid problems with the signed byte type):

int[] t = new int[256];
for (int i = 0, x = 1; i < 256; i ++) {
    t[i] = x;
    x ^= (x << 1) ^ ((x >>> 7) * 0x11B);
int[] S = new int[256];
S[0] = 0x63;
for (int i = 0; i < 255; i ++) {
    int x = t[255 - i];
    x |= x << 8;
    x ^= (x >> 4) ^ (x >> 5) ^ (x >> 6) ^ (x >> 7);
    S[t[i]] = (x ^ 0x63) & 0xFF;

The idea is that 3 is a multiplicative generator of GF(256)*. The table t[] is such that t[x] is equal to 3x; since 3255 = 1, we get that 1/(3x) = 3255-x.

share|improve this answer
shouldn't it be 0x1B (one 1 in the hex literal) instead of 0x11B –  ratchet freak Oct 4 '11 at 12:31
@ratchetfreak: no, it must be 0x11B (I tried). The int type is 32-bit in Java; I must cancel out the higher bit. –  Thomas Pornin Oct 4 '11 at 12:36
ah didn't realize that –  ratchet freak Oct 4 '11 at 12:47
Is that a >>> instead of a >> in line 4? –  Joe Z. Jan 22 '13 at 17:22
@JoeZeng: both would work. In Java, '>>>' is the "unsigned shift", '>>' is the "signed shift". They differ by how they handle the sign bit. Here, the values will never be wide enough for the sign bit to be non-zero, so it makes no real difference. –  Thomas Pornin Jan 22 '13 at 17:39
show 1 more comment


ubyte[256] getLookup(){

    ubyte[256] t=void;
        t[i] = x;
        x ^= (x << 1) ^ ((x >>> 7) * 0x1B);
    ubyte[256] S=void;
    S[0] = 0x63;
        int x = t[255 - i];
        x |= x << 8;
        x ^= (x >> 4) ^ (x >> 5) ^ (x >> 6) ^ (x >> 7);
        S[t[i]] = cast(ubyte)(x & 0xFF) ^ 0x63 ;
    return S;


this can generate the lookup table at compile time, I could save some by making ubyte a generic param

edit direct ubyte to ubyte without array lookups, no branching and fully unrollable loops

B[256] S(B:ubyte)(B i){
    B mulInv(B x){
        B r;
            B p=0,h,a=i,b=x;
                a^=h*0x1b;//h is 0 or 1
            if(p==1)r=i;//happens 1 or less times over 256 iterations
        return r;
    B s= x=mulInv(i);
    return x^99;

edit2 used @Thomas' algo for creating the lookup table

share|improve this answer
add comment

Python, 176 chars

This answer is for Paŭlo Ebermann's comment-question about making the function constant time. This code fits the bill.

def S(x):
 for y in range(256):
  for j in range(8):p^=b%2*a;a*=2;a^=a/256*283;b/=2
share|improve this answer
add comment

GolfScript (82 chars)


Uses global variables A and B, and creates the function as global variable S.

The Galois inversion is brute-force; I experimented with having a separate mul function which could be reused for the post-inversion affine transform, but it turned out to be more expensive because of the different overflow behaviour.

This is too slow for an online demo - it would time out even on the first two rows of the table.

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.