Skip to content

Commit cc8555e

Browse files
committed
adds tests
1 parent ad24bbd commit cc8555e

File tree

2 files changed

+121
-88
lines changed

2 files changed

+121
-88
lines changed

.mocharc.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"extension": ["ts"],
33
"require": "ts-node/register",
44
"spec": "tests/**/*.test.ts",
5-
"timeout": 100000,
5+
"timeout": 1000000,
66
"exit": true
77
}

tests/hkdf.test.ts

+120-87
Original file line numberDiff line numberDiff line change
@@ -2,98 +2,131 @@ import { WitnessTester } from "circomkit";
22
import { circomkit } from "./common";
33

44
describe("HKDF", () => {
5-
describe("Expand", () => {
6-
let circuit: WitnessTester<["secret", "key"], ["out"]>;
7-
before(async () => {
8-
circuit = await circomkit.WitnessTester(`Expand`, {
9-
file: "hkdf",
10-
template: "Expand",
11-
params: [32, 32],
12-
});
13-
console.log("#constraints:", await circuit.getConstraintCount());
14-
});
5+
// tests are based on RFC 5869 test vectors https://www.rfc-editor.org/rfc/rfc5869.html#appendix-A
6+
const testCases = [
7+
{
8+
name: "Test Case 1",
9+
data: {
10+
IKM: "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
11+
salt: "000102030405060708090a0b0c",
12+
info: "f0f1f2f3f4f5f6f7f8f9",
13+
L: 42,
14+
PRK: "077709362c2e32df0ddc3f0dc47bba6390b6c73bb50f9c3122ec844ad7c2b3e5",
15+
OKM: "3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865",
16+
},
17+
},
18+
{
19+
name: "Test Case 2",
20+
data: {
21+
IKM: "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f",
22+
salt: "606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeaf",
23+
info: "b0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
24+
L: 82,
25+
PRK: "06a6b88c5853361a06104c9ceb35b45cef760014904671014a193f40c15fc244",
26+
OKM: "b11e398dc80327a1c8e7f78c596a49344f012eda2d4efad8a050cc4c19afa97c59045a99cac7827271cb41c65e590e09da3275600c2f09b8367793a9aca3db71cc30c58179ec3e87c14c01d5c1f3434f1d87",
27+
},
28+
},
29+
{
30+
name: "Test Case 3",
31+
data: {
32+
IKM: "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
33+
salt: "",
34+
info: "",
35+
L: 42,
36+
PRK: "19ef24a32c717b167f33a91d6f648bdf96596776afdb6377ac434c1c293ccb04",
37+
OKM: "8da4e775a563c18f715f802a063c5a31b8a11f5c5ee1879ec3454e5f3c738d2d9d201395faa4b61a96c8",
38+
},
39+
},
40+
];
1541

16-
it("should expand master key from secret", async () => {
17-
await circuit.expectPass(
18-
{
19-
secret: [
20-
0x10, 0xd9, 0xcb, 0x53, 0xd1, 0xa4, 0x05, 0xcf, 0xe2, 0x68, 0x6e, 0x08, 0x35, 0x90, 0x4d, 0x48, 0x43, 0x5e,
21-
0x80, 0x54, 0xa7, 0x9f, 0x98, 0x56, 0x83, 0xd0, 0xff, 0x72, 0x59, 0xf7, 0xa8, 0x04,
22-
],
23-
key: [
24-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
25-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
26-
],
27-
},
28-
{
29-
out: [
30-
0x8b, 0xeb, 0x33, 0x8d, 0x43, 0x1d, 0x24, 0x3c, 0xee, 0xaa, 0xa6, 0xf0, 0xcb, 0x57, 0x26, 0xfb, 0xc5, 0xa3,
31-
0x5c, 0x5e, 0x45, 0xbf, 0x99, 0x2c, 0xc3, 0xe2, 0x3b, 0x5b, 0xc2, 0xe4, 0xcc, 0xea,
32-
],
33-
}
34-
);
35-
});
36-
});
37-
describe("Extract", () => {
38-
let circuit: WitnessTester<["info", "key"], ["out"]>;
39-
before(async () => {
40-
circuit = await circomkit.WitnessTester(`Extract`, {
41-
file: "hkdf",
42-
template: "Extract",
43-
params: [0, 32, 2, 16],
42+
testCases.forEach((testCase) => {
43+
describe(testCase.name, () => {
44+
const { IKM, salt, info, L, PRK, OKM } = testCase.data;
45+
const ikmBuf = Buffer.from(IKM, "hex");
46+
const saltBuf = Buffer.from(salt, "hex");
47+
const infoBuf = Buffer.from(info, "hex");
48+
const prkBuf = Buffer.from(PRK, "hex");
49+
const okmBuf = Buffer.from(OKM, "hex");
50+
51+
const infolen = infoBuf.length;
52+
const saltlen = saltBuf.length;
53+
const ikmlen = ikmBuf.length;
54+
55+
describe("EXTRACT", () => {
56+
let circuit: WitnessTester<["salt", "key"], ["out"]>;
57+
58+
before(async () => {
59+
circuit = await circomkit.WitnessTester(`Expand`, {
60+
file: "hkdf",
61+
template: "Extract",
62+
params: [saltlen, ikmlen],
63+
});
64+
console.log(`${testCase.name} EXTRACT #constraints:`, await circuit.getConstraintCount());
65+
});
66+
67+
it("should extract pseudorandom key", async () => {
68+
await circuit.expectPass(
69+
{
70+
salt: Array.from(saltBuf),
71+
key: Array.from(ikmBuf),
72+
},
73+
{
74+
out: Array.from(prkBuf),
75+
}
76+
);
77+
});
4478
});
45-
console.log("#constraints:", await circuit.getConstraintCount());
46-
});
4779

48-
it("should extract two 16 bytes keys from key", async () => {
49-
await circuit.expectPass(
50-
{
51-
info: [],
52-
key: [
53-
0x8b, 0xeb, 0x33, 0x8d, 0x43, 0x1d, 0x24, 0x3c, 0xee, 0xaa, 0xa6, 0xf0, 0xcb, 0x57, 0x26, 0xfb, 0xc5, 0xa3,
54-
0x5c, 0x5e, 0x45, 0xbf, 0x99, 0x2c, 0xc3, 0xe2, 0x3b, 0x5b, 0xc2, 0xe4, 0xcc, 0xea,
55-
],
56-
},
57-
{
58-
out: [
59-
[0x5b, 0x02, 0xd2, 0x11, 0x3a, 0xbb, 0x74, 0x49, 0xc3, 0x7d, 0x57, 0xe0, 0xc7, 0x7a, 0x99, 0xc4],
60-
[0x43, 0x7a, 0xb4, 0xc1, 0x85, 0x2f, 0xa9, 0xcc, 0x8e, 0xc5, 0xbd, 0x64, 0x97, 0xf0, 0x31, 0x91],
61-
],
62-
}
63-
);
64-
});
65-
});
66-
describe("HKDFSha256", () => {
67-
let circuit: WitnessTester<["secret", "info", "key"], ["out"]>;
68-
before(async () => {
69-
circuit = await circomkit.WitnessTester(`HKDF`, {
70-
file: "hkdf",
71-
template: "HKDFSha256",
72-
params: [32, 0, 32, 2, 16],
80+
describe("EXPAND", () => {
81+
let circuit: WitnessTester<["info", "key"], ["out"]>;
82+
83+
before(async () => {
84+
circuit = await circomkit.WitnessTester(`HKDF`, {
85+
file: "hkdf",
86+
template: "Expand",
87+
params: [infolen, 32, 1, L],
88+
});
89+
console.log(`${testCase.name} EXPAND #constraints:`, await circuit.getConstraintCount());
90+
});
91+
92+
it("should expand from pseudorandom key", async () => {
93+
await circuit.expectPass(
94+
{
95+
info: Array.from(infoBuf),
96+
key: Array.from(prkBuf),
97+
},
98+
{
99+
out: [Array.from(okmBuf)],
100+
}
101+
);
102+
});
73103
});
74-
console.log("#constraints:", await circuit.getConstraintCount());
75-
});
76104

77-
it("should extract two 16 bytes keys from key", async () => {
78-
await circuit.expectPass(
79-
{
80-
secret: [
81-
0x10, 0xd9, 0xcb, 0x53, 0xd1, 0xa4, 0x05, 0xcf, 0xe2, 0x68, 0x6e, 0x08, 0x35, 0x90, 0x4d, 0x48, 0x43, 0x5e,
82-
0x80, 0x54, 0xa7, 0x9f, 0x98, 0x56, 0x83, 0xd0, 0xff, 0x72, 0x59, 0xf7, 0xa8, 0x04,
83-
],
84-
info: [],
85-
key: [
86-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
87-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
88-
],
89-
},
90-
{
91-
out: [
92-
[0x5b, 0x02, 0xd2, 0x11, 0x3a, 0xbb, 0x74, 0x49, 0xc3, 0x7d, 0x57, 0xe0, 0xc7, 0x7a, 0x99, 0xc4],
93-
[0x43, 0x7a, 0xb4, 0xc1, 0x85, 0x2f, 0xa9, 0xcc, 0x8e, 0xc5, 0xbd, 0x64, 0x97, 0xf0, 0x31, 0x91],
94-
],
95-
}
96-
);
105+
describe("HKDFSHA256", () => {
106+
let circuit: WitnessTester<["salt", "info", "key"], ["out"]>;
107+
108+
before(async () => {
109+
circuit = await circomkit.WitnessTester(`HKDF`, {
110+
file: "hkdf",
111+
template: "HKDFSha256",
112+
params: [saltlen, infolen, ikmlen, 1, L],
113+
});
114+
console.log(`${testCase.name} HKDFSHA256 #constraints:`, await circuit.getConstraintCount());
115+
});
116+
117+
it("should extract and expand from input key material", async () => {
118+
await circuit.expectPass(
119+
{
120+
salt: Array.from(saltBuf),
121+
info: Array.from(infoBuf),
122+
key: Array.from(ikmBuf),
123+
},
124+
{
125+
out: [Array.from(okmBuf)],
126+
}
127+
);
128+
});
129+
});
97130
});
98131
});
99132
});

0 commit comments

Comments
 (0)