github.com/core-coin/go-core/v2@v2.1.9/crypto/blake2b/blake2b_generic.go (about) 1 // Copyright 2019 by the Authors 2 // This file is part of the go-core library. 3 // 4 // The go-core library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-core library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-core library. If not, see <http://www.gnu.org/licenses/>. 16 17 package blake2b 18 19 import ( 20 "encoding/binary" 21 "math/bits" 22 ) 23 24 // the precomputed values for BLAKE2b 25 // there are 10 16-byte arrays - one for each round 26 // the entries are calculated from the sigma constants. 27 var precomputed = [10][16]byte{ 28 {0, 2, 4, 6, 1, 3, 5, 7, 8, 10, 12, 14, 9, 11, 13, 15}, 29 {14, 4, 9, 13, 10, 8, 15, 6, 1, 0, 11, 5, 12, 2, 7, 3}, 30 {11, 12, 5, 15, 8, 0, 2, 13, 10, 3, 7, 9, 14, 6, 1, 4}, 31 {7, 3, 13, 11, 9, 1, 12, 14, 2, 5, 4, 15, 6, 10, 0, 8}, 32 {9, 5, 2, 10, 0, 7, 4, 15, 14, 11, 6, 3, 1, 12, 8, 13}, 33 {2, 6, 0, 8, 12, 10, 11, 3, 4, 7, 15, 1, 13, 5, 14, 9}, 34 {12, 1, 14, 4, 5, 15, 13, 10, 0, 6, 9, 8, 7, 3, 2, 11}, 35 {13, 7, 12, 3, 11, 14, 1, 9, 5, 15, 8, 2, 0, 4, 6, 10}, 36 {6, 14, 11, 0, 15, 9, 3, 8, 12, 13, 1, 10, 2, 7, 4, 5}, 37 {10, 8, 7, 1, 2, 4, 6, 5, 15, 9, 3, 13, 11, 14, 12, 0}, 38 } 39 40 func hashBlocksGeneric(h *[8]uint64, c *[2]uint64, flag uint64, blocks []byte) { 41 var m [16]uint64 42 c0, c1 := c[0], c[1] 43 44 for i := 0; i < len(blocks); { 45 c0 += BlockSize 46 if c0 < BlockSize { 47 c1++ 48 } 49 for j := range m { 50 m[j] = binary.LittleEndian.Uint64(blocks[i:]) 51 i += 8 52 } 53 fGeneric(h, &m, c0, c1, flag, 12) 54 } 55 c[0], c[1] = c0, c1 56 } 57 58 func fGeneric(h *[8]uint64, m *[16]uint64, c0, c1 uint64, flag uint64, rounds uint64) { 59 v0, v1, v2, v3, v4, v5, v6, v7 := h[0], h[1], h[2], h[3], h[4], h[5], h[6], h[7] 60 v8, v9, v10, v11, v12, v13, v14, v15 := iv[0], iv[1], iv[2], iv[3], iv[4], iv[5], iv[6], iv[7] 61 v12 ^= c0 62 v13 ^= c1 63 v14 ^= flag 64 65 for i := 0; i < int(rounds); i++ { 66 s := &(precomputed[i%10]) 67 68 v0 += m[s[0]] 69 v0 += v4 70 v12 ^= v0 71 v12 = bits.RotateLeft64(v12, -32) 72 v8 += v12 73 v4 ^= v8 74 v4 = bits.RotateLeft64(v4, -24) 75 v1 += m[s[1]] 76 v1 += v5 77 v13 ^= v1 78 v13 = bits.RotateLeft64(v13, -32) 79 v9 += v13 80 v5 ^= v9 81 v5 = bits.RotateLeft64(v5, -24) 82 v2 += m[s[2]] 83 v2 += v6 84 v14 ^= v2 85 v14 = bits.RotateLeft64(v14, -32) 86 v10 += v14 87 v6 ^= v10 88 v6 = bits.RotateLeft64(v6, -24) 89 v3 += m[s[3]] 90 v3 += v7 91 v15 ^= v3 92 v15 = bits.RotateLeft64(v15, -32) 93 v11 += v15 94 v7 ^= v11 95 v7 = bits.RotateLeft64(v7, -24) 96 97 v0 += m[s[4]] 98 v0 += v4 99 v12 ^= v0 100 v12 = bits.RotateLeft64(v12, -16) 101 v8 += v12 102 v4 ^= v8 103 v4 = bits.RotateLeft64(v4, -63) 104 v1 += m[s[5]] 105 v1 += v5 106 v13 ^= v1 107 v13 = bits.RotateLeft64(v13, -16) 108 v9 += v13 109 v5 ^= v9 110 v5 = bits.RotateLeft64(v5, -63) 111 v2 += m[s[6]] 112 v2 += v6 113 v14 ^= v2 114 v14 = bits.RotateLeft64(v14, -16) 115 v10 += v14 116 v6 ^= v10 117 v6 = bits.RotateLeft64(v6, -63) 118 v3 += m[s[7]] 119 v3 += v7 120 v15 ^= v3 121 v15 = bits.RotateLeft64(v15, -16) 122 v11 += v15 123 v7 ^= v11 124 v7 = bits.RotateLeft64(v7, -63) 125 126 v0 += m[s[8]] 127 v0 += v5 128 v15 ^= v0 129 v15 = bits.RotateLeft64(v15, -32) 130 v10 += v15 131 v5 ^= v10 132 v5 = bits.RotateLeft64(v5, -24) 133 v1 += m[s[9]] 134 v1 += v6 135 v12 ^= v1 136 v12 = bits.RotateLeft64(v12, -32) 137 v11 += v12 138 v6 ^= v11 139 v6 = bits.RotateLeft64(v6, -24) 140 v2 += m[s[10]] 141 v2 += v7 142 v13 ^= v2 143 v13 = bits.RotateLeft64(v13, -32) 144 v8 += v13 145 v7 ^= v8 146 v7 = bits.RotateLeft64(v7, -24) 147 v3 += m[s[11]] 148 v3 += v4 149 v14 ^= v3 150 v14 = bits.RotateLeft64(v14, -32) 151 v9 += v14 152 v4 ^= v9 153 v4 = bits.RotateLeft64(v4, -24) 154 155 v0 += m[s[12]] 156 v0 += v5 157 v15 ^= v0 158 v15 = bits.RotateLeft64(v15, -16) 159 v10 += v15 160 v5 ^= v10 161 v5 = bits.RotateLeft64(v5, -63) 162 v1 += m[s[13]] 163 v1 += v6 164 v12 ^= v1 165 v12 = bits.RotateLeft64(v12, -16) 166 v11 += v12 167 v6 ^= v11 168 v6 = bits.RotateLeft64(v6, -63) 169 v2 += m[s[14]] 170 v2 += v7 171 v13 ^= v2 172 v13 = bits.RotateLeft64(v13, -16) 173 v8 += v13 174 v7 ^= v8 175 v7 = bits.RotateLeft64(v7, -63) 176 v3 += m[s[15]] 177 v3 += v4 178 v14 ^= v3 179 v14 = bits.RotateLeft64(v14, -16) 180 v9 += v14 181 v4 ^= v9 182 v4 = bits.RotateLeft64(v4, -63) 183 } 184 h[0] ^= v0 ^ v8 185 h[1] ^= v1 ^ v9 186 h[2] ^= v2 ^ v10 187 h[3] ^= v3 ^ v11 188 h[4] ^= v4 ^ v12 189 h[5] ^= v5 ^ v13 190 h[6] ^= v6 ^ v14 191 h[7] ^= v7 ^ v15 192 }