github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/crypto/secp256k1/secp256k1_internal_test.go (about) 1 package secp256k1 2 3 import ( 4 "bytes" 5 "math/big" 6 "testing" 7 8 underlyingSecp256k1 "github.com/btcsuite/btcd/btcec/v2" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func Test_genPrivKey(t *testing.T) { 13 t.Parallel() 14 15 empty := make([]byte, 32) 16 oneB := big.NewInt(1).Bytes() 17 onePadded := make([]byte, 32) 18 copy(onePadded[32-len(oneB):32], oneB) 19 t.Logf("one padded: %v, len=%v", onePadded, len(onePadded)) 20 21 validOne := append(empty, onePadded...) 22 tests := []struct { 23 name string 24 notSoRand []byte 25 shouldPanic bool 26 }{ 27 {"empty bytes (panics because 1st 32 bytes are zero and 0 is not a valid field element)", empty, true}, 28 {"curve order: N", underlyingSecp256k1.S256().N.Bytes(), true}, 29 {"valid because 0 < 1 < N", validOne, false}, 30 } 31 for _, tt := range tests { 32 tt := tt 33 t.Run(tt.name, func(t *testing.T) { 34 t.Parallel() 35 36 if tt.shouldPanic { 37 require.Panics(t, func() { 38 genPrivKey(bytes.NewReader(tt.notSoRand)) 39 }) 40 return 41 } 42 got := genPrivKey(bytes.NewReader(tt.notSoRand)) 43 fe := new(big.Int).SetBytes(got[:]) 44 require.True(t, fe.Cmp(underlyingSecp256k1.S256().N) < 0) 45 require.True(t, fe.Sign() > 0) 46 }) 47 } 48 }