Skip to content

Commit

Permalink
Revamp elliptic utils
Browse files Browse the repository at this point in the history
  • Loading branch information
kigawas committed Oct 28, 2024
1 parent 7e3f95e commit 96abc0c
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 87 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

- Revamp encapsulate/decapsulate
- Revamp symmetric encryption/decryption
- Revamp elliptic utils

## 0.4.10

Expand Down
2 changes: 1 addition & 1 deletion example/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"eciesjs": "file:../.."
},
"devDependencies": {
"vite": "6.0.0-beta.4",
"vite": "6.0.0-beta.5",
"vite-bundle-visualizer": "^1.2.1"
}
}
3 changes: 2 additions & 1 deletion example/browser/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export function setup(encryptedElement, textElement, decryptedElement) {
const _decrypt = () => {
encryptedElement.innerHTML = `click me to encrypt`;
if (encrypted) {
textElement.innerHTML = `${decoder.decode(decrypt(sk.secret, encrypted))}`;
const decrypted = decoder.decode(decrypt(sk.secret, encrypted));
textElement.innerHTML = `${decrypted}`;
decryptedElement.innerHTML = `decrypted:`;
encrypted = undefined;
} else {
Expand Down
8 changes: 6 additions & 2 deletions example/browser/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ export default defineConfig({
rollupOptions: {
output: {
manualChunks(id) {
if (id.includes("@noble")) {
return "noble";
if (id.includes("@noble/curves")) {
return "noble-curves";
} else if (id.includes("@noble/ciphers")) {
return "noble-ciphers";
} else if (id.includes("@noble/hashes")) {
return "noble-hashes";
} else if (id.includes("buffer")) {
return "buffer";
}
Expand Down
153 changes: 81 additions & 72 deletions pnpm-lock.yaml

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

33 changes: 22 additions & 11 deletions src/utils/elliptic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { randomBytes } from "@noble/ciphers/webcrypto";
import { ed25519, x25519 } from "@noble/curves/ed25519";
import { secp256k1 } from "@noble/curves/secp256k1";

import { ellipticCurve } from "../config";
import { ellipticCurve, EllipticCurve } from "../config";
import { ETH_PUBLIC_KEY_SIZE, SECRET_KEY_LENGTH } from "../consts";
import { decodeHex } from "./hex";

Expand All @@ -18,13 +18,15 @@ export const isValidPrivateKey = (secret: Uint8Array): boolean =>
// on secp256k1: only key ∈ (0, group order) is valid
// on curve25519: any 32-byte key is valid
_exec(
ellipticCurve(),
(curve) => curve.utils.isValidPrivateKey(secret),
() => true,
() => true
);

export const getPublicKey = (secret: Uint8Array): Uint8Array =>
_exec(
ellipticCurve(),
(curve) => curve.getPublicKey(secret),
(curve) => curve.getPublicKey(secret),
(curve) => curve.getPublicKey(secret)
Expand All @@ -36,19 +38,16 @@ export const getSharedPoint = (
compressed?: boolean
): Uint8Array =>
_exec(
ellipticCurve(),
(curve) => curve.getSharedSecret(sk, pk, compressed),
(curve) => curve.getSharedSecret(sk, pk),
(curve) => {
// Note: scalar is hashed from sk
const { scalar } = curve.utils.getExtendedPublicKey(sk);
const point = curve.ExtendedPoint.fromHex(pk).multiply(scalar);
return point.toRawBytes();
}
(curve) => getSharedPointOnEd25519(curve, sk, pk)
);

export const convertPublicKeyFormat = (pk: Uint8Array, compressed: boolean): Uint8Array =>
// only for secp256k1
_exec(
ellipticCurve(),
(curve) => curve.getSharedSecret(BigInt(1), pk, compressed),
() => pk,
() => pk
Expand All @@ -57,18 +56,19 @@ export const convertPublicKeyFormat = (pk: Uint8Array, compressed: boolean): Uin
export const hexToPublicKey = (hex: string): Uint8Array => {
const decoded = decodeHex(hex);
return _exec(
ellipticCurve(),
() => compatEthPublicKey(decoded),
() => decoded,
() => decoded
);
};

function _exec<T>(
secp256k1Callback: (curve: typeof secp256k1) => T,
x25519Callback: (curve: typeof x25519) => T,
ed25519Callback: (curve: typeof ed25519) => T
curve: EllipticCurve,
secp256k1Callback: (curveFn: typeof secp256k1) => T,
x25519Callback: (curveFn: typeof x25519) => T,
ed25519Callback: (curveFn: typeof ed25519) => T
): T {
const curve = ellipticCurve();
if (curve === "secp256k1") {
return secp256k1Callback(secp256k1);
} else if (curve === "x25519") {
Expand All @@ -89,3 +89,14 @@ const compatEthPublicKey = (pk: Uint8Array): Uint8Array => {
}
return pk;
};

const getSharedPointOnEd25519 = (
curve: typeof ed25519,
sk: Uint8Array,
pk: Uint8Array
): Uint8Array => {
// Note: scalar is hashed from sk
const { scalar } = curve.utils.getExtendedPublicKey(sk);
const point = curve.ExtendedPoint.fromHex(pk).multiply(scalar);
return point.toRawBytes();
};

0 comments on commit 96abc0c

Please sign in to comment.