Skip to content

Commit

Permalink
Refactor DhkemSecp256k1.
Browse files Browse the repository at this point in the history
  • Loading branch information
dajiaji committed Aug 8, 2023
1 parent 7171443 commit d552a90
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
// @ts-ignore: for "npm:"
import { secp256k1 } from "npm:@noble/[email protected]/secp256k1";

import type { KemPrimitives } from "../../../src/interfaces/kemPrimitives.ts";
import type { KdfInterface } from "../../../src/interfaces/kdfInterface.ts";
import type { KemInterface } from "../../../src/interfaces/kemInterface.ts";
import type { KemPrimitives } from "../../interfaces/kemPrimitives.ts";
import type { KdfInterface } from "../../interfaces/kdfInterface.ts";

import { Algorithm } from "../../../src/algorithm.ts";
import { KemId } from "../../../src/identifiers.ts";
import { Algorithm } from "../../algorithm.ts";
import {
KEM_USAGES,
LABEL_DKP_PRK,
LABEL_SK,
} from "../../../src/interfaces/kemPrimitives.ts";
import { XCryptoKey } from "../../../src/xCryptoKey.ts";
import { HkdfSha256 } from "../../../src/kdfs/hkdfSha256.ts";
import { Dhkem } from "../../../src/kems/dhkem.ts";
} from "../../interfaces/kemPrimitives.ts";
import { XCryptoKey } from "../../xCryptoKey.ts";

import { EMPTY } from "../../../src/consts.ts";
import { EMPTY } from "../../consts.ts";

const ALG_NAME = "ECDH";

class Secp256k1 extends Algorithm implements KemPrimitives {
export class Secp256k1 extends Algorithm implements KemPrimitives {
private _hkdf: KdfInterface;
private _nPk: number;
private _nSk: number;
Expand Down Expand Up @@ -142,41 +138,3 @@ class Secp256k1 extends Algorithm implements KemPrimitives {
});
}
}

/**
* The DHKEM(secp256k1, HKDF-SHA256).
*
* This class is implemented using
* {@link https://github.com/paulmillr/noble-curves | @noble/curves}.
*
* The public keys are assumed to be compressed.
*
* The instance of this class can be specified to the
* {@link https://deno.land/x/hpke/core/mod.ts?s=CipherSuiteParams | CipherSuiteParams} as follows:
*
* @example
* ```ts
* import { KdfId, AeadId, CipherSuite } from "http://deno.land/x/hpke/core/mod.ts";
* import { DhkemSecp256k1HkdfSha256} from "https://deno.land/x/hpke/x/dhkem-secp256k1/mod.ts";
* const suite = new CipherSuite({
* kem: new DhkemSecp256k1HkdfSha256(),
* kdf: KdfId.HkdfSha256,
* aead: AeadId.Aes128Gcm,
* });
* ```
*
* @experimental Note that it is experimental and not standardized.
*/
export class DhkemSecp256k1HkdfSha256 extends Dhkem implements KemInterface {
public readonly id: KemId = KemId.DhkemSecp256k1HkdfSha256;
public readonly secretSize: number = 32;
public readonly encSize: number = 33;
public readonly publicKeySize: number = 33;
public readonly privateKeySize: number = 32;

constructor() {
const kdf = new HkdfSha256();
const prim = new Secp256k1(kdf);
super(prim, kdf);
}
}
43 changes: 43 additions & 0 deletions src/kems/dhkemSecp256k1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { KemId } from "../identifiers.ts";
import { HkdfSha256 } from "../kdfs/hkdfSha256.ts";
import { Dhkem } from "./dhkem.ts";
import { Secp256k1 } from "./dhkemPrimitives/secp256k1.ts";

/**
* The DHKEM(secp256k1, HKDF-SHA256).
*
* This class is implemented using
* {@link https://github.com/paulmillr/noble-curves | @noble/curves}.
*
* The public keys are assumed to be compressed.
*
* The instance of this class can be specified to the
* {@link https://deno.land/x/hpke/core/mod.ts?s=CipherSuiteParams | CipherSuiteParams} as follows:
*
* @example
*
* ```ts
* import { KdfId, AeadId, CipherSuite } from "http://deno.land/x/hpke/core/mod.ts";
* import { DhkemSecp256k1HkdfSha256} from "https://deno.land/x/hpke/x/dhkem-secp256k1/mod.ts";
* const suite = new CipherSuite({
* kem: new DhkemSecp256k1HkdfSha256(),
* kdf: KdfId.HkdfSha256,
* aead: AeadId.Aes128Gcm,
* });
* ```
*
* @experimental Note that it is experimental and not standardized.
*/
export class DhkemSecp256k1HkdfSha256 extends Dhkem {
public readonly id: KemId = KemId.DhkemSecp256k1HkdfSha256;
public readonly secretSize: number = 32;
public readonly encSize: number = 33;
public readonly publicKeySize: number = 33;
public readonly privateKeySize: number = 32;

constructor() {
const kdf = new HkdfSha256();
const prim = new Secp256k1(kdf);
super(prim, kdf);
}
}
2 changes: 1 addition & 1 deletion x/dhkem-secp256k1/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
]
},
"lint": {
"include": ["mod.ts", "src/", "test/"],
"include": ["mod.ts", "test/"],
"exclude": [
"test/runtimes/bun",
"test/runtimes/browsers/node_modules",
Expand Down
2 changes: 1 addition & 1 deletion x/dhkem-secp256k1/mod.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { DhkemSecp256k1HkdfSha256 } from "./src/dhkem-secp256k1.ts";
export { DhkemSecp256k1HkdfSha256 } from "../../src/kems/dhkemSecp256k1.ts";
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { describe, it } from "testing/bdd.ts";
import { AeadId, CipherSuite, KdfId, KemId } from "../../../mod.ts";
// } from "https://deno.land/x/hpke/mod.ts";

import { DhkemSecp256k1HkdfSha256 } from "../src/dhkem-secp256k1.ts";
import { DhkemSecp256k1HkdfSha256 } from "../mod.ts";
import { hexStringToBytes, loadCrypto, loadSubtleCrypto } from "./utils.ts";

describe("DhkemSecp256k1Hkdf256", () => {
Expand Down
2 changes: 1 addition & 1 deletion x/dhkem-secp256k1/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
},
"include": [
"mod.ts",
"src/**/*"
"../../src/kems/dhkemSecp256k1.ts"
]
}

0 comments on commit d552a90

Please sign in to comment.