github.com/consensys/gnark-crypto@v0.14.0/field/generator/internal/templates/element/arith.go (about)

     1  package element
     2  
     3  const Arith = `
     4  import (
     5  	"math/bits"
     6  )
     7  
     8  // madd0 hi = a*b + c (discards lo bits)
     9  func madd0(a, b, c uint64) (hi uint64) {
    10  	var carry, lo uint64
    11  	hi, lo = bits.Mul64(a, b)
    12  	_, carry = bits.Add64(lo, c, 0)
    13  	hi, _ = bits.Add64(hi, 0, carry)
    14  	return
    15  }
    16  
    17  // madd1 hi, lo = a*b + c
    18  func madd1(a, b, c uint64) (hi uint64, lo uint64) {
    19  	var carry uint64
    20  	hi, lo = bits.Mul64(a, b)
    21  	lo, carry = bits.Add64(lo, c, 0)
    22  	hi, _ = bits.Add64(hi, 0, carry)
    23  	return
    24  }
    25  
    26  // madd2 hi, lo = a*b + c + d
    27  func madd2(a, b, c, d uint64) (hi uint64, lo uint64) {
    28  	var carry uint64
    29  	hi, lo = bits.Mul64(a, b)
    30  	c, carry = bits.Add64(c, d, 0)
    31  	hi, _ = bits.Add64(hi, 0, carry)
    32  	lo, carry = bits.Add64(lo, c, 0)
    33  	hi, _ = bits.Add64(hi, 0, carry)
    34  	return
    35  }
    36  
    37  
    38  func madd3(a, b, c, d, e uint64) (hi uint64, lo uint64) {
    39  	var carry uint64
    40  	hi, lo = bits.Mul64(a, b)
    41  	c, carry = bits.Add64(c, d, 0)
    42  	hi, _ = bits.Add64(hi, 0, carry)
    43  	lo, carry = bits.Add64(lo, c, 0)
    44  	hi, _ = bits.Add64(hi, e, carry)
    45  	return
    46  }
    47  
    48  {{- if $.UsingP20Inverse}}
    49  	func max(a int, b int) int {
    50  		if a > b {
    51  			return a
    52  		}
    53  		return b
    54  	}
    55  
    56  	func min(a int, b int) int {
    57  		if a < b {
    58  			return a
    59  		}
    60  		return b
    61  	}
    62  
    63  {{- end}}
    64  
    65  `