Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 // Copyright 2013 The Go Authors. All rights reserved. | 1 // Copyright 2013 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package sha3 | 5 package sha3 |
6 | 6 |
7 // This file implements the core Keccak permutation function necessary for compu ting SHA3. | 7 // This file implements the core Keccak permutation function necessary for compu ting SHA3. |
8 // This is implemented in a separate file as a stand-alone function to allow for replacement | 8 // This is implemented in a separate file to allow for replacement by an optimiz ed implementation. |
nigeltao
2013/04/02 01:15:00
I'd revert this change.
| |
9 // by an optimized implementation. Nothing in this package is exported. | 9 // Nothing in this package is exported. |
10 // For the detailed specification, refer to the Keccak web site (http://keccak.n oekeon.org/). | 10 // For the detailed specification, refer to the Keccak web site (http://keccak.n oekeon.org/). |
11 | 11 |
12 // rc stores the round constants for use in the ι step. | 12 // rc stores the round constants for use in the ι step. |
13 var rc = [...]uint64{ | 13 var rc = [...]uint64{ |
14 0x0000000000000001, | 14 0x0000000000000001, |
15 0x0000000000008082, | 15 0x0000000000008082, |
16 0x800000000000808A, | 16 0x800000000000808A, |
17 0x8000000080008000, | 17 0x8000000080008000, |
18 0x000000000000808B, | 18 0x000000000000808B, |
19 0x0000000080000001, | 19 0x0000000080000001, |
(...skipping 13 matching lines...) Expand all Loading... | |
33 0x800000008000000A, | 33 0x800000008000000A, |
34 0x8000000080008081, | 34 0x8000000080008081, |
35 0x8000000000008080, | 35 0x8000000000008080, |
36 0x0000000080000001, | 36 0x0000000080000001, |
37 0x8000000080008008, | 37 0x8000000080008008, |
38 } | 38 } |
39 | 39 |
40 // keccakF computes the complete Keccak-f function consisting of 24 rounds with a different | 40 // keccakF computes the complete Keccak-f function consisting of 24 rounds with a different |
41 // constant (rc) in each round. This implementation fully unrolls the round func tion to avoid | 41 // constant (rc) in each round. This implementation fully unrolls the round func tion to avoid |
42 // inner loops, as well as pre-calculating shift offsets. | 42 // inner loops, as well as pre-calculating shift offsets. |
43 func keccakF(st *[25]uint64) { | 43 func keccakF(a *[numLanes]uint64) { |
nigeltao
2013/04/02 01:15:00
s/25/numLanes/
Also, IANAKeccakExpert, but I woul
| |
44 var t, bc0, bc1, bc2, bc3, bc4 uint64 | 44 var t, bc0, bc1, bc2, bc3, bc4 uint64 |
45 for _, roundConstant := range rc { | 45 for _, roundConstant := range rc { |
46 // θ step | 46 // θ step |
47 » » bc0 = st[0] ^ st[5] ^ st[10] ^ st[15] ^ st[20] | 47 » » bc0 = a[0] ^ a[5] ^ a[10] ^ a[15] ^ a[20] |
48 » » bc1 = st[1] ^ st[6] ^ st[11] ^ st[16] ^ st[21] | 48 » » bc1 = a[1] ^ a[6] ^ a[11] ^ a[16] ^ a[21] |
49 » » bc2 = st[2] ^ st[7] ^ st[12] ^ st[17] ^ st[22] | 49 » » bc2 = a[2] ^ a[7] ^ a[12] ^ a[17] ^ a[22] |
50 » » bc3 = st[3] ^ st[8] ^ st[13] ^ st[18] ^ st[23] | 50 » » bc3 = a[3] ^ a[8] ^ a[13] ^ a[18] ^ a[23] |
51 » » bc4 = st[4] ^ st[9] ^ st[14] ^ st[19] ^ st[24] | 51 » » bc4 = a[4] ^ a[9] ^ a[14] ^ a[19] ^ a[24] |
52 t = bc4 ^ (bc1<<1 ^ bc1>>63) | 52 t = bc4 ^ (bc1<<1 ^ bc1>>63) |
53 » » st[0] ^= t | 53 » » a[0] ^= t |
54 » » st[5] ^= t | 54 » » a[5] ^= t |
55 » » st[10] ^= t | 55 » » a[10] ^= t |
56 » » st[15] ^= t | 56 » » a[15] ^= t |
57 » » st[20] ^= t | 57 » » a[20] ^= t |
58 t = bc0 ^ (bc2<<1 ^ bc2>>63) | 58 t = bc0 ^ (bc2<<1 ^ bc2>>63) |
59 » » st[1] ^= t | 59 » » a[1] ^= t |
60 » » st[6] ^= t | 60 » » a[6] ^= t |
61 » » st[11] ^= t | 61 » » a[11] ^= t |
62 » » st[16] ^= t | 62 » » a[16] ^= t |
63 » » st[21] ^= t | 63 » » a[21] ^= t |
64 t = bc1 ^ (bc3<<1 ^ bc3>>63) | 64 t = bc1 ^ (bc3<<1 ^ bc3>>63) |
65 » » st[2] ^= t | 65 » » a[2] ^= t |
66 » » st[7] ^= t | 66 » » a[7] ^= t |
67 » » st[12] ^= t | 67 » » a[12] ^= t |
68 » » st[17] ^= t | 68 » » a[17] ^= t |
69 » » st[22] ^= t | 69 » » a[22] ^= t |
70 t = bc2 ^ (bc4<<1 ^ bc4>>63) | 70 t = bc2 ^ (bc4<<1 ^ bc4>>63) |
71 » » st[3] ^= t | 71 » » a[3] ^= t |
72 » » st[8] ^= t | 72 » » a[8] ^= t |
73 » » st[13] ^= t | 73 » » a[13] ^= t |
74 » » st[18] ^= t | 74 » » a[18] ^= t |
75 » » st[23] ^= t | 75 » » a[23] ^= t |
76 t = bc3 ^ (bc0<<1 ^ bc0>>63) | 76 t = bc3 ^ (bc0<<1 ^ bc0>>63) |
77 » » st[4] ^= t | 77 » » a[4] ^= t |
78 » » st[9] ^= t | 78 » » a[9] ^= t |
79 » » st[14] ^= t | 79 » » a[14] ^= t |
80 » » st[19] ^= t | 80 » » a[19] ^= t |
81 » » st[24] ^= t | 81 » » a[24] ^= t |
82 | 82 |
83 // ρ and π steps | 83 // ρ and π steps |
84 » » t = st[1] | 84 » » t = a[1] |
85 » » t, st[10] = st[10], t<<1^t>>(64-1) | 85 » » t, a[10] = a[10], t<<1^t>>(64-1) |
86 » » t, st[7] = st[7], t<<3^t>>(64-3) | 86 » » t, a[7] = a[7], t<<3^t>>(64-3) |
87 » » t, st[11] = st[11], t<<6^t>>(64-6) | 87 » » t, a[11] = a[11], t<<6^t>>(64-6) |
88 » » t, st[17] = st[17], t<<10^t>>(64-10) | 88 » » t, a[17] = a[17], t<<10^t>>(64-10) |
89 » » t, st[18] = st[18], t<<15^t>>(64-15) | 89 » » t, a[18] = a[18], t<<15^t>>(64-15) |
90 » » t, st[3] = st[3], t<<21^t>>(64-21) | 90 » » t, a[3] = a[3], t<<21^t>>(64-21) |
91 » » t, st[5] = st[5], t<<28^t>>(64-28) | 91 » » t, a[5] = a[5], t<<28^t>>(64-28) |
92 » » t, st[16] = st[16], t<<36^t>>(64-36) | 92 » » t, a[16] = a[16], t<<36^t>>(64-36) |
93 » » t, st[8] = st[8], t<<45^t>>(64-45) | 93 » » t, a[8] = a[8], t<<45^t>>(64-45) |
94 » » t, st[21] = st[21], t<<55^t>>(64-55) | 94 » » t, a[21] = a[21], t<<55^t>>(64-55) |
95 » » t, st[24] = st[24], t<<2^t>>(64-2) | 95 » » t, a[24] = a[24], t<<2^t>>(64-2) |
96 » » t, st[4] = st[4], t<<14^t>>(64-14) | 96 » » t, a[4] = a[4], t<<14^t>>(64-14) |
97 » » t, st[15] = st[15], t<<27^t>>(64-27) | 97 » » t, a[15] = a[15], t<<27^t>>(64-27) |
98 » » t, st[23] = st[23], t<<41^t>>(64-41) | 98 » » t, a[23] = a[23], t<<41^t>>(64-41) |
99 » » t, st[19] = st[19], t<<56^t>>(64-56) | 99 » » t, a[19] = a[19], t<<56^t>>(64-56) |
100 » » t, st[13] = st[13], t<<8^t>>(64-8) | 100 » » t, a[13] = a[13], t<<8^t>>(64-8) |
101 » » t, st[12] = st[12], t<<25^t>>(64-25) | 101 » » t, a[12] = a[12], t<<25^t>>(64-25) |
102 » » t, st[2] = st[2], t<<43^t>>(64-43) | 102 » » t, a[2] = a[2], t<<43^t>>(64-43) |
103 » » t, st[20] = st[20], t<<62^t>>(64-62) | 103 » » t, a[20] = a[20], t<<62^t>>(64-62) |
104 » » t, st[14] = st[14], t<<18^t>>(64-18) | 104 » » t, a[14] = a[14], t<<18^t>>(64-18) |
105 » » t, st[22] = st[22], t<<39^t>>(64-39) | 105 » » t, a[22] = a[22], t<<39^t>>(64-39) |
106 » » t, st[9] = st[9], t<<61^t>>(64-61) | 106 » » t, a[9] = a[9], t<<61^t>>(64-61) |
107 » » t, st[6] = st[6], t<<20^t>>(64-20) | 107 » » t, a[6] = a[6], t<<20^t>>(64-20) |
108 » » st[1] = t<<44 ^ t>>(64-44) | 108 » » a[1] = t<<44 ^ t>>(64-44) |
109 | 109 |
110 // χ step | 110 // χ step |
111 » » bc0 = st[0] | 111 » » bc0 = a[0] |
112 » » bc1 = st[1] | 112 » » bc1 = a[1] |
113 » » bc2 = st[2] | 113 » » bc2 = a[2] |
114 » » bc3 = st[3] | 114 » » bc3 = a[3] |
115 » » bc4 = st[4] | 115 » » bc4 = a[4] |
116 » » st[0] ^= bc2 &^ bc1 | 116 » » a[0] ^= bc2 &^ bc1 |
117 » » st[1] ^= bc3 &^ bc2 | 117 » » a[1] ^= bc3 &^ bc2 |
118 » » st[2] ^= bc4 &^ bc3 | 118 » » a[2] ^= bc4 &^ bc3 |
119 » » st[3] ^= bc0 &^ bc4 | 119 » » a[3] ^= bc0 &^ bc4 |
120 » » st[4] ^= bc1 &^ bc0 | 120 » » a[4] ^= bc1 &^ bc0 |
121 » » bc0 = st[5] | 121 » » bc0 = a[5] |
122 » » bc1 = st[6] | 122 » » bc1 = a[6] |
123 » » bc2 = st[7] | 123 » » bc2 = a[7] |
124 » » bc3 = st[8] | 124 » » bc3 = a[8] |
125 » » bc4 = st[9] | 125 » » bc4 = a[9] |
126 » » st[5] ^= bc2 &^ bc1 | 126 » » a[5] ^= bc2 &^ bc1 |
127 » » st[6] ^= bc3 &^ bc2 | 127 » » a[6] ^= bc3 &^ bc2 |
128 » » st[7] ^= bc4 &^ bc3 | 128 » » a[7] ^= bc4 &^ bc3 |
129 » » st[8] ^= bc0 &^ bc4 | 129 » » a[8] ^= bc0 &^ bc4 |
130 » » st[9] ^= bc1 &^ bc0 | 130 » » a[9] ^= bc1 &^ bc0 |
131 » » bc0 = st[10] | 131 » » bc0 = a[10] |
132 » » bc1 = st[11] | 132 » » bc1 = a[11] |
133 » » bc2 = st[12] | 133 » » bc2 = a[12] |
134 » » bc3 = st[13] | 134 » » bc3 = a[13] |
135 » » bc4 = st[14] | 135 » » bc4 = a[14] |
136 » » st[10] ^= bc2 &^ bc1 | 136 » » a[10] ^= bc2 &^ bc1 |
137 » » st[11] ^= bc3 &^ bc2 | 137 » » a[11] ^= bc3 &^ bc2 |
138 » » st[12] ^= bc4 &^ bc3 | 138 » » a[12] ^= bc4 &^ bc3 |
139 » » st[13] ^= bc0 &^ bc4 | 139 » » a[13] ^= bc0 &^ bc4 |
140 » » st[14] ^= bc1 &^ bc0 | 140 » » a[14] ^= bc1 &^ bc0 |
141 » » bc0 = st[15] | 141 » » bc0 = a[15] |
142 » » bc1 = st[16] | 142 » » bc1 = a[16] |
143 » » bc2 = st[17] | 143 » » bc2 = a[17] |
144 » » bc3 = st[18] | 144 » » bc3 = a[18] |
145 » » bc4 = st[19] | 145 » » bc4 = a[19] |
146 » » st[15] ^= bc2 &^ bc1 | 146 » » a[15] ^= bc2 &^ bc1 |
147 » » st[16] ^= bc3 &^ bc2 | 147 » » a[16] ^= bc3 &^ bc2 |
148 » » st[17] ^= bc4 &^ bc3 | 148 » » a[17] ^= bc4 &^ bc3 |
149 » » st[18] ^= bc0 &^ bc4 | 149 » » a[18] ^= bc0 &^ bc4 |
150 » » st[19] ^= bc1 &^ bc0 | 150 » » a[19] ^= bc1 &^ bc0 |
151 » » bc0 = st[20] | 151 » » bc0 = a[20] |
152 » » bc1 = st[21] | 152 » » bc1 = a[21] |
153 » » bc2 = st[22] | 153 » » bc2 = a[22] |
154 » » bc3 = st[23] | 154 » » bc3 = a[23] |
155 » » bc4 = st[24] | 155 » » bc4 = a[24] |
156 » » st[20] ^= bc2 &^ bc1 | 156 » » a[20] ^= bc2 &^ bc1 |
157 » » st[21] ^= bc3 &^ bc2 | 157 » » a[21] ^= bc3 &^ bc2 |
158 » » st[22] ^= bc4 &^ bc3 | 158 » » a[22] ^= bc4 &^ bc3 |
159 » » st[23] ^= bc0 &^ bc4 | 159 » » a[23] ^= bc0 &^ bc4 |
160 » » st[24] ^= bc1 &^ bc0 | 160 » » a[24] ^= bc1 &^ bc0 |
161 | 161 |
162 // ι step | 162 // ι step |
163 » » st[0] ^= roundConstant | 163 » » a[0] ^= roundConstant |
164 } | 164 } |
165 } | 165 } |
LEFT | RIGHT |