Source file src/crypto/internal/bigmod/nat_generic.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  //go:build !wasm
     6  
     7  package bigmod
     8  
     9  import "math/bits"
    10  
    11  // addMulVVW multiplies the multi-word value x by the single-word value y,
    12  // adding the result to the multi-word value z and returning the final carry.
    13  // It can be thought of as one row of a pen-and-paper column multiplication.
    14  func addMulVVW(z, x []uint, y uint) (carry uint) {
    15  	_ = x[len(z)-1] // bounds check elimination hint
    16  	for i := range z {
    17  		hi, lo := bits.Mul(x[i], y)
    18  		lo, c := bits.Add(lo, z[i], 0)
    19  		// We use bits.Add with zero to get an add-with-carry instruction that
    20  		// absorbs the carry from the previous bits.Add.
    21  		hi, _ = bits.Add(hi, 0, c)
    22  		lo, c = bits.Add(lo, carry, 0)
    23  		hi, _ = bits.Add(hi, 0, c)
    24  		carry = hi
    25  		z[i] = lo
    26  	}
    27  	return carry
    28  }
    29  

View as plain text