Longfellow ZK 0290cb32
Loading...
Searching...
No Matches
bit_plucker_encoder.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_LOGIC_BIT_PLUCKER_ENCODER_H_
16#define PRIVACY_PROOFS_ZK_LIB_CIRCUITS_LOGIC_BIT_PLUCKER_ENCODER_H_
17
18#include <stddef.h>
19#include <stdint.h>
20
21#include <array>
22
23#include "circuits/logic/bit_plucker_constants.h"
24
25namespace proofs {
26template <class Field, size_t LOGN>
27class BitPluckerEncoder {
28 const Field& f_;
29
30 using Elt = typename Field::Elt;
31 static constexpr size_t kN = size_t(1) << LOGN;
32 static constexpr size_t kNv32Elts = (32u + LOGN - 1u) / LOGN;
33 static constexpr size_t kNv128Elts = (128u + LOGN - 1u) / LOGN;
34 static constexpr size_t kNv256Elts = (256u + LOGN - 1u) / LOGN;
35
36 public:
37 using packed_v32 = std::array<Elt, kNv32Elts>;
38 using packed_v128 = std::array<Elt, kNv128Elts>;
39 using packed_v256 = std::array<Elt, kNv256Elts>;
40
41 explicit BitPluckerEncoder(const Field& F) : f_(F) {}
42
43 Elt encode(size_t i) const { return bit_plucker_point<Field, kN>()(i, f_); }
44
45 // Special case packer for uint32_t used in sha256.
46 packed_v32 mkpacked_v32(uint32_t j) {
47 packed_v32 r;
48 for (size_t i = 0; i < r.size(); ++i) {
49 r[i] = encode(j & (kN - 1));
50 j >>= LOGN;
51 }
52 return r;
53 }
54
55 template <typename T>
56 T pack(uint8_t bits[/* n bits */], size_t n) {
57 T r;
58 for (size_t i = 0; i < r.size(); ++i) {
59 size_t v = 0;
60 for (size_t j = 0; j < LOGN; ++j) {
61 if (i * LOGN + j < n) {
62 v += (bits[i * LOGN + j] & 0x1) << j;
63 }
64 }
65 r[i] = encode(v);
66 }
67 return r;
68 }
69};
70} // namespace proofs
71
72#endif // PRIVACY_PROOFS_ZK_LIB_CIRCUITS_LOGIC_BIT_PLUCKER_ENCODER_H_
Definition gf2_128.h:63
Definition bit_plucker_constants.h:25