github.com/deroproject/derosuite@v2.1.6-1.0.20200307070847-0f2e589c7a2b+incompatible/cryptonight/hash.go (about)

     1  // Copyright 2017-2018 DERO Project. All rights reserved.
     2  // Use of this source code in any form is governed by RESEARCH license.
     3  // license can be found in the LICENSE file.
     4  // GPG: 0F39 E425 8C65 3947 702A  8234 08B2 0360 A03A 9DE8
     5  //
     6  //
     7  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
     8  // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     9  // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
    10  // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    11  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    12  // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    13  // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    14  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
    15  // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    16  
    17  package cryptonight
    18  
    19  type size_t uint32
    20  type __locale_data struct {
    21  }
    22  type __locale_t struct {
    23  	__locales       [][]__locale_data
    24  	__ctype_b       []uint16
    25  	__ctype_tolower []int
    26  	__ctype_toupper []int
    27  	__names         [][]byte
    28  }
    29  type locale_t struct {
    30  	__locales       [][]__locale_data
    31  	__ctype_b       []uint16
    32  	__ctype_tolower []int
    33  	__ctype_toupper []int
    34  	__names         [][]byte
    35  }
    36  type ptrdiff_t int32
    37  type wchar_t int
    38  type max_align_t struct {
    39  	__clang_max_align_nonce1 int64
    40  	__clang_max_align_nonce2 float64
    41  }
    42  
    43  var sbox []uint8 = []uint8{99, 124, 119, 123, 242, 107, 111, 197, 48, 1, 103, 43, 254, 215, 171, 118, 202, 130, 201, 125, 250, 89, 71, 240, 173, 212, 162, 175, 156, 164, 114, 192, 183, 253, 147, 38, 54, 63, 247, 204, 52, 165, 229, 241, 113, 216, 49, 21, 4, 199, 35, 195, 24, 150, 5, 154, 7, 18, 128, 226, 235, 39, 178, 117, 9, 131, 44, 26, 27, 110, 90, 160, 82, 59, 214, 179, 41, 227, 47, 132, 83, 209, 0, 237, 32, 252, 177, 91, 106, 203, 190, 57, 74, 76, 88, 207, 208, 239, 170, 251, 67, 77, 51, 133, 69, 249, 2, 127, 80, 60, 159, 168, 81, 163, 64, 143, 146, 157, 56, 245, 188, 182, 218, 33, 16, 255, 243, 210, 205, 12, 19, 236, 95, 151, 68, 23, 196, 167, 126, 61, 100, 93, 25, 115, 96, 129, 79, 220, 34, 42, 144, 136, 70, 238, 184, 20, 222, 94, 11, 219, 224, 50, 58, 10, 73, 6, 36, 92, 194, 211, 172, 98, 145, 149, 228, 121, 231, 200, 55, 109, 141, 213, 78, 169, 108, 86, 244, 234, 101, 122, 174, 8, 186, 120, 37, 46, 28, 166, 180, 198, 232, 221, 116, 31, 75, 189, 139, 138, 112, 62, 181, 102, 72, 3, 246, 14, 97, 53, 87, 185, 134, 193, 29, 158, 225, 248, 152, 17, 105, 217, 142, 148, 155, 30, 135, 233, 206, 85, 40, 223, 140, 161, 137, 13, 191, 230, 66, 104, 65, 153, 45, 15, 176, 84, 187, 22}
    44  var mul2 []uint8 = []uint8{0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182, 184, 186, 188, 190, 192, 194, 196, 198, 200, 202, 204, 206, 208, 210, 212, 214, 216, 218, 220, 222, 224, 226, 228, 230, 232, 234, 236, 238, 240, 242, 244, 246, 248, 250, 252, 254, 27, 25, 31, 29, 19, 17, 23, 21, 11, 9, 15, 13, 3, 1, 7, 5, 59, 57, 63, 61, 51, 49, 55, 53, 43, 41, 47, 45, 35, 33, 39, 37, 91, 89, 95, 93, 83, 81, 87, 85, 75, 73, 79, 77, 67, 65, 71, 69, 123, 121, 127, 125, 115, 113, 119, 117, 107, 105, 111, 109, 99, 97, 103, 101, 155, 153, 159, 157, 147, 145, 151, 149, 139, 137, 143, 141, 131, 129, 135, 133, 187, 185, 191, 189, 179, 177, 183, 181, 171, 169, 175, 173, 163, 161, 167, 165, 219, 217, 223, 221, 211, 209, 215, 213, 203, 201, 207, 205, 195, 193, 199, 197, 251, 249, 255, 253, 243, 241, 247, 245, 235, 233, 239, 237, 227, 225, 231, 229}
    45  
    46  func mix_bytes(i0 uint8, i1 uint8, i2 uint8, i3 uint8, i4 uint8, i5 uint8, i6 uint8, i7 uint8, output []uint8) {
    47  	var t0 uint8
    48  	var t1 uint8
    49  	var t2 uint8
    50  	var t3 uint8
    51  	var t4 uint8
    52  	var t5 uint8
    53  	var t6 uint8
    54  	var t7 uint8
    55  	var x0 uint8
    56  	var x1 uint8
    57  	var x2 uint8
    58  	var x3 uint8
    59  	var x4 uint8
    60  	var x5 uint8
    61  	var x6 uint8
    62  	var x7 uint8
    63  	var y0 uint8
    64  	var y1 uint8
    65  	var y2 uint8
    66  	var y3 uint8
    67  	var y4 uint8
    68  	var y5 uint8
    69  	var y6 uint8
    70  	var y7 uint8
    71  	t0 = i0 ^ i1
    72  	t1 = i1 ^ i2
    73  	t2 = i2 ^ i3
    74  	t3 = i3 ^ i4
    75  	t4 = i4 ^ i5
    76  	t5 = i5 ^ i6
    77  	t6 = i6 ^ i7
    78  	t7 = i7 ^ i0
    79  	x0 = t0 ^ t3
    80  	x1 = t1 ^ t4
    81  	x2 = t2 ^ t5
    82  	x3 = t3 ^ t6
    83  	x4 = t4 ^ t7
    84  	x5 = t5 ^ t0
    85  	x6 = t6 ^ t1
    86  	x7 = t7 ^ t2
    87  	y0 = t0 ^ t2 ^ i6
    88  	y1 = t1 ^ t3 ^ i7
    89  	y2 = t2 ^ t4 ^ i0
    90  	y3 = t3 ^ t5 ^ i1
    91  	y4 = t4 ^ t6 ^ i2
    92  	y5 = t5 ^ t7 ^ i3
    93  	y6 = t6 ^ t0 ^ i4
    94  	y7 = t7 ^ t1 ^ i5
    95  	x3 = func() uint8 {
    96  		if (x3 & 128) != 0 {
    97  			return (x3 << uint64(1)) ^ 27
    98  		} else {
    99  			return (x3 << uint64(1))
   100  		}
   101  	}()
   102  	x0 = func() uint8 {
   103  		if (x0 & 128) != 0 {
   104  			return (x0 << uint64(1)) ^ 27
   105  		} else {
   106  			return (x0 << uint64(1))
   107  		}
   108  	}()
   109  	t0 = x3 ^ y7
   110  	t0 = func() uint8 {
   111  		if (t0 & 128) != 0 {
   112  			return (t0 << uint64(1)) ^ 27
   113  		} else {
   114  			return (t0 << uint64(1))
   115  		}
   116  	}()
   117  	t5 = x0 ^ y4
   118  	t5 = func() uint8 {
   119  		if (t5 & 128) != 0 {
   120  			return (t5 << uint64(1)) ^ 27
   121  		} else {
   122  			return (t5 << uint64(1))
   123  		}
   124  	}()
   125  	output[0] = t0 ^ y4
   126  	output[5] = t5 ^ y1
   127  	output[1] = mul2[mul2[x4]^y0] ^ y5
   128  	output[2] = mul2[mul2[x5]^y1] ^ y6
   129  	output[3] = mul2[mul2[x6]^y2] ^ y7
   130  	output[4] = mul2[mul2[x7]^y3] ^ y0
   131  	output[6] = mul2[mul2[x1]^y5] ^ y2
   132  	output[7] = mul2[mul2[x2]^y6] ^ y3
   133  }
   134  
   135  func perm_P(input []uint8, output []uint8) {
   136  	var r0 uint8
   137  	var r1 uint8
   138  	var r2 uint8
   139  	var r3 uint8
   140  	var r4 uint8
   141  	var r5 uint8
   142  	var r6 uint8
   143  	var r7 uint8
   144  	var round uint8
   145  	var state []uint8 = make([]uint8, 64, 64)
   146  	var write []uint8 = state
   147  	var read []uint8 = input
   148  	var p_tmp []uint8
   149  	for {
   150  		break
   151  	}
   152  	{
   153  		for round = uint8(0); round < 10; func() uint8 {
   154  			round += 1
   155  			return round
   156  		}() {
   157  			r0 = sbox[read[0]^round]
   158  			r1 = sbox[read[9]]
   159  			r2 = sbox[read[18]]
   160  			r3 = sbox[read[27]]
   161  			r4 = sbox[read[36]]
   162  			r5 = sbox[read[45]]
   163  			r6 = sbox[read[54]]
   164  			r7 = sbox[read[63]]
   165  			mix_bytes(r0, r1, r2, r3, r4, r5, r6, r7, write)
   166  			r0 = sbox[read[8]^round^16]
   167  			r1 = sbox[read[17]]
   168  			r2 = sbox[read[26]]
   169  			r3 = sbox[read[35]]
   170  			r4 = sbox[read[44]]
   171  			r5 = sbox[read[53]]
   172  			r6 = sbox[read[62]]
   173  			r7 = sbox[read[7]]
   174  			mix_bytes(r0, r1, r2, r3, r4, r5, r6, r7, write[8:])
   175  			r0 = sbox[read[16]^round^32]
   176  			r1 = sbox[read[25]]
   177  			r2 = sbox[read[34]]
   178  			r3 = sbox[read[43]]
   179  			r4 = sbox[read[52]]
   180  			r5 = sbox[read[61]]
   181  			r6 = sbox[read[6]]
   182  			r7 = sbox[read[15]]
   183  			mix_bytes(r0, r1, r2, r3, r4, r5, r6, r7, write[16:])
   184  			r0 = sbox[read[24]^round^48]
   185  			r1 = sbox[read[33]]
   186  			r2 = sbox[read[42]]
   187  			r3 = sbox[read[51]]
   188  			r4 = sbox[read[60]]
   189  			r5 = sbox[read[5]]
   190  			r6 = sbox[read[14]]
   191  			r7 = sbox[read[23]]
   192  			mix_bytes(r0, r1, r2, r3, r4, r5, r6, r7, write[24:])
   193  			r0 = sbox[read[32]^round^64]
   194  			r1 = sbox[read[41]]
   195  			r2 = sbox[read[50]]
   196  			r3 = sbox[read[59]]
   197  			r4 = sbox[read[4]]
   198  			r5 = sbox[read[13]]
   199  			r6 = sbox[read[22]]
   200  			r7 = sbox[read[31]]
   201  			mix_bytes(r0, r1, r2, r3, r4, r5, r6, r7, write[32:])
   202  			r0 = sbox[read[40]^round^80]
   203  			r1 = sbox[read[49]]
   204  			r2 = sbox[read[58]]
   205  			r3 = sbox[read[3]]
   206  			r4 = sbox[read[12]]
   207  			r5 = sbox[read[21]]
   208  			r6 = sbox[read[30]]
   209  			r7 = sbox[read[39]]
   210  			mix_bytes(r0, r1, r2, r3, r4, r5, r6, r7, write[40:])
   211  			r0 = sbox[read[48]^round^96]
   212  			r1 = sbox[read[57]]
   213  			r2 = sbox[read[2]]
   214  			r3 = sbox[read[11]]
   215  			r4 = sbox[read[20]]
   216  			r5 = sbox[read[29]]
   217  			r6 = sbox[read[38]]
   218  			r7 = sbox[read[47]]
   219  			mix_bytes(r0, r1, r2, r3, r4, r5, r6, r7, write[48:])
   220  			r0 = sbox[read[56]^round^112]
   221  			r1 = sbox[read[1]]
   222  			r2 = sbox[read[10]]
   223  			r3 = sbox[read[19]]
   224  			r4 = sbox[read[28]]
   225  			r5 = sbox[read[37]]
   226  			r6 = sbox[read[46]]
   227  			r7 = sbox[read[55]]
   228  			mix_bytes(r0, r1, r2, r3, r4, r5, r6, r7, write[56:])
   229  			if round == uint8(0) {
   230  				read = output
   231  			}
   232  			p_tmp = read
   233  			read = write
   234  			write = p_tmp
   235  		}
   236  	}
   237  }
   238  
   239  func perm_Q(input []uint8, output []uint8) {
   240  	var r0 uint8
   241  	var r1 uint8
   242  	var r2 uint8
   243  	var r3 uint8
   244  	var r4 uint8
   245  	var r5 uint8
   246  	var r6 uint8
   247  	var r7 uint8
   248  	var round uint8
   249  	var state []uint8 = make([]uint8, 64, 64)
   250  	var write []uint8 = state
   251  	var read []uint8 = input
   252  	var p_tmp []uint8
   253  	for {
   254  		break
   255  	}
   256  	{
   257  		for round = uint8(0); round < 10; func() uint8 {
   258  			round += 1
   259  			return round
   260  		}() {
   261  			r0 = sbox[read[8]^255]
   262  			r1 = sbox[read[25]^255]
   263  			r2 = sbox[read[42]^255]
   264  			r3 = sbox[read[59]^255]
   265  			r4 = sbox[read[4]^255]
   266  			r5 = sbox[read[21]^255]
   267  			r6 = sbox[read[38]^255]
   268  			r7 = sbox[read[55]^159^round]
   269  			mix_bytes(r0, r1, r2, r3, r4, r5, r6, r7, write)
   270  			r0 = sbox[read[16]^255]
   271  			r1 = sbox[read[33]^255]
   272  			r2 = sbox[read[50]^255]
   273  			r3 = sbox[read[3]^255]
   274  			r4 = sbox[read[12]^255]
   275  			r5 = sbox[read[29]^255]
   276  			r6 = sbox[read[46]^255]
   277  			r7 = sbox[read[63]^143^round]
   278  			mix_bytes(r0, r1, r2, r3, r4, r5, r6, r7, write[8:])
   279  			r0 = sbox[read[24]^255]
   280  			r1 = sbox[read[41]^255]
   281  			r2 = sbox[read[58]^255]
   282  			r3 = sbox[read[11]^255]
   283  			r4 = sbox[read[20]^255]
   284  			r5 = sbox[read[37]^255]
   285  			r6 = sbox[read[54]^255]
   286  			r7 = sbox[read[7]^255^round]
   287  			mix_bytes(r0, r1, r2, r3, r4, r5, r6, r7, write[16:])
   288  			r0 = sbox[read[32]^255]
   289  			r1 = sbox[read[49]^255]
   290  			r2 = sbox[read[2]^255]
   291  			r3 = sbox[read[19]^255]
   292  			r4 = sbox[read[28]^255]
   293  			r5 = sbox[read[45]^255]
   294  			r6 = sbox[read[62]^255]
   295  			r7 = sbox[read[15]^239^round]
   296  			mix_bytes(r0, r1, r2, r3, r4, r5, r6, r7, write[24:])
   297  			r0 = sbox[read[40]^255]
   298  			r1 = sbox[read[57]^255]
   299  			r2 = sbox[read[10]^255]
   300  			r3 = sbox[read[27]^255]
   301  			r4 = sbox[read[36]^255]
   302  			r5 = sbox[read[53]^255]
   303  			r6 = sbox[read[6]^255]
   304  			r7 = sbox[read[23]^223^round]
   305  			mix_bytes(r0, r1, r2, r3, r4, r5, r6, r7, write[32:])
   306  			r0 = sbox[read[48]^255]
   307  			r1 = sbox[read[1]^255]
   308  			r2 = sbox[read[18]^255]
   309  			r3 = sbox[read[35]^255]
   310  			r4 = sbox[read[44]^255]
   311  			r5 = sbox[read[61]^255]
   312  			r6 = sbox[read[14]^255]
   313  			r7 = sbox[read[31]^207^round]
   314  			mix_bytes(r0, r1, r2, r3, r4, r5, r6, r7, write[40:])
   315  			r0 = sbox[read[56]^255]
   316  			r1 = sbox[read[9]^255]
   317  			r2 = sbox[read[26]^255]
   318  			r3 = sbox[read[43]^255]
   319  			r4 = sbox[read[52]^255]
   320  			r5 = sbox[read[5]^255]
   321  			r6 = sbox[read[22]^255]
   322  			r7 = sbox[read[39]^191^round]
   323  			mix_bytes(r0, r1, r2, r3, r4, r5, r6, r7, write[48:])
   324  			r0 = sbox[read[0]^255]
   325  			r1 = sbox[read[17]^255]
   326  			r2 = sbox[read[34]^255]
   327  			r3 = sbox[read[51]^255]
   328  			r4 = sbox[read[60]^255]
   329  			r5 = sbox[read[13]^255]
   330  			r6 = sbox[read[30]^255]
   331  			r7 = sbox[read[47]^175^round]
   332  			mix_bytes(r0, r1, r2, r3, r4, r5, r6, r7, write[56:])
   333  			if round == uint8(0) {
   334  				read = output
   335  			}
   336  			p_tmp = read
   337  			read = write
   338  			write = p_tmp
   339  		}
   340  	}
   341  }
   342  
   343  func crypto_hash(out []uint8, in []uint8, inlen uint64) int {
   344  	if inlen >= (1 << uint64(16)) {
   345  		return -1
   346  	}
   347  	var msg_len uint32 = uint32(uint32(inlen))
   348  	var padded_len uint32 = ((msg_len+9-1)/64)*64 + 64
   349  	var pad_block_len uint8 = uint8(padded_len - msg_len)
   350  	var pad_block []uint8 = make([]uint8, pad_block_len, pad_block_len)
   351  
   352  	pad_block[0] = uint8(128)
   353  	var blocks uint32 = uint32((padded_len >> uint64(6)))
   354  	pad_block[pad_block_len-1] = (uint8(blocks) & 255)
   355  	var h_state []uint8 = make([]uint8, 64, 64)
   356  	var p_state []uint8 = make([]uint8, 64, 64)
   357  	var q_state []uint8 = make([]uint8, 64, 64)
   358  	var x_state []uint8 = make([]uint8, 64, 64)
   359  	var buf []uint8 = make([]uint8, 64, 64)
   360  
   361  	h_state[62] = uint8(1)
   362  	var i uint8
   363  	var block uint32
   364  	var remaining uint32 = uint32(0)
   365  	var message_left_len uint32 = msg_len
   366  	for {
   367  		break
   368  	}
   369  	{
   370  		for block = uint32(0); block < blocks; func() uint32 {
   371  			block += 1
   372  			return block
   373  		}() {
   374  			if block*64+64 < msg_len {
   375  				//memcpy(buf, in+64*block, uint32(64))
   376  				copy(buf, in[64*block:64*block+64]) //copy full block
   377  				message_left_len -= 64
   378  			} else {
   379  				if message_left_len > 0 {
   380  					remaining = uint32(64 - message_left_len)
   381  					//memcpy(buf, in+64*block, message_left_len)
   382  					copy(buf, in[64*block:64*block+message_left_len])
   383  					//memcpy(buf+message_left_len, pad_block, uint32(remaining))
   384  					copy(buf[message_left_len:], pad_block[:remaining])
   385  					message_left_len = uint32(0)
   386  				} else {
   387  					//memcpy(buf, pad_block+remaining, uint32(64))
   388  					copy(buf, pad_block[remaining:remaining+64])
   389  				}
   390  			}
   391  			for {
   392  				break
   393  			}
   394  			{
   395  				for i = uint8(0); i < 64; func() uint8 {
   396  					i += 1
   397  					return i
   398  				}() {
   399  					x_state[i] = buf[i] ^ h_state[i]
   400  				}
   401  			}
   402  			perm_P(x_state, p_state)
   403  			perm_Q(buf, q_state)
   404  			for {
   405  				break
   406  			}
   407  			{
   408  				for i = uint8(0); i < 64; func() uint8 {
   409  					i += 1
   410  					return i
   411  				}() {
   412  					h_state[i] ^= p_state[i] ^ q_state[i]
   413  				}
   414  			}
   415  		}
   416  	}
   417  	perm_P(h_state, p_state)
   418  	for {
   419  		break
   420  	}
   421  	{
   422  		for i = uint8(32); i < 64; func() uint8 {
   423  			i += 1
   424  			return i
   425  		}() {
   426  			out[i-32] = h_state[i] ^ p_state[i]
   427  		}
   428  	}
   429  	return 0
   430  }
   431  func __init() {
   432  }