Source file src/crypto/internal/fips140/mlkem/field.go

     1  // Copyright 2024 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 mlkem
     6  
     7  import (
     8  	"crypto/internal/fips140/sha3"
     9  	"crypto/internal/fips140deps/byteorder"
    10  	"errors"
    11  )
    12  
    13  // fieldElement is an integer modulo q, an element of ℤ_q. It is always reduced.
    14  type fieldElement uint16
    15  
    16  // fieldCheckReduced checks that a value a is < q.
    17  func fieldCheckReduced(a uint16) (fieldElement, error) {
    18  	if a >= q {
    19  		return 0, errors.New("unreduced field element")
    20  	}
    21  	return fieldElement(a), nil
    22  }
    23  
    24  // fieldReduceOnce reduces a value a < 2q.
    25  func fieldReduceOnce(a uint16) fieldElement {
    26  	x := a - q
    27  	// If x underflowed, then x >= 2¹⁶ - q > 2¹⁵, so the top bit is set.
    28  	x += (x >> 15) * q
    29  	return fieldElement(x)
    30  }
    31  
    32  func fieldAdd(a, b fieldElement) fieldElement {
    33  	x := uint16(a + b)
    34  	return fieldReduceOnce(x)
    35  }
    36  
    37  func fieldSub(a, b fieldElement) fieldElement {
    38  	x := uint16(a - b + q)
    39  	return fieldReduceOnce(x)
    40  }
    41  
    42  const (
    43  	barrettMultiplier = 5039 // 2¹² * 2¹² / q
    44  	barrettShift      = 24   // log₂(2¹² * 2¹²)
    45  )
    46  
    47  // fieldReduce reduces a value a < 2q² using Barrett reduction, to avoid
    48  // potentially variable-time division.
    49  func fieldReduce(a uint32) fieldElement {
    50  	quotient := uint32((uint64(a) * barrettMultiplier) >> barrettShift)
    51  	return fieldReduceOnce(uint16(a - quotient*q))
    52  }
    53  
    54  func fieldMul(a, b fieldElement) fieldElement {
    55  	x := uint32(a) * uint32(b)
    56  	return fieldReduce(x)
    57  }
    58  
    59  // fieldMulSub returns a * (b - c). This operation is fused to save a
    60  // fieldReduceOnce after the subtraction.
    61  func fieldMulSub(a, b, c fieldElement) fieldElement {
    62  	x := uint32(a) * uint32(b-c+q)
    63  	return fieldReduce(x)
    64  }
    65  
    66  // fieldAddMul returns a * b + c * d. This operation is fused to save a
    67  // fieldReduceOnce and a fieldReduce.
    68  func fieldAddMul(a, b, c, d fieldElement) fieldElement {
    69  	x := uint32(a) * uint32(b)
    70  	x += uint32(c) * uint32(d)
    71  	return fieldReduce(x)
    72  }
    73  
    74  // compress maps a field element uniformly to the range 0 to 2ᵈ-1, according to
    75  // FIPS 203, Definition 4.7.
    76  func compress(x fieldElement, d uint8) uint16 {
    77  	// We want to compute (x * 2ᵈ) / q, rounded to nearest integer, with 1/2
    78  	// rounding up (see FIPS 203, Section 2.3).
    79  
    80  	// Barrett reduction produces a quotient and a remainder in the range [0, 2q),
    81  	// such that dividend = quotient * q + remainder.
    82  	dividend := uint32(x) << d // x * 2ᵈ
    83  	quotient := uint32(uint64(dividend) * barrettMultiplier >> barrettShift)
    84  	remainder := dividend - quotient*q
    85  
    86  	// Since the remainder is in the range [0, 2q), not [0, q), we need to
    87  	// portion it into three spans for rounding.
    88  	//
    89  	//     [ 0,       q/2     ) -> round to 0
    90  	//     [ q/2,     q + q/2 ) -> round to 1
    91  	//     [ q + q/2, 2q      ) -> round to 2
    92  	//
    93  	// We can convert that to the following logic: add 1 if remainder > q/2,
    94  	// then add 1 again if remainder > q + q/2.
    95  	//
    96  	// Note that if remainder > x, then ⌊x⌋ - remainder underflows, and the top
    97  	// bit of the difference will be set.
    98  	quotient += (q/2 - remainder) >> 31 & 1
    99  	quotient += (q + q/2 - remainder) >> 31 & 1
   100  
   101  	// quotient might have overflowed at this point, so reduce it by masking.
   102  	var mask uint32 = (1 << d) - 1
   103  	return uint16(quotient & mask)
   104  }
   105  
   106  // decompress maps a number x between 0 and 2ᵈ-1 uniformly to the full range of
   107  // field elements, according to FIPS 203, Definition 4.8.
   108  func decompress(y uint16, d uint8) fieldElement {
   109  	// We want to compute (y * q) / 2ᵈ, rounded to nearest integer, with 1/2
   110  	// rounding up (see FIPS 203, Section 2.3).
   111  
   112  	dividend := uint32(y) * q
   113  	quotient := dividend >> d // (y * q) / 2ᵈ
   114  
   115  	// The d'th least-significant bit of the dividend (the most significant bit
   116  	// of the remainder) is 1 for the top half of the values that divide to the
   117  	// same quotient, which are the ones that round up.
   118  	quotient += dividend >> (d - 1) & 1
   119  
   120  	// quotient is at most (2¹¹-1) * q / 2¹¹ + 1 = 3328, so it didn't overflow.
   121  	return fieldElement(quotient)
   122  }
   123  
   124  // ringElement is a polynomial, an element of R_q, represented as an array
   125  // according to FIPS 203, Section 2.4.4.
   126  type ringElement [n]fieldElement
   127  
   128  // polyAdd adds two ringElements or nttElements.
   129  func polyAdd[T ~[n]fieldElement](a, b T) (s T) {
   130  	for i := range s {
   131  		s[i] = fieldAdd(a[i], b[i])
   132  	}
   133  	return s
   134  }
   135  
   136  // polySub subtracts two ringElements or nttElements.
   137  func polySub[T ~[n]fieldElement](a, b T) (s T) {
   138  	for i := range s {
   139  		s[i] = fieldSub(a[i], b[i])
   140  	}
   141  	return s
   142  }
   143  
   144  // polyByteEncode appends the 384-byte encoding of f to b.
   145  //
   146  // It implements ByteEncode₁₂, according to FIPS 203, Algorithm 5.
   147  func polyByteEncode[T ~[n]fieldElement](b []byte, f T) []byte {
   148  	out, B := sliceForAppend(b, encodingSize12)
   149  	for i := 0; i < n; i += 2 {
   150  		x := uint32(f[i]) | uint32(f[i+1])<<12
   151  		B[0] = uint8(x)
   152  		B[1] = uint8(x >> 8)
   153  		B[2] = uint8(x >> 16)
   154  		B = B[3:]
   155  	}
   156  	return out
   157  }
   158  
   159  // polyByteDecode decodes the 384-byte encoding of a polynomial, checking that
   160  // all the coefficients are properly reduced. This fulfills the "Modulus check"
   161  // step of ML-KEM Encapsulation.
   162  //
   163  // It implements ByteDecode₁₂, according to FIPS 203, Algorithm 6.
   164  func polyByteDecode[T ~[n]fieldElement](b []byte) (T, error) {
   165  	if len(b) != encodingSize12 {
   166  		return T{}, errors.New("mlkem: invalid encoding length")
   167  	}
   168  	var f T
   169  	for i := 0; i < n; i += 2 {
   170  		d := uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16
   171  		const mask12 = 0b1111_1111_1111
   172  		var err error
   173  		if f[i], err = fieldCheckReduced(uint16(d & mask12)); err != nil {
   174  			return T{}, errors.New("mlkem: invalid polynomial encoding")
   175  		}
   176  		if f[i+1], err = fieldCheckReduced(uint16(d >> 12)); err != nil {
   177  			return T{}, errors.New("mlkem: invalid polynomial encoding")
   178  		}
   179  		b = b[3:]
   180  	}
   181  	return f, nil
   182  }
   183  
   184  // sliceForAppend takes a slice and a requested number of bytes. It returns a
   185  // slice with the contents of the given slice followed by that many bytes and a
   186  // second slice that aliases into it and contains only the extra bytes. If the
   187  // original slice has sufficient capacity then no allocation is performed.
   188  func sliceForAppend(in []byte, n int) (head, tail []byte) {
   189  	if total := len(in) + n; cap(in) >= total {
   190  		head = in[:total]
   191  	} else {
   192  		head = make([]byte, total)
   193  		copy(head, in)
   194  	}
   195  	tail = head[len(in):]
   196  	return
   197  }
   198  
   199  // ringCompressAndEncode1 appends a 32-byte encoding of a ring element to s,
   200  // compressing one coefficients per bit.
   201  //
   202  // It implements Compress₁, according to FIPS 203, Definition 4.7,
   203  // followed by ByteEncode₁, according to FIPS 203, Algorithm 5.
   204  func ringCompressAndEncode1(s []byte, f ringElement) []byte {
   205  	s, b := sliceForAppend(s, encodingSize1)
   206  	clear(b)
   207  	for i := range f {
   208  		b[i/8] |= uint8(compress(f[i], 1) << (i % 8))
   209  	}
   210  	return s
   211  }
   212  
   213  // ringDecodeAndDecompress1 decodes a 32-byte slice to a ring element where each
   214  // bit is mapped to 0 or ⌈q/2⌋.
   215  //
   216  // It implements ByteDecode₁, according to FIPS 203, Algorithm 6,
   217  // followed by Decompress₁, according to FIPS 203, Definition 4.8.
   218  func ringDecodeAndDecompress1(b *[encodingSize1]byte) ringElement {
   219  	var f ringElement
   220  	for i := range f {
   221  		b_i := b[i/8] >> (i % 8) & 1
   222  		const halfQ = (q + 1) / 2        // ⌈q/2⌋, rounded up per FIPS 203, Section 2.3
   223  		f[i] = fieldElement(b_i) * halfQ // 0 decompresses to 0, and 1 to ⌈q/2⌋
   224  	}
   225  	return f
   226  }
   227  
   228  // ringCompressAndEncode4 appends a 128-byte encoding of a ring element to s,
   229  // compressing two coefficients per byte.
   230  //
   231  // It implements Compress₄, according to FIPS 203, Definition 4.7,
   232  // followed by ByteEncode₄, according to FIPS 203, Algorithm 5.
   233  func ringCompressAndEncode4(s []byte, f ringElement) []byte {
   234  	s, b := sliceForAppend(s, encodingSize4)
   235  	for i := 0; i < n; i += 2 {
   236  		b[i/2] = uint8(compress(f[i], 4) | compress(f[i+1], 4)<<4)
   237  	}
   238  	return s
   239  }
   240  
   241  // ringDecodeAndDecompress4 decodes a 128-byte encoding of a ring element where
   242  // each four bits are mapped to an equidistant distribution.
   243  //
   244  // It implements ByteDecode₄, according to FIPS 203, Algorithm 6,
   245  // followed by Decompress₄, according to FIPS 203, Definition 4.8.
   246  func ringDecodeAndDecompress4(b *[encodingSize4]byte) ringElement {
   247  	var f ringElement
   248  	for i := 0; i < n; i += 2 {
   249  		f[i] = fieldElement(decompress(uint16(b[i/2]&0b1111), 4))
   250  		f[i+1] = fieldElement(decompress(uint16(b[i/2]>>4), 4))
   251  	}
   252  	return f
   253  }
   254  
   255  // ringCompressAndEncode10 appends a 320-byte encoding of a ring element to s,
   256  // compressing four coefficients per five bytes.
   257  //
   258  // It implements Compress₁₀, according to FIPS 203, Definition 4.7,
   259  // followed by ByteEncode₁₀, according to FIPS 203, Algorithm 5.
   260  func ringCompressAndEncode10(s []byte, f ringElement) []byte {
   261  	s, b := sliceForAppend(s, encodingSize10)
   262  	for i := 0; i < n; i += 4 {
   263  		var x uint64
   264  		x |= uint64(compress(f[i], 10))
   265  		x |= uint64(compress(f[i+1], 10)) << 10
   266  		x |= uint64(compress(f[i+2], 10)) << 20
   267  		x |= uint64(compress(f[i+3], 10)) << 30
   268  		b[0] = uint8(x)
   269  		b[1] = uint8(x >> 8)
   270  		b[2] = uint8(x >> 16)
   271  		b[3] = uint8(x >> 24)
   272  		b[4] = uint8(x >> 32)
   273  		b = b[5:]
   274  	}
   275  	return s
   276  }
   277  
   278  // ringDecodeAndDecompress10 decodes a 320-byte encoding of a ring element where
   279  // each ten bits are mapped to an equidistant distribution.
   280  //
   281  // It implements ByteDecode₁₀, according to FIPS 203, Algorithm 6,
   282  // followed by Decompress₁₀, according to FIPS 203, Definition 4.8.
   283  func ringDecodeAndDecompress10(bb *[encodingSize10]byte) ringElement {
   284  	b := bb[:]
   285  	var f ringElement
   286  	for i := 0; i < n; i += 4 {
   287  		x := uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32
   288  		b = b[5:]
   289  		f[i] = fieldElement(decompress(uint16(x>>0&0b11_1111_1111), 10))
   290  		f[i+1] = fieldElement(decompress(uint16(x>>10&0b11_1111_1111), 10))
   291  		f[i+2] = fieldElement(decompress(uint16(x>>20&0b11_1111_1111), 10))
   292  		f[i+3] = fieldElement(decompress(uint16(x>>30&0b11_1111_1111), 10))
   293  	}
   294  	return f
   295  }
   296  
   297  // ringCompressAndEncode appends an encoding of a ring element to s,
   298  // compressing each coefficient to d bits.
   299  //
   300  // It implements Compress, according to FIPS 203, Definition 4.7,
   301  // followed by ByteEncode, according to FIPS 203, Algorithm 5.
   302  func ringCompressAndEncode(s []byte, f ringElement, d uint8) []byte {
   303  	var b byte
   304  	var bIdx uint8
   305  	for i := 0; i < n; i++ {
   306  		c := compress(f[i], d)
   307  		var cIdx uint8
   308  		for cIdx < d {
   309  			b |= byte(c>>cIdx) << bIdx
   310  			bits := min(8-bIdx, d-cIdx)
   311  			bIdx += bits
   312  			cIdx += bits
   313  			if bIdx == 8 {
   314  				s = append(s, b)
   315  				b = 0
   316  				bIdx = 0
   317  			}
   318  		}
   319  	}
   320  	if bIdx != 0 {
   321  		panic("mlkem: internal error: bitsFilled != 0")
   322  	}
   323  	return s
   324  }
   325  
   326  // ringDecodeAndDecompress decodes an encoding of a ring element where
   327  // each d bits are mapped to an equidistant distribution.
   328  //
   329  // It implements ByteDecode, according to FIPS 203, Algorithm 6,
   330  // followed by Decompress, according to FIPS 203, Definition 4.8.
   331  func ringDecodeAndDecompress(b []byte, d uint8) ringElement {
   332  	var f ringElement
   333  	var bIdx uint8
   334  	for i := 0; i < n; i++ {
   335  		var c uint16
   336  		var cIdx uint8
   337  		for cIdx < d {
   338  			c |= uint16(b[0]>>bIdx) << cIdx
   339  			c &= (1 << d) - 1
   340  			bits := min(8-bIdx, d-cIdx)
   341  			bIdx += bits
   342  			cIdx += bits
   343  			if bIdx == 8 {
   344  				b = b[1:]
   345  				bIdx = 0
   346  			}
   347  		}
   348  		f[i] = fieldElement(decompress(c, d))
   349  	}
   350  	if len(b) != 0 {
   351  		panic("mlkem: internal error: leftover bytes")
   352  	}
   353  	return f
   354  }
   355  
   356  // ringCompressAndEncode5 appends a 160-byte encoding of a ring element to s,
   357  // compressing eight coefficients per five bytes.
   358  //
   359  // It implements Compress₅, according to FIPS 203, Definition 4.7,
   360  // followed by ByteEncode₅, according to FIPS 203, Algorithm 5.
   361  func ringCompressAndEncode5(s []byte, f ringElement) []byte {
   362  	return ringCompressAndEncode(s, f, 5)
   363  }
   364  
   365  // ringDecodeAndDecompress5 decodes a 160-byte encoding of a ring element where
   366  // each five bits are mapped to an equidistant distribution.
   367  //
   368  // It implements ByteDecode₅, according to FIPS 203, Algorithm 6,
   369  // followed by Decompress₅, according to FIPS 203, Definition 4.8.
   370  func ringDecodeAndDecompress5(bb *[encodingSize5]byte) ringElement {
   371  	return ringDecodeAndDecompress(bb[:], 5)
   372  }
   373  
   374  // ringCompressAndEncode11 appends a 352-byte encoding of a ring element to s,
   375  // compressing eight coefficients per eleven bytes.
   376  //
   377  // It implements Compress₁₁, according to FIPS 203, Definition 4.7,
   378  // followed by ByteEncode₁₁, according to FIPS 203, Algorithm 5.
   379  func ringCompressAndEncode11(s []byte, f ringElement) []byte {
   380  	return ringCompressAndEncode(s, f, 11)
   381  }
   382  
   383  // ringDecodeAndDecompress11 decodes a 352-byte encoding of a ring element where
   384  // each eleven bits are mapped to an equidistant distribution.
   385  //
   386  // It implements ByteDecode₁₁, according to FIPS 203, Algorithm 6,
   387  // followed by Decompress₁₁, according to FIPS 203, Definition 4.8.
   388  func ringDecodeAndDecompress11(bb *[encodingSize11]byte) ringElement {
   389  	return ringDecodeAndDecompress(bb[:], 11)
   390  }
   391  
   392  // samplePolyCBD draws a ringElement from the special Dη distribution given a
   393  // stream of random bytes generated by the PRF function, according to FIPS 203,
   394  // Algorithm 8 and Definition 4.3.
   395  func samplePolyCBD(s []byte, b byte) ringElement {
   396  	prf := sha3.NewShake256()
   397  	prf.Write(s)
   398  	prf.Write([]byte{b})
   399  	B := make([]byte, 64*2) // η = 2
   400  	prf.Read(B)
   401  
   402  	// SamplePolyCBD simply draws four (2η) bits for each coefficient, and adds
   403  	// the first two and subtracts the last two.
   404  
   405  	var f ringElement
   406  	for i := 0; i < n; i += 2 {
   407  		b := B[i/2]
   408  		b_7, b_6, b_5, b_4 := b>>7, b>>6&1, b>>5&1, b>>4&1
   409  		b_3, b_2, b_1, b_0 := b>>3&1, b>>2&1, b>>1&1, b&1
   410  		f[i] = fieldSub(fieldElement(b_0+b_1), fieldElement(b_2+b_3))
   411  		f[i+1] = fieldSub(fieldElement(b_4+b_5), fieldElement(b_6+b_7))
   412  	}
   413  	return f
   414  }
   415  
   416  // nttElement is an NTT representation, an element of T_q, represented as an
   417  // array according to FIPS 203, Section 2.4.4.
   418  type nttElement [n]fieldElement
   419  
   420  // gammas are the values ζ^2BitRev7(i)+1 mod q for each index i, according to
   421  // FIPS 203, Appendix A (with negative values reduced to positive).
   422  var gammas = [128]fieldElement{17, 3312, 2761, 568, 583, 2746, 2649, 680, 1637, 1692, 723, 2606, 2288, 1041, 1100, 2229, 1409, 1920, 2662, 667, 3281, 48, 233, 3096, 756, 2573, 2156, 1173, 3015, 314, 3050, 279, 1703, 1626, 1651, 1678, 2789, 540, 1789, 1540, 1847, 1482, 952, 2377, 1461, 1868, 2687, 642, 939, 2390, 2308, 1021, 2437, 892, 2388, 941, 733, 2596, 2337, 992, 268, 3061, 641, 2688, 1584, 1745, 2298, 1031, 2037, 1292, 3220, 109, 375, 2954, 2549, 780, 2090, 1239, 1645, 1684, 1063, 2266, 319, 3010, 2773, 556, 757, 2572, 2099, 1230, 561, 2768, 2466, 863, 2594, 735, 2804, 525, 1092, 2237, 403, 2926, 1026, 2303, 1143, 2186, 2150, 1179, 2775, 554, 886, 2443, 1722, 1607, 1212, 2117, 1874, 1455, 1029, 2300, 2110, 1219, 2935, 394, 885, 2444, 2154, 1175}
   423  
   424  // nttMul multiplies two nttElements.
   425  //
   426  // It implements MultiplyNTTs, according to FIPS 203, Algorithm 11.
   427  func nttMul(f, g nttElement) nttElement {
   428  	var h nttElement
   429  	// We use i += 2 for bounds check elimination. See https://go.dev/issue/66826.
   430  	for i := 0; i < 256; i += 2 {
   431  		a0, a1 := f[i], f[i+1]
   432  		b0, b1 := g[i], g[i+1]
   433  		h[i] = fieldAddMul(a0, b0, fieldMul(a1, b1), gammas[i/2])
   434  		h[i+1] = fieldAddMul(a0, b1, a1, b0)
   435  	}
   436  	return h
   437  }
   438  
   439  // zetas are the values ζ^BitRev7(k) mod q for each index k, according to FIPS
   440  // 203, Appendix A.
   441  var zetas = [128]fieldElement{1, 1729, 2580, 3289, 2642, 630, 1897, 848, 1062, 1919, 193, 797, 2786, 3260, 569, 1746, 296, 2447, 1339, 1476, 3046, 56, 2240, 1333, 1426, 2094, 535, 2882, 2393, 2879, 1974, 821, 289, 331, 3253, 1756, 1197, 2304, 2277, 2055, 650, 1977, 2513, 632, 2865, 33, 1320, 1915, 2319, 1435, 807, 452, 1438, 2868, 1534, 2402, 2647, 2617, 1481, 648, 2474, 3110, 1227, 910, 17, 2761, 583, 2649, 1637, 723, 2288, 1100, 1409, 2662, 3281, 233, 756, 2156, 3015, 3050, 1703, 1651, 2789, 1789, 1847, 952, 1461, 2687, 939, 2308, 2437, 2388, 733, 2337, 268, 641, 1584, 2298, 2037, 3220, 375, 2549, 2090, 1645, 1063, 319, 2773, 757, 2099, 561, 2466, 2594, 2804, 1092, 403, 1026, 1143, 2150, 2775, 886, 1722, 1212, 1874, 1029, 2110, 2935, 885, 2154}
   442  
   443  // ntt maps a ringElement to its nttElement representation.
   444  //
   445  // It implements NTT, according to FIPS 203, Algorithm 9.
   446  func ntt(f ringElement) nttElement {
   447  	k := 1
   448  	for len := 128; len >= 2; len /= 2 {
   449  		for start := 0; start < 256; start += 2 * len {
   450  			zeta := zetas[k]
   451  			k++
   452  			// Bounds check elimination hint.
   453  			f, flen := f[start:start+len], f[start+len:start+len+len]
   454  			for j := 0; j < len; j++ {
   455  				t := fieldMul(zeta, flen[j])
   456  				flen[j] = fieldSub(f[j], t)
   457  				f[j] = fieldAdd(f[j], t)
   458  			}
   459  		}
   460  	}
   461  	return nttElement(f)
   462  }
   463  
   464  // inverseNTT maps a nttElement back to the ringElement it represents.
   465  //
   466  // It implements NTT⁻¹, according to FIPS 203, Algorithm 10.
   467  func inverseNTT(f nttElement) ringElement {
   468  	k := 127
   469  	for len := 2; len <= 128; len *= 2 {
   470  		for start := 0; start < 256; start += 2 * len {
   471  			zeta := zetas[k]
   472  			k--
   473  			// Bounds check elimination hint.
   474  			f, flen := f[start:start+len], f[start+len:start+len+len]
   475  			for j := 0; j < len; j++ {
   476  				t := f[j]
   477  				f[j] = fieldAdd(t, flen[j])
   478  				flen[j] = fieldMulSub(zeta, flen[j], t)
   479  			}
   480  		}
   481  	}
   482  	for i := range f {
   483  		f[i] = fieldMul(f[i], 3303) // 3303 = 128⁻¹ mod q
   484  	}
   485  	return ringElement(f)
   486  }
   487  
   488  // sampleNTT draws a uniformly random nttElement from a stream of uniformly
   489  // random bytes generated by the XOF function, according to FIPS 203,
   490  // Algorithm 7.
   491  func sampleNTT(rho []byte, ii, jj byte) nttElement {
   492  	B := sha3.NewShake128()
   493  	B.Write(rho)
   494  	B.Write([]byte{ii, jj})
   495  
   496  	// SampleNTT essentially draws 12 bits at a time from r, interprets them in
   497  	// little-endian, and rejects values higher than q, until it drew 256
   498  	// values. (The rejection rate is approximately 19%.)
   499  	//
   500  	// To do this from a bytes stream, it draws three bytes at a time, and
   501  	// splits them into two uint16 appropriately masked.
   502  	//
   503  	//               r₀              r₁              r₂
   504  	//       |- - - - - - - -|- - - - - - - -|- - - - - - - -|
   505  	//
   506  	//               Uint16(r₀ || r₁)
   507  	//       |- - - - - - - - - - - - - - - -|
   508  	//       |- - - - - - - - - - - -|
   509  	//                   d₁
   510  	//
   511  	//                                Uint16(r₁ || r₂)
   512  	//                       |- - - - - - - - - - - - - - - -|
   513  	//                               |- - - - - - - - - - - -|
   514  	//                                           d₂
   515  	//
   516  	// Note that in little-endian, the rightmost bits are the most significant
   517  	// bits (dropped with a mask) and the leftmost bits are the least
   518  	// significant bits (dropped with a right shift).
   519  
   520  	var a nttElement
   521  	var j int        // index into a
   522  	var buf [24]byte // buffered reads from B
   523  	off := len(buf)  // index into buf, starts in a "buffer fully consumed" state
   524  	for {
   525  		if off >= len(buf) {
   526  			B.Read(buf[:])
   527  			off = 0
   528  		}
   529  		d1 := byteorder.LEUint16(buf[off:]) & 0b1111_1111_1111
   530  		d2 := byteorder.LEUint16(buf[off+1:]) >> 4
   531  		off += 3
   532  		if d1 < q {
   533  			a[j] = fieldElement(d1)
   534  			j++
   535  		}
   536  		if j >= len(a) {
   537  			break
   538  		}
   539  		if d2 < q {
   540  			a[j] = fieldElement(d2)
   541  			j++
   542  		}
   543  		if j >= len(a) {
   544  			break
   545  		}
   546  	}
   547  	return a
   548  }
   549  

View as plain text