github.com/consensys/gnark-crypto@v0.14.0/ecc/secp256k1/fr/element.go (about)

     1  // Copyright 2020 ConsenSys Software Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Code generated by consensys/gnark-crypto DO NOT EDIT
    16  
    17  package fr
    18  
    19  import (
    20  	"crypto/rand"
    21  	"encoding/binary"
    22  	"errors"
    23  	"io"
    24  	"math/big"
    25  	"math/bits"
    26  	"reflect"
    27  	"strconv"
    28  	"strings"
    29  
    30  	"github.com/bits-and-blooms/bitset"
    31  	"github.com/consensys/gnark-crypto/field/hash"
    32  	"github.com/consensys/gnark-crypto/field/pool"
    33  )
    34  
    35  // Element represents a field element stored on 4 words (uint64)
    36  //
    37  // Element are assumed to be in Montgomery form in all methods.
    38  //
    39  // Modulus q =
    40  //
    41  //	q[base10] = 115792089237316195423570985008687907852837564279074904382605163141518161494337
    42  //	q[base16] = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141
    43  //
    44  // # Warning
    45  //
    46  // This code has not been audited and is provided as-is. In particular, there is no security guarantees such as constant time implementation or side-channel attack resistance.
    47  type Element [4]uint64
    48  
    49  const (
    50  	Limbs = 4   // number of 64 bits words needed to represent a Element
    51  	Bits  = 256 // number of bits needed to represent a Element
    52  	Bytes = 32  // number of bytes needed to represent a Element
    53  )
    54  
    55  // Field modulus q
    56  const (
    57  	q0 uint64 = 13822214165235122497
    58  	q1 uint64 = 13451932020343611451
    59  	q2 uint64 = 18446744073709551614
    60  	q3 uint64 = 18446744073709551615
    61  )
    62  
    63  var qElement = Element{
    64  	q0,
    65  	q1,
    66  	q2,
    67  	q3,
    68  }
    69  
    70  var _modulus big.Int // q stored as big.Int
    71  
    72  // Modulus returns q as a big.Int
    73  //
    74  //	q[base10] = 115792089237316195423570985008687907852837564279074904382605163141518161494337
    75  //	q[base16] = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141
    76  func Modulus() *big.Int {
    77  	return new(big.Int).Set(&_modulus)
    78  }
    79  
    80  // q + r'.r = 1, i.e., qInvNeg = - q⁻¹ mod r
    81  // used for Montgomery reduction
    82  const qInvNeg uint64 = 5408259542528602431
    83  
    84  func init() {
    85  	_modulus.SetString("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141", 16)
    86  }
    87  
    88  // NewElement returns a new Element from a uint64 value
    89  //
    90  // it is equivalent to
    91  //
    92  //	var v Element
    93  //	v.SetUint64(...)
    94  func NewElement(v uint64) Element {
    95  	z := Element{v}
    96  	z.Mul(&z, &rSquare)
    97  	return z
    98  }
    99  
   100  // SetUint64 sets z to v and returns z
   101  func (z *Element) SetUint64(v uint64) *Element {
   102  	//  sets z LSB to v (non-Montgomery form) and convert z to Montgomery form
   103  	*z = Element{v}
   104  	return z.Mul(z, &rSquare) // z.toMont()
   105  }
   106  
   107  // SetInt64 sets z to v and returns z
   108  func (z *Element) SetInt64(v int64) *Element {
   109  
   110  	// absolute value of v
   111  	m := v >> 63
   112  	z.SetUint64(uint64((v ^ m) - m))
   113  
   114  	if m != 0 {
   115  		// v is negative
   116  		z.Neg(z)
   117  	}
   118  
   119  	return z
   120  }
   121  
   122  // Set z = x and returns z
   123  func (z *Element) Set(x *Element) *Element {
   124  	z[0] = x[0]
   125  	z[1] = x[1]
   126  	z[2] = x[2]
   127  	z[3] = x[3]
   128  	return z
   129  }
   130  
   131  // SetInterface converts provided interface into Element
   132  // returns an error if provided type is not supported
   133  // supported types:
   134  //
   135  //	Element
   136  //	*Element
   137  //	uint64
   138  //	int
   139  //	string (see SetString for valid formats)
   140  //	*big.Int
   141  //	big.Int
   142  //	[]byte
   143  func (z *Element) SetInterface(i1 interface{}) (*Element, error) {
   144  	if i1 == nil {
   145  		return nil, errors.New("can't set fr.Element with <nil>")
   146  	}
   147  
   148  	switch c1 := i1.(type) {
   149  	case Element:
   150  		return z.Set(&c1), nil
   151  	case *Element:
   152  		if c1 == nil {
   153  			return nil, errors.New("can't set fr.Element with <nil>")
   154  		}
   155  		return z.Set(c1), nil
   156  	case uint8:
   157  		return z.SetUint64(uint64(c1)), nil
   158  	case uint16:
   159  		return z.SetUint64(uint64(c1)), nil
   160  	case uint32:
   161  		return z.SetUint64(uint64(c1)), nil
   162  	case uint:
   163  		return z.SetUint64(uint64(c1)), nil
   164  	case uint64:
   165  		return z.SetUint64(c1), nil
   166  	case int8:
   167  		return z.SetInt64(int64(c1)), nil
   168  	case int16:
   169  		return z.SetInt64(int64(c1)), nil
   170  	case int32:
   171  		return z.SetInt64(int64(c1)), nil
   172  	case int64:
   173  		return z.SetInt64(c1), nil
   174  	case int:
   175  		return z.SetInt64(int64(c1)), nil
   176  	case string:
   177  		return z.SetString(c1)
   178  	case *big.Int:
   179  		if c1 == nil {
   180  			return nil, errors.New("can't set fr.Element with <nil>")
   181  		}
   182  		return z.SetBigInt(c1), nil
   183  	case big.Int:
   184  		return z.SetBigInt(&c1), nil
   185  	case []byte:
   186  		return z.SetBytes(c1), nil
   187  	default:
   188  		return nil, errors.New("can't set fr.Element from type " + reflect.TypeOf(i1).String())
   189  	}
   190  }
   191  
   192  // SetZero z = 0
   193  func (z *Element) SetZero() *Element {
   194  	z[0] = 0
   195  	z[1] = 0
   196  	z[2] = 0
   197  	z[3] = 0
   198  	return z
   199  }
   200  
   201  // SetOne z = 1 (in Montgomery form)
   202  func (z *Element) SetOne() *Element {
   203  	z[0] = 4624529908474429119
   204  	z[1] = 4994812053365940164
   205  	z[2] = 1
   206  	z[3] = 0
   207  	return z
   208  }
   209  
   210  // Div z = x*y⁻¹ (mod q)
   211  func (z *Element) Div(x, y *Element) *Element {
   212  	var yInv Element
   213  	yInv.Inverse(y)
   214  	z.Mul(x, &yInv)
   215  	return z
   216  }
   217  
   218  // Equal returns z == x; constant-time
   219  func (z *Element) Equal(x *Element) bool {
   220  	return z.NotEqual(x) == 0
   221  }
   222  
   223  // NotEqual returns 0 if and only if z == x; constant-time
   224  func (z *Element) NotEqual(x *Element) uint64 {
   225  	return (z[3] ^ x[3]) | (z[2] ^ x[2]) | (z[1] ^ x[1]) | (z[0] ^ x[0])
   226  }
   227  
   228  // IsZero returns z == 0
   229  func (z *Element) IsZero() bool {
   230  	return (z[3] | z[2] | z[1] | z[0]) == 0
   231  }
   232  
   233  // IsOne returns z == 1
   234  func (z *Element) IsOne() bool {
   235  	return (z[3] | (z[2] ^ 1) | (z[1] ^ 4994812053365940164) | (z[0] ^ 4624529908474429119)) == 0
   236  }
   237  
   238  // IsUint64 reports whether z can be represented as an uint64.
   239  func (z *Element) IsUint64() bool {
   240  	zz := *z
   241  	zz.fromMont()
   242  	return zz.FitsOnOneWord()
   243  }
   244  
   245  // Uint64 returns the uint64 representation of x. If x cannot be represented in a uint64, the result is undefined.
   246  func (z *Element) Uint64() uint64 {
   247  	return z.Bits()[0]
   248  }
   249  
   250  // FitsOnOneWord reports whether z words (except the least significant word) are 0
   251  //
   252  // It is the responsibility of the caller to convert from Montgomery to Regular form if needed.
   253  func (z *Element) FitsOnOneWord() bool {
   254  	return (z[3] | z[2] | z[1]) == 0
   255  }
   256  
   257  // Cmp compares (lexicographic order) z and x and returns:
   258  //
   259  //	-1 if z <  x
   260  //	 0 if z == x
   261  //	+1 if z >  x
   262  func (z *Element) Cmp(x *Element) int {
   263  	_z := z.Bits()
   264  	_x := x.Bits()
   265  	if _z[3] > _x[3] {
   266  		return 1
   267  	} else if _z[3] < _x[3] {
   268  		return -1
   269  	}
   270  	if _z[2] > _x[2] {
   271  		return 1
   272  	} else if _z[2] < _x[2] {
   273  		return -1
   274  	}
   275  	if _z[1] > _x[1] {
   276  		return 1
   277  	} else if _z[1] < _x[1] {
   278  		return -1
   279  	}
   280  	if _z[0] > _x[0] {
   281  		return 1
   282  	} else if _z[0] < _x[0] {
   283  		return -1
   284  	}
   285  	return 0
   286  }
   287  
   288  // LexicographicallyLargest returns true if this element is strictly lexicographically
   289  // larger than its negation, false otherwise
   290  func (z *Element) LexicographicallyLargest() bool {
   291  	// adapted from github.com/zkcrypto/bls12_381
   292  	// we check if the element is larger than (q-1) / 2
   293  	// if z - (((q -1) / 2) + 1) have no underflow, then z > (q-1) / 2
   294  
   295  	_z := z.Bits()
   296  
   297  	var b uint64
   298  	_, b = bits.Sub64(_z[0], 16134479119472337057, 0)
   299  	_, b = bits.Sub64(_z[1], 6725966010171805725, b)
   300  	_, b = bits.Sub64(_z[2], 18446744073709551615, b)
   301  	_, b = bits.Sub64(_z[3], 9223372036854775807, b)
   302  
   303  	return b == 0
   304  }
   305  
   306  // SetRandom sets z to a uniform random value in [0, q).
   307  //
   308  // This might error only if reading from crypto/rand.Reader errors,
   309  // in which case, value of z is undefined.
   310  func (z *Element) SetRandom() (*Element, error) {
   311  	// this code is generated for all modulus
   312  	// and derived from go/src/crypto/rand/util.go
   313  
   314  	// l is number of limbs * 8; the number of bytes needed to reconstruct 4 uint64
   315  	const l = 32
   316  
   317  	// bitLen is the maximum bit length needed to encode a value < q.
   318  	const bitLen = 256
   319  
   320  	// k is the maximum byte length needed to encode a value < q.
   321  	const k = (bitLen + 7) / 8
   322  
   323  	// b is the number of bits in the most significant byte of q-1.
   324  	b := uint(bitLen % 8)
   325  	if b == 0 {
   326  		b = 8
   327  	}
   328  
   329  	var bytes [l]byte
   330  
   331  	for {
   332  		// note that bytes[k:l] is always 0
   333  		if _, err := io.ReadFull(rand.Reader, bytes[:k]); err != nil {
   334  			return nil, err
   335  		}
   336  
   337  		// Clear unused bits in in the most significant byte to increase probability
   338  		// that the candidate is < q.
   339  		bytes[k-1] &= uint8(int(1<<b) - 1)
   340  		z[0] = binary.LittleEndian.Uint64(bytes[0:8])
   341  		z[1] = binary.LittleEndian.Uint64(bytes[8:16])
   342  		z[2] = binary.LittleEndian.Uint64(bytes[16:24])
   343  		z[3] = binary.LittleEndian.Uint64(bytes[24:32])
   344  
   345  		if !z.smallerThanModulus() {
   346  			continue // ignore the candidate and re-sample
   347  		}
   348  
   349  		return z, nil
   350  	}
   351  }
   352  
   353  // smallerThanModulus returns true if z < q
   354  // This is not constant time
   355  func (z *Element) smallerThanModulus() bool {
   356  	return (z[3] < q3 || (z[3] == q3 && (z[2] < q2 || (z[2] == q2 && (z[1] < q1 || (z[1] == q1 && (z[0] < q0)))))))
   357  }
   358  
   359  // One returns 1
   360  func One() Element {
   361  	var one Element
   362  	one.SetOne()
   363  	return one
   364  }
   365  
   366  // Halve sets z to z / 2 (mod q)
   367  func (z *Element) Halve() {
   368  	var carry uint64
   369  
   370  	if z[0]&1 == 1 {
   371  		// z = z + q
   372  		z[0], carry = bits.Add64(z[0], q0, 0)
   373  		z[1], carry = bits.Add64(z[1], q1, carry)
   374  		z[2], carry = bits.Add64(z[2], q2, carry)
   375  		z[3], carry = bits.Add64(z[3], q3, carry)
   376  
   377  	}
   378  	// z = z >> 1
   379  	z[0] = z[0]>>1 | z[1]<<63
   380  	z[1] = z[1]>>1 | z[2]<<63
   381  	z[2] = z[2]>>1 | z[3]<<63
   382  	z[3] >>= 1
   383  
   384  	if carry != 0 {
   385  		// when we added q, the result was larger than our available limbs
   386  		// when we shift right, we need to set the highest bit
   387  		z[3] |= (1 << 63)
   388  	}
   389  
   390  }
   391  
   392  // fromMont converts z in place (i.e. mutates) from Montgomery to regular representation
   393  // sets and returns z = z * 1
   394  func (z *Element) fromMont() *Element {
   395  	fromMont(z)
   396  	return z
   397  }
   398  
   399  // Add z = x + y (mod q)
   400  func (z *Element) Add(x, y *Element) *Element {
   401  
   402  	var carry uint64
   403  	z[0], carry = bits.Add64(x[0], y[0], 0)
   404  	z[1], carry = bits.Add64(x[1], y[1], carry)
   405  	z[2], carry = bits.Add64(x[2], y[2], carry)
   406  	z[3], carry = bits.Add64(x[3], y[3], carry)
   407  	// if we overflowed the last addition, z >= q
   408  	// if z >= q, z = z - q
   409  	if carry != 0 {
   410  		var b uint64
   411  		// we overflowed, so z >= q
   412  		z[0], b = bits.Sub64(z[0], q0, 0)
   413  		z[1], b = bits.Sub64(z[1], q1, b)
   414  		z[2], b = bits.Sub64(z[2], q2, b)
   415  		z[3], _ = bits.Sub64(z[3], q3, b)
   416  		return z
   417  	}
   418  
   419  	// if z ⩾ q → z -= q
   420  	if !z.smallerThanModulus() {
   421  		var b uint64
   422  		z[0], b = bits.Sub64(z[0], q0, 0)
   423  		z[1], b = bits.Sub64(z[1], q1, b)
   424  		z[2], b = bits.Sub64(z[2], q2, b)
   425  		z[3], _ = bits.Sub64(z[3], q3, b)
   426  	}
   427  	return z
   428  }
   429  
   430  // Double z = x + x (mod q), aka Lsh 1
   431  func (z *Element) Double(x *Element) *Element {
   432  
   433  	var carry uint64
   434  	z[0], carry = bits.Add64(x[0], x[0], 0)
   435  	z[1], carry = bits.Add64(x[1], x[1], carry)
   436  	z[2], carry = bits.Add64(x[2], x[2], carry)
   437  	z[3], carry = bits.Add64(x[3], x[3], carry)
   438  	// if we overflowed the last addition, z >= q
   439  	// if z >= q, z = z - q
   440  	if carry != 0 {
   441  		var b uint64
   442  		// we overflowed, so z >= q
   443  		z[0], b = bits.Sub64(z[0], q0, 0)
   444  		z[1], b = bits.Sub64(z[1], q1, b)
   445  		z[2], b = bits.Sub64(z[2], q2, b)
   446  		z[3], _ = bits.Sub64(z[3], q3, b)
   447  		return z
   448  	}
   449  
   450  	// if z ⩾ q → z -= q
   451  	if !z.smallerThanModulus() {
   452  		var b uint64
   453  		z[0], b = bits.Sub64(z[0], q0, 0)
   454  		z[1], b = bits.Sub64(z[1], q1, b)
   455  		z[2], b = bits.Sub64(z[2], q2, b)
   456  		z[3], _ = bits.Sub64(z[3], q3, b)
   457  	}
   458  	return z
   459  }
   460  
   461  // Sub z = x - y (mod q)
   462  func (z *Element) Sub(x, y *Element) *Element {
   463  	var b uint64
   464  	z[0], b = bits.Sub64(x[0], y[0], 0)
   465  	z[1], b = bits.Sub64(x[1], y[1], b)
   466  	z[2], b = bits.Sub64(x[2], y[2], b)
   467  	z[3], b = bits.Sub64(x[3], y[3], b)
   468  	if b != 0 {
   469  		var c uint64
   470  		z[0], c = bits.Add64(z[0], q0, 0)
   471  		z[1], c = bits.Add64(z[1], q1, c)
   472  		z[2], c = bits.Add64(z[2], q2, c)
   473  		z[3], _ = bits.Add64(z[3], q3, c)
   474  	}
   475  	return z
   476  }
   477  
   478  // Neg z = q - x
   479  func (z *Element) Neg(x *Element) *Element {
   480  	if x.IsZero() {
   481  		z.SetZero()
   482  		return z
   483  	}
   484  	var borrow uint64
   485  	z[0], borrow = bits.Sub64(q0, x[0], 0)
   486  	z[1], borrow = bits.Sub64(q1, x[1], borrow)
   487  	z[2], borrow = bits.Sub64(q2, x[2], borrow)
   488  	z[3], _ = bits.Sub64(q3, x[3], borrow)
   489  	return z
   490  }
   491  
   492  // Select is a constant-time conditional move.
   493  // If c=0, z = x0. Else z = x1
   494  func (z *Element) Select(c int, x0 *Element, x1 *Element) *Element {
   495  	cC := uint64((int64(c) | -int64(c)) >> 63) // "canonicized" into: 0 if c=0, -1 otherwise
   496  	z[0] = x0[0] ^ cC&(x0[0]^x1[0])
   497  	z[1] = x0[1] ^ cC&(x0[1]^x1[1])
   498  	z[2] = x0[2] ^ cC&(x0[2]^x1[2])
   499  	z[3] = x0[3] ^ cC&(x0[3]^x1[3])
   500  	return z
   501  }
   502  
   503  // _mulGeneric is unoptimized textbook CIOS
   504  // it is a fallback solution on x86 when ADX instruction set is not available
   505  // and is used for testing purposes.
   506  func _mulGeneric(z, x, y *Element) {
   507  
   508  	// Implements CIOS multiplication -- section 2.3.2 of Tolga Acar's thesis
   509  	// https://www.microsoft.com/en-us/research/wp-content/uploads/1998/06/97Acar.pdf
   510  	//
   511  	// The algorithm:
   512  	//
   513  	// for i=0 to N-1
   514  	// 		C := 0
   515  	// 		for j=0 to N-1
   516  	// 			(C,t[j]) := t[j] + x[j]*y[i] + C
   517  	// 		(t[N+1],t[N]) := t[N] + C
   518  	//
   519  	// 		C := 0
   520  	// 		m := t[0]*q'[0] mod D
   521  	// 		(C,_) := t[0] + m*q[0]
   522  	// 		for j=1 to N-1
   523  	// 			(C,t[j-1]) := t[j] + m*q[j] + C
   524  	//
   525  	// 		(C,t[N-1]) := t[N] + C
   526  	// 		t[N] := t[N+1] + C
   527  	//
   528  	// → N is the number of machine words needed to store the modulus q
   529  	// → D is the word size. For example, on a 64-bit architecture D is 2	64
   530  	// → x[i], y[i], q[i] is the ith word of the numbers x,y,q
   531  	// → q'[0] is the lowest word of the number -q⁻¹ mod r. This quantity is pre-computed, as it does not depend on the inputs.
   532  	// → t is a temporary array of size N+2
   533  	// → C, S are machine words. A pair (C,S) refers to (hi-bits, lo-bits) of a two-word number
   534  
   535  	var t [5]uint64
   536  	var D uint64
   537  	var m, C uint64
   538  	// -----------------------------------
   539  	// First loop
   540  
   541  	C, t[0] = bits.Mul64(y[0], x[0])
   542  	C, t[1] = madd1(y[0], x[1], C)
   543  	C, t[2] = madd1(y[0], x[2], C)
   544  	C, t[3] = madd1(y[0], x[3], C)
   545  
   546  	t[4], D = bits.Add64(t[4], C, 0)
   547  
   548  	// m = t[0]n'[0] mod W
   549  	m = t[0] * qInvNeg
   550  
   551  	// -----------------------------------
   552  	// Second loop
   553  	C = madd0(m, q0, t[0])
   554  	C, t[0] = madd2(m, q1, t[1], C)
   555  	C, t[1] = madd2(m, q2, t[2], C)
   556  	C, t[2] = madd2(m, q3, t[3], C)
   557  
   558  	t[3], C = bits.Add64(t[4], C, 0)
   559  	t[4], _ = bits.Add64(0, D, C)
   560  	// -----------------------------------
   561  	// First loop
   562  
   563  	C, t[0] = madd1(y[1], x[0], t[0])
   564  	C, t[1] = madd2(y[1], x[1], t[1], C)
   565  	C, t[2] = madd2(y[1], x[2], t[2], C)
   566  	C, t[3] = madd2(y[1], x[3], t[3], C)
   567  
   568  	t[4], D = bits.Add64(t[4], C, 0)
   569  
   570  	// m = t[0]n'[0] mod W
   571  	m = t[0] * qInvNeg
   572  
   573  	// -----------------------------------
   574  	// Second loop
   575  	C = madd0(m, q0, t[0])
   576  	C, t[0] = madd2(m, q1, t[1], C)
   577  	C, t[1] = madd2(m, q2, t[2], C)
   578  	C, t[2] = madd2(m, q3, t[3], C)
   579  
   580  	t[3], C = bits.Add64(t[4], C, 0)
   581  	t[4], _ = bits.Add64(0, D, C)
   582  	// -----------------------------------
   583  	// First loop
   584  
   585  	C, t[0] = madd1(y[2], x[0], t[0])
   586  	C, t[1] = madd2(y[2], x[1], t[1], C)
   587  	C, t[2] = madd2(y[2], x[2], t[2], C)
   588  	C, t[3] = madd2(y[2], x[3], t[3], C)
   589  
   590  	t[4], D = bits.Add64(t[4], C, 0)
   591  
   592  	// m = t[0]n'[0] mod W
   593  	m = t[0] * qInvNeg
   594  
   595  	// -----------------------------------
   596  	// Second loop
   597  	C = madd0(m, q0, t[0])
   598  	C, t[0] = madd2(m, q1, t[1], C)
   599  	C, t[1] = madd2(m, q2, t[2], C)
   600  	C, t[2] = madd2(m, q3, t[3], C)
   601  
   602  	t[3], C = bits.Add64(t[4], C, 0)
   603  	t[4], _ = bits.Add64(0, D, C)
   604  	// -----------------------------------
   605  	// First loop
   606  
   607  	C, t[0] = madd1(y[3], x[0], t[0])
   608  	C, t[1] = madd2(y[3], x[1], t[1], C)
   609  	C, t[2] = madd2(y[3], x[2], t[2], C)
   610  	C, t[3] = madd2(y[3], x[3], t[3], C)
   611  
   612  	t[4], D = bits.Add64(t[4], C, 0)
   613  
   614  	// m = t[0]n'[0] mod W
   615  	m = t[0] * qInvNeg
   616  
   617  	// -----------------------------------
   618  	// Second loop
   619  	C = madd0(m, q0, t[0])
   620  	C, t[0] = madd2(m, q1, t[1], C)
   621  	C, t[1] = madd2(m, q2, t[2], C)
   622  	C, t[2] = madd2(m, q3, t[3], C)
   623  
   624  	t[3], C = bits.Add64(t[4], C, 0)
   625  	t[4], _ = bits.Add64(0, D, C)
   626  
   627  	if t[4] != 0 {
   628  		// we need to reduce, we have a result on 5 words
   629  		var b uint64
   630  		z[0], b = bits.Sub64(t[0], q0, 0)
   631  		z[1], b = bits.Sub64(t[1], q1, b)
   632  		z[2], b = bits.Sub64(t[2], q2, b)
   633  		z[3], _ = bits.Sub64(t[3], q3, b)
   634  		return
   635  	}
   636  
   637  	// copy t into z
   638  	z[0] = t[0]
   639  	z[1] = t[1]
   640  	z[2] = t[2]
   641  	z[3] = t[3]
   642  
   643  	// if z ⩾ q → z -= q
   644  	if !z.smallerThanModulus() {
   645  		var b uint64
   646  		z[0], b = bits.Sub64(z[0], q0, 0)
   647  		z[1], b = bits.Sub64(z[1], q1, b)
   648  		z[2], b = bits.Sub64(z[2], q2, b)
   649  		z[3], _ = bits.Sub64(z[3], q3, b)
   650  	}
   651  }
   652  
   653  func _fromMontGeneric(z *Element) {
   654  	// the following lines implement z = z * 1
   655  	// with a modified CIOS montgomery multiplication
   656  	// see Mul for algorithm documentation
   657  	{
   658  		// m = z[0]n'[0] mod W
   659  		m := z[0] * qInvNeg
   660  		C := madd0(m, q0, z[0])
   661  		C, z[0] = madd2(m, q1, z[1], C)
   662  		C, z[1] = madd2(m, q2, z[2], C)
   663  		C, z[2] = madd2(m, q3, z[3], C)
   664  		z[3] = C
   665  	}
   666  	{
   667  		// m = z[0]n'[0] mod W
   668  		m := z[0] * qInvNeg
   669  		C := madd0(m, q0, z[0])
   670  		C, z[0] = madd2(m, q1, z[1], C)
   671  		C, z[1] = madd2(m, q2, z[2], C)
   672  		C, z[2] = madd2(m, q3, z[3], C)
   673  		z[3] = C
   674  	}
   675  	{
   676  		// m = z[0]n'[0] mod W
   677  		m := z[0] * qInvNeg
   678  		C := madd0(m, q0, z[0])
   679  		C, z[0] = madd2(m, q1, z[1], C)
   680  		C, z[1] = madd2(m, q2, z[2], C)
   681  		C, z[2] = madd2(m, q3, z[3], C)
   682  		z[3] = C
   683  	}
   684  	{
   685  		// m = z[0]n'[0] mod W
   686  		m := z[0] * qInvNeg
   687  		C := madd0(m, q0, z[0])
   688  		C, z[0] = madd2(m, q1, z[1], C)
   689  		C, z[1] = madd2(m, q2, z[2], C)
   690  		C, z[2] = madd2(m, q3, z[3], C)
   691  		z[3] = C
   692  	}
   693  
   694  	// if z ⩾ q → z -= q
   695  	if !z.smallerThanModulus() {
   696  		var b uint64
   697  		z[0], b = bits.Sub64(z[0], q0, 0)
   698  		z[1], b = bits.Sub64(z[1], q1, b)
   699  		z[2], b = bits.Sub64(z[2], q2, b)
   700  		z[3], _ = bits.Sub64(z[3], q3, b)
   701  	}
   702  }
   703  
   704  func _reduceGeneric(z *Element) {
   705  
   706  	// if z ⩾ q → z -= q
   707  	if !z.smallerThanModulus() {
   708  		var b uint64
   709  		z[0], b = bits.Sub64(z[0], q0, 0)
   710  		z[1], b = bits.Sub64(z[1], q1, b)
   711  		z[2], b = bits.Sub64(z[2], q2, b)
   712  		z[3], _ = bits.Sub64(z[3], q3, b)
   713  	}
   714  }
   715  
   716  // BatchInvert returns a new slice with every element inverted.
   717  // Uses Montgomery batch inversion trick
   718  func BatchInvert(a []Element) []Element {
   719  	res := make([]Element, len(a))
   720  	if len(a) == 0 {
   721  		return res
   722  	}
   723  
   724  	zeroes := bitset.New(uint(len(a)))
   725  	accumulator := One()
   726  
   727  	for i := 0; i < len(a); i++ {
   728  		if a[i].IsZero() {
   729  			zeroes.Set(uint(i))
   730  			continue
   731  		}
   732  		res[i] = accumulator
   733  		accumulator.Mul(&accumulator, &a[i])
   734  	}
   735  
   736  	accumulator.Inverse(&accumulator)
   737  
   738  	for i := len(a) - 1; i >= 0; i-- {
   739  		if zeroes.Test(uint(i)) {
   740  			continue
   741  		}
   742  		res[i].Mul(&res[i], &accumulator)
   743  		accumulator.Mul(&accumulator, &a[i])
   744  	}
   745  
   746  	return res
   747  }
   748  
   749  func _butterflyGeneric(a, b *Element) {
   750  	t := *a
   751  	a.Add(a, b)
   752  	b.Sub(&t, b)
   753  }
   754  
   755  // BitLen returns the minimum number of bits needed to represent z
   756  // returns 0 if z == 0
   757  func (z *Element) BitLen() int {
   758  	if z[3] != 0 {
   759  		return 192 + bits.Len64(z[3])
   760  	}
   761  	if z[2] != 0 {
   762  		return 128 + bits.Len64(z[2])
   763  	}
   764  	if z[1] != 0 {
   765  		return 64 + bits.Len64(z[1])
   766  	}
   767  	return bits.Len64(z[0])
   768  }
   769  
   770  // Hash msg to count prime field elements.
   771  // https://tools.ietf.org/html/draft-irtf-cfrg-hash-to-curve-06#section-5.2
   772  func Hash(msg, dst []byte, count int) ([]Element, error) {
   773  	// 128 bits of security
   774  	// L = ceil((ceil(log2(p)) + k) / 8), where k is the security parameter = 128
   775  	const Bytes = 1 + (Bits-1)/8
   776  	const L = 16 + Bytes
   777  
   778  	lenInBytes := count * L
   779  	pseudoRandomBytes, err := hash.ExpandMsgXmd(msg, dst, lenInBytes)
   780  	if err != nil {
   781  		return nil, err
   782  	}
   783  
   784  	// get temporary big int from the pool
   785  	vv := pool.BigInt.Get()
   786  
   787  	res := make([]Element, count)
   788  	for i := 0; i < count; i++ {
   789  		vv.SetBytes(pseudoRandomBytes[i*L : (i+1)*L])
   790  		res[i].SetBigInt(vv)
   791  	}
   792  
   793  	// release object into pool
   794  	pool.BigInt.Put(vv)
   795  
   796  	return res, nil
   797  }
   798  
   799  // Exp z = xᵏ (mod q)
   800  func (z *Element) Exp(x Element, k *big.Int) *Element {
   801  	if k.IsUint64() && k.Uint64() == 0 {
   802  		return z.SetOne()
   803  	}
   804  
   805  	e := k
   806  	if k.Sign() == -1 {
   807  		// negative k, we invert
   808  		// if k < 0: xᵏ (mod q) == (x⁻¹)ᵏ (mod q)
   809  		x.Inverse(&x)
   810  
   811  		// we negate k in a temp big.Int since
   812  		// Int.Bit(_) of k and -k is different
   813  		e = pool.BigInt.Get()
   814  		defer pool.BigInt.Put(e)
   815  		e.Neg(k)
   816  	}
   817  
   818  	z.Set(&x)
   819  
   820  	for i := e.BitLen() - 2; i >= 0; i-- {
   821  		z.Square(z)
   822  		if e.Bit(i) == 1 {
   823  			z.Mul(z, &x)
   824  		}
   825  	}
   826  
   827  	return z
   828  }
   829  
   830  // rSquare where r is the Montgommery constant
   831  // see section 2.3.2 of Tolga Acar's thesis
   832  // https://www.microsoft.com/en-us/research/wp-content/uploads/1998/06/97Acar.pdf
   833  var rSquare = Element{
   834  	9902555850136342848,
   835  	8364476168144746616,
   836  	16616019711348246470,
   837  	11342065889886772165,
   838  }
   839  
   840  // toMont converts z to Montgomery form
   841  // sets and returns z = z * r²
   842  func (z *Element) toMont() *Element {
   843  	return z.Mul(z, &rSquare)
   844  }
   845  
   846  // String returns the decimal representation of z as generated by
   847  // z.Text(10).
   848  func (z *Element) String() string {
   849  	return z.Text(10)
   850  }
   851  
   852  // toBigInt returns z as a big.Int in Montgomery form
   853  func (z *Element) toBigInt(res *big.Int) *big.Int {
   854  	var b [Bytes]byte
   855  	binary.BigEndian.PutUint64(b[24:32], z[0])
   856  	binary.BigEndian.PutUint64(b[16:24], z[1])
   857  	binary.BigEndian.PutUint64(b[8:16], z[2])
   858  	binary.BigEndian.PutUint64(b[0:8], z[3])
   859  
   860  	return res.SetBytes(b[:])
   861  }
   862  
   863  // Text returns the string representation of z in the given base.
   864  // Base must be between 2 and 36, inclusive. The result uses the
   865  // lower-case letters 'a' to 'z' for digit values 10 to 35.
   866  // No prefix (such as "0x") is added to the string. If z is a nil
   867  // pointer it returns "<nil>".
   868  // If base == 10 and -z fits in a uint16 prefix "-" is added to the string.
   869  func (z *Element) Text(base int) string {
   870  	if base < 2 || base > 36 {
   871  		panic("invalid base")
   872  	}
   873  	if z == nil {
   874  		return "<nil>"
   875  	}
   876  
   877  	const maxUint16 = 65535
   878  	if base == 10 {
   879  		var zzNeg Element
   880  		zzNeg.Neg(z)
   881  		zzNeg.fromMont()
   882  		if zzNeg.FitsOnOneWord() && zzNeg[0] <= maxUint16 && zzNeg[0] != 0 {
   883  			return "-" + strconv.FormatUint(zzNeg[0], base)
   884  		}
   885  	}
   886  	zz := *z
   887  	zz.fromMont()
   888  	if zz.FitsOnOneWord() {
   889  		return strconv.FormatUint(zz[0], base)
   890  	}
   891  	vv := pool.BigInt.Get()
   892  	r := zz.toBigInt(vv).Text(base)
   893  	pool.BigInt.Put(vv)
   894  	return r
   895  }
   896  
   897  // BigInt sets and return z as a *big.Int
   898  func (z *Element) BigInt(res *big.Int) *big.Int {
   899  	_z := *z
   900  	_z.fromMont()
   901  	return _z.toBigInt(res)
   902  }
   903  
   904  // ToBigIntRegular returns z as a big.Int in regular form
   905  //
   906  // Deprecated: use BigInt(*big.Int) instead
   907  func (z Element) ToBigIntRegular(res *big.Int) *big.Int {
   908  	z.fromMont()
   909  	return z.toBigInt(res)
   910  }
   911  
   912  // Bits provides access to z by returning its value as a little-endian [4]uint64 array.
   913  // Bits is intended to support implementation of missing low-level Element
   914  // functionality outside this package; it should be avoided otherwise.
   915  func (z *Element) Bits() [4]uint64 {
   916  	_z := *z
   917  	fromMont(&_z)
   918  	return _z
   919  }
   920  
   921  // Bytes returns the value of z as a big-endian byte array
   922  func (z *Element) Bytes() (res [Bytes]byte) {
   923  	BigEndian.PutElement(&res, *z)
   924  	return
   925  }
   926  
   927  // Marshal returns the value of z as a big-endian byte slice
   928  func (z *Element) Marshal() []byte {
   929  	b := z.Bytes()
   930  	return b[:]
   931  }
   932  
   933  // Unmarshal is an alias for SetBytes, it sets z to the value of e.
   934  func (z *Element) Unmarshal(e []byte) {
   935  	z.SetBytes(e)
   936  }
   937  
   938  // SetBytes interprets e as the bytes of a big-endian unsigned integer,
   939  // sets z to that value, and returns z.
   940  func (z *Element) SetBytes(e []byte) *Element {
   941  	if len(e) == Bytes {
   942  		// fast path
   943  		v, err := BigEndian.Element((*[Bytes]byte)(e))
   944  		if err == nil {
   945  			*z = v
   946  			return z
   947  		}
   948  	}
   949  
   950  	// slow path.
   951  	// get a big int from our pool
   952  	vv := pool.BigInt.Get()
   953  	vv.SetBytes(e)
   954  
   955  	// set big int
   956  	z.SetBigInt(vv)
   957  
   958  	// put temporary object back in pool
   959  	pool.BigInt.Put(vv)
   960  
   961  	return z
   962  }
   963  
   964  // SetBytesCanonical interprets e as the bytes of a big-endian 32-byte integer.
   965  // If e is not a 32-byte slice or encodes a value higher than q,
   966  // SetBytesCanonical returns an error.
   967  func (z *Element) SetBytesCanonical(e []byte) error {
   968  	if len(e) != Bytes {
   969  		return errors.New("invalid fr.Element encoding")
   970  	}
   971  	v, err := BigEndian.Element((*[Bytes]byte)(e))
   972  	if err != nil {
   973  		return err
   974  	}
   975  	*z = v
   976  	return nil
   977  }
   978  
   979  // SetBigInt sets z to v and returns z
   980  func (z *Element) SetBigInt(v *big.Int) *Element {
   981  	z.SetZero()
   982  
   983  	var zero big.Int
   984  
   985  	// fast path
   986  	c := v.Cmp(&_modulus)
   987  	if c == 0 {
   988  		// v == 0
   989  		return z
   990  	} else if c != 1 && v.Cmp(&zero) != -1 {
   991  		// 0 < v < q
   992  		return z.setBigInt(v)
   993  	}
   994  
   995  	// get temporary big int from the pool
   996  	vv := pool.BigInt.Get()
   997  
   998  	// copy input + modular reduction
   999  	vv.Mod(v, &_modulus)
  1000  
  1001  	// set big int byte value
  1002  	z.setBigInt(vv)
  1003  
  1004  	// release object into pool
  1005  	pool.BigInt.Put(vv)
  1006  	return z
  1007  }
  1008  
  1009  // setBigInt assumes 0 ⩽ v < q
  1010  func (z *Element) setBigInt(v *big.Int) *Element {
  1011  	vBits := v.Bits()
  1012  
  1013  	if bits.UintSize == 64 {
  1014  		for i := 0; i < len(vBits); i++ {
  1015  			z[i] = uint64(vBits[i])
  1016  		}
  1017  	} else {
  1018  		for i := 0; i < len(vBits); i++ {
  1019  			if i%2 == 0 {
  1020  				z[i/2] = uint64(vBits[i])
  1021  			} else {
  1022  				z[i/2] |= uint64(vBits[i]) << 32
  1023  			}
  1024  		}
  1025  	}
  1026  
  1027  	return z.toMont()
  1028  }
  1029  
  1030  // SetString creates a big.Int with number and calls SetBigInt on z
  1031  //
  1032  // The number prefix determines the actual base: A prefix of
  1033  // ”0b” or ”0B” selects base 2, ”0”, ”0o” or ”0O” selects base 8,
  1034  // and ”0x” or ”0X” selects base 16. Otherwise, the selected base is 10
  1035  // and no prefix is accepted.
  1036  //
  1037  // For base 16, lower and upper case letters are considered the same:
  1038  // The letters 'a' to 'f' and 'A' to 'F' represent digit values 10 to 15.
  1039  //
  1040  // An underscore character ”_” may appear between a base
  1041  // prefix and an adjacent digit, and between successive digits; such
  1042  // underscores do not change the value of the number.
  1043  // Incorrect placement of underscores is reported as a panic if there
  1044  // are no other errors.
  1045  //
  1046  // If the number is invalid this method leaves z unchanged and returns nil, error.
  1047  func (z *Element) SetString(number string) (*Element, error) {
  1048  	// get temporary big int from the pool
  1049  	vv := pool.BigInt.Get()
  1050  
  1051  	if _, ok := vv.SetString(number, 0); !ok {
  1052  		return nil, errors.New("Element.SetString failed -> can't parse number into a big.Int " + number)
  1053  	}
  1054  
  1055  	z.SetBigInt(vv)
  1056  
  1057  	// release object into pool
  1058  	pool.BigInt.Put(vv)
  1059  
  1060  	return z, nil
  1061  }
  1062  
  1063  // MarshalJSON returns json encoding of z (z.Text(10))
  1064  // If z == nil, returns null
  1065  func (z *Element) MarshalJSON() ([]byte, error) {
  1066  	if z == nil {
  1067  		return []byte("null"), nil
  1068  	}
  1069  	const maxSafeBound = 15 // we encode it as number if it's small
  1070  	s := z.Text(10)
  1071  	if len(s) <= maxSafeBound {
  1072  		return []byte(s), nil
  1073  	}
  1074  	var sbb strings.Builder
  1075  	sbb.WriteByte('"')
  1076  	sbb.WriteString(s)
  1077  	sbb.WriteByte('"')
  1078  	return []byte(sbb.String()), nil
  1079  }
  1080  
  1081  // UnmarshalJSON accepts numbers and strings as input
  1082  // See Element.SetString for valid prefixes (0x, 0b, ...)
  1083  func (z *Element) UnmarshalJSON(data []byte) error {
  1084  	s := string(data)
  1085  	if len(s) > Bits*3 {
  1086  		return errors.New("value too large (max = Element.Bits * 3)")
  1087  	}
  1088  
  1089  	// we accept numbers and strings, remove leading and trailing quotes if any
  1090  	if len(s) > 0 && s[0] == '"' {
  1091  		s = s[1:]
  1092  	}
  1093  	if len(s) > 0 && s[len(s)-1] == '"' {
  1094  		s = s[:len(s)-1]
  1095  	}
  1096  
  1097  	// get temporary big int from the pool
  1098  	vv := pool.BigInt.Get()
  1099  
  1100  	if _, ok := vv.SetString(s, 0); !ok {
  1101  		return errors.New("can't parse into a big.Int: " + s)
  1102  	}
  1103  
  1104  	z.SetBigInt(vv)
  1105  
  1106  	// release object into pool
  1107  	pool.BigInt.Put(vv)
  1108  	return nil
  1109  }
  1110  
  1111  // A ByteOrder specifies how to convert byte slices into a Element
  1112  type ByteOrder interface {
  1113  	Element(*[Bytes]byte) (Element, error)
  1114  	PutElement(*[Bytes]byte, Element)
  1115  	String() string
  1116  }
  1117  
  1118  // BigEndian is the big-endian implementation of ByteOrder and AppendByteOrder.
  1119  var BigEndian bigEndian
  1120  
  1121  type bigEndian struct{}
  1122  
  1123  // Element interpret b is a big-endian 32-byte slice.
  1124  // If b encodes a value higher than q, Element returns error.
  1125  func (bigEndian) Element(b *[Bytes]byte) (Element, error) {
  1126  	var z Element
  1127  	z[0] = binary.BigEndian.Uint64((*b)[24:32])
  1128  	z[1] = binary.BigEndian.Uint64((*b)[16:24])
  1129  	z[2] = binary.BigEndian.Uint64((*b)[8:16])
  1130  	z[3] = binary.BigEndian.Uint64((*b)[0:8])
  1131  
  1132  	if !z.smallerThanModulus() {
  1133  		return Element{}, errors.New("invalid fr.Element encoding")
  1134  	}
  1135  
  1136  	z.toMont()
  1137  	return z, nil
  1138  }
  1139  
  1140  func (bigEndian) PutElement(b *[Bytes]byte, e Element) {
  1141  	e.fromMont()
  1142  	binary.BigEndian.PutUint64((*b)[24:32], e[0])
  1143  	binary.BigEndian.PutUint64((*b)[16:24], e[1])
  1144  	binary.BigEndian.PutUint64((*b)[8:16], e[2])
  1145  	binary.BigEndian.PutUint64((*b)[0:8], e[3])
  1146  }
  1147  
  1148  func (bigEndian) String() string { return "BigEndian" }
  1149  
  1150  // LittleEndian is the little-endian implementation of ByteOrder and AppendByteOrder.
  1151  var LittleEndian littleEndian
  1152  
  1153  type littleEndian struct{}
  1154  
  1155  func (littleEndian) Element(b *[Bytes]byte) (Element, error) {
  1156  	var z Element
  1157  	z[0] = binary.LittleEndian.Uint64((*b)[0:8])
  1158  	z[1] = binary.LittleEndian.Uint64((*b)[8:16])
  1159  	z[2] = binary.LittleEndian.Uint64((*b)[16:24])
  1160  	z[3] = binary.LittleEndian.Uint64((*b)[24:32])
  1161  
  1162  	if !z.smallerThanModulus() {
  1163  		return Element{}, errors.New("invalid fr.Element encoding")
  1164  	}
  1165  
  1166  	z.toMont()
  1167  	return z, nil
  1168  }
  1169  
  1170  func (littleEndian) PutElement(b *[Bytes]byte, e Element) {
  1171  	e.fromMont()
  1172  	binary.LittleEndian.PutUint64((*b)[0:8], e[0])
  1173  	binary.LittleEndian.PutUint64((*b)[8:16], e[1])
  1174  	binary.LittleEndian.PutUint64((*b)[16:24], e[2])
  1175  	binary.LittleEndian.PutUint64((*b)[24:32], e[3])
  1176  }
  1177  
  1178  func (littleEndian) String() string { return "LittleEndian" }
  1179  
  1180  // Legendre returns the Legendre symbol of z (either +1, -1, or 0.)
  1181  func (z *Element) Legendre() int {
  1182  	var l Element
  1183  	// z^((q-1)/2)
  1184  	l.expByLegendreExp(*z)
  1185  
  1186  	if l.IsZero() {
  1187  		return 0
  1188  	}
  1189  
  1190  	// if l == 1
  1191  	if l.IsOne() {
  1192  		return 1
  1193  	}
  1194  	return -1
  1195  }
  1196  
  1197  // Sqrt z = √x (mod q)
  1198  // if the square root doesn't exist (x is not a square mod q)
  1199  // Sqrt leaves z unchanged and returns nil
  1200  func (z *Element) Sqrt(x *Element) *Element {
  1201  	// q ≡ 1 (mod 4)
  1202  	// see modSqrtTonelliShanks in math/big/int.go
  1203  	// using https://www.maa.org/sites/default/files/pdf/upload_library/22/Polya/07468342.di020786.02p0470a.pdf
  1204  
  1205  	var y, b, t, w Element
  1206  	// w = x^((s-1)/2))
  1207  	w.expBySqrtExp(*x)
  1208  
  1209  	// y = x^((s+1)/2)) = w * x
  1210  	y.Mul(x, &w)
  1211  
  1212  	// b = xˢ = w * w * x = y * x
  1213  	b.Mul(&w, &y)
  1214  
  1215  	// g = nonResidue ^ s
  1216  	var g = Element{
  1217  		16727483617216526287,
  1218  		14607548025256143850,
  1219  		15265302390528700431,
  1220  		15433920720005950142,
  1221  	}
  1222  	r := uint64(6)
  1223  
  1224  	// compute legendre symbol
  1225  	// t = x^((q-1)/2) = r-1 squaring of xˢ
  1226  	t = b
  1227  	for i := uint64(0); i < r-1; i++ {
  1228  		t.Square(&t)
  1229  	}
  1230  	if t.IsZero() {
  1231  		return z.SetZero()
  1232  	}
  1233  	if !t.IsOne() {
  1234  		// t != 1, we don't have a square root
  1235  		return nil
  1236  	}
  1237  	for {
  1238  		var m uint64
  1239  		t = b
  1240  
  1241  		// for t != 1
  1242  		for !t.IsOne() {
  1243  			t.Square(&t)
  1244  			m++
  1245  		}
  1246  
  1247  		if m == 0 {
  1248  			return z.Set(&y)
  1249  		}
  1250  		// t = g^(2^(r-m-1)) (mod q)
  1251  		ge := int(r - m - 1)
  1252  		t = g
  1253  		for ge > 0 {
  1254  			t.Square(&t)
  1255  			ge--
  1256  		}
  1257  
  1258  		g.Square(&t)
  1259  		y.Mul(&y, &t)
  1260  		b.Mul(&b, &g)
  1261  		r = m
  1262  	}
  1263  }
  1264  
  1265  // Inverse z = x⁻¹ (mod q)
  1266  //
  1267  // note: allocates a big.Int (math/big)
  1268  func (z *Element) Inverse(x *Element) *Element {
  1269  	var _xNonMont big.Int
  1270  	x.BigInt(&_xNonMont)
  1271  	_xNonMont.ModInverse(&_xNonMont, Modulus())
  1272  	z.SetBigInt(&_xNonMont)
  1273  	return z
  1274  }