Skip to content

Commit a5674a1

Browse files
authored
Merge pull request #299 from getamis/addSelfEd25519AndMethod-2
Fix edwards cruve CompressedPublicKey
2 parents c70f57c + 526bdd9 commit a5674a1

30 files changed

+793
-176
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ profile.out
1919

2020
/tools/*
2121
!/tools/*.mk
22+
!/tools/tools.go
2223

2324
# Exclude example binaries
2425
example/example

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ notifications:
2828
# .golangci.yml file at the top level of your repo.
2929
script:
3030
- make tss-example || travis_terminate 1; # Build tss-example to make sure example is executable.
31-
- make lint || travis_terminate 1; # Run a bunch of code checkers/linters in parallel.
31+
- make install-golinter || make lint || travis_terminate 1; # Run a bunch of code checkers/linters in parallel.
3232
- make unit-test || travis_terminate 1; # Run all the tests with the race detector enabled.
3333

3434
after_success:

crypto/binaryquadraticform/interface.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"math/big"
1919
)
2020

21-
//go:generate mockery --name Exper
21+
//go:generate go run github.com/vektra/mockery/v2 --name Exper
2222
type Exper interface {
2323
Exp(power *big.Int) (*BQuadraticForm, error)
2424
ToMessage() *BQForm

crypto/binaryquadraticform/mocks/Exper.go

+13-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crypto/elliptic/ed25519.go

+23-13
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,23 @@ package elliptic
1616

1717
import (
1818
"crypto/elliptic"
19+
"crypto/sha512"
1920
"math/big"
2021

21-
ED25519 "crypto/ed25519"
22+
"filippo.io/edwards25519"
2223

23-
"github.com/decred/dcrd/dcrec/edwards"
24+
edwards "github.com/decred/dcrd/dcrec/edwards"
25+
)
26+
27+
const (
28+
CurveTypeEd25519 CurveType = "ed25519"
2429
)
2530

2631
var (
2732
big1 = big.NewInt(1)
2833
ed25519Curve = &ed25519{
2934
Curve: edwards.Edwards(),
3035
}
31-
32-
BIP32ED25519 = "bip32"
3336
)
3437

3538
type ed25519 struct {
@@ -46,20 +49,27 @@ func (ed *ed25519) Neg(x, y *big.Int) (*big.Int, *big.Int) {
4649
return negativeX.Mod(negativeX, ed.Params().P), new(big.Int).Set(y)
4750
}
4851

49-
func (ed *ed25519) Type() string {
50-
return "ed25519"
52+
func (ed *ed25519) Type() CurveType {
53+
return CurveTypeEd25519
5154
}
5255

5356
func (ed *ed25519) Slip10SeedList() []byte {
5457
return []byte("ed25519 seed")
5558
}
5659

57-
func (ed *ed25519) CompressedPublicKey(secret *big.Int, method string) []byte {
58-
if method == BIP32ED25519 {
59-
x, y := edwards.Edwards().ScalarBaseMult(secret.Bytes()[:32])
60-
return edwards.BigIntPointToEncodedBytes(x, y)[:]
61-
} else {
62-
privateKey := ED25519.NewKeyFromSeed(secret.Bytes()[:32])
63-
return privateKey[32:]
60+
func (ed *ed25519) CompressedPoint(s *big.Int, isHash bool) []byte {
61+
if isHash {
62+
sha512 := sha512.New()
63+
sha512.Write(s.Bytes()[:32])
64+
h := sha512.Sum(nil)
65+
return pubKeyRFC8032Compression(h[:32])
6466
}
67+
return pubKeyRFC8032Compression(s.Bytes()[:32])
68+
}
69+
70+
func pubKeyRFC8032Compression(secret []byte) []byte {
71+
s := edwards25519.NewScalar()
72+
s, _ = s.SetBytesWithClamping(secret)
73+
v := edwards25519.NewGeneratorPoint().ScalarMult(s, edwards25519.NewGeneratorPoint())
74+
return v.Bytes()
6575
}

crypto/elliptic/ed25519_test.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,16 @@ var _ = Describe("ed25519", func() {
3434
})
3535
})
3636
// Test vectors : https://asecuritysite.com/ecc/eddsa4
37-
DescribeTable("Compressed PubKey", func(secrethex string, expected string) {
37+
DescribeTable("Compressed Point", func(secrethex string, expected string, isHash bool) {
3838
secret, _ := new(big.Int).SetString(secrethex, 16)
39-
Expect(hex.EncodeToString(Ed25519().CompressedPublicKey(secret, "test")) == expected).Should(BeTrue())
39+
pubKey := Ed25519().CompressedPoint(secret, isHash)
40+
Expect(hex.EncodeToString(pubKey) == expected).Should(BeTrue())
4041
},
41-
Entry("case1:", "9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60", "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a"),
42-
Entry("case2:", "4ccd089b28ff96da9db6c346ec114e0f5b8a319f35aba624da8cf6ed4fb8a6fb", "3d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c"),
43-
Entry("case3:", "c5aa8df43f9f837bedb7442f31dcb7b166d38535076f094b85ce3a2e0b4458f7", "fc51cd8e6218a1a38da47ed00230f0580816ed13ba3303ac5deb911548908025"),
42+
Entry("case1:", "9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60", "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a", true),
43+
Entry("case2:", "4ccd089b28ff96da9db6c346ec114e0f5b8a319f35aba624da8cf6ed4fb8a6fb", "3d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c", true),
44+
Entry("case3:", "c5aa8df43f9f837bedb7442f31dcb7b166d38535076f094b85ce3a2e0b4458f7", "fc51cd8e6218a1a38da47ed00230f0580816ed13ba3303ac5deb911548908025", true),
45+
Entry("case4:", "f8c5fe7ef12d7a7f787aa7c3ba107b07f15b9de49528b681f3229f5cb62e725f", "78701ff87a9da875b1aca15421a7974ab753df5f1dd8abff20aa1cca0eca32ab", false),
46+
Entry("case5:", "c08190be7808e5a48713eef997775fa5c4ecc8beb3c6ea4c8800ea66b82e725f", "a1ab9daf42b069c127c76a9c9ba18351abc6e88b427f988b372db6f63c67bc9f", false),
47+
Entry("case6:", "18e0793579b9a9e4bdda1b6080af8afacf4ced61c6da7d2c54d25175bf2e725f", "8d6929446ef260a556a8a5a4f7f7349611b34b49888abce2a1f2e24634783022", false),
4448
)
4549
})

crypto/elliptic/elliptic_curve.go

+5-22
Original file line numberDiff line numberDiff line change
@@ -29,35 +29,18 @@ func (c *ellipticCurve) Neg(x, y *big.Int) (*big.Int, *big.Int) {
2929
return new(big.Int).Set(x), NegY.Mod(NegY, c.Curve.Params().P)
3030
}
3131

32-
func (c *ellipticCurve) Type() string {
33-
if c.Params().N.Cmp(p256Curve.Params().N) == 0 {
34-
return "P256"
35-
}
36-
if c.Params().N.Cmp(secp256k1Curve.Params().N) == 0 {
37-
return "secp256k1"
38-
}
39-
return "None"
40-
}
41-
42-
func (c *ellipticCurve) Slip10SeedList() []byte {
43-
if c.Params().N.Cmp(p256Curve.Params().N) == 0 {
44-
return []byte("Bitcoin seed")
45-
}
46-
if c.Params().N.Cmp(secp256k1Curve.Params().N) == 0 {
47-
return []byte("Bitcoin seed")
48-
}
49-
return []byte("None")
50-
}
51-
5232
// WARN: Only support P256 and Secp256k1
53-
func (c *ellipticCurve) CompressedPublicKey(secret *big.Int, method string) []byte {
33+
func (c *ellipticCurve) CompressedPoint(s *big.Int, isHash bool) []byte {
34+
if isHash {
35+
panic("Not implemented")
36+
}
5437
/* Returns the compressed bytes for this point.
5538
If pt.y is odd, 0x03 is pre-pended to pt.x.
5639
If pt.y is even, 0x02 is pre-pended to pt.x.
5740
Returns:
5841
bytes: Compressed byte representation.
5942
*/
60-
x, y := c.ScalarBaseMult(secret.Bytes())
43+
x, y := c.ScalarBaseMult(s.Bytes())
6144
xBytePadding := x.Bytes()
6245
if len(x.Bytes()) < 32 {
6346
padding := make([]byte, 32-len(x.Bytes()))

crypto/elliptic/interface.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ import (
1919
"math/big"
2020
)
2121

22+
type CurveType string
23+
2224
type Curve interface {
2325
elliptic.Curve
2426

2527
Neg(x1, y1 *big.Int) (x, y *big.Int)
26-
Type() string
28+
Type() CurveType
2729
Slip10SeedList() []byte
28-
CompressedPublicKey(secret *big.Int, method string) []byte
30+
CompressedPoint(s *big.Int, isHash bool) []byte
2931
}

crypto/elliptic/p256.go

+21-3
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,30 @@ import (
1818
"crypto/elliptic"
1919
)
2020

21+
const (
22+
CurveTypeP256 CurveType = "p256"
23+
)
24+
2125
var (
22-
p256Curve = &ellipticCurve{
23-
Curve: elliptic.P256(),
26+
p256Curve = &p256{
27+
ellipticCurve: &ellipticCurve{
28+
Curve: elliptic.P256(),
29+
},
2430
}
2531
)
2632

27-
func P256() *ellipticCurve {
33+
func P256() *p256 {
2834
return p256Curve
2935
}
36+
37+
type p256 struct {
38+
*ellipticCurve
39+
}
40+
41+
func (c *p256) Type() CurveType {
42+
return CurveTypeP256
43+
}
44+
45+
func (c *p256) Slip10SeedList() []byte {
46+
return []byte("Bitcoin seed")
47+
}

crypto/elliptic/secp256k1.go

+21-3
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,30 @@ import (
1818
"github.com/btcsuite/btcd/btcec/v2"
1919
)
2020

21+
const (
22+
CurveTypeSecp256k1 CurveType = "secp256k1"
23+
)
24+
2125
var (
22-
secp256k1Curve = &ellipticCurve{
23-
Curve: btcec.S256(),
26+
secp256k1Curve = &secp256k1{
27+
ellipticCurve: &ellipticCurve{
28+
Curve: btcec.S256(),
29+
},
2430
}
2531
)
2632

27-
func Secp256k1() *ellipticCurve {
33+
func Secp256k1() *secp256k1 {
2834
return secp256k1Curve
2935
}
36+
37+
type secp256k1 struct {
38+
*ellipticCurve
39+
}
40+
41+
func (c *secp256k1) Type() CurveType {
42+
return CurveTypeSecp256k1
43+
}
44+
45+
func (c *secp256k1) Slip10SeedList() []byte {
46+
return []byte("Bitcoin seed")
47+
}

crypto/elliptic/secp256k1_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ var _ = Describe("secp256k1", func() {
3535
})
3636
})
3737

38-
DescribeTable("Compressed PubKey", func(secrethex string, expected string) {
38+
DescribeTable("Compressed Point", func(secrethex string, expected string) {
3939
secret, _ := new(big.Int).SetString(secrethex, 16)
40-
Expect(hex.EncodeToString(Secp256k1().CompressedPublicKey(secret, "test")) == expected).Should(BeTrue())
40+
pubKey := Secp256k1().CompressedPoint(secret, false)
41+
Expect(hex.EncodeToString(pubKey) == expected).Should(BeTrue())
4142
},
4243
Entry("case1:", "f91d8f3a49805fff9289769247e984b355939679f3080156fe295229e00f25af", "0252972572d465d016d4c501887b8df303eee3ed602c056b1eb09260dfa0da0ab2"),
4344
Entry("case2:", "ac609e0cc9681f8cb63e968be20e0f19721751561944f5b4e52d54d5f27ec57b", "0318ed2e1ec629e2d3dae7be1103d4f911c24e0c80e70038f5eb5548245c475f50"),

crypto/homo/interface.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
pt "github.com/getamis/alice/crypto/ecpointgrouplaw"
2323
)
2424

25-
//go:generate mockery --name Pubkey
25+
//go:generate go run github.com/vektra/mockery/v2 --name Pubkey
2626
type Pubkey interface {
2727
GetMessageRange(fieldOrder *big.Int) *big.Int
2828
Encrypt(m []byte) ([]byte, error)
@@ -32,7 +32,7 @@ type Pubkey interface {
3232
ToPubKeyBytes() []byte
3333
}
3434

35-
//go:generate mockery --name Crypto
35+
//go:generate go run github.com/vektra/mockery/v2 --name Crypto
3636
type Crypto interface {
3737
Pubkey
3838
Decrypt(c []byte) ([]byte, error)

0 commit comments

Comments
 (0)