Skip to content
Snippets Groups Projects
Commit 068e8820 authored by Brian Warner's avatar Brian Warner
Browse files

expose and test crypto_scalarmult

fixes #65
parent e0f791c9
No related branches found
No related tags found
No related merge requests found
...@@ -17,3 +17,4 @@ size_t crypto_scalarmult_bytes(); ...@@ -17,3 +17,4 @@ size_t crypto_scalarmult_bytes();
size_t crypto_scalarmult_scalarbytes(); size_t crypto_scalarmult_scalarbytes();
int crypto_scalarmult_base(unsigned char *q, const unsigned char *n); int crypto_scalarmult_base(unsigned char *q, const unsigned char *n);
int crypto_scalarmult(unsigned char *q, const unsigned char *n, const unsigned char *p);
...@@ -25,7 +25,7 @@ from nacl.c.crypto_hash import ( ...@@ -25,7 +25,7 @@ from nacl.c.crypto_hash import (
) )
from nacl.c.crypto_scalarmult import ( from nacl.c.crypto_scalarmult import (
crypto_scalarmult_BYTES, crypto_scalarmult_SCALARBYTES, crypto_scalarmult_BYTES, crypto_scalarmult_SCALARBYTES,
crypto_scalarmult_base, crypto_scalarmult, crypto_scalarmult_base,
) )
from nacl.c.crypto_secretbox import ( from nacl.c.crypto_secretbox import (
crypto_secretbox_KEYBYTES, crypto_secretbox_NONCEBYTES, crypto_secretbox_KEYBYTES, crypto_secretbox_NONCEBYTES,
...@@ -63,6 +63,7 @@ __all__ = [ ...@@ -63,6 +63,7 @@ __all__ = [
"crypto_scalarmult_BYTES", "crypto_scalarmult_BYTES",
"crypto_scalarmult_SCALARBYTES", "crypto_scalarmult_SCALARBYTES",
"crypto_scalarmult",
"crypto_scalarmult_base", "crypto_scalarmult_base",
"crypto_secretbox_KEYBYTES", "crypto_secretbox_KEYBYTES",
......
...@@ -36,3 +36,21 @@ def crypto_scalarmult_base(n): ...@@ -36,3 +36,21 @@ def crypto_scalarmult_base(n):
"An error occurred while computing the scalar product") "An error occurred while computing the scalar product")
return lib.ffi.buffer(q, crypto_scalarmult_SCALARBYTES)[:] return lib.ffi.buffer(q, crypto_scalarmult_SCALARBYTES)[:]
def crypto_scalarmult(n, p):
"""
Computes and returns the scalar product of the given group element and an
integer ``n``.
:param p: bytes
:param n: bytes
:rtype: bytes
"""
q = lib.ffi.new("unsigned char[]", crypto_scalarmult_BYTES)
if lib.crypto_scalarmult(q, n, p) != 0:
raise CryptoError(
"An error occurred while computing the scalar product")
return lib.ffi.buffer(q, crypto_scalarmult_SCALARBYTES)[:]
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from binascii import hexlify from binascii import hexlify, unhexlify
from nacl import c from nacl import c
import hashlib import hashlib
...@@ -108,6 +108,21 @@ def test_scalarmult(): ...@@ -108,6 +108,21 @@ def test_scalarmult():
x, xpub = secret_scalar() x, xpub = secret_scalar()
assert len(x) == 32 assert len(x) == 32
y, ypub = secret_scalar() y, ypub = secret_scalar()
# the Curve25519 base point (generator)
bx = c.crypto_scalarmult_base(x) base = unhexlify(b"09" + b"00"*31)
assert tohex(bx) == tohex(xpub)
bx1 = c.crypto_scalarmult_base(x)
bx2 = c.crypto_scalarmult(x, base)
assert tohex(bx1) == tohex(bx2)
assert tohex(bx1) == tohex(xpub)
xby = c.crypto_scalarmult(x, c.crypto_scalarmult_base(y))
ybx = c.crypto_scalarmult(y, c.crypto_scalarmult_base(x))
assert tohex(xby) == tohex(ybx)
z = unhexlify(b"10"*32)
bz1 = c.crypto_scalarmult_base(z)
assert tohex(bz1) == ("781faab908430150daccdd6f9d6c5086"
"e34f73a93ebbaa271765e5036edfc519")
bz2 = c.crypto_scalarmult(z, base)
assert tohex(bz1) == tohex(bz2)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment