github.com/turingchain2020/turingchain@v1.1.21/wallet/bipwallet/btcutilecc/random.go (about) 1 // Copyright Turing Corp. 2018 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 package btcutil 6 7 import ( 8 "io" 9 "math/big" 10 ) 11 12 var one = new(big.Int).SetInt64(1) 13 14 // RandFieldElement returns a random element of the field underlying the given 15 // curve using the procedure given in [NSA] A.2.1. 16 // 17 // Implementation copied from Go's crypto/ecdsa package since 18 // the function wasn't public. Modified to always use secp256k1 curve. 19 func RandFieldElement(rand io.Reader) (k *big.Int, err error) { 20 params := Secp256k1().Params() 21 b := make([]byte, params.BitSize/8+8) 22 _, err = io.ReadFull(rand, b) 23 if err != nil { 24 return 25 } 26 27 k = new(big.Int).SetBytes(b) 28 n := new(big.Int).Sub(params.N, one) 29 k.Mod(k, n) 30 k.Add(k, one) 31 return 32 }