The 3DES subdirectory in the tutorials contains a complete Verilog triple-DES implementation. The RTL source code is in tutorial/3DES/rtl; the testbench is in tutorial/3DES/test. The original Verilog testbench is tutorial/3DES/test/des_kats.v, while the Maia equivalent is tutorial/3DES/test/des_kats.tv.
The validation of a triple-DES implementation is specified by the NIST in publication 800-20, as a set of "known answer" tests. The data files in the test/vectors subdirectory contain the known answer tests, as lists of key values, "plaintext", and "ciphertext". The Verilog and Maia testbenches simply read these files, apply the necessary inputs to the DUT (the key and the plaintext for encryption, or the ciphertext for decryption), and confirm that the DUT later produces the required output (the ciphertext for encryption, or the plaintext for decryption). The test/README file contains instructions for running both the Verilog and the Maia versions of the testbench. When running on Icarus, with a 2.4GHz processor, both testbenches complete in under 5 minutes, with the Maia testbench being marginally faster than the Verilog equivalent.
des_kats.tv introduces file I/O. In C and related languages, there is no specific language support for file I/O: files are handled externally and explicitly, by a standard library. Maia, however, has a dedicated stream data type, which greatly simplifies I/O operations for the cases which are commonly used in testbenches. It is not necessary to open or close files, or to position them, or to parse data lines to strip out comments and extract data fields; these operations are handled automatically and transparently.
The stream which contains the "variable plaintext known answer" data is declared as follows, on line 55:
stream { mode 1; file "vectors/des_vpkat.dat"; format "%i %64'h %64'h", round, plain, cipher; } vpkat;
This declares stream object vpkat, which is associated with the
named file. Each line of this file, after any comments and whitespace are
stripped, contains 3 data fields; a generic integer, and two 64-bit hex
integers. The file is automatically opened and closed when it comes into, or
leaves, context. The stream can be positioned to a given data line by
assigning an integer to it. vpkat = 10
, for example, sets the
stream to the eleventh data line, while vpkat++ moves the stream on
to the next data line. vpkat.round, vpkat.plain,
and vpkat.cipher contain the values of the named fields in the
current line.
des_kats.tv contains one other feature of note, which is that the streams are advanced not by explicit assignment operations (such as vpkat++), but by the for all construct, which was briefly introduced in tut11.tv. Its use here is analagous to its use in tut11.tv for cycling through all possible values of a numeric data type. In this case, however, it cycles through all possible values of the stream; in other words, it sets the stream to its first line, and then increments through the stream, until it reaches the last line.