normalizeSignature

Normalizes a DER-encoded secp256r1 signature to compact format with low-S normalization.

This function performs the following steps:

  1. Parses DER format via parseDerSignature, which returns R and S as BigIntegers

  2. Normalizes S to low-S form if S > halfOrder

  3. Pads both R and S to exactly 32 bytes

  4. Returns concatenated R || S (64 bytes total)

Low-S normalization ensures that s values greater than half the curve order are converted to their complements (n - s), which is required for Stellar/Soroban signature verification.

Return

Compact 64-byte signature (32-byte r || 32-byte s)

Parameters

derSignature

DER-encoded signature bytes

Throws

if the DER format is invalid

Example:

val derSig = byteArrayOf(...)  // DER-encoded signature from WebAuthn
val compactSig = SmartAccountUtils.normalizeSignature(derSig)
// compactSig is now 64 bytes: r (32 bytes) || s (32 bytes)