Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions apps/mobile/modules/kin-approval-key/src/publicJwk.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { p256 } from "@noble/curves/nist";
import type { DpopPublicJwk } from "@t3tools/shared/dpopCommon";
import * as Base64Url from "effect/encoding/Base64Url";
import * as Schema from "effect/Schema";
Expand All @@ -9,6 +8,19 @@ const P256_SPKI_PREFIX = Uint8Array.from([
0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00,
]);
const UNCOMPRESSED_POINT_BYTES = 65;
// P-256 field prime and curve constant b (SEC 2); the curve is y² = x³ − 3x + b.
const P256_P = 2n ** 256n - 2n ** 224n + 2n ** 192n + 2n ** 96n - 1n;
const P256_B = 0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604bn;

const toBigInt = (bytes: Uint8Array) =>
bytes.reduce((value, byte) => (value << 8n) | BigInt(byte), 0n);

function isOnP256(point: Uint8Array): boolean {
const x = toBigInt(point.subarray(1, 33));
const y = toBigInt(point.subarray(33, 65));
if (x >= P256_P || y >= P256_P) return false;
return (y * y - (x * x * x - 3n * x + P256_B)) % P256_P === 0n;
}

export class InvalidApprovalPublicKeyError extends Schema.TaggedError<InvalidApprovalPublicKeyError>()(
"InvalidApprovalPublicKeyError",
Expand Down Expand Up @@ -40,13 +52,8 @@ export function publicJwkFromNativePublicKey(publicKey: Uint8Array): DpopPublicJ
message: "Approval public key is not an uncompressed point.",
});
}
try {
p256.ProjectivePoint.fromHex(point).assertValidity();
} catch (cause) {
throw new InvalidApprovalPublicKeyError({
message: "Approval public key is not on P-256.",
cause,
});
if (!isOnP256(point)) {
throw new InvalidApprovalPublicKeyError({ message: "Approval public key is not on P-256." });
}
return {
kty: "EC",
Expand Down
2 changes: 1 addition & 1 deletion apps/mobile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
"@expo/ui": "58.0.27",
"@legendapp/list": "catalog:",
"@material/material-color-utilities": "0.3.0",
"@noble/curves": "catalog:",
"@pierre/diffs": "catalog:",
"@pierre/highlights": "catalog:",
"@react-native-ai/apple": "0.12.0",
Expand Down Expand Up @@ -145,6 +144,7 @@
"@babel/core": "^7.29.7",
"@effect/platform-node": "catalog:",
"@effect/vitest": "catalog:",
"@noble/curves": "catalog:",
"@pierre/trees": "1.0.0-beta.6",
"@types/mdast": "^4.0.4",
"@types/react": "catalog:",
Expand Down
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

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