<?xml version="1.0" encoding="UTF-8"?>

<rfc
    version="3"
    category="exp"
    submissionType="independent"
    ipr="trust200902"
    docName="draft-dutta-gcmf-00"
    tocInclude="true"
    symRefs="true"
    sortRefs="true">

  <front>

    <title abbrev="GCMF">
      General-Purpose Compression via Mathematical Functions (GCMF)
    </title>

    <author
        fullname="Srijal Dutta"
        initials="S."
        surname="Dutta">
      <organization>The ZSMC Co.</organization>
      <address>
        <postal>
          <city>Gurugram</city>
          <region>Haryana</region>
          <country>India</country>
        </postal>
        <email>srijal@thezsmc.co</email>
      </address>
    </author>

    <date year="2026" month="08" day="29"/>

    <area>General</area>

    <workgroup>Independent Research</workgroup>

    <keyword>compression</keyword>
    <keyword>lossless compression</keyword>
    <keyword>mathematical functions</keyword>
    <keyword>GCMF</keyword>
    <keyword>data compression</keyword>

    <abstract>
      <t>
        This document specifies General-Purpose Compression via
        Mathematical Functions (GCMF), a lossless compression format
        that represents sequences of data using mathematical functions
        and associated parameters.
      </t>

      <t>
        GCMF attempts to represent a sequence using a compact mathematical
        representation rather than storing every value explicitly.
        This document defines the GCMF data format, function types,
        encoding rules, decoding procedure, and interoperability
        requirements.
      </t>
    </abstract>

  </front>

  <middle>

    <section anchor="introduction">
      <name>Introduction</name>

      <t>
        General-Purpose Compression via Mathematical Functions (GCMF)
        is a lossless compression method that represents data using
        mathematical functions.
      </t>

      <t>
        Instead of storing every value in a sequence explicitly, GCMF
        may store a function and a range over which the function can
        reproduce the original values.
      </t>

      <t>
        The objective of GCMF is to reduce the amount of information
        required to represent sequences that exhibit mathematical
        regularity, in the spirit of minimum-description-length
        approaches to modeling such as <xref target="J_RISSANEN_1978"/>.
      </t>

      <t>
        A GCMF encoder <bcp14>MUST</bcp14> evaluate a set of valid candidate
        representations supported by the implementation and
        <bcp14>SHOULD</bcp14> select the candidate having the minimum
        complete encoded length, within the closed interval
        [X_start, X_end], as described in <xref target="function-representation-model"/>.
      </t>

      <section anchor="terminology">
        <name>Terminology</name>

        <t>
          The key words <bcp14>MUST</bcp14>, <bcp14>MUST NOT</bcp14>,
          <bcp14>REQUIRED</bcp14>, <bcp14>SHALL</bcp14>,
          <bcp14>SHALL NOT</bcp14>, <bcp14>SHOULD</bcp14>,
          <bcp14>SHOULD NOT</bcp14>, <bcp14>RECOMMENDED</bcp14>,
          <bcp14>NOT RECOMMENDED</bcp14>, <bcp14>MAY</bcp14>, and
          <bcp14>OPTIONAL</bcp14> in this document are to be interpreted
          as described in <xref target="RFC2119"/>.
        </t>
      </section>

    </section>

    <section anchor="mime-and-extension">
      <name>File Extension and MIME Type</name>

      <t>
        GCMF currently utilizes a working MIME type of
        <tt>application/gcmf</tt>. This is not an officially registered
        MIME type under <xref target="RFC2046"/>, or under any current
        standard or working group as of the publication of this document.
        It is intended only to identify the file type to adopting
        implementations.
      </t>

      <t>
        GCMF also currently utilizes a working file extension of
        <tt>.gcmf</tt>. Until formal registration occurs, this extension
        <bcp14>SHOULD</bcp14> be treated with the same significance as
        the MIME type above.
      </t>
    </section>

    <section anchor="function-representation-model">
      <name>Function Representation Model</name>

      <t>
        GCMF represents a numerical data sequence as the evaluation of
        a mathematical function over a finite domain, optionally
        augmented by a residual function. The original sequence
        <bcp14>MUST</bcp14> be:
      </t>

      <artwork><![CDATA[
D = d0, d1, ..., d(n-1)
]]></artwork>

      <t>
        A GCMF encoder <bcp14>MUST</bcp14> attempt to determine a function
        (f), a domain sequence (X = x0, x1, ..., x(n-1)), and
        <bcp14>MAY</bcp14> also determine a residual function. For a
        generalized representation:
      </t>

      <artwork><![CDATA[
d(i) = f(x(i))
]]></artwork>

      <t>For an exact representation:</t>

      <artwork><![CDATA[
d(i) = f(x(i)) + r(x(i))
]]></artwork>

      <t>
        The exact representation is therefore a special case of the
        generalized representation for which:
      </t>

      <artwork><![CDATA[
r(x) = 0
]]></artwork>

      <t>
        Let C be the theoretical set of all valid GCMF candidate
        representations and let C_I be the subset of candidates
        generated by a particular implementation, where C_I is a
        subset of C. The encoder optimization objective is:
      </t>

      <artwork><![CDATA[
C*_I = arg min L(C), for C in C_I
]]></artwork>

      <t>
        where L(C) is the complete encoded length of candidate C in
        bytes.
      </t>

      <t>
        An implementation <bcp14>MAY</bcp14> use heuristic, statistical,
        symbolic, or other candidate-discovery methods. An implementation
        is not required to discover every mathematically valid
        representation.
      </t>

      <t>
        A candidate is considered compressive only when its complete
        encoded representation is smaller than the original input under
        the source-size comparison defined in <xref target="source-size-comparison"/>.
        The global optimization objective is:
      </t>

      <artwork><![CDATA[
C* = arg min L(C), for C in C
]]></artwork>

      <t>
        where C is the set of all valid GCMF candidate representations
        and L(C) is the number of bytes required to encode candidate C.
      </t>

      <section anchor="source-size-comparison">
        <name>Source Size Comparison</name>

        <t>
          For the purpose of determining whether a GCMF representation
          is compressive, the original input is considered as a sequence
          of N bytes and has a source size of N bytes.
        </t>

        <t>A GCMF representation is compressive if and only if:</t>

        <artwork><![CDATA[
L(C) < N
]]></artwork>

        <t>
          where L(C) is the complete physical size of the GCMF
          representation in bytes.
        </t>

        <t>
          All GCMF structural overhead <bcp14>MUST</bcp14> be included in
          L(C), including magic numbers, version information, function
          addresses, parameters, domains, residual data, padding, and
          all other encoded fields.
        </t>

        <t>
          An implementation <bcp14>MUST NOT</bcp14> claim compression
          solely because the mathematical description of the data is
          shorter than the original data.
        </t>
      </section>

    </section>

    <section anchor="binary-format">
      <name>GCMF Binary Format Specification</name>

      <t>
        This section defines the binary representation of GCMF. A
        conforming GCMF decoder <bcp14>MUST</bcp14> be capable of
        parsing all mandatory structure defined in this specification.
        Unless otherwise stated, all bit fields are packed from the
        most-significant bit to the least-significant bit.
      </t>

      <t>
        GCMF is a lossless representation format. A valid GCMF stream
        <bcp14>MUST</bcp14> decode to exactly the byte sequence
        represented by the encoded data.
      </t>

      <section anchor="file-structure">
        <name>File Structure</name>

        <t>
          A GCMF file consists of a fixed header followed by a
          representation-specific payload. The general structure is:
        </t>

        <artwork><![CDATA[
+--------------------------------+
| Magic               | 4 bytes  |
+--------------------------------+
| Version              | 1 byte  |
+--------------------------------+
| Function Address     | 5 bits  |
+--------------------------------+
| Representation Data  | variable|
+--------------------------------+
| Physical EOF                   |
+--------------------------------+
]]></artwork>

        <t>
          The Function Address determines the interpretation of
          Representation Data. For mathematical representations,
          Representation Data consists of the function definition or
          predefined-function parameters, X_start, X_end, BASE, and,
          when present, the residual representation.
        </t>

        <t>
          For LITERAL, Representation Data consists entirely of the
          original source bytes after the required alignment padding.
        </t>

        <t>
          Physical EOF is not encoded as a field or byte. The end of
          the physical GCMF stream terminates the final representation.
        </t>

        <t>
          The exact fields present after the function address depend on
          the selected function type. The LITERAL function type is an
          exception and terminates the structured portion of the stream.
        </t>
      </section>

      <section anchor="magic-number">
        <name>Magic Number</name>

        <t>
          Every GCMF file <bcp14>MUST</bcp14> begin with the following
          4 bytes: 0x47 0x43 0x4D 0x46. These bytes correspond to the
          ASCII string "GCMF". A decoder <bcp14>MUST</bcp14> reject a
          file whose first 4 bytes do not equal 0x47 0x43 0x4D 0x46.
        </t>
      </section>

      <section anchor="version">
        <name>Version</name>

        <t>
          Immediately following the magic number is an 8-bit unsigned
          version field. The version number assigned to this publication
          is Version 0, written as 0x00.
        </t>

        <t>
          Future versions <bcp14>MAY</bcp14> use additional version
          values. A decoder encountering an unsupported version
          <bcp14>MUST NOT</bcp14> attempt to interpret the remainder of
          the stream according to an unsupported specification.
        </t>
      </section>

      <section anchor="function-address">
        <name>Function Address</name>

        <t>The function address is a 5-bit field defined as:</t>

        <artwork><![CDATA[
+---+---------+
| 1 |    4    | BITS
+---+---------+
| R | A A A A |
+---+---------+
]]></artwork>

        <t>
          where R is the residual flag and AAAA is the function type
          address.
        </t>

        <section anchor="residual-flag">
          <name>Residual Flag</name>

          <t>
            The most-significant bit <bcp14>MUST</bcp14> be the residual
            flag, where 0 indicates no residual function is present and
            1 indicates a residual function is present.
          </t>

          <t>
            If R = 1, the residual representation immediately follows
            the primary mathematical representation according to the
            residual encoding rules in <xref target="residual-representation"/>.
          </t>
        </section>

        <section anchor="function-type-address">
          <name>Function Type Address</name>

          <t>
            LITERAL + RESIDUAL is reserved and <bcp14>MUST</bcp14> be
            rejected by a conforming decoder unless a future GCMF
            version explicitly defines semantics for it.
          </t>
        </section>
      </section>

      <section anchor="bit-packing">
        <name>Bit Packing</name>

        <t>
          The Function Address occupies exactly five bits and
          <bcp14>MUST NOT</bcp14> automatically consume a complete byte.
        </t>

        <t>
          The bit immediately following the Function Address is the
          first bit of the next field unless alignment is explicitly
          required by the representation.
        </t>

        <t>
          GCMF-VLI units <bcp14>MAY</bcp14> begin at any bit offset
          within a bit-packed structure. Implementations
          <bcp14>MUST</bcp14> therefore provide a bit-level reader and
          writer for structured GCMF fields.
        </t>

        <t>
          Where a field is explicitly required to be byte-aligned,
          zero-valued padding bits <bcp14>MUST</bcp14> be inserted
          before that field. Padding bits <bcp14>MUST NOT</bcp14> be
          interpreted as data and <bcp14>MUST</bcp14> be zero.
        </t>

        <t>
          Unless explicitly specified otherwise, no implicit alignment
          is performed.
        </t>
      </section>

      <section anchor="vli-encoding">
        <name>Variable Length Integer Encoding</name>

        <t>
          GCMF uses a variable-length integer representation called
          GCMF-VLI. Each encoded unit contains:
        </t>

        <artwork><![CDATA[
+---+-------+
| 1 |   7   | BITS
+---+-------+
| C | VALUE |
+---+-------+
]]></artwork>

        <t>
          where C = 1 indicates that another VLI unit follows, C = 0
          indicates the final VLI unit, and VALUE contains 7 bits of
          the integer.
        </t>

        <t>
          The first unit contains the most significant portion of the
          value. Therefore, GCMF-VLI is a big-endian base-128
          representation.
        </t>

        <t>
          A decoder <bcp14>MUST</bcp14> continue reading VLI units until
          a unit with C=0 is encountered. A VLI <bcp14>MUST NOT</bcp14>
          contain unnecessary leading zero payload groups.
        </t>
      </section>

      <section anchor="signed-integer-encoding">
        <name>Signed Integer Encoding</name>

        <t>
          Where a mathematical parameter may be negative, GCMF uses
          ZigZag encoding before GCMF-VLI encoding. For a signed
          integer n:
        </t>

        <artwork><![CDATA[
Z(n) = 2n,        n >= 0
Z(n) = -2n - 1,   n < 0
]]></artwork>

        <t>
          The resulting non-negative integer is then encoded using
          GCMF-VLI. This allows small negative and positive values to
          have similarly compact representations.
        </t>
      </section>

      <section anchor="custom-function">
        <name>CUSTOM Function</name>

        <t>A CUSTOM function has Function Type Address:</t>

        <artwork><![CDATA[
0000
]]></artwork>

        <t>or, when residual data is present:</t>

        <artwork><![CDATA[
10000
]]></artwork>

        <t>The structure is:</t>

        <artwork><![CDATA[
+-------------------------------------------+
| Magic              | 4 bytes (0x47434D46) |
+-------------------------------------------+
| Version             | 1 byte (0x00)       |
+-------------------------------------------+
| Function Address    | 5 bits              |
+-------------------------------------------+
| Function Length      | OPTIONAL VLI       |
+-------------------------------------------+
| Function Bytecode    | OPTIONAL, Function |
|                       | Length bits        |
+-------------------------------------------+
| X_start              | variable VLI       |
+-------------------------------------------+
| X_end                | variable VLI       |
+-------------------------------------------+
| Base                 | variable VLI       |
+-------------------------------------------+
| Magic                | 4 bytes (0x47434D46)|
+-------------------------------------------+
| EOF                                        |
+-------------------------------------------+
]]></artwork>

        <t>
          The Function Length specifies the number of bits occupied by
          the function bytecode, including opcode bits, operand bits,
          and the END instruction.
        </t>

        <t>
          The Function Length <bcp14>MUST</bcp14> be sufficient to
          contain a complete bytecode sequence ending in an END
          instruction.
        </t>

        <t>
          The decoder <bcp14>MUST NOT</bcp14> read beyond the declared
          function length while executing the function.
        </t>

        <t>
          Any unused bits between the final END instruction and the
          declared function boundary <bcp14>MUST</bcp14> be zero. A
          canonical encoder <bcp14>SHOULD</bcp14> emit no unused bits
          after END.
        </t>

        <t>
          The function bytecode <bcp14>MUST</bcp14> contain a valid END
          instruction within the specified function length.
        </t>
      </section>

      <section anchor="predefined-functions">
        <name>Predefined Functions</name>

        <t>
          A predefined function does not contain its mathematical
          definition in the file. The Function Address identifies the
          mathematical function through the GCMF predefined-function
          registry. The decoder obtains the function definition from
          the version-specific registry.
        </t>

        <t>A predefined function therefore has the general structure:</t>

        <artwork><![CDATA[
+-------------------------------------------+
| Magic              | 4 bytes (0x47434D46) |
+-------------------------------------------+
| Version             | 1 byte (0x00)       |
+-------------------------------------------+
| Function Address    | 5 bits              |
+-------------------------------------------+
| Function Parameters | variable            |
+-------------------------------------------+
| X_start              | variable VLI       |
+-------------------------------------------+
| X_end                | variable VLI       |
+-------------------------------------------+
| Base                 | variable VLI       |
+-------------------------------------------+
| Magic                | 4 bytes (0x47434D46)|
+-------------------------------------------+
| EOF                                        |
+-------------------------------------------+
]]></artwork>

        <t>
          Each predefined function <bcp14>MUST</bcp14> have a precisely
          defined parameter encoding.
        </t>

        <section anchor="predefined-parameter-encoding">
          <name>Predefined Function Parameter Encoding</name>

          <t>
            Unless otherwise specified, all integer parameters of
            predefined functions are encoded using GCMF-VLI.
            Parameters that may be negative <bcp14>MUST</bcp14> use
            ZigZag encoding before GCMF-VLI encoding.
          </t>

          <t>The predefined functions use the following parameter encodings:</t>

          <artwork><![CDATA[
CONSTANT:     c
LINEAR:       a, b
ARITHMETIC:   a, d
GEOMETRIC:    a, r
POLYNOMIAL:   n, a0, a1, ..., an
EXPONENTIAL:  a, b
LOGARITHMIC:  a, b, c
POWER:        a, b, c
MODULAR:      G(x), m
FIBONACCI:    no parameters
FACTORIAL:    no parameters
TRIANGULAR:   no parameters
SQUARE:       no parameters
CUBE:         no parameters
]]></artwork>

          <t>
            For MODULAR, G(x) is a mathematical function defined by a
            nested GCMF function representation and m is a positive
            integer modulus. The nested function G(x) <bcp14>MUST</bcp14>
            be encoded using the same function representation rules
            defined by this specification, excluding residual encoding.
          </t>

          <t>
            The modulus m <bcp14>MUST</bcp14> be encoded as an unsigned
            GCMF-VLI integer and <bcp14>MUST</bcp14> satisfy m &gt;= 1.
          </t>

          <t>
            For every domain value x, the decoder first evaluates G(x)
            and then computes:
          </t>

          <artwork><![CDATA[
F(x) = G(x) mod m
]]></artwork>

          <t>
            The result of the modulo operation <bcp14>MUST</bcp14> be
            the unique integer r satisfying 0 &lt;= r &lt; m, where
            G(x) = qm + r for some integer q.
          </t>

          <t>
            The MODULAR function <bcp14>MUST NOT</bcp14> contain a
            residual representation within its nested G(x) function.
            Recursive MODULAR functions <bcp14>MAY</bcp14> be permitted,
            provided that the implementation-defined maximum nesting
            depth is not exceeded.
          </t>

          <t>
            A decoder <bcp14>MUST</bcp14> reject a MODULAR
            representation if G(x) is invalid, m is zero, or evaluation
            of G(x) produces a value outside the supported numerical
            domain.
          </t>

          <t>
            All parameters <bcp14>MUST</bcp14> appear in the order
            specified above. A decoder <bcp14>MUST</bcp14> reject a
            representation containing missing, malformed, or unexpected
            parameter data.
          </t>
        </section>
      </section>

      <section anchor="function-bytecode">
        <name>Function Bytecode</name>

        <t>
          GCMF mathematical functions use a stack-based bytecode. Each
          opcode occupies exactly five bits.
        </t>

        <t>
          The opcode is immediately followed by its operand, if the
          opcode requires one. No implicit byte alignment is performed
          between an opcode and its operand.
        </t>

        <t>
          For CONST, the operand is a signed GCMF-VLI value. For
          instructions that do not require an operand, no operand bits
          are present.
        </t>

        <t>
          The Function Length counts every bit occupied by the
          bytecode, including five-bit opcodes and all operand bits. A
          decoder <bcp14>MUST NOT</bcp14> read beyond the declared
          Function Length while decoding instructions.
        </t>

        <t>
          The bytecode <bcp14>MUST</bcp14> contain exactly one
          terminating END instruction as its logical terminator. A
          canonical bytecode representation <bcp14>MUST NOT</bcp14>
          contain instructions after END.
        </t>

        <t>
          Because opcodes are bit-packed, an operand begins at the bit
          immediately following the opcode.
        </t>
      </section>

      <section anchor="stack-machine-requirements">
        <name>Stack Machine Requirements</name>

        <t>
          A custom function is evaluated independently for every value
          of x. Before evaluating a new x, the evaluation stack
          <bcp14>MUST</bcp14> be empty. Each instruction operates on
          the stack.
        </t>

        <t>For binary operators: [a, b] becomes [a OP b].</t>
        <t>For unary operators: [a] becomes [OP(a)].</t>
        <t>DUP transforms: [a] becomes [a, a].</t>
        <t>X pushes the current domain value.</t>
        <t>CONST pushes its encoded constant.</t>
        <t>END terminates execution.</t>

        <t>
          At END, the stack <bcp14>MUST</bcp14> contain exactly one
          value. That value is the output of the function.
        </t>

        <t>
          A function that underflows the stack; executes an invalid
          operation; reaches the declared function boundary without
          END; contains an invalid operand; or terminates with anything
          other than exactly one stack value <bcp14>MUST</bcp14> be
          considered invalid.
        </t>

        <section anchor="numerical-evaluation">
          <name>Numerical Evaluation</name>

          <t>
            GCMF v1 mathematical evaluation <bcp14>MUST</bcp14> use
            exact integer arithmetic unless a specific operation is
            explicitly defined to use another numerical domain.
          </t>

          <t>
            An operation whose mathematical result is undefined within
            the applicable numerical domain <bcp14>MUST</bcp14> cause
            decoding to fail. Examples include division by zero,
            logarithms outside their valid domain, square roots of
            invalid values, and other undefined operations.
          </t>

          <t>
            A decoder <bcp14>MUST</bcp14> reject a representation if
            evaluation produces a value that cannot be represented by
            the numerical domain defined for the representation.
          </t>

          <t>
            Implementations <bcp14>MUST NOT</bcp14> silently round,
            truncate, saturate, wrap, or otherwise alter a mathematical
            result unless such behavior is explicitly defined by the
            applicable GCMF version.
          </t>

          <t>
            All conforming implementations of the same GCMF version
            <bcp14>MUST</bcp14> produce the same mathematical result
            for the same valid input.
          </t>
        </section>
      </section>

      <section anchor="example-custom-function">
        <name>Example Custom Function</name>

        <t>The function f(x) = 3x + 10 can be represented as:</t>

        <artwork><![CDATA[
CONST 3 X MUL CONST 10 ADD END
]]></artwork>

        <t>The logical execution is:</t>

        <artwork><![CDATA[
CONST 3   -> [3]
X         -> [3, x]
MUL       -> [3x]
CONST 10  -> [3x, 10]
ADD       -> [3x + 10]
END       -> result
]]></artwork>
      </section>

      <section anchor="xstart-xend">
        <name>X_start and X_end</name>

        <t>
          For all non-LITERAL representations, the domain is specified
          using two unsigned GCMF-VLI fields:
        </t>

        <artwork><![CDATA[
+----------------+
| X_START        | GCMF-VLI
+----------------+
| X_END          | GCMF-VLI
+----------------+
]]></artwork>

        <t>
          X_start and X_end <bcp14>MUST</bcp14> be non-negative
          integers. The default domain is the integer sequence:
        </t>

        <artwork><![CDATA[
X = X_start, X_start + 1, ..., X_end
]]></artwork>

        <t>The number of generated values is:</t>

        <artwork><![CDATA[
N = X_end - X_start + 1
]]></artwork>

        <t>
          X_end <bcp14>MUST</bcp14> be greater than or equal to
          X_start. A decoder <bcp14>MUST</bcp14> reject a representation
          whose domain would require an invalid, negative, or
          implementation-prohibited number of values.
        </t>

        <t>
          The number of generated values <bcp14>MUST</bcp14> also be
          consistent with the number of source values represented by
          the GCMF stream.
        </t>
      </section>

      <section anchor="base-encoding">
        <name>Base Encoding</name>

        <t>
          The BASE field specifies the numerical radix associated with
          the domain representation. BASE is encoded as an unsigned
          GCMF-VLI integer. The base <bcp14>MUST</bcp14> be greater
          than or equal to 2.
        </t>

        <t>
          A conforming implementation <bcp14>SHOULD</bcp14> support at
          least bases 2 through 36.
        </t>

        <t>
          The BASE field does not alter the binary encoding of
          GCMF-VLI values, function opcodes, function bytecode, or
          other binary fields.
        </t>
      </section>

      <section anchor="base-interpretation">
        <name>Base Interpretation</name>

        <t>
          BASE specifies the radix used for the interpretation of
          X_start and X_end when a representation requires
          base-dependent numerical interpretation.
        </t>

        <t>
          The mathematical values represented by X_start and X_end are
          independent of the selected radix. For example, a domain
          value of decimal 16 has the same mathematical value regardless
          of whether BASE is 10 or 16.
        </t>

        <t>
          BASE therefore <bcp14>MUST NOT</bcp14> change the numerical
          value of X_start or X_end.
        </t>

        <t>
          An encoder <bcp14>MAY</bcp14> evaluate multiple BASE values
          when selecting a representation if the selected base affects
          the representation length of the domain under the encoding
          rules defined by this specification.
        </t>

        <t>
          If BASE does not affect the encoded length or interpretation
          of a candidate, changing BASE <bcp14>MUST NOT</bcp14> be used
          as a basis for selecting that candidate.
        </t>
      </section>

      <section anchor="residual-representation">
        <name>Residual Representation</name>

        <t>
          If the Residual Flag is 1, a residual representation follows
          the primary function representation. The residual represents
          the difference between the original datum and the primary
          mathematical output. For every value:
        </t>

        <artwork><![CDATA[
r(i) = d(i) - f(x(i))
]]></artwork>

        <t>The original datum is reconstructed using:</t>

        <artwork><![CDATA[
d(i) = f(x(i)) + r(i)
]]></artwork>

        <t>
          The residual representation <bcp14>MUST</bcp14> itself be
          encoded using a valid GCMF function representation.
          Therefore a residual <bcp14>MAY</bcp14> be a predefined
          function, a custom function, or another mathematically
          represented sequence, subject to the restrictions imposed by
          the GCMF version.
        </t>

        <t>
          The residual representation does not independently require a
          second Function Address byte unless specified by the residual
          container structure.
        </t>
      </section>

      <section anchor="residual-function-container">
        <name>Residual Function Container</name>

        <t>
          When R = 1, the residual representation immediately follows
          the complete primary mathematical representation. The
          residual section is encoded as:
        </t>

        <artwork><![CDATA[
+-----------------------------+
| Residual Function Type | 4 bits    |
+-----------------------------+
| Residual Function Data | variable  |
+-----------------------------+
]]></artwork>

        <t>
          The Residual Function Type uses the same function-type
          address table as the primary function, excluding the
          residual flag. The residual function therefore contains
          exactly four function-type bits.
        </t>

        <t>
          For a custom residual, 0000 is used as the Residual Function
          Type. A custom residual <bcp14>MUST</bcp14> contain its
          Function Length followed by its Function Bytecode according
          to the CUSTOM function encoding rules.
        </t>

        <t>
          A predefined residual <bcp14>MUST</bcp14> contain the
          parameter fields specified for its function type.
        </t>

        <t>
          The residual function <bcp14>MUST NOT</bcp14> contain a
          residual flag and <bcp14>MUST NOT</bcp14> contain another
          residual representation. Recursive residual nesting is
          therefore prohibited in GCMF v1.
        </t>

        <t>
          The residual representation <bcp14>MUST</bcp14> generate
          exactly the same number of values as the primary
          representation.
        </t>
      </section>

      <section anchor="literal">
        <name>Literal</name>

        <t>The LITERAL function has Function Type Address:</t>

        <artwork><![CDATA[
0001
]]></artwork>

        <t>
          and <bcp14>MUST NOT</bcp14> have the Residual Flag set. Its
          complete Function Address is therefore:
        </t>

        <artwork><![CDATA[
00001
]]></artwork>

        <t>
          LITERAL is a terminal representation. Immediately following
          the Function Address, the decoder <bcp14>MUST</bcp14> consume
          three zero-valued padding bits. These three padding bits
          align the stream to the next byte boundary. The remainder of
          the GCMF stream is then interpreted as raw source bytes.
        </t>

        <artwork><![CDATA[
+----------------------+
| Magic                |
+----------------------+
| Version               |
+----------------------+
| Function Address      |
+----------------------+
| Raw source bytes      |
| Raw source bytes      |
| Raw source bytes      |
| ...                    |
+----------------------+
| EOF                    |
+----------------------+
]]></artwork>

        <t>
          No function length is stored. No X_start is stored. No X_end
          is stored. No BASE field is stored. No residual representation
          is stored.
        </t>

        <t>
          The decoder <bcp14>MUST</bcp14> copy the literal payload
          directly to the output without interpretation or
          transformation.
        </t>
      </section>

      <section anchor="physical-eof">
        <name>Physical End Of File</name>

        <t>
          The physical end of the GCMF file is the termination
          condition for the final field. No EOF byte or other in-band
          EOF marker is defined by GCMF.
        </t>

        <t>
          This is particularly important for LITERAL, because every
          byte value from 0x00 through 0xFF is valid source data.
          Consequently, no byte value <bcp14>MAY</bcp14> be reserved as
          an EOF marker.
        </t>

        <t>For LITERAL, EOF is the physical end of the GCMF stream. The final byte of the file is therefore the final literal source byte.</t>

        <t>
          A transport or container system embedding GCMF
          <bcp14>MUST</bcp14> preserve the exact file length.
        </t>
      </section>

      <section anchor="canonical-layout">
        <name>Complete Canonical Layout</name>

        <t>For a normal mathematical representation without residual data:</t>

        <artwork><![CDATA[
GCMF
VERSION
FUNCTION ADDRESS
[FUNCTION LENGTH]
[FUNCTION BYTECODE]
[FUNCTION PARAMETERS]
X_START
X_END
BASE
EOF
]]></artwork>

        <t>For a representation containing a residual:</t>

        <artwork><![CDATA[
GCMF
VERSION
FUNCTION ADDRESS
[FUNCTION LENGTH]
[FUNCTION BYTECODE]
[FUNCTION PARAMETERS]
X_START
X_END
BASE
RESIDUAL FUNCTION
RESIDUAL DATA
EOF
]]></artwork>

        <t>For LITERAL:</t>

        <artwork><![CDATA[
GCMF
VERSION
00001
RAW DATA
EOF
]]></artwork>
      </section>

      <section anchor="encoding-example">
        <name>Encoding Example</name>

        <t>Consider the sequence:</t>

        <artwork><![CDATA[
10, 13, 16, 19, 22, 25
]]></artwork>

        <t>It can be represented by the linear function f(x) = 3x + 10 over the domain X = 0, 1, 2, 3, 4, 5. The logical GCMF representation is therefore:</t>

        <artwork><![CDATA[
MAGIC
VERSION
FUNCTION ADDRESS = LINEAR
a = 3
b = 10
X_START = 0
X_END = 5
BASE
PHYSICAL EOF
]]></artwork>

        <t>The decoder evaluates:</t>

        <artwork><![CDATA[
f(0) = 10
f(1) = 13
f(2) = 16
f(3) = 19
f(4) = 22
f(5) = 25
]]></artwork>

        <t>The resulting sequence exactly reproduces the source sequence.</t>
      </section>

      <section anchor="encoder-selection-algorithm">
        <name>Encoder Selection Algorithm</name>

        <t>
          The encoder <bcp14>SHOULD</bcp14> construct multiple candidate
          representations. Candidates <bcp14>MAY</bcp14> differ in
          function type, function parameters, custom bytecode, domain,
          numerical base, or residual representation.
        </t>

        <t>For every candidate C, the encoder computes L(C), the total encoded length in bytes. The encoder selects:</t>

        <artwork><![CDATA[
C* = arg min L(C)
]]></artwork>

        <t>
          The encoder <bcp14>MUST</bcp14> include all structural
          overhead in this calculation. Therefore the cost of a
          candidate includes magic, version, function address, function
          length where applicable, function bytecode, parameters,
          domain, base, residual data, and any required alignment.
        </t>

        <t>
          The encoder <bcp14>MUST NOT</bcp14> select a mathematical
          representation merely because the function itself is shorter.
        </t>
      </section>

      <section anchor="literal-selection">
        <name>Literal Selection</name>

        <t>
          The encoder <bcp14>MUST</bcp14> be permitted to select
          LITERAL when no mathematical representation provides
          sufficient compression.
        </t>

        <t>
          The encoder <bcp14>SHOULD</bcp14> compare the best
          mathematical candidate against the LITERAL representation:
        </t>

        <artwork><![CDATA[
arg min (L(C_best_math), L(C_literal))
]]></artwork>

        <t>
          This prevents the creation of deliberately inflated
          mathematical representations for incompressible data.
        </t>
      </section>

      <section anchor="decoder-state-machine">
        <name>Decoder State Machine</name>

        <t>A conforming decoder can conceptually operate as follows:</t>

        <artwork><![CDATA[
START
  |
  v
READ MAGIC
  |
  v
READ VERSION
  |
  v
READ FUNCTION ADDRESS
  |
  +-----> LITERAL -----> COPY REMAINDER -----> END
  |
  v
READ FUNCTION
  |
  v
READ PARAMETERS
  |
  v
READ X_start
  |
  v
READ X_end
  |
  v
READ BASE
  |
  +-----> R = 0 -----> EVALUATE
  |
  +-----> R = 1 -----> READ RESIDUAL
                          |
                          v
                     EVALUATE RESIDUAL
                          |
                          v
                     RECONSTRUCT
                          |
                          v
                         END
]]></artwork>
      </section>

      <section anchor="decoder-validation">
        <name>Decoder Validation</name>

        <t>A decoder <bcp14>MUST</bcp14> reject a GCMF stream if any of the following occurs:</t>

        <ul>
          <li>The magic number is invalid.</li>
          <li>The version is unsupported.</li>
          <li>The Function Address is invalid.</li>
          <li>A required VLI is truncated.</li>
          <li>A VLI is malformed.</li>
          <li>A custom function exceeds its declared length.</li>
          <li>A custom function lacks a valid END.</li>
          <li>A function causes stack underflow.</li>
          <li>A function terminates with an invalid stack state.</li>
          <li>X_end is less than X_start.</li>
          <li>BASE is less than 2.</li>
          <li>A required function parameter is missing.</li>
          <li>A residual representation is malformed.</li>
          <li>A prohibited recursive residual is encountered.</li>
          <li>The mathematical evaluation produces a value that cannot be represented by the selected numerical representation.</li>
          <li>The stream terminates before all mandatory fields are read.</li>
        </ul>

        <t>A decoder <bcp14>SHOULD</bcp14> also impose implementation-defined limits on:</t>

        <ul>
          <li>maximum function length;</li>
          <li>maximum domain size;</li>
          <li>maximum stack depth;</li>
          <li>maximum numerical magnitude;</li>
          <li>maximum execution time.</li>
        </ul>

        <t>
          Such limits are important because GCMF functions are
          executable mathematical expressions and should not be
          permitted to consume unbounded computational resources.
        </t>
      </section>

      <section anchor="canonical-encoding">
        <name>Canonical Encoding</name>

        <t>A GCMF encoder <bcp14>SHOULD</bcp14> produce canonical encodings. A canonical representation <bcp14>MUST</bcp14>:</t>

        <ul>
          <li>use the shortest valid GCMF-VLI representation;</li>
          <li>avoid unnecessary leading VLI groups;</li>
          <li>use the minimum required function length;</li>
          <li>terminate bytecode with exactly one END;</li>
          <li>avoid unnecessary residual representations;</li>
          <li>avoid unnecessary domain extension;</li>
          <li>avoid unnecessary padding;</li>
          <li>select the smallest valid complete representation when deterministic encoding is required.</li>
        </ul>

        <t>
          Canonicalization permits byte-for-byte comparison of
          equivalent GCMF representations.
        </t>
      </section>

      <section anchor="versioning">
        <name>Versioning</name>

        <t>The version byte identifies the binary interpretation rules. A future version <bcp14>MAY</bcp14>:</t>

        <ul>
          <li>add predefined functions;</li>
          <li>add bytecode instructions;</li>
          <li>modify residual semantics;</li>
          <li>add new representation modes;</li>
          <li>define previously reserved fields.</li>
        </ul>

        <t>
          Existing function addresses <bcp14>MUST NOT</bcp14> be
          silently reassigned within a compatible version. A new
          incompatible interpretation <bcp14>MUST</bcp14> use a new
          version number.
        </t>
      </section>

      <section anchor="reserved-space">
        <name>Reserved Space</name>

        <t>
          The initial GCMF function address space has no unused
          base-function address because all 16 four-bit values are
          assigned. Future extensions <bcp14>SHOULD</bcp14> therefore
          use one of the following mechanisms:
        </t>

        <ul>
          <li>a new GCMF version;</li>
          <li>extension instructions;</li>
          <li>additional extension containers;</li>
          <li>a future extension mechanism explicitly defined by the specification.</li>
        </ul>

        <t>
          The five-bit address space itself <bcp14>MUST NOT</bcp14> be
          silently expanded within GCMF v1. The bytecode instruction
          space is similarly fully allocated.
        </t>
      </section>

      <section anchor="losslessness-requirement">
        <name>Losslessness Requirement</name>

        <t>For every valid GCMF representation G, decoding <bcp14>MUST</bcp14> satisfy:</t>

        <artwork><![CDATA[
Decode(G) = D
]]></artwork>

        <t>where D is the exact original input byte sequence. For mathematical representations:</t>

        <artwork><![CDATA[
d(i) = f(x(i)) + r(x(i))
]]></artwork>

        <t>For exact mathematical representations, r(x(i)) = 0. For LITERAL:</t>

        <artwork><![CDATA[
Decode(G) = LiteralPayload(G)
]]></artwork>

        <t>
          No rounding, approximation, or numerical loss is permitted
          unless explicitly introduced by a future GCMF version.
        </t>
      </section>

      <section anchor="binary-grammar-summary">
        <name>Summary of Binary Grammar</name>

        <t>The logical grammar of GCMF v1 is:</t>

        <artwork><![CDATA[
GCMF_FILE :=
    MAGIC
    VERSION
    FUNCTION_ADDRESS
    REPRESENTATION

REPRESENTATION :=
      LITERAL_REPRESENTATION
    | MATHEMATICAL_REPRESENTATION

LITERAL_REPRESENTATION :=
    LITERAL_FUNCTION_ADDRESS
    ZERO_PADDING_3_BITS
    RAW_BYTES

MATHEMATICAL_REPRESENTATION :=
    FUNCTION
    X_START
    X_END
    BASE
    [RESIDUAL]

FUNCTION :=
      CUSTOM_FUNCTION
    | PREDEFINED_FUNCTION

CUSTOM_FUNCTION :=
    FUNCTION_LENGTH
    FUNCTION_BYTECODE

PREDEFINED_FUNCTION :=
    FUNCTION_PARAMETERS

RESIDUAL :=
    RESIDUAL_FUNCTION_TYPE
    RESIDUAL_FUNCTION_DATA

CUSTOM_RESIDUAL :=
    0000
    FUNCTION_LENGTH
    FUNCTION_BYTECODE

PHYSICAL_EOF :=
    END_OF_PHYSICAL_GCMF_STREAM
]]></artwork>

        <t>The fundamental mathematical decoding equation is:</t>

        <artwork><![CDATA[
d(i) = f(x(i)) + r(x(i))
]]></artwork>

        <t>
          where r(x(i)) = 0 for representations without residuals. For
          LITERAL, Decode(G) = LiteralPayload(G). No additional EOF
          field is encoded.
        </t>
      </section>

    </section>

    <section anchor="iana-considerations">
      <name>IANA Considerations</name>

      <t>
        This document defines a working, unregistered MIME type,
        <tt>application/gcmf</tt>, as described in <xref target="mime-and-extension"/>.
        This document does not request any registration action from
        IANA at this time. Should GCMF advance toward broader adoption,
        a future document <bcp14>MAY</bcp14> request registration of
        <tt>application/gcmf</tt> in accordance with the procedures of
        <xref target="RFC2046"/> and BCP 13.
      </t>
    </section>

    <section anchor="security-considerations">
      <name>Security Considerations</name>

      <t>
        GCMF CUSTOM function representations embed executable
        stack-based bytecode within the file format, as described in
        <xref target="function-bytecode"/>. A decoder that evaluates
        untrusted GCMF input therefore executes untrusted, attacker-
        supplied instructions. Implementations <bcp14>MUST</bcp14>
        treat GCMF decoding of CUSTOM and MODULAR functions as
        execution of untrusted code and <bcp14>SHOULD</bcp14> apply
        appropriate sandboxing.
      </t>

      <t>
        Because GCMF functions are evaluated over a domain that can be
        specified independently of the resulting output size, a
        maliciously crafted file can specify an extremely large
        [X_start, X_end] domain, or a MODULAR or POLYNOMIAL function
        with a large nesting or degree, in order to force a decoder to
        perform an excessive amount of computation or produce an
        excessive volume of output from a very small input file. This
        is a form of decompression-bomb risk analogous to those found
        in other compression formats. Implementations <bcp14>SHOULD</bcp14>
        impose configurable limits on maximum domain size, maximum
        function length, maximum stack depth, maximum recursive
        MODULAR nesting depth, maximum numerical magnitude, and maximum
        decode execution time, as noted in <xref target="decoder-validation"/>.
      </t>

      <t>
        Certain bytecode operations, such as DIV, MOD, LOG, LOG10, and
        SQRT, are undefined for some operand values (for example,
        division by zero or the logarithm of a non-positive number).
        Implementations <bcp14>MUST</bcp14> reject such evaluations
        rather than substituting an implementation-defined or
        platform-defined result, as required in <xref target="numerical-evaluation"/>,
        since inconsistent handling of undefined operations across
        implementations could otherwise be leveraged to produce
        divergent decoded output from the same input.
      </t>

      <t>
        Because GCMF-VLI and the bit-packed Function Address are
        variable-width fields that need not be byte-aligned, a
        non-conforming or naive decoder implementation is at
        heightened risk of buffer over-read or integer overflow when
        parsing malformed or truncated input. Implementations
        <bcp14>MUST</bcp14> validate that sufficient input remains
        before consuming any field and <bcp14>MUST</bcp14> reject
        truncated streams as described in <xref target="decoder-validation"/>.
      </t>

      <t>
        GCMF does not itself provide authentication, integrity
        protection, or confidentiality for encoded data. Applications
        that require these properties for GCMF streams transmitted or
        stored in untrusted environments <bcp14>SHOULD</bcp14> apply
        them at another layer.
      </t>
    </section>

  </middle>

  <back>

    <references anchor="normative-references">
      <name>Normative References</name>

      <reference anchor="RFC2119"
                 target="https://www.rfc-editor.org/rfc/rfc2119">
        <front>
          <title>
            Key words for use in RFCs to Indicate Requirement Levels
          </title>

          <author initials="S."
                  surname="Bradner"
                  fullname="Scott Bradner"/>

          <date month="March" year="1997"/>
        </front>

        <seriesInfo name="RFC" value="2119"/>
        <seriesInfo name="DOI" value="10.17487/RFC2119"/>
      </reference>

      <reference anchor="RFC2046"
                 target="https://www.rfc-editor.org/rfc/rfc2046">
        <front>
          <title>
            Multipurpose Internet Mail Extensions (MIME) Part Two: Media Types
          </title>

          <author initials="N."
                  surname="Freed"
                  fullname="Ned Freed"/>

          <author initials="N."
                  surname="Borenstein"
                  fullname="Nathaniel S. Borenstein"/>

          <date month="November" year="1996"/>
        </front>

        <seriesInfo name="RFC" value="2046"/>
        <seriesInfo name="DOI" value="10.17487/RFC2046"/>
      </reference>

    </references>

    <references anchor="informative-references">
      <name>Informative References</name>

      <reference anchor="J_RISSANEN_1978">
        <front>
          <title>
            Modeling by shortest data description
          </title>

          <author initials="J."
                  fullname="Jorma Rissanen"
                  surname="Rissanen">
          </author>
          <date year="1978"/>
        </front>

        <refcontent>Automatica, vol. 14, no. 5, pp. 465-471</refcontent>
      </reference>

    </references>

    <section anchor="appendix-example" numbered="false">
      <name>Appendix A. Example GCMF Representation</name>

      <t>
        This appendix reproduces, for reference, the worked example of
        <xref target="encoding-example"/> in a single consolidated
        view.
      </t>

      <t>
        Given the source sequence 10, 13, 16, 19, 22, 25 (six 8-bit
        unsigned values, an 6-byte source), an encoder implementing
        this specification could produce a LINEAR representation with
        a = 3, b = 10, X_start = 0, X_end = 5, discovering that:
      </t>

      <artwork><![CDATA[
f(x) = 3x + 10

f(0) = 10
f(1) = 13
f(2) = 16
f(3) = 19
f(4) = 22
f(5) = 25
]]></artwork>

      <t>
        which exactly reproduces the six-value source sequence with an
        exact (residual-free) representation, per
        <xref target="function-representation-model"/>. Per
        <xref target="encoder-selection-algorithm"/>, the encoder
        would compare the total encoded length of this LINEAR
        representation, including all structural overhead, against the
        LITERAL representation of the same six bytes, and select
        whichever candidate yields the smaller value of L(C).
      </t>
    </section>

  </back>

</rfc>