Skip to main content

What the "Silicon Valley" Easter Egg code does and how

In the TV series Silicon Valley there was apparently a snippet of code presented that is part of a compression algorithm. The code (or at least part of it) can be executed and the the program will output:

DREAM_ON_ASSHOLES

The code itself has been published first as a screen shot, then as text and got some press. But I hadn't seen a good explanation of how it works. It looks pretty complicated but most of the code (written in C) is unused and the part that is actually executed is pretty simple (if you've spent years coding in C-like languages). Here it as presented:

#include <stdio.h>
#include <stdlib.h>

typedef unsigned long u64;

/* Start here */
typedef void enc_cfg_t;
typedef int enc_cfg2_t;
typedef __int128_t dcf_t;

enc_cfg_t _ctx_iface(dcf_t s, enc_cfg2_t i){
	int c = (((s & ((dcf_t)0x1FULL << i * 5)) >> i * 5) + 65);
	printf("%c", c); }
	enc_cfg2_t main() {
	for (int i=0; i<17; i++){
		_ctx_iface(0x79481E6BBCC01223 + ((dcf_t)0x1222DC << 64), i);
	}
}
/* End here */

The writers have tried to make this look more complicated by using a bunch of typedefs and deliberately messing with indentation so that the main() function is kind of hidden.  After a bit of clean up the code looks like this:

#include <stdio.h>
#include <stdlib.h>

void _ctx_iface(__int28_t s, int i){
	int c = (((s & ((__int128_t))0x1FULL << i * 5)) >> i * 5) + 65);
	printf("%c", c); 
}
void main() {
	for (int i=0; i<17; i++){
		_ctx_iface(0x79481E6BBCC01223 + ((__int128_t)0x1222DC << 64), i);
	}
}

So, main() calls _ctx_iface() 17 times with i set to 0, 1, ... 16 and passed in a weird looking number 0x79481E6BBCC01223 + ((__int128_t)0x1222DC << 64) which seems to have been obscured a bit with a left shift of 64 bits and an addition. The parameter is actually just 0x1222DC79481E6BBCC01223. Still looks odd but it's pretty simple: that's DREAM_ON_ASSHOLES encoded where each 5 bits indicating one upper case character or symbol (A = 0, B = 1, C= 2, ...).

If you express that number in binary you can separate it out into five bit chunks like this: 10010 00100 01011 01110 00111 10010 10010 00000 11110 01101 01110 11110 01100 00000 00100 10001 00011. You can even manually discover that this represents S E L O H S S A _ N O _ M A E R D if you take a guess that 11110 (30 = _).

The _ctx_iface() function extracts each character of DREAM_IN_ASSHOLES (17 characters) one by one and prints them. If does that with the code (((s & ((dcf_t)0x1FULL << i * 5)) >> i * 5) + 65) which uses i to indicate which 5 bits to extract and then shifts 0x1f (which is 11111 in binary) to the right position in the string, does a binary AND to extract just those five bits and then shifts the five bits back so that they become a number between 0 and 31.

The +65 turns a number between 0 and 31 into the characters ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_ because 65 is the ASCII character for A. Note that this encoding also explains why the program outputs DREAM_ON_ASSHOLES and not DREAM ON ASSHOLES. There's no way for the encoding to result in a space (which is ASCII 32).

All a bit of a disappointment really.

The rest of the code is more fun. There's a function called HammingCtr which computes the Hamming Weight of a binary string and even references and uses bit twiddling tricks from seander to make the code run quickly.

The other function is ConvolutedMagic which I have yet to decipher. Readers?

Comments

Lukas Rusak said…
You have some errors in your code.

The first __int28_t should be __int128_t.
The line
int c = (((s & ((__int128_t))0x1FULL << i * 5)) >> i * 5) + 65);

should be
int c = (((s & ((__int128_t)0x1FULL << i * 5)) >> i * 5) + 65);

notice the removed parentheses .

So the whole code should be
#include
#include

void _ctx_iface(__int128_t s, int i){
int c = (((s & ((__int128_t)0x1FULL << i * 5)) >> i * 5) + 65);
printf("%c", c);
}

void main() {
for (int i=0; i<17; i++){
_ctx_iface(0x79481E6BBCC01223 + ((__int128_t)0x1222DC << 64), i);
}
}

then for the kids you can compile it with (where silicon_valley.c contains the code)
gcc -o silicon_valley silicon_valley.c

Popular posts from this blog

Your last name contains invalid characters

My last name is "Graham-Cumming". But here's a typical form response when I enter it:


Does the web site have any idea how rude it is to claim that my last name contains invalid characters? Clearly not. What they actually meant is: our web site will not accept that hyphen in your last name. But do they say that? No, of course not. They decide to shove in my face the claim that there's something wrong with my name.

There's nothing wrong with my name, just as there's nothing wrong with someone whose first name is Jean-Marie, or someone whose last name is O'Reilly.

What is wrong is that way this is being handled. If the system can't cope with non-letters and spaces it needs to say that. How about the following error message:

Our system is unable to process last names that contain non-letters, please replace them with spaces.

Don't blame me for having a last name that your system doesn't like, whose fault is that? Saying "Your last name …

Importing an existing SSL key/certificate pair into a Java keystore

I'm writing this blog post in case anyone else has to Google that. In Java 6 keytool has been improved so that it now becomes possible to import an existing key and certificate (say one you generated outside of the Java world) into a keystore.

You need: Java 6 and openssl.

1. Suppose you have a certificate and key in PEM format. The key is named host.key and the certificate host.crt.

2. The first step is to convert them into a single PKCS12 file using the command: openssl pkcs12 -export -in host.crt -inkey host.key > host.p12. You will be asked for various passwords (the password to access the key (if set) and then the password for the PKCS12 file being created).

3. Then import the PKCS12 file into a keystore using the command: keytool -importkeystore -srckeystore host.p12 -destkeystore host.jks -srcstoretype pkcs12. You now have a keystore named host.jks containing the certificate/key you need.

For the sake of completeness here's the output of a full session I performe…

More fun with toys: the Ikea LILLABO Train Set

As further proof of my unsuitability to be a child minder (see previous post) I found myself playing with an Ikea LILLABO 20-piece basic set train.


The train set has 16 pieces of track (12 curves, two straight pieces and a two part bridge) and 4 pieces of train. What I wondered was... how many possible looping train tracks can be made using all 16 pieces?

The answer is... 9. Here's a picture of the 9 different layouts.


The picture was generated using a little program written in Processing. The bridge is red, the straight pieces are green and the curves are blue or magenta depending on whether they are oriented clockwise or anticlockwise. The curved pieces can be oriented in either way.

To generate those layouts I wrote a small program which runs through all the possible layouts and determines which form a loop. The program eliminates duplicate layouts (such as those that are mirror images of each other).

It outputs a list of instructions for building loops. These instructions con…