Skip to content

Commit

Permalink
Merge pull request #29 from dajiaji/add-docstring
Browse files Browse the repository at this point in the history
Add docstring.
  • Loading branch information
dajiaji authored Jan 27, 2024
2 parents 1e0cda6 + 8c3cb28 commit 309d9ff
Show file tree
Hide file tree
Showing 4 changed files with 387 additions and 3 deletions.
42 changes: 41 additions & 1 deletion src/kyber1024.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import { KyberBase } from "./kyberBase.ts";
import { byte, int16, uint16, uint32 } from "./utils.ts";

/**
* The Kyber1024 implementation.
* Represents the Kyber1024 class.
*
* Kyber1024 is a subclass of KyberBase and implements specific methods for the Kyber-1024 parameter set.
*
* @example
*
Expand All @@ -33,6 +35,9 @@ export class Kyber1024 extends KyberBase {
protected _eta1 = 2;
protected _eta2 = 2;

/**
* Constructs a new instance of the Kyber1024 class.
*/
constructor() {
super();
this._skSize = 12 * this._k * N / 8;
Expand All @@ -42,6 +47,13 @@ export class Kyber1024 extends KyberBase {
}

// compressU lossily compresses and serializes a vector of polynomials.

/**
* Lossily compresses and serializes a vector of polynomials.
*
* @param u - The vector of polynomials to compress.
* @returns The compressed and serialized data as a Uint8Array.
*/
protected override _compressU(
r: Uint8Array,
u: Array<Array<number>>,
Expand Down Expand Up @@ -72,6 +84,14 @@ export class Kyber1024 extends KyberBase {
}

// compressV lossily compresses and subsequently serializes a polynomial.

/**
* Lossily compresses and serializes a polynomial.
*
* @param r - The output buffer to store the compressed data.
* @param v - The polynomial to compress.
* @returns The compressed and serialized data as a Uint8Array.
*/
protected override _compressV(r: Uint8Array, v: Array<number>): Uint8Array {
const t = new Uint8Array(8);
for (let rr = 0, i = 0; i < N / 8; i++) {
Expand All @@ -92,6 +112,15 @@ export class Kyber1024 extends KyberBase {
// decompressU de-serializes and decompresses a vector of polynomials and
// represents the approximate inverse of compress1. Since compression is lossy,
// the results of decompression will may not match the original vector of polynomials.

/**
* Deserializes and decompresses a vector of polynomials.
* This is the approximate inverse of the `_compressU` method.
* Since compression is lossy, the decompressed data may not match the original vector of polynomials.
*
* @param a - The compressed and serialized data as a Uint8Array.
* @returns The decompressed vector of polynomials.
*/
protected override _decompressU(a: Uint8Array): Array<Array<number>> {
const r = new Array<Array<number>>(this._k);
for (let i = 0; i < this._k; i++) {
Expand Down Expand Up @@ -123,6 +152,17 @@ export class Kyber1024 extends KyberBase {
// representing the approximate inverse of compress2.
// Note that compression is lossy, and thus decompression will not match the
// original input.

/**
* Decompresses a given polynomial, representing the approximate inverse of
* compress2, in Uint8Array into an array of numbers.
*
* Note that compression is lossy, and thus decompression will not match the
* original input.
*
* @param a - The Uint8Array to decompress.
* @returns An array of numbers obtained from the decompression process.
*/
protected override _decompressV(a: Uint8Array): Array<number> {
const r = new Array<number>(384);
const t = new Array<number>(8);
Expand Down
26 changes: 25 additions & 1 deletion src/kyber512.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import { KyberBase } from "./kyberBase.ts";
import { byteopsLoad24, int16, prf } from "./utils.ts";

/**
* The Kyber512 implementation.
* Represents the Kyber512 class.
*
* This class extends the KyberBase class and provides specific implementation for Kyber512.
*
* @remarks
*
* Kyber512 is a specific implementation of the Kyber key encapsulation mechanism.
*
* @example
*
Expand All @@ -33,6 +39,9 @@ export class Kyber512 extends KyberBase {
protected _eta1 = 3;
protected _eta2 = 2;

/**
* Constructs a new instance of the Kyber512 class.
*/
constructor() {
super();
this._skSize = 12 * this._k * N / 8;
Expand All @@ -41,6 +50,14 @@ export class Kyber512 extends KyberBase {
this._compressedVSize = this._dv * N / 8;
}

/**
* Samples a vector of polynomials from a seed.
* @internal
* @param sigma - The seed.
* @param offset - The offset.
* @param size - The size.
* @returns The sampled vector of polynomials.
*/
protected override _sampleNoise1(
sigma: Uint8Array,
offset: number,
Expand All @@ -55,6 +72,13 @@ export class Kyber512 extends KyberBase {
}
}

/**
* Performs the byte operations for the Cbd function.
*
* @param buf - The input buffer.
* @param eta - The value of eta.
* @returns An array of numbers representing the result of the byte operations.
*/
function byteopsCbd(buf: Uint8Array, eta: number): Array<number> {
let t, d;
let a, b;
Expand Down
8 changes: 7 additions & 1 deletion src/kyber768.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import { N } from "./consts.ts";
import { KyberBase } from "./kyberBase.ts";

/**
* The Kyber768 implementation.
* Represents the Kyber768 class, which extends the KyberBase class.
*
* Kyber768 is a specific implementation of the Kyber key encapsulation mechanism.
*
* @remarks
*
* This class extends the KyberBase class and provides specific implementation for Kyber768.
*
* @example
*
Expand Down
Loading

0 comments on commit 309d9ff

Please sign in to comment.