Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
Tmp: commit npm folder
Browse files Browse the repository at this point in the history
  • Loading branch information
larabr committed Feb 26, 2024
1 parent 9bf9310 commit e87c731
Show file tree
Hide file tree
Showing 149 changed files with 12,148 additions and 0 deletions.
78 changes: 78 additions & 0 deletions npm/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
src/
esm/test/kyber.test.js
esm/test/kyber.test.d.ts
esm/test/utils.js
esm/test/utils.d.ts
esm/deps/deno.land/[email protected]/testing/_test_suite.js
esm/deps/deno.land/[email protected]/testing/_test_suite.d.ts
esm/deps/deno.land/[email protected]/testing/bdd.js
esm/deps/deno.land/[email protected]/testing/bdd.d.ts
esm/deps/deno.land/[email protected]/assert/_constants.js
esm/deps/deno.land/[email protected]/assert/_constants.d.ts
esm/deps/deno.land/[email protected]/assert/_diff.js
esm/deps/deno.land/[email protected]/assert/_diff.d.ts
esm/deps/deno.land/[email protected]/assert/_format.js
esm/deps/deno.land/[email protected]/assert/_format.d.ts
esm/deps/deno.land/[email protected]/assert/assert.js
esm/deps/deno.land/[email protected]/assert/assert.d.ts
esm/deps/deno.land/[email protected]/assert/assert_almost_equals.js
esm/deps/deno.land/[email protected]/assert/assert_almost_equals.d.ts
esm/deps/deno.land/[email protected]/assert/assert_array_includes.js
esm/deps/deno.land/[email protected]/assert/assert_array_includes.d.ts
esm/deps/deno.land/[email protected]/assert/assert_equals.js
esm/deps/deno.land/[email protected]/assert/assert_equals.d.ts
esm/deps/deno.land/[email protected]/assert/assert_exists.js
esm/deps/deno.land/[email protected]/assert/assert_exists.d.ts
esm/deps/deno.land/[email protected]/assert/assert_false.js
esm/deps/deno.land/[email protected]/assert/assert_false.d.ts
esm/deps/deno.land/[email protected]/assert/assert_greater.js
esm/deps/deno.land/[email protected]/assert/assert_greater.d.ts
esm/deps/deno.land/[email protected]/assert/assert_greater_or_equal.js
esm/deps/deno.land/[email protected]/assert/assert_greater_or_equal.d.ts
esm/deps/deno.land/[email protected]/assert/assert_instance_of.js
esm/deps/deno.land/[email protected]/assert/assert_instance_of.d.ts
esm/deps/deno.land/[email protected]/assert/assert_is_error.js
esm/deps/deno.land/[email protected]/assert/assert_is_error.d.ts
esm/deps/deno.land/[email protected]/assert/assert_less.js
esm/deps/deno.land/[email protected]/assert/assert_less.d.ts
esm/deps/deno.land/[email protected]/assert/assert_less_or_equal.js
esm/deps/deno.land/[email protected]/assert/assert_less_or_equal.d.ts
esm/deps/deno.land/[email protected]/assert/assert_match.js
esm/deps/deno.land/[email protected]/assert/assert_match.d.ts
esm/deps/deno.land/[email protected]/assert/assert_not_equals.js
esm/deps/deno.land/[email protected]/assert/assert_not_equals.d.ts
esm/deps/deno.land/[email protected]/assert/assert_not_instance_of.js
esm/deps/deno.land/[email protected]/assert/assert_not_instance_of.d.ts
esm/deps/deno.land/[email protected]/assert/assert_not_match.js
esm/deps/deno.land/[email protected]/assert/assert_not_match.d.ts
esm/deps/deno.land/[email protected]/assert/assert_not_strict_equals.js
esm/deps/deno.land/[email protected]/assert/assert_not_strict_equals.d.ts
esm/deps/deno.land/[email protected]/assert/assert_object_match.js
esm/deps/deno.land/[email protected]/assert/assert_object_match.d.ts
esm/deps/deno.land/[email protected]/assert/assert_rejects.js
esm/deps/deno.land/[email protected]/assert/assert_rejects.d.ts
esm/deps/deno.land/[email protected]/assert/assert_strict_equals.js
esm/deps/deno.land/[email protected]/assert/assert_strict_equals.d.ts
esm/deps/deno.land/[email protected]/assert/assert_string_includes.js
esm/deps/deno.land/[email protected]/assert/assert_string_includes.d.ts
esm/deps/deno.land/[email protected]/assert/assert_throws.js
esm/deps/deno.land/[email protected]/assert/assert_throws.d.ts
esm/deps/deno.land/[email protected]/assert/assertion_error.js
esm/deps/deno.land/[email protected]/assert/assertion_error.d.ts
esm/deps/deno.land/[email protected]/assert/equal.js
esm/deps/deno.land/[email protected]/assert/equal.d.ts
esm/deps/deno.land/[email protected]/assert/fail.js
esm/deps/deno.land/[email protected]/assert/fail.d.ts
esm/deps/deno.land/[email protected]/assert/mod.js
esm/deps/deno.land/[email protected]/assert/mod.d.ts
esm/deps/deno.land/[email protected]/assert/unimplemented.js
esm/deps/deno.land/[email protected]/assert/unimplemented.d.ts
esm/deps/deno.land/[email protected]/assert/unreachable.js
esm/deps/deno.land/[email protected]/assert/unreachable.d.ts
esm/deps/deno.land/[email protected]/fmt/colors.js
esm/deps/deno.land/[email protected]/fmt/colors.d.ts
esm/_dnt.test_shims.js
esm/_dnt.test_shims.d.ts
test_runner.js
yarn.lock
pnpm-lock.yaml
21 changes: 21 additions & 0 deletions npm/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Ajitomi Daisuke

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
246 changes: 246 additions & 0 deletions npm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
<h1 align="center">crystals-kyber-js</h1>

<div align="center">

[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/crystals_kyber/mod.ts)
![Browser CI](https://github.com/dajiaji/crystals-kyber-js/actions/workflows/ci_browsers.yml/badge.svg)
![Node.js CI](https://github.com/dajiaji/crystals-kyber-js/actions/workflows/ci_node.yml/badge.svg)
![Deno CI](https://github.com/dajiaji/crystals-kyber-js/actions/workflows/ci_deno.yml/badge.svg)
![Cloudflare Workers CI](https://github.com/dajiaji/crystals-kyber-js/actions/workflows/ci_cloudflare.yml/badge.svg)
![Bun CI](https://github.com/dajiaji/crystals-kyber-js/actions/workflows/ci_bun.yml/badge.svg)
[![codecov](https://codecov.io/gh/dajiaji/crystals-kyber-js/branch/main/graph/badge.svg?token=7I7JGKDDJ2)](https://codecov.io/gh/dajiaji/crystals-kyber-js)

</div>

<div align="center">
A CRYSTALS-KYBER implementation written in TypeScript for various JavaScript runtimes.<br>
</div>
<p></p>

<div align="center">

[Documentation for main](https://dajiaji.github.io/crystals-kyber-js/docs/main/)

</div>

This module is based on
<a href="https://github.com/antontutoveanu/crystals-kyber-javascript">ntontutoveanu/crystals-kyber-javascript</a>,
but includes the following improvements:<br>

- ✅ Available on various JavaScript runtimes: Browsers, Node.js, Deno,
Cloudflare Workers, etc.<br>
- ✅ Written in TypeScript.<br>
- ✅ Deterministic key generation support.<br>
- ✅ Constant-time validation for ciphertext.<br>
- ✅ Better performance: 1.4 to 1.8 times faster than the original
implementation.<br>
- ✅ Tree-shaking friendly.<br>

For Node.js, you can install `crystals-kyber-js` via npm/yarn:

```sh
npm install crystals-kyber-js
```

Then, you can use it as follows:

```js
import { MlKem768 } from "crystals-kyber-js";

async function doKyber() {
// A recipient generates a key pair.
const recipient = new MlKem768(); // MlKem512 and MlKem1024 are also available.
const [pkR, skR] = await recipient.generateKeyPair();
//// Deterministic key generation is also supported
// const seed = new Uint8Array(64);
// globalThis.crypto.getRandomValues(seed); // node >= 19
// const [pkR, skR] = await recipient.deriveKeyPair(seed);

// A sender generates a ciphertext and a shared secret with pkR.
const sender = new MlKem768();
const [ct, ssS] = await sender.encap(pkR);

// The recipient decapsulates the ciphertext and generates the same shared secret with skR.
const ssR = await recipient.decap(ct, skR);

// ssS === ssR
return;
}

try {
doKyber();
} catch (err) {
console.log("failed: ", err.message);
}
```

## Index

- [Installation](#installation)
- [Node.js](#nodejs)
- [Deno](#deno)
- [Web Browsers](#web-browsers)
- [Cloudflare Workers](#cloudflare-workers)
- [Usage](#usage)
- [Contributing](#contributing)

## Installation

### Node.js

Using npm:

```sh
npm install crystals-kyber-js
```

Using yarn:

```sh
yarn add crystals-kyber-js
```

### Deno

Using deno.land:

```js
// use a specific version
import { MlKem768 } from "https://deno.land/x/[email protected]/mod.ts";

// use the latest stable version
import { MlKem768 } from "https://deno.land/x/crystals_kyber/mod.ts";
```

### Web Browsers

Followings are how to use this module with typical CDNs. Other CDNs can be used
as well.

Using esm.sh:

```html
<!-- use a specific version -->
<script type="module">
import { MlKem768 } from "https://esm.sh/[email protected]";
// ...
</script>

<!-- use the latest stable version -->
<script type="module">
import { MlKem768 } from "https://esm.sh/crystals-kyber-js";
// ...
</script>
```

Using unpkg:

```html
<!-- use a specific version -->
<script type="module">
import { MlKem768 } from "https://unpkg.com/[email protected]";
// ...
</script>
```

### Cloudflare Workers

```sh
git clone [email protected]:dajiaji/crystals-kyber-js.git
cd crystals-kyber-js
npm install -g esbuild
deno task dnt
deno task minify > $YOUR_SRC_PATH/crystals-kyber.js
```

## Usage

This section shows some typical usage examples.

### Node.js

```js
import { MlKem768 } from "crystals-kyber-js";
// const { MlKem768 } = require("crystals-kyber-js");

async function doKyber() {
const recipient = new MlKem768();
const [pkR, skR] = await recipient.generateKeyPair();

const sender = new MlKem768();
const [ct, ssS] = await sender.encap(pkR);

const ssR = await recipient.decap(ct, skR);

// ssS === ssR
return;
}

try {
doKyber();
} catch (err) {
console.log("failed: ", err.message);
}
```

### Deno

```js
import { MlKem512 } from "https://deno.land/x/[email protected]/mod.ts";

async function doKyber() {

const recipient = new MlKem512();
const [pkR, skR] = await recipient.generateKeyPair();

const sender = new MlKem512();
const [ct, ssS] = await sender.encap(pkR);

const ssR = await recipient.decap(ct, skR);

// ssS === ssR
return;
}

try {
doKyber();
} catch (_err: unknown) {
console.log("failed.");
}
```

### Browsers

```html
<html>
<head></head>
<body>
<script type="module">
import { MlKem1024 } from "https://esm.sh/[email protected]";
globalThis.doKyber = async () => {
try {
const recipient = new MlKem1024();
const [pkR, skR] = await recipient.generateKeyPair();
const sender = new MlKem1024();
const [ct, ssS] = await sender.encap(pkR);
const ssR = await recipient.decap(ct, skR);
// ssS === ssR
return;
} catch (err) {
alert("failed: ", err.message);
}
}
</script>
<button type="button" onclick="doKyber()">do CRYSTALS-KYBER</button>
</body>
</html>
```

## Contributing

We welcome all kind of contributions, filing issues, suggesting new features or
sending PRs.
1 change: 1 addition & 0 deletions npm/esm/_dnt.shims.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare const dntGlobalThis: Omit<typeof globalThis, never>;
Loading

0 comments on commit e87c731

Please sign in to comment.