Line data Source code
1 : #include "fd_chacha20rng.h"
2 : #include "../../util/simd/fd_avx512.h"
3 : #include <assert.h>
4 :
5 599366640 : #define wwu_rol16(a) wwb_exch_adj_pair( (a) )
6 599366640 : #define wwu_rol12(a) wwu_rol( (a), 12 )
7 599366640 : #define wwu_rol7(a) wwu_rol( (a), 7 )
8 :
9 : static inline __attribute__((always_inline)) wwu_t
10 599366640 : wwu_rol8( wwu_t x ) {
11 599366640 : wwb_t const mask =
12 599366640 : wwb_bcast_hex( 3,0,1,2, 7,4,5,6, 11,8,9,10, 15,12,13,14 );
13 599366640 : return _mm512_shuffle_epi8( x, mask );
14 599366640 : }
15 :
16 : void
17 7492083 : fd_chacha20rng_refill_avx512( fd_chacha20rng_t * rng ) {
18 :
19 : /* This function should only be called if the buffer is empty. */
20 7492083 : if( FD_UNLIKELY( rng->buf_off != rng->buf_fill ) ) {
21 0 : FD_LOG_CRIT(( "refill out of sync: buf_off=%lu buf_fill=%lu", rng->buf_off, rng->buf_fill ));
22 0 : }
23 :
24 7492083 : wwu_t iv0 = wwu_bcast( 0x61707865U );
25 7492083 : wwu_t iv1 = wwu_bcast( 0x3320646eU );
26 7492083 : wwu_t iv2 = wwu_bcast( 0x79622d32U );
27 7492083 : wwu_t iv3 = wwu_bcast( 0x6b206574U );
28 7492083 : wwu_t zero = wwu_zero();
29 :
30 : /* Unpack key equivalent to:
31 :
32 : c4 = wwu_bcast( (uint const *)(rng->key)[0] );
33 : c5 = wwu_bcast( (uint const *)(rng->key)[1] );
34 : ...
35 : cB = wwu_bcast( (uint const *)(rng->key)[7] ); */
36 :
37 7492083 : wwu_t key_lo = _mm512_broadcast_i32x4( _mm_load_epi32( rng->key ) ); /* [0,1,2,3,0,1,2,3] */
38 7492083 : wwu_t key_hi = _mm512_broadcast_i32x4( _mm_load_epi32( rng->key+16 ) ); /* [4,5,6,7,4,5,6,7] */
39 7492083 : wwu_t k0 = _mm512_shuffle_epi32( key_lo, 0x00 );
40 7492083 : wwu_t k1 = _mm512_shuffle_epi32( key_lo, 0x55 );
41 7492083 : wwu_t k2 = _mm512_shuffle_epi32( key_lo, 0xaa );
42 7492083 : wwu_t k3 = _mm512_shuffle_epi32( key_lo, 0xff );
43 7492083 : wwu_t k4 = _mm512_shuffle_epi32( key_hi, 0x00 );
44 7492083 : wwu_t k5 = _mm512_shuffle_epi32( key_hi, 0x55 );
45 7492083 : wwu_t k6 = _mm512_shuffle_epi32( key_hi, 0xaa );
46 7492083 : wwu_t k7 = _mm512_shuffle_epi32( key_hi, 0xff );
47 :
48 : /* Derive block index */
49 :
50 7492083 : ulong idx = rng->buf_fill / FD_CHACHA20_BLOCK_SZ; /* really a right shift */
51 7492083 : wwu_t idxs = wwu_add( wwu_bcast( idx ), wwu( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ) );
52 :
53 : /* Run through the round function */
54 :
55 7492083 : wwu_t c0 = iv0; wwu_t c1 = iv1; wwu_t c2 = iv2; wwu_t c3 = iv3;
56 7492083 : wwu_t c4 = k0; wwu_t c5 = k1; wwu_t c6 = k2; wwu_t c7 = k3;
57 7492083 : wwu_t c8 = k4; wwu_t c9 = k5; wwu_t cA = k6; wwu_t cB = k7;
58 7492083 : wwu_t cC = idxs; wwu_t cD = zero; wwu_t cE = zero; wwu_t cF = zero;
59 :
60 7492083 : # define QUARTER_ROUND(a,b,c,d) \
61 599366640 : do { \
62 599366640 : a = wwu_add( a, b ); d = wwu_xor( d, a ); d = wwu_rol16( d ); \
63 599366640 : c = wwu_add( c, d ); b = wwu_xor( b, c ); b = wwu_rol12( b ); \
64 599366640 : a = wwu_add( a, b ); d = wwu_xor( d, a ); d = wwu_rol8( d ); \
65 599366640 : c = wwu_add( c, d ); b = wwu_xor( b, c ); b = wwu_rol7( b ); \
66 599366640 : } while(0)
67 :
68 82412913 : for( ulong i=0UL; i<10UL; i++ ) {
69 74920830 : QUARTER_ROUND( c0, c4, c8, cC );
70 74920830 : QUARTER_ROUND( c1, c5, c9, cD );
71 74920830 : QUARTER_ROUND( c2, c6, cA, cE );
72 74920830 : QUARTER_ROUND( c3, c7, cB, cF );
73 74920830 : QUARTER_ROUND( c0, c5, cA, cF );
74 74920830 : QUARTER_ROUND( c1, c6, cB, cC );
75 74920830 : QUARTER_ROUND( c2, c7, c8, cD );
76 74920830 : QUARTER_ROUND( c3, c4, c9, cE );
77 74920830 : }
78 7492083 : # undef QUARTER_ROUND
79 :
80 : /* Finalize */
81 :
82 7492083 : c0 = wwu_add( c0, iv0 );
83 7492083 : c1 = wwu_add( c1, iv1 );
84 7492083 : c2 = wwu_add( c2, iv2 );
85 7492083 : c3 = wwu_add( c3, iv3 );
86 7492083 : c4 = wwu_add( c4, k0 );
87 7492083 : c5 = wwu_add( c5, k1 );
88 7492083 : c6 = wwu_add( c6, k2 );
89 7492083 : c7 = wwu_add( c7, k3 );
90 7492083 : c8 = wwu_add( c8, k4 );
91 7492083 : c9 = wwu_add( c9, k5 );
92 7492083 : cA = wwu_add( cA, k6 );
93 7492083 : cB = wwu_add( cB, k7 );
94 7492083 : cC = wwu_add( cC, idxs );
95 : //cD = wwu_add( cD, zero );
96 : //cE = wwu_add( cE, zero );
97 : //cF = wwu_add( cF, zero );
98 :
99 : /* Transpose matrix to get output vector */
100 :
101 7492083 : wwu_transpose_16x16( c0, c1, c2, c3, c4, c5, c6, c7,
102 7492083 : c8, c9, cA, cB, cC, cD, cE, cF,
103 7492083 : c0, c1, c2, c3, c4, c5, c6, c7,
104 7492083 : c8, c9, cA, cB, cC, cD, cE, cF );
105 :
106 : /* Update ring buffer */
107 :
108 7492083 : uint * out = (uint *)rng->buf;
109 7492083 : wwu_st( out+0x00, c0 ); wwu_st( out+0x10, c1 );
110 7492083 : wwu_st( out+0x20, c2 ); wwu_st( out+0x30, c3 );
111 7492083 : wwu_st( out+0x40, c4 ); wwu_st( out+0x50, c5 );
112 7492083 : wwu_st( out+0x60, c6 ); wwu_st( out+0x70, c7 );
113 7492083 : wwu_st( out+0x80, c8 ); wwu_st( out+0x90, c9 );
114 7492083 : wwu_st( out+0xa0, cA ); wwu_st( out+0xb0, cB );
115 7492083 : wwu_st( out+0xc0, cC ); wwu_st( out+0xd0, cD );
116 7492083 : wwu_st( out+0xe0, cE ); wwu_st( out+0xf0, cF );
117 :
118 : /* Update ring descriptor */
119 :
120 7492083 : rng->buf_fill += 16*FD_CHACHA20_BLOCK_SZ;
121 7492083 : }
|