Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit fd4391b

Browse files
authored
Add test vectors from RFC-28 (#297)
* add test vectors from RFC-28 * fix the tests for go 1.15 * make the test struct consistent with other tests
1 parent edf83a5 commit fd4391b

File tree

3 files changed

+157
-2
lines changed

3 files changed

+157
-2
lines changed

signature_test.go

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package iotago_test
22

33
import (
4+
"encoding/json"
45
"errors"
5-
"github.com/iotaledger/hive.go/serializer"
6-
"github.com/iotaledger/iota.go/v2/tpkg"
6+
"io/ioutil"
7+
"path/filepath"
78
"testing"
89

10+
"github.com/iotaledger/hive.go/serializer"
911
"github.com/iotaledger/iota.go/v2"
12+
"github.com/iotaledger/iota.go/v2/tpkg"
1013
"github.com/stretchr/testify/assert"
14+
"github.com/stretchr/testify/require"
1115
)
1216

1317
func TestSignatureSelector(t *testing.T) {
@@ -68,3 +72,39 @@ func TestEd25519Signature_Serialize(t *testing.T) {
6872
})
6973
}
7074
}
75+
76+
func TestEd25519Signature_Valid(t *testing.T) {
77+
type test struct {
78+
Address tpkg.HexBytes `json:"address"`
79+
Message tpkg.HexBytes `json:"message"`
80+
PublicKey tpkg.HexBytes `json:"pub_key"`
81+
Signature tpkg.HexBytes `json:"signature"`
82+
Valid bool `json:"valid"`
83+
}
84+
var tests []test
85+
// load the tests from file
86+
b, err := ioutil.ReadFile(filepath.Join("testdata", t.Name()+".json"))
87+
require.NoError(t, err)
88+
require.NoError(t, json.Unmarshal(b, &tests))
89+
90+
for _, tt := range tests {
91+
t.Run("", func(t *testing.T) {
92+
// deserialize the address from the test
93+
addr := &iotago.Ed25519Address{}
94+
_, err = addr.Deserialize(tt.Address, serializer.DeSeriModePerformValidation)
95+
require.NoError(t, err)
96+
// create the signature type
97+
sig := &iotago.Ed25519Signature{}
98+
copy(sig.PublicKey[:], tt.PublicKey)
99+
copy(sig.Signature[:], tt.Signature)
100+
101+
sigError := sig.Valid(tt.Message, addr)
102+
switch tt.Valid {
103+
case true:
104+
assert.NoError(t, sigError)
105+
case false:
106+
assert.Error(t, sigError)
107+
}
108+
})
109+
}
110+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
[
2+
{
3+
"address": "008f5a6fdcfef8989fb88312cbc956ccc54dceb9c693ec99c08a28bdda5f11da44",
4+
"message": "8c93255d71dcab10e8f379c26200f3c7bd5f09d9bc3068d3ef4edeb4853022b6",
5+
"pub_key": "c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac03fa",
6+
"signature": "c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac037a0000000000000000000000000000000000000000000000000000000000000000",
7+
"valid": true
8+
},
9+
{
10+
"address": "008f5a6fdcfef8989fb88312cbc956ccc54dceb9c693ec99c08a28bdda5f11da44",
11+
"message": "9bd9f44f4dcc75bd531b56b2cd280b0bb38fc1cd6d1230e14861d861de092e79",
12+
"pub_key": "c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac03fa",
13+
"signature": "f7badec5b8abeaf699583992219b7b223f1df3fbbea919844e3f7c554a43dd43a5bb704786be79fc476f91d3f3f89b03984d8068dcf1bb7dfc6637b45450ac04",
14+
"valid": true
15+
},
16+
{
17+
"address": "00df7de50e110ead8cb83451a503dbe03cc5edb2eced7e35212ae10af28d38c000",
18+
"message": "aebf3f2601a0c8c5d39cc7d8911642f740b78168218da8471772b35f9d35b9ab",
19+
"pub_key": "f7badec5b8abeaf699583992219b7b223f1df3fbbea919844e3f7c554a43dd43",
20+
"signature": "c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac03fa8c4bd45aecaca5b24fb97bc10ac27ac8751a7dfe1baff8b953ec9f5833ca260e",
21+
"valid": true
22+
},
23+
{
24+
"address": "001482f60b75cc1a6b1fe9810b64b95a978c35ed73ff371cb99f6f1fd0479d7b21",
25+
"message": "9bd9f44f4dcc75bd531b56b2cd280b0bb38fc1cd6d1230e14861d861de092e79",
26+
"pub_key": "cdb267ce40c5cd45306fa5d2f29731459387dbf9eb933b7bd5aed9a765b88d4d",
27+
"signature": "9046a64750444938de19f227bb80485e92b83fdb4b6506c160484c016cc1852f87909e14428a7a1d62e9f22f3d3ad7802db02eb2e688b6c52fcd6648a98bd009",
28+
"valid": true
29+
},
30+
{
31+
"address": "001482f60b75cc1a6b1fe9810b64b95a978c35ed73ff371cb99f6f1fd0479d7b21",
32+
"message": "e47d62c63f830dc7a6851a0b1f33ae4bb2f507fb6cffec4011eaccd55b53f56c",
33+
"pub_key": "cdb267ce40c5cd45306fa5d2f29731459387dbf9eb933b7bd5aed9a765b88d4d",
34+
"signature": "160a1cb0dc9c0258cd0a7d23e94d8fa878bcb1925f2c64246b2dee1796bed5125ec6bc982a269b723e0668e540911a9a6a58921d6925e434ab10aa7940551a09",
35+
"valid": true
36+
},
37+
{
38+
"address": "001482f60b75cc1a6b1fe9810b64b95a978c35ed73ff371cb99f6f1fd0479d7b21",
39+
"message": "e47d62c63f830dc7a6851a0b1f33ae4bb2f507fb6cffec4011eaccd55b53f56c",
40+
"pub_key": "cdb267ce40c5cd45306fa5d2f29731459387dbf9eb933b7bd5aed9a765b88d4d",
41+
"signature": "21122a84e0b5fca4052f5b1235c80a537878b38f3142356b2c2384ebad4668b7e40bc836dac0f71076f9abe3a53f9c03c1ceeeddb658d0030494ace586687405",
42+
"valid": true
43+
},
44+
{
45+
"address": "002aeb2a345ceecd740ffa7cd891ac0116dd10fe169079d158e492043ec49b834d",
46+
"message": "85e241a07d148b41e47d62c63f830dc7a6851a0b1f33ae4bb2f507fb6cffec40",
47+
"pub_key": "442aad9f089ad9e14647b1ef9099a1ff4798d78589e66f28eca69c11f582a623",
48+
"signature": "e96f66be976d82e60150baecff9906684aebb1ef181f67a7189ac78ea23b6c0e547f7690a0e2ddcd04d87dbc3490dc19b3b3052f7ff0538cb68afb369ba3a514",
49+
"valid": false
50+
},
51+
{
52+
"address": "002aeb2a345ceecd740ffa7cd891ac0116dd10fe169079d158e492043ec49b834d",
53+
"message": "85e241a07d148b41e47d62c63f830dc7a6851a0b1f33ae4bb2f507fb6cffec40",
54+
"pub_key": "442aad9f089ad9e14647b1ef9099a1ff4798d78589e66f28eca69c11f582a623",
55+
"signature": "8ce5b96c8f26d0ab6c47958c9e68b937104cd36e13c33566acd2fe8d38aa19427e71f98a473474f2f13f06f97c20d58cc3f54b8bd0d272f42b695dd7e89a8c22",
56+
"valid": false
57+
},
58+
{
59+
"address": "00df7de50e110ead8cb83451a503dbe03cc5edb2eced7e35212ae10af28d38c000",
60+
"message": "9bedc267423725d473888631ebf45988bad3db83851ee85c85e241a07d148b41",
61+
"pub_key": "f7badec5b8abeaf699583992219b7b223f1df3fbbea919844e3f7c554a43dd43",
62+
"signature": "ecffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff03be9678ac102edcd92b0210bb34d7428d12ffc5df5f37e359941266a4e35f0f",
63+
"valid": false
64+
},
65+
{
66+
"address": "00df7de50e110ead8cb83451a503dbe03cc5edb2eced7e35212ae10af28d38c000",
67+
"message": "9bedc267423725d473888631ebf45988bad3db83851ee85c85e241a07d148b41",
68+
"pub_key": "f7badec5b8abeaf699583992219b7b223f1df3fbbea919844e3f7c554a43dd43",
69+
"signature": "ecffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffca8c5b64cd208982aa38d4936621a4775aa233aa0505711d8fdcfdaa943d4908",
70+
"valid": true
71+
},
72+
{
73+
"address": "00f25275baf4e267a1ec213dfd8283e9c97bf8c3494752c1c14b4bd5e5f9bfa9d8",
74+
"message": "e96b7021eb39c1a163b6da4e3093dcd3f21387da4cc4572be588fafae23c155b",
75+
"pub_key": "ecffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
76+
"signature": "a9d55260f765261eb9b84e106f665e00b867287a761990d7135963ee0a7d59dca5bb704786be79fc476f91d3f3f89b03984d8068dcf1bb7dfc6637b45450ac04",
77+
"valid": false
78+
},
79+
{
80+
"address": "003e7cca5d2979e71caa7325ce147026402da2aec6f95586be89e24f84bfb7f8fc",
81+
"message": "39a591f5321bbe07fd5a23dc2f39d025d74526615746727ceefd6e82ae65c06f",
82+
"pub_key": "ecffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
83+
"signature": "a9d55260f765261eb9b84e106f665e00b867287a761990d7135963ee0a7d59dca5bb704786be79fc476f91d3f3f89b03984d8068dcf1bb7dfc6637b45450ac04",
84+
"valid": true
85+
}
86+
]

tpkg/hex_bytes.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package tpkg
2+
3+
import (
4+
"encoding/hex"
5+
)
6+
7+
// HexBytes is a slice of bytes that marshals/unmarshals as a string in hexadecimal encoding.
8+
// It is a simple utility to parse hex encoded test vectors.
9+
type HexBytes []byte
10+
11+
// MarshalText implements the encoding.TextMarshaler interface.
12+
func (b HexBytes) MarshalText() ([]byte, error) {
13+
return []byte(b.String()), nil
14+
}
15+
16+
// UnmarshalText implements the encoding.TextUnmarshaler interface.
17+
func (b *HexBytes) UnmarshalText(text []byte) (err error) {
18+
dec := make([]byte, hex.DecodedLen(len(text)))
19+
if _, err = hex.Decode(dec, text); err != nil {
20+
return err
21+
}
22+
*b = dec
23+
return
24+
}
25+
26+
// String returns the hex encoding of b.
27+
func (b HexBytes) String() string {
28+
return hex.EncodeToString(b)
29+
}

0 commit comments

Comments
 (0)