Skip to content

Commit

Permalink
Refine dhkem-x25519 test for bun.
Browse files Browse the repository at this point in the history
  • Loading branch information
dajiaji committed Aug 29, 2024
1 parent 3eda202 commit 13d5896
Show file tree
Hide file tree
Showing 9 changed files with 305 additions and 12 deletions.
10 changes: 6 additions & 4 deletions .github/workflows/ci_bun.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ jobs:
working-directory: ./x/core
run: |
deno run -A dnt.ts
deno task minify > ../dhkem-x25519/test/runtimes/hpke-core.js
deno task minify > ../dhkem-x448/test/runtimes/hpke-core.js
deno task minify > ../hybridkem-x25519-kyber768/test/runtimes/hpke-core.js
deno task minify > ../dhkem-secp256k1/test/runtimes/hpke-core.js
Expand Down Expand Up @@ -66,13 +65,16 @@ jobs:
working-directory: ./x/dhkem-x25519
run: |
deno task dnt
deno task minify > test/runtimes/hpke-dhkem-x25519.js
- name: Run npm pack for ./x/dhkem-x25519
working-directory: ./npm-packages/x/dhkem-x25519
run: npm pack
- name: Run test for ./x/dhkem-x25519
working-directory: ./x/dhkem-x25519/test/runtimes/bun
run: |
nohup bun src/index.js &
bun install
nohup bun src/index.ts &
sleep 3
deno test dhkem-x25519.spec.ts --allow-net
deno test dhkem-x25519.spec.ts --allow-net --config ../../../deno.json
- name: Prepare test for ./x/dhkem-x448
working-directory: ./x/dhkem-x448
run: |
Expand Down
175 changes: 175 additions & 0 deletions x/dhkem-x25519/test/runtimes/bun/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore

# Logs

logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Caches

.cache

# Diagnostic reports (https://nodejs.org/api/report.html)

report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# Runtime data

pids
_.pid
_.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover

lib-cov

# Coverage directory used by tools like istanbul

coverage
*.lcov

# nyc test coverage

.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)

.grunt

# Bower dependency directory (https://bower.io/)

bower_components

# node-waf configuration

.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)

build/Release

# Dependency directories

node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)

web_modules/

# TypeScript cache

*.tsbuildinfo

# Optional npm cache directory

.npm

# Optional eslint cache

.eslintcache

# Optional stylelint cache

.stylelintcache

# Microbundle cache

.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history

.node_repl_history

# Output of 'npm pack'

*.tgz

# Yarn Integrity file

.yarn-integrity

# dotenv environment variable files

.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)

.parcel-cache

# Next.js build output

.next
out

# Nuxt.js build / generate output

.nuxt
dist

# Gatsby files

# Comment in the public line in if your project uses Gatsby and not Next.js

# https://nextjs.org/blog/next-9-1#public-directory-support

# public

# vuepress build output

.vuepress/dist

# vuepress v2.x temp and cache directory

.temp

# Docusaurus cache and generated files

.docusaurus

# Serverless directories

.serverless/

# FuseBox cache

.fusebox/

# DynamoDB Local files

.dynamodb/

# TernJS port file

.tern-port

# Stores VSCode versions used for testing VSCode extensions

.vscode-test

# yarn v2

.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# IntelliJ based IDEs
.idea

# Finder (MacOS) folder config
.DS_Store
1 change: 1 addition & 0 deletions x/dhkem-x25519/test/runtimes/bun/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@jsr:registry=https://npm.jsr.io
16 changes: 16 additions & 0 deletions x/dhkem-x25519/test/runtimes/bun/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# bunx

To install dependencies:

```bash
bun install
```

To run:

```bash
bun run index.ts
```

This project was created using `bun init` in bun v1.1.22. [Bun](https://bun.sh)
is a fast all-in-one JavaScript runtime.
Binary file added x/dhkem-x25519/test/runtimes/bun/bun.lockb
Binary file not shown.
15 changes: 15 additions & 0 deletions x/dhkem-x25519/test/runtimes/bun/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "test",
"module": "src/index.ts",
"type": "module",
"devDependencies": {
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"@hpke/dhkem-x25519": "file:../../../../../npm-packages/x/dhkem-x25519/hpke-dhkem-x25519-1.2.9.tgz",
"@hpke/core": "file:../../../../../npm-packages/x/core/hpke-core-1.2.9.tgz"
}
}
8 changes: 0 additions & 8 deletions x/dhkem-x25519/test/runtimes/bun/src/index.js

This file was deleted.

8 changes: 8 additions & 0 deletions x/dhkem-x25519/test/runtimes/bun/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { testServer } from "./server.ts";

export default {
port: 3003,
async fetch(request: Request): Promise<Response> {
return await testServer(request);
},
};
84 changes: 84 additions & 0 deletions x/dhkem-x25519/test/runtimes/bun/src/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import {
AeadId,
Aes128Gcm,
Aes256Gcm,
CipherSuite,
ExportOnly,
HkdfSha256,
HkdfSha384,
HkdfSha512,
KdfId,
} from "@hpke/core";

import { DhkemX25519HkdfSha256 } from "@hpke/dhkem-x25519";

function createKdf(id) {
switch (id) {
case KdfId.HkdfSha256:
return new HkdfSha256();
case KdfId.HkdfSha384:
return new HkdfSha384();
case KdfId.HkdfSha512:
return new HkdfSha512();
default:
break;
}
throw new Error("ng: invalid kdf");
}

function createAead(id) {
switch (id) {
case AeadId.Aes128Gcm:
return new Aes128Gcm();
case AeadId.Aes256Gcm:
return new Aes256Gcm();
case AeadId.ExportOnly:
return new ExportOnly();
default:
break;
}
throw new Error("ng: invalid aead");
}

export async function testServer(request) {
const url = new URL(request.url);
if (url.pathname !== "/test") {
return new Response("ng: invalid path");
}
const params = url.searchParams;
const kdfStr = params.get("kdf");
const aeadStr = params.get("aead");
if (kdfStr === null || aeadStr === null) {
return new Response("ng: invalid params");
}
const kem = new DhkemX25519HkdfSha256();
const kdf = Number.parseInt(kdfStr);
const aead = Number.parseInt(aeadStr);
if (Number.isNaN(kdf) || Number.isNaN(aead)) {
return new Response("ng: invalid params");
}

try {
const suite = new CipherSuite({
kem: kem,
kdf: createKdf(kdf),
aead: createAead(aead),
});
const rkp = await suite.kem.generateKeyPair();
const sender = await suite.createSenderContext({
recipientPublicKey: rkp.publicKey,
});
const recipient = await suite.createRecipientContext({
recipientKey: rkp,
enc: sender.enc,
});
const ct = await sender.seal(new TextEncoder().encode("hello world!"));
const pt = await recipient.open(ct);
if ("hello world!" !== new TextDecoder().decode(pt)) {
return new Response("ng");
}
} catch (e) {
return new Response("ng: " + e.message);
}
return new Response("ok");
}

0 comments on commit 13d5896

Please sign in to comment.