mat.go raw

   1  // Code generated from kyber512/internal/mat.go by gen.go
   2  
   3  package internal
   4  
   5  import (
   6  	"github.com/cloudflare/circl/pke/kyber/internal/common"
   7  )
   8  
   9  // A k by k matrix of polynomials.
  10  type Mat [K]Vec
  11  
  12  // Expands the given seed to the corresponding matrix A or its transpose Aᵀ.
  13  func (m *Mat) Derive(seed *[32]byte, transpose bool) {
  14  	if !common.DeriveX4Available {
  15  		if transpose {
  16  			for i := 0; i < K; i++ {
  17  				for j := 0; j < K; j++ {
  18  					m[i][j].DeriveUniform(seed, uint8(i), uint8(j))
  19  				}
  20  			}
  21  		} else {
  22  			for i := 0; i < K; i++ {
  23  				for j := 0; j < K; j++ {
  24  					m[i][j].DeriveUniform(seed, uint8(j), uint8(i))
  25  				}
  26  			}
  27  		}
  28  		return
  29  	}
  30  
  31  	var ps [4]*common.Poly
  32  	var xs [4]uint8
  33  	var ys [4]uint8
  34  	x := uint8(0)
  35  	y := uint8(0)
  36  
  37  	for x != K {
  38  		idx := 0
  39  		for ; idx < 4; idx++ {
  40  			ps[idx] = &m[x][y]
  41  
  42  			if transpose {
  43  				xs[idx] = x
  44  				ys[idx] = y
  45  			} else {
  46  				xs[idx] = y
  47  				ys[idx] = x
  48  			}
  49  
  50  			y++
  51  			if y == K {
  52  				x++
  53  				y = 0
  54  
  55  				if x == K {
  56  					if idx == 0 {
  57  						// If there is just one left, then a plain DeriveUniform
  58  						// is quicker than the X4 variant.
  59  						ps[0].DeriveUniform(seed, xs[0], ys[0])
  60  						return
  61  					}
  62  
  63  					for idx++; idx < 4; idx++ {
  64  						ps[idx] = nil
  65  					}
  66  
  67  					break
  68  				}
  69  			}
  70  		}
  71  
  72  		common.PolyDeriveUniformX4(ps, seed, xs, ys)
  73  	}
  74  }
  75  
  76  // Transposes A in place.
  77  func (m *Mat) Transpose() {
  78  	for i := 0; i < K-1; i++ {
  79  		for j := i + 1; j < K; j++ {
  80  			t := m[i][j]
  81  			m[i][j] = m[j][i]
  82  			m[j][i] = t
  83  		}
  84  	}
  85  }
  86