AES Implementation in RPython

20Dec09

I implemented the Advanced Encryption Standard (AES)[4] using Restricted Python (RPython)[1], which is a subset of the standard Python programming language and comes from the PyPy[2] project. By using RPython, we can write our code in high level programming language similar to Python and later be translated to various backends such as C, Java Virtual Machine (JVM), or Common Language Infrastructure (CLI). As a result, we can develop our programs very quickly and get the performance similar to the one written in pure C, Java, or C# .NET.

However, RPython is not as powerful as standard CPython, so that’s why it is called “Restricted” Python. There are some restrictions in RPython. For example, you can’t use most Python packages comes from the CPython world because they are not written in RPython. Also, translated code are compiled to executables, not compiled Python modules, neither Python packages. Fortunately, these restrictions are trivial in this simple program because I’m intend to create executables and I don’t need libraries not supported by PyPy. Most importantly, performance matters.

In my experiments, to encrypt and decrypt a file on my iMac (Early 2008), the translated C executable is about 150 times faster than the pure Python code running on top of CPython, and the translated Java bytecode is about 16 times faster. Consequently, RPython should be very useful in case we don’t need serious dependencies. We just need to figure out a better way to integrate it with pure Python code, much like the C extensions for Python.

The AES algorithm implemented in this project is mostly following the AES Tutorial[3] and the Advanced Encryption Standard[4]; the values of Rijndael’s S-box and Inverse S-box are copied from [5]; and the implementation of Rijndael key schedule is following [6].

To test the program, you can use executables put under the bin directory shipped with the source code. There are 4 executables compiled in C, one compiled to Java bytecode as a JAR file (JVM), and one translated to C# and compiled for .NET framework (CLI). Now say that you want to encrypt a file under Mac OS X using the C executable. You can do this by:

bin/aes-mac-c  encrypt  mypassword  path_to_input_file  path_to_encrypted_file



To decrypt the file on a Java Virtual Machine, you can then run:

java  -jar  bin/aes-jvm.jar  decrypt  mypassword  path_to_encrypted_file  path_to_decrypted_file



You can also run the pure Python code on top of CPython or other Python implementation, but you’ll need to get the PyPy’s source code. You can find its repository at their website[2].

Here’s some benchmarks I made on Snow Leopard, Windows XP, and Gentoo Linux respectively.

  1. iMac (Early 2008)
    • Mac OS X 10.6.2 (Snow Leopard)
    • 2.8 GHz Intel Core 2 Duo
    • 4 GB 800 MHz DDR2 SDRAM
    • GCC version: 4.2.1
    • CPython version: 2.6.1
    • Java version: 1.6.0_17 (JVM)
    • Mono version: 2.6.1 (CLI)

  2. Windows XP 32-bit
    • Running in VirtualBox on iMac (Early 2008)
    • 1 GB RAM
    • Microsoft Visual Studio 2008 Express Edition
    • Java version: 1.6.0_17
    • CPython version: 2.6.4

  3. Gentoo Linux 2008 (amd64)
    • AMD Athlon 64 Processor 3500+
    • 2GB DDR 400 SDRAM
    • GCC version: 4.1.2
    • Java version: 1.6.0_17
    • Mono version: 2.4.2.3
    • CPython version: 2.6.4

References:
[1] Restricted Python: http://codespeak.net/pypy/dist/pypy/doc/coding-guide.html#restricted-python
[2] PyPy: http://codespeak.net/pypy/
[3] AES Tutorial: http://www.progressive-coding.com/tutorial.php
[4] Advanced Encryption Standard: http://en.wikipedia.org/wiki/Advanced_Encryption_Standard
[5] Rijndael S-box: http://en.wikipedia.org/wiki/Rijndael_S-box
[6] Rijndael key schedule: http://en.wikipedia.org/wiki/Rijndael_key_schedule

Advertisement


3 Responses to “AES Implementation in RPython”

  1. 1 Martian

    Probably it would be a good idea to add to the comparison the pure Python code running on the top of the compiled PyPy interpreter without and especially with the JIT enabled. How much faster will RPython be than JIT-compiled code?

    • Hi, pure Python code running on top of pypy-c have been added to the benchmark, pypy-c-jit has been added as well, but only on the Linux, because I still can’t get the jit backend compiled successfully on Snow Leopard and Windows XP… :(


  1. 1 uberVU - social comments

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.