github.com/emmansun/gmsm@v0.29.1/internal/sm2ec/fiat/sm2p256scalar.go (about) 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 // SM2P256OrderElement is an integer modulo 2^256 - 2^224 - 188730267045675049073202170516080344797. 15 // 16 // The zero value is a valid zero element. 17 type SM2P256OrderElement struct { 18 // Values are represented internally always in the Montgomery domain, and 19 // converted in Bytes and SetBytes. 20 x sm2p256scalarMontgomeryDomainFieldElement 21 } 22 23 const sm2p256scalarElementLen = 32 24 25 type sm2p256scalarUntypedFieldElement = [4]uint64 26 27 // One sets e = 1, and returns e. 28 func (e *SM2P256OrderElement) One() *SM2P256OrderElement { 29 sm2p256scalarSetOne(&e.x) 30 return e 31 } 32 33 // Equal returns 1 if e == t, and zero otherwise. 34 func (e *SM2P256OrderElement) Equal(t *SM2P256OrderElement) 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 *SM2P256OrderElement) IsZero() int { 42 zero := make([]byte, sm2p256scalarElementLen) 43 eBytes := e.Bytes() 44 return subtle.ConstantTimeCompare(eBytes, zero) 45 } 46 47 // Set sets e = t, and returns e. 48 func (e *SM2P256OrderElement) Set(t *SM2P256OrderElement) *SM2P256OrderElement { 49 e.x = t.x 50 return e 51 } 52 53 // Bytes returns the 32-byte big-endian encoding of e. 54 func (e *SM2P256OrderElement) 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 [sm2p256scalarElementLen]byte 58 return e.bytes(&out) 59 } 60 61 func (e *SM2P256OrderElement) bytes(out *[sm2p256scalarElementLen]byte) []byte { 62 var tmp sm2p256scalarNonMontgomeryDomainFieldElement 63 sm2p256scalarFromMontgomery(&tmp, &e.x) 64 sm2p256scalarToBytes(out, (*sm2p256scalarUntypedFieldElement)(&tmp)) 65 sm2p256scalarInvertEndianness(out[:]) 66 return out[:] 67 } 68 69 // SetBytes sets e = v, where v is a big-endian 32-byte encoding, and returns e. 70 // If v is not 32 bytes or it encodes a value higher than 2^256 - 2^224 - 188730267045675049073202170516080344797, 71 // SetBytes returns nil and an error, and e is unchanged. 72 func (e *SM2P256OrderElement) SetBytes(v []byte) (*SM2P256OrderElement, error) { 73 if len(v) != sm2p256scalarElementLen { 74 return nil, errors.New("invalid SM2P256OrderElement 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(SM2P256OrderElement).Sub( 80 new(SM2P256OrderElement), new(SM2P256OrderElement).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 SM2P256OrderElement encoding") 87 } 88 }*/ 89 var in [sm2p256scalarElementLen]byte 90 copy(in[:], v) 91 sm2p256scalarInvertEndianness(in[:]) 92 var tmp sm2p256scalarNonMontgomeryDomainFieldElement 93 sm2p256scalarFromBytes((*sm2p256scalarUntypedFieldElement)(&tmp), &in) 94 sm2p256scalarToMontgomery(&e.x, &tmp) 95 return e, nil 96 } 97 98 // Add sets e = t1 + t2, and returns e. 99 func (e *SM2P256OrderElement) Add(t1, t2 *SM2P256OrderElement) *SM2P256OrderElement { 100 sm2p256scalarAdd(&e.x, &t1.x, &t2.x) 101 return e 102 } 103 104 // Sub sets e = t1 - t2, and returns e. 105 func (e *SM2P256OrderElement) Sub(t1, t2 *SM2P256OrderElement) *SM2P256OrderElement { 106 sm2p256scalarSub(&e.x, &t1.x, &t2.x) 107 return e 108 } 109 110 // Mul sets e = t1 * t2, and returns e. 111 func (e *SM2P256OrderElement) Mul(t1, t2 *SM2P256OrderElement) *SM2P256OrderElement { 112 sm2p256scalarMul(&e.x, &t1.x, &t2.x) 113 return e 114 } 115 116 // Square sets e = t * t, and returns e. 117 func (e *SM2P256OrderElement) Square(t *SM2P256OrderElement) *SM2P256OrderElement { 118 sm2p256scalarSquare(&e.x, &t.x) 119 return e 120 } 121 122 // Select sets v to a if cond == 1, and to b if cond == 0. 123 func (v *SM2P256OrderElement) Select(a, b *SM2P256OrderElement, cond int) *SM2P256OrderElement { 124 sm2p256scalarSelectznz((*sm2p256scalarUntypedFieldElement)(&v.x), sm2p256scalarUint1(cond), 125 (*sm2p256scalarUntypedFieldElement)(&b.x), (*sm2p256scalarUntypedFieldElement)(&a.x)) 126 return v 127 } 128 129 func sm2p256scalarInvertEndianness(v []byte) { 130 for i := 0; i < len(v)/2; i++ { 131 v[i], v[len(v)-1-i] = v[len(v)-1-i], v[i] 132 } 133 }