grafana/k6 · error

public exponent too large

Error message

public exponent too large

What it means

Thrown by crypto.subtle.generateKey for RSA algorithms when publicExponent exceeds 2^31-1. validatePublicExponent (internal/js/modules/k6/webcrypto/rsa.go:154) caps the exponent at the maximum signed 32-bit int, matching Go's rsa package limits; larger values are rejected before key generation.

Solutions

  1. Use the standard exponent 65537: new Uint8Array([0x01, 0x00, 0x01])
  2. Trim leading zero bytes from fixed-width exponent buffers before passing them
  3. Validate the decoded exponent value fits 2 <= e <= 2^31-1 before generateKey

Example fix

// before
publicExponent: new Uint8Array([0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00]) // decoded value overflows int32

// after
publicExponent: new Uint8Array([0x01, 0x00, 0x01]) // 65537
Defensive patterns

Strategy: validation

Validate before calling

const e = publicExponent.reduce((acc, b) => acc * 256 + b, 0);
if (e > 2 ** 31 - 1) {
  throw new Error(`publicExponent ${e} exceeds the 2^31-1 limit — check for untrimmed or misaligned bytes`);
}

Prevention

When it happens

Trigger: Passing publicExponent as a big-endian byte array longer than 4 bytes with the high bits set, e.g. new Uint8Array([0x7F, 0xFF, 0xFF, 0xFF, 0xFF]) or new Uint8Array(8) filled with 0xFF, in the generateKey algorithm identifier.

Common situations: Concatenating the exponent onto the wrong end of the modulus buffer; a config value stored as a 64-bit integer; porting a test vector that encodes the exponent in a fixed-width 8-byte field without trimming leading zero bytes — note [0x00, 0x01, 0x00, 0x01] trims fine but [0xFF x8] does not.

Related errors


AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18). Data as JSON: /api/errors/149a7f44987e5bd3. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/webcrypto/rsa.go:154

	return &CryptoKeyPair{
		PrivateKey: privateKey,
		PublicKey:  publicKey,
	}, nil
}

// validatePublicExponent validates the public exponent.
// it's done same way how golang's rsa package does it + additional check for evenness.
func validatePublicExponent(e int) error {
	if e%2 == 0 {
		return errors.New("public exponent is even")
	}

	if e < 2 {
		return errors.New("public exponent too small")
	}
	if e > 1<<31-1 {
		return errors.New("public exponent too large")
	}

	return nil
}

func generateRSAKeyPair(
	modulusLength int,
	publicExponent int,
) (any, any, error) {
	privateKey, err := rsa.GenerateKey(rand.Reader, modulusLength)
	if err != nil {
		return nil, nil, NewError(OperationError, "could not generate RSA key pair")
	}

	privateKey.E = publicExponent

	// validate the key pair, since we are setting the public exponent manually
	if err := privateKey.Validate(); err != nil {

View on GitHub (pinned to 01ffac6f24)