Bitcoin Core 28.99.0
P2P Digital Currency
example.c
Go to the documentation of this file.
1/**********************************************************************
2 * Copyright (c) 2018 Pieter Wuille, Greg Maxwell, Gleb Naumenko *
3 * Distributed under the MIT software license, see the accompanying *
4 * file LICENSE or http://www.opensource.org/licenses/mit-license.php.*
5 **********************************************************************/
6
7#include <stdio.h>
8#include <assert.h>
9#include "../include/minisketch.h"
10
11int main(void) {
12
13 minisketch *sketch_a = minisketch_create(12, 0, 4);
14
15 for (int i = 3000; i < 3010; ++i) {
16 minisketch_add_uint64(sketch_a, i);
17 }
18
19 size_t sersize = minisketch_serialized_size(sketch_a);
20 assert(sersize == 12 * 4 / 8); // 4 12-bit values is 6 bytes.
21 unsigned char *buffer_a = malloc(sersize);
22 minisketch_serialize(sketch_a, buffer_a);
23 minisketch_destroy(sketch_a);
24
25 minisketch *sketch_b = minisketch_create(12, 0, 4); // Bob's own sketch
26 for (int i = 3002; i < 3012; ++i) {
27 minisketch_add_uint64(sketch_b, i);
28 }
29
30 sketch_a = minisketch_create(12, 0, 4); // Alice's sketch
31 minisketch_deserialize(sketch_a, buffer_a); // Load Alice's sketch
32 free(buffer_a);
33
34 // Merge the elements from sketch_a into sketch_b. The result is a sketch_b
35 // which contains all elements that occurred in Alice's or Bob's sets, but not
36 // in both.
37 minisketch_merge(sketch_b, sketch_a);
38
39 uint64_t differences[4];
40 ssize_t num_differences = minisketch_decode(sketch_b, 4, differences);
41 minisketch_destroy(sketch_a);
42 minisketch_destroy(sketch_b);
43 if (num_differences < 0) {
44 printf("More than 4 differences!\n");
45 } else {
46 ssize_t i;
47 for (i = 0; i < num_differences; ++i) {
48 printf("%u is in only one of the two sets\n", (unsigned)differences[i]);
49 }
50 }
51}
int main(void)
Definition: example.c:11
MINISKETCH_API void minisketch_serialize(const minisketch *sketch, unsigned char *output)
Serialize a sketch to bytes.
Definition: minisketch.cpp:440
MINISKETCH_API ssize_t minisketch_decode(const minisketch *sketch, size_t max_elements, uint64_t *output)
Decode a sketch.
Definition: minisketch.cpp:468
MINISKETCH_API size_t minisketch_merge(minisketch *sketch, const minisketch *other_sketch)
Merge the elements of another sketch into this sketch.
Definition: minisketch.cpp:458
MINISKETCH_API size_t minisketch_serialized_size(const minisketch *sketch)
Compute the size in bytes for serializing a given sketch.
Definition: minisketch.cpp:432
MINISKETCH_API minisketch * minisketch_create(uint32_t bits, uint32_t implementation, size_t capacity)
Construct a sketch for a given element size, implementation and capacity.
Definition: minisketch.cpp:378
MINISKETCH_API void minisketch_add_uint64(minisketch *sketch, uint64_t element)
Add an element to a sketch.
Definition: minisketch.cpp:452
MINISKETCH_API void minisketch_deserialize(minisketch *sketch, const unsigned char *input)
Deserialize a sketch from bytes.
Definition: minisketch.cpp:446
MINISKETCH_API void minisketch_destroy(minisketch *sketch)
Destroy a sketch.
Definition: minisketch.cpp:424
struct minisketch minisketch
Opaque type for decoded sketches.
Definition: minisketch.h:41
void printf(FormatStringCheck< sizeof...(Args)> fmt, const Args &... args)
Format list of arguments to std::cout, according to the given format string.
Definition: tinyformat.h:1089
assert(!tx.IsCoinBase())