github.com/theQRL/go-zond@v0.1.1/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  	"encoding/binary"
     9  	"math/bits"
    10  )
    11  
    12  // the precomputed values for BLAKE2b
    13  // there are 10 16-byte arrays - one for each round
    14  // the entries are calculated from the sigma constants.
    15  var precomputed = [10][16]byte{
    16  	{0, 2, 4, 6, 1, 3, 5, 7, 8, 10, 12, 14, 9, 11, 13, 15},
    17  	{14, 4, 9, 13, 10, 8, 15, 6, 1, 0, 11, 5, 12, 2, 7, 3},
    18  	{11, 12, 5, 15, 8, 0, 2, 13, 10, 3, 7, 9, 14, 6, 1, 4},
    19  	{7, 3, 13, 11, 9, 1, 12, 14, 2, 5, 4, 15, 6, 10, 0, 8},
    20  	{9, 5, 2, 10, 0, 7, 4, 15, 14, 11, 6, 3, 1, 12, 8, 13},
    21  	{2, 6, 0, 8, 12, 10, 11, 3, 4, 7, 15, 1, 13, 5, 14, 9},
    22  	{12, 1, 14, 4, 5, 15, 13, 10, 0, 6, 9, 8, 7, 3, 2, 11},
    23  	{13, 7, 12, 3, 11, 14, 1, 9, 5, 15, 8, 2, 0, 4, 6, 10},
    24  	{6, 14, 11, 0, 15, 9, 3, 8, 12, 13, 1, 10, 2, 7, 4, 5},
    25  	{10, 8, 7, 1, 2, 4, 6, 5, 15, 9, 3, 13, 11, 14, 12, 0},
    26  }
    27  
    28  // nolint:unused,deadcode
    29  func hashBlocksGeneric(h *[8]uint64, c *[2]uint64, flag uint64, blocks []byte) {
    30  	var m [16]uint64
    31  	c0, c1 := c[0], c[1]
    32  
    33  	for i := 0; i < len(blocks); {
    34  		c0 += BlockSize
    35  		if c0 < BlockSize {
    36  			c1++
    37  		}
    38  		for j := range m {
    39  			m[j] = binary.LittleEndian.Uint64(blocks[i:])
    40  			i += 8
    41  		}
    42  		fGeneric(h, &m, c0, c1, flag, 12)
    43  	}
    44  	c[0], c[1] = c0, c1
    45  }
    46  
    47  func fGeneric(h *[8]uint64, m *[16]uint64, c0, c1 uint64, flag uint64, rounds uint64) {
    48  	v0, v1, v2, v3, v4, v5, v6, v7 := h[0], h[1], h[2], h[3], h[4], h[5], h[6], h[7]
    49  	v8, v9, v10, v11, v12, v13, v14, v15 := iv[0], iv[1], iv[2], iv[3], iv[4], iv[5], iv[6], iv[7]
    50  	v12 ^= c0
    51  	v13 ^= c1
    52  	v14 ^= flag
    53  
    54  	for i := 0; i < int(rounds); i++ {
    55  		s := &(precomputed[i%10])
    56  
    57  		v0 += m[s[0]]
    58  		v0 += v4
    59  		v12 ^= v0
    60  		v12 = bits.RotateLeft64(v12, -32)
    61  		v8 += v12
    62  		v4 ^= v8
    63  		v4 = bits.RotateLeft64(v4, -24)
    64  		v1 += m[s[1]]
    65  		v1 += v5
    66  		v13 ^= v1
    67  		v13 = bits.RotateLeft64(v13, -32)
    68  		v9 += v13
    69  		v5 ^= v9
    70  		v5 = bits.RotateLeft64(v5, -24)
    71  		v2 += m[s[2]]
    72  		v2 += v6
    73  		v14 ^= v2
    74  		v14 = bits.RotateLeft64(v14, -32)
    75  		v10 += v14
    76  		v6 ^= v10
    77  		v6 = bits.RotateLeft64(v6, -24)
    78  		v3 += m[s[3]]
    79  		v3 += v7
    80  		v15 ^= v3
    81  		v15 = bits.RotateLeft64(v15, -32)
    82  		v11 += v15
    83  		v7 ^= v11
    84  		v7 = bits.RotateLeft64(v7, -24)
    85  
    86  		v0 += m[s[4]]
    87  		v0 += v4
    88  		v12 ^= v0
    89  		v12 = bits.RotateLeft64(v12, -16)
    90  		v8 += v12
    91  		v4 ^= v8
    92  		v4 = bits.RotateLeft64(v4, -63)
    93  		v1 += m[s[5]]
    94  		v1 += v5
    95  		v13 ^= v1
    96  		v13 = bits.RotateLeft64(v13, -16)
    97  		v9 += v13
    98  		v5 ^= v9
    99  		v5 = bits.RotateLeft64(v5, -63)
   100  		v2 += m[s[6]]
   101  		v2 += v6
   102  		v14 ^= v2
   103  		v14 = bits.RotateLeft64(v14, -16)
   104  		v10 += v14
   105  		v6 ^= v10
   106  		v6 = bits.RotateLeft64(v6, -63)
   107  		v3 += m[s[7]]
   108  		v3 += v7
   109  		v15 ^= v3
   110  		v15 = bits.RotateLeft64(v15, -16)
   111  		v11 += v15
   112  		v7 ^= v11
   113  		v7 = bits.RotateLeft64(v7, -63)
   114  
   115  		v0 += m[s[8]]
   116  		v0 += v5
   117  		v15 ^= v0
   118  		v15 = bits.RotateLeft64(v15, -32)
   119  		v10 += v15
   120  		v5 ^= v10
   121  		v5 = bits.RotateLeft64(v5, -24)
   122  		v1 += m[s[9]]
   123  		v1 += v6
   124  		v12 ^= v1
   125  		v12 = bits.RotateLeft64(v12, -32)
   126  		v11 += v12
   127  		v6 ^= v11
   128  		v6 = bits.RotateLeft64(v6, -24)
   129  		v2 += m[s[10]]
   130  		v2 += v7
   131  		v13 ^= v2
   132  		v13 = bits.RotateLeft64(v13, -32)
   133  		v8 += v13
   134  		v7 ^= v8
   135  		v7 = bits.RotateLeft64(v7, -24)
   136  		v3 += m[s[11]]
   137  		v3 += v4
   138  		v14 ^= v3
   139  		v14 = bits.RotateLeft64(v14, -32)
   140  		v9 += v14
   141  		v4 ^= v9
   142  		v4 = bits.RotateLeft64(v4, -24)
   143  
   144  		v0 += m[s[12]]
   145  		v0 += v5
   146  		v15 ^= v0
   147  		v15 = bits.RotateLeft64(v15, -16)
   148  		v10 += v15
   149  		v5 ^= v10
   150  		v5 = bits.RotateLeft64(v5, -63)
   151  		v1 += m[s[13]]
   152  		v1 += v6
   153  		v12 ^= v1
   154  		v12 = bits.RotateLeft64(v12, -16)
   155  		v11 += v12
   156  		v6 ^= v11
   157  		v6 = bits.RotateLeft64(v6, -63)
   158  		v2 += m[s[14]]
   159  		v2 += v7
   160  		v13 ^= v2
   161  		v13 = bits.RotateLeft64(v13, -16)
   162  		v8 += v13
   163  		v7 ^= v8
   164  		v7 = bits.RotateLeft64(v7, -63)
   165  		v3 += m[s[15]]
   166  		v3 += v4
   167  		v14 ^= v3
   168  		v14 = bits.RotateLeft64(v14, -16)
   169  		v9 += v14
   170  		v4 ^= v9
   171  		v4 = bits.RotateLeft64(v4, -63)
   172  	}
   173  	h[0] ^= v0 ^ v8
   174  	h[1] ^= v1 ^ v9
   175  	h[2] ^= v2 ^ v10
   176  	h[3] ^= v3 ^ v11
   177  	h[4] ^= v4 ^ v12
   178  	h[5] ^= v5 ^ v13
   179  	h[6] ^= v6 ^ v14
   180  	h[7] ^= v7 ^ v15
   181  }