aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/dexon-foundation/mcl/sample/bls_sig.cpp
blob: d75f7d4277a2a6a8c2b42f29d3c6477b1df8e937 (plain) (blame)
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
/**
    @file
    @brief a sample of BLS signature
    see https://github.com/herumi/bls
    @author MITSUNARI Shigeo(@herumi)
    @license modified new BSD license
    http://opensource.org/licenses/BSD-3-Clause

*/
#include <mcl/bn256.hpp>
#include <iostream>

using namespace mcl::bn256;

void Hash(G1& P, const std::string& m)
{
    Fp t;
    t.setHashOf(m);
    mapToG1(P, t);
}

void KeyGen(Fr& s, G2& pub, const G2& Q)
{
    s.setRand();
    G2::mul(pub, Q, s); // pub = sQ
}

void Sign(G1& sign, const Fr& s, const std::string& m)
{
    G1 Hm;
    Hash(Hm, m);
    G1::mul(sign, Hm, s); // sign = s H(m)
}

bool Verify(const G1& sign, const G2& Q, const G2& pub, const std::string& m)
{
    Fp12 e1, e2;
    G1 Hm;
    Hash(Hm, m);
    pairing(e1, sign, Q); // e1 = e(sign, Q)
    pairing(e2, Hm, pub); // e2 = e(Hm, sQ)
    return e1 == e2;
}

int main(int argc, char *argv[])
{
    std::string m = argc == 1 ? "hello mcl" : argv[1];

    // setup parameter
    initPairing();
    G2 Q;
    mapToG2(Q, 1);

    // generate secret key and public key
    Fr s;
    G2 pub;
    KeyGen(s, pub, Q);
    std::cout << "secret key " << s << std::endl;
    std::cout << "public key " << pub << std::endl;

    // sign
    G1 sign;
    Sign(sign, s, m);
    std::cout << "msg " << m << std::endl;
    std::cout << "sign " << sign << std::endl;

    // verify
    bool ok = Verify(sign, Q, pub, m);
    std::cout << "verify " << (ok ? "ok" : "ng") << std::endl;
}