Source file src/crypto/internal/fips140/sha3/_asm/keccakf1600x4_amd64_asm.go

     1  // Copyright 2026 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 main
     6  
     7  import (
     8  	. "github.com/mmcloughlin/avo/build"
     9  	. "github.com/mmcloughlin/avo/operand"
    10  	. "github.com/mmcloughlin/avo/reg"
    11  )
    12  
    13  // keccakF1600x4 implements the permutation for four states at once with AVX2,
    14  // one 64-bit Keccak lane from each state per 256-bit register.
    15  //
    16  // The four states are kept interleaved in two ping-pong stack buffers of 25
    17  // 32-byte rows each, with the θ D values, the current χ row, and the ρ
    18  // byte-rotation shuffle controls in registers.
    19  //
    20  //	Y0-Y4    θ D values (persist for the round)
    21  //	Y5-Y9    θ C values, then the ρ+π inputs of the current output row
    22  //	Y10-Y11  temporaries
    23  //	Y12-Y13  VPSHUFB controls for the ρ rotations by 8 and 56
    24  //	Y14      round constant, χ and ι results
    25  func keccakF1600x4() {
    26  	Implement("keccakF1600x4AVX2")
    27  	buf := AllocLocal(2*bufSize + 32)
    28  
    29  	var p [4]GPPhysical = [4]GPPhysical{RDI, RSI, RDX, RCX}
    30  	Load(Param("a0"), p[0])
    31  	Load(Param("a1"), p[1])
    32  	Load(Param("a2"), p[2])
    33  	Load(Param("a3"), p[3])
    34  
    35  	Comment("Align the state buffers to 32 bytes")
    36  	LEAQ(buf.Offset(31), R10)
    37  	ANDQ(I8(-32), R10)
    38  
    39  	x4TransposeIn(p)
    40  
    41  	VMOVDQU(rho8_DATA(), Y12)
    42  	VMOVDQU(rho56_DATA(), Y13)
    43  
    44  	Comment("Loop over the rounds, two per iteration to alternate buffers")
    45  	LEAQ(rc_DATA(), R11)
    46  	MOVQ(U32(12), R8)
    47  	Label("loop")
    48  	x4Round(0, 1, Mem{Base: R11})
    49  	x4Round(1, 0, Mem{Base: R11, Disp: 8})
    50  	ADDQ(Imm(16), R11)
    51  	DECQ(R8)
    52  	JNE(LabelRef("loop"))
    53  
    54  	x4TransposeOut(p)
    55  
    56  	VZEROUPPER()
    57  	RET()
    58  }
    59  
    60  const bufSize = 25 * 32
    61  
    62  // lane returns the 32-byte row holding lane i of the four states in the
    63  // ping-pong buffer buf (0 or 1).
    64  func lane(buf, i int) Mem {
    65  	return Mem{Base: R10, Disp: buf*bufSize + i*32}
    66  }
    67  
    68  // x4Rows lists, for each of the five output rows of a round (b, g, k, m, s),
    69  // the five ρ+π source lanes and their left rotations, derived from FIPS 202
    70  // Sections 3.2.2-3.2.3: output lane (x, y) = (j, r) comes from input lane
    71  // A[(x+3y) mod 5, x], rotated by its ρ offset. The rows match the fused round
    72  // structure of mKeccakRound in keccakf_amd64_asm.go, although χ is computed
    73  // directly, without the complemented lanes trick.
    74  var x4Rows = func() (rows [5][5]struct{ src, rot int }) {
    75  	var rho [25]int
    76  	x, y := 1, 0
    77  	for t := range 24 {
    78  		rho[x+5*y] = (t + 1) * (t + 2) / 2 % 64
    79  		x, y = y, (2*x+3*y)%5
    80  	}
    81  	for r := range 5 {
    82  		for j := range 5 {
    83  			src := (j+3*r)%5 + 5*j
    84  			rows[r][j].src = src
    85  			rows[r][j].rot = rho[src]
    86  		}
    87  	}
    88  	return rows
    89  }()
    90  
    91  // x4Round applies one round, reading the interleaved state from buffer in and
    92  // writing it to buffer out, with the round constant at rc.
    93  func x4Round(in, out int, rc Mem) {
    94  	C := [5]VecPhysical{Y5, Y6, Y7, Y8, Y9}
    95  	D := [5]VecPhysical{Y0, Y1, Y2, Y3, Y4}
    96  	T := C // reused once D is computed
    97  
    98  	Comment("θ: C[x] = A[x,0] ^ A[x,1] ^ A[x,2] ^ A[x,3] ^ A[x,4]")
    99  	for x := range 5 {
   100  		VMOVDQA(lane(in, x), C[x])
   101  		for y := 1; y < 5; y++ {
   102  			VPXOR(lane(in, x+5*y), C[x], C[x])
   103  		}
   104  	}
   105  	Comment("θ: D[x] = C[x-1] ^ (C[x+1] <<< 1)")
   106  	for x := range 5 {
   107  		VPSLLQ(Imm(1), C[(x+1)%5], Y10)
   108  		VPSRLQ(Imm(63), C[(x+1)%5], Y11)
   109  		VPOR(Y11, Y10, Y10)
   110  		VPXOR(C[(x+4)%5], Y10, D[x])
   111  	}
   112  
   113  	for r := range 5 {
   114  		Commentf("ρ and π: row %d of the fused round", r)
   115  		for j := range 5 {
   116  			e := x4Rows[r][j]
   117  			VPXOR(lane(in, e.src), D[e.src%5], T[j])
   118  			switch e.rot {
   119  			case 0:
   120  			case 8:
   121  				VPSHUFB(Y12, T[j], T[j])
   122  			case 56:
   123  				VPSHUFB(Y13, T[j], T[j])
   124  			default:
   125  				VPSLLQ(Imm(uint64(e.rot)), T[j], Y10)
   126  				VPSRLQ(Imm(uint64(64-e.rot)), T[j], T[j])
   127  				VPOR(Y10, T[j], T[j])
   128  			}
   129  		}
   130  		if r == 0 {
   131  			VPBROADCASTQ(rc, Y14)
   132  			Comment("χ and ι")
   133  		} else {
   134  			Comment("χ")
   135  		}
   136  		for j := range 5 {
   137  			VPANDN(T[(j+2)%5], T[(j+1)%5], Y10)
   138  			if r == 0 && j == 0 {
   139  				VPXOR(Y14, Y10, Y10)
   140  			}
   141  			VPXOR(Y10, T[j], Y10)
   142  			VMOVDQA(Y10, lane(out, r*5+j))
   143  		}
   144  	}
   145  }
   146  
   147  // x4TransposeIn interleaves the four sequential states into buffer 0, so that
   148  // row i holds lane i of each of the four states.
   149  func x4TransposeIn(p [4]GPPhysical) {
   150  	Comment("Interleave the four states into the stack buffer")
   151  	for q := range 6 {
   152  		for i, pi := range p {
   153  			VMOVDQU(Mem{Base: pi, Disp: q * 32}, [4]VecPhysical{Y0, Y1, Y2, Y3}[i])
   154  		}
   155  		VPUNPCKLQDQ(Y1, Y0, Y4)
   156  		VPUNPCKHQDQ(Y1, Y0, Y5)
   157  		VPUNPCKLQDQ(Y3, Y2, Y6)
   158  		VPUNPCKHQDQ(Y3, Y2, Y7)
   159  		VPERM2I128(Imm(0x20), Y6, Y4, Y8)
   160  		VPERM2I128(Imm(0x20), Y7, Y5, Y9)
   161  		VPERM2I128(Imm(0x31), Y6, Y4, Y10)
   162  		VPERM2I128(Imm(0x31), Y7, Y5, Y11)
   163  		VMOVDQA(Y8, lane(0, 4*q+0))
   164  		VMOVDQA(Y9, lane(0, 4*q+1))
   165  		VMOVDQA(Y10, lane(0, 4*q+2))
   166  		VMOVDQA(Y11, lane(0, 4*q+3))
   167  	}
   168  	VMOVQ(Mem{Base: p[0], Disp: 192}, X0)
   169  	VPINSRQ(Imm(1), Mem{Base: p[1], Disp: 192}, X0, X0)
   170  	VMOVQ(Mem{Base: p[2], Disp: 192}, X1)
   171  	VPINSRQ(Imm(1), Mem{Base: p[3], Disp: 192}, X1, X1)
   172  	VINSERTI128(Imm(1), X1, Y0, Y0)
   173  	VMOVDQA(Y0, lane(0, 24))
   174  }
   175  
   176  // x4TransposeOut writes the interleaved states in buffer 0 back out to the
   177  // four sequential states.
   178  func x4TransposeOut(p [4]GPPhysical) {
   179  	Comment("Deinterleave the stack buffer back into the four states")
   180  	for q := range 6 {
   181  		VMOVDQA(lane(0, 4*q+0), Y0)
   182  		VMOVDQA(lane(0, 4*q+1), Y1)
   183  		VMOVDQA(lane(0, 4*q+2), Y2)
   184  		VMOVDQA(lane(0, 4*q+3), Y3)
   185  		VPUNPCKLQDQ(Y1, Y0, Y4)
   186  		VPUNPCKHQDQ(Y1, Y0, Y5)
   187  		VPUNPCKLQDQ(Y3, Y2, Y6)
   188  		VPUNPCKHQDQ(Y3, Y2, Y7)
   189  		VPERM2I128(Imm(0x20), Y6, Y4, Y8)
   190  		VPERM2I128(Imm(0x20), Y7, Y5, Y9)
   191  		VPERM2I128(Imm(0x31), Y6, Y4, Y10)
   192  		VPERM2I128(Imm(0x31), Y7, Y5, Y11)
   193  		for i, pi := range p {
   194  			VMOVDQU([4]VecPhysical{Y8, Y9, Y10, Y11}[i], Mem{Base: pi, Disp: q * 32})
   195  		}
   196  	}
   197  	VMOVDQA(lane(0, 24), Y0)
   198  	VMOVQ(X0, Mem{Base: p[0], Disp: 192})
   199  	VPEXTRQ(Imm(1), X0, Mem{Base: p[1], Disp: 192})
   200  	VEXTRACTI128(Imm(1), Y0, X1)
   201  	VMOVQ(X1, Mem{Base: p[2], Disp: 192})
   202  	VPEXTRQ(Imm(1), X1, Mem{Base: p[3], Disp: 192})
   203  }
   204  
   205  var rho8Mem, rho56Mem, rcMem *Mem
   206  
   207  // rho8_DATA returns the VPSHUFB control for a 64-bit rotation left by 8.
   208  func rho8_DATA() Mem {
   209  	if rho8Mem != nil {
   210  		return *rho8Mem
   211  	}
   212  	rho8 := GLOBL("rho8", RODATA|NOPTR)
   213  	rho8Mem = &rho8
   214  	DATA(0x00, U64(0x0605040302010007))
   215  	DATA(0x08, U64(0x0e0d0c0b0a09080f))
   216  	DATA(0x10, U64(0x0605040302010007))
   217  	DATA(0x18, U64(0x0e0d0c0b0a09080f))
   218  	return rho8
   219  }
   220  
   221  // rho56_DATA returns the VPSHUFB control for a 64-bit rotation left by 56.
   222  func rho56_DATA() Mem {
   223  	if rho56Mem != nil {
   224  		return *rho56Mem
   225  	}
   226  	rho56 := GLOBL("rho56", RODATA|NOPTR)
   227  	rho56Mem = &rho56
   228  	DATA(0x00, U64(0x0007060504030201))
   229  	DATA(0x08, U64(0x080f0e0d0c0b0a09))
   230  	DATA(0x10, U64(0x0007060504030201))
   231  	DATA(0x18, U64(0x080f0e0d0c0b0a09))
   232  	return rho56
   233  }
   234  
   235  // rc_DATA returns the table of round constants for the ι step.
   236  func rc_DATA() Mem {
   237  	if rcMem != nil {
   238  		return *rcMem
   239  	}
   240  	rc := GLOBL("rc", RODATA|NOPTR)
   241  	rcMem = &rc
   242  	for i, c := range RoundConstants {
   243  		DATA(8*i, U64(c))
   244  	}
   245  	return rc
   246  }
   247  

View as plain text