Longfellow ZK 0290cb32

Loading...
Searching...
No Matches
gf2poly.h
1// Copyright 2025 Google LLC.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef PRIVACY_PROOFS_ZK_LIB_GF2K_GF2POLY_H_
16#define PRIVACY_PROOFS_ZK_LIB_GF2K_GF2POLY_H_
17
18#include <array>
19#include <cstddef>
20#include <cstdint>
21
22#include "algebra/limb.h"
23
24 namespace proofs {
25
26// Rough equivalent of Nat<W64> but representing polynomials
27// over GF2 instead of natural numbers.
28 template <size_t W64>
29 class GF2Poly : public Limb<W64> {
30 public:
31 using Super = Limb<W64>;
32 using T = GF2Poly<W64>;
33 using Super::kLimbs;
34 using Super::kU64;
35 using Super::limb_;
36
37 GF2Poly() = default; // uninitialized
38 explicit GF2Poly(uint64_t x) : Super(x) {}
39
40 explicit GF2Poly(const std::array<uint64_t, kU64>& a) : Super(a) {}
41
42 bool operator<(const T& other) const {
43 for (size_t i = kLimbs; i-- > 0;) {
44 if (limb_[i] < other.limb_[i]) {
45 return true;
46 }
47 if (limb_[i] > other.limb_[i]) {
48 return false;
49 }
50 }
51 return false;
52 }
53
54 // Interpret A[] as a little-endian nat
55 static T of_bytes(const uint8_t a[/* kBytes */]) {
56 T r;
57 for (size_t i = 0; i < kLimbs; ++i) {
58 a = Super::of_bytes(&r.limb_[i], a);
59 }
60 return r;
61 }
62
63 T& add(const T& y) {
64 for (size_t i = 0; i < kLimbs; ++i) {
65 limb_[i] ^= y.limb_[i];
66 }
67 return *this;
68 }
69 T& sub(const T& y) { return add(y); }
70};
71
72} // namespace proofs
73
74#endif // PRIVACY_PROOFS_ZK_LIB_GF2K_GF2POLY_H_