2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
25 * Representation of a polynomial of degree DEG over GF(2) (that is,
26 * with binary coefficients).
28 * Probably of no use outside of Fingerprint code; used by
29 * GenerateFingerprintTables and the unittest.
32 class FingerprintPolynomial {
34 FingerprintPolynomial() {
35 for (int i = 0; i < size(); i++) {
40 explicit FingerprintPolynomial(const uint64_t* vals) {
41 for (int i = 0; i < size(); i++) {
46 void write(uint64_t* out) const {
47 for (int i = 0; i < size(); i++) {
52 void add(const FingerprintPolynomial<DEG>& other) {
53 for (int i = 0; i < size(); i++) {
54 val_[i] ^= other.val_[i];
58 // Multiply by X. The actual degree must be < DEG.
60 CHECK_EQ(0u, val_[0] & (1ULL << 63));
62 for (int i = size()-1; i >= 0; i--) {
63 uint64_t nb = val_[i] >> 63;
64 val_[i] = (val_[i] << 1) | b;
69 // Compute (this * X) mod P(X), where P(X) is a monic polynomial of degree
70 // DEG+1 (represented as a FingerprintPolynomial<DEG> object, with the
71 // implicit coefficient of X^(DEG+1)==1)
73 // This is a bit tricky. If k=DEG+1:
74 // Let P(X) = X^k + p_(k-1) * X^(k-1) + ... + p_1 * X + p_0
75 // Let this = A(X) = a_(k-1) * X^(k-1) + ... + a_1 * X + a_0
78 // = a_(k-1) * X^k + (a_(k-2) * X^(k-1) + ... + a_1 * X^2 + a_0 * X)
79 // = a_(k-1) * X^k + (the binary representation of A, left shift by 1)
81 // if a_(k-1) = 0, we can ignore the first term.
82 // if a_(k-1) = 1, then:
86 // = p_(k-1) * X^(k-1) + ... + p_1 * X + p_0
87 // = exactly the binary representation passed in as an argument to this
90 // So A(X) * X mod P(X) is:
91 // the binary representation of A, left shift by 1,
92 // XOR p if a_(k-1) == 1
93 void mulXmod(const FingerprintPolynomial<DEG>& p) {
94 bool needXOR = (val_[0] & (1ULL<<63));
95 val_[0] &= ~(1ULL<<63);
102 // Compute (this * X^k) mod P(X) by repeatedly multiplying by X (see above)
103 void mulXkmod(int k, const FingerprintPolynomial<DEG>& p) {
104 for (int i = 0; i < k; i++) {
109 // add X^k, where k <= DEG
113 int word_offset = (DEG - k) / 64;
114 int bit_offset = 63 - (DEG - k) % 64;
115 val_[word_offset] ^= (1ULL << bit_offset);
118 // Set the highest 8 bits to val.
119 // If val is interpreted as polynomial of degree 7, then this sets *this
120 // to val * X^(DEG-7)
121 void setHigh8Bits(uint8_t val) {
122 val_[0] = ((uint64_t)val) << (64-8);
123 for (int i = 1; i < size(); i++) {
128 static constexpr int size() {
132 // Internal representation: big endian
133 // val_[0] contains the highest order coefficients, with bit 63 as the
134 // highest order coefficient
136 // If DEG+1 is not a multiple of 64, val_[size()-1] only uses the highest
137 // order (DEG+1)%64 bits (the others are always 0)
138 uint64_t val_[1 + DEG/64];
141 } // namespace detail