Debian Codegraph @

mlspp/src/treekem.cpp

raw symbols SCIP
1 #cxx . . $ `<file>/mlspp/src/treekem.cpp`/include <mls/treekem.h>cxx . . $ `<file>/mlspp/include/mls/treekem.h`/
2
3 #if ENABLE_TREE_DUMPcxx . . $ `mlspp/include/mls/treekem.h:9:9`!
4 #include <iostream>
5 #endif
6
7 namespace mlsppcxx . . $ mlspp/ {
8
9 // Utility method used for removing leaves from a resolution
10 static void
11 remove_leavescxx . . $ mlspp/remove_leaves(fec29baccf868da2).(stdcxx . . $ std/::vectorcxx . . $ std/vector#<NodeIndexcxx . . $ mlspp/NodeIndex#>& res, const stdcxx . . $ std/::vectorcxx . . $ std/vector#<LeafIndexcxx . . $ mlspp/LeafIndex#>& except)
12 {
13 for (const auto& leaf : except) {
14 auto it = stdcxx . . $ std/::findcxx . . $ std/find(a92da12e15934614).(res.begincxx . . $ std/vector#begin(55a6de06420d4903).(), res.endcxx . . $ std/vector#end(55a6de06420d4903).(), NodeIndexcxx . . $ mlspp/NodeIndex#NodeIndex(5a3d3cee3fdf86fc).(leaf));
15 if (it ==cxx . . $ __gnu_cxx/`operator==`(3cd84a314b671a91). res.endcxx . . $ std/vector#end(55a6de06420d4903).()) {
16 continue;
17 }
18
19 res.erasecxx . . $ std/vector#erase(351407f384f345bc).(itcxx . . $ __gnu_cxx/__normal_iterator#__normal_iterator(459a7e7b9fbe7640).);
20 }
21 }
22
23 ///
24 /// Node
25 ///
26
27 const HPKEPublicKeycxx . . $ mlspp/HPKEPublicKey#&
28 Nodecxx . . $ mlspp/Node#::public_keycxx . . $ mlspp/Node#public_key(d5c38d46ab7a7ba1).() const
29 {
30 const auto get_key = overloaded{
31 [](const LeafNodecxx . . $ mlspp/LeafNode#& n) -> const HPKEPublicKeycxx . . $ mlspp/HPKEPublicKey#& { return n.encryption_keycxx . . $ mlspp/LeafNode#encryption_key.; },
32 [](const ParentNodecxx . . $ mlspp/ParentNode#& n) -> const HPKEPublicKeycxx . . $ mlspp/HPKEPublicKey#& { return n.public_keycxx . . $ mlspp/ParentNode#public_key.; },
33 };
34 return var::visitcxx . . $ std/visit(9828ab07ec69b3d0).(get_key, nodecxx . . $ mlspp/Node#node.);
35 }
36
37 stdcxx . . $ std/::optionalcxx . . $ std/optional#<bytescxx . . $ mlspp/bytes_ns/bytes#>
38 Nodecxx . . $ mlspp/Node#::parent_hashcxx . . $ mlspp/Node#parent_hash(82a679487d1deb04).() const
39 {
40 const auto get_leaf_ph = overloaded{
41 [](const ParentHashcxx . . $ mlspp/ParentHash#& ph) -> stdcxx . . $ std/::optionalcxx . . $ std/optional#<bytescxx . . $ mlspp/bytes_ns/bytes#> { return phcxx . . $ std/optional#optional(e3f0d6382bd52f24)..parent_hashcxx . . $ mlspp/ParentHash#parent_hash.; },
42 [](const auto& /* other */) -> stdcxx . . $ std/::optionalcxx . . $ std/optional#<bytescxx . . $ mlspp/bytes_ns/bytes#> {
43 return stdcxx . . $ std/optional#optional(eb2253fdec8870b5).::nulloptcxx . . $ std/nullopt.;
44 },
45 };
46
47 const auto get_ph = overloaded{
48 [&](const LeafNodecxx . . $ mlspp/LeafNode#& node) -> stdcxx . . $ std/::optionalcxx . . $ std/optional#<bytescxx . . $ mlspp/bytes_ns/bytes#> {
49 return var::visitcxx . . $ std/visit(9828ab07ec69b3d0).(get_leaf_ph, node.contentcxx . . $ mlspp/LeafNode#content.);
50 },
51 [](const ParentNodecxx . . $ mlspp/ParentNode#& node) -> stdcxx . . $ std/::optionalcxx . . $ std/optional#<bytescxx . . $ mlspp/bytes_ns/bytes#> {
52 return nodecxx . . $ std/optional#optional(e3f0d6382bd52f24)..parent_hashcxx . . $ mlspp/ParentNode#parent_hash.;
53 },
54 };
55
56 return var::visitcxx . . $ std/visit(9828ab07ec69b3d0).(get_ph, nodecxx . . $ mlspp/Node#node.);
57 }
58
59 ///
60 /// TreeKEMPrivateKey
61 ///
62
63 TreeKEMPrivateKeycxx . . $ mlspp/TreeKEMPrivateKey#
64 TreeKEMPrivateKeycxx . . $ mlspp/TreeKEMPrivateKey#::solocxx . . $ mlspp/TreeKEMPrivateKey#solo(314639de3ba28cb).(CipherSuitecxx . . $ mlspp/CipherSuite# suite,
65 LeafIndexcxx . . $ mlspp/LeafIndex# index,
66 HPKEPrivateKeycxx . . $ mlspp/HPKEPrivateKey# leaf_priv)
67 {
68 auto priv = TreeKEMPrivateKeycxx . . $ mlspp/TreeKEMPrivateKey#{ suite, index, {}, {}, {} };
69 priv.private_key_cachecxx . . $ mlspp/TreeKEMPrivateKey#private_key_cache..insertcxx . . $ std/map#insert(274b9441a76f0de8).({cxx . . $ std/pair#pair(14d64bafa7379ebc). NodeIndexcxx . . $ mlspp/NodeIndex#NodeIndex(5a3d3cee3fdf86fc).(index), stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(leaf_priv) });
70 return priv;
71 }
72
73 TreeKEMPrivateKeycxx . . $ mlspp/TreeKEMPrivateKey#
74 TreeKEMPrivateKeycxx . . $ mlspp/TreeKEMPrivateKey#::createcxx . . $ mlspp/TreeKEMPrivateKey#create(a23e82a828226b37).(const TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#& pub,
75 LeafIndexcxx . . $ mlspp/LeafIndex# from,
76 const bytescxx . . $ mlspp/bytes_ns/bytes#& leaf_secret)
77 {
78 auto priv = TreeKEMPrivateKeycxx . . $ mlspp/TreeKEMPrivateKey#{ pub.suitecxx . . $ mlspp/TreeKEMPublicKey#suite., from, {}, {}, {} };
79 priv.implantcxx . . $ mlspp/TreeKEMPrivateKey#implant(333467ad79be4356).(pub, NodeIndexcxx . . $ mlspp/NodeIndex#NodeIndex(5a3d3cee3fdf86fc).(from), leaf_secret);
80 return priv;
81 }
82
83 TreeKEMPrivateKeycxx . . $ mlspp/TreeKEMPrivateKey#
84 TreeKEMPrivateKeycxx . . $ mlspp/TreeKEMPrivateKey#::joinercxx . . $ mlspp/TreeKEMPrivateKey#joiner(3c3a8fd039c9c001).(const TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#& pub,
85 LeafIndexcxx . . $ mlspp/LeafIndex# index,
86 HPKEPrivateKeycxx . . $ mlspp/HPKEPrivateKey# leaf_priv,
87 NodeIndexcxx . . $ mlspp/NodeIndex# intersect,
88 const stdcxx . . $ std/::optionalcxx . . $ std/optional#<bytescxx . . $ mlspp/bytes_ns/bytes#>& path_secret)
89 {
90 auto priv = TreeKEMPrivateKeycxx . . $ mlspp/TreeKEMPrivateKey#{ pub.suitecxx . . $ mlspp/TreeKEMPublicKey#suite., index, {}, {}, {} };
91 priv.private_key_cachecxx . . $ mlspp/TreeKEMPrivateKey#private_key_cache..insertcxx . . $ std/map#insert(274b9441a76f0de8).({cxx . . $ std/pair#pair(14d64bafa7379ebc). NodeIndexcxx . . $ mlspp/NodeIndex#NodeIndex(5a3d3cee3fdf86fc).(index), stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(leaf_priv) });
92 if (path_secret) {
93 priv.implantcxx . . $ mlspp/TreeKEMPrivateKey#implant(333467ad79be4356).(pub, intersect, opt::getcxx . . $ mlspp/tls/opt/get(3a846ed22d48625).(path_secret));
94 }
95 return priv;
96 }
97
98 void
99 TreeKEMPrivateKeycxx . . $ mlspp/TreeKEMPrivateKey#::implantcxx . . $ mlspp/TreeKEMPrivateKey#implant(333467ad79be4356).(const TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#& pub,
100 NodeIndexcxx . . $ mlspp/NodeIndex# start,
101 const bytescxx . . $ mlspp/bytes_ns/bytes#& path_secret)
102 {
103 const auto fdp = pub.filtered_direct_pathcxx . . $ mlspp/TreeKEMPublicKey#filtered_direct_path(b755724fb327e77c).(start);
104 auto secret = path_secretcxx . . $ mlspp/bytes_ns/bytes#bytes(68ca607eebb5be3e).;
105
106 path_secretscxx . . $ mlspp/TreeKEMPrivateKey#path_secrets..insert_or_assigncxx . . $ std/map#insert_or_assign(a356f559b7c7a273).(start, secret);
107 private_key_cachecxx . . $ mlspp/TreeKEMPrivateKey#private_key_cache..erasecxx . . $ std/map#erase(a7de169f3ef4bfb3).(start);
108
109 for (const auto& [n, _res] : fdp) {
110 secret =cxx . . $ mlspp/bytes_ns/bytes#`operator=`(463b53a2545785ed). pub.suitecxx . . $ mlspp/TreeKEMPublicKey#suite..derive_secretcxx . . $ mlspp/CipherSuite#derive_secret(5169660e3f01b6ab).(secret, "path"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
111 path_secretscxx . . $ mlspp/TreeKEMPrivateKey#path_secrets..insert_or_assigncxx . . $ std/map#insert_or_assign(a356f559b7c7a273).(n, secret);
112 private_key_cachecxx . . $ mlspp/TreeKEMPrivateKey#private_key_cache..erasecxx . . $ std/map#erase(a7de169f3ef4bfb3).(n);
113 }
114
115 update_secretcxx . . $ mlspp/TreeKEMPrivateKey#update_secret. =cxx . . $ mlspp/bytes_ns/bytes#`operator=`(463b53a2545785ed). pub.suitecxx . . $ mlspp/TreeKEMPublicKey#suite..derive_secretcxx . . $ mlspp/CipherSuite#derive_secret(5169660e3f01b6ab).(secret, "path"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
116 }
117
118 stdcxx . . $ std/::optionalcxx . . $ std/optional#<HPKEPrivateKeycxx . . $ mlspp/HPKEPrivateKey#>
119 TreeKEMPrivateKeycxx . . $ mlspp/TreeKEMPrivateKey#::private_keycxx . . $ mlspp/TreeKEMPrivateKey#private_key(ff9927d09a546cdc).(NodeIndexcxx . . $ mlspp/NodeIndex# n) const
120 {
121 auto pki = private_key_cachecxx . . $ mlspp/TreeKEMPrivateKey#private_key_cache..findcxx . . $ std/map#find(7b3585bb5da8f7d).(n);
122 if (pki !=cxx . . $ std/`operator!=`(a84eb84a7a5e8237). private_key_cachecxx . . $ mlspp/TreeKEMPrivateKey#private_key_cache..endcxx . . $ std/map#end(b953fec338ad1a3b).()) {
123 return pkicxx . . $ std/optional#optional(e3f0d6382bd52f24).->cxx . . $ std/_Rb_tree_const_iterator#`operator->`(8e614c7de0b1573e).secondcxx . . $ std/pair#second.;
124 }
125
126 auto i = path_secretscxx . . $ mlspp/TreeKEMPrivateKey#path_secrets..findcxx . . $ std/map#find(7b3585bb5da8f7d).(n);
127 if (i ==cxx . . $ std/`operator==`(a84eb84a7a5e8237). path_secretscxx . . $ mlspp/TreeKEMPrivateKey#path_secrets..endcxx . . $ std/map#end(b953fec338ad1a3b).()) {
128 return stdcxx . . $ std/optional#optional(eb2253fdec8870b5).::nulloptcxx . . $ std/nullopt.;
129 }
130
131 auto node_secret = suitecxx . . $ mlspp/TreeKEMPrivateKey#suite..derive_secretcxx . . $ mlspp/CipherSuite#derive_secret(5169660e3f01b6ab).(i->cxx . . $ std/_Rb_tree_const_iterator#`operator->`(8e614c7de0b1573e).secondcxx . . $ std/pair#second., "node"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
132 return HPKEPrivateKeycxx . . $ std/optional#optional(e3f0d6382bd52f24).::derivecxx . . $ mlspp/HPKEPrivateKey#derive(d8fbf663c4028288).(suitecxx . . $ mlspp/TreeKEMPrivateKey#suite., node_secret);
133 }
134
135 bool
136 TreeKEMPrivateKeycxx . . $ mlspp/TreeKEMPrivateKey#::have_private_keycxx . . $ mlspp/TreeKEMPrivateKey#have_private_key(df9c6d3ab1251738).(NodeIndexcxx . . $ mlspp/NodeIndex# n) const
137 {
138 auto path_secret = path_secretscxx . . $ mlspp/TreeKEMPrivateKey#path_secrets..findcxx . . $ std/map#find(7b3585bb5da8f7d).(n) !=cxx . . $ std/`operator!=`(a84eb84a7a5e8237). path_secretscxx . . $ mlspp/TreeKEMPrivateKey#path_secrets..endcxx . . $ std/map#end(b953fec338ad1a3b).();
139 auto cached_priv = private_key_cachecxx . . $ mlspp/TreeKEMPrivateKey#private_key_cache..findcxx . . $ std/map#find(7b3585bb5da8f7d).(n) !=cxx . . $ std/`operator!=`(a84eb84a7a5e8237). private_key_cachecxx . . $ mlspp/TreeKEMPrivateKey#private_key_cache..endcxx . . $ std/map#end(b953fec338ad1a3b).();
140 return path_secret || cached_priv;
141 }
142
143 stdcxx . . $ std/::optionalcxx . . $ std/optional#<HPKEPrivateKeycxx . . $ mlspp/HPKEPrivateKey#>
144 TreeKEMPrivateKeycxx . . $ mlspp/TreeKEMPrivateKey#::private_keycxx . . $ mlspp/TreeKEMPrivateKey#private_key(4d6c5a4be6fa7ce4).(NodeIndexcxx . . $ mlspp/NodeIndex# n)
145 {
146 auto priv = static_cast<const TreeKEMPrivateKeycxx . . $ mlspp/TreeKEMPrivateKey#&>(*this).private_keycxx . . $ mlspp/TreeKEMPrivateKey#private_key(ff9927d09a546cdc).(n);
147 if (priv) {
148 private_key_cachecxx . . $ mlspp/TreeKEMPrivateKey#private_key_cache..insert_or_assigncxx . . $ std/map#insert_or_assign(a356f559b7c7a273).(n, opt::getcxx . . $ mlspp/tls/opt/get(f3e04ae79584f296).(priv));
149 }
150 return priv;
151 }
152
153 void
154 TreeKEMPrivateKeycxx . . $ mlspp/TreeKEMPrivateKey#::set_leaf_privcxx . . $ mlspp/TreeKEMPrivateKey#set_leaf_priv(35c8eff745156dde).(HPKEPrivateKeycxx . . $ mlspp/HPKEPrivateKey# priv)
155 {
156 auto n = NodeIndexcxx . . $ mlspp/NodeIndex#NodeIndex(5a3d3cee3fdf86fc).(indexcxx . . $ mlspp/TreeKEMPrivateKey#index.);
157 path_secretscxx . . $ mlspp/TreeKEMPrivateKey#path_secrets..erasecxx . . $ std/map#erase(a7de169f3ef4bfb3).(n);
158 private_key_cachecxx . . $ mlspp/TreeKEMPrivateKey#private_key_cache..insert_or_assigncxx . . $ std/map#insert_or_assign(a356f559b7c7a273).(n, stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(priv));
159 }
160
161 stdcxx . . $ std/::tuplecxx . . $ std/tuple#<NodeIndexcxx . . $ mlspp/NodeIndex#, bytescxx . . $ mlspp/bytes_ns/bytes#, bool>
162 TreeKEMPrivateKeycxx . . $ mlspp/TreeKEMPrivateKey#::shared_path_secretcxx . . $ mlspp/TreeKEMPrivateKey#shared_path_secret(f9c1aa84c71de2c3).(LeafIndexcxx . . $ mlspp/LeafIndex# to) const
163 {
164 auto n = indexcxx . . $ mlspp/TreeKEMPrivateKey#index..ancestorcxx . . $ mlspp/LeafIndex#ancestor(9a73b556cbb4fcfc).(to);
165 auto i = path_secretscxx . . $ mlspp/TreeKEMPrivateKey#path_secrets..findcxx . . $ std/map#find(7b3585bb5da8f7d).(n);
166 if (i ==cxx . . $ std/`operator==`(a84eb84a7a5e8237). path_secretscxx . . $ mlspp/TreeKEMPrivateKey#path_secrets..endcxx . . $ std/map#end(b953fec338ad1a3b).()) {
167 return stdcxx . . $ std/::make_tuplecxx . . $ std/make_tuple(3964edbfd7e8599f).(n, bytescxx . . $ mlspp/bytes_ns/bytes#bytes(ced63f7c635d850d).{}, false);
168 }
169
170 return stdcxx . . $ std/::make_tuplecxx . . $ std/make_tuple(3964edbfd7e8599f).(n, i->cxx . . $ std/_Rb_tree_const_iterator#`operator->`(8e614c7de0b1573e).secondcxx . . $ std/pair#second., true);
171 }
172
173 #if ENABLE_TREE_DUMPcxx . . $ `mlspp/include/mls/treekem.h:9:9`!
174 // XXX(RLB) This should ultimately be deleted, but it is handy for interop
175 // debugging, so I'm keeping it around for now. If re-enabled, you'll also need
176 // to add the appropriate declarations to treekem.h and include <iostream>
177
178 void
179 TreeKEMPrivateKeycxx . . $ mlspp/TreeKEMPrivateKey#::dumpcxx . . $ mlspp/TreeKEMPrivateKey#dump(83f5f38750778ac8).() const
180 {
181 for (const auto& [node, _] : path_secretscxx . . $ mlspp/TreeKEMPrivateKey#path_secrets.) {
182 private_keycxx . . $ mlspp/TreeKEMPrivateKey#private_key(ff9927d09a546cdc).(node);
183 }
184
185 stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Tree (priv):" <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9).;
186 stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " Index: " <<cxx . . $ std/basic_ostream#`operator<<`(b4c055c363850233). NodeIndexcxx . . $ mlspp/NodeIndex#NodeIndex(5a3d3cee3fdf86fc).(indexcxx . . $ mlspp/TreeKEMPrivateKey#index.).valcxx . . $ mlspp/UInt32#val. <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9).;
187
188 stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " Secrets: " <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9).;
189 for (const auto& [n, path_secret] : path_secretscxx . . $ mlspp/TreeKEMPrivateKey#path_secrets.) {
190 auto node_secret = suitecxx . . $ mlspp/TreeKEMPrivateKey#suite..derive_secretcxx . . $ mlspp/CipherSuite#derive_secret(5169660e3f01b6ab).(path_secret, "node"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
191 auto sk = HPKEPrivateKeycxx . . $ mlspp/HPKEPrivateKey#::derivecxx . . $ mlspp/HPKEPrivateKey#derive(d8fbf663c4028288).(suitecxx . . $ mlspp/TreeKEMPrivateKey#suite., node_secret);
192
193 auto psm = to_hexcxx . . $ mlspp/bytes_ns/to_hex(b0627b6bf491e6e2).(path_secret).substrcxx . . $ std/__cxx11/basic_string#substr(149c655009c9bbd2).(0, 8);
194 auto pkm = to_hexcxx . . $ mlspp/bytes_ns/to_hex(b0627b6bf491e6e2).(sk.public_keycxx . . $ mlspp/HPKEPrivateKey#public_key..datacxx . . $ mlspp/HPKEPublicKey#data.).substrcxx . . $ std/__cxx11/basic_string#substr(149c655009c9bbd2).(0, 8);
195 stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " " <<cxx . . $ std/basic_ostream#`operator<<`(b4c055c363850233). n.valcxx . . $ mlspp/UInt32#val. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " => " <<cxx . . $ std/`operator<<`(88df5bef833f7f83). psm <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " => " <<cxx . . $ std/`operator<<`(88df5bef833f7f83). pkm <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9).;
196 }
197
198 stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " Cached key pairs: " <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9).;
199 for (const auto& [n, sk] : private_key_cachecxx . . $ mlspp/TreeKEMPrivateKey#private_key_cache.) {
200 auto pkm = to_hexcxx . . $ mlspp/bytes_ns/to_hex(b0627b6bf491e6e2).(sk.public_keycxx . . $ mlspp/HPKEPrivateKey#public_key..datacxx . . $ mlspp/HPKEPublicKey#data.).substrcxx . . $ std/__cxx11/basic_string#substr(149c655009c9bbd2).(0, 8);
201 stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " " <<cxx . . $ std/basic_ostream#`operator<<`(b4c055c363850233). n.valcxx . . $ mlspp/UInt32#val. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " => " <<cxx . . $ std/`operator<<`(88df5bef833f7f83). pkm <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9).;
202 }
203 }
204
205 void
206 TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#::dumpcxx . . $ mlspp/TreeKEMPublicKey#dump(83f5f38750778ac8).() const
207 {
208 stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Tree:" <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9).;
209 auto width = NodeCountcxx . . $ mlspp/NodeCount#NodeCount(a733f6ba51500b8c).(sizecxx . . $ mlspp/TreeKEMPublicKey#size.);
210 for (auto i = NodeIndexcxx . . $ mlspp/NodeIndex#{ 0 }; i.valcxx . . $ mlspp/UInt32#val. < width.valcxx . . $ mlspp/UInt32#val.; i.valcxx . . $ mlspp/UInt32#val.++) {
211 printf(" %03d : ", i.valcxx . . $ mlspp/UInt32#val.); // NOLINT
212 if (!node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(7c807605eec2a484).(i).blankcxx . . $ mlspp/OptionalNode#blank(50ce9a9e25b4a850).()) {
213 auto pkRm = to_hexcxx . . $ mlspp/bytes_ns/to_hex(b0627b6bf491e6e2).(opt::getcxx . . $ mlspp/tls/opt/get(3a846ed22d48625).(node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(7c807605eec2a484).(i).nodecxx . . $ mlspp/OptionalNode#node.).public_keycxx . . $ mlspp/Node#public_key(d5c38d46ab7a7ba1).().datacxx . . $ mlspp/HPKEPublicKey#data.);
214 stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(88df5bef833f7f83). pkRm.substrcxx . . $ std/__cxx11/basic_string#substr(149c655009c9bbd2).(0, 8);
215 } else {
216 stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " ";
217 }
218
219 stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " | ";
220 for (uint32_tcxx . . $ uint32_t# j = 0; j < i.levelcxx . . $ mlspp/NodeIndex#level(ddd8539a2fcdd4ce).(); j++) {
221 stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " ";
222 }
223
224 if (!node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(7c807605eec2a484).(i).blankcxx . . $ mlspp/OptionalNode#blank(50ce9a9e25b4a850).()) {
225 stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "X";
226
227 if (!i.is_leafcxx . . $ mlspp/NodeIndex#is_leaf(50ce9a9e25b4a850).()) {
228 auto parent = node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(7c807605eec2a484).(i).parent_nodecxx . . $ mlspp/OptionalNode#parent_node(17c9744d3cd194d).();
229 stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " [";
230 for (const auto u : parent.unmerged_leavescxx . . $ mlspp/ParentNode#unmerged_leaves.) {
231 stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/basic_ostream#`operator<<`(b4c055c363850233). u.valcxx . . $ mlspp/UInt32#val. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). ", ";
232 }
233 stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "]";
234 }
235
236 } else {
237 stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "_";
238 }
239
240 stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9).;
241 }
242 }
243 #endif
244
245 void
246 TreeKEMPrivateKeycxx . . $ mlspp/TreeKEMPrivateKey#::decapcxx . . $ mlspp/TreeKEMPrivateKey#decap(ca2156724251758f).(LeafIndexcxx . . $ mlspp/LeafIndex# from,
247 const TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#& pub,
248 const bytescxx . . $ mlspp/bytes_ns/bytes#& context,
249 const UpdatePathcxx . . $ mlspp/UpdatePath#& path,
250 const stdcxx . . $ std/::vectorcxx . . $ std/vector#<LeafIndexcxx . . $ mlspp/LeafIndex#>& except)
251 {
252 // Identify which node in the path secret we will be decrypting
253 auto ni = NodeIndexcxx . . $ mlspp/NodeIndex#NodeIndex(5a3d3cee3fdf86fc).(indexcxx . . $ mlspp/TreeKEMPrivateKey#index.);
254 auto dp = pub.filtered_direct_pathcxx . . $ mlspp/TreeKEMPublicKey#filtered_direct_path(b755724fb327e77c).(NodeIndexcxx . . $ mlspp/NodeIndex#NodeIndex(5a3d3cee3fdf86fc).(from));
255 if (dp.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() != path.nodescxx . . $ mlspp/UpdatePath#nodes..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()) {
256 throw ProtocolErrorcxx . . $ mlspp/ProtocolError#("Malformed direct path");
257 }
258
259 size_t dpi = 0;
260 auto overlap_node = NodeIndexcxx . . $ mlspp/NodeIndex#{};
261 auto res = stdcxx . . $ std/vector#vector(49f6e7a06ebc5aa8).::vectorcxx . . $ std/vector#<NodeIndexcxx . . $ mlspp/NodeIndex#>{};
262 for (dpi = 0; dpi < dp.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).(); dpi++) {
263 const auto [dpn, dpres] = dpcxx . . $ std/tuple#tuple(f6149edcf2e66b11).[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).dpi];
264 if (ni.is_belowcxx . . $ mlspp/NodeIndex#is_below(df9c6d3ab1251738).(dpn)) {
265 overlap_node =cxx . . $ mlspp/NodeIndex#`operator=`(f8d7ad8ae2e57adf). dpn;
266 res =cxx . . $ std/vector#`operator=`(e6b68e61becad66f). dpres;
267 break;
268 }
269 }
270
271 if (dpi == dp.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()) {
272 throw ProtocolErrorcxx . . $ mlspp/ProtocolError#("No overlap in path");
273 }
274
275 // Identify which node in the resolution of the copath we will use to decrypt
276 remove_leavescxx . . $ mlspp/remove_leaves(fec29baccf868da2).(res, except);
277 if (res.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() != path.nodescxx . . $ mlspp/UpdatePath#nodes.[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32).dpi].encrypted_path_secretcxx . . $ mlspp/UpdatePathNode#encrypted_path_secret..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()) {
278 throw ProtocolErrorcxx . . $ mlspp/ProtocolError#("Malformed direct path node");
279 }
280
281 size_t resi = 0;
282 const NodeIndexcxx . . $ mlspp/NodeIndex# res_overlap_node;
283 for (resi = 0; resi < res.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).(); resi++) {
284 if (have_private_keycxx . . $ mlspp/TreeKEMPrivateKey#have_private_key(df9c6d3ab1251738).(res[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).resi])) {
285 break;
286 }
287 }
288
289 if (resi == res.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()) {
290 throw ProtocolErrorcxx . . $ mlspp/ProtocolError#("No private key to decrypt path secret");
291 }
292
293 // Decrypt and implant
294 auto priv = opt::getcxx . . $ mlspp/tls/opt/get(382ba35666c2b70b).(private_keycxx . . $ mlspp/TreeKEMPrivateKey#private_key(4d6c5a4be6fa7ce4).(res[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).resi]));
295 auto path_secret = priv.decryptcxx . . $ mlspp/HPKEPrivateKey#decrypt(e80468221b998043).(suitecxx . . $ mlspp/TreeKEMPrivateKey#suite.,
296 encrypt_labelcxx . . $ mlspp/encrypt_label/::update_path_nodecxx . . $ mlspp/encrypt_label/update_path_node.,
297 context,
298 path.nodescxx . . $ mlspp/UpdatePath#nodes.[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32).dpi].encrypted_path_secretcxx . . $ mlspp/UpdatePathNode#encrypted_path_secret.[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32).resi]);
299 implantcxx . . $ mlspp/TreeKEMPrivateKey#implant(333467ad79be4356).(pub, overlap_node, path_secret);
300
301 // Check that the resulting state is consistent with the public key
302 if (!consistentcxx . . $ mlspp/TreeKEMPrivateKey#consistent(eaed869e1938a2dc).(pub)) {
303 throw ProtocolErrorcxx . . $ mlspp/ProtocolError#("TreeKEMPublicKey inconsistent with TreeKEMPrivateKey");
304 }
305 }
306
307 void
308 TreeKEMPrivateKeycxx . . $ mlspp/TreeKEMPrivateKey#::truncatecxx . . $ mlspp/TreeKEMPrivateKey#truncate(a733f6ba51500b8c).(LeafCountcxx . . $ mlspp/LeafCount# size)
309 {
310 auto ni = NodeIndexcxx . . $ mlspp/NodeIndex#NodeIndex(5a3d3cee3fdf86fc).(LeafIndexcxx . . $ mlspp/LeafIndex#{ size.valcxx . . $ mlspp/UInt32#val. - 1 });
311 auto to_remove = stdcxx . . $ std/vector#vector(49f6e7a06ebc5aa8).::vectorcxx . . $ std/vector#<NodeIndexcxx . . $ mlspp/NodeIndex#>{};
312 for (const auto& entry : path_secretscxx . . $ mlspp/TreeKEMPrivateKey#path_secrets.) {
313 if (entry.firstcxx . . $ std/pair#first..valcxx . . $ mlspp/UInt32#val. > ni.valcxx . . $ mlspp/UInt32#val.) {
314 to_remove.push_backcxx . . $ std/vector#push_back(77d9f17553126ecb).(entry.firstcxx . . $ std/pair#first.);
315 }
316 }
317
318 for (auto n : to_remove) {
319 path_secretscxx . . $ mlspp/TreeKEMPrivateKey#path_secrets..erasecxx . . $ std/map#erase(a7de169f3ef4bfb3).(n);
320 private_key_cachecxx . . $ mlspp/TreeKEMPrivateKey#private_key_cache..erasecxx . . $ std/map#erase(a7de169f3ef4bfb3).(n);
321 }
322 }
323
324 bool
325 TreeKEMPrivateKeycxx . . $ mlspp/TreeKEMPrivateKey#::consistentcxx . . $ mlspp/TreeKEMPrivateKey#consistent(5ec773e44f7a02e9).(const TreeKEMPrivateKeycxx . . $ mlspp/TreeKEMPrivateKey#& other) const
326 {
327 if (suitecxx . . $ mlspp/TreeKEMPrivateKey#suite. !=cxx . . $ mlspp/`operator!=`(5401d31d8ac0aee4). other.suitecxx . . $ mlspp/TreeKEMPrivateKey#suite.) {
328 return false;
329 }
330
331 if (update_secretcxx . . $ mlspp/TreeKEMPrivateKey#update_secret. !=cxx . . $ mlspp/bytes_ns/bytes#`operator!=`(efd4f3c976ab92ce). other.update_secretcxx . . $ mlspp/TreeKEMPrivateKey#update_secret.) {
332 return false;
333 }
334
335 const auto match_if_present = [&](const auto& entry) {
336 auto other_entry = other.path_secretscxx . . $ mlspp/TreeKEMPrivateKey#path_secrets..findcxx . . $ std/map#find(7b3585bb5da8f7d).(entry.first);
337 if (other_entry ==cxx . . $ mlspp/`operator==`(bd3c88d6bafb6558). other.path_secretscxx . . $ mlspp/TreeKEMPrivateKey#path_secrets..endcxx . . $ std/map#end(b953fec338ad1a3b).()) {
338 return true;
339 }
340
341 return entry.second ==cxx . . $ mlspp/`operator==`(bd3c88d6bafb6558). other_entry->second;
342 };
343 return stdxcxx . . $ mlspp/stdx/::all_ofcxx . . $ mlspp/stdx/all_of(acb94948942028a1).(path_secretscxx . . $ mlspp/TreeKEMPrivateKey#path_secrets., match_if_present);
344 }
345
346 bool
347 TreeKEMPrivateKeycxx . . $ mlspp/TreeKEMPrivateKey#::consistentcxx . . $ mlspp/TreeKEMPrivateKey#consistent(eaed869e1938a2dc).(const TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#& other) const
348 {
349 if (suitecxx . . $ mlspp/TreeKEMPrivateKey#suite. !=cxx . . $ mlspp/`operator!=`(5401d31d8ac0aee4). other.suitecxx . . $ mlspp/TreeKEMPublicKey#suite.) {
350 return false;
351 }
352
353 for (const auto& [node, _] : path_secretscxx . . $ mlspp/TreeKEMPrivateKey#path_secrets.) {
354 private_keycxx . . $ mlspp/TreeKEMPrivateKey#private_key(ff9927d09a546cdc).(node);
355 }
356
357 return stdxcxx . . $ mlspp/stdx/::all_ofcxx . . $ mlspp/stdx/all_of(acb94948942028a1).(private_key_cachecxx . . $ mlspp/TreeKEMPrivateKey#private_key_cache., [othercxx . . $ mlspp/TreeKEMPublicKey#TreeKEMPublicKey(748824e58f275fcf).](const auto& entry) {
358 const auto& [node, priv] = entry;
359 const auto& opt_node = other.node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(81e4f0422d7e507e).(node).node;
360 if (!opt_node) {
361 // It's OK for a TreeKEMPrivateKey to have private keys
362 // for nodes that are blank in the TreeKEMPublicKey.
363 // This will happen traniently during Commit
364 // processing, since proposals will be applied in the
365 // public tree and not in the private tree.
366 return true;
367 }
368
369 const auto& pub = opt::getcxx . . $ mlspp/tls/opt/get(f3e04ae79584f296).(opt_node).public_key();
370 return priv.public_key ==cxx . . $ mlspp/`operator==`(bd3c88d6bafb6558). pub;
371 });
372 }
373
374 ///
375 /// TreeKEMPublicKey
376 ///
377
378 TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#::TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#TreeKEMPublicKey(1ae84ca71f879861).(CipherSuitecxx . . $ mlspp/CipherSuite# suite_in)
379 : suitecxx . . $ mlspp/TreeKEMPublicKey#suite.(suite_in)
380 {
381 }
382
383 LeafIndexcxx . . $ mlspp/LeafIndex#
384 TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#::allocate_leafcxx . . $ mlspp/TreeKEMPublicKey#allocate_leaf(1c00ca8acb9206b2).()
385 {
386 // Find the leftmost blank leaf node
387 auto index = LeafIndexcxx . . $ mlspp/LeafIndex#(0);
388 while (index.valcxx . . $ mlspp/UInt32#val. < sizecxx . . $ mlspp/TreeKEMPublicKey#size..valcxx . . $ mlspp/UInt32#val. && !node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(cb98b8279b84ec8f).(index).blankcxx . . $ mlspp/OptionalNode#blank(50ce9a9e25b4a850).()) {
389 index.valcxx . . $ mlspp/UInt32#val.++;
390 }
391
392 // Extend the tree if necessary
393 if (index.valcxx . . $ mlspp/UInt32#val. >= sizecxx . . $ mlspp/TreeKEMPublicKey#size..valcxx . . $ mlspp/UInt32#val.) {
394 if (sizecxx . . $ mlspp/TreeKEMPublicKey#size..valcxx . . $ mlspp/UInt32#val. == 0) {
395 sizecxx . . $ mlspp/TreeKEMPublicKey#size..valcxx . . $ mlspp/UInt32#val. = 1;
396 nodescxx . . $ mlspp/TreeKEMPublicKey#nodes..resizecxx . . $ std/vector#resize(2c44cd906ae4e829).(1);
397 } else {
398 sizecxx . . $ mlspp/TreeKEMPublicKey#size..valcxx . . $ mlspp/UInt32#val. *= 2;
399 nodescxx . . $ mlspp/TreeKEMPublicKey#nodes..resizecxx . . $ std/vector#resize(2c44cd906ae4e829).(2 * nodescxx . . $ mlspp/TreeKEMPublicKey#nodes..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() + 1);
400 }
401 }
402
403 return index;
404 }
405
406 LeafIndexcxx . . $ mlspp/LeafIndex#
407 TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#::add_leafcxx . . $ mlspp/TreeKEMPublicKey#add_leaf(9995a7bfeddd280).(const LeafNodecxx . . $ mlspp/LeafNode#& leaf)
408 {
409 // Check that the leaf node's keys are not already present in the tree
410 if (exists_in_treecxx . . $ mlspp/TreeKEMPublicKey#exists_in_tree(a28fd0d7d55e435a).(leaf.encryption_keycxx . . $ mlspp/LeafNode#encryption_key., stdcxx . . $ std/optional#optional(eb2253fdec8870b5).::nulloptcxx . . $ std/nullopt.)) {
411 throw InvalidParameterErrorcxx . . $ mlspp/InvalidParameterError#("Duplicate encryption key");
412 }
413
414 if (exists_in_treecxx . . $ mlspp/TreeKEMPublicKey#exists_in_tree(e34b2eb2d389dc07).(leaf.signature_keycxx . . $ mlspp/LeafNode#signature_key., stdcxx . . $ std/optional#optional(eb2253fdec8870b5).::nulloptcxx . . $ std/nullopt.)) {
415 throw InvalidParameterErrorcxx . . $ mlspp/InvalidParameterError#("Duplicate signature key");
416 }
417
418 // Allocate a blank leaf for this node
419 const auto index = allocate_leafcxx . . $ mlspp/TreeKEMPublicKey#allocate_leaf(1c00ca8acb9206b2).();
420
421 // Set the leaf
422 node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(cb98b8279b84ec8f).(index).nodecxx . . $ mlspp/OptionalNode#node. =cxx . . $ std/optional#`operator=`(4ce4dd0ee161fed). Nodecxx . . $ mlspp/Node#{ leaf };
423
424 // Update the unmerged list
425 for (auto& n : NodeIndexcxx . . $ mlspp/NodeIndex#NodeIndex(5a3d3cee3fdf86fc).(index).dirpathcxx . . $ mlspp/NodeIndex#dirpath(41abe7a05f92e8bb).(sizecxx . . $ mlspp/TreeKEMPublicKey#size.)) {
426 if (!node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(282ae2203a69cdd5).(n).nodecxx . . $ mlspp/OptionalNode#node.) {
427 continue;
428 }
429
430 auto& parent = var::getcxx . . $ std/get(55ed64a6518b24d0).<ParentNodecxx . . $ mlspp/ParentNode#>(opt::getcxx . . $ mlspp/tls/opt/get(f3e04ae79584f296).(node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(282ae2203a69cdd5).(n).nodecxx . . $ mlspp/OptionalNode#node.).nodecxx . . $ mlspp/Node#node.);
431
432 // Insert into unmerged leaves while maintaining order
433 const auto insert_point = stdxcxx . . $ mlspp/stdx/::upper_boundcxx . . $ mlspp/stdx/upper_bound(91f28ad055782622).(parent.unmerged_leavescxx . . $ mlspp/ParentNode#unmerged_leaves., index);
434 parent.unmerged_leavescxx . . $ mlspp/ParentNode#unmerged_leaves..insertcxx . . $ std/vector#insert(e54735f7c99f2c9d).(insert_point, index);
435 }
436
437 clear_hash_pathcxx . . $ mlspp/TreeKEMPublicKey#clear_hash_path(5a3d3cee3fdf86fc).(index);
438 return index;
439 }
440
441 void
442 TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#::update_leafcxx . . $ mlspp/TreeKEMPublicKey#update_leaf(b4be21d46bd512a5).(LeafIndexcxx . . $ mlspp/LeafIndex# index, const LeafNodecxx . . $ mlspp/LeafNode#& leaf)
443 {
444 // Check that the leaf node's keys are not already present in the tree, except
445 // for the signature key, which is allowed to repeat.
446 if (exists_in_treecxx . . $ mlspp/TreeKEMPublicKey#exists_in_tree(a28fd0d7d55e435a).(leaf.encryption_keycxx . . $ mlspp/LeafNode#encryption_key., stdcxx . . $ std/optional#optional(eb2253fdec8870b5).::nulloptcxx . . $ std/nullopt.)) {
447 throw InvalidParameterErrorcxx . . $ mlspp/InvalidParameterError#("Duplicate encryption key");
448 }
449
450 if (exists_in_treecxx . . $ mlspp/TreeKEMPublicKey#exists_in_tree(e34b2eb2d389dc07).(leaf.signature_keycxx . . $ mlspp/LeafNode#signature_key., indexcxx . . $ std/optional#optional(e3f0d6382bd52f24).)) {
451 throw InvalidParameterErrorcxx . . $ mlspp/InvalidParameterError#("Duplicate signature key");
452 }
453
454 blank_pathcxx . . $ mlspp/TreeKEMPublicKey#blank_path(5a3d3cee3fdf86fc).(index);
455 node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(282ae2203a69cdd5).(NodeIndexcxx . . $ mlspp/NodeIndex#NodeIndex(5a3d3cee3fdf86fc).(index)).nodecxx . . $ mlspp/OptionalNode#node. =cxx . . $ std/optional#`operator=`(4ce4dd0ee161fed). Nodecxx . . $ mlspp/Node#{ leaf };
456 clear_hash_pathcxx . . $ mlspp/TreeKEMPublicKey#clear_hash_path(5a3d3cee3fdf86fc).(index);
457 }
458
459 void
460 TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#::blank_pathcxx . . $ mlspp/TreeKEMPublicKey#blank_path(5a3d3cee3fdf86fc).(LeafIndexcxx . . $ mlspp/LeafIndex# index)
461 {
462 if (nodescxx . . $ mlspp/TreeKEMPublicKey#nodes..emptycxx . . $ std/vector#empty(3482b152b9333168).()) {
463 return;
464 }
465
466 auto ni = NodeIndexcxx . . $ mlspp/NodeIndex#NodeIndex(5a3d3cee3fdf86fc).(index);
467 node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(282ae2203a69cdd5).(ni).nodecxx . . $ mlspp/OptionalNode#node..resetcxx . . $ std/optional#reset(ced63f7c635d850d).();
468 for (auto n : ni.dirpathcxx . . $ mlspp/NodeIndex#dirpath(41abe7a05f92e8bb).(sizecxx . . $ mlspp/TreeKEMPublicKey#size.)) {
469 node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(282ae2203a69cdd5).(n).nodecxx . . $ mlspp/OptionalNode#node..resetcxx . . $ std/optional#reset(ced63f7c635d850d).();
470 }
471
472 clear_hash_pathcxx . . $ mlspp/TreeKEMPublicKey#clear_hash_path(5a3d3cee3fdf86fc).(index);
473 }
474
475 void
476 TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#::mergecxx . . $ mlspp/TreeKEMPublicKey#merge(5dcbaa0a66e57c87).(LeafIndexcxx . . $ mlspp/LeafIndex# from, const UpdatePathcxx . . $ mlspp/UpdatePath#& path)
477 {
478 update_leafcxx . . $ mlspp/TreeKEMPublicKey#update_leaf(b4be21d46bd512a5).(from, path.leaf_nodecxx . . $ mlspp/UpdatePath#leaf_node.);
479
480 auto dp = filtered_direct_pathcxx . . $ mlspp/TreeKEMPublicKey#filtered_direct_path(b755724fb327e77c).(NodeIndexcxx . . $ mlspp/NodeIndex#NodeIndex(5a3d3cee3fdf86fc).(from));
481 if (dp.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() != path.nodescxx . . $ mlspp/UpdatePath#nodes..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()) {
482 throw ProtocolErrorcxx . . $ mlspp/ProtocolError#("Malformed direct path");
483 }
484
485 auto ph = parent_hashescxx . . $ mlspp/TreeKEMPublicKey#parent_hashes(2f270ebbd853fb5).(from, dp, path.nodescxx . . $ mlspp/UpdatePath#nodes.);
486 for (size_t i = 0; i < dp.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).(); i++) {
487 auto [n, _res] = dpcxx . . $ std/tuple#tuple(f6149edcf2e66b11).[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i];
488
489 auto parent_hash = bytescxx . . $ mlspp/bytes_ns/bytes#bytes(ced63f7c635d850d).{};
490 if (i < dp.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() - 1) {
491 parent_hash =cxx . . $ mlspp/bytes_ns/bytes#`operator=`(89adb9997e4a49c). ph[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i + 1];
492 }
493
494 node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(282ae2203a69cdd5).(n).nodecxx . . $ mlspp/OptionalNode#node. =cxx . . $ std/optional#`operator=`(4ce4dd0ee161fed).
495 Nodecxx . . $ mlspp/Node#{ ParentNodecxx . . $ mlspp/ParentNode#{ path.nodescxx . . $ mlspp/UpdatePath#nodes.[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32).i].public_keycxx . . $ mlspp/UpdatePathNode#public_key., parent_hash, {} } };
496 }
497
498 set_hash_allcxx . . $ mlspp/TreeKEMPublicKey#set_hash_all(49f6e7a06ebc5aa8).();
499 }
500
501 void
502 TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#::set_hash_allcxx . . $ mlspp/TreeKEMPublicKey#set_hash_all(49f6e7a06ebc5aa8).()
503 {
504 auto r = NodeIndexcxx . . $ mlspp/NodeIndex#::rootcxx . . $ mlspp/NodeIndex#root(f0a39db46e675ba1).(sizecxx . . $ mlspp/TreeKEMPublicKey#size.);
505 get_hashcxx . . $ mlspp/TreeKEMPublicKey#get_hash(1e11ec888f0fea7b).(r);
506 }
507
508 bytescxx . . $ mlspp/bytes_ns/bytes#
509 TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#::root_hashcxx . . $ mlspp/TreeKEMPublicKey#root_hash(d7392ba79aa35767).() const
510 {
511 auto r = NodeIndexcxx . . $ mlspp/NodeIndex#::rootcxx . . $ mlspp/NodeIndex#root(f0a39db46e675ba1).(sizecxx . . $ mlspp/TreeKEMPublicKey#size.);
512 if (hashescxx . . $ mlspp/TreeKEMPublicKey#hashes..countcxx . . $ std/map#count(ea780b5adf43227).(r) == 0) {
513 throw InvalidParameterErrorcxx . . $ mlspp/InvalidParameterError#("Root hash not set");
514 }
515
516 return hashescxx . . $ mlspp/bytes_ns/bytes#bytes(68ca607eebb5be3e)..atcxx . . $ std/map#at(9bc8fa515171eae).(r);
517 }
518
519 bool
520 TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#::has_parent_hashcxx . . $ mlspp/TreeKEMPublicKey#has_parent_hash(a14d1792dfe14d87).(NodeIndexcxx . . $ mlspp/NodeIndex# child, const bytescxx . . $ mlspp/bytes_ns/bytes#& target_ph) const
521 {
522 const auto res = resolvecxx . . $ mlspp/TreeKEMPublicKey#resolve(54fac8e335512371).(child);
523 return stdxcxx . . $ mlspp/stdx/::any_ofcxx . . $ mlspp/stdx/any_of(acb94948942028a1).(res, [&](auto nr) {
524 return opt::getcxx . . $ mlspp/tls/opt/get(f3e04ae79584f296).(node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(81e4f0422d7e507e).(nr).node).parent_hash() ==cxx . . $ mlspp/`operator==`(bd3c88d6bafb6558). target_ph;
525 });
526 }
527
528 bool
529 TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#::parent_hash_validcxx . . $ mlspp/TreeKEMPublicKey#parent_hash_valid(50ce9a9e25b4a850).() const
530 {
531 auto cache = TreeHashCachecxx . . $ std/map#map(49f6e7a06ebc5aa8).{};
532
533 auto width = NodeCountcxx . . $ mlspp/NodeCount#NodeCount(a733f6ba51500b8c).(sizecxx . . $ mlspp/TreeKEMPublicKey#size.);
534 auto height = NodeIndexcxx . . $ mlspp/NodeIndex#::rootcxx . . $ mlspp/NodeIndex#root(f0a39db46e675ba1).(sizecxx . . $ mlspp/TreeKEMPublicKey#size.).levelcxx . . $ mlspp/NodeIndex#level(ddd8539a2fcdd4ce).();
535 for (auto level = uint32_tcxx . . $ uint32_t#(1); level <= height; level++) {
536 auto stride = uint32_tcxx . . $ uint32_t#(2) << level;
537 auto start = NodeIndexcxx . . $ mlspp/NodeIndex#{ (stride >> 1U) - 1 };
538
539 for (auto p = start; p.valcxx . . $ mlspp/UInt32#val. < width.valcxx . . $ mlspp/UInt32#val.; p.valcxx . . $ mlspp/UInt32#val. += stride) {
540 if (node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(7c807605eec2a484).(p).blankcxx . . $ mlspp/OptionalNode#blank(50ce9a9e25b4a850).()) {
541 continue;
542 }
543
544 auto l = p.leftcxx . . $ mlspp/NodeIndex#left(d66767722381bbc7).();
545 auto r = p.rightcxx . . $ mlspp/NodeIndex#right(d66767722381bbc7).();
546
547 auto lh = original_parent_hashcxx . . $ mlspp/TreeKEMPublicKey#original_parent_hash(1cc6965e803bdea9).(cache, p, r);
548 auto rh = original_parent_hashcxx . . $ mlspp/TreeKEMPublicKey#original_parent_hash(1cc6965e803bdea9).(cache, p, l);
549
550 if (!has_parent_hashcxx . . $ mlspp/TreeKEMPublicKey#has_parent_hash(a14d1792dfe14d87).(l, lh) && !has_parent_hashcxx . . $ mlspp/TreeKEMPublicKey#has_parent_hash(a14d1792dfe14d87).(r, rh)) {
551 dumpcxx . . $ mlspp/TreeKEMPublicKey#dump(83f5f38750778ac8).();
552 return false;
553 }
554 }
555 }
556 return true;
557 }
558
559 stdcxx . . $ std/::vectorcxx . . $ std/vector#<NodeIndexcxx . . $ mlspp/NodeIndex#>
560 TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#::resolvecxx . . $ mlspp/TreeKEMPublicKey#resolve(54fac8e335512371).(NodeIndexcxx . . $ mlspp/NodeIndex# index) const
561 {
562 auto at_leaf = (index.levelcxx . . $ mlspp/NodeIndex#level(ddd8539a2fcdd4ce).() == 0);
563 if (!node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(7c807605eec2a484).(index).blankcxx . . $ mlspp/OptionalNode#blank(50ce9a9e25b4a850).()) {
564 auto out = stdcxx . . $ std/vector#vector(9619226211e5a241).::vectorcxx . . $ std/vector#<NodeIndexcxx . . $ mlspp/NodeIndex#>{ index };
565 if (index.is_leafcxx . . $ mlspp/NodeIndex#is_leaf(50ce9a9e25b4a850).()) {
566 return outcxx . . $ std/vector#vector(6f559d22b039ccdb).;
567 }
568
569 const auto& node = node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(7c807605eec2a484).(index);
570 auto unmerged =
571 stdxcxx . . $ mlspp/stdx/::transformcxx . . $ mlspp/stdx/transform(895a059819fd6266).<NodeIndexcxx . . $ mlspp/NodeIndex#>(node.parent_nodecxx . . $ mlspp/OptionalNode#parent_node(17c9744d3cd194d).().unmerged_leavescxx . . $ mlspp/ParentNode#unmerged_leaves.,
572 [](LeafIndexcxx . . $ mlspp/LeafIndex# x) { return NodeIndexcxx . . $ mlspp/NodeIndex#NodeIndex(5a3d3cee3fdf86fc).(x); });
573
574 out.insertcxx . . $ std/vector#insert(134ef7853f0b3ebe).(outcxx . . $ __gnu_cxx/__normal_iterator#__normal_iterator(459a7e7b9fbe7640)..endcxx . . $ std/vector#end(55a6de06420d4903).(), unmerged.begincxx . . $ std/vector#begin(55a6de06420d4903).(), unmerged.endcxx . . $ std/vector#end(55a6de06420d4903).());
575 return outcxx . . $ std/vector#vector(6f559d22b039ccdb).;
576 }
577
578 if (at_leaf) {
579 return {cxx . . $ std/vector#vector(49f6e7a06ebc5aa8).};
580 }
581
582 auto l = resolvecxx . . $ mlspp/TreeKEMPublicKey#resolve(54fac8e335512371).(index.leftcxx . . $ mlspp/NodeIndex#left(d66767722381bbc7).());
583 auto r = resolvecxx . . $ mlspp/TreeKEMPublicKey#resolve(54fac8e335512371).(index.rightcxx . . $ mlspp/NodeIndex#right(d66767722381bbc7).());
584 l.insertcxx . . $ std/vector#insert(134ef7853f0b3ebe).(lcxx . . $ __gnu_cxx/__normal_iterator#__normal_iterator(459a7e7b9fbe7640)..endcxx . . $ std/vector#end(55a6de06420d4903).(), r.begincxx . . $ std/vector#begin(55a6de06420d4903).(), r.endcxx . . $ std/vector#end(55a6de06420d4903).());
585 return lcxx . . $ std/vector#vector(6f559d22b039ccdb).;
586 }
587
588 TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#::FilteredDirectPathcxx . . $ mlspp/TreeKEMPublicKey#FilteredDirectPath#
589 TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#::filtered_direct_pathcxx . . $ mlspp/TreeKEMPublicKey#filtered_direct_path(b755724fb327e77c).(NodeIndexcxx . . $ mlspp/NodeIndex# index) const
590 {
591 auto fdp = FilteredDirectPathcxx . . $ std/vector#vector(49f6e7a06ebc5aa8).{};
592
593 const auto cp = index.copathcxx . . $ mlspp/NodeIndex#copath(41abe7a05f92e8bb).(sizecxx . . $ mlspp/TreeKEMPublicKey#size.);
594 auto last = index;
595 for (auto n : cp) {
596 const auto p = n.parentcxx . . $ mlspp/NodeIndex#parent(d66767722381bbc7).();
597 const auto res = resolvecxx . . $ mlspp/TreeKEMPublicKey#resolve(54fac8e335512371).(n);
598 last =cxx . . $ mlspp/NodeIndex#`operator=`(f8d7ad8ae2e57adf). p;
599 if (res.emptycxx . . $ std/vector#empty(3482b152b9333168).()) {
600 continue;
601 }
602
603 fdp.emplace_backcxx . . $ std/vector#emplace_back(a1bcdfdabc3a37df).(p, res);
604 }
605
606 return fdpcxx . . $ std/vector#vector(6f559d22b039ccdb).;
607 }
608
609 bool
610 TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#::has_leafcxx . . $ mlspp/TreeKEMPublicKey#has_leaf(be225f04b72ff6f7).(LeafIndexcxx . . $ mlspp/LeafIndex# index) const
611 {
612 return !node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(81e4f0422d7e507e).(index).blankcxx . . $ mlspp/OptionalNode#blank(50ce9a9e25b4a850).();
613 }
614
615 stdcxx . . $ std/::optionalcxx . . $ std/optional#<LeafIndexcxx . . $ mlspp/LeafIndex#>
616 TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#::findcxx . . $ mlspp/TreeKEMPublicKey#find(ce1aa2f7913b80cb).(const LeafNodecxx . . $ mlspp/LeafNode#& leaf) const
617 {
618 for (LeafIndexcxx . . $ mlspp/LeafIndex# i{ 0 }; i <cxx . . $ mlspp/LeafIndex#`operator<`(6a6f1ee87d9a491e). sizecxx . . $ mlspp/TreeKEMPublicKey#size.; i.valcxx . . $ mlspp/UInt32#val.++) {
619 const auto& node = node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(81e4f0422d7e507e).(i);
620 if (!node.blankcxx . . $ mlspp/OptionalNode#blank(50ce9a9e25b4a850).() && node.leaf_nodecxx . . $ mlspp/OptionalNode#leaf_node(2a430397a2a37286).() ==cxx . . $ mlspp/`operator==`(5401d31d8ac0aee4). leaf) {
621 return icxx . . $ std/optional#optional(e3f0d6382bd52f24).;
622 }
623 }
624
625 return stdcxx . . $ std/optional#optional(eb2253fdec8870b5).::nulloptcxx . . $ std/nullopt.;
626 }
627
628 stdcxx . . $ std/::optionalcxx . . $ std/optional#<LeafNodecxx . . $ mlspp/LeafNode#>
629 TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#::leaf_nodecxx . . $ mlspp/TreeKEMPublicKey#leaf_node(e2d641b4ea45e2f).(LeafIndexcxx . . $ mlspp/LeafIndex# index) const
630 {
631 const auto& node = node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(81e4f0422d7e507e).(index);
632 if (node.blankcxx . . $ mlspp/OptionalNode#blank(50ce9a9e25b4a850).()) {
633 return stdcxx . . $ std/optional#optional(eb2253fdec8870b5).::nulloptcxx . . $ std/nullopt.;
634 }
635
636 return nodecxx . . $ std/optional#optional(e3f0d6382bd52f24)..leaf_nodecxx . . $ mlspp/OptionalNode#leaf_node(2a430397a2a37286).();
637 }
638
639 TreeKEMPrivateKeycxx . . $ mlspp/TreeKEMPrivateKey#
640 TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#::updatecxx . . $ mlspp/TreeKEMPublicKey#update(9c859f54cfdffec).(LeafIndexcxx . . $ mlspp/LeafIndex# from,
641 const bytescxx . . $ mlspp/bytes_ns/bytes#& leaf_secret,
642 const bytescxx . . $ mlspp/bytes_ns/bytes#& group_id,
643 const SignaturePrivateKeycxx . . $ mlspp/SignaturePrivateKey#& sig_priv,
644 const LeafNodeOptionscxx . . $ mlspp/LeafNodeOptions#& opts)
645 {
646 // Grab information about the sender
647 const auto& leaf_node = node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(cb98b8279b84ec8f).(from);
648 if (leaf_node.blankcxx . . $ mlspp/OptionalNode#blank(50ce9a9e25b4a850).()) {
649 throw InvalidParameterErrorcxx . . $ mlspp/InvalidParameterError#("Cannot update from blank node");
650 }
651
652 // Generate path secrets
653 auto priv = TreeKEMPrivateKeycxx . . $ mlspp/TreeKEMPrivateKey#::createcxx . . $ mlspp/TreeKEMPrivateKey#create(a23e82a828226b37).(*this, from, leaf_secret);
654 auto dp = filtered_direct_pathcxx . . $ mlspp/TreeKEMPublicKey#filtered_direct_path(b755724fb327e77c).(NodeIndexcxx . . $ mlspp/NodeIndex#NodeIndex(5a3d3cee3fdf86fc).(from));
655
656 // Encrypt path secrets to the copath, forming a stub UpdatePath with no
657 // encryptions
658 auto path_nodes = stdxcxx . . $ mlspp/stdx/::transformcxx . . $ mlspp/stdx/transform(895a059819fd6266).<UpdatePathNodecxx . . $ mlspp/UpdatePathNode#>(dp, [&](const auto& dpn) {
659 auto [n, _res] = dpn;
660
661 auto path_secret = priv.path_secretscxx . . $ mlspp/TreeKEMPrivateKey#path_secrets..atcxx . . $ std/map#at(bbd415b616967878).(n);
662 auto node_priv = opt::getcxx . . $ mlspp/tls/opt/get(f3e04ae79584f296).(priv.private_keycxx . . $ mlspp/TreeKEMPrivateKey#private_key(ff9927d09a546cdc).(n));
663
664 return UpdatePathNodecxx . . $ mlspp/UpdatePathNode#{ node_priv.public_key, {} };
665 });
666
667 // Update and re-sign the leaf_node
668 auto ph = parent_hashescxx . . $ mlspp/TreeKEMPublicKey#parent_hashes(2f270ebbd853fb5).(from, dp, path_nodes);
669 auto ph0 = bytescxx . . $ mlspp/bytes_ns/bytes#bytes(ced63f7c635d850d).{};
670 if (!ph.emptycxx . . $ std/vector#empty(3482b152b9333168).()) {
671 ph0 =cxx . . $ mlspp/bytes_ns/bytes#`operator=`(89adb9997e4a49c). ph[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).0];
672 }
673
674 auto leaf_pub = opt::getcxx . . $ mlspp/tls/opt/get(382ba35666c2b70b).(priv.private_keycxx . . $ mlspp/TreeKEMPrivateKey#private_key(4d6c5a4be6fa7ce4).(NodeIndexcxx . . $ mlspp/NodeIndex#NodeIndex(5a3d3cee3fdf86fc).(from))).public_keycxx . . $ mlspp/HPKEPrivateKey#public_key.;
675 auto new_leaf = leaf_node.leaf_nodecxx . . $ mlspp/OptionalNode#leaf_node(2a430397a2a37286).().for_commitcxx . . $ mlspp/LeafNode#for_commit(991fd3b2d5fd8366).(
676 suitecxx . . $ mlspp/TreeKEMPublicKey#suite., group_id, from, leaf_pub, ph0, opts, sig_priv);
677
678 // Merge the changes into the tree
679 mergecxx . . $ mlspp/TreeKEMPublicKey#merge(5dcbaa0a66e57c87).(from, UpdatePathcxx . . $ mlspp/UpdatePath#{ stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(new_leaf), stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(path_nodes) });
680
681 return priv;
682 }
683
684 UpdatePathcxx . . $ mlspp/UpdatePath#
685 TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#::encapcxx . . $ mlspp/TreeKEMPublicKey#encap(b02cdda980f00156).(const TreeKEMPrivateKeycxx . . $ mlspp/TreeKEMPrivateKey#& priv,
686 const bytescxx . . $ mlspp/bytes_ns/bytes#& context,
687 const stdcxx . . $ std/::vectorcxx . . $ std/vector#<LeafIndexcxx . . $ mlspp/LeafIndex#>& except) const
688 {
689 auto dp = filtered_direct_pathcxx . . $ mlspp/TreeKEMPublicKey#filtered_direct_path(b755724fb327e77c).(NodeIndexcxx . . $ mlspp/NodeIndex#NodeIndex(5a3d3cee3fdf86fc).(priv.indexcxx . . $ mlspp/TreeKEMPrivateKey#index.));
690
691 // Encrypt path secrets to the copath
692 auto path_nodes = stdxcxx . . $ mlspp/stdx/::transformcxx . . $ mlspp/stdx/transform(895a059819fd6266).<UpdatePathNodecxx . . $ mlspp/UpdatePathNode#>(dp, [&](const auto& dpn) {
693 // We need the copy here so that we can modify the resolution.
694 // NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
695 auto [n, res] = dpn;
696 remove_leavescxx . . $ mlspp/remove_leaves(fec29baccf868da2).(res, except);
697
698 auto path_secret = priv.path_secretscxx . . $ mlspp/TreeKEMPrivateKey#path_secrets..atcxx . . $ std/map#at(bbd415b616967878).(n);
699 auto node_priv = opt::getcxx . . $ mlspp/tls/opt/get(f3e04ae79584f296).(priv.private_keycxx . . $ mlspp/TreeKEMPrivateKey#private_key(ff9927d09a546cdc).(n));
700
701 auto ct = stdxcxx . . $ mlspp/stdx/::transformcxx . . $ mlspp/stdx/transform(895a059819fd6266).<HPKECiphertextcxx . . $ mlspp/HPKECiphertext#>(res, [&](auto nr) {
702 const auto& node_pub = opt::getcxx . . $ mlspp/tls/opt/get(f3e04ae79584f296).(node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(81e4f0422d7e507e).(nr).node).public_key();
703 return node_pub.encrypt(
704 suitecxx . . $ mlspp/TreeKEMPublicKey#suite., encrypt_labelcxx . . $ mlspp/encrypt_label/::update_path_nodecxx . . $ mlspp/encrypt_label/update_path_node., context, path_secret);
705 });
706
707 return UpdatePathNodecxx . . $ mlspp/UpdatePathNode#{ node_priv.public_key, stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(ct) };
708 });
709
710 // Package everything into an UpdatePath
711 auto new_leaf = optcxx . . $ mlspp/LeafNode#LeafNode(d63c1b60f6df9256).::getcxx . . $ mlspp/tls/opt/get(382ba35666c2b70b).(leaf_nodecxx . . $ mlspp/TreeKEMPublicKey#leaf_node(e2d641b4ea45e2f).(priv.indexcxx . . $ mlspp/TreeKEMPrivateKey#index.));
712 auto path = UpdatePathcxx . . $ mlspp/UpdatePath#{ new_leaf, stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(path_nodes) };
713
714 return path;
715 }
716
717 void
718 TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#::truncatecxx . . $ mlspp/TreeKEMPublicKey#truncate(49f6e7a06ebc5aa8).()
719 {
720 if (sizecxx . . $ mlspp/TreeKEMPublicKey#size..valcxx . . $ mlspp/UInt32#val. == 0) {
721 return;
722 }
723
724 // Clear the parent hashes across blank leaves before truncating
725 auto index = LeafIndexcxx . . $ mlspp/LeafIndex#{ sizecxx . . $ mlspp/TreeKEMPublicKey#size..valcxx . . $ mlspp/UInt32#val. - 1 };
726 for (; index.valcxx . . $ mlspp/UInt32#val. > 0; index.valcxx . . $ mlspp/UInt32#val.--) {
727 if (!node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(cb98b8279b84ec8f).(index).blankcxx . . $ mlspp/OptionalNode#blank(50ce9a9e25b4a850).()) {
728 break;
729 }
730 clear_hash_pathcxx . . $ mlspp/TreeKEMPublicKey#clear_hash_path(5a3d3cee3fdf86fc).(index);
731 }
732
733 if (node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(cb98b8279b84ec8f).(index).blankcxx . . $ mlspp/OptionalNode#blank(50ce9a9e25b4a850).()) {
734 nodescxx . . $ mlspp/TreeKEMPublicKey#nodes..clearcxx . . $ std/vector#clear(ced63f7c635d850d).();
735 return;
736 }
737
738 // Remove the right subtree until the tree is of minimal size
739 while (sizecxx . . $ mlspp/TreeKEMPublicKey#size..valcxx . . $ mlspp/UInt32#val. / 2 > index.valcxx . . $ mlspp/UInt32#val.) {
740 nodescxx . . $ mlspp/TreeKEMPublicKey#nodes..resizecxx . . $ std/vector#resize(2c44cd906ae4e829).(nodescxx . . $ mlspp/TreeKEMPublicKey#nodes..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() / 2);
741 sizecxx . . $ mlspp/TreeKEMPublicKey#size..valcxx . . $ mlspp/UInt32#val. /= 2;
742 }
743 }
744
745 OptionalNodecxx . . $ mlspp/OptionalNode#&
746 TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#::node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(282ae2203a69cdd5).(NodeIndexcxx . . $ mlspp/NodeIndex# n)
747 {
748 auto width = NodeCountcxx . . $ mlspp/NodeCount#NodeCount(a733f6ba51500b8c).(sizecxx . . $ mlspp/TreeKEMPublicKey#size.);
749 if (n.valcxx . . $ mlspp/UInt32#val. >= width.valcxx . . $ mlspp/UInt32#val.) {
750 throw InvalidParameterErrorcxx . . $ mlspp/InvalidParameterError#("Node index not in tree");
751 }
752
753 if (n.valcxx . . $ mlspp/UInt32#val. >= nodescxx . . $ mlspp/TreeKEMPublicKey#nodes..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()) {
754 return blank_nodecxx . . $ mlspp/TreeKEMPublicKey#blank_node.;
755 }
756
757 return nodescxx . . $ mlspp/TreeKEMPublicKey#nodes..atcxx . . $ std/vector#at(428bb168810b9e52).(n.valcxx . . $ mlspp/UInt32#val.);
758 }
759
760 const OptionalNodecxx . . $ mlspp/OptionalNode#&
761 TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#::node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(7c807605eec2a484).(NodeIndexcxx . . $ mlspp/NodeIndex# n) const
762 {
763 auto width = NodeCountcxx . . $ mlspp/NodeCount#NodeCount(a733f6ba51500b8c).(sizecxx . . $ mlspp/TreeKEMPublicKey#size.);
764 if (n.valcxx . . $ mlspp/UInt32#val. >= width.valcxx . . $ mlspp/UInt32#val.) {
765 throw InvalidParameterErrorcxx . . $ mlspp/InvalidParameterError#("Node index not in tree");
766 }
767
768 if (n.valcxx . . $ mlspp/UInt32#val. >= nodescxx . . $ mlspp/TreeKEMPublicKey#nodes..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()) {
769 return blank_nodecxx . . $ mlspp/TreeKEMPublicKey#blank_node.;
770 }
771
772 return nodescxx . . $ mlspp/TreeKEMPublicKey#nodes..atcxx . . $ std/vector#at(b61e45a10b63f40a).(n.valcxx . . $ mlspp/UInt32#val.);
773 }
774
775 OptionalNodecxx . . $ mlspp/OptionalNode#&
776 TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#::node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(cb98b8279b84ec8f).(LeafIndexcxx . . $ mlspp/LeafIndex# n)
777 {
778 return node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(282ae2203a69cdd5).(NodeIndexcxx . . $ mlspp/NodeIndex#NodeIndex(5a3d3cee3fdf86fc).(n));
779 }
780
781 const OptionalNodecxx . . $ mlspp/OptionalNode#&
782 TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#::node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(81e4f0422d7e507e).(LeafIndexcxx . . $ mlspp/LeafIndex# n) const
783 {
784 return node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(7c807605eec2a484).(NodeIndexcxx . . $ mlspp/NodeIndex#NodeIndex(5a3d3cee3fdf86fc).(n));
785 }
786
787 void
788 TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#::clear_hash_allcxx . . $ mlspp/TreeKEMPublicKey#clear_hash_all(49f6e7a06ebc5aa8).()
789 {
790 hashescxx . . $ mlspp/TreeKEMPublicKey#hashes..clearcxx . . $ std/map#clear(ced63f7c635d850d).();
791 }
792
793 void
794 TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#::clear_hash_pathcxx . . $ mlspp/TreeKEMPublicKey#clear_hash_path(5a3d3cee3fdf86fc).(LeafIndexcxx . . $ mlspp/LeafIndex# index)
795 {
796 auto dp = NodeIndexcxx . . $ mlspp/NodeIndex#NodeIndex(5a3d3cee3fdf86fc).(index).dirpathcxx . . $ mlspp/NodeIndex#dirpath(41abe7a05f92e8bb).(sizecxx . . $ mlspp/TreeKEMPublicKey#size.);
797 hashescxx . . $ mlspp/TreeKEMPublicKey#hashes..erasecxx . . $ std/map#erase(a7de169f3ef4bfb3).(NodeIndexcxx . . $ mlspp/NodeIndex#NodeIndex(5a3d3cee3fdf86fc).(index));
798 for (auto n : dp) {
799 hashescxx . . $ mlspp/TreeKEMPublicKey#hashes..erasecxx . . $ std/map#erase(a7de169f3ef4bfb3).(n);
800 }
801 }
802
803 struct LeafNodeHashInputcxx . . $ mlspp/LeafNodeHashInput#
804 {
805 LeafIndexcxx . . $ mlspp/LeafIndex# leaf_indexcxx . . $ mlspp/LeafNodeHashInput#leaf_index.;
806 stdcxx . . $ std/::optionalcxx . . $ std/optional#<LeafNodecxx . . $ mlspp/LeafNode#> leaf_nodecxx . . $ mlspp/LeafNodeHashInput#leaf_node.;
807 TLS_SERIALIZABLEcxx . . $ `mlspp/lib/bytes/include/tls/tls_syntax.h:333:9`!(leaf_index, leaf_node)
808 };
809
810 struct ParentNodeHashInputcxx . . $ mlspp/ParentNodeHashInput#
811 {
812 stdcxx . . $ std/::optionalcxx . . $ std/optional#<ParentNodecxx . . $ mlspp/ParentNode#> parent_nodecxx . . $ mlspp/ParentNodeHashInput#parent_node.;
813 const bytescxx . . $ mlspp/bytes_ns/bytes#& left_hashcxx . . $ mlspp/ParentNodeHashInput#left_hash.;
814 const bytescxx . . $ mlspp/bytes_ns/bytes#& right_hashcxx . . $ mlspp/ParentNodeHashInput#right_hash.;
815 TLS_SERIALIZABLEcxx . . $ `mlspp/lib/bytes/include/tls/tls_syntax.h:333:9`!(parent_node, left_hash, right_hash)
816 };
817
818 struct TreeHashInputcxx . . $ mlspp/TreeHashInput#
819 {
820 var::variantcxx . . $ std/variant#<LeafNodeHashInputcxx . . $ mlspp/LeafNodeHashInput#, ParentNodeHashInputcxx . . $ mlspp/ParentNodeHashInput#> nodecxx . . $ mlspp/TreeHashInput#node.;
821 TLS_SERIALIZABLEcxx . . $ `mlspp/lib/bytes/include/tls/tls_syntax.h:333:9`!(node);
822 TLS_TRAITScxx . . $ `mlspp/lib/bytes/include/tls/tls_syntax.h:346:9`!(tls::variant<NodeType>)
823 };
824
825 const bytescxx . . $ mlspp/bytes_ns/bytes#&
826 TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#::get_hashcxx . . $ mlspp/TreeKEMPublicKey#get_hash(1e11ec888f0fea7b).(NodeIndexcxx . . $ mlspp/NodeIndex# index)
827 {
828 if (hashescxx . . $ mlspp/TreeKEMPublicKey#hashes..countcxx . . $ std/map#count(ea780b5adf43227).(index) > 0) {
829 return hashescxx . . $ mlspp/TreeKEMPublicKey#hashes..atcxx . . $ std/map#at(bbd415b616967878).(index);
830 }
831
832 auto hash_input = bytescxx . . $ mlspp/bytes_ns/bytes#bytes(ced63f7c635d850d).{};
833 const auto& node = node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(282ae2203a69cdd5).(index);
834 if (index.levelcxx . . $ mlspp/NodeIndex#level(ddd8539a2fcdd4ce).() == 0) {
835 auto input = LeafNodeHashInputcxx . . $ mlspp/LeafNodeHashInput#{ LeafIndexcxx . . $ mlspp/LeafIndex#(index), {} };
836 if (!node.blankcxx . . $ mlspp/OptionalNode#blank(50ce9a9e25b4a850).()) {
837 input.leaf_nodecxx . . $ mlspp/LeafNodeHashInput#leaf_node. =cxx . . $ std/optional#`operator=`(4ce4dd0ee161fed). node.leaf_nodecxx . . $ mlspp/OptionalNode#leaf_node(2a430397a2a37286).();
838 }
839
840 hash_input =cxx . . $ mlspp/bytes_ns/bytes#`operator=`(463b53a2545785ed). tlscxx . . $ mlspp/bytes_ns/bytes#bytes(dd8c33c7e39dbe02).::marshalcxx . . $ mlspp/tls/marshal(7df0b13a9cabe487).(TreeHashInputcxx . . $ mlspp/TreeHashInput#{ input });
841 } else {
842 auto input = ParentNodeHashInputcxx . . $ mlspp/ParentNodeHashInput#{
843 {},
844 get_hashcxx . . $ mlspp/TreeKEMPublicKey#get_hash(1e11ec888f0fea7b).(index.leftcxx . . $ mlspp/NodeIndex#left(d66767722381bbc7).()),
845 get_hashcxx . . $ mlspp/TreeKEMPublicKey#get_hash(1e11ec888f0fea7b).(index.rightcxx . . $ mlspp/NodeIndex#right(d66767722381bbc7).()),
846 };
847
848 if (!node.blankcxx . . $ mlspp/OptionalNode#blank(50ce9a9e25b4a850).()) {
849 input.parent_nodecxx . . $ mlspp/ParentNodeHashInput#parent_node. =cxx . . $ std/optional#`operator=`(4ce4dd0ee161fed). node.parent_nodecxx . . $ mlspp/OptionalNode#parent_node(17c9744d3cd194d).();
850 }
851
852 hash_input =cxx . . $ mlspp/bytes_ns/bytes#`operator=`(463b53a2545785ed). tlscxx . . $ mlspp/bytes_ns/bytes#bytes(dd8c33c7e39dbe02).::marshalcxx . . $ mlspp/tls/marshal(7df0b13a9cabe487).(TreeHashInputcxx . . $ mlspp/TreeHashInput#{ input });
853 }
854
855 auto hash = suitecxx . . $ mlspp/TreeKEMPublicKey#suite..digestcxx . . $ mlspp/CipherSuite#digest(cdb80d7d9b9bc276).().hashcxx . . $ mlspp/hpke/Digest#hash(c5e1b85e16e94921).(hash_input);
856 hashescxx . . $ mlspp/TreeKEMPublicKey#hashes..insert_or_assigncxx . . $ std/map#insert_or_assign(a356f559b7c7a273).(index, hash);
857 return hashescxx . . $ mlspp/TreeKEMPublicKey#hashes..atcxx . . $ std/map#at(bbd415b616967878).(index);
858 }
859
860 // struct {
861 // HPKEPublicKey encryption_key;
862 // opaque parent_hash<V>;
863 // opaque original_sibling_tree_hash<V>;
864 // } ParentHashInput;
865 struct ParentHashInputcxx . . $ mlspp/ParentHashInput#
866 {
867 const HPKEPublicKeycxx . . $ mlspp/HPKEPublicKey#& public_keycxx . . $ mlspp/ParentHashInput#public_key.;
868 const bytescxx . . $ mlspp/bytes_ns/bytes#& parent_hashcxx . . $ mlspp/ParentHashInput#parent_hash.;
869 const bytescxx . . $ mlspp/bytes_ns/bytes#& original_child_resolutioncxx . . $ mlspp/ParentHashInput#original_child_resolution.;
870
871 TLS_SERIALIZABLEcxx . . $ `mlspp/lib/bytes/include/tls/tls_syntax.h:333:9`!(public_key, parent_hash, original_child_resolution)
872 };
873
874 bytescxx . . $ mlspp/bytes_ns/bytes#
875 TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#::parent_hashcxx . . $ mlspp/TreeKEMPublicKey#parent_hash(600469b6cb5d3224).(const ParentNodecxx . . $ mlspp/ParentNode#& parent,
876 NodeIndexcxx . . $ mlspp/NodeIndex# copath_child) const
877 {
878 if (hashescxx . . $ mlspp/TreeKEMPublicKey#hashes..countcxx . . $ std/map#count(ea780b5adf43227).(copath_child) == 0) {
879 throw InvalidParameterErrorcxx . . $ mlspp/InvalidParameterError#("Child hash not set");
880 }
881
882 auto hash_input = ParentHashInputcxx . . $ mlspp/ParentHashInput#{
883 parent.public_keycxx . . $ mlspp/ParentNode#public_key.,
884 parent.parent_hashcxx . . $ mlspp/ParentNode#parent_hash.,
885 hashescxx . . $ mlspp/TreeKEMPublicKey#hashes..atcxx . . $ std/map#at(9bc8fa515171eae).(copath_child),
886 };
887
888 return suitecxx . . $ mlspp/TreeKEMPublicKey#suite..digestcxx . . $ mlspp/CipherSuite#digest(cdb80d7d9b9bc276).().hashcxx . . $ mlspp/hpke/Digest#hash(c5e1b85e16e94921).(tlscxx . . $ mlspp/bytes_ns/bytes#bytes(dd8c33c7e39dbe02).::marshalcxx . . $ mlspp/tls/marshal(7df0b13a9cabe487).(hash_input));
889 }
890
891 stdcxx . . $ std/::vectorcxx . . $ std/vector#<bytescxx . . $ mlspp/bytes_ns/bytes#>
892 TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#::parent_hashescxx . . $ mlspp/TreeKEMPublicKey#parent_hashes(2f270ebbd853fb5).(
893 LeafIndexcxx . . $ mlspp/LeafIndex# from,
894 const FilteredDirectPathcxx . . $ mlspp/TreeKEMPublicKey#FilteredDirectPath#& fdp,
895 const stdcxx . . $ std/::vectorcxx . . $ std/vector#<UpdatePathNodecxx . . $ mlspp/UpdatePathNode#>& path_nodes) const
896 {
897 // An empty filtered direct path indicates a one-member tree, since there's
898 // nobody else there to encrypt with. In this special case, there's no
899 // parent hashing to be done.
900 if (fdp.emptycxx . . $ std/vector#empty(3482b152b9333168).()) {
901 return {cxx . . $ std/vector#vector(49f6e7a06ebc5aa8).};
902 }
903
904 // The list of nodes for whom parent hashes are computed, namely: Direct path
905 // excluding the last entry, including leaf
906 auto from_node = NodeIndexcxx . . $ mlspp/NodeIndex#NodeIndex(5a3d3cee3fdf86fc).(from);
907 auto dp = fdpcxx . . $ std/vector#vector(a1201200cf0efa42).;
908 auto [last, _res_last] = dpcxx . . $ std/tuple#tuple(f6149edcf2e66b11)..backcxx . . $ std/vector#back(6baabfa6fc7b3a8e).();
909 dp.pop_backcxx . . $ std/vector#pop_back(ced63f7c635d850d).();
910 dp.insertcxx . . $ std/vector#insert(d9a51c9ec3a8b22).(dpcxx . . $ __gnu_cxx/__normal_iterator#__normal_iterator(459a7e7b9fbe7640)..begincxx . . $ std/vector#begin(55a6de06420d4903).(), {cxx . . $ std/tuple#tuple(68f3cec925df479f). from_node, {cxx . . $ std/vector#vector(49f6e7a06ebc5aa8).} });
911
912 if (dp.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() != path_nodes.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()) {
913 throw ProtocolErrorcxx . . $ mlspp/ProtocolError#("Malformed UpdatePath");
914 }
915
916 // Parent hash for all the parents, starting from the last entry of the
917 // filtered direct path
918 auto last_hash = bytescxx . . $ mlspp/bytes_ns/bytes#bytes(ced63f7c635d850d).{};
919 auto ph = stdcxx . . $ std/::vectorcxx . . $ std/vector#<bytescxx . . $ mlspp/bytes_ns/bytes#>(dp.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).());
920 for (int i = static_cast<int>(dp.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()) - 1; i >= 0; i--) {
921 auto [n, _res] = dpcxx . . $ std/tuple#tuple(f6149edcf2e66b11).[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i];
922 auto s = n.siblingcxx . . $ mlspp/NodeIndex#sibling(6f0fa48a65b17a10).(last);
923
924 auto parent_node = ParentNodecxx . . $ mlspp/ParentNode#{ path_nodes[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32).i].public_keycxx . . $ mlspp/UpdatePathNode#public_key., last_hash, {} };
925 last_hash =cxx . . $ mlspp/bytes_ns/bytes#`operator=`(463b53a2545785ed). parent_hashcxx . . $ mlspp/TreeKEMPublicKey#parent_hash(600469b6cb5d3224).(parent_node, s);
926 ph[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i] =cxx . . $ mlspp/bytes_ns/bytes#`operator=`(89adb9997e4a49c). last_hash;
927
928 last =cxx . . $ mlspp/NodeIndex#`operator=`(f8d7ad8ae2e57adf). n;
929 }
930
931 return phcxx . . $ std/vector#vector(6f559d22b039ccdb).;
932 }
933
934 const bytescxx . . $ mlspp/bytes_ns/bytes#&
935 TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#::original_tree_hashcxx . . $ mlspp/TreeKEMPublicKey#original_tree_hash(dfa38275fb6e05bc).(TreeHashCachecxx . . $ mlspp/TreeKEMPublicKey#TreeHashCache#& cache,
936 NodeIndexcxx . . $ mlspp/NodeIndex# index,
937 stdcxx . . $ std/::vectorcxx . . $ std/vector#<LeafIndexcxx . . $ mlspp/LeafIndex#> parent_except) const
938 {
939 // Scope the unmerged leaves list down to this subtree
940 auto except = stdcxx . . $ std/vector#vector(49f6e7a06ebc5aa8).::vectorcxx . . $ std/vector#<LeafIndexcxx . . $ mlspp/LeafIndex#>{};
941 stdcxx . . $ std/::copy_ifcxx . . $ std/copy_if(da30451c4a040264).(parent_except.begincxx . . $ std/vector#begin(55a6de06420d4903).(),
942 parent_except.endcxx . . $ std/vector#end(55a6de06420d4903).(),
943 stdcxx . . $ std/::back_insertercxx . . $ std/back_inserter(7b559d6bdd90ff74).(except),
944 [&](auto i) { return NodeIndexcxx . . $ mlspp/NodeIndex#(i).is_belowcxx . . $ mlspp/NodeIndex#is_below(df9c6d3ab1251738).(index); });
945
946 auto have_local_changes = !except.emptycxx . . $ std/vector#empty(3482b152b9333168).();
947
948 // If there are no local changes, then we can use the cached tree hash
949 if (!have_local_changes) {
950 return hashescxx . . $ mlspp/TreeKEMPublicKey#hashes..atcxx . . $ std/map#at(9bc8fa515171eae).(index);
951 }
952
953 // If this method has been called before with the same number of excluded
954 // leaves (which implies the same set), then use the cached value.
955 if (auto it = cache.findcxx . . $ std/map#find(36f8d56706583706).(index); it !=cxx . . $ std/`operator!=`(be6a71a859368080). cache.endcxx . . $ std/map#end(7e4de33083b6e738).()) {
956 const auto& [key, value] = *cxx . . $ std/_Rb_tree_iterator#`operator*`(d426bc7b625886b7).it;
957 const auto& [except_size, hash] = value;
958 if (except_size == except.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()) {
959 return hash;
960 }
961 }
962
963 // If there is no entry in either cache, recompute the value
964 auto hash = bytescxx . . $ mlspp/bytes_ns/bytes#bytes(ced63f7c635d850d).{};
965 if (index.is_leafcxx . . $ mlspp/NodeIndex#is_leaf(50ce9a9e25b4a850).()) {
966 // A leaf node with local changes is by definition excluded from the parent
967 // hash. So we return the hash of an empty leaf.
968 auto leaf_hash_input = LeafNodeHashInputcxx . . $ mlspp/LeafNodeHashInput#{ LeafIndexcxx . . $ mlspp/LeafIndex#(index), stdcxx . . $ std/::nulloptcxx . . $ std/nullopt. };
969 hash =cxx . . $ mlspp/bytes_ns/bytes#`operator=`(463b53a2545785ed). suitecxx . . $ mlspp/TreeKEMPublicKey#suite..digestcxx . . $ mlspp/CipherSuite#digest(cdb80d7d9b9bc276).().hashcxx . . $ mlspp/hpke/Digest#hash(c5e1b85e16e94921).(tlscxx . . $ mlspp/bytes_ns/bytes#bytes(dd8c33c7e39dbe02).::marshalcxx . . $ mlspp/tls/marshal(7df0b13a9cabe487).(TreeHashInputcxx . . $ mlspp/TreeHashInput#{ leaf_hash_input }));
970 } else {
971 // If there is no cached value, recalculate the child hashes with the
972 // specified `except` list, removing the `except` list from
973 // `unmerged_leaves`.
974 auto parent_hash_input = ParentNodeHashInputcxx . . $ mlspp/ParentNodeHashInput#{
975 stdcxx . . $ std/::nulloptcxx . . $ std/nullopt.,
976 original_tree_hashcxx . . $ mlspp/TreeKEMPublicKey#original_tree_hash(dfa38275fb6e05bc).(cache, index.leftcxx . . $ mlspp/NodeIndex#left(d66767722381bbc7).(), exceptcxx . . $ std/vector#vector(a1201200cf0efa42).),
977 original_tree_hashcxx . . $ mlspp/TreeKEMPublicKey#original_tree_hash(dfa38275fb6e05bc).(cache, index.rightcxx . . $ mlspp/NodeIndex#right(d66767722381bbc7).(), exceptcxx . . $ std/vector#vector(a1201200cf0efa42).),
978 };
979
980 if (!node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(7c807605eec2a484).(index).blankcxx . . $ mlspp/OptionalNode#blank(50ce9a9e25b4a850).()) {
981 parent_hash_input.parent_nodecxx . . $ mlspp/ParentNodeHashInput#parent_node. =cxx . . $ std/optional#`operator=`(4ce4dd0ee161fed). node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(7c807605eec2a484).(index).parent_nodecxx . . $ mlspp/OptionalNode#parent_node(17c9744d3cd194d).();
982 auto& unmerged_leaves =
983 opt::getcxx . . $ mlspp/tls/opt/get(f3e04ae79584f296).(parent_hash_input.parent_nodecxx . . $ mlspp/ParentNodeHashInput#parent_node.).unmerged_leavescxx . . $ mlspp/ParentNode#unmerged_leaves.;
984 auto end = stdcxx . . $ std/::remove_ifcxx . . $ std/remove_if(f0a0a4f94ee5cb00).(
985 unmerged_leaves.begincxx . . $ std/vector#begin(55a6de06420d4903).(), unmerged_leaves.endcxx . . $ std/vector#end(55a6de06420d4903).(), [&](auto leaf) {
986 return stdcxx . . $ std/::countcxx . . $ std/count(698bbaade62a0610).(except.begincxx . . $ std/vector#begin(55a6de06420d4903).(), except.endcxx . . $ std/vector#end(55a6de06420d4903).(), leaf) !=cxx . . $ mlspp/`operator!=`(bd3c88d6bafb6558). 0;
987 });
988 unmerged_leaves.erasecxx . . $ std/vector#erase(cc2a997b00bceed8).(endcxx . . $ __gnu_cxx/__normal_iterator#__normal_iterator(459a7e7b9fbe7640)., unmerged_leavescxx . . $ __gnu_cxx/__normal_iterator#__normal_iterator(459a7e7b9fbe7640)..endcxx . . $ std/vector#end(55a6de06420d4903).());
989 }
990
991 hash =cxx . . $ mlspp/bytes_ns/bytes#`operator=`(463b53a2545785ed).
992 suitecxx . . $ mlspp/TreeKEMPublicKey#suite..digestcxx . . $ mlspp/CipherSuite#digest(cdb80d7d9b9bc276).().hashcxx . . $ mlspp/hpke/Digest#hash(c5e1b85e16e94921).(tlscxx . . $ mlspp/bytes_ns/bytes#bytes(dd8c33c7e39dbe02).::marshalcxx . . $ mlspp/tls/marshal(7df0b13a9cabe487).(TreeHashInputcxx . . $ mlspp/TreeHashInput#{ parent_hash_input }));
993 }
994
995 cache.insert_or_assigncxx . . $ std/map#insert_or_assign(a356f559b7c7a273).(index, stdcxx . . $ std/::make_paircxx . . $ std/make_pair(1d30b6e115203116).(except.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).(), hash));
996 return cache.atcxx . . $ std/map#at(bbd415b616967878).(index).secondcxx . . $ std/pair#second.;
997 }
998
999 bytescxx . . $ mlspp/bytes_ns/bytes#
1000 TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#::original_parent_hashcxx . . $ mlspp/TreeKEMPublicKey#original_parent_hash(1cc6965e803bdea9).(TreeHashCachecxx . . $ mlspp/TreeKEMPublicKey#TreeHashCache#& cache,
1001 NodeIndexcxx . . $ mlspp/NodeIndex# parent,
1002 NodeIndexcxx . . $ mlspp/NodeIndex# sibling) const
1003 {
1004 const auto& parent_node = node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(7c807605eec2a484).(parent).parent_nodecxx . . $ mlspp/OptionalNode#parent_node(17c9744d3cd194d).();
1005 const auto& unmerged = parent_node.unmerged_leavescxx . . $ mlspp/ParentNode#unmerged_leaves.;
1006 const auto& sibling_hash = original_tree_hashcxx . . $ mlspp/TreeKEMPublicKey#original_tree_hash(dfa38275fb6e05bc).(cache, sibling, unmergedcxx . . $ std/vector#vector(a1201200cf0efa42).);
1007
1008 return suitecxx . . $ mlspp/TreeKEMPublicKey#suite..digestcxx . . $ mlspp/CipherSuite#digest(cdb80d7d9b9bc276).().hashcxx . . $ mlspp/hpke/Digest#hash(c5e1b85e16e94921).(tlscxx . . $ mlspp/bytes_ns/bytes#bytes(dd8c33c7e39dbe02).::marshalcxx . . $ mlspp/tls/marshal(7df0b13a9cabe487).(ParentHashInputcxx . . $ mlspp/ParentHashInput#{
1009 parent_node.public_keycxx . . $ mlspp/ParentNode#public_key.,
1010 parent_node.parent_hashcxx . . $ mlspp/ParentNode#parent_hash.,
1011 sibling_hash,
1012 }));
1013 }
1014
1015 bool
1016 TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#::parent_hash_validcxx . . $ mlspp/TreeKEMPublicKey#parent_hash_valid(de8db98f2089d9d5).(LeafIndexcxx . . $ mlspp/LeafIndex# from,
1017 const UpdatePathcxx . . $ mlspp/UpdatePath#& path) const
1018 {
1019 auto fdp = filtered_direct_pathcxx . . $ mlspp/TreeKEMPublicKey#filtered_direct_path(b755724fb327e77c).(NodeIndexcxx . . $ mlspp/NodeIndex#NodeIndex(5a3d3cee3fdf86fc).(from));
1020 auto hash_chain = parent_hashescxx . . $ mlspp/TreeKEMPublicKey#parent_hashes(2f270ebbd853fb5).(from, fdp, path.nodescxx . . $ mlspp/UpdatePath#nodes.);
1021 auto leaf_ph =
1022 var::visitcxx . . $ std/visit(9828ab07ec69b3d0).(overloaded{
1023 [](const ParentHashcxx . . $ mlspp/ParentHash#& ph) -> stdcxx . . $ std/::optionalcxx . . $ std/optional#<bytescxx . . $ mlspp/bytes_ns/bytes#> {
1024 return phcxx . . $ std/optional#optional(e3f0d6382bd52f24)..parent_hashcxx . . $ mlspp/ParentHash#parent_hash.;
1025 },
1026 [](const auto& /* other */) -> stdcxx . . $ std/::optionalcxx . . $ std/optional#<bytescxx . . $ mlspp/bytes_ns/bytes#> {
1027 return stdcxx . . $ std/optional#optional(eb2253fdec8870b5).::nulloptcxx . . $ std/nullopt.;
1028 },
1029 },
1030 path.leaf_nodecxx . . $ mlspp/UpdatePath#leaf_node..contentcxx . . $ mlspp/LeafNode#content.);
1031
1032 // If there are no nodes to hash, then ParentHash MUST be omitted
1033 if (hash_chain.emptycxx . . $ std/vector#empty(3482b152b9333168).()) {
1034 return !leaf_ph;
1035 }
1036
1037 return leaf_ph && opt::getcxx . . $ mlspp/tls/opt/get(f3e04ae79584f296).(leaf_ph) ==cxx . . $ mlspp/bytes_ns/bytes#`operator==`(efd4f3c976ab92ce). hash_chain[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).0];
1038 }
1039
1040 bool
1041 TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#::exists_in_treecxx . . $ mlspp/TreeKEMPublicKey#exists_in_tree(a28fd0d7d55e435a).(const HPKEPublicKeycxx . . $ mlspp/HPKEPublicKey#& key,
1042 stdcxx . . $ std/::optionalcxx . . $ std/optional#<LeafIndexcxx . . $ mlspp/LeafIndex#> except) const
1043 {
1044 return any_leafcxx . . $ mlspp/TreeKEMPublicKey#any_leaf(8877ebe272b9620d).([&](auto i, const auto& node) {
1045 return i !=cxx . . $ mlspp/`operator!=`(bd3c88d6bafb6558). except && node.encryption_key ==cxx . . $ mlspp/`operator==`(bd3c88d6bafb6558). key;
1046 });
1047 }
1048
1049 bool
1050 TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#::exists_in_treecxx . . $ mlspp/TreeKEMPublicKey#exists_in_tree(e34b2eb2d389dc07).(const SignaturePublicKeycxx . . $ mlspp/SignaturePublicKey#& key,
1051 stdcxx . . $ std/::optionalcxx . . $ std/optional#<LeafIndexcxx . . $ mlspp/LeafIndex#> except) const
1052 {
1053 return any_leafcxx . . $ mlspp/TreeKEMPublicKey#any_leaf(8877ebe272b9620d).([&](auto i, const auto& node) {
1054 return i !=cxx . . $ mlspp/`operator!=`(bd3c88d6bafb6558). except && node.signature_key ==cxx . . $ mlspp/`operator==`(bd3c88d6bafb6558). key;
1055 });
1056 }
1057
1058 tlscxx . . $ mlspp/tls/::ostreamcxx . . $ mlspp/tls/ostream#&
1059 operatorcxx . . $ mlspp/`operator<<`(9b37aeb90c040d6a).<<(tlscxx . . $ mlspp/tls/::ostreamcxx . . $ mlspp/tls/ostream#& str, const TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#& obj)
1060 {
1061 // Empty tree
1062 if (obj.sizecxx . . $ mlspp/TreeKEMPublicKey#size..valcxx . . $ mlspp/UInt32#val. == 0) {
1063 return str <<cxx . . $ mlspp/tls/`operator<<`(60620eb73935a469). stdcxx . . $ std/vector#vector(49f6e7a06ebc5aa8).::vectorcxx . . $ std/vector#<OptionalNodecxx . . $ mlspp/OptionalNode#>{};
1064 }
1065
1066 LeafIndexcxx . . $ mlspp/LeafIndex# cut = LeafIndexcxx . . $ mlspp/LeafIndex#{ obj.sizecxx . . $ mlspp/TreeKEMPublicKey#size..valcxx . . $ mlspp/UInt32#val. - 1 };
1067 while (cut.valcxx . . $ mlspp/UInt32#val. > 0 && obj.node_atcxx . . $ mlspp/TreeKEMPublicKey#node_at(81e4f0422d7e507e).(cut).blankcxx . . $ mlspp/OptionalNode#blank(50ce9a9e25b4a850).()) {
1068 cut.valcxx . . $ mlspp/UInt32#val. -= 1;
1069 }
1070
1071 const auto begin = obj.nodescxx . . $ mlspp/TreeKEMPublicKey#nodes..begincxx . . $ std/vector#begin(d1e1699016b9d7e6).();
1072 const auto end = begin +cxx . . $ __gnu_cxx/__normal_iterator#operator+(192bf940fa7bb3b2). NodeIndexcxx . . $ mlspp/NodeIndex#NodeIndex(5a3d3cee3fdf86fc).(cut).valcxx . . $ mlspp/UInt32#val. +cxx . . $ __gnu_cxx/__normal_iterator#operator+(192bf940fa7bb3b2). 1;
1073 const auto view = stdcxx . . $ std/vector#vector(13e0640cd3842f28).::vectorcxx . . $ std/vector#<OptionalNodecxx . . $ mlspp/OptionalNode#>(begin, end);
1074 return str <<cxx . . $ mlspp/tls/`operator<<`(60620eb73935a469). view;
1075 }
1076
1077 tlscxx . . $ mlspp/tls/::istreamcxx . . $ mlspp/tls/istream#&
1078 operatorcxx . . $ mlspp/`operator>>`(28db5d42b9a5186e).>>(tlscxx . . $ mlspp/tls/::istreamcxx . . $ mlspp/tls/istream#& str, TreeKEMPublicKeycxx . . $ mlspp/TreeKEMPublicKey#& obj)
1079 {
1080 // Read the node list
1081 str >>cxx . . $ mlspp/tls/`operator>>`(7ff05afe8fa0e542). obj.nodescxx . . $ mlspp/TreeKEMPublicKey#nodes.;
1082 if (obj.nodescxx . . $ mlspp/TreeKEMPublicKey#nodes..emptycxx . . $ std/vector#empty(3482b152b9333168).()) {
1083 return str;
1084 }
1085
1086 // Verify that the tree is well-formed and minimal
1087 if (obj.nodescxx . . $ mlspp/TreeKEMPublicKey#nodes..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() % 2 == 0) {
1088 throw ProtocolErrorcxx . . $ mlspp/ProtocolError#("Malformed ratchet tree: even number of nodes");
1089 }
1090
1091 if (obj.nodescxx . . $ mlspp/TreeKEMPublicKey#nodes..backcxx . . $ std/vector#back(6baabfa6fc7b3a8e).().blankcxx . . $ mlspp/OptionalNode#blank(50ce9a9e25b4a850).()) {
1092 throw ProtocolErrorcxx . . $ mlspp/ProtocolError#("Ratchet tree does not use minimal encoding");
1093 }
1094
1095 // Adjust the size value to fit the non-blank nodes
1096 obj.sizecxx . . $ mlspp/TreeKEMPublicKey#size..valcxx . . $ mlspp/UInt32#val. = 1;
1097 while (NodeCountcxx . . $ mlspp/NodeCount#NodeCount(a733f6ba51500b8c).(obj.sizecxx . . $ mlspp/TreeKEMPublicKey#size.).valcxx . . $ mlspp/UInt32#val. < obj.nodescxx . . $ mlspp/TreeKEMPublicKey#nodes..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()) {
1098 obj.sizecxx . . $ mlspp/TreeKEMPublicKey#size..valcxx . . $ mlspp/UInt32#val. *= 2;
1099 }
1100
1101 // Add blank nodes to the end
1102 obj.nodescxx . . $ mlspp/TreeKEMPublicKey#nodes..resizecxx . . $ std/vector#resize(2c44cd906ae4e829).(NodeCountcxx . . $ mlspp/NodeCount#NodeCount(a733f6ba51500b8c).(obj.sizecxx . . $ mlspp/TreeKEMPublicKey#size.).valcxx . . $ mlspp/UInt32#val.);
1103
1104 // Verify the basic structure of the tree is sane
1105 for (size_t i = 0; i < obj.nodescxx . . $ mlspp/TreeKEMPublicKey#nodes..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).(); i++) {
1106 if (obj.nodescxx . . $ mlspp/TreeKEMPublicKey#nodes.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i].blankcxx . . $ mlspp/OptionalNode#blank(50ce9a9e25b4a850).()) {
1107 continue;
1108 }
1109
1110 const auto& node = opt::getcxx . . $ mlspp/tls/opt/get(f3e04ae79584f296).(obj.nodescxx . . $ mlspp/TreeKEMPublicKey#nodes.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i].nodecxx . . $ mlspp/OptionalNode#node.).nodecxx . . $ mlspp/Node#node.;
1111 auto at_leaf = (i % 2 == 0);
1112 auto holds_leaf = var::holds_alternativecxx . . $ std/holds_alternative(4d382a1a2d74a3ce).<LeafNodecxx . . $ mlspp/LeafNode#>(node);
1113 auto holds_parent = var::holds_alternativecxx . . $ std/holds_alternative(4d382a1a2d74a3ce).<ParentNodecxx . . $ mlspp/ParentNode#>(node);
1114
1115 if (at_leaf && !holds_leaf) {
1116 throw InvalidParameterErrorcxx . . $ mlspp/InvalidParameterError#("Parent node in leaf node position");
1117 }
1118
1119 if (!at_leaf && !holds_parent) {
1120 throw InvalidParameterErrorcxx . . $ mlspp/InvalidParameterError#("Leaf node in parent node position");
1121 }
1122 }
1123
1124 return str;
1125 }
1126
1127 } // namespace mlspp