Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions dcrec/edwards/ciphering_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2015 The btcsuite developers
// Copyright (c) 2015-2024 The Decred developers
// Copyright (c) 2015-2026 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand All @@ -13,6 +13,8 @@ import (
)

func TestGenerateSharedSecret(t *testing.T) {
t.Parallel()

privKey1, err := GeneratePrivateKey()
if err != nil {
t.Errorf("private key generation error: %s", err)
Expand All @@ -39,6 +41,8 @@ func TestGenerateSharedSecret(t *testing.T) {

// Test 1: Encryption and decryption.
func TestCipheringBasic(t *testing.T) {
t.Parallel()

privkey, err := GeneratePrivateKey()
if err != nil {
t.Fatal("failed to generate private key")
Expand All @@ -64,6 +68,8 @@ func TestCipheringBasic(t *testing.T) {
}

func TestCiphering(t *testing.T) {
t.Parallel()

c := Edwards()

pb, _ := hex.DecodeString("fe38240982f313ae5afb3e904fb8215fb11af1200592b" +
Expand Down Expand Up @@ -103,6 +109,8 @@ func TestCiphering(t *testing.T) {
}

func TestCipheringErrors(t *testing.T) {
t.Parallel()

privkey, err := GeneratePrivateKey()
if err != nil {
t.Fatal("failed to generate private key")
Expand Down Expand Up @@ -178,15 +186,9 @@ func TestCipheringErrors(t *testing.T) {
{bytes.Repeat([]byte{0x07}, 15)},
}
for i, test := range tests2 {
_, err = TstRemovePKCSPadding(test.in)
_, err = removePKCSPadding(test.in)
if err == nil {
t.Errorf("removePKCSPadding #%d did not get error", i)
}
}
}

// TstRemovePKCSPadding makes the internal removePKCSPadding function available
// to the test package.
func TstRemovePKCSPadding(src []byte) ([]byte, error) {
return removePKCSPadding(src)
}
8 changes: 7 additions & 1 deletion dcrec/edwards/curve_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2015-2022 The Decred developers
// Copyright (c) 2015-2026 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -40,6 +40,8 @@ func testPointXRecoveryVectors() []XRecoveryVector {
// - extendedToBigAffine
// - EncodedBytesToBigIntPoint
func TestXRecovery(t *testing.T) {
t.Parallel()

curve := Edwards()

for _, vector := range testPointXRecoveryVectors() {
Expand Down Expand Up @@ -97,6 +99,8 @@ func TestXRecovery(t *testing.T) {
// extendedToBigAffine
// encodedBytesToBigIntPoint
func TestAdd(t *testing.T) {
t.Parallel()

pointHexStrIdx := 0
pointHexStrSet := []string{
"4a3f2684abc42977fe50adbb158a9939cc31b210a7c6e6ea4856395ef3e51bf4",
Expand Down Expand Up @@ -214,6 +218,8 @@ func testVectorsScalarMult() []ScalarMultVector {
// - Double
// - ScalarMult
func TestScalarMult(t *testing.T) {
t.Parallel()

curve := Edwards()

for _, vector := range testVectorsScalarMult() {
Expand Down
175 changes: 98 additions & 77 deletions dcrec/edwards/ecdsa_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2015-2020 The Decred developers
// Copyright (c) 2015-2026 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand All @@ -15,10 +15,13 @@ import (
"math/rand"
"os"
"strings"
"sync"
"testing"
)

func TestGolden(t *testing.T) {
t.Parallel()

// sign.input.gz is a selection of test cases from
// https://ed25519.cr.yp.to/python/sign.input
testDataZ, err := os.Open("testdata/sign.input.gz")
Expand Down Expand Up @@ -200,7 +203,7 @@ func randPrivScalarKeyList(i int) []*PrivateKey {
}

func TestNonStandardSignatures(t *testing.T) {
tRand := rand.New(rand.NewSource(54321))
t.Parallel()

msg := []byte{
0xbe, 0x13, 0xae, 0xf4,
Expand All @@ -214,94 +217,112 @@ func TestNonStandardSignatures(t *testing.T) {
}

pks := randPrivScalarKeyList(50)
for _, pk := range pks {
r, s, err := Sign(pk, msg)
if err != nil {
t.Fatalf("unexpected error %s", err)
}

pubX, pubY := pk.Public()
pub := NewPublicKey(pubX, pubY)
ok := Verify(pub, msg, r, s)
if !ok {
t.Fatalf("expected %v, got %v", true, ok)
}
var wg sync.WaitGroup
for i, pk := range pks {
wg.Add(1)
go func(i int, pk *PrivateKey) {
defer wg.Done()

tRand := rand.New(rand.NewSource(54321))
r, s, err := Sign(pk, msg)
if err != nil {
t.Errorf("unexpected error %s", err)
return
}

// Test serializing/deserializing.
privKeyDupTest, _, err := PrivKeyFromScalar(
copyBytes(pk.ecPk.D.Bytes())[:])
pubX, pubY := pk.Public()
pub := NewPublicKey(pubX, pubY)
ok := Verify(pub, msg, r, s)
if !ok {
t.Errorf("expected %v, got %v", true, ok)
return
}

if err != nil {
t.Fatalf("unexpected error %s", err)
}
// Test serializing/deserializing.
privKeyDupTest, _, err := PrivKeyFromScalar(
copyBytes(pk.ecPk.D.Bytes())[:])

cmp := privKeyDupTest.GetD().Cmp(pk.GetD()) == 0
if !cmp {
t.Fatalf("expected %v, got %v", true, cmp)
}
if err != nil {
t.Errorf("unexpected error %s", err)
return
}

privKeyDupTest2, _, err := PrivKeyFromScalar(pk.Serialize())
if err != nil {
t.Fatalf("unexpected error %s", err)
}
cmp := privKeyDupTest.GetD().Cmp(pk.GetD()) == 0
if !cmp {
t.Errorf("expected %v, got %v", true, cmp)
return
}

cmp = privKeyDupTest2.GetD().Cmp(pk.GetD()) == 0
if !cmp {
t.Fatalf("expected %v, got %v", true, cmp)
}
privKeyDupTest2, _, err := PrivKeyFromScalar(pk.Serialize())
if err != nil {
t.Errorf("unexpected error %s", err)
return
}

// Screw up a random bit in the signature and
// make sure it still fails.
sig := NewSignature(r, s)
sigBad := sig.Serialize()
pos := tRand.Intn(63)
bitPos := tRand.Intn(7)
sigBad[pos] ^= 1 << uint8(bitPos)
cmp = privKeyDupTest2.GetD().Cmp(pk.GetD()) == 0
if !cmp {
t.Errorf("expected %v, got %v", true, cmp)
return
}

bSig, err := ParseSignature(sigBad)
if err != nil {
// Signature failed to parse, continue.
continue
}
ok = Verify(pub, msg, bSig.GetR(), bSig.GetS())
if ok {
t.Fatalf("expected %v, got %v", false, ok)
}
// Screw up a random bit in the signature and
// make sure it still fails.
sig := NewSignature(r, s)
sigBad := sig.Serialize()
pos := tRand.Intn(63)
bitPos := tRand.Intn(7)
sigBad[pos] ^= 1 << uint8(bitPos)

// Screw up a random bit in the pubkey and
// make sure it still fails.
pkBad := pub.Serialize()
pos = tRand.Intn(31)
if pos == 0 {
// 0th bit in first byte doesn't matter
bitPos = tRand.Intn(6) + 1
} else {
bitPos = tRand.Intn(7)
}
pkBad[pos] ^= 1 << uint8(bitPos)
bPub, err := ParsePubKey(pkBad)
if err == nil && bPub != nil {
ok = Verify(bPub, msg, r, s)
bSig, err := ParseSignature(sigBad)
if err != nil {
// Signature failed to parse, nothing more to check.
return
}
ok = Verify(pub, msg, bSig.GetR(), bSig.GetS())
if ok {
t.Fatalf("expected %v, got %v", false, ok)
t.Errorf("expected %v, got %v", false, ok)
return
}
}

// Append an extra byte and make sure the parse fails.
pkBad2 := append(pub.Serialize(), 0x01)
_, err = ParsePubKey(pkBad2)
if err == nil {
t.Fatal("expected err, got nil")
}
// Screw up a random bit in the pubkey and
// make sure it still fails.
pkBad := pub.Serialize()
pos = tRand.Intn(31)
if pos == 0 {
// 0th bit in first byte doesn't matter
bitPos = tRand.Intn(6) + 1
} else {
bitPos = tRand.Intn(7)
}
pkBad[pos] ^= 1 << uint8(bitPos)
bPub, err := ParsePubKey(pkBad)
if err == nil && bPub != nil {
ok = Verify(bPub, msg, r, s)
if ok {
t.Errorf("expected %v, got %v", false, ok)
return
}
}

// Remove a random byte and make sure the parse fails.
pkBad3 := pub.Serialize()
pkBad3 = append(pkBad3[:pos], pkBad3[pos+1:]...)
_, err = ParsePubKey(pkBad3)
if err == nil {
t.Fatal("expected err, got nil")
}
// Append an extra byte and make sure the parse fails.
pkBad2 := append(pub.Serialize(), 0x01)
_, err = ParsePubKey(pkBad2)
if err == nil {
t.Errorf("expected err, got nil")
return
}

// Remove a random byte and make sure the parse fails.
pkBad3 := pub.Serialize()
pkBad3 = append(pkBad3[:pos], pkBad3[pos+1:]...)
_, err = ParsePubKey(pkBad3)
if err == nil {
t.Errorf("expected err, got nil")
return
}
}(i, pk)
}
wg.Wait()
}

func randPrivKeyList(i int) []*PrivateKey {
Expand Down
6 changes: 5 additions & 1 deletion dcrec/edwards/primitives_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2015-2022 The Decred developers
// Copyright (c) 2015-2026 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -45,6 +45,8 @@ func testConversionVectors() []ConversionVector {
// - FieldElementToBigInt
// - EncodedBytesToFieldElement
func TestConversion(t *testing.T) {
t.Parallel()

encodedNumToStrIdx := 0
encodedNumToStrSet := []string{
"20196841024736227335511321252453997055107605473446826399550527392484145048463",
Expand Down Expand Up @@ -229,6 +231,8 @@ func testPointConversionVectors() []ConversionVector {
// - extendedToBigAffine
// - EncodedBytesToBigIntPoint
func TestPointConversion(t *testing.T) {
t.Parallel()

decodedPointsIdx := 0
decodedPointsSet := []string{
"36342386295235510298682738805067969701306540594271578388800019131093341795154,12122921476001995645148951048614280991245620197289177635264906062452356396947",
Expand Down
4 changes: 3 additions & 1 deletion dcrec/edwards/privkey_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019 The Decred developers
// Copyright (c) 2019-2026 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand All @@ -9,6 +9,8 @@ import (
)

func TestPrivKeySign(t *testing.T) {
t.Parallel()

message := []byte("the quick brown fox jumps over the lazy dog")

privkey, err := GeneratePrivateKey()
Expand Down
Loading