· 5 years ago · Oct 12, 2020, 10:10 AM
1#include <boost/multiprecision/cpp_int.hpp>
2#include <boost/random.hpp>
3#include <iostream>
4#include <numeric>
5#include <cmath>
6
7using namespace boost::multiprecision;
8
9namespace mp = boost::multiprecision;
10
11struct OpenKey {
12 cpp_int p; // usually 1024 bits (prime!)
13 cpp_int q; // usually 160 bits; (p - 1) % q == 0
14 // w < q
15 cpp_int g; // (g^q) % p == 1
16 cpp_int y; // y = g^(-w) % p
17};
18
19struct SecretKey {
20 cpp_int w;
21};
22
23
24auto init_random_value_gen(const OpenKey &open_key) {
25 using namespace boost::random;
26 return uniform_int_distribution<cpp_int>(0, open_key.q);
27};
28
29
30cpp_int pre_signing(const OpenKey &open_key, const cpp_int &rand_power) {
31 namespace mp = boost::multiprecision;
32 return mp::powm(open_key.g, rand_power, open_key.p);
33};
34
35template<typename T>
36auto init_random_value_gen(T bits_count) {
37 // multiprecision do not allow cpp_int as exponent, because https://stackoverflow.com/a/43508036
38 using namespace boost::random;
39 namespace mp = boost::multiprecision;
40 const cpp_int &base = 2;
41 return uniform_int_distribution<cpp_int>(0, mp::pow(base, bits_count) - 1);
42};
43
44cpp_int sing(const OpenKey &open_key, const SecretKey &secret_key, cpp_int& r, cpp_int& e) {
45 return (r + e * secret_key.w) % open_key.q;
46}
47
48
49void singing(const OpenKey &open_key, const SecretKey &secret_key) {
50 // Note: Alice has secret key
51 using namespace boost::multiprecision;
52 using namespace boost::random;
53
54 mt19937 mt;
55
56 auto rand_power_gen = init_random_value_gen(open_key);
57 cpp_int r = rand_power_gen(mt);
58 auto x = pre_signing(open_key, r);
59
60 int bit_count = 72;
61 auto e_gen = init_random_value_gen(bit_count);
62 cpp_int e = e_gen(mt);
63 cpp_int s = sign(open_key, secret_key, r, e);
64
65
66
67
68};
69
70
71int main() {
72 using namespace std;
73 mp::cpp_int x = mp::pow(mp::cpp_int(2), 1024);
74 std::cout << x << "\n";
75 cpp_int v = 1;
76
77 // Do some arithmetic:
78 for (unsigned i = 1; i <= 1000; ++i)
79 v *= i;
80
81 std::cout << v << std::endl; // prints 1000! / 10
82
83 cpp_rational w(2, 3); // component wise constructor
84 std::cout << w << std::endl; // prints 2/3
85
86 OpenKey open_key{100, 234, 23452, 15434};
87
88 cout << "";
89 return 0;
90}