-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbob.c
77 lines (68 loc) · 1.41 KB
/
bob.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "bob.h"
bignum256 b;
bignum256 d[LEN];
bignum256 d_ = {0};
static bignum256 p_kb;
//generate b
void gen_b()
{
bn_rand(&b, &secp256k1.prime);
}
//get b
void get_b()
{
uint64_t b_;
scanf("%lu", &b_);
bn_read_uint64(b_, &b);
}
void bob_p_kb()
{
bn_rand(&p_kb, &secp256k1.prime);
}
void bob_pub_kb(int i)
{
if(bn_testbit(&b, i))
{
//pub_kb=p_kb*(G)+A
point_multiply(&secp256k1, &p_kb, &secp256k1.G, &pub_kb);
point_add(&secp256k1, &pub_ka, &pub_kb);
}
else
{
//pub_kb=p_kb*G
point_multiply(&secp256k1, &p_kb, &secp256k1.G, &pub_kb);
}
}
void decrypt(int i, bignum256 *enc_m)
{
// Prepare decryption key kr=hash(p_kb*A)
curve_point kr_key;
point_multiply(&secp256k1, &p_kb, &pub_ka, &kr_key);
bn_multiply(&kr_key.x, &kr_key.y, &secp256k1.prime);
bignum256 kr;
bn_mod(&kr_key.y, &secp256k1.prime);
get_hash(&kr_key.y, &kr);
// Decrypt the message
if (bn_testbit(&b, i))
{
bn_xor(&d[i], &kr, &enc_m[1]);
}
else
{
bn_xor(&d[i], &kr, &enc_m[0]);
}
}
void calculate_d()
{
bn_zero(&d_);
for (int i = 1; i <= LEN; i++)
{
bignum256 temp;
bn_zero(&temp);
bn_setbit(&temp, i);
bn_multiply(&temp, &d[i], &secp256k1.prime);
bn_mod(&d[i], &secp256k1.prime);
bn_add(&d_, &d[i]);
}
bn_negate(&d_, &secp256k1.prime);
}