Longfellow ZK 0290cb32
Loading...
Searching...
No Matches
mdoc_revocation_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_MDOC_MDOC_REVOCATION_WITNESS_H_
16#define PRIVACY_PROOFS_ZK_LIB_CIRCUITS_MDOC_MDOC_REVOCATION_WITNESS_H_
17
18#include <cstddef>
19#include <cstdint>
20#include <cstdio>
21#include <vector>
22
23#include "arrays/dense.h"
24#include "circuits/ecdsa/verify_witness.h"
25#include "circuits/logic/bit_plucker_encoder.h"
26#include "circuits/mdoc/mdoc_revocation_constants.h"
27#include "circuits/sha/flatsha256_witness.h"
28
29namespace proofs {
30
31template <class Field>
32typename Field::Elt compute_mdoc_revocation_list_witness(
33 typename Field::Elt id, const typename Field::Elt list[], size_t list_size,
34 const Field& F) {
35 typename Field::Elt prodinv = F.one();
36 for (size_t i = 0; i < list_size; ++i) {
37 prodinv = F.mulf(prodinv, F.subf(list[i], id));
38 }
39 F.invert(prodinv);
40 return prodinv;
41}
42
43template <class EC, class ScalarField>
44class MdocRevocationSpanWitness {
45 using Field = typename EC::Field;
46 using Elt = typename Field::Elt;
47 using Nat = typename Field::N;
48 using EcdsaWitness = VerifyWitness3<EC, ScalarField>;
49 const EC& ec_;
50
51 public:
52 Elt e_, r_, s_;
53 EcdsaWitness sig_;
54 uint8_t preimage_[64 * 2];
55 uint8_t id_bits_[256];
56 uint8_t e_bits_[256];
58
59 explicit MdocRevocationSpanWitness(const EC& ec, const ScalarField& Fn)
60 : ec_(ec), sig_(Fn, ec) {}
61
62 void fill_witness(DenseFiller<Field>& filler) const {
63 filler.push_back(r_);
64 filler.push_back(s_);
65 filler.push_back(e_);
66 sig_.fill_witness(filler);
67
68 // Write the span message.
69 for (size_t i = 0; i < 64 * 2; ++i) {
70 for (size_t j = 0; j < 8; ++j) {
71 filler.push_back((preimage_[i] >> j) & 0x1 ? ec_.f_.one()
72 : ec_.f_.zero());
73 }
74 }
75
76 for (size_t i = 0; i < 256; ++i) {
77 filler.push_back(id_bits_[i] ? ec_.f_.one() : ec_.f_.zero());
78 }
79 for (size_t i = 0; i < 256; ++i) {
80 filler.push_back(e_bits_[i] ? ec_.f_.one() : ec_.f_.zero());
81 }
82
83 for (size_t j = 0; j < 2; j++) {
84 fill_sha(filler, sha_bw_[j]);
85 }
86 }
87
88 void fill_sha(DenseFiller<Field>& filler,
89 const FlatSHA256Witness::BlockWitness& bw) const {
91 for (size_t k = 0; k < 48; ++k) {
92 filler.push_back(BPENC.mkpacked_v32(bw.outw[k]));
93 }
94 for (size_t k = 0; k < 64; ++k) {
95 filler.push_back(BPENC.mkpacked_v32(bw.oute[k]));
96 filler.push_back(BPENC.mkpacked_v32(bw.outa[k]));
97 }
98 for (size_t k = 0; k < 8; ++k) {
99 filler.push_back(BPENC.mkpacked_v32(bw.h1[k]));
100 }
101 }
102
103 bool compute_witness(Elt pkX, Elt pkY, Nat ne, Nat nr, Nat ns, Nat id, Nat ll,
104 Nat rr, uint64_t epoch) {
105 e_ = ec_.f_.to_montgomery(ne);
106 r_ = ec_.f_.to_montgomery(nr);
107 s_ = ec_.f_.to_montgomery(ns);
108 sig_.compute_witness(pkX, pkY, ne, nr, ns);
109
110 std::vector<uint8_t> buf;
111 for (size_t i = 0; i < 8; ++i) {
112 buf.push_back(epoch & 0xff);
113 epoch >>= 8;
114 }
115 uint8_t tmp[Field::kBytes];
116 ll.to_bytes(tmp);
117 buf.insert(buf.end(), tmp, tmp + Field::kBytes);
118 rr.to_bytes(tmp);
119 buf.insert(buf.end(), tmp, tmp + Field::kBytes);
120
121 for (size_t i = 0; i < 256; ++i) {
122 id_bits_[i] = id.bit(i);
123 e_bits_[i] = ne.bit(i);
124 }
125
126 uint8_t numb = 0;
127 FlatSHA256Witness::transform_and_witness_message(buf.size(), buf.data(), 2,
128 numb, preimage_, sha_bw_);
129
130 return true;
131 }
132};
133
134} // namespace proofs
135
136#endif // PRIVACY_PROOFS_ZK_LIB_CIRCUITS_MDOC_MDOC_REVOCATION_WITNESS_H_
Definition bit_plucker_encoder.h:27
Definition dense.h:153
Definition verify_witness.h:30
Definition flatsha256_witness.h:27
Definition gf2_128.h:63