pi_aes.cpp (3841B)
1 /* 2 Copyright 2010-2011, D. E. Shaw Research. 3 All rights reserved. 4 5 Redistribution and use in source and binary forms, with or without 6 modification, are permitted provided that the following conditions are 7 met: 8 9 * Redistributions of source code must retain the above copyright 10 notice, this list of conditions, and the following disclaimer. 11 12 * Redistributions in binary form must reproduce the above copyright 13 notice, this list of conditions, and the following disclaimer in the 14 documentation and/or other materials provided with the distribution. 15 16 * Neither the name of D. E. Shaw Research nor the names of its 17 contributors may be used to endorse or promote products derived from 18 this software without specific prior written permission. 19 20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 #include <Random123/aes.h> 33 #include <stdio.h> 34 #include <assert.h> 35 using namespace r123; 36 37 // Everyone's favorite PRNG example: calculate pi/4 by throwing darts 38 // at a square board and counting the fraction that are inside the 39 // inscribed circle. 40 41 // This version uses the C++ API to AESNI. 42 43 #include "pi_check.h" 44 #include "example_seeds.h" 45 46 int main(int, char **){ 47 #if R123_USE_AES_NI 48 unsigned long seed = example_seed_u32(EXAMPLE_SEED1_U32); // example user-settable seed 49 unsigned long hits = 0, tries = 0; 50 const int64_t two_to_the_62 = ((int64_t)1)<<62; 51 52 if (!haveAESNI()) { 53 std::cerr << "AES-NI instructions not available on this hardware, skipping the pi_aes test." << std::endl; 54 return 0; 55 } 56 typedef AESNI4x32 G; 57 G generator; 58 // As an example, we illustrate one user-provided seed word and the rest as arbitrary constants 59 G::ukey_type ukey = {{(G::ukey_type::value_type)seed, EXAMPLE_SEED2_U32, EXAMPLE_SEED3_U32, EXAMPLE_SEED4_U32}}; 60 // The key_type constructor transforms the 128bit AES ukey_type to an expanded (1408bit) form. 61 G::key_type key = ukey; 62 // start ctr from an arbitrary point. 0 would work fine too, this is just to show that it does 63 // not matter where it starts from (or for that matter, whether it increments by 1, or some other 64 // arbitrary stride or in some other way, as long as it never repeats a key,ctr combination) 65 G::ctr_type ctr = {{EXAMPLE_SEED5_U32, EXAMPLE_SEED6_U32, EXAMPLE_SEED7_U32, EXAMPLE_SEED8_U32}}; 66 67 printf("Throwing %lu darts at a square board using AESNI4x32\n", NTRIES); 68 std::cout << "Initializing AES key with hex userkey: " << std::hex << ukey << " ctr: " << ctr << std::endl; 69 70 while(tries < NTRIES){ 71 ctr.incr(); 72 G::ctr_type r = generator(ctr, key); 73 if (tries == 0) { 74 std::cout << "first random from AESNI is " << std::hex << r << std::endl;; 75 } 76 for(size_t j=0; j<r.size(); j+=2){ 77 int64_t x = (int32_t)r[j]; 78 int64_t y = (int32_t)r[j+1]; 79 if( (x*x + y*y) < two_to_the_62 ) 80 hits++; 81 tries++; 82 } 83 } 84 return pi_check(hits, tries); 85 #else 86 std::cout << "AESNI RNG not compiled into this binary, skipping the pi_aes test.\n"; 87 return 0; // Not a failure to not have AESNI compiled into this. 88 #endif 89 }