diff --git a/src/__tests__/curve-detection.test.ts b/src/__tests__/curve-detection.test.ts new file mode 100644 index 0000000..d449f52 --- /dev/null +++ b/src/__tests__/curve-detection.test.ts @@ -0,0 +1,16 @@ +import { describe, expect, it } from "vitest"; +import { detectPublicKeyCurve } from "../curve-detection.js"; +import { CurveType } from "../types.js"; + +describe("detectPublicKeyCurve", () => { + it("accepts raw and uncompressed Ethereum secp256k1 public keys", () => { + expect(detectPublicKeyCurve("11".repeat(64))).toBe(CurveType.ETHSECP256K1); + expect(detectPublicKeyCurve("04" + "11".repeat(64))).toBe(CurveType.ETHSECP256K1); + }); + + it("rejects 65-byte Ethereum secp256k1 public keys without the uncompressed prefix", () => { + expect(() => detectPublicKeyCurve("05" + "11".repeat(64))).toThrow( + "65-byte ETHSECP256K1 keys must start with 0x04", + ); + }); +}); \ No newline at end of file diff --git a/src/curve-detection.ts b/src/curve-detection.ts index c6a6564..f3e3b58 100644 --- a/src/curve-detection.ts +++ b/src/curve-detection.ts @@ -12,8 +12,10 @@ export function detectPublicKeyCurve(publicKeyHex: string): CurveType { case KEY_SIZES.PUBLIC.BLS12381: return CurveType.BLS12381; case KEY_SIZES.PUBLIC.ETHSECP256K1: - case 65: return CurveType.ETHSECP256K1; + case 65: + if (bytes[0] === 0x04) return CurveType.ETHSECP256K1; + throw new Error("Unrecognized public key format: 65-byte ETHSECP256K1 keys must start with 0x04"); default: throw new Error(`Unrecognized public key format: ${bytes.length} bytes`); } diff --git a/src/keystore.ts b/src/keystore.ts index 193bde3..120ffc1 100644 --- a/src/keystore.ts +++ b/src/keystore.ts @@ -26,9 +26,12 @@ export function importFromGoKeystore( const curveType = detectPublicKeyCurve(entry.publicKey); const derivedAddress = deriveAddress(entry.publicKey, curveType); + // Throw instead of warn: a mismatch means the entry is tampered or corrupted. + // Silently importing the wrong address would cause funds to be sent to the + // wrong destination or transactions to be rejected by the network. if (derivedAddress.toLowerCase() !== entry.keyAddress.toLowerCase()) { - console.warn( - `Address mismatch: expected ${entry.keyAddress}, derived ${derivedAddress}` + throw new Error( + `Keystore integrity check failed: stored address ${entry.keyAddress} does not match address derived from public key ${derivedAddress}. The keystore entry may be corrupted or tampered.` ); } diff --git a/src/provably-fair.ts b/src/provably-fair.ts index a60371d..f849c84 100644 --- a/src/provably-fair.ts +++ b/src/provably-fair.ts @@ -36,7 +36,15 @@ export function computeHMAC( /** * Computes a dice roll in the range [0, 9999] from the given seeds and nonce. - * Maps the first 4 bytes of HMAC-SHA256 to a uint32, then takes modulo 10000. + * + * Uses rejection sampling over successive 4-byte HMAC windows to eliminate + * modulo bias. A naive `uint32 % 10000` is biased because 2^32 (4 294 967 296) + * is not evenly divisible by 10000 — values 0–7295 appear once more often than + * values 7296–9999, giving the house a hidden systematic edge on those outcomes. + * + * Rejection threshold: 4 294 960 000 (= floor(2^32 / 10000) * 10000). + * Values at or above the threshold are discarded and the next 4-byte window is + * tried. In practice fewer than 2 iterations are needed on average. */ export function computeDiceRoll( serverSeed: string, @@ -44,9 +52,16 @@ export function computeDiceRoll( nonce: number ): number { const h = computeHMAC(serverSeed, clientSeed, nonce); - const view = new DataView(h.buffer, h.byteOffset, h.byteLength); - const raw = view.getUint32(0, false); // big-endian - return raw % 10000; + const RANGE = 10000; + const THRESHOLD = Math.floor(0x100000000 / RANGE) * RANGE; // 4_294_960_000 + for (let offset = 0; offset + 4 <= h.length; offset += 4) { + const view = new DataView(h.buffer, h.byteOffset + offset, 4); + const raw = view.getUint32(0, false); // big-endian + if (raw < THRESHOLD) return raw % RANGE; + } + // Fallback (astronomically unlikely): use modulo on last window + const view = new DataView(h.buffer, h.byteOffset, 4); + return view.getUint32(0, false) % RANGE; } /** @@ -63,18 +78,30 @@ export function verifyDiceRoll( /** * Computes the crash point for a rocket round. - * Uses HMAC-SHA256(serverSeed, nonce) with 3% house edge. + * Uses HMAC-SHA256(serverSeed, clientSeed:nonce) with 3% house edge. * Result is clamped to [1.01, 100.0]. + * + * @param clientSeed - Player-supplied seed that contributes entropy to the outcome. + * Must not be empty: passing "" removes the client's ability to independently + * influence and verify the result, breaking the provably-fair guarantee. */ export function computeCrashPoint( serverSeed: string, + clientSeed: string, nonce: number ): number { - const h = computeHMAC(serverSeed, "", nonce); + if (!clientSeed) { + throw new Error("clientSeed must not be empty — an empty client seed removes player entropy from the provably-fair computation"); + } + const h = computeHMAC(serverSeed, clientSeed, nonce); const hex8 = bytesToHex(h).slice(0, 8); const result = parseInt(hex8, 16) >>> 0; // unsigned 32-bit - if (result % 33 === 0) return 1.0; + // Instant-crash case: return the advertised floor (1.01), not 1.0. + // The docstring and UI both promise outcomes in [1.01, 100.0]; returning + // 1.0 violates the stated minimum and gives players a worse payout than + // the published house-edge formula implies. + if (result % 33 === 0) return 1.01; const houseEdge = 0.03; const e = 0x100000000; @@ -83,4 +110,4 @@ export function computeCrashPoint( if (crashPoint < 1.01) crashPoint = 1.01; if (crashPoint > 100.0) crashPoint = 100.0; return crashPoint; -} +} \ No newline at end of file