77 PrivateKey ,
88 PrivateKeyVariants ,
99 Serializer ,
10- SlhDsaSha2128sKeyPair ,
1110 SlhDsaSha2128sPrivateKey ,
1211 SlhDsaSha2128sPublicKey ,
1312 SlhDsaSha2128sSignature ,
@@ -16,9 +15,9 @@ import {
1615/* eslint-disable max-len */
1716describe ( "SlhDsaSha2128sPublicKey" , ( ) => {
1817 it ( "should create the instance correctly without error" , ( ) => {
19- // Generate a key pair to get a valid public key
20- const keyPair = SlhDsaSha2128sKeyPair . generate ( ) ;
21- const publicKeyBytes = keyPair . publicKey . toUint8Array ( ) ;
18+ // Generate a private key to get a valid public key
19+ const privateKey = SlhDsaSha2128sPrivateKey . generate ( ) ;
20+ const publicKeyBytes = privateKey . publicKey ( ) . toUint8Array ( ) ;
2221
2322 // Create from Uint8Array
2423 const publicKey = new SlhDsaSha2128sPublicKey ( publicKeyBytes ) ;
@@ -40,10 +39,10 @@ describe("SlhDsaSha2128sPublicKey", () => {
4039 } ) ;
4140
4241 it ( "should verify the signature correctly" , ( ) => {
43- const keyPair = SlhDsaSha2128sKeyPair . generate ( ) ;
44- const pubKey = keyPair . publicKey ;
42+ const privateKey = SlhDsaSha2128sPrivateKey . generate ( ) ;
43+ const pubKey = privateKey . publicKey ( ) ;
4544 const message = new TextEncoder ( ) . encode ( "hello aptos" ) ;
46- const signature = keyPair . privateKey . sign ( message ) ;
45+ const signature = privateKey . sign ( message ) ;
4746
4847 // Verify with correct signed message
4948 expect ( pubKey . verifySignature ( { message, signature } ) ) . toBe ( true ) ;
@@ -54,8 +53,8 @@ describe("SlhDsaSha2128sPublicKey", () => {
5453 } ) ;
5554
5655 it ( "should serialize correctly" , ( ) => {
57- const keyPair = SlhDsaSha2128sKeyPair . generate ( ) ;
58- const publicKey = keyPair . publicKey ;
56+ const privateKey = SlhDsaSha2128sPrivateKey . generate ( ) ;
57+ const publicKey = privateKey . publicKey ( ) ;
5958 const serializer = new Serializer ( ) ;
6059 publicKey . serialize ( serializer ) ;
6160
@@ -64,8 +63,8 @@ describe("SlhDsaSha2128sPublicKey", () => {
6463 } ) ;
6564
6665 it ( "should deserialize correctly" , ( ) => {
67- const keyPair = SlhDsaSha2128sKeyPair . generate ( ) ;
68- const originalPublicKey = keyPair . publicKey ;
66+ const privateKey = SlhDsaSha2128sPrivateKey . generate ( ) ;
67+ const originalPublicKey = privateKey . publicKey ( ) ;
6968 const serializer = new Serializer ( ) ;
7069 originalPublicKey . serialize ( serializer ) ;
7170
@@ -76,8 +75,8 @@ describe("SlhDsaSha2128sPublicKey", () => {
7675 } ) ;
7776
7877 it ( "should serialize and deserialize correctly" , ( ) => {
79- const keyPair = SlhDsaSha2128sKeyPair . generate ( ) ;
80- const publicKey = keyPair . publicKey ;
78+ const privateKey = SlhDsaSha2128sPrivateKey . generate ( ) ;
79+ const publicKey = privateKey . publicKey ( ) ;
8180 const serializer = new Serializer ( ) ;
8281 publicKey . serialize ( serializer ) ;
8382
@@ -90,31 +89,29 @@ describe("SlhDsaSha2128sPublicKey", () => {
9089
9190describe ( "SlhDsaSha2128sPrivateKey" , ( ) => {
9291 it ( "should create the instance correctly without error with AIP-80 compliant private key" , ( ) => {
93- const keyPair = SlhDsaSha2128sKeyPair . generate ( ) ;
94- const privateKey = keyPair . privateKey ;
92+ const privateKey = SlhDsaSha2128sPrivateKey . generate ( ) ;
9593 expect ( privateKey ) . toBeInstanceOf ( SlhDsaSha2128sPrivateKey ) ;
9694 expect ( privateKey . toString ( ) ) . toContain ( "slh-dsa-sha2-128s-priv-" ) ;
9795 } ) ;
9896
9997 it ( "should create the instance correctly without error with non-AIP-80 compliant private key" , ( ) => {
100- const keyPair = SlhDsaSha2128sKeyPair . generate ( ) ;
101- const privateKeyHex = keyPair . privateKey . toHexString ( ) ;
98+ const originalPrivateKey = SlhDsaSha2128sPrivateKey . generate ( ) ;
99+ const privateKeyHex = originalPrivateKey . toHexString ( ) ;
102100 const privateKey = new SlhDsaSha2128sPrivateKey ( privateKeyHex , false ) ;
103101 expect ( privateKey ) . toBeInstanceOf ( SlhDsaSha2128sPrivateKey ) ;
104102 expect ( privateKey . toHexString ( ) ) . toEqual ( privateKeyHex ) ;
105103 } ) ;
106104
107105 it ( "should create the instance correctly without error with Uint8Array private key" , ( ) => {
108- const keyPair = SlhDsaSha2128sKeyPair . generate ( ) ;
109- const privateKeyBytes = keyPair . privateKey . toUint8Array ( ) ;
106+ const originalPrivateKey = SlhDsaSha2128sPrivateKey . generate ( ) ;
107+ const privateKeyBytes = originalPrivateKey . toUint8Array ( ) ;
110108 const privateKey = new SlhDsaSha2128sPrivateKey ( privateKeyBytes , false ) ;
111109 expect ( privateKey ) . toBeInstanceOf ( SlhDsaSha2128sPrivateKey ) ;
112110 expect ( privateKey . toUint8Array ( ) ) . toEqual ( privateKeyBytes ) ;
113111 } ) ;
114112
115113 it ( "should print in AIP-80 format" , ( ) => {
116- const keyPair = SlhDsaSha2128sKeyPair . generate ( ) ;
117- const privateKey = keyPair . privateKey ;
114+ const privateKey = SlhDsaSha2128sPrivateKey . generate ( ) ;
118115 expect ( privateKey . toString ( ) ) . toContain ( "slh-dsa-sha2-128s-priv-" ) ;
119116 } ) ;
120117
@@ -126,22 +123,21 @@ describe("SlhDsaSha2128sPrivateKey", () => {
126123 } ) ;
127124
128125 it ( "should sign the message correctly" , ( ) => {
129- const keyPair = SlhDsaSha2128sKeyPair . generate ( ) ;
130- const privateKey = keyPair . privateKey ;
126+ const privateKey = SlhDsaSha2128sPrivateKey . generate ( ) ;
131127 const message = new TextEncoder ( ) . encode ( "hello aptos" ) ;
132128 const signature = privateKey . sign ( message ) ;
133129
134130 expect ( signature ) . toBeInstanceOf ( SlhDsaSha2128sSignature ) ;
135131 expect ( signature . toUint8Array ( ) . length ) . toBe ( SlhDsaSha2128sSignature . LENGTH ) ;
136132
137133 // Verify the signature
138- const isValid = keyPair . publicKey . verifySignature ( { message, signature } ) ;
134+ const publicKey = privateKey . publicKey ( ) ;
135+ const isValid = publicKey . verifySignature ( { message, signature } ) ;
139136 expect ( isValid ) . toBe ( true ) ;
140137 } ) ;
141138
142139 it ( "should serialize correctly" , ( ) => {
143- const keyPair = SlhDsaSha2128sKeyPair . generate ( ) ;
144- const privateKey = keyPair . privateKey ;
140+ const privateKey = SlhDsaSha2128sPrivateKey . generate ( ) ;
145141 const serializer = new Serializer ( ) ;
146142 privateKey . serialize ( serializer ) ;
147143
@@ -150,8 +146,7 @@ describe("SlhDsaSha2128sPrivateKey", () => {
150146 } ) ;
151147
152148 it ( "should deserialize correctly" , ( ) => {
153- const keyPair = SlhDsaSha2128sKeyPair . generate ( ) ;
154- const originalPrivateKey = keyPair . privateKey ;
149+ const originalPrivateKey = SlhDsaSha2128sPrivateKey . generate ( ) ;
155150 const serializer = new Serializer ( ) ;
156151 originalPrivateKey . serialize ( serializer ) ;
157152
@@ -162,8 +157,7 @@ describe("SlhDsaSha2128sPrivateKey", () => {
162157 } ) ;
163158
164159 it ( "should serialize and deserialize correctly" , ( ) => {
165- const keyPair = SlhDsaSha2128sKeyPair . generate ( ) ;
166- const privateKey = keyPair . privateKey ;
160+ const privateKey = SlhDsaSha2128sPrivateKey . generate ( ) ;
167161 const serializer = new Serializer ( ) ;
168162 privateKey . serialize ( serializer ) ;
169163
@@ -174,30 +168,29 @@ describe("SlhDsaSha2128sPrivateKey", () => {
174168 } ) ;
175169
176170 it ( "should derive same public key from randomly-generated private key or from-bytes private key" , ( ) => {
177- const keyPair = SlhDsaSha2128sKeyPair . generate ( ) ;
178- const privateKeyBytes = keyPair . privateKey . toUint8Array ( ) ;
171+ const originalPrivateKey = SlhDsaSha2128sPrivateKey . generate ( ) ;
172+ const privateKeyBytes = originalPrivateKey . toUint8Array ( ) ;
179173 // Create a new private key instance
180174 const privateKey = new SlhDsaSha2128sPrivateKey ( privateKeyBytes , false ) ;
181175 // Public key should be derivable
182176 const derivedPublicKey = privateKey . publicKey ( ) ;
183177 expect ( derivedPublicKey ) . toBeInstanceOf ( SlhDsaSha2128sPublicKey ) ;
184- expect ( derivedPublicKey . toUint8Array ( ) ) . toEqual ( keyPair . publicKey . toUint8Array ( ) ) ;
178+ expect ( derivedPublicKey . toUint8Array ( ) ) . toEqual ( originalPrivateKey . publicKey ( ) . toUint8Array ( ) ) ;
185179 } ) ;
186180
187181 it ( "should get public key from private key" , ( ) => {
188- const keyPair = SlhDsaSha2128sKeyPair . generate ( ) ;
189- const privateKey = keyPair . privateKey ;
182+ const privateKey = SlhDsaSha2128sPrivateKey . generate ( ) ;
190183 const publicKey = privateKey . publicKey ( ) ;
191184 expect ( publicKey ) . toBeInstanceOf ( SlhDsaSha2128sPublicKey ) ;
192- expect ( publicKey . toUint8Array ( ) ) . toEqual ( keyPair . publicKey . toUint8Array ( ) ) ;
185+ expect ( publicKey . toUint8Array ( ) . length ) . toBe ( SlhDsaSha2128sPublicKey . LENGTH ) ;
193186 } ) ;
194187} ) ;
195188
196189describe ( "SlhDsaSha2128sSignature" , ( ) => {
197190 it ( "should create an instance correctly without error" , ( ) => {
198- const keyPair = SlhDsaSha2128sKeyPair . generate ( ) ;
191+ const privateKey = SlhDsaSha2128sPrivateKey . generate ( ) ;
199192 const message = new TextEncoder ( ) . encode ( "hello aptos" ) ;
200- const signature = keyPair . privateKey . sign ( message ) ;
193+ const signature = privateKey . sign ( message ) ;
201194
202195 expect ( signature ) . toBeInstanceOf ( SlhDsaSha2128sSignature ) ;
203196 expect ( signature . toUint8Array ( ) . length ) . toBe ( SlhDsaSha2128sSignature . LENGTH ) ;
@@ -211,9 +204,9 @@ describe("SlhDsaSha2128sSignature", () => {
211204 } ) ;
212205
213206 it ( "should serialize correctly" , ( ) => {
214- const keyPair = SlhDsaSha2128sKeyPair . generate ( ) ;
207+ const privateKey = SlhDsaSha2128sPrivateKey . generate ( ) ;
215208 const message = new TextEncoder ( ) . encode ( "hello aptos" ) ;
216- const signature = keyPair . privateKey . sign ( message ) ;
209+ const signature = privateKey . sign ( message ) ;
217210 const serializer = new Serializer ( ) ;
218211 signature . serialize ( serializer ) ;
219212
@@ -222,9 +215,9 @@ describe("SlhDsaSha2128sSignature", () => {
222215 } ) ;
223216
224217 it ( "should deserialize correctly" , ( ) => {
225- const keyPair = SlhDsaSha2128sKeyPair . generate ( ) ;
218+ const privateKey = SlhDsaSha2128sPrivateKey . generate ( ) ;
226219 const message = new TextEncoder ( ) . encode ( "hello aptos" ) ;
227- const originalSignature = keyPair . privateKey . sign ( message ) ;
220+ const originalSignature = privateKey . sign ( message ) ;
228221 const serializer = new Serializer ( ) ;
229222 originalSignature . serialize ( serializer ) ;
230223
@@ -234,37 +227,3 @@ describe("SlhDsaSha2128sSignature", () => {
234227 expect ( deserializedSignature . toUint8Array ( ) ) . toEqual ( originalSignature . toUint8Array ( ) ) ;
235228 } ) ;
236229} ) ;
237-
238- describe ( "SlhDsaSha2128sKeyPair" , ( ) => {
239- it ( "should generate a valid key pair" , ( ) => {
240- const keyPair = SlhDsaSha2128sKeyPair . generate ( ) ;
241- expect ( keyPair . privateKey ) . toBeInstanceOf ( SlhDsaSha2128sPrivateKey ) ;
242- expect ( keyPair . publicKey ) . toBeInstanceOf ( SlhDsaSha2128sPublicKey ) ;
243- expect ( keyPair . privateKey . toUint8Array ( ) . length ) . toBe ( SlhDsaSha2128sPrivateKey . LENGTH ) ;
244- expect ( keyPair . publicKey . toUint8Array ( ) . length ) . toBe ( SlhDsaSha2128sPublicKey . LENGTH ) ;
245- } ) ;
246-
247- it ( "should allow signing and verification" , ( ) => {
248- const keyPair = SlhDsaSha2128sKeyPair . generate ( ) ;
249- const message = new TextEncoder ( ) . encode ( "test message" ) ;
250- const signature = keyPair . privateKey . sign ( message ) ;
251- const isValid = keyPair . publicKey . verifySignature ( { message, signature } ) ;
252- expect ( isValid ) . toBe ( true ) ;
253- } ) ;
254-
255- it ( "should create key pair from existing keys" , ( ) => {
256- const originalKeyPair = SlhDsaSha2128sKeyPair . generate ( ) ;
257- const privateKeyBytes = originalKeyPair . privateKey . toUint8Array ( ) ;
258- const publicKeyBytes = originalKeyPair . publicKey . toUint8Array ( ) ;
259-
260- const privateKey = new SlhDsaSha2128sPrivateKey ( privateKeyBytes , false ) ;
261- const publicKey = new SlhDsaSha2128sPublicKey ( publicKeyBytes ) ;
262- const keyPair = new SlhDsaSha2128sKeyPair ( privateKey , publicKey ) ;
263-
264- expect ( keyPair . privateKey ) . toBe ( privateKey ) ;
265- expect ( keyPair . publicKey ) . toBe ( publicKey ) ;
266- // Verify the derived public key matches
267- const derivedPublicKey = privateKey . publicKey ( ) ;
268- expect ( derivedPublicKey . toUint8Array ( ) ) . toEqual ( publicKey . toUint8Array ( ) ) ;
269- } ) ;
270- } ) ;
0 commit comments