Skip to content
This repository has been archived by the owner on May 22, 2023. It is now read-only.

Commit

Permalink
Merge pull request #5 from spacemeshos/remove-key-derivation
Browse files Browse the repository at this point in the history
Remove key derivation function from ed25519-recovery
  • Loading branch information
fasmat committed Mar 30, 2023
2 parents be6cd97 + bb2e612 commit a68b6d8
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 54 deletions.
18 changes: 0 additions & 18 deletions ed25519.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"bytes"
"crypto/ed25519"
"crypto/sha512"
"encoding/binary"
"errors"
"io"
"strconv"
Expand Down Expand Up @@ -47,23 +46,6 @@ func NewKeyFromSeed(seed []byte) PrivateKey {
return ed25519.NewKeyFromSeed(seed)
}

// NewDerivedKeyFromSeed calculates a private key from a 32 bytes random seed,
// an integer index and salt.
func NewDerivedKeyFromSeed(seed []byte, index uint64, salt []byte) PrivateKey {
if l := len(seed); l != SeedSize {
panic("ed25519: bad seed length: " + strconv.Itoa(l))
}

digest := sha512.New()
digest.Write(seed)
digest.Write(salt)
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, index)
digest.Write(buf)

return NewKeyFromSeed(digest.Sum(nil)[:SeedSize])
}

// ExtractPublicKey extracts the signer's public key given a message and its
// signature. It will panic if len(sig) is not [SignatureSize].
//
Expand Down
25 changes: 0 additions & 25 deletions ed25519_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,28 +60,3 @@ func Fuzz_Sign(f *testing.F) {
require.EqualValues(t, key.Public(), pub)
})
}

func Fuzz_Derive(f *testing.F) {
py, err := pure25519.New()
if err != nil {
f.Skip("failed to initialize python bindings")
}

f.Add(int64(0), []byte("Spacemesh rocks"), uint64(5))
f.Fuzz(func(t *testing.T, rndSeed int64, salt []byte, index uint64) {
src := rand.New(rand.NewSource(rndSeed))
seed := make([]byte, 32)

// generate random seed
_, err := src.Read(seed)
require.NoError(t, err, "failed to read random seed")

// derive key from seed
goKey := NewDerivedKeyFromSeed(seed, index, salt)
pyKey, err := py.Derive(seed, salt, index)
require.NoError(t, err)

// compare keys
require.EqualValues(t, pyKey, goKey.Seed())
})
}
11 changes: 0 additions & 11 deletions ed25519_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package ed25519

import (
"encoding/hex"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -56,13 +55,3 @@ func Test_SignVerify(t *testing.T) {
wrongMessage := []byte("wrong message")
require.False(t, Verify(public, wrongMessage, sig), "signature of different message accepted")
}

func Test_Derive(t *testing.T) {
const expectedEncodedKey = "b6e1caa7ed8fb8b517dbbd5a49f7c9e76f33f0dd74100396207b640479d6fade2b0f080a354fd3c981630efe75bcbc5f4134895b749364f25badeae5a687950c"
const s = "8d03a58456bb1b45f696032444b09d476fa5406f998ed0a50e694ee8a40cfb09"
seed, err := hex.DecodeString(s)
require.NoError(t, err)

privateKey1 := NewDerivedKeyFromSeed(seed, 5, []byte("Spacemesh rocks"))
require.Equal(t, expectedEncodedKey, hex.EncodeToString(privateKey1), "Unexpected key")
}

0 comments on commit a68b6d8

Please sign in to comment.