-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsign.test.ts
165 lines (133 loc) · 5.54 KB
/
sign.test.ts
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
163
164
165
import dcrypto from "../src";
import nacl from "tweetnacl";
import {
crypto_sign_ed25519_PUBLICKEYBYTES,
crypto_sign_ed25519_SECRETKEYBYTES,
crypto_sign_ed25519_SEEDBYTES,
} from "../src/utils/interfaces";
const arraysAreEqual = (arr1: Uint8Array, arr2: Uint8Array): boolean => {
const len = arr1.length;
if (len !== arr2.length) return false;
for (let i = 0; i < len; i++) {
if (arr1[i] !== arr2[i]) return false;
}
return true;
};
describe("Signing and verifying with Ed25519 keys test suite.", () => {
test("Generating a new keypair works.", async () => {
const keypair = await dcrypto.keyPair();
const wasmMemory = dcrypto.loadWasmMemory.newKeyPair();
const module = await dcrypto.loadWasmModule({ wasmMemory });
const someOtherKeypair = await dcrypto.keyPair(module);
expect(typeof keypair === "object").toBe(true);
expect(keypair.secretKey.length).toBe(crypto_sign_ed25519_SECRETKEYBYTES);
expect(keypair.publicKey.length).toBe(crypto_sign_ed25519_PUBLICKEYBYTES);
expect(arraysAreEqual(keypair.secretKey, someOtherKeypair.secretKey)).toBe(
false,
);
});
test("Generating a new keypair from a random seed works.", async () => {
const seed = await dcrypto.randomBytes(crypto_sign_ed25519_SEEDBYTES);
const keypair = await dcrypto.keyPairFromSeed(seed);
const wasmMemory = dcrypto.loadWasmMemory.keyPairFromSeed();
const module = await dcrypto.loadWasmModule({ wasmMemory });
const sameKeypair = await dcrypto.keyPairFromSeed(seed, module);
expect(typeof keypair === "object").toBe(true);
expect(keypair.secretKey.length).toBe(crypto_sign_ed25519_SECRETKEYBYTES);
expect(keypair.publicKey.length).toBe(crypto_sign_ed25519_PUBLICKEYBYTES);
expect(arraysAreEqual(sameKeypair.secretKey, keypair.secretKey)).toBe(true);
});
test("Generating a new keypair from a secret key works.", async () => {
const original = await dcrypto.keyPair();
const keypair = await dcrypto.keyPairFromSecretKey(original.secretKey);
const wasmMemory = dcrypto.loadWasmMemory.keyPairFromSecretKey();
const module = await dcrypto.loadWasmModule({ wasmMemory });
const sameKeypair = await dcrypto.keyPairFromSecretKey(
original.secretKey,
module,
);
expect(typeof keypair === "object").toBe(true);
expect(keypair.secretKey.length).toBe(crypto_sign_ed25519_SECRETKEYBYTES);
expect(keypair.publicKey.length).toBe(crypto_sign_ed25519_PUBLICKEYBYTES);
expect(arraysAreEqual(original.publicKey, keypair.publicKey)).toBe(true);
expect(arraysAreEqual(sameKeypair.secretKey, original.secretKey)).toBe(
true,
);
});
test("Signing a Uint8Array message works.", async () => {
const keyPair = await dcrypto.keyPair();
const randomMessage = await dcrypto.randomBytes(256);
const signature = await dcrypto.sign(randomMessage, keyPair.secretKey);
const wasmMemory = dcrypto.loadWasmMemory.sign(randomMessage.length);
const module = await dcrypto.loadWasmModule({ wasmMemory });
const otherSignature = await dcrypto.sign(
randomMessage,
keyPair.secretKey,
module,
);
expect(signature !== null).toBe(true);
expect(signature.length).toBe(64);
expect(arraysAreEqual(signature, otherSignature)).toBe(true);
});
test("Signing a Uint8Array message can be verified by tweetnacl.", async () => {
const keyPair = await dcrypto.keyPair();
const randomMessage = await dcrypto.randomBytes(256);
const signature = await dcrypto.sign(randomMessage, keyPair.secretKey);
const naclVerification = nacl.sign.detached.verify(
randomMessage,
signature,
keyPair.publicKey,
);
expect(naclVerification).toBe(true);
});
test("Verifying the signature of a Uint8Array message works.", async () => {
const mnemonic = await dcrypto.generateMnemonic();
const keypair = await dcrypto.keyPairFromMnemonic(mnemonic);
const randomMessage = await dcrypto.randomBytes(256);
const signature = await dcrypto.sign(randomMessage, keypair.secretKey);
const verification = await dcrypto.verify(
randomMessage,
signature,
keypair.publicKey,
);
const wasmMemory = dcrypto.loadWasmMemory.verify(randomMessage.length);
const module = await dcrypto.loadWasmModule({ wasmMemory });
const otherVerification = await dcrypto.verify(
randomMessage,
signature,
keypair.publicKey,
module,
);
expect(verification).toBe(true);
expect(otherVerification).toBe(true);
});
test("Verifying the signature of a Uint8Array message from tweetnacl works.", async () => {
const keyPair = await dcrypto.keyPair();
const randomMessage = await dcrypto.randomBytes(256);
const signature = nacl.sign.detached(randomMessage, keyPair.secretKey);
const naclVerification = nacl.sign.detached.verify(
randomMessage,
signature,
keyPair.publicKey,
);
const verification = await dcrypto.verify(
randomMessage,
signature,
keyPair.publicKey,
);
expect(naclVerification).toBe(true);
expect(verification).toBe(true);
});
test("Verifying signature with wrong key should return false.", async () => {
const rightKeyPair = await dcrypto.keyPair();
const wrongKeyPair = await dcrypto.keyPair();
const randomMessage = await dcrypto.randomBytes(10240);
const signature = await dcrypto.sign(randomMessage, rightKeyPair.secretKey);
const verification = await dcrypto.verify(
randomMessage,
signature,
wrongKeyPair.publicKey,
);
expect(verification).toBe(false);
});
});