git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/crypto/chacha/chacha_amd64.go (about)

     1  // Copyright (c) 2017 Andreas Auernhammer. All rights reserved.
     2  // Use of this source code is governed by a license that can be
     3  // found in the LICENSE file.
     4  
     5  //go:build go1.7 && amd64 && !gccgo && !appengine && !nacl
     6  // +build go1.7,amd64,!gccgo,!appengine,!nacl
     7  
     8  package chacha
     9  
    10  import "golang.org/x/sys/cpu"
    11  
    12  func init() {
    13  	useSSE2 = cpu.X86.HasSSE2
    14  	useSSSE3 = cpu.X86.HasSSSE3
    15  	useAVX = cpu.X86.HasAVX
    16  	useAVX2 = cpu.X86.HasAVX2
    17  }
    18  
    19  // This function is implemented in chacha_amd64.s
    20  //
    21  //go:noescape
    22  func initialize(state *[64]byte, key []byte, nonce *[16]byte)
    23  
    24  // This function is implemented in chacha_amd64.s
    25  //
    26  //go:noescape
    27  func xorKeyStreamSSE2(dst, src []byte, block, state *[64]byte, rounds int) int
    28  
    29  // This function is implemented in chacha_amd64.s
    30  //
    31  //go:noescape
    32  func xorKeyStreamSSSE3(dst, src []byte, block, state *[64]byte, rounds int) int
    33  
    34  // This function is implemented in chacha_amd64.s
    35  //
    36  //go:noescape
    37  func xorKeyStreamAVX(dst, src []byte, block, state *[64]byte, rounds int) int
    38  
    39  // This function is implemented in chachaAVX2_amd64.s
    40  //
    41  //go:noescape
    42  func xorKeyStreamAVX2(dst, src []byte, block, state *[64]byte, rounds int) int
    43  
    44  func xorKeyStream(dst, src []byte, block, state *[64]byte, rounds int) int {
    45  	switch {
    46  	case useAVX2:
    47  		return xorKeyStreamAVX2(dst, src, block, state, rounds)
    48  	case useAVX:
    49  		return xorKeyStreamAVX(dst, src, block, state, rounds)
    50  	case useSSSE3:
    51  		return xorKeyStreamSSSE3(dst, src, block, state, rounds)
    52  	case useSSE2:
    53  		return xorKeyStreamSSE2(dst, src, block, state, rounds)
    54  	default:
    55  		return xorKeyStreamGeneric(dst, src, block, state, rounds)
    56  	}
    57  }