/src/cryptofuzz-sp-math/components.cpp
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | #include <cryptofuzz/crypto.h>  | 
2  |  | #include <cryptofuzz/generic.h>  | 
3  |  | #include <cryptofuzz/components.h>  | 
4  |  | #include <cryptofuzz/util.h>  | 
5  |  | #include <boost/multiprecision/cpp_int.hpp>  | 
6  |  | #include <cryptofuzz/repository.h>  | 
7  |  | #include "third_party/json/json.hpp"  | 
8  |  | #include "config.h"  | 
9  |  |  | 
10  |  | namespace cryptofuzz { | 
11  |  |  | 
12  |  | /* Type */  | 
13  |  |  | 
14  |  | Type::Type(Datasource& ds) :  | 
15  |  |     type ( ds.Get<uint64_t>(0) )  | 
16  | 33.9k  | { } | 
17  |  |  | 
18  |  | Type::Type(const Type& other) :  | 
19  |  |     type(other.type)  | 
20  | 225k  | { } | 
21  |  |  | 
22  |  | Type::Type(nlohmann::json json) :  | 
23  |  |     type(json.get<uint64_t>())  | 
24  | 0  | { } | 
25  |  |  | 
26  | 203k  | uint64_t Type::Get(void) const { | 
27  | 203k  |     return type;  | 
28  | 203k  | }  | 
29  |  |  | 
30  | 33.7k  | bool Type::Is(const uint64_t t) const { | 
31  | 33.7k  |     return type == t;  | 
32  | 33.7k  | }  | 
33  |  |  | 
34  | 0  | bool Type::Is(const std::vector<uint64_t> t) const { | 
35  | 0  |     return std::find(t.begin(), t.end(), type) != t.end();  | 
36  | 0  | }  | 
37  |  |  | 
38  | 0  | nlohmann::json Type::ToJSON(void) const { | 
39  | 0  |     nlohmann::json j;  | 
40  |  |     /* Store as string, not as number, because JavaScript's number  | 
41  |  |      * type has only 53 bits of precision.  | 
42  |  |      */  | 
43  | 0  |     j = std::to_string(type);  | 
44  | 0  |     return j;  | 
45  | 0  | }  | 
46  |  |  | 
47  | 0  | bool Type::operator==(const Type& rhs) const { | 
48  | 0  |     return type == rhs.type;  | 
49  | 0  | }  | 
50  |  |  | 
51  | 0  | void Type::Serialize(Datasource& ds) const { | 
52  | 0  |     ds.Put<>(type);  | 
53  | 0  | }  | 
54  |  |  | 
55  |  | /* Buffer */  | 
56  |  |  | 
57  |  | Buffer::Buffer(Datasource& ds) :  | 
58  |  |     data( ds.GetData(0, 0, (10*1024*1024)) )  | 
59  | 102k  | { } | 
60  |  |  | 
61  | 0  | Buffer::Buffer(nlohmann::json json) { | 
62  | 0  |     const auto s = json.get<std::string>();  | 
63  | 0  |     boost::algorithm::unhex(s, std::back_inserter(data));  | 
64  | 0  | }  | 
65  |  |  | 
66  |  | Buffer::Buffer(const std::vector<uint8_t>& data) :  | 
67  |  |     data(data)  | 
68  | 3.06k  | { } | 
69  |  |  | 
70  |  | Buffer::Buffer(const uint8_t* data, const size_t size) :  | 
71  |  |     data(data, data + size)  | 
72  | 60.1k  | { } | 
73  |  |  | 
74  | 2.30k  | Buffer::Buffer(void) { } | 
75  |  |  | 
76  | 882  | std::vector<uint8_t> Buffer::Get(void) const { | 
77  | 882  |     return data;  | 
78  | 882  | }  | 
79  |  |  | 
80  | 268k  | const uint8_t* Buffer::GetPtr(fuzzing::datasource::Datasource* ds) const { | 
81  | 268k  |     if ( data.size() == 0 ) { | 
82  | 116k  |         return util::GetNullPtr(ds);  | 
83  | 151k  |     } else { | 
84  | 151k  |         return data.data();  | 
85  | 151k  |     }  | 
86  | 268k  | }  | 
87  |  |  | 
88  | 103k  | std::vector<uint8_t>& Buffer::GetVectorPtr(void) { | 
89  | 103k  |     return data;  | 
90  | 103k  | }  | 
91  |  |  | 
92  | 70.8k  | const std::vector<uint8_t>& Buffer::GetConstVectorPtr(void) const { | 
93  | 70.8k  |     return data;  | 
94  | 70.8k  | }  | 
95  |  |  | 
96  | 557k  | size_t Buffer::GetSize(void) const { | 
97  | 557k  |     return data.size();  | 
98  | 557k  | }  | 
99  |  |  | 
100  | 15.3k  | bool Buffer::operator==(const Buffer& rhs) const { | 
101  | 15.3k  |     return data == rhs.data;  | 
102  | 15.3k  | }  | 
103  |  |  | 
104  | 0  | nlohmann::json Buffer::ToJSON(void) const { | 
105  | 0  |     nlohmann::json j;  | 
106  | 0  |     std::string asHex;  | 
107  | 0  |     boost::algorithm::hex(data, std::back_inserter(asHex));  | 
108  | 0  |     j = asHex;  | 
109  | 0  |     return j;  | 
110  | 0  | }  | 
111  |  |  | 
112  | 2.14k  | std::string Buffer::ToHex(void) const { | 
113  | 2.14k  |     std::string asHex;  | 
114  | 2.14k  |     boost::algorithm::hex(data, std::back_inserter(asHex));  | 
115  | 2.14k  |     return asHex;  | 
116  | 2.14k  | }  | 
117  |  |  | 
118  | 0  | void Buffer::Serialize(Datasource& ds) const { | 
119  | 0  |     ds.PutData(data);  | 
120  | 0  | }  | 
121  |  |  | 
122  | 0  | Datasource Buffer::AsDatasource(void) const { | 
123  | 0  |     return Datasource(data.data(), data.size());  | 
124  | 0  | }  | 
125  |  |  | 
126  | 0  | std::string Buffer::AsString(void) const { | 
127  | 0  |     return std::string(data.data(), data.data() + data.size());  | 
128  | 0  | }  | 
129  |  |  | 
130  | 0  | Buffer Buffer::ECDSA_Pad(const size_t retSize) const { | 
131  | 0  |     size_t bufSize = GetSize();  | 
132  |  | 
  | 
133  | 0  |     if ( bufSize > retSize ) { | 
134  | 0  |         bufSize = retSize;  | 
135  | 0  |     }  | 
136  |  | 
  | 
137  | 0  |     std::vector<uint8_t> ret(retSize);  | 
138  |  | 
  | 
139  | 0  |     if ( retSize != 0 ) { | 
140  | 0  |         const size_t delta = retSize - bufSize;  | 
141  |  | 
  | 
142  | 0  |         if ( delta != 0 ) { | 
143  | 0  |             memset(ret.data(), 0, delta);  | 
144  | 0  |         }  | 
145  |  | 
  | 
146  | 0  |         if ( bufSize != 0 ) { | 
147  | 0  |             memcpy(ret.data() + delta, GetPtr(), bufSize);  | 
148  | 0  |         }  | 
149  | 0  |     }  | 
150  |  | 
  | 
151  | 0  |     return Buffer(ret);  | 
152  | 0  | }  | 
153  |  |  | 
154  |  | /* Randomly modify an ECDSA input in such a way that it remains equivalent  | 
155  |  |  * to ECDSA verify/sign functions  | 
156  |  |  */  | 
157  | 3.06k  | Buffer Buffer::ECDSA_RandomPad(Datasource& ds, const Type& curveType) const { | 
158  | 3.06k  |     const auto numBits = cryptofuzz::repository::ECC_CurveToBits(curveType.Get());  | 
159  | 3.06k  |     if ( numBits == std::nullopt ) { | 
160  |  |         /* The size of this curve is not known, so return the original buffer */  | 
161  | 11  |         return Buffer(data);  | 
162  | 11  |     }  | 
163  |  |  | 
164  | 3.05k  |     if ( *numBits % 8 != 0 ) { | 
165  |  |         /* Curve sizes which are not a byte multiple are currently not supported,  | 
166  |  |          * so return the original buffer  | 
167  |  |          */  | 
168  | 941  |         return Buffer(data);  | 
169  | 941  |     }  | 
170  |  |  | 
171  | 2.11k  |     const size_t numBytes = (*numBits + 7) / 8;  | 
172  |  |  | 
173  | 2.11k  |     std::vector<uint8_t> stripped;  | 
174  | 2.11k  |     { | 
175  | 2.11k  |         size_t startPos;  | 
176  | 2.11k  |         const size_t endPos = GetSize() > numBytes ? numBytes : GetSize();  | 
177  |  |  | 
178  | 4.25k  |         for (startPos = 0; startPos < endPos; startPos++) { | 
179  | 4.16k  |             if ( data[startPos] != 0 ) { | 
180  | 2.02k  |                 break;  | 
181  | 2.02k  |             }  | 
182  | 4.16k  |         }  | 
183  | 2.11k  |         const auto& ref = GetConstVectorPtr();  | 
184  |  |  | 
185  | 2.11k  |         stripped.insert(std::end(stripped), std::begin(ref) + startPos, std::begin(ref) + endPos);  | 
186  | 2.11k  |     }  | 
187  |  |  | 
188  |  |     /* Decide how many bytes to insert */  | 
189  | 2.11k  |     uint16_t numInserts = 0;  | 
190  | 2.11k  |     try { | 
191  | 2.11k  |         numInserts = ds.Get<uint16_t>();  | 
192  | 2.11k  |     } catch ( fuzzing::datasource::Datasource::OutOfData ) { } | 
193  |  |  | 
194  | 2.11k  |     std::vector<uint8_t> ret;  | 
195  |  |  | 
196  |  |     /* Left-pad the input until it is the curve size */  | 
197  | 2.11k  |     { | 
198  | 2.11k  |         if ( stripped.size() < numBytes ) { | 
199  | 1.59k  |             const size_t needed = numBytes - stripped.size();  | 
200  | 1.59k  |             const std::vector<uint8_t> zeroes(numInserts > needed ? needed : numInserts, 0);  | 
201  | 1.59k  |             ret.insert(std::end(ret), std::begin(zeroes), std::end(zeroes));  | 
202  | 1.59k  |             numInserts -= zeroes.size();  | 
203  | 1.59k  |         }  | 
204  | 2.11k  |     }  | 
205  |  |  | 
206  |  |     /* Insert the input */  | 
207  | 2.11k  |     ret.insert(std::end(ret), std::begin(stripped), std::end(stripped));  | 
208  |  |  | 
209  |  |     /* Right-pad the input with random bytes (if available) or zeroes */  | 
210  | 2.11k  |     if ( numInserts > 0 ) { | 
211  | 166  |         std::vector<uint8_t> toInsert;  | 
212  | 166  |         try { | 
213  | 166  |             toInsert = ds.GetData(0, numInserts, numInserts);  | 
214  | 166  |         } catch ( fuzzing::datasource::Datasource::OutOfData ) { | 
215  | 161  |             toInsert = std::vector<uint8_t>(numInserts, 0);  | 
216  | 161  |         }  | 
217  | 166  |         ret.insert(std::end(ret), std::begin(toInsert), std::end(toInsert));  | 
218  | 166  |     }  | 
219  |  |  | 
220  | 2.11k  |     return Buffer(ret);  | 
221  | 2.11k  | }  | 
222  |  |  | 
223  | 0  | Buffer Buffer::SHA256(void) const { | 
224  | 0  |     const auto hash = crypto::sha256(Get());  | 
225  | 0  |     return Buffer(hash);  | 
226  | 0  | }  | 
227  |  |  | 
228  | 0  | bool Buffer::IsZero(void) const { | 
229  | 0  |     for (size_t i = 0; i < data.size(); i++) { | 
230  | 0  |         if ( data[i] != 0 ) { | 
231  | 0  |             return false;  | 
232  | 0  |         }  | 
233  | 0  |     }  | 
234  |  |  | 
235  | 0  |     return true;  | 
236  | 0  | }  | 
237  |  |  | 
238  |  | /* Bignum */  | 
239  |  |  | 
240  |  | Bignum::Bignum(Datasource& ds) :  | 
241  | 97.1k  |     data(ds) { | 
242  | 97.1k  |     transform();  | 
243  | 97.1k  | }  | 
244  |  |  | 
245  |  | Bignum::Bignum(nlohmann::json json) :  | 
246  |  |     Bignum(json.get<std::string>())  | 
247  | 0  | { | 
248  | 0  | }  | 
249  |  |  | 
250  |  | Bignum::Bignum(const std::string s) :  | 
251  |  |     data((const uint8_t*)s.data(), s.size())  | 
252  | 29.2k  | { } | 
253  |  |  | 
254  | 97.0k  | void Bignum::transform(void) { | 
255  | 97.0k  |     auto& ptr = data.GetVectorPtr();  | 
256  |  |  | 
257  | 8.43M  |     for (size_t i = 0; i < ptr.size(); i++) { | 
258  | 8.33M  |         if ( isdigit(ptr[i]) ) continue;  | 
259  | 4.42M  |         if ( config::kNegativeIntegers == true ) { | 
260  | 4.42M  |             if ( i == 0 && ptr[i] == '-') continue;  | 
261  | 4.42M  |         }  | 
262  | 4.42M  |         ptr[i] %= 10;  | 
263  | 4.42M  |         ptr[i] += '0';  | 
264  | 4.42M  |     }  | 
265  | 97.0k  | }  | 
266  |  |  | 
267  | 8.61k  | bool Bignum::operator==(const Bignum& rhs) const { | 
268  | 8.61k  |     return data == rhs.data;  | 
269  | 8.61k  | }  | 
270  |  |  | 
271  | 131k  | size_t Bignum::GetSize(void) const { | 
272  | 131k  |     return data.GetSize();  | 
273  | 131k  | }  | 
274  |  |  | 
275  | 0  | bool Bignum::IsZero(void) const { | 
276  | 0  |     return ToTrimmedString() == "0";  | 
277  | 0  | }  | 
278  |  |  | 
279  | 153k  | bool Bignum::IsNegative(void) const { | 
280  | 153k  |     return data.GetSize() && data.GetConstVectorPtr()[0] == '-';  | 
281  | 153k  | }  | 
282  |  |  | 
283  | 0  | bool Bignum::IsGreaterThan(const std::string& other) const { | 
284  | 0  |     CF_ASSERT(IsNegative() == false, "IsGreaterThan on negative numbers not supported");  | 
285  | 0  |     const auto s = ToTrimmedString();  | 
286  | 0  |     if ( s.size() > other.size() ) { | 
287  | 0  |         return true;  | 
288  | 0  |     } else if ( s.size() < other.size() ) { | 
289  | 0  |         return false;  | 
290  | 0  |     } else { | 
291  | 0  |         for (size_t i = 0; i < s.size(); i++) { | 
292  | 0  |             const int a = s[i];  | 
293  | 0  |             const int b = other[i];  | 
294  | 0  |             if ( a > b ) { | 
295  | 0  |                 return true;  | 
296  | 0  |             } else if ( a < b ) { | 
297  | 0  |                 return false;  | 
298  | 0  |             }  | 
299  | 0  |         }  | 
300  | 0  |     }  | 
301  |  |  | 
302  | 0  |     CF_ASSERT(s == other, "Logic error");  | 
303  | 0  |     return false;  | 
304  | 0  | }  | 
305  |  |  | 
306  | 0  | bool Bignum::IsLessThan(const std::string& other) const { | 
307  | 0  |     boost::multiprecision::cpp_int A(ToTrimmedString());  | 
308  | 0  |     boost::multiprecision::cpp_int B(other);  | 
309  | 0  |     return A < B;  | 
310  | 0  | }  | 
311  |  |  | 
312  | 0  | void Bignum::ToPositive(void) { | 
313  | 0  |     if ( !IsNegative() ) { | 
314  | 0  |         return;  | 
315  | 0  |     }  | 
316  |  |  | 
317  | 0  |     data.GetVectorPtr().erase(data.GetVectorPtr().begin());  | 
318  | 0  | }  | 
319  |  |  | 
320  | 0  | void Bignum::SubFrom(const std::string& v) { | 
321  | 0  |     boost::multiprecision::cpp_int A(ToTrimmedString());  | 
322  | 0  |     boost::multiprecision::cpp_int B(v);  | 
323  | 0  |     boost::multiprecision::cpp_int res = B - A;  | 
324  | 0  |     const auto s = res.str();  | 
325  | 0  |     data = {(const uint8_t*)s.data(), s.size()}; | 
326  | 0  | }  | 
327  |  |  | 
328  | 210k  | std::string Bignum::ToString(void) const { | 
329  | 210k  |     const auto ptr = data.GetPtr();  | 
330  | 210k  |     return std::string(ptr, ptr + data.GetSize());  | 
331  | 210k  | }  | 
332  |  |  | 
333  | 192k  | std::string Bignum::ToTrimmedString(void) const { | 
334  | 192k  |     auto s = ToString();  | 
335  | 192k  |     trim_left_if(s, boost::is_any_of("0")); | 
336  |  |  | 
337  | 192k  |     if ( s == "" ) { | 
338  | 91.2k  |         return "0";  | 
339  | 101k  |     } else { | 
340  | 101k  |         return s;  | 
341  | 101k  |     }  | 
342  | 192k  | }  | 
343  |  |  | 
344  |  | /* Prefix the string with a pseudo-random amount of zeroes */  | 
345  | 123k  | std::string Bignum::ToString(Datasource& ds) const { | 
346  | 123k  |     std::string zeros;  | 
347  |  |  | 
348  | 123k  |     try { | 
349  | 266k  |         while ( ds.Get<bool>() == true ) { | 
350  | 143k  |             zeros += "0";  | 
351  | 143k  |         }  | 
352  | 123k  |     } catch ( fuzzing::datasource::Datasource::OutOfData ) { } | 
353  |  |  | 
354  | 123k  |     auto s = ToTrimmedString();  | 
355  | 123k  |     const bool isNegative = IsNegative();  | 
356  | 123k  |     if ( s.size() && s[0] == '-' ) { | 
357  | 4.90k  |         s.erase(0, 1);  | 
358  | 4.90k  |     }  | 
359  | 123k  |     return (isNegative ? "-" : "") + zeros + s;  | 
360  | 123k  | }  | 
361  |  |  | 
362  | 0  | std::optional<std::vector<uint8_t>> Bignum::ToBin(std::optional<size_t> size) const { | 
363  | 0  |     return util::DecToBin(ToTrimmedString(), size);  | 
364  | 0  | }  | 
365  |  |  | 
366  | 0  | nlohmann::json Bignum::ToJSON(void) const { | 
367  | 0  |     return ToString();  | 
368  | 0  | }  | 
369  |  |  | 
370  | 0  | void Bignum::Serialize(Datasource& ds) const { | 
371  | 0  |     data.Serialize(ds);  | 
372  | 0  | }  | 
373  |  |  | 
374  |  | namespace component { | 
375  |  | /* SymmetricCipher */  | 
376  |  |  | 
377  |  | SymmetricCipher::SymmetricCipher(Datasource& ds) :  | 
378  |  |     iv(ds),  | 
379  |  |     key(ds),  | 
380  |  |     cipherType(ds)  | 
381  | 0  | { } | 
382  |  |  | 
383  |  | SymmetricCipher::SymmetricCipher(nlohmann::json json) :  | 
384  |  |     iv(json["iv"]),  | 
385  |  |     key(json["key"]),  | 
386  |  |     cipherType(json["cipherType"])  | 
387  | 0  | { } | 
388  |  |  | 
389  | 0  | nlohmann::json SymmetricCipher::ToJSON(void) const { | 
390  | 0  |     nlohmann::json j;  | 
391  | 0  |     j["iv"] = iv.ToJSON();  | 
392  | 0  |     j["key"] = key.ToJSON();  | 
393  | 0  |     j["cipherType"] = cipherType.ToJSON();  | 
394  | 0  |     return j;  | 
395  | 0  | }  | 
396  |  |  | 
397  | 0  | bool SymmetricCipher::operator==(const SymmetricCipher& rhs) const { | 
398  | 0  |     return  | 
399  | 0  |         (iv == rhs.iv) &&  | 
400  | 0  |         (key == rhs.key) &&  | 
401  | 0  |         (cipherType == rhs.cipherType);  | 
402  | 0  | }  | 
403  | 0  | void SymmetricCipher::Serialize(Datasource& ds) const { | 
404  | 0  |     iv.Serialize(ds);  | 
405  | 0  |     key.Serialize(ds);  | 
406  | 0  |     cipherType.Serialize(ds);  | 
407  | 0  | }  | 
408  |  |  | 
409  |  | /* Ciphertext */  | 
410  |  |  | 
411  |  | Ciphertext::Ciphertext(Datasource& ds) :  | 
412  |  |     ciphertext(ds),  | 
413  |  |     tag( ds.Get<bool>() ? std::nullopt : std::make_optional<Tag>(ds) )  | 
414  | 0  | { } | 
415  |  |  | 
416  |  | Ciphertext::Ciphertext(Buffer ciphertext, std::optional<Tag> tag) :  | 
417  |  |     ciphertext(ciphertext),  | 
418  |  |     tag(tag)  | 
419  | 55  | { } | 
420  |  |  | 
421  | 16  | bool Ciphertext::operator==(const Ciphertext& rhs) const { | 
422  | 16  |     return (ciphertext == rhs.ciphertext) && (tag == rhs.tag);  | 
423  | 16  | }  | 
424  |  |  | 
425  | 0  | void Ciphertext::Serialize(Datasource& ds) const { | 
426  | 0  |     ciphertext.Serialize(ds);  | 
427  | 0  |     if ( tag == std::nullopt ) { | 
428  | 0  |         ds.Put<bool>(true);  | 
429  | 0  |     } else { | 
430  | 0  |         ds.Put<bool>(false);  | 
431  | 0  |         tag->Serialize(ds);  | 
432  | 0  |     }  | 
433  | 0  | }  | 
434  |  |  | 
435  |  | /* BignumPair */  | 
436  |  |  | 
437  |  | BignumPair::BignumPair(Datasource& ds) :  | 
438  |  |     first(ds),  | 
439  |  |     second(ds)  | 
440  | 9.01k  | { } | 
441  |  |  | 
442  |  | BignumPair::BignumPair(const std::string first, const std::string second) :  | 
443  |  |     first(first),  | 
444  |  |     second(second)  | 
445  | 7.11k  | { } | 
446  |  |  | 
447  |  | BignumPair::BignumPair(nlohmann::json json) :  | 
448  |  |     first(json[0].get<std::string>()),  | 
449  |  |     second(json[1].get<std::string>())  | 
450  | 0  | { } | 
451  |  |  | 
452  |  |  | 
453  | 2.08k  | bool BignumPair::operator==(const BignumPair& rhs) const { | 
454  | 2.08k  |     return  | 
455  | 2.08k  |         (first == rhs.first) &&  | 
456  | 2.08k  |         (second == rhs.second);  | 
457  | 2.08k  | }  | 
458  |  |  | 
459  | 0  | void BignumPair::Serialize(Datasource& ds) const { | 
460  | 0  |     first.Serialize(ds);  | 
461  | 0  |     second.Serialize(ds);  | 
462  | 0  | }  | 
463  |  |  | 
464  | 0  | nlohmann::json BignumPair::ToJSON(void) const { | 
465  | 0  |     return std::vector<nlohmann::json>{first.ToJSON(), second.ToJSON()}; | 
466  | 0  | }  | 
467  |  |  | 
468  |  | /* ECC_KeyPair */  | 
469  |  |  | 
470  |  | ECC_KeyPair::ECC_KeyPair(Datasource& ds) :  | 
471  |  |     priv(ds),  | 
472  |  |     pub(ds)  | 
473  | 0  | { } | 
474  |  |  | 
475  |  | ECC_KeyPair::ECC_KeyPair(ECC_PrivateKey priv, BignumPair pub) :  | 
476  |  |     priv(priv),  | 
477  |  |     pub(pub)  | 
478  | 1.33k  | { } | 
479  |  |  | 
480  | 0  | bool ECC_KeyPair::operator==(const ECC_KeyPair& rhs) const { | 
481  | 0  |     return  | 
482  | 0  |         (priv == rhs.priv) &&  | 
483  | 0  |         (pub == rhs.pub);  | 
484  | 0  | }  | 
485  |  |  | 
486  | 0  | void ECC_KeyPair::Serialize(Datasource& ds) const { | 
487  | 0  |     priv.Serialize(ds);  | 
488  | 0  |     pub.Serialize(ds);  | 
489  | 0  | }  | 
490  |  |  | 
491  | 0  | nlohmann::json ECC_KeyPair::ToJSON(void) const { | 
492  | 0  |     return std::vector<nlohmann::json>{priv.ToJSON(), pub.ToJSON()}; | 
493  | 0  | }  | 
494  |  |  | 
495  |  | /* ECDSA_Signature */  | 
496  |  | ECDSA_Signature::ECDSA_Signature(Datasource& ds) :  | 
497  |  |     signature(ds),  | 
498  |  |     pub(ds)  | 
499  | 1.28k  | { } | 
500  |  |  | 
501  |  | ECDSA_Signature::ECDSA_Signature(BignumPair signature, ECC_PublicKey pub) :  | 
502  |  |     signature(signature),  | 
503  |  |     pub(pub)  | 
504  | 2.14k  | { } | 
505  |  |  | 
506  |  | ECDSA_Signature::ECDSA_Signature(nlohmann::json json) :  | 
507  |  |     signature(json["signature"]),  | 
508  |  |     pub(json["pub"])  | 
509  | 0  | { } | 
510  |  |  | 
511  | 769  | bool ECDSA_Signature::operator==(const ECDSA_Signature& rhs) const { | 
512  | 769  |     return  | 
513  | 769  |         (signature == rhs.signature) &&  | 
514  | 769  |         (pub == rhs.pub);  | 
515  | 769  | }  | 
516  |  |  | 
517  | 0  | void ECDSA_Signature::Serialize(Datasource& ds) const { | 
518  | 0  |     signature.Serialize(ds);  | 
519  | 0  |     pub.Serialize(ds);  | 
520  | 0  | }  | 
521  |  |  | 
522  | 0  | nlohmann::json ECDSA_Signature::ToJSON(void) const { | 
523  | 0  |     return std::vector<nlohmann::json>{signature.ToJSON(), pub.ToJSON()}; | 
524  | 0  | }  | 
525  |  |  | 
526  |  | /* MACType */  | 
527  |  |  | 
528  |  | MACType::MACType(Datasource& ds) :  | 
529  |  |     mode(ds.Get<bool>()),  | 
530  |  |     type(ds)  | 
531  | 0  | { } | 
532  |  |  | 
533  |  | MACType::MACType(nlohmann::json json) :  | 
534  |  |     mode(json["mode"].get<bool>()),  | 
535  |  |     type(json["type"])  | 
536  | 0  | { } | 
537  |  |  | 
538  | 0  | nlohmann::json MACType::ToJSON(void) const { | 
539  | 0  |     nlohmann::json j;  | 
540  | 0  |     j["mode"] = mode;  | 
541  | 0  |     j["type"] = type.ToJSON();  | 
542  | 0  |     return j;  | 
543  | 0  | }  | 
544  |  |  | 
545  | 0  | bool MACType::operator==(const MACType& rhs) const { | 
546  | 0  |     return  | 
547  | 0  |         (mode == rhs.mode) &&  | 
548  | 0  |         (type == rhs.type);  | 
549  | 0  | }  | 
550  |  |  | 
551  | 0  | void MACType::Serialize(Datasource& ds) const { | 
552  | 0  |     ds.Put<>(mode);  | 
553  | 0  |     type.Serialize(ds);  | 
554  | 0  | }  | 
555  |  |  | 
556  |  | G2::G2(nlohmann::json json) :  | 
557  |  |     first(json[0]),  | 
558  | 0  |     second(json[1]) { | 
559  | 0  | }  | 
560  |  |  | 
561  | 0  | nlohmann::json G2::ToJSON(void) const { | 
562  | 0  |     return std::vector<nlohmann::json>{ | 
563  | 0  |         first.first.ToJSON(), first.second.ToJSON(),  | 
564  | 0  |         second.first.ToJSON(), second.second.ToJSON()  | 
565  | 0  |     };  | 
566  | 0  | }  | 
567  |  |  | 
568  | 0  | void G2::Serialize(Datasource& ds) const { | 
569  | 0  |     first.Serialize(ds);  | 
570  | 0  |     second.Serialize(ds);  | 
571  | 0  | }  | 
572  |  |  | 
573  | 0  | nlohmann::json Fp12::ToJSON(void) const { | 
574  | 0  |     return std::vector<nlohmann::json>{ | 
575  | 0  |         bn1.ToJSON(),  | 
576  | 0  |         bn2.ToJSON(),  | 
577  | 0  |         bn3.ToJSON(),  | 
578  | 0  |         bn4.ToJSON(),  | 
579  | 0  |         bn5.ToJSON(),  | 
580  | 0  |         bn6.ToJSON(),  | 
581  | 0  |         bn7.ToJSON(),  | 
582  | 0  |         bn8.ToJSON(),  | 
583  | 0  |         bn9.ToJSON(),  | 
584  | 0  |         bn10.ToJSON(),  | 
585  | 0  |         bn11.ToJSON(),  | 
586  | 0  |         bn12.ToJSON(),  | 
587  | 0  |     };  | 
588  | 0  | }  | 
589  |  |  | 
590  |  | Fp12::Fp12(nlohmann::json json) :  | 
591  |  |     bn1(json[0].get<std::string>()),  | 
592  |  |     bn2(json[1].get<std::string>()),  | 
593  |  |     bn3(json[2].get<std::string>()),  | 
594  |  |     bn4(json[3].get<std::string>()),  | 
595  |  |     bn5(json[4].get<std::string>()),  | 
596  |  |     bn6(json[5].get<std::string>()),  | 
597  |  |     bn7(json[6].get<std::string>()),  | 
598  |  |     bn8(json[7].get<std::string>()),  | 
599  |  |     bn9(json[8].get<std::string>()),  | 
600  |  |     bn10(json[9].get<std::string>()),  | 
601  |  |     bn11(json[10].get<std::string>()),  | 
602  |  |     bn12(json[11].get<std::string>())  | 
603  | 0  | { } | 
604  |  |  | 
605  | 0  | void Fp12::Serialize(Datasource& ds) const { | 
606  | 0  |     bn1.Serialize(ds);  | 
607  | 0  |     bn2.Serialize(ds);  | 
608  | 0  |     bn3.Serialize(ds);  | 
609  | 0  |     bn4.Serialize(ds);  | 
610  | 0  |     bn5.Serialize(ds);  | 
611  | 0  |     bn6.Serialize(ds);  | 
612  | 0  |     bn7.Serialize(ds);  | 
613  | 0  |     bn8.Serialize(ds);  | 
614  | 0  |     bn9.Serialize(ds);  | 
615  | 0  |     bn10.Serialize(ds);  | 
616  | 0  |     bn11.Serialize(ds);  | 
617  | 0  |     bn12.Serialize(ds);  | 
618  | 0  | }  | 
619  |  |  | 
620  |  | /* BLS_Signature */  | 
621  |  | BLS_Signature::BLS_Signature(Datasource& ds) :  | 
622  |  |     signature(ds),  | 
623  |  |     pub(ds)  | 
624  | 0  | { } | 
625  |  |  | 
626  |  | BLS_Signature::BLS_Signature(G2 signature, ECC_PublicKey pub) :  | 
627  |  |     signature(signature),  | 
628  |  |     pub(pub)  | 
629  | 0  | { } | 
630  |  |  | 
631  |  | BLS_Signature::BLS_Signature(nlohmann::json json) :  | 
632  |  |     signature(json["signature"]),  | 
633  |  |     pub(json["pub"])  | 
634  | 0  | { } | 
635  |  |  | 
636  | 0  | bool BLS_Signature::operator==(const BLS_Signature& rhs) const { | 
637  | 0  |     return  | 
638  | 0  |         (signature == rhs.signature) &&  | 
639  | 0  |         (pub == rhs.pub);  | 
640  | 0  | }  | 
641  |  |  | 
642  | 0  | void BLS_Signature::Serialize(Datasource& ds) const { | 
643  | 0  |     signature.Serialize(ds);  | 
644  | 0  |     pub.Serialize(ds);  | 
645  | 0  | }  | 
646  |  |  | 
647  | 0  | nlohmann::json BLS_Signature::ToJSON(void) const { | 
648  | 0  |     return std::vector<nlohmann::json>{signature.ToJSON(), pub.ToJSON()}; | 
649  | 0  | }  | 
650  |  |  | 
651  |  | /* BLS_BatchSign_Vector */  | 
652  |  |  | 
653  | 0  | BLS_BatchSign_Vector::BLS_BatchSign_Vector(Datasource& ds) { | 
654  | 0  |     const auto num = ds.Get<uint32_t>(0);  | 
655  | 0  |     for (size_t i = 0; i < num; i++) { | 
656  | 0  |         c.push_back( BatchSign_single{{ds}, {ds}} ); | 
657  | 0  |     }  | 
658  | 0  | }  | 
659  |  |  | 
660  | 0  | BLS_BatchSign_Vector::BLS_BatchSign_Vector(nlohmann::json json) { | 
661  | 0  |     for (const auto& j : json) { | 
662  | 0  |         c.push_back( BatchSign_single{ | 
663  | 0  |                 j["priv"],  | 
664  | 0  |                 {j["g1_x"], j["g1_y"]} }); | 
665  | 0  |     }  | 
666  | 0  | }  | 
667  |  |  | 
668  | 0  | void BLS_BatchSign_Vector::Serialize(Datasource& ds) const { | 
669  | 0  |     ds.Put<uint32_t>(c.size());  | 
670  | 0  |     for (const auto& component : c) { | 
671  | 0  |         component.priv.Serialize(ds);  | 
672  | 0  |         component.g1.Serialize(ds);  | 
673  | 0  |     }  | 
674  | 0  | }  | 
675  |  |  | 
676  |  | /* BLS_BatchSignature */  | 
677  |  |  | 
678  |  | BLS_BatchSignature::BLS_BatchSignature(std::vector< std::pair<G1, G2> > msgpub) :  | 
679  |  |     msgpub(msgpub)  | 
680  | 0  | { } | 
681  |  |  | 
682  | 0  | bool BLS_BatchSignature::operator==(const BLS_BatchSignature& rhs) const { | 
683  | 0  |     return  | 
684  | 0  |         (msgpub == rhs.msgpub);  | 
685  | 0  | }  | 
686  |  |  | 
687  | 0  | void BLS_BatchSignature::Serialize(Datasource& ds) const { | 
688  | 0  |     ds.Put<uint32_t>(msgpub.size());  | 
689  | 0  |     for (const auto& component : msgpub) { | 
690  | 0  |         component.first.Serialize(ds);  | 
691  | 0  |         component.second.Serialize(ds);  | 
692  | 0  |     }  | 
693  | 0  | }  | 
694  |  |  | 
695  | 0  | nlohmann::json BLS_BatchSignature::ToJSON(void) const { | 
696  | 0  |     return {}; /* TODO */ | 
697  | 0  | }  | 
698  |  |  | 
699  |  |  | 
700  |  | /* BLS_KeyPair */  | 
701  |  |  | 
702  |  | BLS_KeyPair::BLS_KeyPair(Datasource& ds) :  | 
703  |  |     priv(ds),  | 
704  |  |     pub(ds)  | 
705  | 0  | { } | 
706  |  |  | 
707  |  | BLS_KeyPair::BLS_KeyPair(BLS_PrivateKey priv, BignumPair pub) :  | 
708  |  |     priv(priv),  | 
709  |  |     pub(pub)  | 
710  | 0  | { } | 
711  |  |  | 
712  | 0  | bool BLS_KeyPair::operator==(const BLS_KeyPair& rhs) const { | 
713  | 0  |     return  | 
714  | 0  |         (priv == rhs.priv) &&  | 
715  | 0  |         (pub == rhs.pub);  | 
716  | 0  | }  | 
717  |  |  | 
718  | 0  | void BLS_KeyPair::Serialize(Datasource& ds) const { | 
719  | 0  |     priv.Serialize(ds);  | 
720  | 0  |     pub.Serialize(ds);  | 
721  | 0  | }  | 
722  |  |  | 
723  | 0  | nlohmann::json BLS_KeyPair::ToJSON(void) const { | 
724  | 0  |     return std::vector<nlohmann::json>{priv.ToJSON(), pub.ToJSON()}; | 
725  | 0  | }  | 
726  |  |  | 
727  |  | /* BLS_BatchVerify_Vector */  | 
728  |  |  | 
729  | 0  | BLS_BatchVerify_Vector::BLS_BatchVerify_Vector(Datasource& ds) { | 
730  | 0  |     const auto num = ds.Get<uint32_t>(0);  | 
731  | 0  |     for (size_t i = 0; i < num; i++) { | 
732  | 0  |         c.push_back( BatchVerify_single{{ds}, {ds}} ); | 
733  | 0  |     }  | 
734  | 0  | }  | 
735  |  |  | 
736  | 0  | BLS_BatchVerify_Vector::BLS_BatchVerify_Vector(nlohmann::json json) { | 
737  | 0  |     for (const auto& j : json) { | 
738  | 0  |         c.push_back( BatchVerify_single{ | 
739  | 0  |                 {j["g1_x"], j["g1_y"]}, | 
740  | 0  |                 {j["g2_v"], j["g2_w"], j["g2_x"], j["g2_y"]} }); | 
741  | 0  |     }  | 
742  | 0  | }  | 
743  |  |  | 
744  | 0  | void BLS_BatchVerify_Vector::Serialize(Datasource& ds) const { | 
745  | 0  |     ds.Put<uint32_t>(c.size());  | 
746  | 0  |     for (const auto& component : c) { | 
747  | 0  |         component.g1.Serialize(ds);  | 
748  | 0  |         component.g2.Serialize(ds);  | 
749  | 0  |     }  | 
750  | 0  | }  | 
751  |  |  | 
752  | 0  | nlohmann::json BLS_BatchVerify_Vector::ToJSON(void) const { | 
753  | 0  |     nlohmann::json j = nlohmann::json::array();  | 
754  | 0  |     for (const auto& cur : c) { | 
755  | 0  |         nlohmann::json j;  | 
756  | 0  |         j["g1_x"] = cur.g1.first.ToJSON();  | 
757  | 0  |         j["g1_y"] = cur.g1.second.ToJSON();  | 
758  |  | 
  | 
759  | 0  |         j["g2_v"] = cur.g2.first.first.ToJSON();  | 
760  | 0  |         j["g2_w"] = cur.g2.first.second.ToJSON();  | 
761  | 0  |         j["g2_x"] = cur.g2.second.first.ToJSON();  | 
762  | 0  |         j["g2_y"] = cur.g2.second.second.ToJSON();  | 
763  | 0  |     }  | 
764  | 0  |     return j;  | 
765  | 0  | }  | 
766  |  |  | 
767  |  | /* BLS_G1_Vector */  | 
768  |  |  | 
769  | 0  | BLS_G1_Vector::BLS_G1_Vector(Datasource& ds) { | 
770  | 0  |     const auto num = ds.Get<uint32_t>(0);  | 
771  | 0  |     for (size_t i = 0; i < num; i++) { | 
772  | 0  |         points.push_back( component::G1(ds) );  | 
773  | 0  |     }  | 
774  | 0  | }  | 
775  |  |  | 
776  | 0  | BLS_G1_Vector::BLS_G1_Vector(nlohmann::json json) { | 
777  | 0  |     for (const auto& j : json) { | 
778  | 0  |         points.push_back( component::G1{j["x"], j["y"]} ); | 
779  | 0  |     }  | 
780  | 0  | }  | 
781  |  |  | 
782  | 0  | void BLS_G1_Vector::Serialize(Datasource& ds) const { | 
783  | 0  |     ds.Put<uint32_t>(points.size());  | 
784  | 0  |     for (const auto& signature : points) { | 
785  | 0  |         signature.Serialize(ds);  | 
786  | 0  |     }  | 
787  | 0  | }  | 
788  |  |  | 
789  |  | /* BLS_G2_Vector */  | 
790  |  |  | 
791  | 0  | BLS_G2_Vector::BLS_G2_Vector(Datasource& ds) { | 
792  | 0  |     const auto num = ds.Get<uint32_t>(0);  | 
793  | 0  |     for (size_t i = 0; i < num; i++) { | 
794  | 0  |         points.push_back( component::G2(ds) );  | 
795  | 0  |     }  | 
796  | 0  | }  | 
797  |  |  | 
798  | 0  | BLS_G2_Vector::BLS_G2_Vector(nlohmann::json json) { | 
799  | 0  |     for (const auto& j : json) { | 
800  | 0  |         points.push_back( component::G2{j["v"], j["w"], j["x"], j["y"]} ); | 
801  | 0  |     }  | 
802  | 0  | }  | 
803  |  |  | 
804  | 0  | void BLS_G2_Vector::Serialize(Datasource& ds) const { | 
805  | 0  |     ds.Put<uint32_t>(points.size());  | 
806  | 0  |     for (const auto& signature : points) { | 
807  | 0  |         signature.Serialize(ds);  | 
808  | 0  |     }  | 
809  | 0  | }  | 
810  |  |  | 
811  |  | /* SR25519_Signature */  | 
812  |  | SR25519_Signature::SR25519_Signature(Datasource& ds) :  | 
813  |  |     signature(ds),  | 
814  |  |     pub(ds)  | 
815  | 0  | { } | 
816  |  |  | 
817  |  | SR25519_Signature::SR25519_Signature(BignumPair signature, Bignum pub) :  | 
818  |  |     signature(signature),  | 
819  |  |     pub(pub)  | 
820  | 0  | { } | 
821  |  |  | 
822  |  | SR25519_Signature::SR25519_Signature(nlohmann::json json) :  | 
823  |  |     signature(json["signature"]),  | 
824  |  |     pub(json["pub"])  | 
825  | 0  | { } | 
826  |  |  | 
827  | 0  | bool SR25519_Signature::operator==(const SR25519_Signature& rhs) const { | 
828  | 0  |     return  | 
829  | 0  |         (signature == rhs.signature) &&  | 
830  | 0  |         (pub == rhs.pub);  | 
831  | 0  | }  | 
832  |  |  | 
833  | 0  | void SR25519_Signature::Serialize(Datasource& ds) const { | 
834  | 0  |     signature.Serialize(ds);  | 
835  | 0  |     pub.Serialize(ds);  | 
836  | 0  | }  | 
837  |  |  | 
838  | 0  | nlohmann::json SR25519_Signature::ToJSON(void) const { | 
839  | 0  |     return std::vector<nlohmann::json>{signature.ToJSON(), pub.ToJSON()}; | 
840  | 0  | }  | 
841  |  |  | 
842  |  | } /* namespace component */  | 
843  |  |  | 
844  |  | } /* namespace cryptofuzz */  |