Longfellow ZK 0290cb32

Loading...
Searching...
No Matches
circuit_id.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_SUMCHECK_CIRCUIT_ID_H_
16#define PRIVACY_PROOFS_ZK_LIB_SUMCHECK_CIRCUIT_ID_H_
17
18#include <stddef.h>
19
20#include <cstdint>
21
22#include "sumcheck/circuit.h"
23#include "util/crypto.h"
24
25 namespace proofs {
26
27// This method produces a unique name for a circuit. It does not match
28// the serialization method for the circuit.
29 template <class Field>
30 void circuit_id(uint8_t id[/*32*/], const Circuit<Field>& c, const Field& F) {
31 const uint64_t CHAR2 = 0x2;
32 const uint64_t ODD = 0x1;
33 SHA256 sha;
34 uint8_t tmp[Field::kBytes];
35 if (F.kCharacteristicTwo) {
36 // Characteristic two fields are uniquely determined by their length
37 // in our codebase.
38 sha.Update8(CHAR2); // Indicates binary field.
39 sha.Update8(F.kBits);
40 } else {
41 // Prime fields are determined by -1.
42 sha.Update8(ODD); // Indicates odd prime field.
43 F.to_bytes_field(tmp, F.mone());
44 sha.Update(tmp, sizeof(tmp));
45 }
46 sha.Update8(c.nv);
47 sha.Update8(c.logv);
48 sha.Update8(c.nc);
49 sha.Update8(c.logc);
50 sha.Update8(c.nl);
51 sha.Update8(c.ninputs);
52 sha.Update8(c.npub_in);
53 sha.Update8(c.subfield_boundary);
54 for (const auto& layer : c.l) {
55 sha.Update8(layer.nw);
56 sha.Update8(layer.logw);
57 sha.Update8(layer.quad->n_);
58 for (size_t i = 0; i < layer.quad->n_; ++i) {
59 sha.Update8(static_cast<uint64_t>(layer.quad->c_[i].g));
60 sha.Update8(static_cast<uint64_t>(layer.quad->c_[i].h[0]));
61 sha.Update8(static_cast<uint64_t>(layer.quad->c_[i].h[1]));
62 F.to_bytes_field(tmp, layer.quad->c_[i].v);
63 sha.Update(tmp, sizeof(tmp));
64 }
65 }
66 sha.DigestData(id);
67}
68
69} // namespace proofs
70
71#endif // PRIVACY_PROOFS_ZK_LIB_SUMCHECK_CIRCUIT_ID_H_
proofs::SHA256
Definition crypto.h:40
proofs::Circuit
Definition circuit.h:45