Longfellow ZK 0290cb32

Loading...
Searching...
No Matches
mac_witness.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_CIRCUITS_MAC_MAC_WITNESS_H_
16#define PRIVACY_PROOFS_ZK_LIB_CIRCUITS_MAC_MAC_WITNESS_H_
17
18#include <cstddef>
19#include <cstdint>
20
21#include "arrays/dense.h"
22#include "circuits/logic/bit_plucker_encoder.h"
23#include "gf2k/gf2_128.h"
24
25 namespace proofs {
26
27 template <class Field>
28 class MacWitness {
29 using f_128 = GF2_128<>;
30 using gf2k = f_128::Elt;
31 using packer = BitPluckerEncoder<Field, 2>;
32 using packed_v128 = typename packer::packed_v128;
33 using packed_v256 = typename packer::packed_v256;
34
35 public:
36 explicit MacWitness(const Field& F, const f_128& GF) : f_(F), gf_(GF) {}
37
38 void fill_witness(DenseFiller<Field>& fill) const {
39 packer bp(f_);
40 uint8_t tmp[f_128::kBits];
41 for (size_t i = 0; i < 2; ++i) {
42 for (size_t j = 0; j < f_128::kBits; ++j) {
43 tmp[j] = ap_[i][j];
44 }
45 fill.push_back(bp.template pack<packed_v128>(tmp, f_128::kBits));
46 }
47
48 for (size_t i = 0; i < 2; ++i) {
49 for (size_t j = 0; j < f_128::kBits; ++j) {
50 tmp[j] = x_[i][j];
51 }
52 fill.push_back(bp.template pack<packed_v128>(tmp, 128));
53 }
54 }
55
56 // Computes a mac witness on a 32-byte message x.
57 // This code assumes that a gf element is at least 16 bytes.
58 void compute_witness(const gf2k a_p[/*2*/], const uint8_t x[/*32*/]) {
59 for (size_t i = 0; i < 2; ++i) {
60 x_[i] = gf_.of_bytes_field(&x[i * 16]).value();
61 ap_[i] = a_p[i];
62 }
63 }
64
65 private:
66 gf2k ap_[2], x_[2];
67 const Field& f_;
68 const f_128& gf_;
69};
70
71 class MacGF2Witness {
72 using f_128 = GF2_128<>;
73 using gf2k = f_128::Elt;
74
75 public:
76 void fill_witness(DenseFiller<f_128>& fill) const {
77 fill.push_back(ap_[0]);
78 fill.push_back(ap_[1]);
79 }
80
81 // Computes a mac witness on a 32-byte message x.
82 void compute_witness(const gf2k a_p[/*2*/]) {
83 for (size_t i = 0; i < 2; ++i) {
84 ap_[i] = a_p[i];
85 }
86 }
87
88 private:
89 gf2k ap_[2];
90};
91
92} // namespace proofs
93
94#endif // PRIVACY_PROOFS_ZK_LIB_CIRCUITS_MAC_MAC_WITNESS_H_
proofs::BitPluckerEncoder
Definition bit_plucker_encoder.h:27
proofs::DenseFiller
Definition dense.h:153
proofs::GF2_128
Definition gf2_128.h:35
proofs::MacGF2Witness
Definition mac_witness.h:71
proofs::GF2_128::Elt
Definition gf2_128.h:63