github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/btcec/internal_test.go (about)

     1  // Copyright (c) 2013-2014 The btcsuite developers
     2  // Copyright (c) 2016 The Dash developers
     3  // Use of this source code is governed by an ISC
     4  // license that can be found in the LICENSE file.
     5  
     6  package btcec
     7  
     8  import (
     9  	"math/big"
    10  )
    11  
    12  const (
    13  	TstPubkeyUncompressed = pubkeyUncompressed
    14  	TstPubkeyCompressed   = pubkeyCompressed
    15  	TstPubkeyHybrid       = pubkeyHybrid
    16  )
    17  
    18  // TstRawInts allows the test package to get the integers from the internal
    19  // field representation for ensuring correctness.  It is only available during
    20  // the tests.
    21  func (f *fieldVal) TstRawInts() [10]uint32 {
    22  	return f.n
    23  }
    24  
    25  // TstSetRawInts allows the test package to directly set the integers used by
    26  // the internal field representation.  It is only available during the tests.
    27  func (f *fieldVal) TstSetRawInts(raw [10]uint32) *fieldVal {
    28  	for i := 0; i < len(raw); i++ {
    29  		f.n[i] = raw[i]
    30  	}
    31  	return f
    32  }
    33  
    34  // TstFieldJacobianToBigAffine makes the internal fieldJacobianToBigAffine
    35  // function available to the test package.
    36  func (curve *KoblitzCurve) TstFieldJacobianToBigAffine(x, y, z *fieldVal) (*big.Int, *big.Int) {
    37  	return curve.fieldJacobianToBigAffine(x, y, z)
    38  }
    39  
    40  // TstIsJacobianOnCurve returns boolean if the point (x,y,z) is on the curve.
    41  func (curve *KoblitzCurve) TstIsJacobianOnCurve(x, y, z *fieldVal) bool {
    42  	// Elliptic curve equation for secp256k1 is: y^2 = x^3 + 7
    43  	// In Jacobian coordinates, Y = y/z^3 and X = x/z^2
    44  	// Thus:
    45  	// (y/z^3)^2 = (x/z^2)^3 + 7
    46  	// y^2/z^6 = x^3/z^6 + 7
    47  	// y^2 = x^3 + 7*z^6
    48  	var y2, z2, x3, result fieldVal
    49  	y2.SquareVal(y).Normalize()
    50  	z2.SquareVal(z)
    51  	x3.SquareVal(x).Mul(x)
    52  	result.SquareVal(&z2).Mul(&z2).MulInt(7).Add(&x3).Normalize()
    53  	return y2.Equals(&result)
    54  }
    55  
    56  // TstAddJacobian makes the internal addJacobian function available to the test
    57  // package.
    58  func (curve *KoblitzCurve) TstAddJacobian(x1, y1, z1, x2, y2, z2, x3, y3, z3 *fieldVal) {
    59  	curve.addJacobian(x1, y1, z1, x2, y2, z2, x3, y3, z3)
    60  }
    61  
    62  // TstDoubleJacobian makes the internal doubleJacobian function available to the test
    63  // package.
    64  func (curve *KoblitzCurve) TstDoubleJacobian(x1, y1, z1, x3, y3, z3 *fieldVal) {
    65  	curve.doubleJacobian(x1, y1, z1, x3, y3, z3)
    66  }
    67  
    68  // NewFieldVal returns a new field value set to 0.  This is only available to
    69  // the test package.
    70  func NewFieldVal() *fieldVal {
    71  	return new(fieldVal)
    72  }
    73  
    74  // TstNonceRFC6979 makes the nonceRFC6979 function available to the test package.
    75  func TstNonceRFC6979(privkey *big.Int, hash []byte) *big.Int {
    76  	return nonceRFC6979(privkey, hash)
    77  }
    78  
    79  // TstRemovePKCSPadding makes the internal removePKCSPadding function available
    80  // to the test package.
    81  func TstRemovePKCSPadding(src []byte) ([]byte, error) {
    82  	return removePKCSPadding(src)
    83  }