From 13d589613db9e8e48b7197cf89f4a4652e031fa3 Mon Sep 17 00:00:00 2001 From: Ajitomi Daisuke Date: Thu, 29 Aug 2024 22:25:05 +0900 Subject: [PATCH] Refine dhkem-x25519 test for bun. --- .github/workflows/ci_bun.yml | 10 +- x/dhkem-x25519/test/runtimes/bun/.gitignore | 175 ++++++++++++++++++ x/dhkem-x25519/test/runtimes/bun/.npmrc | 1 + x/dhkem-x25519/test/runtimes/bun/README.md | 16 ++ x/dhkem-x25519/test/runtimes/bun/bun.lockb | Bin 0 -> 5096 bytes x/dhkem-x25519/test/runtimes/bun/package.json | 15 ++ x/dhkem-x25519/test/runtimes/bun/src/index.js | 8 - x/dhkem-x25519/test/runtimes/bun/src/index.ts | 8 + .../test/runtimes/bun/src/server.ts | 84 +++++++++ 9 files changed, 305 insertions(+), 12 deletions(-) create mode 100644 x/dhkem-x25519/test/runtimes/bun/.gitignore create mode 100644 x/dhkem-x25519/test/runtimes/bun/.npmrc create mode 100644 x/dhkem-x25519/test/runtimes/bun/README.md create mode 100755 x/dhkem-x25519/test/runtimes/bun/bun.lockb create mode 100644 x/dhkem-x25519/test/runtimes/bun/package.json delete mode 100644 x/dhkem-x25519/test/runtimes/bun/src/index.js create mode 100644 x/dhkem-x25519/test/runtimes/bun/src/index.ts create mode 100644 x/dhkem-x25519/test/runtimes/bun/src/server.ts diff --git a/.github/workflows/ci_bun.yml b/.github/workflows/ci_bun.yml index 0fcc2011d..f426d779c 100644 --- a/.github/workflows/ci_bun.yml +++ b/.github/workflows/ci_bun.yml @@ -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 @@ -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: | diff --git a/x/dhkem-x25519/test/runtimes/bun/.gitignore b/x/dhkem-x25519/test/runtimes/bun/.gitignore new file mode 100644 index 000000000..9b1ee42e8 --- /dev/null +++ b/x/dhkem-x25519/test/runtimes/bun/.gitignore @@ -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 diff --git a/x/dhkem-x25519/test/runtimes/bun/.npmrc b/x/dhkem-x25519/test/runtimes/bun/.npmrc new file mode 100644 index 000000000..41583e36c --- /dev/null +++ b/x/dhkem-x25519/test/runtimes/bun/.npmrc @@ -0,0 +1 @@ +@jsr:registry=https://npm.jsr.io diff --git a/x/dhkem-x25519/test/runtimes/bun/README.md b/x/dhkem-x25519/test/runtimes/bun/README.md new file mode 100644 index 000000000..10da54492 --- /dev/null +++ b/x/dhkem-x25519/test/runtimes/bun/README.md @@ -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. diff --git a/x/dhkem-x25519/test/runtimes/bun/bun.lockb b/x/dhkem-x25519/test/runtimes/bun/bun.lockb new file mode 100755 index 0000000000000000000000000000000000000000..671bc72e9530d859d4d17f83a22411ddfaccd02b GIT binary patch literal 5096 zcmds53se+k6rNqz7iok*dNON$WC8BX?zT&aD`1M94km&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wd$E5+Dset6si44jRq@5J#8@3L8KZaRU53Bfs8g? z2jdTZVT+ZcU);V6%F`;asc9@vho_4yZ@1c}iacje4AC0?+MuATHb@lWB-ZLypLKlE zQXed^an7^x4a>zL^R|eB!1BO2t5&-(Iw9gp7#tEf!r3`N=KZSl+uQcYUQCfIYBh4HTV^`4j zxFYVVkar%78*Nl)Znhr;u!aArgBcZs4&p#s>;&DeBZDP { + return await testServer(request); + }, +}; diff --git a/x/dhkem-x25519/test/runtimes/bun/src/server.ts b/x/dhkem-x25519/test/runtimes/bun/src/server.ts new file mode 100644 index 000000000..9b6134f62 --- /dev/null +++ b/x/dhkem-x25519/test/runtimes/bun/src/server.ts @@ -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"); +}