/** * Zero-dependency Base32 Geohash encoder & 9-cell halo generator for blind locality PSI. * - Precision 5 (~4.9km x 4.9km cell, ~15km x 15km 9-cell halo): city / borough / metro-area scale * - Precision 6 (~1.2km x 0.6km cell, ~3.6km x 1.8km 9-cell halo): neighborhood scale (e.g., Park Slope + adjacent blocks) * * Plaintext coordinates NEVER leave the local device; only Ristretto255-blinded tokens are published. */ const BASE32 = "0123456789bcdefghjkmnpqrstuvwxyz"; export function encodeGeohash(lat: number, lng: number, precision: number): string { if (!Number.isFinite(lat) || lat < -90 || lat > 90) { throw new Error(`Invalid latitude: ${lat}`); } if (!Number.isFinite(lng) || lng < -180 || lng > 180) { throw new Error(`Invalid longitude: ${lng}`); } if (!Number.isInteger(precision) || precision < 1 || precision > 8) { throw new Error(`Invalid geohash precision: ${precision}`); } let latMin = -90.0; let latMax = 90.0; let lngMin = -180.0; let lngMax = 180.0; let isLng = true; let bit = 0; let ch = 0; let hash = ""; while (hash.length < precision) { if (isLng) { const mid = (lngMin + lngMax) / 2; if (lng >= mid) { ch = (ch << 1) | 1; lngMin = mid; } else { ch = ch << 1; lngMax = mid; } } else { const mid = (latMin + latMax) / 2; if (lat >= mid) { ch = (ch << 1) | 1; latMin = mid; } else { ch = ch << 1; latMax = mid; } } isLng = !isLng; bit++; if (bit === 5) { hash += BASE32[ch]; bit = 0; ch = 0; } } return hash; } export function decodeGeohashBounds(hash: string): { latMin: number; latMax: number; lngMin: number; lngMax: number; } { let latMin = -90.0; let latMax = 90.0; let lngMin = -180.0; let lngMax = 180.0; let isLng = true; for (const c of hash.toLowerCase()) { const idx = BASE32.indexOf(c); if (idx < 0) throw new Error(`Invalid geohash char '${c}'`); for (let mask = 16; mask > 0; mask >>= 1) { if (isLng) { const mid = (lngMin + lngMax) / 2; if (idx & mask) lngMin = mid; else lngMax = mid; } else { const mid = (latMin + latMax) / 2; if (idx & mask) latMin = mid; else latMax = mid; } isLng = !isLng; } } return { latMin, latMax, lngMin, lngMax }; } function wrapLng(lng: number): number { if (lng > 180) return lng - 360; if (lng < -180) return lng + 360; return lng; } /** * Compute center cell + 8 surrounding adjacent cells so people on opposite sides of a geohash border still match. */ export function getGeohashHalo(lat: number, lng: number, precision: 5 | 6): string[] { const center = encodeGeohash(lat, lng, precision); const b = decodeGeohashBounds(center); const dLat = b.latMax - b.latMin; const dLng = b.lngMax - b.lngMin; const cLat = (b.latMin + b.latMax) / 2; const cLng = (b.lngMin + b.lngMax) / 2; const cells = new Set([center]); for (const dy of [-1, 0, 1]) { for (const dx of [-1, 0, 1]) { if (dx === 0 && dy === 0) continue; const nLat = Math.max(-89.9999, Math.min(89.9999, cLat + dy * dLat)); const nLng = wrapLng(cLng + dx * dLng); cells.add(encodeGeohash(nLat, nLng, precision)); } } return Array.from(cells).sort(); } export interface BlindLocalityTokens { metro_cells: string[]; // precision-5 halo (up to 9 cells, ~15km span) neighborhood_cells: string[]; // precision-6 halo (up to 9 cells, ~3.5km span) } export function buildLocalityTokens(lat: number, lng: number): BlindLocalityTokens { return { metro_cells: getGeohashHalo(lat, lng, 5), neighborhood_cells: getGeohashHalo(lat, lng, 6), }; }