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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
# Copyright 2013-2018 Donald Stufft and individual contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from nacl import exceptions as exc
from nacl._sodium import ffi, lib
from nacl.exceptions import ensure
has_crypto_scalarmult_ed25519 = bool(lib.PYNACL_HAS_CRYPTO_SCALARMULT_ED25519)
crypto_scalarmult_BYTES: int = lib.crypto_scalarmult_bytes()
crypto_scalarmult_SCALARBYTES: int = lib.crypto_scalarmult_scalarbytes()
crypto_scalarmult_ed25519_BYTES = 0
crypto_scalarmult_ed25519_SCALARBYTES = 0
if has_crypto_scalarmult_ed25519:
crypto_scalarmult_ed25519_BYTES = lib.crypto_scalarmult_ed25519_bytes()
crypto_scalarmult_ed25519_SCALARBYTES = (
lib.crypto_scalarmult_ed25519_scalarbytes()
)
def crypto_scalarmult_base(n: bytes) -> bytes:
"""
Computes and returns the scalar product of a standard group element and an
integer ``n``.
:param n: bytes
:rtype: bytes
"""
q = ffi.new("unsigned char[]", crypto_scalarmult_BYTES)
rc = lib.crypto_scalarmult_base(q, n)
ensure(rc == 0, "Unexpected library error", raising=exc.RuntimeError)
return ffi.buffer(q, crypto_scalarmult_SCALARBYTES)[:]
def crypto_scalarmult(n: bytes, p: bytes) -> bytes:
"""
Computes and returns the scalar product of the given group element and an
integer ``n``.
:param p: bytes
:param n: bytes
:rtype: bytes
"""
q = ffi.new("unsigned char[]", crypto_scalarmult_BYTES)
rc = lib.crypto_scalarmult(q, n, p)
ensure(rc == 0, "Unexpected library error", raising=exc.RuntimeError)
return ffi.buffer(q, crypto_scalarmult_SCALARBYTES)[:]
def crypto_scalarmult_ed25519_base(n: bytes) -> bytes:
"""
Computes and returns the scalar product of a standard group element and an
integer ``n`` on the edwards25519 curve.
:param n: a :py:data:`.crypto_scalarmult_ed25519_SCALARBYTES` long bytes
sequence representing a scalar
:type n: bytes
:return: a point on the edwards25519 curve, represented as a
:py:data:`.crypto_scalarmult_ed25519_BYTES` long bytes sequence
:rtype: bytes
:raises nacl.exceptions.UnavailableError: If called when using a
minimal build of libsodium.
"""
ensure(
has_crypto_scalarmult_ed25519,
"Not available in minimal build",
raising=exc.UnavailableError,
)
ensure(
isinstance(n, bytes)
and len(n) == crypto_scalarmult_ed25519_SCALARBYTES,
"Input must be a {} long bytes sequence".format(
"crypto_scalarmult_ed25519_SCALARBYTES"
),
raising=exc.TypeError,
)
q = ffi.new("unsigned char[]", crypto_scalarmult_ed25519_BYTES)
rc = lib.crypto_scalarmult_ed25519_base(q, n)
ensure(rc == 0, "Unexpected library error", raising=exc.RuntimeError)
return ffi.buffer(q, crypto_scalarmult_ed25519_BYTES)[:]
def crypto_scalarmult_ed25519_base_noclamp(n: bytes) -> bytes:
"""
Computes and returns the scalar product of a standard group element and an
integer ``n`` on the edwards25519 curve. The integer ``n`` is not clamped.
:param n: a :py:data:`.crypto_scalarmult_ed25519_SCALARBYTES` long bytes
sequence representing a scalar
:type n: bytes
:return: a point on the edwards25519 curve, represented as a
:py:data:`.crypto_scalarmult_ed25519_BYTES` long bytes sequence
:rtype: bytes
:raises nacl.exceptions.UnavailableError: If called when using a
minimal build of libsodium.
"""
ensure(
has_crypto_scalarmult_ed25519,
"Not available in minimal build",
raising=exc.UnavailableError,
)
ensure(
isinstance(n, bytes)
and len(n) == crypto_scalarmult_ed25519_SCALARBYTES,
"Input must be a {} long bytes sequence".format(
"crypto_scalarmult_ed25519_SCALARBYTES"
),
raising=exc.TypeError,
)
q = ffi.new("unsigned char[]", crypto_scalarmult_ed25519_BYTES)
rc = lib.crypto_scalarmult_ed25519_base_noclamp(q, n)
ensure(rc == 0, "Unexpected library error", raising=exc.RuntimeError)
return ffi.buffer(q, crypto_scalarmult_ed25519_BYTES)[:]
def crypto_scalarmult_ed25519(n: bytes, p: bytes) -> bytes:
"""
Computes and returns the scalar product of a *clamped* integer ``n``
and the given group element on the edwards25519 curve.
The scalar is clamped, as done in the public key generation case,
by setting to zero the bits in position [0, 1, 2, 255] and setting
to one the bit in position 254.
:param n: a :py:data:`.crypto_scalarmult_ed25519_SCALARBYTES` long bytes
sequence representing a scalar
:type n: bytes
:param p: a :py:data:`.crypto_scalarmult_ed25519_BYTES` long bytes sequence
representing a point on the edwards25519 curve
:type p: bytes
:return: a point on the edwards25519 curve, represented as a
:py:data:`.crypto_scalarmult_ed25519_BYTES` long bytes sequence
:rtype: bytes
:raises nacl.exceptions.UnavailableError: If called when using a
minimal build of libsodium.
"""
ensure(
has_crypto_scalarmult_ed25519,
"Not available in minimal build",
raising=exc.UnavailableError,
)
ensure(
isinstance(n, bytes)
and len(n) == crypto_scalarmult_ed25519_SCALARBYTES,
"Input must be a {} long bytes sequence".format(
"crypto_scalarmult_ed25519_SCALARBYTES"
),
raising=exc.TypeError,
)
ensure(
isinstance(p, bytes) and len(p) == crypto_scalarmult_ed25519_BYTES,
"Input must be a {} long bytes sequence".format(
"crypto_scalarmult_ed25519_BYTES"
),
raising=exc.TypeError,
)
q = ffi.new("unsigned char[]", crypto_scalarmult_ed25519_BYTES)
rc = lib.crypto_scalarmult_ed25519(q, n, p)
ensure(rc == 0, "Unexpected library error", raising=exc.RuntimeError)
return ffi.buffer(q, crypto_scalarmult_ed25519_BYTES)[:]
def crypto_scalarmult_ed25519_noclamp(n: bytes, p: bytes) -> bytes:
"""
Computes and returns the scalar product of an integer ``n``
and the given group element on the edwards25519 curve. The integer
``n`` is not clamped.
:param n: a :py:data:`.crypto_scalarmult_ed25519_SCALARBYTES` long bytes
sequence representing a scalar
:type n: bytes
:param p: a :py:data:`.crypto_scalarmult_ed25519_BYTES` long bytes sequence
representing a point on the edwards25519 curve
:type p: bytes
:return: a point on the edwards25519 curve, represented as a
:py:data:`.crypto_scalarmult_ed25519_BYTES` long bytes sequence
:rtype: bytes
:raises nacl.exceptions.UnavailableError: If called when using a
minimal build of libsodium.
"""
ensure(
has_crypto_scalarmult_ed25519,
"Not available in minimal build",
raising=exc.UnavailableError,
)
ensure(
isinstance(n, bytes)
and len(n) == crypto_scalarmult_ed25519_SCALARBYTES,
"Input must be a {} long bytes sequence".format(
"crypto_scalarmult_ed25519_SCALARBYTES"
),
raising=exc.TypeError,
)
ensure(
isinstance(p, bytes) and len(p) == crypto_scalarmult_ed25519_BYTES,
"Input must be a {} long bytes sequence".format(
"crypto_scalarmult_ed25519_BYTES"
),
raising=exc.TypeError,
)
q = ffi.new("unsigned char[]", crypto_scalarmult_ed25519_BYTES)
rc = lib.crypto_scalarmult_ed25519_noclamp(q, n, p)
ensure(rc == 0, "Unexpected library error", raising=exc.RuntimeError)
return ffi.buffer(q, crypto_scalarmult_ed25519_BYTES)[:]
|