{"record":{"id":"21d45bb760497f4b","repo":"denoland/deno","slug":"err-ossl-bn-bignum-too-long","errorCode":"ERR_OSSL_BN_BIGNUM_TOO_LONG","errorMessage":"bignum too long","messagePattern":"bignum too long","errorType":"exception","errorClass":"NodeError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/internal/crypto/random.ts","lineNumber":102,"sourceCode":"\n  const {\n    checks = 0,\n  } = options!;\n\n  validateInt32(checks, \"options.checks\", 0);\n\n  let candidateBytes: ArrayBufferView | ArrayBuffer;\n  if (typeof candidate === \"bigint\") {\n    if (candidate < 0) {\n      throw new ERR_OUT_OF_RANGE(\"candidate\", \">= 0\", candidate);\n    }\n    candidateBytes = bigintToBytes(candidate);\n  } else if (isAnyArrayBuffer(candidate) || isArrayBufferView(candidate)) {\n    const byteLength = isArrayBufferView(candidate)\n      ? arrayBufferViewByteLength(candidate as ArrayBufferView)\n      : ArrayBufferPrototypeGetByteLength(candidate as ArrayBuffer);\n    if (byteLength > OPENSSL_BIGNUM_MAX_BYTES) {\n      throw new NodeError(\n        \"ERR_OSSL_BN_BIGNUM_TOO_LONG\",\n        \"bignum too long\",\n      );\n    }\n    candidateBytes = candidate;\n  } else {\n    throw new ERR_INVALID_ARG_TYPE(\n      \"candidate\",\n      [\n        \"ArrayBuffer\",\n        \"TypedArray\",\n        \"Buffer\",\n        \"DataView\",\n        \"bigint\",\n      ],\n      candidate,\n    );\n  }","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/internal/crypto/random.ts#L84-L120","documentation":"Thrown by checkPrime() in Deno's node:crypto polyfill (ext/node/polyfills/internal/crypto/random.ts:102) when the candidate ArrayBuffer/TypedArray/Buffer/DataView exceeds OPENSSL_BIGNUM_MAX_BYTES (67,108,856 bytes, the OpenSSL BIGNUM word limit). OpenSSL cannot represent a bignum larger than this, so the input is rejected before the primality op runs.","triggerScenarios":"crypto.checkPrime(new Uint8Array(67_108_857)) or passing an ArrayBuffer whose byteLength is greater than 67108856 (a ~64MiB integer, i.e. a >536-million-bit number).","commonSituations":"Almost always a bug: a buffer of the wrong size passed by mistake (e.g. a whole file or key blob), a length calculation in bits vs bytes, or a huge preallocated buffer that was never filled.","solutions":["Check the byteLength of the candidate buffer — if it is near or above 64MiB you almost certainly passed the wrong object.","If you built the buffer from a BigInt, pass the BigInt itself so bigintToBytes produces a minimal byte representation.","Verify your bit/byte math: a 2048-bit prime needs a 256-byte buffer, not a 2048-byte one."],"exampleFix":"// before\ncrypto.checkPrime(hugeBuffer, cb);\n\n// after\nconst bits = 2048;\nconst buf = new Uint8Array(bits / 8); // bytes, not bits\ncrypto.checkPrime(buf, cb);","handlingStrategy":"validation","validationCode":"const MAX_BN = 67108856;\nconst bytes = ArrayBuffer.isView(c) ? c.byteLength : c.byteLength;\nif (bytes > MAX_BN) throw new Error('candidate too large for OpenSSL bignum');","typeGuard":"null","tryCatchPattern":"try { crypto.checkPrime(c, cb); } catch (e) { if (e.code === 'ERR_OSSL_BN_BIGNUM_TOO_LONG') { /* wrong buffer passed — log size and fix caller */ } else throw e; }","preventionTips":["Allocate candidate buffers as bits/8 bytes.","Trim leading zero bytes before passing buffers."],"tags":["node-compat","crypto","prime","buffer-overflow-limit","openssl"],"backgroundTag":"bignum-too-long","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}