vSMC
vSMC: Scalable Monte Carlo
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Friends | List of all members
vsmc::AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks > Class Template Reference

RNG engine using AES-NI instructions. More...

#include <vsmc/rng/aes_ni.hpp>

Public Types

typedef Array< __m128i, Blocks > buffer_type
 
typedef Array< ctr_type, Blocks > ctr_block_type
 
typedef Array< ResultType, sizeof(__m128i)/sizeof(ResultType)> ctr_type
 
typedef Array< __m128i, Rounds+1 > key_seq_type
 
typedef KeySeq::key_type key_type
 
typedef ResultType result_type
 

Public Member Functions

 AESNIEngine (result_type s=0)
 
template<typename SeedSeq >
 AESNIEngine (SeedSeq &seq, typename cxx11::enable_if< internal::is_seed_seq< SeedSeq, result_type, key_type, AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks > >::value >::type *=nullptr)
 
 AESNIEngine (const key_type &k)
 
template<std::size_t B>
ctr_type ctr () const
 
void ctr (const ctr_type &c)
 
ctr_block_type ctr_block () const
 
void discard (result_type nskip)
 
key_type key () const
 
void key (const key_type &k)
 
key_seq_type key_seq () const
 
result_type operator() ()
 
buffer_type operator() (const ctr_type &c) const
 Generate a buffer of random bits given a counter using the current key. More...
 
buffer_type operator() (const ctr_block_type &cb) const
 Generate a buffer of random bits given a block of counters using the current key. More...
 
void operator() (const ctr_type &c, buffer_type &buf) const
 Generate random bits in a pre-allocated buffer given a counter using the current key. More...
 
void operator() (const ctr_block_type &cb, buffer_type &buf) const
 Generate ranodm bits in a pre-allocated buffer given a block of counters using the current key. More...
 
void seed (result_type s)
 
template<typename SeedSeq >
void seed (SeedSeq &seq, typename cxx11::enable_if< internal::is_seed_seq< SeedSeq, result_type, key_type >::value >::type *=nullptr)
 
void seed (const key_type &k)
 

Static Public Member Functions

static constexpr result_type max ()
 
static constexpr result_type min ()
 

Static Public Attributes

static constexpr const result_type _Max
 
static constexpr const result_type _Min = 0
 

Friends

bool operator!= (const AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks > &eng1, const AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks > &eng2)
 
template<typename CharT , typename Traits >
std::basic_ostream< CharT, Traits > & operator<< (std::basic_ostream< CharT, Traits > &os, const AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks > &eng)
 
bool operator== (const AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks > &eng1, const AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks > &eng2)
 
template<typename CharT , typename Traits >
std::basic_istream< CharT, Traits > & operator>> (std::basic_istream< CharT, Traits > &is, AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks > &eng)
 

Detailed Description

template<typename ResultType, typename KeySeq, bool KeySeqInit, std::size_t Rounds, std::size_t Blocks>
class vsmc::AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks >

RNG engine using AES-NI instructions.

Two dervied class AES128Engine and ARSEngine behave exactly the same as AES and ARS RNG engines as described in Parallel Random Numbers: As Easy as 1, 2, 3 and implemented in Random123, when used with uint32_t as ResultType. The first \(2^{32}\) iterations will be exactly the same as r123::Engine<r123:AESNI4x32> and r123::Engine<r123:ARS4x32_R<10> >. (Note, they could be simple template alias in C++11, but to support C++98 we had to derive from it. Since the derived classes contains nothing and does nothing, there is no performance cost).

This implementation is more flexible than the original. First, it allows using any unsigned integers as output. Second, it allows user defined key schedule (the second template argument). The two derived classed merely use two special key schedule to reproduce the original behavior.

The terminology used in this engine is slightly different than that in Random123. In the later, there is a distinction between key_type and ukey_type. They are key_type and key_seq_type in this class, respectively. In other Random123 engines, key_type and ukey_type are usually the same. And in ThreefryEngine and PhiloxEngine, vSMC does not define ukey_type. In this engine, the term key (ukey, short for unique key, in Rnadom123) usually refer to the 128-, 192-, or 256-bits input key in the context of the AES algorithm. And key_seq (key in Random123) refers to the key schedule in the same context. In the context of C++11, key_seq is also sort of like the seed_seq for other RNG (basically both expand a single input value into a sequence such that the sequence provides extra entropy even the inputs are not random at all). Therefore vSMC's terminology shall be both familiar to people familiar with block ciphers (of which AES is one), and people familiar with C++11, but at the risk of confusing people already familiar with [Random123r123lib. Of course, if only used as a standard C++11 engine, then none of these matters since users only provides seeds or a seed sequence.

Template Parameters
ResultTypeThe output type of operator()
KeySeqUsing other key schedule can lead to other rng. The KeySeq template parameter needs to has a member function of the form,
which is similar to that of C++11 seed_seq. Given a unique key, a sequence of round keys shall be generated and filled into key_seq. The KeySeq type also needs to have a member type key_type.
KeySeqInitThe key sequence can be computed when the engine is constructed or seeded, or computed each time it is needed. Prepare the key when seeding increase the size of the engine considerably. But in some cases such as AES128Engine, etc, it increase the throughput significantly.
RoundsThe third template argument is the rounds of the algorithm. AES requires 10 rounds when using a 128-bits key. With reduced strength, any number of round below 10 can be used.
BlocksThe fourth template argument specifies how many blocks shall be used. The AES-NI instructions have noticeable latency but can be started every two cycles. By allowing generating multiple blocks at once, and interleaving the instructions, the throughput can be increased at the cost of space.

Definition at line 213 of file aes_ni.hpp.

Member Typedef Documentation

template<typename ResultType, typename KeySeq, bool KeySeqInit, std::size_t Rounds, std::size_t Blocks>
typedef Array<__m128i, Blocks> vsmc::AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks >::buffer_type

Definition at line 221 of file aes_ni.hpp.

template<typename ResultType, typename KeySeq, bool KeySeqInit, std::size_t Rounds, std::size_t Blocks>
typedef Array<ctr_type, Blocks> vsmc::AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks >::ctr_block_type

Definition at line 223 of file aes_ni.hpp.

template<typename ResultType, typename KeySeq, bool KeySeqInit, std::size_t Rounds, std::size_t Blocks>
typedef Array<ResultType, sizeof(__m128i) / sizeof(ResultType)> vsmc::AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks >::ctr_type

Definition at line 222 of file aes_ni.hpp.

template<typename ResultType, typename KeySeq, bool KeySeqInit, std::size_t Rounds, std::size_t Blocks>
typedef Array<__m128i, Rounds + 1> vsmc::AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks >::key_seq_type

Definition at line 225 of file aes_ni.hpp.

template<typename ResultType, typename KeySeq, bool KeySeqInit, std::size_t Rounds, std::size_t Blocks>
typedef KeySeq::key_type vsmc::AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks >::key_type

Definition at line 224 of file aes_ni.hpp.

template<typename ResultType, typename KeySeq, bool KeySeqInit, std::size_t Rounds, std::size_t Blocks>
typedef ResultType vsmc::AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks >::result_type

Definition at line 220 of file aes_ni.hpp.

Constructor & Destructor Documentation

template<typename ResultType, typename KeySeq, bool KeySeqInit, std::size_t Rounds, std::size_t Blocks>
vsmc::AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks >::AESNIEngine ( result_type  s = 0)
inlineexplicit

Definition at line 233 of file aes_ni.hpp.

template<typename ResultType, typename KeySeq, bool KeySeqInit, std::size_t Rounds, std::size_t Blocks>
template<typename SeedSeq >
vsmc::AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks >::AESNIEngine ( SeedSeq &  seq,
typename cxx11::enable_if< internal::is_seed_seq< SeedSeq, result_type, key_type, AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks > >::value >::type = nullptr 
)
inlineexplicit

Definition at line 240 of file aes_ni.hpp.

template<typename ResultType, typename KeySeq, bool KeySeqInit, std::size_t Rounds, std::size_t Blocks>
vsmc::AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks >::AESNIEngine ( const key_type k)
inline

Definition at line 249 of file aes_ni.hpp.

Member Function Documentation

template<typename ResultType, typename KeySeq, bool KeySeqInit, std::size_t Rounds, std::size_t Blocks>
template<std::size_t B>
ctr_type vsmc::AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks >::ctr ( ) const
inline

Definition at line 283 of file aes_ni.hpp.

template<typename ResultType, typename KeySeq, bool KeySeqInit, std::size_t Rounds, std::size_t Blocks>
void vsmc::AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks >::ctr ( const ctr_type c)
inline

Definition at line 291 of file aes_ni.hpp.

template<typename ResultType, typename KeySeq, bool KeySeqInit, std::size_t Rounds, std::size_t Blocks>
ctr_block_type vsmc::AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks >::ctr_block ( ) const
inline

Definition at line 285 of file aes_ni.hpp.

template<typename ResultType, typename KeySeq, bool KeySeqInit, std::size_t Rounds, std::size_t Blocks>
void vsmc::AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks >::discard ( result_type  nskip)
inline

Definition at line 351 of file aes_ni.hpp.

template<typename ResultType, typename KeySeq, bool KeySeqInit, std::size_t Rounds, std::size_t Blocks>
key_type vsmc::AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks >::key ( ) const
inline

Definition at line 287 of file aes_ni.hpp.

template<typename ResultType, typename KeySeq, bool KeySeqInit, std::size_t Rounds, std::size_t Blocks>
void vsmc::AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks >::key ( const key_type k)
inline

Definition at line 297 of file aes_ni.hpp.

template<typename ResultType, typename KeySeq, bool KeySeqInit, std::size_t Rounds, std::size_t Blocks>
key_seq_type vsmc::AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks >::key_seq ( ) const
inline

Definition at line 289 of file aes_ni.hpp.

template<typename ResultType, typename KeySeq, bool KeySeqInit, std::size_t Rounds, std::size_t Blocks>
static constexpr result_type vsmc::AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks >::max ( )
inlinestatic

Definition at line 378 of file aes_ni.hpp.

template<typename ResultType, typename KeySeq, bool KeySeqInit, std::size_t Rounds, std::size_t Blocks>
static constexpr result_type vsmc::AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks >::min ( )
inlinestatic

Definition at line 377 of file aes_ni.hpp.

template<typename ResultType, typename KeySeq, bool KeySeqInit, std::size_t Rounds, std::size_t Blocks>
result_type vsmc::AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks >::operator() ( )
inline

Definition at line 304 of file aes_ni.hpp.

template<typename ResultType, typename KeySeq, bool KeySeqInit, std::size_t Rounds, std::size_t Blocks>
buffer_type vsmc::AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks >::operator() ( const ctr_type c) const
inline

Generate a buffer of random bits given a counter using the current key.

Definition at line 317 of file aes_ni.hpp.

template<typename ResultType, typename KeySeq, bool KeySeqInit, std::size_t Rounds, std::size_t Blocks>
buffer_type vsmc::AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks >::operator() ( const ctr_block_type cb) const
inline

Generate a buffer of random bits given a block of counters using the current key.

Definition at line 329 of file aes_ni.hpp.

template<typename ResultType, typename KeySeq, bool KeySeqInit, std::size_t Rounds, std::size_t Blocks>
void vsmc::AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks >::operator() ( const ctr_type c,
buffer_type buf 
) const
inline

Generate random bits in a pre-allocated buffer given a counter using the current key.

Definition at line 339 of file aes_ni.hpp.

template<typename ResultType, typename KeySeq, bool KeySeqInit, std::size_t Rounds, std::size_t Blocks>
void vsmc::AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks >::operator() ( const ctr_block_type cb,
buffer_type buf 
) const
inline

Generate ranodm bits in a pre-allocated buffer given a block of counters using the current key.

Definition at line 348 of file aes_ni.hpp.

template<typename ResultType, typename KeySeq, bool KeySeqInit, std::size_t Rounds, std::size_t Blocks>
void vsmc::AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks >::seed ( result_type  s)
inline

Definition at line 255 of file aes_ni.hpp.

template<typename ResultType, typename KeySeq, bool KeySeqInit, std::size_t Rounds, std::size_t Blocks>
template<typename SeedSeq >
void vsmc::AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks >::seed ( SeedSeq &  seq,
typename cxx11::enable_if< internal::is_seed_seq< SeedSeq, result_type, key_type >::value >::type = nullptr 
)
inline

Definition at line 265 of file aes_ni.hpp.

template<typename ResultType, typename KeySeq, bool KeySeqInit, std::size_t Rounds, std::size_t Blocks>
void vsmc::AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks >::seed ( const key_type k)
inline

Definition at line 274 of file aes_ni.hpp.

Friends And Related Function Documentation

template<typename ResultType, typename KeySeq, bool KeySeqInit, std::size_t Rounds, std::size_t Blocks>
bool operator!= ( const AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks > &  eng1,
const AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks > &  eng2 
)
friend

Definition at line 391 of file aes_ni.hpp.

template<typename ResultType, typename KeySeq, bool KeySeqInit, std::size_t Rounds, std::size_t Blocks>
template<typename CharT , typename Traits >
std::basic_ostream<CharT, Traits>& operator<< ( std::basic_ostream< CharT, Traits > &  os,
const AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks > &  eng 
)
friend

Definition at line 399 of file aes_ni.hpp.

template<typename ResultType, typename KeySeq, bool KeySeqInit, std::size_t Rounds, std::size_t Blocks>
bool operator== ( const AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks > &  eng1,
const AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks > &  eng2 
)
friend

Definition at line 380 of file aes_ni.hpp.

template<typename ResultType, typename KeySeq, bool KeySeqInit, std::size_t Rounds, std::size_t Blocks>
template<typename CharT , typename Traits >
std::basic_istream<CharT, Traits>& operator>> ( std::basic_istream< CharT, Traits > &  is,
AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks > &  eng 
)
friend

Definition at line 419 of file aes_ni.hpp.

Member Data Documentation

template<typename ResultType, typename KeySeq, bool KeySeqInit, std::size_t Rounds, std::size_t Blocks>
constexpr const result_type vsmc::AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks >::_Max
static
Initial value:
= static_cast<result_type>(
~(static_cast<result_type>(0)))

Definition at line 374 of file aes_ni.hpp.

template<typename ResultType, typename KeySeq, bool KeySeqInit, std::size_t Rounds, std::size_t Blocks>
constexpr const result_type vsmc::AESNIEngine< ResultType, KeySeq, KeySeqInit, Rounds, Blocks >::_Min = 0
static

Definition at line 373 of file aes_ni.hpp.