Source file src/crypto/internal/fips140/sha3/readmulti.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 sha3
     6  
     7  import (
     8  	"crypto/internal/fips140"
     9  	"slices"
    10  )
    11  
    12  // ReadMulti reads output (like [SHAKE.Read] would) from each element of s into
    13  // the corresponding element of out. It behaves exactly like
    14  //
    15  //	for i := range s {
    16  //	    s[i].Read(out[i])
    17  //	}
    18  //
    19  // Depending on the platform, groups of states with the same rate, sponge phase,
    20  // and output offset may be processed together, which can be faster than reading
    21  // them one at a time.
    22  //
    23  // ReadMulti panics if len(s) != len(out). The output slices may have different
    24  // lengths, including zero, and must not overlap each other.
    25  func ReadMulti(s []*SHAKE, out [][]byte) {
    26  	if len(s) != len(out) {
    27  		panic("sha3: ReadMulti: mismatched lengths")
    28  	}
    29  	fips140.RecordApproved()
    30  	for len(s) > 0 {
    31  		k := min(batchSize, len(s))
    32  		if k >= 2 && batchable(s[:k]) {
    33  			readMultiGroup(s[:k], out[:k])
    34  		} else {
    35  			for i := range s[:k] {
    36  				s[i].d.read(out[i])
    37  			}
    38  		}
    39  		s, out = s[k:], out[k:]
    40  	}
    41  }
    42  
    43  // batchSize is the maximum number of states passed to a Keccak-f[1600] kernel
    44  // together. It matches the widest kernel arity (four lanes with AVX2).
    45  const batchSize = 4
    46  
    47  // batchable reports whether the states can be read in lockstep.
    48  func batchable(s []*SHAKE) bool {
    49  	// The states must be non-nil and distinct.
    50  	for i, x := range s {
    51  		if x == nil {
    52  			return false
    53  		}
    54  		if slices.Contains(s[i+1:], x) {
    55  			return false
    56  		}
    57  	}
    58  	// The states must have the same rate, sponge phase, and output offset.
    59  	d0 := &s[0].d
    60  	for _, x := range s[1:] {
    61  		if x.d.rate != d0.rate || x.d.state != d0.state {
    62  			return false
    63  		}
    64  		if d0.state == spongeSqueezing && x.d.n != d0.n {
    65  			return false
    66  		}
    67  	}
    68  	return true
    69  }
    70  
    71  // readMultiGroup reads o[i] from s[i] for a group of two to batchSize lockstep
    72  // states (as checked by [batchable]), running their permutations together.
    73  func readMultiGroup(s []*SHAKE, o [][]byte) {
    74  	// Copy the output slices, to advance them below without corrupting the
    75  	// caller's slice. The fixed-size array with indexed stores (not append
    76  	// or copy) keeps escape analysis from moving the buffers to the heap.
    77  	var out [batchSize][]byte
    78  	for i := range o {
    79  		out[i] = o[i]
    80  	}
    81  
    82  	// If the states are still absorbing, pad and permute all of them,
    83  	// like [Digest.padAndPermute].
    84  	if s[0].d.state == spongeAbsorbing {
    85  		var p [batchSize]*[200]byte
    86  		for i := range s {
    87  			d := &s[i].d
    88  			d.a[d.n] ^= d.dsbyte
    89  			d.a[d.rate-1] ^= 0x80
    90  			d.state = spongeSqueezing
    91  			d.n = 0
    92  			p[i] = &d.a
    93  		}
    94  		keccakF1600Multi(p[:len(s)])
    95  	}
    96  
    97  	rate := s[0].d.rate
    98  	for {
    99  		var p [batchSize]*[200]byte
   100  		m := 0
   101  		for i := range s {
   102  			d := &s[i].d
   103  			// Copy out any remaining buffered output.
   104  			if len(out[i]) > 0 && d.n < rate {
   105  				x := copy(out[i], d.a[d.n:rate])
   106  				d.n += x
   107  				out[i] = out[i][x:]
   108  			}
   109  			// If this state still needs to produce more,
   110  			// add it to the permutation batch.
   111  			if len(out[i]) > 0 {
   112  				p[m] = &d.a
   113  				m++
   114  				d.n = 0
   115  			}
   116  		}
   117  		if m == 0 {
   118  			return
   119  		}
   120  		keccakF1600Multi(p[:m])
   121  	}
   122  }
   123  
   124  // keccakF1600MultiGeneric applies the permutation to all states.
   125  func keccakF1600MultiGeneric(a []*[200]byte) {
   126  	for _, s := range a {
   127  		keccakF1600(s)
   128  	}
   129  }
   130  

View as plain text