github.com/amazechain/amc@v0.1.3/common/crypto/blake2b/blake2b_generic.go (about) 1 // Copyright 2016 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package blake2b 6 7 import ( 8 "math/bits" 9 ) 10 11 // the precomputed values for BLAKE2b 12 // there are 10 16-byte arrays - one for each round 13 // the entries are calculated from the sigma constants. 14 var precomputed = [10][16]byte{ 15 {0, 2, 4, 6, 1, 3, 5, 7, 8, 10, 12, 14, 9, 11, 13, 15}, 16 {14, 4, 9, 13, 10, 8, 15, 6, 1, 0, 11, 5, 12, 2, 7, 3}, 17 {11, 12, 5, 15, 8, 0, 2, 13, 10, 3, 7, 9, 14, 6, 1, 4}, 18 {7, 3, 13, 11, 9, 1, 12, 14, 2, 5, 4, 15, 6, 10, 0, 8}, 19 {9, 5, 2, 10, 0, 7, 4, 15, 14, 11, 6, 3, 1, 12, 8, 13}, 20 {2, 6, 0, 8, 12, 10, 11, 3, 4, 7, 15, 1, 13, 5, 14, 9}, 21 {12, 1, 14, 4, 5, 15, 13, 10, 0, 6, 9, 8, 7, 3, 2, 11}, 22 {13, 7, 12, 3, 11, 14, 1, 9, 5, 15, 8, 2, 0, 4, 6, 10}, 23 {6, 14, 11, 0, 15, 9, 3, 8, 12, 13, 1, 10, 2, 7, 4, 5}, 24 {10, 8, 7, 1, 2, 4, 6, 5, 15, 9, 3, 13, 11, 14, 12, 0}, 25 } 26 27 func fGeneric(h *[8]uint64, m *[16]uint64, c0, c1 uint64, flag uint64, rounds uint64) { 28 v0, v1, v2, v3, v4, v5, v6, v7 := h[0], h[1], h[2], h[3], h[4], h[5], h[6], h[7] 29 v8, v9, v10, v11, v12, v13, v14, v15 := iv[0], iv[1], iv[2], iv[3], iv[4], iv[5], iv[6], iv[7] 30 v12 ^= c0 31 v13 ^= c1 32 v14 ^= flag 33 34 for i := 0; i < int(rounds); i++ { 35 s := &(precomputed[i%10]) 36 37 v0 += m[s[0]] 38 v0 += v4 39 v12 ^= v0 40 v12 = bits.RotateLeft64(v12, -32) 41 v8 += v12 42 v4 ^= v8 43 v4 = bits.RotateLeft64(v4, -24) 44 v1 += m[s[1]] 45 v1 += v5 46 v13 ^= v1 47 v13 = bits.RotateLeft64(v13, -32) 48 v9 += v13 49 v5 ^= v9 50 v5 = bits.RotateLeft64(v5, -24) 51 v2 += m[s[2]] 52 v2 += v6 53 v14 ^= v2 54 v14 = bits.RotateLeft64(v14, -32) 55 v10 += v14 56 v6 ^= v10 57 v6 = bits.RotateLeft64(v6, -24) 58 v3 += m[s[3]] 59 v3 += v7 60 v15 ^= v3 61 v15 = bits.RotateLeft64(v15, -32) 62 v11 += v15 63 v7 ^= v11 64 v7 = bits.RotateLeft64(v7, -24) 65 66 v0 += m[s[4]] 67 v0 += v4 68 v12 ^= v0 69 v12 = bits.RotateLeft64(v12, -16) 70 v8 += v12 71 v4 ^= v8 72 v4 = bits.RotateLeft64(v4, -63) 73 v1 += m[s[5]] 74 v1 += v5 75 v13 ^= v1 76 v13 = bits.RotateLeft64(v13, -16) 77 v9 += v13 78 v5 ^= v9 79 v5 = bits.RotateLeft64(v5, -63) 80 v2 += m[s[6]] 81 v2 += v6 82 v14 ^= v2 83 v14 = bits.RotateLeft64(v14, -16) 84 v10 += v14 85 v6 ^= v10 86 v6 = bits.RotateLeft64(v6, -63) 87 v3 += m[s[7]] 88 v3 += v7 89 v15 ^= v3 90 v15 = bits.RotateLeft64(v15, -16) 91 v11 += v15 92 v7 ^= v11 93 v7 = bits.RotateLeft64(v7, -63) 94 95 v0 += m[s[8]] 96 v0 += v5 97 v15 ^= v0 98 v15 = bits.RotateLeft64(v15, -32) 99 v10 += v15 100 v5 ^= v10 101 v5 = bits.RotateLeft64(v5, -24) 102 v1 += m[s[9]] 103 v1 += v6 104 v12 ^= v1 105 v12 = bits.RotateLeft64(v12, -32) 106 v11 += v12 107 v6 ^= v11 108 v6 = bits.RotateLeft64(v6, -24) 109 v2 += m[s[10]] 110 v2 += v7 111 v13 ^= v2 112 v13 = bits.RotateLeft64(v13, -32) 113 v8 += v13 114 v7 ^= v8 115 v7 = bits.RotateLeft64(v7, -24) 116 v3 += m[s[11]] 117 v3 += v4 118 v14 ^= v3 119 v14 = bits.RotateLeft64(v14, -32) 120 v9 += v14 121 v4 ^= v9 122 v4 = bits.RotateLeft64(v4, -24) 123 124 v0 += m[s[12]] 125 v0 += v5 126 v15 ^= v0 127 v15 = bits.RotateLeft64(v15, -16) 128 v10 += v15 129 v5 ^= v10 130 v5 = bits.RotateLeft64(v5, -63) 131 v1 += m[s[13]] 132 v1 += v6 133 v12 ^= v1 134 v12 = bits.RotateLeft64(v12, -16) 135 v11 += v12 136 v6 ^= v11 137 v6 = bits.RotateLeft64(v6, -63) 138 v2 += m[s[14]] 139 v2 += v7 140 v13 ^= v2 141 v13 = bits.RotateLeft64(v13, -16) 142 v8 += v13 143 v7 ^= v8 144 v7 = bits.RotateLeft64(v7, -63) 145 v3 += m[s[15]] 146 v3 += v4 147 v14 ^= v3 148 v14 = bits.RotateLeft64(v14, -16) 149 v9 += v14 150 v4 ^= v9 151 v4 = bits.RotateLeft64(v4, -63) 152 } 153 h[0] ^= v0 ^ v8 154 h[1] ^= v1 ^ v9 155 h[2] ^= v2 ^ v10 156 h[3] ^= v3 ^ v11 157 h[4] ^= v4 ^ v12 158 h[5] ^= v5 ^ v13 159 h[6] ^= v6 ^ v14 160 h[7] ^= v7 ^ v15 161 }