UNPKG

1.25 kBJavaScriptView Raw
1/* eslint-disable camelcase */
2const { crypto_scalarmult_base } = require('./crypto_scalarmult')
3const { crypto_generichash } = require('./crypto_generichash')
4const { randombytes_buf } = require('./randombytes')
5const assert = require('nanoassert')
6
7const crypto_kx_SEEDBYTES = 32
8const crypto_kx_PUBLICKEYBYTES = 32
9const crypto_kx_SECRETKEYBYTES = 32
10
11function crypto_kx_keypair (pk, sk) {
12 assert(pk.byteLength === crypto_kx_PUBLICKEYBYTES, "pk must be 'crypto_kx_PUBLICKEYBYTES' bytes")
13 assert(sk.byteLength === crypto_kx_SECRETKEYBYTES, "sk must be 'crypto_kx_SECRETKEYBYTES' bytes")
14
15 randombytes_buf(sk, crypto_kx_SECRETKEYBYTES)
16 return crypto_scalarmult_base(pk, sk)
17}
18
19function crypto_kx_seed_keypair (pk, sk, seed) {
20 assert(pk.byteLength === crypto_kx_PUBLICKEYBYTES, "pk must be 'crypto_kx_PUBLICKEYBYTES' bytes")
21 assert(sk.byteLength === crypto_kx_SECRETKEYBYTES, "sk must be 'crypto_kx_SECRETKEYBYTES' bytes")
22 assert(seed.byteLength === crypto_kx_SEEDBYTES, "seed must be 'crypto_kx_SEEDBYTES' bytes")
23
24 crypto_generichash(sk, seed)
25 return crypto_scalarmult_base(pk, sk)
26}
27
28module.exports = {
29 crypto_kx_keypair,
30 crypto_kx_seed_keypair,
31 crypto_kx_SEEDBYTES,
32 crypto_kx_SECRETKEYBYTES,
33 crypto_kx_PUBLICKEYBYTES
34}