-
Notifications
You must be signed in to change notification settings - Fork 0
/
secure_id.hpp
93 lines (79 loc) · 2.93 KB
/
secure_id.hpp
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#ifndef SECUREID_SECURE_ID_HPP
#define SECUREID_SECURE_ID_HPP
#include <mcl/bn256.hpp>
namespace SecureID {
#define SID_BYTE_SIZE 32
class Init {
public:
Init() {
mcl::bn::initPairing();
if (!mcl::bn::setMapToMode(MCL_MAP_TO_MODE_ORIGINAL)) {
throw std::invalid_argument("SetMapToMode");
}
basePoint.setStr("1 0x2523648240000001BA344D80000000086121000000000013A700000000000012 0x01", 16);
}
mcl::bn::G1 basePoint;
};
inline Init _init;
class PublicKey : public mcl::bn::G1 {
public:
void blind(unsigned char out[SID_BYTE_SIZE], const char *msg, size_t msg_len, const mcl::bn::Fr* random) {
mcl::bn::G1 gin, gout;
mcl::bn::hashAndMapToG1(gin, msg, msg_len);
mcl::bn::G1::mul(gout, _init.basePoint, *random);
mcl::bn::G1::add(gout, gin, gout); // IN + r * G
size_t n = gout.serialize(out, SID_BYTE_SIZE);
if (n == 0) {
throw std::invalid_argument("err serialize");
}
}
void unblind(unsigned char out[SID_BYTE_SIZE], unsigned char in[SID_BYTE_SIZE], const mcl::bn::Fr* random) {
mcl::bn::G1 gin, gout;
size_t n = gin.deserialize(in, SID_BYTE_SIZE);
if (n != SID_BYTE_SIZE) {
throw std::invalid_argument("err deserialize");
}
mcl::bn::G1::mul(gout, *this, *random);
mcl::bn::G1::sub(gout, gin, gout); // IN - r * Q
n = gout.serialize(out, SID_BYTE_SIZE);
if (n == 0) {
throw std::invalid_argument("err serialize");
}
}
};
class SecretKey : public mcl::bn::Fr {
public:
static SecretKey generate() {
SecretKey sk;
sk.setByCSPRNG();
return sk;
}
void sign1(unsigned char out[SID_BYTE_SIZE], const char *msg, size_t msg_len) {
mcl::bn::G1 gin, gout;
mcl::bn::hashAndMapToG1(gin, msg, msg_len);
mcl::bn::G1::mul(gout, gin, *this);
size_t n = gout.serialize(out, SID_BYTE_SIZE);
if (n == 0) {
throw std::invalid_argument("err serialize");
}
}
void sign2(unsigned char out[SID_BYTE_SIZE], unsigned char in[SID_BYTE_SIZE]) {
mcl::bn::G1 gin, gout;
size_t n = gin.deserialize(in, SID_BYTE_SIZE);
if (n != SID_BYTE_SIZE) {
throw std::invalid_argument("err deserialize");
}
mcl::bn::G1::mul(gout, gin, *this);
n = gout.serialize(out, SID_BYTE_SIZE);
if (n == 0) {
throw std::invalid_argument("err serialize");
}
}
PublicKey public_key() {
PublicKey pk;
mcl::bn::G1::mul(pk, _init.basePoint, *this);
return pk;
}
};
}
#endif //SECUREID_SECURE_ID_HPP