1 /**************************************************************************
3 * Java Grande Forum Benchmark Suite - Thread Version 1.0 *
7 * Java Grande Benchmarking Project *
11 * Edinburgh Parallel Computing Centre *
13 * email: epcc-javagrande@epcc.ed.ac.uk *
16 * This version copyright (c) The University of Edinburgh, 2001. *
17 * All rights reserved. *
19 **************************************************************************/
20 /**************************************************************************
21 * Ported for DSTM Benchmark *
22 **************************************************************************/
24 public class JGFCryptBench {
27 private int datasizes[];
30 byte [] plain1; // Buffer for plaintext data.
31 byte [] crypt1; // Buffer for encrypted data.
32 byte [] plain2; // Buffer for decrypted data.
34 short [] userkey; // Key for encryption/decryption.
35 int [] Z; // Encryption subkey (userkey derived).
36 int [] DK; // Decryption subkey (userkey derived).
41 //Builds the data used for the test -- each time the test is run.
46 // Create three byte arrays that will be used (and reused) for
47 // encryption/decryption operations.
50 plain1 = global new byte [array_rows];
51 crypt1 = global new byte [array_rows];
52 plain2 = global new byte [array_rows];
55 Random rndnum = global new Random(136506717L); // Create random number generator.
58 // Allocate three arrays to hold keys: userkey is the 128-bit key.
59 // Z is the set of 16-bit encryption subkeys derived from userkey,
60 // while DK is the set of 16-bit decryption subkeys also derived
61 // from userkey. NOTE: The 16-bit values are stored here in
62 // 32-bit int arrays so that the values may be used in calculations
63 // as if they are unsigned. Each 64-bit block of plaintext goes
64 // through eight processing rounds involving six of the subkeys
65 // then a final output transform with four of the keys; (8 * 6)
68 userkey = global new short [8]; // User key has 8 16-bit shorts.
69 Z = global new int [52]; // Encryption subkey (user key derived).
70 DK = global new int [52]; // Decryption subkey (user key derived).
72 // Generate user key randomly; eight 16-bit values in an array.
74 for (int i = 0; i < 8; i++)
76 // Again, the random number function returns int. Converting
77 // to a short type preserves the bit pattern in the lower 16
78 // bits of the int and discards the rest.
80 userkey[i] = (short) rndnum.nextInt();
83 // Compute encryption and decryption subkeys.
88 // Fill plain1 with "text."
89 for (int i = 0; i < array_rows; i++)
93 // Converting to a byte
94 // type preserves the bit pattern in the lower 8 bits of the
95 // int and discards the rest.
102 // Builds the 52 16-bit encryption subkeys Z[] from the user key and
103 //stores in 32-bit int array. The routing corrects an error in the
104 //source code in the Schnier book. Basically, the sense of the 7-
105 //and 9-bit shifts are reversed. It still works reversed, but would
106 //encrypted code would not decrypt with someone else's IDEA code.
109 private void calcEncryptKey()
111 int j; // Utility variable.
113 for (int i = 0; i < 52; i++) // Zero out the 52-int Z array.
116 for (int i = 0; i < 8; i++) // First 8 subkeys are userkey itself.
118 Z[i] = userkey[i] & 0xffff; // Convert "unsigned"
122 // Each set of 8 subkeys thereafter is derived from left rotating
123 // the whole 128-bit key 25 bits to left (once between each set of
124 // eight keys and then before the last four). Instead of actually
125 // rotating the whole key, this routine just grabs the 16 bits
126 // that are 25 bits to the right of the corresponding subkey
127 // eight positions below the current subkey. That 16-bit extent
128 // straddles two array members, so bits are shifted left in one
129 // member and right (with zero fill) in the other. For the last
130 // two subkeys in any group of eight, those 16 bits start to
131 // wrap around to the first two members of the previous eight.
133 for (int i = 8; i < 52; i++)
139 Z[i] = ((Z[i -7]>>>9) | (Z[i-6]<<7)) // Shift and combine.
140 & 0xFFFF; // Just 16 bits.
141 //continue; // Next iteration.
148 if (j == 6) // Wrap to beginning for second chunk.
150 Z[i] = ((Z[i -7]>>>9) | (Z[i-14]<<7))
157 // j == 7 so wrap to beginning for both chunks.
158 Z[i] = ((Z[i -15]>>>9) | (Z[i-14]<<7))
168 //Builds the 52 16-bit encryption subkeys DK[] from the encryption-
169 //subkeys Z[]. DK[] is a 32-bit int array holding 16-bit values as
173 private void calcDecryptKey()
175 int j, k; // Index counters.
176 int t1, t2, t3; // Temps to hold decrypt subkeys.
178 t1 = inv(Z[0]); // Multiplicative inverse (mod x10001).
179 t2 = - Z[1] & 0xffff; // Additive inverse, 2nd encrypt subkey.
180 t3 = - Z[2] & 0xffff; // Additive inverse, 3rd encrypt subkey.
182 DK[51] = inv(Z[3]); // Multiplicative inverse (mod x10001).
187 j = 47; // Indices into temp and encrypt arrays.
189 for (int i = 0; i < 7; i++)
195 t2 = -Z[k++] & 0xffff;
196 t3 = -Z[k++] & 0xffff;
197 DK[j--] = inv(Z[k++]);
207 t2 = -Z[k++] & 0xffff;
208 t3 = -Z[k++] & 0xffff;
209 DK[j--] = inv(Z[k++]);
219 // Performs multiplication, modulo (2**16)+1. This code is structured
220 // on the assumption that untaken branches are cheaper than taken
221 // branches, and that the compiler doesn't schedule branches.
222 // Java: Must work with 32-bit int and one 64-bit long to keep
223 // 16-bit values and their products "unsigned." The routine assumes
224 // that both a and b could fit in 16 bits even though they come in
225 // as 32-bit ints. Lots of "& 0xFFFF" masks here to keep things 16-bit.
226 // Also, because the routine stores mod (2**16)+1 results in a 2**16
227 // space, the result is truncated to zero whenever the result would
228 // zero, be 2**16. And if one of the multiplicands is 0, the result
229 // is not zero, but (2**16) + 1 minus the other multiplicand (sort
230 // of an additive inverse mod 0x10001).
232 // NOTE: The java conversion of this routine works correctly, but
233 // is half the speed of using Java's modulus division function (%)
234 // on the multiplication with a 16-bit masking of the result--running
235 // in the Symantec Caje IDE. So it's not called for now; the test
236 // uses Java % instead.
239 private int mul(int a, int b)
242 long p; // Large enough to catch 16-bit multiply
243 // without hitting sign bit.
249 b = (int) p & 0xFFFF; // Lower 16 bits.
250 a = (int) p >>> 16; // Upper 16 bits.
252 return (b - a + 1) & 0xFFFF;
254 return (b - a) & 0xFFFF;
257 return ((1 - a) & 0xFFFF); // If b = 0, then same as
260 else // If a = 0, then return
261 return((1 - b) & 0xFFFF); // same as 0x10001 - b.
267 // Compute multiplicative inverse of x, modulo (2**16)+1 using
268 // extended Euclid's GCD (greatest common divisor) algorithm.
269 // It is unrolled twice to avoid swapping the meaning of
270 // the registers. And some subtracts are changed to adds.
271 // Java: Though it uses signed 32-bit ints, the interpretation
272 // of the bits within is strictly unsigned 16-bit.
275 private int inv(int x)
280 if (x <= 1) // Assumes positive x.
281 return(x); // 0 and 1 are self-inverse.
283 t1 = 0x10001 / x; // (2**16+1)/x; x is >= 2, so fits 16 bits.
286 return((1 - t1) & 0xFFFF);
293 if (x == 1) return(t0);
299 return((1 - t1) & 0xFFFF);
305 // Nulls arrays and forces garbage collection to free up memory.
308 void freeTestData(int array_rows)
310 for(int i = 0; i<array_rows; i++) {
311 plain1[i] = (byte) 0;
312 crypt1[i] = (byte) 0;
313 plain2[i] = (byte) 0;
316 for(int i = 0; i<8; i++) {
317 userkey[i] = (short) 0;
320 for(int i = 0; i<52; i++) {
325 //System.gc(); // Force garbage collection.
329 public JGFCryptBench()
331 datasizes = global new int[3];
332 datasizes[0] = 3000000;
333 datasizes[1] = 20000000;
334 datasizes[2] = 50000000;
338 public void JGFsetsize(int size){
342 public void JGFinitialise(){
343 array_rows = datasizes[size];
348 public void JGFkernel(){
353 public void JGFvalidate(){
357 for (int i = 0; i < array_rows; i++){
358 error = (plain1 [i] != plain2 [i]);
360 //System.printString("Validation failed");
361 //System.printString("Original Byte " + i + " = " + plain1[i]);
362 //System.printString("Encrypted Byte " + i + " = " + crypt1[i]);
363 //System.printString("Decrypted Byte " + i + " = " + plain2[i]);
371 public void JGFtidyup(){
372 freeTestData(array_rows);
376 public void JGFrun(int size){
377 instr.addTimer("Section2:Crypt:Kernel", "Kbyte",size);
383 instr.addOpsToTimer("Section2:Crypt:Kernel", (2*array_rows)/1000.);
384 instr.printTimer("Section2:Crypt:Kernel");