Skip to content

Commit

Permalink
Update NPM files
Browse files Browse the repository at this point in the history
  • Loading branch information
ronhombre committed Aug 31, 2024
1 parent c65309a commit 36a66c6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 32 deletions.
21 changes: 11 additions & 10 deletions npm/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# KyberKotlin (NPM build)
### Bringing ML-KEM into Javascript from a compiled Kotlin Multiplatform Project.

Parent Source: [KyberKotlin](https://github.com/ronhombre/KyberKotlin)

Generated from parent KMM library using `./gradlew bundleNPM`
Expand All @@ -11,22 +13,21 @@ Visit [kyber.hombre.asia](https://kyber.hombre.asia). All **common** methods and

## Usage sample
```javascript
const { KyberParameter, KyberKeyGenerator, KyberAgreement } = require("kyberkotlin").asia.hombre.kyber;

let aliceKeypair = KyberKeyGenerator.Companion.generate(KyberParameter.ML_KEM_512);
const { KyberParameter, KyberKeyGenerator } = require("kyberkotlin").asia.hombre.kyber;

let aliceAgreement = new KyberAgreement(aliceKeypair.decapsulationKey);
let aliceKeypair = KyberKeyGenerator.generate(KyberParameter.ML_KEM_512);

//Send the Encapsulation Key and they encapsulate a Shared Secret Key with it.
let results = KyberAgreement.Companion.encapsulate(aliceKeypair.encapsulationKey);
//Send the Encapsulation Key to Bob and they encapsulate a Shared Secret Key with it.
let results = aliceKeypair.encapsulationKey.encapsulate();

let ciphertext = results.cipherText;
let bobSecretKey = results.secretKey;
let bobSecretKey = results.sharedSecretKey; //This is Bob's copy of the Shared Secret Key

//Receive the Cipher Text and decapsulate the Shared Secret Key in it.
let aliceSecretKey = aliceAgreement.decapsulate(ciphertext);
//Alice receives the Cipher Text from Bob and decapsulates the Shared Secret Key in it.
let aliceSecretKey = aliceKeypair.decapsulationKey.decapsulate(ciphertext);
//You can also decapsulate the other way -> ciphertext.decapsulate(aliceKeypair.decapsulationKey);

console.assert(contentEquals(aliceSecretKey, bobSecretKey), "Secret Keys does not match!");
console.assert(contentEquals(aliceSecretKey, bobSecretKey), "Shared Secret Keys does not match!");

//Simple check
function contentEquals(a, b) {
Expand Down
38 changes: 16 additions & 22 deletions npm/test.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,45 @@
const { KyberParameter, KyberKeyGenerator, KyberAgreement } = require("./kotlin/KyberKotlin").asia.hombre.kyber;
const { KyberParameter, KyberKeyGenerator } = require("./kotlin/KyberKotlin").asia.hombre.kyber;

function test512() {
console.log("Testing 512...");
let aliceKeypair = KyberKeyGenerator.Companion.generate(KyberParameter.ML_KEM_512);
let aliceKeypair = KyberKeyGenerator.generate(KyberParameter.ML_KEM_512);

let aliceAgreement = new KyberAgreement(aliceKeypair.decapsulationKey);

let results = KyberAgreement.Companion.encapsulate(aliceKeypair.encapsulationKey);
let results = aliceKeypair.encapsulationKey.encapsulate();

let ciphertext = results.cipherText;
let bobSecretKey = results.secretKey;
let bobSecretKey = results.sharedSecretKey;

let aliceSecretKey = aliceAgreement.decapsulate(ciphertext);
let aliceSecretKey = aliceKeypair.decapsulationKey.decapsulate(ciphertext);

console.assert(contentEquals(aliceSecretKey, bobSecretKey), "Secret Keys for 512 does not match!");
console.assert(contentEquals(aliceSecretKey, bobSecretKey), "Shared Secret Keys for 512 does not match!");
}

function test768() {
console.log("Testing 768...");
let aliceKeypair = KyberKeyGenerator.Companion.generate(KyberParameter.ML_KEM_768);

let aliceAgreement = new KyberAgreement(aliceKeypair.decapsulationKey);
let aliceKeypair = KyberKeyGenerator.generate(KyberParameter.ML_KEM_768);

let results = KyberAgreement.Companion.encapsulate(aliceKeypair.encapsulationKey);
let results = aliceKeypair.encapsulationKey.encapsulate();

let ciphertext = results.cipherText;
let bobSecretKey = results.secretKey;
let bobSecretKey = results.sharedSecretKey;

let aliceSecretKey = aliceAgreement.decapsulate(ciphertext);
let aliceSecretKey = aliceKeypair.decapsulationKey.decapsulate(ciphertext);

console.assert(contentEquals(aliceSecretKey, bobSecretKey), "Secret Keys for 768 does not match!");
console.assert(contentEquals(aliceSecretKey, bobSecretKey), "Shared Secret Keys for 768 does not match!");
}

function test1024() {
console.log("Testing 1024...");
let aliceKeypair = KyberKeyGenerator.Companion.generate(KyberParameter.ML_KEM_1024);

let aliceAgreement = new KyberAgreement(aliceKeypair.decapsulationKey);
let aliceKeypair = KyberKeyGenerator.generate(KyberParameter.ML_KEM_1024);

let results = KyberAgreement.Companion.encapsulate(aliceKeypair.encapsulationKey);
let results = aliceKeypair.encapsulationKey.encapsulate();

let ciphertext = results.cipherText;
let bobSecretKey = results.secretKey;
let bobSecretKey = results.sharedSecretKey;

let aliceSecretKey = aliceAgreement.decapsulate(ciphertext);
let aliceSecretKey = aliceKeypair.decapsulationKey.decapsulate(ciphertext);

console.assert(contentEquals(aliceSecretKey, bobSecretKey), "Secret Keys for 1024 does not match!");
console.assert(contentEquals(aliceSecretKey, bobSecretKey), "Shared Secret Keys for 1024 does not match!");
}

test512();
Expand Down

0 comments on commit 36a66c6

Please sign in to comment.