From 92362c2837ce1b538fb6adebb0ccb1ac98ee7e67 Mon Sep 17 00:00:00 2001 From: GLDRoger <130458023+GLDRoger@users.noreply.github.com> Date: Tue, 10 Nov 2026 15:10:26 +0530 Subject: [PATCH] perf(mobile): check the approval key's curve without @noble/curves Upstream moved DPoP signing native and dropped @noble/curves from the app. The approval key already signs in the Secure Enclave; its one runtime use was an on-curve check at enrolment, now a few lines of BigInt arithmetic. The library stays for tests only. Co-Authored-By: Claude Opus 5.5 --- .../modules/kin-approval-key/src/publicJwk.ts | 23 ++++++++++++------- apps/mobile/package.json | 2 +- pnpm-lock.yaml | 6 ++--- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/apps/mobile/modules/kin-approval-key/src/publicJwk.ts b/apps/mobile/modules/kin-approval-key/src/publicJwk.ts index c977a0db09094..b2dd077319f8a 100644 --- a/apps/mobile/modules/kin-approval-key/src/publicJwk.ts +++ b/apps/mobile/modules/kin-approval-key/src/publicJwk.ts @@ -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"; @@ -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", @@ -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", diff --git a/apps/mobile/package.json b/apps/mobile/package.json index ef79d0c4491de..6c6c0d8ea3951 100644 --- a/apps/mobile/package.json +++ b/apps/mobile/package.json @@ -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", @@ -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:", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e42eac528bec2..cb7f764481a6c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -559,9 +559,6 @@ importers: '@material/material-color-utilities': specifier: 0.3.0 version: 0.3.0 - '@noble/curves': - specifier: 'catalog:' - version: 1.9.1 '@pierre/diffs': specifier: 1.7.0-beta.3 version: 1.7.0-beta.3(patch_hash=aea143f4a2e59744371219c4e7ae675ec739c9ba16f1732e2f11748b496ea05e)(@shikijs/themes@4.2.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0) @@ -806,6 +803,9 @@ importers: '@effect/vitest': specifier: 4.1.2 version: 4.1.2(patch_hash=299510b71c36537a832a67e9d160a5f112a5f326a29e95c41cb45fc8234e47c7)(@astrojs/compiler-rs@0.5.1)(@types/node@24.12.4)(@vitest/browser-playwright@5.0.5)(@vitest/ui@5.0.5)(@volar/typescript@2.4.28(typescript@6.0.3))(bufferutil@4.1.0)(effect@4.1.2)(esbuild@0.28.2)(jiti@2.7.0)(jsdom@30.1.0(@noble/hashes@1.8.0))(playwright@1.60.0)(terser@5.48.0)(typescript@6.0.3)(unrun@0.2.39)(utf-8-validate@6.0.6)(yaml@2.9.0) + '@noble/curves': + specifier: 'catalog:' + version: 1.9.1 '@pierre/trees': specifier: 1.0.0-beta.6 version: 1.0.0-beta.6(@pierre/theme@2.0.0)(@shikijs/themes@4.2.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)(shiki@4.2.0)