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