Longfellow ZK 0290cb32
Loading...
Searching...
No Matches
memcmp.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_MEMCMP_H_
16#define PRIVACY_PROOFS_ZK_LIB_CIRCUITS_LOGIC_MEMCMP_H_
17
18#include <stddef.h>
19
20#include <vector>
21
22namespace proofs {
23// This class implements the an equivalent of memcmp for arrays of
24// v8. The logic comparison operators do all the work, and the only
25// problem is to arrange bits in the correct order for comparison.
26// In more detail, these methods compare the bit strings represented by
27// the array of v8 inputs (recall a v8 is 8 wires each containing a {0,1} value
28// in the Field).
29template <class Logic>
30class Memcmp {
31 public:
32 using BitW = typename Logic::BitW;
33 using v8 = typename Logic::v8;
34 const Logic& l_;
35
36 explicit Memcmp(const Logic& l) : l_(l) {}
37
38 // A < B
39 BitW lt(size_t n, const v8 A[/*n*/], const v8 B[/*n*/]) const {
40 std::vector<BitW> a(8 * n);
41 std::vector<BitW> b(8 * n);
42 arrange(n, a.data(), A);
43 arrange(n, b.data(), B);
44 return l_.lt(8 * n, a.data(), b.data());
45 }
46
47 // A <= B
48 BitW leq(size_t n, const v8 A[/*n*/], const v8 B[/*n*/]) const {
49 std::vector<BitW> a(8 * n);
50 std::vector<BitW> b(8 * n);
51 arrange(n, a.data(), A);
52 arrange(n, b.data(), B);
53 return l_.leq(8 * n, a.data(), b.data());
54 }
55
56 private:
57 void arrange(size_t n, BitW bits[/* 8 * n */], const v8 bytes[/*n*/]) const {
58 // from LSB to MSB:
59 for (size_t i = n; i-- > 0;) {
60 for (size_t j = 0; j < 8; ++j) {
61 *bits++ = bytes[i][j];
62 }
63 }
64 }
65};
66} // namespace proofs
67
68#endif // PRIVACY_PROOFS_ZK_LIB_CIRCUITS_LOGIC_MEMCMP_H_
Definition logic.h:38
Definition logic.h:130