MCPcopy Create free account
hub / github.com/ElementsProject/elements / PolyMod

Function PolyMod

src/blech32.cpp:62–119  ·  view source on GitHub ↗

This function will compute what 6 5-bit values to XOR into the last 6 input values, in order to * make the checksum 0. These 6 values are packed together in a single 30-bit integer. The higher * bits correspond to earlier values. */

Source from the content-addressed store, hash-verified

60 * make the checksum 0. These 6 values are packed together in a single 30-bit integer. The higher
61 * bits correspond to earlier values. */
62uint64_t PolyMod(const data& v)
63{
64 // The input is interpreted as a list of coefficients of a polynomial over F = GF(32), with an
65 // implicit 1 in front. If the input is [v0,v1,v2,v3,v4], that polynomial is v(x) =
66 // 1*x^5 + v0*x^4 + v1*x^3 + v2*x^2 + v3*x + v4. The implicit 1 guarantees that
67 // [v0,v1,v2,...] has a distinct checksum from [0,v0,v1,v2,...].
68
69 // The output is a 30-bit integer whose 5-bit groups are the coefficients of the remainder of
70 // v(x) mod g(x), where g(x) is the Blech32 generator,
71 // x^6 + {29}x^5 + {22}x^4 + {20}x^3 + {21}x^2 + {29}x + {18}. g(x) is chosen in such a way
72 // that the resulting code is a BCH code, guaranteeing detection of up to 3 errors within a
73 // window of 1023 characters. Among the various possible BCH codes, one was selected to in
74 // fact guarantee detection of up to 4 errors within a window of 89 characters.
75
76 // Note that the coefficients are elements of GF(32), here represented as decimal numbers
77 // between {}. In this finite field, addition is just XOR of the corresponding numbers. For
78 // example, {27} + {13} = {27 ^ 13} = {22}. Multiplication is more complicated, and requires
79 // treating the bits of values themselves as coefficients of a polynomial over a smaller field,
80 // GF(2), and multiplying those polynomials mod a^5 + a^3 + 1. For example, {5} * {26} =
81 // (a^2 + 1) * (a^4 + a^3 + a) = (a^4 + a^3 + a) * a^2 + (a^4 + a^3 + a) = a^6 + a^5 + a^4 + a
82 // = a^3 + 1 (mod a^5 + a^3 + 1) = {9}.
83
84 // During the course of the loop below, `c` contains the bitpacked coefficients of the
85 // polynomial constructed from just the values of v that were processed so far, mod g(x). In
86 // the above example, `c` initially corresponds to 1 mod g(x), and after processing 2 inputs of
87 // v, it corresponds to x^2 + v0*x + v1 mod g(x). As 1 mod g(x) = 1, that is the starting value
88 // for `c`.
89 uint64_t c = 1;
90 for (const auto v_i : v) {
91 // We want to update `c` to correspond to a polynomial with one extra term. If the initial
92 // value of `c` consists of the coefficients of c(x) = f(x) mod g(x), we modify it to
93 // correspond to c'(x) = (f(x) * x + v_i) mod g(x), where v_i is the next input to
94 // process. Simplifying:
95 // c'(x) = (f(x) * x + v_i) mod g(x)
96 // ((f(x) mod g(x)) * x + v_i) mod g(x)
97 // (c(x) * x + v_i) mod g(x)
98 // If c(x) = c0*x^5 + c1*x^4 + c2*x^3 + c3*x^2 + c4*x + c5, we want to compute
99 // c'(x) = (c0*x^5 + c1*x^4 + c2*x^3 + c3*x^2 + c4*x + c5) * x + v_i mod g(x)
100 // = c0*x^6 + c1*x^5 + c2*x^4 + c3*x^3 + c4*x^2 + c5*x + v_i mod g(x)
101 // = c0*(x^6 mod g(x)) + c1*x^5 + c2*x^4 + c3*x^3 + c4*x^2 + c5*x + v_i
102 // If we call (x^6 mod g(x)) = k(x), this can be written as
103 // c'(x) = (c1*x^5 + c2*x^4 + c3*x^3 + c4*x^2 + c5*x + v_i) + c0*k(x)
104
105 // First, determine the value of c0:
106 uint8_t c0 = c >> 55; // ELEMENTS: 25->55
107
108 // Then compute c1*x^5 + c2*x^4 + c3*x^3 + c4*x^2 + c5*x + v_i:
109 c = ((c & 0x7fffffffffffff) << 5) ^ v_i; // ELEMENTS 0x1ffffff->0x7fffffffffffff
110
111 // Finally, for each set bit n in c0, conditionally add {2^n}k(x):
112 if (c0 & 1) c ^= 0x7d52fba40bd886; // ELEMENTS
113 if (c0 & 2) c ^= 0x5e8dbf1a03950c; // ELEMENTS
114 if (c0 & 4) c ^= 0x1c3a3c74072a18; // ELEMENTS
115 if (c0 & 8) c ^= 0x385d72fa0e5139; // ELEMENTS
116 if (c0 & 16) c ^= 0x7093e5a608865b; // ELEMENTS
117 }
118 return c;
119}

Callers 2

VerifyChecksumFunction · 0.70
CreateChecksumFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected