github.com/ledgerwatch/erigon-lib@v1.0.0/crypto/secp256k1.go (about)

     1  /*
     2     Copyright 2022 The Erigon contributors
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package crypto
    18  
    19  import (
    20  	"github.com/holiman/uint256"
    21  
    22  	"github.com/ledgerwatch/erigon-lib/common/hexutility"
    23  )
    24  
    25  var (
    26  	secp256k1N     = new(uint256.Int).SetBytes(hexutility.MustDecodeHex("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"))
    27  	secp256k1halfN = new(uint256.Int).Rsh(secp256k1N, 1)
    28  )
    29  
    30  // See Appendix F "Signing Transactions" of the Yellow Paper
    31  func TransactionSignatureIsValid(v byte, r, s *uint256.Int, allowPreEip2s bool) bool {
    32  	if r.IsZero() || s.IsZero() {
    33  		return false
    34  	}
    35  
    36  	// See EIP-2: Homestead Hard-fork Changes
    37  	if !allowPreEip2s && s.Gt(secp256k1halfN) {
    38  		return false
    39  	}
    40  
    41  	return r.Lt(secp256k1N) && s.Lt(secp256k1N) && (v == 0 || v == 1)
    42  }