Source file src/crypto/elliptic/nistec_p256.go

     1  // Copyright 2022 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  //go:build amd64 || arm64
     6  
     7  package elliptic
     8  
     9  import (
    10  	"crypto/internal/nistec"
    11  	"math/big"
    12  )
    13  
    14  func (c p256Curve) Inverse(k *big.Int) *big.Int {
    15  	if k.Sign() < 0 {
    16  		// This should never happen.
    17  		k = new(big.Int).Neg(k)
    18  	}
    19  	if k.Cmp(c.params.N) >= 0 {
    20  		// This should never happen.
    21  		k = new(big.Int).Mod(k, c.params.N)
    22  	}
    23  	scalar := k.FillBytes(make([]byte, 32))
    24  	inverse, err := nistec.P256OrdInverse(scalar)
    25  	if err != nil {
    26  		panic("crypto/elliptic: nistec rejected normalized scalar")
    27  	}
    28  	return new(big.Int).SetBytes(inverse)
    29  }
    30  

View as plain text