random123

Counter-based Random Number Generators
git clone git://git.meso-star.com/random123.git
Log | Files | Refs | README | LICENSE

time_initkeyctr.h (6052B)


      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 #ifndef TIME_INITKEYCTR_H__
     33 #define TIME_INITKEYCTR_H__ 1
     34 
     35 /*
     36  * EXAMPLE_KEY* and EXAMPLE_CTR* values are just arbitrary numbers
     37  * with some bits set, they have no special importance, they do
     38  * not even have to be different.  If they are changed, then the
     39  * good_* values further below will need to be updated to match.
     40  */
     41 
     42 #define EXAMPLE_KEY0 0xdeadbeefU
     43 #define EXAMPLE_KEY1 0x12345678U
     44 #define EXAMPLE_KEY2 0xc0debad1U
     45 #define EXAMPLE_KEY3 0x31415926U
     46 
     47 #define EXAMPLE_CTR0 0x00000000U
     48 #define EXAMPLE_CTR1 0x10000000U
     49 #define EXAMPLE_CTR2 0x20000000U
     50 #define EXAMPLE_CTR3 0x30000000U
     51 
     52 /*
     53  * The magic hex numbers below are just known good values that
     54  * result from the arbitrary EXAMPLE* inputs above.  We check that
     55  * we got these results at the end of timing tests to ensure that
     56  * we didn't accidentally let a compiler optimize away some loop.
     57  */
     58 #if R123_USE_PHILOX_64BIT
     59 static philox2x64_ctr_t good_philox2x64_6 = {{R123_64BIT(0xdd40cdb81af968d2),R123_64BIT(0x0cb57d6d5f7b68dc)}};
     60 static philox2x64_ctr_t good_philox2x64_10 = {{R123_64BIT(0x539e5b3d18faf5da),R123_64BIT(0x838ca1328d07d3ba)}};
     61 static philox4x64_ctr_t good_philox4x64_7 = {{R123_64BIT(0xcf492074862957a2),R123_64BIT(0x7057627260938584),R123_64BIT(0x676e23214a14901d),R123_64BIT(0xefa2c5df3848e3fe)}};
     62 static philox4x64_ctr_t good_philox4x64_10 = {{R123_64BIT(0x1b64f56b381a5a89),R123_64BIT(0x940a282a8add45e1),R123_64BIT(0x53c936376ac7d5df),R123_64BIT(0x6147e87ec9bd9caa)}};
     63 #endif
     64 static philox4x32_ctr_t good_philox4x32_7 = {{0x40ba6a95,0x799e6a43,0x7dcabe10,0xa7a81636}};
     65 static philox4x32_ctr_t good_philox4x32_10 = {{0xf16d828e,0xa1c5962d,0xacac820c,0x58113d7a}};
     66 static threefry4x32_ctr_t good_threefry4x32_12 = {{0xe461db1c,0xfdfa62a7,0x0b10cd2a,0xa3679758}};
     67 static threefry4x32_ctr_t good_threefry4x32_20 = {{0xf82cf576,0x162ca116,0x3afefe23,0x54cc64ac}};
     68 #if R123_USE_64BIT
     69 static threefry2x64_ctr_t good_threefry2x64_13 = {{R123_64BIT(0xdf0f096c179ad798),R123_64BIT(0x077862fbaa1a0d11)}};
     70 static threefry2x64_ctr_t good_threefry2x64_20 = {{R123_64BIT(0xb91153d59815d50e),R123_64BIT(0xdb0dd45e5b0eab81)}};
     71 static threefry4x64_ctr_t good_threefry4x64_12 = {{R123_64BIT(0x416d1802da0a4a0f),R123_64BIT(0xabd4d80749306281),R123_64BIT(0x62c6b120b542bff0),R123_64BIT(0xefb28dc80c6fc36c)}};
     72 static threefry4x64_ctr_t good_threefry4x64_20 = {{R123_64BIT(0xad8f0b8c18ed5187),R123_64BIT(0xd80146a6961e1880),R123_64BIT(0x7fce9d950d8acbc4),R123_64BIT(0x782948d5203519f1)}};
     73 static threefry4x64_ctr_t good_threefry4x64_72 = {{R123_64BIT(0x73ff3f7a0b878f68),R123_64BIT(0x6668f6bbaba83f31),R123_64BIT(0x088eb85d40fbdb56),R123_64BIT(0xd1f39136adc96552)}};
     74 #endif
     75 
     76 #if R123_USE_AES_NI
     77 static ars4x32_ctr_t good_ars4x32_5 = {{0x279f6b0b, 0xd0b1edf6, 0x6044b433, 0x66c06817}};
     78 static ars4x32_ctr_t good_ars4x32_7 = {{0xa9cd8055, 0x80272a47, 0x4b7ab914, 0x5351d78e}};
     79 static aesni4x32_ctr_t good_aesni4x32_10 = {{0x1e68c9fd, 0x347b0858, 0x503d8d91, 0x9e73460a}};
     80 #endif
     81 
     82 /*
     83  * template code initializes a ukey and counter to known values
     84  * with a known offset and calls a Random123 test function with
     85  * that ukey, ctr and a count and closure.  keyctroffset is
     86  * a variable initialized from runtime environment (e.g. argv, argc,
     87  * getenv(), etc) to avoid compile-time optimization
     88  * caused by constants, so we get worst-case numbers.  Users may,
     89  * of course, benefit from compile-time optimization if they
     90  * have some constants for key or ctr values.
     91  * 
     92  */
     93 #define TEST_TPL(NAME, N, W, R) \
     94 if ((strncmp(#NAME, "aes", 3) == 0 || strncmp(#NAME, "ars", 3) == 0) && !haveAESNI()) { \
     95     printf("AESNI not available on this hardware\n"); \
     96 } else { \
     97     NAME##N##x##W##_ukey_t ukey={{0}};            \
     98     NAME##N##x##W##_ctr_t ctr={{0}};            \
     99     size_t xi; \
    100     for (xi = 0; xi < sizeof(ukey)/sizeof(ukey.v[0]); xi++) { \
    101 	switch (xi) { \
    102 	    case 0: ukey.v[xi] = EXAMPLE_KEY0+keyctroffset; break; \
    103 	    case 1: ukey.v[xi] = EXAMPLE_KEY1+keyctroffset; break; \
    104 	    case 2: ukey.v[xi] = EXAMPLE_KEY2+keyctroffset; break; \
    105 	    case 3: ukey.v[xi] = EXAMPLE_KEY3+keyctroffset; break; \
    106 	} \
    107     } \
    108     for (xi = 0; xi < N; xi++) { \
    109 	switch (xi) { \
    110 	    case 0: ctr.v[xi] = EXAMPLE_CTR0+keyctroffset; break; \
    111 	    case 1: ctr.v[xi] = EXAMPLE_CTR1+keyctroffset; break; \
    112 	    case 2: ctr.v[xi] = EXAMPLE_CTR2+keyctroffset; break; \
    113 	    case 3: ctr.v[xi] = EXAMPLE_CTR3+keyctroffset; break; \
    114 	} \
    115     } \
    116     NAME##N##x##W##_##R(ctr, ukey, good_##NAME##N##x##W##_##R, count, infop); \
    117 }
    118 
    119 #include "util_expandtpl.h"
    120 
    121 #endif /* TIME_INITKEYCTR_H__ */
    122 
    123