This repository has been archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmc_ed25519.nim
128 lines (106 loc) · 2.59 KB
/
mc_ed25519.nim
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#Include and link the Ed25519 library.
const currentFolder = currentSourcePath().substr(0, currentSourcePath().len - 15)
{.passC: "-I" & currentFolder & "ed25519/src/".}
{.compile: currentFolder & "ed25519/src/add_scalar.c".}
{.compile: currentFolder & "ed25519/src/fe.c".}
{.compile: currentFolder & "ed25519/src/ge.c".}
{.compile: currentFolder & "ed25519/src/key_exchange.c".}
{.compile: currentFolder & "ed25519/src/keypair.c".}
{.compile: currentFolder & "ed25519/src/sc.c".}
{.compile: currentFolder & "ed25519/src/seed.c".}
{.compile: currentFolder & "ed25519/src/sha512.c".}
{.compile: currentFolder & "ed25519/src/sign.c".}
{.compile: currentFolder & "ed25519/src/verify.c".}
type
PrivateKey* = array[64, cuchar]
PublicKey* = array[32, cuchar]
{.push header: "sc.h".}
proc mulAdd*(
res: ptr cuchar,
a: ptr cuchar,
b: ptr cuchar,
c: ptr cuchar
) {.importc: "sc_muladd".}
#Reduce a scalar.
proc reduceScalar*(
scalar: ptr cuchar
) {.importc: "sc_reduce".}
{.pop.}
{.push header: "ge.h".}
#Define the Ed25519 objects.
type
PointP1P1* {.
header: "ge.h",
importc: "ge_p1p1"
.} = object
Point2* {.
header: "ge.h",
importc: "ge_p2"
.} = object
Point3* {.
header: "ge.h",
importc: "ge_p3"
.} = object
PointCached* {.
header: "ge.h",
importc: "ge_cached"
.} = object
#Convert a Public Key to a Point3.
proc keyToNegativePoint*(
res: ptr Point3,
bytes: ptr cuchar
) {.importc: "ge_frombytes_negate_vartime".}
proc p3ToCached*(
cached: ptr PointCached,
p3: ptr Point3
) {.importc: "ge_p3_to_cached".}
proc p1p1ToP3*(
p3: ptr Point3,
p1p1: ptr PointP1P1
) {.importc: "ge_p1p1_to_p3".}
#Add two points.
proc add*(
res: ptr PointP1P1,
p: ptr Point3,
p2: ptr PointCached
) {.importc: "ge_add".}
#Multiply by Ed25519's base.
proc multiplyBase*(
res: ptr Point3,
point: ptr cuchar
) {.importc: "ge_scalarmult_base".}
#Multiply scalars.
proc multiplyScalar*(
res: ptr Point2,
scalar: ptr cuchar,
point: ptr Point3,
addScalar: ptr cuchar
) {.importc: "ge_double_scalarmult_vartime".}
#Serialize a Point2.
proc serialize*(
res: ptr cuchar,
point2: ptr Point2
) {.importc: "ge_tobytes".}
#Serialize a Point3.
proc serialize*(
res: ptr cuchar,
point: ptr Point3
) {.importc: "ge_p3_tobytes".}
{.pop.}
{.push header: "ed25519.h".}
#Sign a message.
proc sign*(
sig: ptr cuchar,
msg: ptr cuchar,
msgLen: csize,
pubKey: ptr cuchar,
privKey: ptr cuchar
) {.importc: "ed25519_sign".}
#Verify a message.
proc verify*(
sig: ptr cuchar,
msg: ptr cuchar,
msgLen: csize,
pubKey: ptr cuchar
): int {.importc: "ed25519_verify".}
{.pop.}