Skip to content

Commit

Permalink
Use noble/curves/secp256k1 instead of noble/secp256k1.
Browse files Browse the repository at this point in the history
  • Loading branch information
dajiaji committed Jul 17, 2023
1 parent 16074c2 commit af9c0c8
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 109 deletions.
103 changes: 2 additions & 101 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/cipherSuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ export class CipherSuite {
break;
case Kem.DhkemSecp256K1HkdfSha256:
this.kemSecretSize = 32;
this.kemEncSize = 65;
this.kemPublicKeySize = 65;
this.kemEncSize = 33;
this.kemPublicKeySize = 33;
this.kemPrivateKeySize = 32;
break;
case Kem.DhkemX25519HkdfSha256:
Expand Down
10 changes: 5 additions & 5 deletions src/kems/dhkemPrimitives/secp256k1.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as secp from "npm:@noble/secp256k1@1.7.1";
import { secp256k1 } from "npm:@noble/curves@1.1.0/secp256k1";

import type { KemPrimitives } from "../../interfaces/kemPrimitives.ts";
import type { KdfInterface } from "../../interfaces/kdfInterface.ts";
Expand All @@ -18,7 +18,7 @@ export class Secp256K1 extends Algorithm implements KemPrimitives {
constructor(hkdf: KdfInterface) {
super();
this._hkdf = hkdf;
this._nPk = 65;
this._nPk = 33;
this._nSk = 32;
}

Expand Down Expand Up @@ -46,7 +46,7 @@ export class Secp256K1 extends Algorithm implements KemPrimitives {
}

public async generateKeyPair(): Promise<CryptoKeyPair> {
const rawSk = secp.utils.randomPrivateKey();
const rawSk = secp256k1.utils.randomPrivateKey();
const sk = new XCryptoKey(ALG_NAME, rawSk, "private");
const pk = await this.derivePublicKey(sk);
return { publicKey: pk, privateKey: sk };
Expand Down Expand Up @@ -111,15 +111,15 @@ export class Secp256K1 extends Algorithm implements KemPrimitives {

private _derivePublicKey(k: XCryptoKey): Promise<CryptoKey> {
return new Promise((resolve) => {
const pk = secp.getPublicKey(k.key);
const pk = secp256k1.getPublicKey(k.key);
resolve(new XCryptoKey(ALG_NAME, pk, "public"));
});
}

private _dh(sk: XCryptoKey, pk: XCryptoKey): Promise<ArrayBuffer> {
return new Promise((resolve, reject) => {
try {
resolve(secp.getSharedSecret(sk.key, pk.key).buffer);
resolve(secp256k1.getSharedSecret(sk.key, pk.key).buffer);
} catch (e: unknown) {
reject(e);
}
Expand Down
2 changes: 1 addition & 1 deletion test/kemContext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ describe("importKey", () => {
kemContext.init(api);

const cryptoApi = await loadCrypto();
const rawKey = new Uint8Array(65);
const rawKey = new Uint8Array(33);
rawKey[0] = hexStringToBytes("04")[0];
cryptoApi.getRandomValues(rawKey);
const privKey = await kemContext.importKey("raw", rawKey, true);
Expand Down

0 comments on commit af9c0c8

Please sign in to comment.