Random123
ars.h
Go to the documentation of this file.
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 __Random123_ars_dot_hpp__
33 #define __Random123_ars_dot_hpp__
34 
36 #include "array.h"
37 
38 #if R123_USE_AES_NI
39 
40 #ifndef ARS1xm128i_DEFAULT_ROUNDS
41 #define ARS1xm128i_DEFAULT_ROUNDS 7
42 #endif
43 
46 
47 /* ARS1xm128i with Weyl keys. Fast, and Crush-resistant, but NOT CRYPTO. */
55 R123_STATIC_INLINE ars1xm128i_key_t ars1xm128ikeyinit(ars1xm128i_ukey_t uk) { return uk; }
57 R123_STATIC_INLINE ars1xm128i_ctr_t ars1xm128i_R(unsigned int Nrounds, ars1xm128i_ctr_t in, ars1xm128i_key_t k){
58  __m128i kweyl = _mm_set_epi64x(R123_64BIT(0xBB67AE8584CAA73B), /* sqrt(3) - 1.0 */
59  R123_64BIT(0x9E3779B97F4A7C15)); /* golden ratio */
60  /* N.B. the aesenc instructions do the xor *after*
61  // so if we want to follow the AES pattern, we
62  // have to do the initial xor explicitly */
63  __m128i kk = k.v[0].m;
64  __m128i v = _mm_xor_si128(in.v[0].m, kk);
65  ars1xm128i_ctr_t ret;
66  R123_ASSERT(Nrounds<=10);
67  if( Nrounds>1 ){
68  kk = _mm_add_epi64(kk, kweyl);
69  v = _mm_aesenc_si128(v, kk);
70  }
71  if( Nrounds>2 ){
72  kk = _mm_add_epi64(kk, kweyl);
73  v = _mm_aesenc_si128(v, kk);
74  }
75  if( Nrounds>3 ){
76  kk = _mm_add_epi64(kk, kweyl);
77  v = _mm_aesenc_si128(v, kk);
78  }
79  if( Nrounds>4 ){
80  kk = _mm_add_epi64(kk, kweyl);
81  v = _mm_aesenc_si128(v, kk);
82  }
83  if( Nrounds>5 ){
84  kk = _mm_add_epi64(kk, kweyl);
85  v = _mm_aesenc_si128(v, kk);
86  }
87  if( Nrounds>6 ){
88  kk = _mm_add_epi64(kk, kweyl);
89  v = _mm_aesenc_si128(v, kk);
90  }
91  if( Nrounds>7 ){
92  kk = _mm_add_epi64(kk, kweyl);
93  v = _mm_aesenc_si128(v, kk);
94  }
95  if( Nrounds>8 ){
96  kk = _mm_add_epi64(kk, kweyl);
97  v = _mm_aesenc_si128(v, kk);
98  }
99  if( Nrounds>9 ){
100  kk = _mm_add_epi64(kk, kweyl);
101  v = _mm_aesenc_si128(v, kk);
102  }
103  kk = _mm_add_epi64(kk, kweyl);
104  v = _mm_aesenclast_si128(v, kk);
105  ret.v[0].m = v;
106  return ret;
107 }
108 
112 #define ars1xm128i(c,k) ars1xm128i_R(ars1xm128i_rounds, c, k)
113 
123 R123_STATIC_INLINE ars4x32_key_t ars4x32keyinit(ars4x32_ukey_t uk) { return uk; }
125 R123_STATIC_INLINE ars4x32_ctr_t ars4x32_R(unsigned int Nrounds, ars4x32_ctr_t c, ars4x32_key_t k){
126  ars1xm128i_ctr_t c128;
127  ars1xm128i_key_t k128;
128  c128.v[0].m = _mm_set_epi32(c.v[3], c.v[2], c.v[1], c.v[0]);
129  k128.v[0].m = _mm_set_epi32(k.v[3], k.v[2], k.v[1], k.v[0]);
130  c128 = ars1xm128i_R(Nrounds, c128, k128);
131  _mm_storeu_si128((__m128i*)&c.v[0], c128.v[0].m);
132  return c;
133 }
134 
138 #define ars4x32(c,k) ars4x32_R(ars4x32_rounds, c, k)
139 
140 #ifdef __cplusplus
141 namespace r123{
163 template<unsigned int ROUNDS>
168  static const unsigned int rounds=ROUNDS;
169  R123_FORCE_INLINE(ctr_type operator()(ctr_type ctr, key_type key) const){
170  return ars1xm128i_R(ROUNDS, ctr, key);
171  }
172 };
173 
178 template<unsigned int ROUNDS>
179 struct ARS4x32_R{
183  static const unsigned int rounds=ROUNDS;
184  R123_FORCE_INLINE(ctr_type operator()(ctr_type ctr, key_type key) const){
185  return ars4x32_R(ROUNDS, ctr, key);
186  }
187 };
198 } // namespace r123
199 
200 #endif /* __cplusplus */
201 
202 #endif /* R123_USE_AES_NI */
203 
204 #endif
r123::ARS4x32
ARS4x32_R< ars4x32_rounds > ARS4x32
Definition: ars.h:197
ARS1xm128i_DEFAULT_ROUNDS
#define ARS1xm128i_DEFAULT_ROUNDS
Definition: ars.h:41
r123array4x32::v
uint32_t v[4]
Definition: array.h:316
r123::ARS1xm128i
ARS1xm128i_R< ars1xm128i_rounds > ARS1xm128i
Definition: ars.h:196
r123::ARS4x32_R::ctr_type
ars4x32_ctr_t ctr_type
Definition: ars.h:180
r123::ARS1xm128i_R
Definition: ars.h:164
ars4x32_rounds
@ ars4x32_rounds
Definition: ars.h:121
r123array1xm128i::v
r123m128i v[1]
Definition: array.h:331
r123::ARS4x32_R
Definition: ars.h:179
ars1xm128i_rounds
@ ars1xm128i_rounds
Definition: ars.h:45
ars4x32keyinit
static ars4x32_key_t ars4x32keyinit(ars4x32_ukey_t uk)
Definition: ars.h:123
r123::ARS4x32_R::rounds
static const unsigned int rounds
Definition: ars.h:183
array.h
r123_enum_ars1xm128i
r123_enum_ars1xm128i
Definition: ars.h:45
r123m128i::m
__m128i m
Definition: sse.h:149
r123::ARS1xm128i_R::ctr_type
ars1xm128i_ctr_t ctr_type
Definition: ars.h:165
r123_enum_ars4x32
r123_enum_ars4x32
Definition: ars.h:121
ars1xm128i_R
static ars1xm128i_ctr_t ars1xm128i_R(unsigned int Nrounds, ars1xm128i_ctr_t in, ars1xm128i_key_t k)
Definition: ars.h:57
ars4x32_R
static ars4x32_ctr_t ars4x32_R(unsigned int Nrounds, ars4x32_ctr_t c, ars4x32_key_t k)
Definition: ars.h:125
r123array1xm128i
Definition: array.h:331
r123::ARS1xm128i_R::key_type
ars1xm128i_key_t key_type
Definition: ars.h:166
r123::ARS1xm128i_R::ukey_type
ars1xm128i_key_t ukey_type
Definition: ars.h:167
r123array4x32
Definition: array.h:316
r123::ARS4x32_R::ukey_type
ars4x32_key_t ukey_type
Definition: ars.h:182
r123
Definition: aes.h:242
compilerfeatures.h
r123::ARS1xm128i_R::rounds
static const unsigned int rounds
Definition: ars.h:168
ars1xm128ikeyinit
static ars1xm128i_key_t ars1xm128ikeyinit(ars1xm128i_ukey_t uk)
Definition: ars.h:55
r123::ARS4x32_R::key_type
ars4x32_key_t key_type
Definition: ars.h:181