Skip to content

Commit

Permalink
add selfEd25519
Browse files Browse the repository at this point in the history
  • Loading branch information
cychuang0924 committed Nov 25, 2024
1 parent c70f57c commit 59bd901
Show file tree
Hide file tree
Showing 31 changed files with 5,753 additions and 17 deletions.
13 changes: 8 additions & 5 deletions crypto/elliptic/ed25519.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"crypto/elliptic"
"math/big"

ED25519 "crypto/ed25519"
ED25519 "github.com/getamis/alice/crypto/elliptic/ed25519prue"

"github.com/decred/dcrd/dcrec/edwards"
)
Expand Down Expand Up @@ -54,12 +54,15 @@ func (ed *ed25519) Slip10SeedList() []byte {
return []byte("ed25519 seed")
}

func (ed *ed25519) CompressedPublicKey(secret *big.Int, method string) []byte {
func (ed *ed25519) CompressedPublicKey(secret *big.Int, method string) ([]byte, error) {
if method == BIP32ED25519 {
x, y := edwards.Edwards().ScalarBaseMult(secret.Bytes()[:32])
return edwards.BigIntPointToEncodedBytes(x, y)[:]
pubKey, err := ED25519.PubKeyCompression(secret.Bytes())
if err != nil {
return nil, err
}
return pubKey, nil
} else {
privateKey := ED25519.NewKeyFromSeed(secret.Bytes()[:32])
return privateKey[32:]
return privateKey[32:], nil
}
}
15 changes: 10 additions & 5 deletions crypto/elliptic/ed25519_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,17 @@ var _ = Describe("ed25519", func() {
})
})
// Test vectors : https://asecuritysite.com/ecc/eddsa4
DescribeTable("Compressed PubKey", func(secrethex string, expected string) {
DescribeTable("Compressed PubKey", func(secrethex string, expected string, method string) {
secret, _ := new(big.Int).SetString(secrethex, 16)
Expect(hex.EncodeToString(Ed25519().CompressedPublicKey(secret, "test")) == expected).Should(BeTrue())
pubKey, err := Ed25519().CompressedPublicKey(secret, method)
Expect(err).Should(BeNil())
Expect(hex.EncodeToString(pubKey) == expected).Should(BeTrue())
},
Entry("case1:", "9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60", "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a"),
Entry("case2:", "4ccd089b28ff96da9db6c346ec114e0f5b8a319f35aba624da8cf6ed4fb8a6fb", "3d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c"),
Entry("case3:", "c5aa8df43f9f837bedb7442f31dcb7b166d38535076f094b85ce3a2e0b4458f7", "fc51cd8e6218a1a38da47ed00230f0580816ed13ba3303ac5deb911548908025"),
Entry("case1:", "9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60", "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a", ""),
Entry("case2:", "4ccd089b28ff96da9db6c346ec114e0f5b8a319f35aba624da8cf6ed4fb8a6fb", "3d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c", ""),
Entry("case3:", "c5aa8df43f9f837bedb7442f31dcb7b166d38535076f094b85ce3a2e0b4458f7", "fc51cd8e6218a1a38da47ed00230f0580816ed13ba3303ac5deb911548908025", ""),
Entry("case4:", "f8c5fe7ef12d7a7f787aa7c3ba107b07f15b9de49528b681f3229f5cb62e725f", "78701ff87a9da875b1aca15421a7974ab753df5f1dd8abff20aa1cca0eca32ab", "bip32"),
Entry("case5:", "c08190be7808e5a48713eef997775fa5c4ecc8beb3c6ea4c8800ea66b82e725f", "a1ab9daf42b069c127c76a9c9ba18351abc6e88b427f988b372db6f63c67bc9f", "bip32"),
Entry("case6:", "18e0793579b9a9e4bdda1b6080af8afacf4ced61c6da7d2c54d25175bf2e725f", "8d6929446ef260a556a8a5a4f7f7349611b34b49888abce2a1f2e24634783022", "bip32"),
)
})
149 changes: 149 additions & 0 deletions crypto/elliptic/ed25519prue/byte/byteorder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package byteorder provides functions for decoding and encoding
// little and big endian integer types from/to byte slices.
package byteorder

func LeUint16(b []byte) uint16 {
_ = b[1] // bounds check hint to compiler; see golang.org/issue/14808
return uint16(b[0]) | uint16(b[1])<<8
}

func LePutUint16(b []byte, v uint16) {
_ = b[1] // early bounds check to guarantee safety of writes below
b[0] = byte(v)
b[1] = byte(v >> 8)
}

func LeAppendUint16(b []byte, v uint16) []byte {
return append(b,
byte(v),
byte(v>>8),
)
}

func LeUint32(b []byte) uint32 {
_ = b[3] // bounds check hint to compiler; see golang.org/issue/14808
return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
}

func LePutUint32(b []byte, v uint32) {
_ = b[3] // early bounds check to guarantee safety of writes below
b[0] = byte(v)
b[1] = byte(v >> 8)
b[2] = byte(v >> 16)
b[3] = byte(v >> 24)
}

func LeAppendUint32(b []byte, v uint32) []byte {
return append(b,
byte(v),
byte(v>>8),
byte(v>>16),
byte(v>>24),
)
}

func LeUint64(b []byte) uint64 {
_ = b[7] // bounds check hint to compiler; see golang.org/issue/14808
return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
}

func LePutUint64(b []byte, v uint64) {
_ = b[7] // early bounds check to guarantee safety of writes below
b[0] = byte(v)
b[1] = byte(v >> 8)
b[2] = byte(v >> 16)
b[3] = byte(v >> 24)
b[4] = byte(v >> 32)
b[5] = byte(v >> 40)
b[6] = byte(v >> 48)
b[7] = byte(v >> 56)
}

func LeAppendUint64(b []byte, v uint64) []byte {
return append(b,
byte(v),
byte(v>>8),
byte(v>>16),
byte(v>>24),
byte(v>>32),
byte(v>>40),
byte(v>>48),
byte(v>>56),
)
}

func BeUint16(b []byte) uint16 {
_ = b[1] // bounds check hint to compiler; see golang.org/issue/14808
return uint16(b[1]) | uint16(b[0])<<8
}

func BePutUint16(b []byte, v uint16) {
_ = b[1] // early bounds check to guarantee safety of writes below
b[0] = byte(v >> 8)
b[1] = byte(v)
}

func BeAppendUint16(b []byte, v uint16) []byte {
return append(b,
byte(v>>8),
byte(v),
)
}

func BeUint32(b []byte) uint32 {
_ = b[3] // bounds check hint to compiler; see golang.org/issue/14808
return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
}

func BePutUint32(b []byte, v uint32) {
_ = b[3] // early bounds check to guarantee safety of writes below
b[0] = byte(v >> 24)
b[1] = byte(v >> 16)
b[2] = byte(v >> 8)
b[3] = byte(v)
}

func BeAppendUint32(b []byte, v uint32) []byte {
return append(b,
byte(v>>24),
byte(v>>16),
byte(v>>8),
byte(v),
)
}

func BeUint64(b []byte) uint64 {
_ = b[7] // bounds check hint to compiler; see golang.org/issue/14808
return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 |
uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56
}

func BePutUint64(b []byte, v uint64) {
_ = b[7] // early bounds check to guarantee safety of writes below
b[0] = byte(v >> 56)
b[1] = byte(v >> 48)
b[2] = byte(v >> 40)
b[3] = byte(v >> 32)
b[4] = byte(v >> 24)
b[5] = byte(v >> 16)
b[6] = byte(v >> 8)
b[7] = byte(v)
}

func BeAppendUint64(b []byte, v uint64) []byte {
return append(b,
byte(v>>56),
byte(v>>48),
byte(v>>40),
byte(v>>32),
byte(v>>24),
byte(v>>16),
byte(v>>8),
byte(v),
)
}
Loading

0 comments on commit 59bd901

Please sign in to comment.