-
Notifications
You must be signed in to change notification settings - Fork 3
/
SR25519_Tests.cs
162 lines (137 loc) · 5.76 KB
/
SR25519_Tests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using sr25519_dotnet.lib;
using sr25519_dotnet.lib.Models;
namespace sr25519_dotnet.test
{
[TestClass]
public class SR25519_Tests
{
[TestMethod]
public void ShouldGenerateKeypair()
{
// Arrange.
var seed = "fac7959dbfe72f052e5a0c3c8d6530f202b02fd8f9f5ca3580ec8deb7797479e";
var expectedPublic = "46ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a";
// Act.
var derived = SR25519.GenerateKeypairFromSeed(seed);
// Assert.
Assert.AreEqual(expectedPublic, Utils.ByteArrayToHexString(derived.Public));
}
[TestMethod]
public void ShouldSignAndVerifyMessageString()
{
// Arrange.
var message1 = "positive test message";
var message2 = "negative test message";
// Act.
var keys = SR25519.GenerateKeypairFromSeed(
"f6dbe0604959f8d4f53ef58754f44391c69cfc87f1b97872abef63161e18c885");
var sig = SR25519.Sign(message1, keys);
var verification1 = SR25519.Verify(message1, sig, keys.Public);
var verification2 = SR25519.Verify(message2, sig, keys.Public);
// Assert.
Assert.IsTrue(verification1);
Assert.IsFalse(verification2);
}
[TestMethod]
public void ShouldSignAndVerifyMessageBytes()
{
// Arrange.
var message1 = "010203040506070809";
var message2 = "090807060504030201";
// Act.
var keys = SR25519.GenerateKeypairFromSeed(
"f6dbe0604959f8d4f53ef58754f44391c69cfc87f1b97872abef63161e18c885");
var sig = SR25519.Sign(Utils.HexStringToByteArray(message1), keys);
var verification1 = SR25519.Verify(Utils.HexStringToByteArray(message1), sig, keys.Public);
var verification2 = SR25519.Verify(Utils.HexStringToByteArray(message2), sig, keys.Public);
// Assert.
Assert.IsTrue(verification1);
Assert.IsFalse(verification2);
}
[TestMethod]
public void ShouldHardDeriveKeypair()
{
// Arrange.
var keys = SR25519.GenerateKeypairFromSeed(
"fac7959dbfe72f052e5a0c3c8d6530f202b02fd8f9f5ca3580ec8deb7797479e");
var cc = "14416c6963650000000000000000000000000000000000000000000000000000";
var expectedPublic = "d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d";
// Act.
var derived = SR25519.HardDeriveKeypair(keys, cc);
// Assert.
Assert.AreEqual(expectedPublic, Utils.ByteArrayToHexString(derived.Public));
}
[TestMethod]
public void ShouldSoftDeriveKeypair()
{
// Arrange.
var keys = SR25519.GenerateKeypairFromSeed(
"fac7959dbfe72f052e5a0c3c8d6530f202b02fd8f9f5ca3580ec8deb7797479e");
var cc = "0c666f6f00000000000000000000000000000000000000000000000000000000";
var expectedPublic = "40b9675df90efa6069ff623b0fdfcf706cd47ca7452a5056c7ad58194d23440a";
// Act.
var derived = SR25519.SoftDeriveKeypair(keys, cc);
// Assert.
Assert.AreEqual(expectedPublic, Utils.ByteArrayToHexString(derived.Public));
}
[TestMethod]
public void ShouldSoftDerivePublicKey()
{
// Arrange.
var publicKey = "46ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a";
var cc = "0c666f6f00000000000000000000000000000000000000000000000000000000";
var expectedPublic = "40b9675df90efa6069ff623b0fdfcf706cd47ca7452a5056c7ad58194d23440a";
// Act.
var derived = SR25519.SoftDerivePublicKey(publicKey, cc);
// Assert.
Assert.AreEqual(expectedPublic, Utils.ByteArrayToHexString(derived));
}
[TestMethod]
public void ShouldVrfSignAndVerifyMessageBytes()
{
// Arrange.
var message1 = "Hello, world!";
var message2 = "Hello, Earth!";
// Act.
var keys = SR25519.GenerateKeypairFromSeed(
"f6dbe0604959f8d4f53ef58754f44391c69cfc87f1b97872abef63161e18c885");
var threshold = Enumerable.Repeat((byte)0xFF, 16).ToArray();
VrfSignResult signResult;
var signVerification = SR25519.VrfSignIfLess(
Encoding.UTF8.GetBytes(message1),
keys,
threshold,
out signResult
);
Assert.IsTrue(signVerification);
Assert.AreEqual(Sr25519SignatureResult.Ok, signResult.Result);
Assert.IsTrue(signResult.IsLess);
VrfVerifyResult verifyResult;
var verification1 = SR25519.VrfVerify(
Encoding.UTF8.GetBytes(message1),
keys.Public,
signResult.Output,
signResult.Proof,
threshold,
out verifyResult
);
Assert.IsTrue(verification1);
Assert.AreEqual(Sr25519SignatureResult.Ok, verifyResult.Result);
Assert.IsTrue(verifyResult.IsLess);
var verification2 = SR25519.VrfVerify(
Encoding.UTF8.GetBytes(message2),
keys.Public,
signResult.Output,
signResult.Proof,
threshold,
out verifyResult
);
Assert.IsFalse(verification2);
Assert.AreEqual(Sr25519SignatureResult.EquationFalse, verifyResult.Result);
Assert.IsFalse(verifyResult.IsLess);
}
}
}