public static function integerToOctetString(Integer $num, $mlen = null): OctetString
    {
        $gmp = gmp_init($num->number(), 10);
        $str = gmp_export($gmp, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN);
        if (null !== $mlen) {
            $len = strlen($str);
            if ($len > $mlen) {
                throw new \RangeException("Number is too large.");
            }
            // pad with zeroes
            if ($len < $mlen) {
                $str = str_repeat("\0", $mlen - $len) . $str;
            }
        }
        return new OctetString($str);
    }