Source file src/crypto/internal/nistec/fiat/p224.go

     1  // Copyright 2021 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  // Code generated by generate.go. DO NOT EDIT.
     6  
     7  package fiat
     8  
     9  import (
    10  	"crypto/subtle"
    11  	"errors"
    12  )
    13  
    14  // P224Element is an integer modulo 2^224 - 2^96 + 1.
    15  //
    16  // The zero value is a valid zero element.
    17  type P224Element struct {
    18  	// Values are represented internally always in the Montgomery domain, and
    19  	// converted in Bytes and SetBytes.
    20  	x p224MontgomeryDomainFieldElement
    21  }
    22  
    23  const p224ElementLen = 28
    24  
    25  type p224UntypedFieldElement = [4]uint64
    26  
    27  // One sets e = 1, and returns e.
    28  func (e *P224Element) One() *P224Element {
    29  	p224SetOne(&e.x)
    30  	return e
    31  }
    32  
    33  // Equal returns 1 if e == t, and zero otherwise.
    34  func (e *P224Element) Equal(t *P224Element) int {
    35  	eBytes := e.Bytes()
    36  	tBytes := t.Bytes()
    37  	return subtle.ConstantTimeCompare(eBytes, tBytes)
    38  }
    39  
    40  // IsZero returns 1 if e == 0, and zero otherwise.
    41  func (e *P224Element) IsZero() int {
    42  	zero := make([]byte, p224ElementLen)
    43  	eBytes := e.Bytes()
    44  	return subtle.ConstantTimeCompare(eBytes, zero)
    45  }
    46  
    47  // Set sets e = t, and returns e.
    48  func (e *P224Element) Set(t *P224Element) *P224Element {
    49  	e.x = t.x
    50  	return e
    51  }
    52  
    53  // Bytes returns the 28-byte big-endian encoding of e.
    54  func (e *P224Element) Bytes() []byte {
    55  	// This function is outlined to make the allocations inline in the caller
    56  	// rather than happen on the heap.
    57  	var out [p224ElementLen]byte
    58  	return e.bytes(&out)
    59  }
    60  
    61  func (e *P224Element) bytes(out *[p224ElementLen]byte) []byte {
    62  	var tmp p224NonMontgomeryDomainFieldElement
    63  	p224FromMontgomery(&tmp, &e.x)
    64  	p224ToBytes(out, (*p224UntypedFieldElement)(&tmp))
    65  	p224InvertEndianness(out[:])
    66  	return out[:]
    67  }
    68  
    69  // SetBytes sets e = v, where v is a big-endian 28-byte encoding, and returns e.
    70  // If v is not 28 bytes or it encodes a value higher than 2^224 - 2^96 + 1,
    71  // SetBytes returns nil and an error, and e is unchanged.
    72  func (e *P224Element) SetBytes(v []byte) (*P224Element, error) {
    73  	if len(v) != p224ElementLen {
    74  		return nil, errors.New("invalid P224Element encoding")
    75  	}
    76  
    77  	// Check for non-canonical encodings (p + k, 2p + k, etc.) by comparing to
    78  	// the encoding of -1 mod p, so p - 1, the highest canonical encoding.
    79  	var minusOneEncoding = new(P224Element).Sub(
    80  		new(P224Element), new(P224Element).One()).Bytes()
    81  	for i := range v {
    82  		if v[i] < minusOneEncoding[i] {
    83  			break
    84  		}
    85  		if v[i] > minusOneEncoding[i] {
    86  			return nil, errors.New("invalid P224Element encoding")
    87  		}
    88  	}
    89  
    90  	var in [p224ElementLen]byte
    91  	copy(in[:], v)
    92  	p224InvertEndianness(in[:])
    93  	var tmp p224NonMontgomeryDomainFieldElement
    94  	p224FromBytes((*p224UntypedFieldElement)(&tmp), &in)
    95  	p224ToMontgomery(&e.x, &tmp)
    96  	return e, nil
    97  }
    98  
    99  // Add sets e = t1 + t2, and returns e.
   100  func (e *P224Element) Add(t1, t2 *P224Element) *P224Element {
   101  	p224Add(&e.x, &t1.x, &t2.x)
   102  	return e
   103  }
   104  
   105  // Sub sets e = t1 - t2, and returns e.
   106  func (e *P224Element) Sub(t1, t2 *P224Element) *P224Element {
   107  	p224Sub(&e.x, &t1.x, &t2.x)
   108  	return e
   109  }
   110  
   111  // Mul sets e = t1 * t2, and returns e.
   112  func (e *P224Element) Mul(t1, t2 *P224Element) *P224Element {
   113  	p224Mul(&e.x, &t1.x, &t2.x)
   114  	return e
   115  }
   116  
   117  // Square sets e = t * t, and returns e.
   118  func (e *P224Element) Square(t *P224Element) *P224Element {
   119  	p224Square(&e.x, &t.x)
   120  	return e
   121  }
   122  
   123  // Select sets v to a if cond == 1, and to b if cond == 0.
   124  func (v *P224Element) Select(a, b *P224Element, cond int) *P224Element {
   125  	p224Selectznz((*p224UntypedFieldElement)(&v.x), p224Uint1(cond),
   126  		(*p224UntypedFieldElement)(&b.x), (*p224UntypedFieldElement)(&a.x))
   127  	return v
   128  }
   129  
   130  func p224InvertEndianness(v []byte) {
   131  	for i := 0; i < len(v)/2; i++ {
   132  		v[i], v[len(v)-1-i] = v[len(v)-1-i], v[i]
   133  	}
   134  }
   135  

View as plain text