aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/byzantine-lab/mcl/src/ecdsa_c.cpp
blob: f2222a224d3c8064ce5bae9071afa20e26ea4abd (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#define ECDSA_DLL_EXPORT
#include <mcl/ecdsa.h>
#include <mcl/ecdsa.hpp>
#include <new>

using namespace mcl::ecdsa;

static SecretKey *cast(ecdsaSecretKey *p) { return reinterpret_cast<SecretKey*>(p); }
static const SecretKey *cast(const ecdsaSecretKey *p) { return reinterpret_cast<const SecretKey*>(p); }

static PublicKey *cast(ecdsaPublicKey *p) { return reinterpret_cast<PublicKey*>(p); }
static const PublicKey *cast(const ecdsaPublicKey *p) { return reinterpret_cast<const PublicKey*>(p); }

static Signature *cast(ecdsaSignature *p) { return reinterpret_cast<Signature*>(p); }
static const Signature *cast(const ecdsaSignature *p) { return reinterpret_cast<const Signature*>(p); }

static PrecomputedPublicKey *cast(ecdsaPrecomputedPublicKey *p) { return reinterpret_cast<PrecomputedPublicKey*>(p); }
static const PrecomputedPublicKey *cast(const ecdsaPrecomputedPublicKey *p) { return reinterpret_cast<const PrecomputedPublicKey*>(p); }

#ifdef __EMSCRIPTEN__
// use these functions forcibly
extern "C" ECDSA_DLL_API void *ecdsaMalloc(size_t n)
{
    return malloc(n);
}
extern "C" ECDSA_DLL_API void ecdsaFree(void *p)
{
    free(p);
}
#endif

int ecdsaInit(void)
{
    bool b;
    init(&b);
    return b ? 0 : -1;
}

mclSize ecdsaSecretKeySerialize(void *buf, mclSize maxBufSize, const ecdsaSecretKey *sec)
{
    return (mclSize)cast(sec)->serialize(buf, maxBufSize);
}
mclSize ecdsaPublicKeySerialize(void *buf, mclSize maxBufSize, const ecdsaPublicKey *pub)
{
    return (mclSize)cast(pub)->serialize(buf, maxBufSize);
}
mclSize ecdsaSignatureSerialize(void *buf, mclSize maxBufSize, const ecdsaSignature *sig)
{
    return (mclSize)cast(sig)->serialize(buf, maxBufSize);
}

mclSize ecdsaSecretKeyDeserialize(ecdsaSecretKey* sec, const void *buf, mclSize bufSize)
{
    return (mclSize)cast(sec)->deserialize(buf, bufSize);
}
mclSize ecdsaPublicKeyDeserialize(ecdsaPublicKey* pub, const void *buf, mclSize bufSize)
{
    return (mclSize)cast(pub)->deserialize(buf, bufSize);
}
mclSize ecdsaSignatureDeserialize(ecdsaSignature* sig, const void *buf, mclSize bufSize)
{
    return (mclSize)cast(sig)->deserialize(buf, bufSize);
}

//  return 0 if success
int ecdsaSecretKeySetByCSPRNG(ecdsaSecretKey *sec)
{
    cast(sec)->setByCSPRNG();
    return 0;
}

void ecdsaGetPublicKey(ecdsaPublicKey *pub, const ecdsaSecretKey *sec)
{
    getPublicKey(*cast(pub), *cast(sec));
}

void ecdsaSign(ecdsaSignature *sig, const ecdsaSecretKey *sec, const void *m, mclSize size)
{
    sign(*cast(sig), *cast(sec), m, size);
}

int ecdsaVerify(const ecdsaSignature *sig, const ecdsaPublicKey *pub, const void *m, mclSize size)
{
    return verify(*cast(sig), *cast(pub), m, size);
}
int ecdsaVerifyPrecomputed(const ecdsaSignature *sig, const ecdsaPrecomputedPublicKey *ppub, const void *m, mclSize size)
{
    return verify(*cast(sig), *cast(ppub), m, size);
}

ecdsaPrecomputedPublicKey *ecdsaPrecomputedPublicKeyCreate()
{
    PrecomputedPublicKey *ppub = (PrecomputedPublicKey*)malloc(sizeof(PrecomputedPublicKey));
    if (ppub == 0) return 0;
    new(ppub) PrecomputedPublicKey();
    return reinterpret_cast<ecdsaPrecomputedPublicKey*>(ppub);
}

void ecdsaPrecomputedPublicKeyDestroy(ecdsaPrecomputedPublicKey *ppub)
{
    cast(ppub)->~PrecomputedPublicKey();
    free(ppub);
}

int ecdsaPrecomputedPublicKeyInit(ecdsaPrecomputedPublicKey *ppub, const ecdsaPublicKey *pub)
{
    bool b;
    cast(ppub)->init(&b, *cast(pub));
    return b ? 0 : -1;
}