github.com/consensys/gnark-crypto@v0.14.0/internal/generator/iop/template/utils.go.tmpl (about)

     1  import (
     2  	"github.com/consensys/gnark-crypto/ecc/{{ .Name }}/fr"
     3  )
     4  
     5  //----------------------------------------------------
     6  // exp functions until 5
     7  
     8  func exp0(x fr.Element) fr.Element {
     9  	var res fr.Element
    10  	res.SetOne()
    11  	return res
    12  }
    13  
    14  func exp1(x fr.Element) fr.Element {
    15  	return x
    16  }
    17  
    18  func exp2(x fr.Element) fr.Element {
    19  	return *x.Square(&x)
    20  }
    21  
    22  func exp3(x fr.Element) fr.Element {
    23  	var res fr.Element
    24  	res.Square(&x).Mul(&res, &x)
    25  	return res
    26  }
    27  
    28  func exp4(x fr.Element) fr.Element {
    29  	x.Square(&x).Square(&x)
    30  	return x
    31  }
    32  
    33  func exp5(x fr.Element) fr.Element {
    34  	var res fr.Element
    35  	res.Square(&x).Square(&res).Mul(&res, &x)
    36  	return res
    37  }
    38  
    39  // doesn't return any errors, it is a private method, that
    40  // is assumed to be called with correct arguments.
    41  func smallExp(x fr.Element, n int) fr.Element {
    42  	if n == 0 {
    43  		return exp0(x)
    44  	}
    45  	if n == 1 {
    46  		return exp1(x)
    47  	}
    48  	if n == 2 {
    49  		return exp2(x)
    50  	}
    51  	if n == 3 {
    52  		return exp3(x)
    53  	}
    54  	if n == 4 {
    55  		return exp4(x)
    56  	}
    57  	if n == 5 {
    58  		return exp5(x)
    59  	}
    60  	return fr.Element{}
    61  }