Skip to content

Commit f1896d2

Browse files
committed
Refine hybridkem-x25519-kyber768 test for bun.
1 parent 0fc7385 commit f1896d2

File tree

9 files changed

+309
-14
lines changed

9 files changed

+309
-14
lines changed

.github/workflows/ci_bun.yml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ jobs:
3232
run: npm install -g esbuild
3333
- name: Prepare test for ./x/core
3434
working-directory: ./x/core
35-
run: |
36-
deno run -A dnt.ts
37-
deno task minify > ../hybridkem-x25519-kyber768/test/runtimes/hpke-core.js
35+
run: deno run -A dnt.ts
3836
- name: Run npm pack for ./x/core
3937
working-directory: ./npm-packages/x/core
4038
run: npm pack
@@ -97,20 +95,24 @@ jobs:
9795
- name: Run test for ./x/dhkem-secp256k1
9896
working-directory: ./x/dhkem-secp256k1/test/runtimes/bun
9997
run: |
98+
bun install
10099
nohup bun src/index.ts &
101100
sleep 3
102101
deno test dhkem-secp256k1.spec.ts --allow-net --config ../../../deno.json
103102
- name: Prepare test for ./x/hybridkem-x25519-kyber768
104103
working-directory: ./x/hybridkem-x25519-kyber768
105104
run: |
106105
deno task dnt
107-
deno task minify > test/runtimes/hpke-hybridkem-x25519-kyber768.js
106+
- name: Run npm pack for ./x/hybridkem-x25519-kyber768
107+
working-directory: ./npm-packages/x/hybridkem-x25519-kyber768
108+
run: npm pack
108109
- name: Run test for ./x/hybridkem-x25519-kyber768
109110
working-directory: ./x/hybridkem-x25519-kyber768/test/runtimes/bun
110111
run: |
111-
nohup bun src/index.js &
112+
bun install
113+
nohup bun src/index.ts &
112114
sleep 3
113-
deno test hybridkem-x25519-kyber768.spec.ts --allow-net
115+
deno test hybridkem-x25519-kyber768.spec.ts --allow-net --config ../../../deno.json
114116
- name: Prepare test for ./x/hpke-js
115117
working-directory: ./x/hpke-js
116118
run: |
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
2+
3+
# Logs
4+
5+
logs
6+
_.log
7+
npm-debug.log_
8+
yarn-debug.log*
9+
yarn-error.log*
10+
lerna-debug.log*
11+
.pnpm-debug.log*
12+
13+
# Caches
14+
15+
.cache
16+
17+
# Diagnostic reports (https://nodejs.org/api/report.html)
18+
19+
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
20+
21+
# Runtime data
22+
23+
pids
24+
_.pid
25+
_.seed
26+
*.pid.lock
27+
28+
# Directory for instrumented libs generated by jscoverage/JSCover
29+
30+
lib-cov
31+
32+
# Coverage directory used by tools like istanbul
33+
34+
coverage
35+
*.lcov
36+
37+
# nyc test coverage
38+
39+
.nyc_output
40+
41+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
42+
43+
.grunt
44+
45+
# Bower dependency directory (https://bower.io/)
46+
47+
bower_components
48+
49+
# node-waf configuration
50+
51+
.lock-wscript
52+
53+
# Compiled binary addons (https://nodejs.org/api/addons.html)
54+
55+
build/Release
56+
57+
# Dependency directories
58+
59+
node_modules/
60+
jspm_packages/
61+
62+
# Snowpack dependency directory (https://snowpack.dev/)
63+
64+
web_modules/
65+
66+
# TypeScript cache
67+
68+
*.tsbuildinfo
69+
70+
# Optional npm cache directory
71+
72+
.npm
73+
74+
# Optional eslint cache
75+
76+
.eslintcache
77+
78+
# Optional stylelint cache
79+
80+
.stylelintcache
81+
82+
# Microbundle cache
83+
84+
.rpt2_cache/
85+
.rts2_cache_cjs/
86+
.rts2_cache_es/
87+
.rts2_cache_umd/
88+
89+
# Optional REPL history
90+
91+
.node_repl_history
92+
93+
# Output of 'npm pack'
94+
95+
*.tgz
96+
97+
# Yarn Integrity file
98+
99+
.yarn-integrity
100+
101+
# dotenv environment variable files
102+
103+
.env
104+
.env.development.local
105+
.env.test.local
106+
.env.production.local
107+
.env.local
108+
109+
# parcel-bundler cache (https://parceljs.org/)
110+
111+
.parcel-cache
112+
113+
# Next.js build output
114+
115+
.next
116+
out
117+
118+
# Nuxt.js build / generate output
119+
120+
.nuxt
121+
dist
122+
123+
# Gatsby files
124+
125+
# Comment in the public line in if your project uses Gatsby and not Next.js
126+
127+
# https://nextjs.org/blog/next-9-1#public-directory-support
128+
129+
# public
130+
131+
# vuepress build output
132+
133+
.vuepress/dist
134+
135+
# vuepress v2.x temp and cache directory
136+
137+
.temp
138+
139+
# Docusaurus cache and generated files
140+
141+
.docusaurus
142+
143+
# Serverless directories
144+
145+
.serverless/
146+
147+
# FuseBox cache
148+
149+
.fusebox/
150+
151+
# DynamoDB Local files
152+
153+
.dynamodb/
154+
155+
# TernJS port file
156+
157+
.tern-port
158+
159+
# Stores VSCode versions used for testing VSCode extensions
160+
161+
.vscode-test
162+
163+
# yarn v2
164+
165+
.yarn/cache
166+
.yarn/unplugged
167+
.yarn/build-state.yml
168+
.yarn/install-state.gz
169+
.pnp.*
170+
171+
# IntelliJ based IDEs
172+
.idea
173+
174+
# Finder (MacOS) folder config
175+
.DS_Store
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@jsr:registry=https://npm.jsr.io
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# bunx
2+
3+
To install dependencies:
4+
5+
```bash
6+
bun install
7+
```
8+
9+
To run:
10+
11+
```bash
12+
bun run index.ts
13+
```
14+
15+
This project was created using `bun init` in bun v1.1.22. [Bun](https://bun.sh)
16+
is a fast all-in-one JavaScript runtime.
4.69 KB
Binary file not shown.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "test",
3+
"module": "src/index.ts",
4+
"type": "module",
5+
"devDependencies": {
6+
"@types/bun": "latest"
7+
},
8+
"peerDependencies": {
9+
"typescript": "^5.0.0"
10+
},
11+
"dependencies": {
12+
"@hpke/core": "file:../../../../../npm-packages/x/core/hpke-core-1.2.9.tgz",
13+
"@hpke/hybridkem-x25519-kyber768": "file:../../../../../npm-packages/x/hybridkem-x25519-kyber768/hpke-hybridkem-x25519-kyber768-1.2.9.tgz"
14+
}
15+
}

x/hybridkem-x25519-kyber768/test/runtimes/bun/src/index.js

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { testServer } from "./server.ts";
2+
3+
export default {
4+
port: 3006,
5+
async fetch(request: Request): Promise<Response> {
6+
return await testServer(request);
7+
},
8+
};
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import {
2+
AeadId,
3+
Aes128Gcm,
4+
Aes256Gcm,
5+
CipherSuite,
6+
ExportOnly,
7+
HkdfSha256,
8+
HkdfSha384,
9+
HkdfSha512,
10+
KdfId,
11+
} from "@hpke/core";
12+
13+
import { HybridkemX25519Kyber768 } from "@hpke/hybridkem-x25519-kyber768";
14+
15+
function createKdf(id) {
16+
switch (id) {
17+
case KdfId.HkdfSha256:
18+
return new HkdfSha256();
19+
case KdfId.HkdfSha384:
20+
return new HkdfSha384();
21+
case KdfId.HkdfSha512:
22+
return new HkdfSha512();
23+
default:
24+
break;
25+
}
26+
throw new Error("ng: invalid kdf");
27+
}
28+
29+
function createAead(id) {
30+
switch (id) {
31+
case AeadId.Aes128Gcm:
32+
return new Aes128Gcm();
33+
case AeadId.Aes256Gcm:
34+
return new Aes256Gcm();
35+
// case AeadId.Chacha20Poly1305:
36+
// return new Chacha20Poly1305();
37+
case AeadId.ExportOnly:
38+
return new ExportOnly();
39+
default:
40+
break;
41+
}
42+
throw new Error("ng: invalid aead");
43+
}
44+
45+
export async function testServer(request) {
46+
const url = new URL(request.url);
47+
if (url.pathname !== "/test") {
48+
return new Response("ng: invalid path");
49+
}
50+
const params = url.searchParams;
51+
const kdfStr = params.get("kdf");
52+
const aeadStr = params.get("aead");
53+
if (kdfStr === null || aeadStr === null) {
54+
return new Response("ng: invalid params");
55+
}
56+
const kem = new HybridkemX25519Kyber768();
57+
const kdf = Number.parseInt(kdfStr);
58+
const aead = Number.parseInt(aeadStr);
59+
if (Number.isNaN(kdf) || Number.isNaN(aead)) {
60+
return new Response("ng: invalid params");
61+
}
62+
63+
try {
64+
const suite = new CipherSuite({
65+
kem: kem,
66+
kdf: createKdf(kdf),
67+
aead: createAead(aead),
68+
});
69+
const rkp = await suite.kem.generateKeyPair();
70+
const sender = await suite.createSenderContext({
71+
recipientPublicKey: rkp.publicKey,
72+
});
73+
const recipient = await suite.createRecipientContext({
74+
recipientKey: rkp,
75+
enc: sender.enc,
76+
});
77+
const ct = await sender.seal(new TextEncoder().encode("hello world!"));
78+
const pt = await recipient.open(ct);
79+
if ("hello world!" !== new TextDecoder().decode(pt)) {
80+
return new Response("ng");
81+
}
82+
} catch (e) {
83+
return new Response("ng: " + e.message);
84+
}
85+
return new Response("ok");
86+
}

0 commit comments

Comments
 (0)