Skip to content

Commit 6d3fa75

Browse files
authored
Merge pull request #161 from ademcan/master
Add Dilithium+Kyber to webAssembly build
2 parents 10ab0be + 563ac25 commit 6d3fa75

File tree

5 files changed

+177
-17
lines changed

5 files changed

+177
-17
lines changed

.circleci/build_emscripten.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ emmake make
77
emcc --bind libjsqrl.so -s DISABLE_EXCEPTION_CATCHING=0 -O3 --memory-init-file 0 -o libjsqrl.js
88
emcc --bind libjsqrl.so -s DISABLE_EXCEPTION_CATCHING=0 -O3 -s WASM=1 -o web-libjsqrl.js
99
emcc --bind libjsqrl.so -s DISABLE_EXCEPTION_CATCHING=0 -O3 -s WASM=1 -s SINGLE_FILE=1 -o offline-libjsqrl.js
10+
emcc --bind libjsdilithium.so -s DISABLE_EXCEPTION_CATCHING=0 -O3 -s WASM=1 -s SINGLE_FILE=1 -o offline-libjsdilithium.js
11+
emcc --bind libjskyber.so -s DISABLE_EXCEPTION_CATCHING=0 -O3 -s WASM=1 -s SINGLE_FILE=1 -o offline-libjskyber.js
1012
echo "QRLLIB=Module;" >> web-libjsqrl.js
1113
echo "QRLLIB=Module;" >> offline-libjsqrl.js
14+
echo "DILLIB=Module;" >> offline-libjsdilithium.js
15+
echo "KYBLIB=Module;" >> offline-libjskyber.js
1216

1317
# Fix paths of web-libjsqrl.wasm for web clients
1418
sed -i 's/web-libjsqrl\.wasm/\/web-libjsqrl\.wasm/g' web-libjsqrl.js

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,4 @@ tests/js/tmp/*.js
7272
tests/js/tmp/*.wasm
7373

7474
\.npmrc
75+
.vscode/*

CMakeLists.txt

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -552,22 +552,31 @@ if (BUILD_WEBASSEMBLY)
552552
PROPERTIES LINK_FLAGS "-s DISABLE_EXCEPTION_CATCHING=0"
553553
)
554554

555-
# set(JS_DILITHIUM_INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/src/jswrapper/jsdilwrapper.cpp)
556-
# set(JS_KYBINTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/src/jswrapper/jskybwrapper.cpp)
557-
# add_library(jsdilithium SHARED
558-
# ${JS_DILITHIUM_INTERFACE}
559-
# ${LIB_KYBER_SRC}
560-
# ${LIBDILITHIUM_SRC}
561-
# )
562-
# target_include_directories( jsdilithium PRIVATE
563-
# ${LIB_DILITHIUM_INCLUDES} )
564-
#
565-
# add_library(jskyber SHARED
566-
# ${JS_KYBER_INTERFACE}
567-
# ${LIB_KYBER_SRC}
568-
# ${LIBKYBER_SRC}
569-
# )
570-
# target_include_directories( jskyber PRIVATE
571-
# ${LIB_KYBER_INCLUDES} )
555+
556+
set(JS_DILITHIUM_INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/src/jswrapper/jsdilwrapper.cpp)
557+
558+
add_library(jsdilithium SHARED
559+
${JS_DILITHIUM_INTERFACE}
560+
${LIB_DILITHIUM_SRC}
561+
${LIBDILITHIUM_SRC}
562+
${REF_DILITHIUM_SRC}
563+
${LIB_QRL_SRC}
564+
)
565+
target_include_directories( jsdilithium PRIVATE
566+
${LIB_QRL_INCLUDES}
567+
${LIB_DILITHIUM_INCLUDES} )
568+
569+
set(JS_KYBER_INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/src/jswrapper/jskybwrapper.cpp)
570+
571+
add_library(jskyber SHARED
572+
${JS_KYBER_INTERFACE}
573+
${LIB_KYBER_SRC}
574+
${LIBKYBER_SRC}
575+
${REF_KYBER_SRC}
576+
${LIB_QRL_SRC}
577+
)
578+
target_include_directories( jskyber PRIVATE
579+
${LIB_QRL_INCLUDES}
580+
${LIB_KYBER_INCLUDES} )
572581

573582
endif ()

src/jswrapper/jsdilwrapper.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,79 @@
11
#include <emscripten.h>
22
#include <emscripten/bind.h>
33
#include <iostream>
4+
#include <dilithium.h>
5+
#include <misc.h>
46

57
namespace {
8+
class DilithiumWrapper {
9+
explicit DilithiumWrapper()
10+
:_dilithium() { }
11+
12+
public:
13+
14+
static DilithiumWrapper empty()
15+
{
16+
return DilithiumWrapper();
17+
}
18+
19+
std::vector<uint8_t> getSKRaw()
20+
{
21+
return _dilithium.getSK() ;
22+
}
23+
24+
std::string getSK()
25+
{
26+
return bin2hstr( _dilithium.getSK() );
27+
}
28+
29+
std::vector<uint8_t> getPKRaw()
30+
{
31+
return _dilithium.getPK() ;
32+
}
33+
34+
std::string getPK()
35+
{
36+
return bin2hstr( _dilithium.getPK() );
37+
}
38+
39+
private:
40+
Dilithium _dilithium;
41+
};
42+
43+
std::string EMSCRIPTEN_KEEPALIVE
44+
_bin2hstr(const std::vector<unsigned char>& input)
45+
{
46+
return bin2hstr(input, 0);
47+
}
48+
49+
std::string EMSCRIPTEN_KEEPALIVE
50+
_getString()
51+
{
52+
return "Test String from Dilithium JS Wrapper";
53+
}
54+
55+
int EMSCRIPTEN_KEEPALIVE
56+
crypto_sign_keypair(
57+
unsigned char pk,
58+
unsigned char sk)
59+
{
60+
return crypto_sign_keypair(pk, sk);
61+
}
62+
63+
using namespace emscripten;
64+
65+
EMSCRIPTEN_BINDINGS(my_module) {
66+
67+
function("getString", &_getString);
68+
function("crypto_sign_keypair", &crypto_sign_keypair);
69+
function("bin2hstr", &_bin2hstr);
70+
71+
class_<DilithiumWrapper>("Dilithium")
72+
.class_function("empty", &DilithiumWrapper::empty)
73+
.function("getPKRaw", &DilithiumWrapper::getPKRaw)
74+
.function("getPK", &DilithiumWrapper::getPK)
75+
.function("getSKRaw", &DilithiumWrapper::getSKRaw)
76+
.function("getSK", &DilithiumWrapper::getSK);
77+
}
78+
679
}

src/jswrapper/jskybwrapper.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,79 @@
11
#include <emscripten.h>
22
#include <emscripten/bind.h>
33
#include <iostream>
4+
#include <kyber.h>
5+
#include <misc.h>
46

57
namespace {
8+
class KyberWrapper {
9+
explicit KyberWrapper()
10+
:_kyber() { }
11+
12+
public:
13+
14+
static KyberWrapper empty()
15+
{
16+
return KyberWrapper();
17+
}
18+
19+
std::vector<uint8_t> getSKRaw()
20+
{
21+
return _kyber.getSK() ;
22+
}
23+
24+
std::string getSK()
25+
{
26+
return bin2hstr( _kyber.getSK() );
27+
}
28+
29+
std::vector<uint8_t> getPKRaw()
30+
{
31+
return _kyber.getPK() ;
32+
}
33+
34+
std::string getPK()
35+
{
36+
return bin2hstr( _kyber.getPK() );
37+
}
38+
39+
private:
40+
Kyber _kyber;
41+
};
42+
43+
std::string EMSCRIPTEN_KEEPALIVE
44+
_bin2hstr(const std::vector<unsigned char>& input)
45+
{
46+
return bin2hstr(input, 0);
47+
}
48+
49+
std::string EMSCRIPTEN_KEEPALIVE
50+
_getString()
51+
{
52+
return "Test String from Kyber JS Wrapper";
53+
}
54+
55+
int EMSCRIPTEN_KEEPALIVE
56+
crypto_kem_keypair(
57+
unsigned char pk,
58+
unsigned char sk)
59+
{
60+
return crypto_kem_keypair(pk, sk);
61+
}
62+
63+
using namespace emscripten;
64+
65+
EMSCRIPTEN_BINDINGS(my_module) {
66+
67+
function("getString", &_getString);
68+
function("crypto_kem_keypair", &crypto_kem_keypair);
69+
function("bin2hstr", &_bin2hstr);
70+
71+
class_<KyberWrapper>("Kyber")
72+
.class_function("empty", &KyberWrapper::empty)
73+
.function("getPKRaw", &KyberWrapper::getPKRaw)
74+
.function("getPK", &KyberWrapper::getPK)
75+
.function("getSKRaw", &KyberWrapper::getSKRaw)
76+
.function("getSK", &KyberWrapper::getSK);
77+
}
78+
679
}

0 commit comments

Comments
 (0)