aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMITSUNARI Shigeo <herumi@nifty.com>2017-10-25 11:01:19 +0800
committerMITSUNARI Shigeo <herumi@nifty.com>2017-10-25 11:01:19 +0800
commit3219815819b17af5b182fdd14b4a10d4b8d2fb2c (patch)
treed5bb878c9234903f1d85723cab950ea467eb6670
parent00380d7d70f092b10351eb14c54daa2e475d17d7 (diff)
downloaddexon-mcl-3219815819b17af5b182fdd14b4a10d4b8d2fb2c.tar.gz
dexon-mcl-3219815819b17af5b182fdd14b4a10d4b8d2fb2c.tar.zst
dexon-mcl-3219815819b17af5b182fdd14b4a10d4b8d2fb2c.zip
[js] move exported-she.js to exported-she.json
-rw-r--r--Makefile4
-rw-r--r--docs/demo/exported-she.js46
-rw-r--r--docs/demo/exported-she.json266
-rw-r--r--docs/demo/she-demo.js76
-rw-r--r--docs/demo/she-ja.html1
-rw-r--r--docs/demo/she.html1
-rw-r--r--docs/demo/she.js893
-rw-r--r--docs/demo/she2.html1
-rw-r--r--ffi/js/export-functions.py37
9 files changed, 773 insertions, 552 deletions
diff --git a/Makefile b/Makefile
index 2ec5f49..a0e9349 100644
--- a/Makefile
+++ b/Makefile
@@ -206,7 +206,7 @@ test: $(TEST_EXE)
@sh -ec 'for i in $(TEST_EXE); do $$i|grep "ctest:name"; done' > result.txt
@grep -v "ng=0, exception=0" result.txt; if [ $$? -eq 1 ]; then echo "all unit tests succeed"; else exit 1; fi
-EXPORTED_SHE_JS=docs/demo/exported-she.js
+EXPORTED_SHE_JS=docs/demo/exported-she.json
SHE_TXT=ffi/js/she.txt
SHE_RE_TXT=ffi/js/she-re.txt
EXPORT_OPT=-re $(SHE_RE_TXT)
@@ -214,7 +214,7 @@ $(SHE_TXT): ./include/mcl/she.h $(SHE_RE_TXT)
python ffi/js/export-functions.py $(EXPORT_OPT) $< > $@
$(EXPORTED_SHE_JS): ./include/mcl/she.h
- python ffi/js/export-functions.py $(EXPORT_OPT) -js she $< > $@
+ python ffi/js/export-functions.py $(EXPORT_OPT) -json $< > docs/demo/exported-she.json
EXPORTED_SHE=$(shell cat $(SHE_TXT))
diff --git a/docs/demo/exported-she.js b/docs/demo/exported-she.js
deleted file mode 100644
index f4a10e1..0000000
--- a/docs/demo/exported-she.js
+++ /dev/null
@@ -1,46 +0,0 @@
-function define_exported_she(mod) {
-_sheInit = mod.cwrap('sheInit', 'number', ['number', 'number', ])
-_sheSecretKeySerialize = mod.cwrap('sheSecretKeySerialize', 'number', ['number', 'number', 'number', ])
-_shePublicKeySerialize = mod.cwrap('shePublicKeySerialize', 'number', ['number', 'number', 'number', ])
-_sheCipherTextG1Serialize = mod.cwrap('sheCipherTextG1Serialize', 'number', ['number', 'number', 'number', ])
-_sheCipherTextG2Serialize = mod.cwrap('sheCipherTextG2Serialize', 'number', ['number', 'number', 'number', ])
-_sheCipherTextGTSerialize = mod.cwrap('sheCipherTextGTSerialize', 'number', ['number', 'number', 'number', ])
-_sheSecretKeyDeserialize = mod.cwrap('sheSecretKeyDeserialize', 'number', ['number', 'number', 'number', ])
-_shePublicKeyDeserialize = mod.cwrap('shePublicKeyDeserialize', 'number', ['number', 'number', 'number', ])
-_sheCipherTextG1Deserialize = mod.cwrap('sheCipherTextG1Deserialize', 'number', ['number', 'number', 'number', ])
-_sheCipherTextG2Deserialize = mod.cwrap('sheCipherTextG2Deserialize', 'number', ['number', 'number', 'number', ])
-_sheCipherTextGTDeserialize = mod.cwrap('sheCipherTextGTDeserialize', 'number', ['number', 'number', 'number', ])
-sheSecretKeySetByCSPRNG = mod.cwrap('sheSecretKeySetByCSPRNG', 'number', ['number', ])
-sheGetPublicKey = mod.cwrap('sheGetPublicKey', 'null', ['number', 'number', ])
-sheSetRangeForDLP = mod.cwrap('sheSetRangeForDLP', 'number', ['number', 'number', ])
-sheSetRangeForG1DLP = mod.cwrap('sheSetRangeForG1DLP', 'number', ['number', 'number', ])
-sheSetRangeForG2DLP = mod.cwrap('sheSetRangeForG2DLP', 'number', ['number', 'number', ])
-sheSetRangeForGTDLP = mod.cwrap('sheSetRangeForGTDLP', 'number', ['number', 'number', ])
-sheEncG1 = mod.cwrap('sheEncG1', 'number', ['number', 'number', 'number', ])
-sheEncG2 = mod.cwrap('sheEncG2', 'number', ['number', 'number', 'number', ])
-sheEncGT = mod.cwrap('sheEncGT', 'number', ['number', 'number', 'number', ])
-sheEnc32G1 = mod.cwrap('sheEnc32G1', 'number', ['number', 'number', 'number', ])
-sheEnc32G2 = mod.cwrap('sheEnc32G2', 'number', ['number', 'number', 'number', ])
-sheEnc32GT = mod.cwrap('sheEnc32GT', 'number', ['number', 'number', 'number', ])
-_sheDecG1 = mod.cwrap('sheDecG1', 'number', ['number', 'number', 'number', ])
-_sheDecG2 = mod.cwrap('sheDecG2', 'number', ['number', 'number', 'number', ])
-_sheDecGT = mod.cwrap('sheDecGT', 'number', ['number', 'number', 'number', ])
-sheAddG1 = mod.cwrap('sheAddG1', 'number', ['number', 'number', 'number', ])
-sheAddG2 = mod.cwrap('sheAddG2', 'number', ['number', 'number', 'number', ])
-sheAddGT = mod.cwrap('sheAddGT', 'number', ['number', 'number', 'number', ])
-sheSubG1 = mod.cwrap('sheSubG1', 'number', ['number', 'number', 'number', ])
-sheSubG2 = mod.cwrap('sheSubG2', 'number', ['number', 'number', 'number', ])
-sheSubGT = mod.cwrap('sheSubGT', 'number', ['number', 'number', 'number', ])
-sheMulG1 = mod.cwrap('sheMulG1', 'number', ['number', 'number', 'number', ])
-sheMulG2 = mod.cwrap('sheMulG2', 'number', ['number', 'number', 'number', ])
-sheMulGT = mod.cwrap('sheMulGT', 'number', ['number', 'number', 'number', ])
-sheMul32G1 = mod.cwrap('sheMul32G1', 'number', ['number', 'number', 'number', ])
-sheMul32G2 = mod.cwrap('sheMul32G2', 'number', ['number', 'number', 'number', ])
-sheMul32GT = mod.cwrap('sheMul32GT', 'number', ['number', 'number', 'number', ])
-sheMul = mod.cwrap('sheMul', 'number', ['number', 'number', 'number', ])
-sheReRandG1 = mod.cwrap('sheReRandG1', 'number', ['number', 'number', ])
-sheReRandG2 = mod.cwrap('sheReRandG2', 'number', ['number', 'number', ])
-sheReRandGT = mod.cwrap('sheReRandGT', 'number', ['number', 'number', ])
-sheConvertFromG1 = mod.cwrap('sheConvertFromG1', 'number', ['number', 'number', 'number', ])
-sheConvertFromG2 = mod.cwrap('sheConvertFromG2', 'number', ['number', 'number', 'number', ])
-}
diff --git a/docs/demo/exported-she.json b/docs/demo/exported-she.json
new file mode 100644
index 0000000..74f1d2f
--- /dev/null
+++ b/docs/demo/exported-she.json
@@ -0,0 +1,266 @@
+[
+{
+ "name":"sheInit",
+ "exportName":"_sheInit",
+ "ret":"number",
+ "args":[ "number", "number" ]
+}
+,{
+ "name":"sheSecretKeySerialize",
+ "exportName":"_sheSecretKeySerialize",
+ "ret":"number",
+ "args":[ "number", "number", "number" ]
+}
+,{
+ "name":"shePublicKeySerialize",
+ "exportName":"_shePublicKeySerialize",
+ "ret":"number",
+ "args":[ "number", "number", "number" ]
+}
+,{
+ "name":"sheCipherTextG1Serialize",
+ "exportName":"_sheCipherTextG1Serialize",
+ "ret":"number",
+ "args":[ "number", "number", "number" ]
+}
+,{
+ "name":"sheCipherTextG2Serialize",
+ "exportName":"_sheCipherTextG2Serialize",
+ "ret":"number",
+ "args":[ "number", "number", "number" ]
+}
+,{
+ "name":"sheCipherTextGTSerialize",
+ "exportName":"_sheCipherTextGTSerialize",
+ "ret":"number",
+ "args":[ "number", "number", "number" ]
+}
+,{
+ "name":"sheSecretKeyDeserialize",
+ "exportName":"_sheSecretKeyDeserialize",
+ "ret":"number",
+ "args":[ "number", "number", "number" ]
+}
+,{
+ "name":"shePublicKeyDeserialize",
+ "exportName":"_shePublicKeyDeserialize",
+ "ret":"number",
+ "args":[ "number", "number", "number" ]
+}
+,{
+ "name":"sheCipherTextG1Deserialize",
+ "exportName":"_sheCipherTextG1Deserialize",
+ "ret":"number",
+ "args":[ "number", "number", "number" ]
+}
+,{
+ "name":"sheCipherTextG2Deserialize",
+ "exportName":"_sheCipherTextG2Deserialize",
+ "ret":"number",
+ "args":[ "number", "number", "number" ]
+}
+,{
+ "name":"sheCipherTextGTDeserialize",
+ "exportName":"_sheCipherTextGTDeserialize",
+ "ret":"number",
+ "args":[ "number", "number", "number" ]
+}
+,{
+ "name":"sheSecretKeySetByCSPRNG",
+ "exportName":"sheSecretKeySetByCSPRNG",
+ "ret":"number",
+ "args":[ "number" ]
+}
+,{
+ "name":"sheGetPublicKey",
+ "exportName":"sheGetPublicKey",
+ "ret":"null",
+ "args":[ "number", "number" ]
+}
+,{
+ "name":"sheSetRangeForDLP",
+ "exportName":"sheSetRangeForDLP",
+ "ret":"number",
+ "args":[ "number", "number" ]
+}
+,{
+ "name":"sheSetRangeForG1DLP",
+ "exportName":"sheSetRangeForG1DLP",
+ "ret":"number",
+ "args":[ "number", "number" ]
+}
+,{
+ "name":"sheSetRangeForG2DLP",
+ "exportName":"sheSetRangeForG2DLP",
+ "ret":"number",
+ "args":[ "number", "number" ]
+}
+,{
+ "name":"sheSetRangeForGTDLP",
+ "exportName":"sheSetRangeForGTDLP",
+ "ret":"number",
+ "args":[ "number", "number" ]
+}
+,{
+ "name":"sheEncG1",
+ "exportName":"sheEncG1",
+ "ret":"number",
+ "args":[ "number", "number", "number" ]
+}
+,{
+ "name":"sheEncG2",
+ "exportName":"sheEncG2",
+ "ret":"number",
+ "args":[ "number", "number", "number" ]
+}
+,{
+ "name":"sheEncGT",
+ "exportName":"sheEncGT",
+ "ret":"number",
+ "args":[ "number", "number", "number" ]
+}
+,{
+ "name":"sheEnc32G1",
+ "exportName":"sheEnc32G1",
+ "ret":"number",
+ "args":[ "number", "number", "number" ]
+}
+,{
+ "name":"sheEnc32G2",
+ "exportName":"sheEnc32G2",
+ "ret":"number",
+ "args":[ "number", "number", "number" ]
+}
+,{
+ "name":"sheEnc32GT",
+ "exportName":"sheEnc32GT",
+ "ret":"number",
+ "args":[ "number", "number", "number" ]
+}
+,{
+ "name":"sheDecG1",
+ "exportName":"_sheDecG1",
+ "ret":"number",
+ "args":[ "number", "number", "number" ]
+}
+,{
+ "name":"sheDecG2",
+ "exportName":"_sheDecG2",
+ "ret":"number",
+ "args":[ "number", "number", "number" ]
+}
+,{
+ "name":"sheDecGT",
+ "exportName":"_sheDecGT",
+ "ret":"number",
+ "args":[ "number", "number", "number" ]
+}
+,{
+ "name":"sheAddG1",
+ "exportName":"sheAddG1",
+ "ret":"number",
+ "args":[ "number", "number", "number" ]
+}
+,{
+ "name":"sheAddG2",
+ "exportName":"sheAddG2",
+ "ret":"number",
+ "args":[ "number", "number", "number" ]
+}
+,{
+ "name":"sheAddGT",
+ "exportName":"sheAddGT",
+ "ret":"number",
+ "args":[ "number", "number", "number" ]
+}
+,{
+ "name":"sheSubG1",
+ "exportName":"sheSubG1",
+ "ret":"number",
+ "args":[ "number", "number", "number" ]
+}
+,{
+ "name":"sheSubG2",
+ "exportName":"sheSubG2",
+ "ret":"number",
+ "args":[ "number", "number", "number" ]
+}
+,{
+ "name":"sheSubGT",
+ "exportName":"sheSubGT",
+ "ret":"number",
+ "args":[ "number", "number", "number" ]
+}
+,{
+ "name":"sheMulG1",
+ "exportName":"sheMulG1",
+ "ret":"number",
+ "args":[ "number", "number", "number" ]
+}
+,{
+ "name":"sheMulG2",
+ "exportName":"sheMulG2",
+ "ret":"number",
+ "args":[ "number", "number", "number" ]
+}
+,{
+ "name":"sheMulGT",
+ "exportName":"sheMulGT",
+ "ret":"number",
+ "args":[ "number", "number", "number" ]
+}
+,{
+ "name":"sheMul32G1",
+ "exportName":"sheMul32G1",
+ "ret":"number",
+ "args":[ "number", "number", "number" ]
+}
+,{
+ "name":"sheMul32G2",
+ "exportName":"sheMul32G2",
+ "ret":"number",
+ "args":[ "number", "number", "number" ]
+}
+,{
+ "name":"sheMul32GT",
+ "exportName":"sheMul32GT",
+ "ret":"number",
+ "args":[ "number", "number", "number" ]
+}
+,{
+ "name":"sheMul",
+ "exportName":"sheMul",
+ "ret":"number",
+ "args":[ "number", "number", "number" ]
+}
+,{
+ "name":"sheReRandG1",
+ "exportName":"sheReRandG1",
+ "ret":"number",
+ "args":[ "number", "number" ]
+}
+,{
+ "name":"sheReRandG2",
+ "exportName":"sheReRandG2",
+ "ret":"number",
+ "args":[ "number", "number" ]
+}
+,{
+ "name":"sheReRandGT",
+ "exportName":"sheReRandGT",
+ "ret":"number",
+ "args":[ "number", "number" ]
+}
+,{
+ "name":"sheConvertFromG1",
+ "exportName":"sheConvertFromG1",
+ "ret":"number",
+ "args":[ "number", "number", "number" ]
+}
+,{
+ "name":"sheConvertFromG2",
+ "exportName":"sheConvertFromG2",
+ "ret":"number",
+ "args":[ "number", "number", "number" ]
+}
+]
diff --git a/docs/demo/she-demo.js b/docs/demo/she-demo.js
index f6edfbb..ab49c05 100644
--- a/docs/demo/she-demo.js
+++ b/docs/demo/she-demo.js
@@ -9,22 +9,6 @@ function setText(name, val) { document.getElementsByName(name)[0].innerText = va
she.init(range, tryNum, function() { setText('status', 'ok')})
}())
-function putSecretKey(x, msg = "") {
- console.log(msg + ' sk=' + Uint8ArrayToHexString(sheSecretKeySerialize(x)))
-}
-function putPublicKey(x, msg = "") {
- console.log(msg + ' pk=' + Uint8ArrayToHexString(shePublicKeySerialize(x)))
-}
-function putCipherTextG1(x, msg = "") {
- console.log(msg + ' ctG1=' + Uint8ArrayToHexString(sheCipherTextG1Serialize(x)))
-}
-function putCipherTextG2(x, msg = "") {
- console.log(msg + ' ctG2=' + Uint8ArrayToHexString(sheCipherTextG2Serialize(x)))
-}
-function putCipherTextGT(x, msg = "") {
- console.log(msg + ' ctGT=' + Uint8ArrayToHexString(sheCipherTextGTSerialize(x)))
-}
-
function bench(label, count, func) {
let start = Date.now()
for (let i = 0; i < count; i++) {
@@ -44,50 +28,34 @@ function onClickBenchmark() {
function onClickTestSHE() {
try {
- let sec = sheSecretKey_malloc()
- let pub = shePublicKey_malloc()
- let c11 = sheCipherTextG1_malloc()
- let c12 = sheCipherTextG1_malloc()
- let c21 = sheCipherTextG2_malloc()
- let c22 = sheCipherTextG2_malloc()
- let ct = sheCipherTextGT_malloc()
-
- sheSecretKeySetByCSPRNG(sec)
- setText('secretKey', Uint8ArrayToHexString(sheSecretKeySerialize(sec)))
- sheGetPublicKey(pub, sec)
- setText('publicKey', Uint8ArrayToHexString(shePublicKeySerialize(pub)))
- putPublicKey(pub)
+ let sec = new she.SecretKey()
+ sec.setByCSPRNG()
+ setText('secretKey', sec.toHexStr())
+ let pub = sec.getPublicKey()
+ setText('publicKey', pub.toHexStr())
let m1 = getValue('msg1')
let m2 = getValue('msg2')
let m3 = getValue('msg3')
let m4 = getValue('msg4')
- sheEnc32G1(c11, pub, m1)
- console.log('dec c11=' + sheDecG1(sec, c11))
- sheEnc32G1(c12, pub, m2)
- console.log('dec c12=' + sheDecG1(sec, c12))
- sheEnc32G2(c21, pub, m3)
- console.log('dec c21=' + sheDecG2(sec, c21))
- sheEnc32G2(c22, pub, m4)
- console.log('dec c22=' + sheDecG2(sec, c22))
- setText('encG11', Uint8ArrayToHexString(sheCipherTextG1Serialize(c11)))
- setText('encG12', Uint8ArrayToHexString(sheCipherTextG1Serialize(c12)))
- setText('encG21', Uint8ArrayToHexString(sheCipherTextG2Serialize(c21)))
- setText('encG22', Uint8ArrayToHexString(sheCipherTextG2Serialize(c22)))
- sheAddG1(c11, c11, c12)
- sheAddG2(c21, c21, c22)
- sheMul(ct, c11, c21)
- setText('encGT', Uint8ArrayToHexString(sheCipherTextGTSerialize(ct)))
- let d = sheDecGT(sec, ct)
+ let c11 = pub.encG1(m1)
+ console.log('dec c11=' + sec.dec(c11))
+ let c12 = pub.encG1(m2)
+ console.log('dec c12=' + sec.dec(c12))
+ let c21 = pub.encG2(m3)
+ console.log('dec c21=' + sec.dec(c21))
+ let c22 = pub.encG2(m4)
+ console.log('dec c22=' + sec.dec(c22))
+ setText('encG11', c11.toHexStr())
+ setText('encG12', c12.toHexStr())
+ setText('encG21', c21.toHexStr())
+ setText('encG22', c22.toHexStr())
+ c11 = she.add(c11, c12)
+ c21 = she.add(c21, c22)
+ let ct = she.mul(c11, c21)
+ setText('encGT', ct.toHexStr())
+ let d = sec.dec(ct)
setText('decMsg', d)
-
- she_free(ct)
- she_free(c22)
- she_free(c21)
- she_free(c12)
- she_free(c11)
- she_free(pub)
- she_free(sec)
} catch (e) {
console.log('exception ' + e)
}
diff --git a/docs/demo/she-ja.html b/docs/demo/she-ja.html
index a0e3c74..9d7acc5 100644
--- a/docs/demo/she-ja.html
+++ b/docs/demo/she-ja.html
@@ -7,7 +7,6 @@
<title>Two-level Homomorphic Encryption demo</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script type='text/javascript' src="jquery-3.2.1.min.js"></script>
- <script type='text/javascript' src="exported-she.js"></script>
<script type='text/javascript' src="mclshe.js"></script>
<script type='text/javascript' src="she.js"></script>
<script type="text/javascript" src="she-demo2.js"></script>
diff --git a/docs/demo/she.html b/docs/demo/she.html
index e0993c9..8afbf4c 100644
--- a/docs/demo/she.html
+++ b/docs/demo/she.html
@@ -4,7 +4,6 @@
<meta charset="UTF-8">
<title>SHE with 1multi sample</title>
<link rel="stylesheet" href="style.css" type="text/css">
-<script type='text/javascript' src="./exported-she.js"></script>
<script type='text/javascript' src="./mclshe.js"></script>
<script type='text/javascript' src="./she.js"></script>
<script type='text/javascript' src="./she-demo.js"></script>
diff --git a/docs/demo/she.js b/docs/demo/she.js
index 392ac80..e6e9d28 100644
--- a/docs/demo/she.js
+++ b/docs/demo/she.js
@@ -8,7 +8,11 @@
const crypto = window.crypto || window.msCrypto
const MCLBN_CURVE_FP254BNB = 0
+ const MCLBN_CURVE_FP382_1 = 1
+ const MCLBN_CURVE_FP382_2 = 2
+
const MCLBN_FP_UNIT_SIZE = 4
+
const MCLBN_FP_SIZE = MCLBN_FP_UNIT_SIZE * 8
const MCLBN_G1_SIZE = MCLBN_FP_SIZE * 3
const MCLBN_G2_SIZE = MCLBN_FP_SIZE * 6
@@ -21,8 +25,10 @@
const SHE_CIPHERTEXT_GT_SIZE = MCLBN_GT_SIZE * 4
let mod = {}
- let she = {}
- she.mod = mod
+ let capi = {}
+ let self = {}
+ self.mod = mod
+ self.capi = capi
const setupWasm = function(fileName, nameSpace, setupFct) {
console.log('setupWasm ' + fileName)
@@ -39,541 +45,550 @@
})
return mod
}
-
- const define_she_extra_functions = function(mod) {
- const ptrToStr = function(pos, n) {
- let s = ''
- for (let i = 0; i < n; i++) {
- s += String.fromCharCode(mod.HEAP8[pos + i])
- }
- return s
- }
- const Uint8ArrayToMem = function(pos, buf) {
- for (let i = 0; i < buf.length; i++) {
- mod.HEAP8[pos + i] = buf[i]
- }
- }
- const AsciiStrToMem = function(pos, s) {
- for (let i = 0; i < s.length; i++) {
- mod.HEAP8[pos + i] = s.charCodeAt(i)
- }
- }
- const wrap_outputString = function(func, doesReturnString = true) {
- return function(x, ioMode = 0) {
- let maxBufSize = 2048
- let stack = mod.Runtime.stackSave()
- let pos = mod.Runtime.stackAlloc(maxBufSize)
- let n = func(pos, maxBufSize, x, ioMode)
- if (n < 0) {
- throw('err gen_str:' + x)
- }
- if (doesReturnString) {
- let s = ptrToStr(pos, n)
- mod.Runtime.stackRestore(stack)
- return s
- } else {
- let a = new Uint8Array(n)
- for (let i = 0; i < n; i++) {
- a[i] = mod.HEAP8[pos + i]
- }
- mod.Runtime.stackRestore(stack)
- return a
- }
- }
- }
- const wrap_outputArray = function(func) {
- return wrap_outputString(func, false)
- }
- const wrap_input0 = function(func, returnValue = false) {
- return function(buf, ioMode = 0) {
- let stack = mod.Runtime.stackSave()
- let pos = mod.Runtime.stackAlloc(buf.length)
- if (typeof(buf) == "string") {
- AsciiStrToMem(pos, buf)
- } else {
- Uint8ArrayToMem(pos, buf)
- }
- let r = func(pos, buf.length, ioMode)
- mod.Runtime.stackRestore(stack)
- if (returnValue) return r
- if (r) throw('err wrap_input0 ' + buf)
- }
- }
- const wrap_input1 = function(func, returnValue = false) {
- return function(x1, buf, ioMode = 0) {
- let stack = mod.Runtime.stackSave()
- let pos = mod.Runtime.stackAlloc(buf.length)
- if (typeof(buf) == "string") {
- AsciiStrToMem(pos, buf)
- } else {
- Uint8ArrayToMem(pos, buf)
- }
- let r = func(x1, pos, buf.length, ioMode)
- mod.Runtime.stackRestore(stack)
- if (returnValue) return r
- if (r) throw('err wrap_input1 ' + buf)
- }
- }
- const wrap_input2 = function(func, returnValue = false) {
- return function(x1, x2, buf, ioMode = 0) {
- let stack = mod.Runtime.stackSave()
- let pos = mod.Runtime.stackAlloc(buf.length)
- if (typeof(buf) == "string") {
- AsciiStrToMem(pos, buf)
- } else {
- Uint8ArrayToMem(pos, buf)
- }
- let r = func(x1, x2, pos, buf.length, ioMode)
- mod.Runtime.stackRestore(stack)
- if (returnValue) return r
- if (r) throw('err wrap_input2 ' + buf)
- }
- }
- const wrap_dec = function(func) {
- return function(sec, c) {
- let stack = mod.Runtime.stackSave()
- let pos = mod.Runtime.stackAlloc(8)
- let r = func(pos, sec, c)
- mod.Runtime.stackRestore(stack)
- if (r != 0) throw('sheDec')
- let v = mod.HEAP32[pos / 4]
- return v
- }
+ const ptrToStr = function(pos, n) {
+ let s = ''
+ for (let i = 0; i < n; i++) {
+ s += String.fromCharCode(mod.HEAP8[pos + i])
}
- const copyToUint32Array = function(a, pos) {
- for (let i = 0; i < a.length; i++) {
- a[i] = mod.HEAP32[pos / 4 + i]
- }
+ return s
+ }
+ const Uint8ArrayToMem = function(pos, buf) {
+ for (let i = 0; i < buf.length; i++) {
+ mod.HEAP8[pos + i] = buf[i]
}
- const copyFromUint32Array = function(pos, a) {
- for (let i = 0; i < a.length; i++) {
- mod.HEAP32[pos / 4 + i] = a[i]
- }
+ }
+ const AsciiStrToMem = function(pos, s) {
+ for (let i = 0; i < s.length; i++) {
+ mod.HEAP8[pos + i] = s.charCodeAt(i)
}
- const callSetter = function(func, a, p1, p2) {
- let pos = mod._malloc(a.length * 4)
- func(pos, p1, p2) // p1, p2 may be undefined
- copyToUint32Array(a, pos)
- mod._free(pos)
+ }
+ const copyToUint32Array = function(a, pos) {
+ for (let i = 0; i < a.length; i++) {
+ a[i] = mod.HEAP32[pos / 4 + i]
}
- const callGetter = function(func, a, p1, p2) {
- let pos = mod._malloc(a.length * 4)
- mod.HEAP32.set(a, pos / 4)
- let s = func(pos, p1, p2)
- mod._free(pos)
- return s
+ }
+ const copyFromUint32Array = function(pos, a) {
+ for (let i = 0; i < a.length; i++) {
+ mod.HEAP32[pos / 4 + i] = a[i]
}
- const callModifier = function(func, a, p1, p2) {
- let pos = mod._malloc(a.length * 4)
- mod.HEAP32.set(a, pos / 4)
- func(pos, p1, p2) // p1, p2 may be undefined
- copyToUint32Array(a, pos)
- mod._free(pos)
+ }
+ self.toHex = function(a, start, n) {
+ let s = ''
+ for (let i = 0; i < n; i++) {
+ s += ('0' + a[start + i].toString(16)).slice(-2)
}
- she.toHex = function(a, start, n) {
- let s = ''
- for (let i = 0; i < n; i++) {
- s += ('0' + a[start + i].toString(16)).slice(-2)
- }
- return s
+ return s
+ }
+ // Uint8Array to hex string
+ self.toHexStr = function(a) {
+ return self.toHex(a, 0, a.length)
+ }
+ // hex string to Uint8Array
+ self.fromHexStr = function(s) {
+ let n = (s.length + 1) / 2
+ let a = new Uint8Array(n)
+ for (let i = 0; i < s.length / 2; i++) {
+ a[i] = parseInt(s.slice(i * 2, i * 2 + 2), 16)
}
- // Uint8Array to hex string
- she.toHexStr = function(a) {
- return she.toHex(a, 0, a.length)
+ if ((s.length & 1) != 0) {
+ a[n - 1] = parseInt(s[s.length - 1] + '0', 16)
}
- // hex string to Uint8Array
- she.fromHexStr = function(s) {
- let n = (s.length + 1) / 2
- let a = new Uint8Array(n)
- for (let i = 0; i < s.length / 2; i++) {
- a[i] = parseInt(s.slice(i * 2, i * 2 + 2), 16)
+ return a
+ }
+
+ const wrap_outputString = function(func, doesReturnString = true) {
+ return function(x, ioMode = 0) {
+ let maxBufSize = 2048
+ let stack = mod.Runtime.stackSave()
+ let pos = mod.Runtime.stackAlloc(maxBufSize)
+ let n = func(pos, maxBufSize, x, ioMode)
+ if (n < 0) {
+ throw('err gen_str:' + x)
}
- if ((s.length & 1) != 0) {
- a[n - 1] = parseInt(s[s.length - 1] + '0', 16)
+ if (doesReturnString) {
+ let s = ptrToStr(pos, n)
+ mod.Runtime.stackRestore(stack)
+ return s
+ } else {
+ let a = new Uint8Array(n)
+ for (let i = 0; i < n; i++) {
+ a[i] = mod.HEAP8[pos + i]
+ }
+ mod.Runtime.stackRestore(stack)
+ return a
}
- return a
- }
- const callEnc = function(func, cstr, pub, m) {
- let c = new cstr()
- let stack = mod.Runtime.stackSave()
- let cPos = mod.Runtime.stackAlloc(c.a_.length * 4)
- let pubPos = mod.Runtime.stackAlloc(pub.length * 4)
- copyFromUint32Array(pubPos, pub);
- func(cPos, pubPos, m)
- copyToUint32Array(c.a_, cPos)
- mod.Runtime.stackRestore(stack)
- return c
}
- // return func(x, y)
- const callAddSub = function(func, cstr, x, y) {
- let z = new cstr()
+ }
+ const wrap_outputArray = function(func) {
+ return wrap_outputString(func, false)
+ }
+ /*
+ argNum : n
+ func(x0, ..., x_(n-1), buf, ioMode)
+ => func(x0, ..., x_(n-1), pos, buf.length, ioMode)
+ */
+ const wrap_input = function(func, argNum, returnValue = false) {
+ return function() {
+ const args = [...arguments]
+ let buf = args[argNum]
+ let ioMode = args[argNum + 1] // may undefined
let stack = mod.Runtime.stackSave()
- let xPos = mod.Runtime.stackAlloc(x.length * 4)
- let yPos = mod.Runtime.stackAlloc(y.length * 4)
- let zPos = mod.Runtime.stackAlloc(z.a_.length * 4)
- copyFromUint32Array(xPos, x);
- copyFromUint32Array(yPos, y);
- func(zPos, xPos, yPos)
- copyToUint32Array(z.a_, zPos)
+ let pos = mod.Runtime.stackAlloc(buf.length)
+ if (typeof(buf) == "string") {
+ AsciiStrToMem(pos, buf)
+ } else {
+ Uint8ArrayToMem(pos, buf)
+ }
+ let r = func(...args.slice(0, argNum), pos, buf.length, ioMode)
mod.Runtime.stackRestore(stack)
- return z
+ if (returnValue) return r
+ if (r) throw('err wrap_input0 ' + buf)
}
- // return func(x, y)
- const callMulInt = function(func, cstr, x, y) {
- let z = new cstr()
+ }
+ const wrap_input0 = function(func, returnValue = false) {
+ return function(buf, ioMode = 0) {
let stack = mod.Runtime.stackSave()
- let xPos = mod.Runtime.stackAlloc(x.length * 4)
- let zPos = mod.Runtime.stackAlloc(z.a_.length * 4)
- copyFromUint32Array(xPos, x);
- func(zPos, xPos, y)
- copyToUint32Array(z.a_, zPos)
+ let pos = mod.Runtime.stackAlloc(buf.length)
+ if (typeof(buf) == "string") {
+ AsciiStrToMem(pos, buf)
+ } else {
+ Uint8ArrayToMem(pos, buf)
+ }
+ let r = func(pos, buf.length, ioMode)
mod.Runtime.stackRestore(stack)
- return z
+ if (returnValue) return r
+ if (r) throw('err wrap_input0 ' + buf)
}
- // return func(c)
- const callDec = function(func, sec, c) {
+ }
+ const wrap_input1 = function(func, returnValue = false) {
+ return function(x1, buf, ioMode = 0) {
let stack = mod.Runtime.stackSave()
- let secPos = mod.Runtime.stackAlloc(sec.length * 4)
- let cPos = mod.Runtime.stackAlloc(c.length * 4)
- copyFromUint32Array(secPos, sec);
- copyFromUint32Array(cPos, c);
- let r = func(secPos, cPos)
+ let pos = mod.Runtime.stackAlloc(buf.length)
+ if (typeof(buf) == "string") {
+ AsciiStrToMem(pos, buf)
+ } else {
+ Uint8ArrayToMem(pos, buf)
+ }
+ let r = func(x1, pos, buf.length, ioMode)
mod.Runtime.stackRestore(stack)
- return r
+ if (returnValue) return r
+ if (r) throw('err wrap_input1 ' + buf)
}
- // reRand(c)
- const callReRand = function(func, c, pub) {
+ }
+ const wrap_input2 = function(func, returnValue = false) {
+ return function(x1, x2, buf, ioMode = 0) {
let stack = mod.Runtime.stackSave()
- let cPos = mod.Runtime.stackAlloc(c.length * 4)
- let pubPos = mod.Runtime.stackAlloc(pub.length * 4)
- copyFromUint32Array(cPos, c);
- copyFromUint32Array(pubPos, pub);
- let r = func(cPos, pubPos)
- copyToUint32Array(c, cPos)
+ let pos = mod.Runtime.stackAlloc(buf.length)
+ if (typeof(buf) == "string") {
+ AsciiStrToMem(pos, buf)
+ } else {
+ Uint8ArrayToMem(pos, buf)
+ }
+ let r = func(x1, x2, pos, buf.length, ioMode)
mod.Runtime.stackRestore(stack)
- if (r) throw('callReRand err')
+ if (returnValue) return r
+ if (r) throw('err wrap_input2 ' + buf)
}
- // convertFrom
- const callConvertFrom = function(func, pub, c) {
- let ct = new she.CipherTextGT()
+ }
+ const callSetter = function(func, a, p1, p2) {
+ let pos = mod._malloc(a.length * 4)
+ func(pos, p1, p2) // p1, p2 may be undefined
+ copyToUint32Array(a, pos)
+ mod._free(pos)
+ }
+ const callGetter = function(func, a, p1, p2) {
+ let pos = mod._malloc(a.length * 4)
+ mod.HEAP32.set(a, pos / 4)
+ let s = func(pos, p1, p2)
+ mod._free(pos)
+ return s
+ }
+ const callModifier = function(func, a, p1, p2) {
+ let pos = mod._malloc(a.length * 4)
+ mod.HEAP32.set(a, pos / 4)
+ func(pos, p1, p2) // p1, p2 may be undefined
+ copyToUint32Array(a, pos)
+ mod._free(pos)
+ }
+ const wrap_dec = function(func) {
+ return function(sec, c) {
let stack = mod.Runtime.stackSave()
- let ctPos = mod.Runtime.stackAlloc(ct.a_.length * 4)
- let pubPos = mod.Runtime.stackAlloc(pub.length * 4)
- let cPos = mod.Runtime.stackAlloc(c.length * 4)
- copyFromUint32Array(pubPos, pub);
- copyFromUint32Array(cPos, c);
- let r = func(ctPos, pubPos, cPos)
- copyToUint32Array(ct.a_, ctPos)
+ let pos = mod.Runtime.stackAlloc(8)
+ let r = func(pos, sec, c)
mod.Runtime.stackRestore(stack)
- if (r) throw('callConvertFrom err')
- return ct
+ if (r != 0) throw('sheDec')
+ let v = mod.HEAP32[pos / 4]
+ return v
}
- she_free = function(p) {
+ }
+ const callEnc = function(func, cstr, pub, m) {
+ let c = new cstr()
+ let stack = mod.Runtime.stackSave()
+ let cPos = mod.Runtime.stackAlloc(c.a_.length * 4)
+ let pubPos = mod.Runtime.stackAlloc(pub.length * 4)
+ copyFromUint32Array(pubPos, pub);
+ func(cPos, pubPos, m)
+ copyToUint32Array(c.a_, cPos)
+ mod.Runtime.stackRestore(stack)
+ return c
+ }
+ // return func(x, y)
+ const callAddSub = function(func, cstr, x, y) {
+ let z = new cstr()
+ let stack = mod.Runtime.stackSave()
+ let xPos = mod.Runtime.stackAlloc(x.length * 4)
+ let yPos = mod.Runtime.stackAlloc(y.length * 4)
+ let zPos = mod.Runtime.stackAlloc(z.a_.length * 4)
+ copyFromUint32Array(xPos, x);
+ copyFromUint32Array(yPos, y);
+ func(zPos, xPos, yPos)
+ copyToUint32Array(z.a_, zPos)
+ mod.Runtime.stackRestore(stack)
+ return z
+ }
+ // return func(x, y)
+ const callMulInt = function(func, cstr, x, y) {
+ let z = new cstr()
+ let stack = mod.Runtime.stackSave()
+ let xPos = mod.Runtime.stackAlloc(x.length * 4)
+ let zPos = mod.Runtime.stackAlloc(z.a_.length * 4)
+ copyFromUint32Array(xPos, x);
+ func(zPos, xPos, y)
+ copyToUint32Array(z.a_, zPos)
+ mod.Runtime.stackRestore(stack)
+ return z
+ }
+ // return func(c)
+ const callDec = function(func, sec, c) {
+ let stack = mod.Runtime.stackSave()
+ let secPos = mod.Runtime.stackAlloc(sec.length * 4)
+ let cPos = mod.Runtime.stackAlloc(c.length * 4)
+ copyFromUint32Array(secPos, sec);
+ copyFromUint32Array(cPos, c);
+ let r = func(secPos, cPos)
+ mod.Runtime.stackRestore(stack)
+ return r
+ }
+ // reRand(c)
+ const callReRand = function(func, c, pub) {
+ let stack = mod.Runtime.stackSave()
+ let cPos = mod.Runtime.stackAlloc(c.length * 4)
+ let pubPos = mod.Runtime.stackAlloc(pub.length * 4)
+ copyFromUint32Array(cPos, c);
+ copyFromUint32Array(pubPos, pub);
+ let r = func(cPos, pubPos)
+ copyToUint32Array(c, cPos)
+ mod.Runtime.stackRestore(stack)
+ if (r) throw('callReRand err')
+ }
+ // convertFrom
+ const callConvertFrom = function(func, pub, c) {
+ let ct = new self.CipherTextGT()
+ let stack = mod.Runtime.stackSave()
+ let ctPos = mod.Runtime.stackAlloc(ct.a_.length * 4)
+ let pubPos = mod.Runtime.stackAlloc(pub.length * 4)
+ let cPos = mod.Runtime.stackAlloc(c.length * 4)
+ copyFromUint32Array(pubPos, pub);
+ copyFromUint32Array(cPos, c);
+ let r = func(ctPos, pubPos, cPos)
+ copyToUint32Array(ct.a_, ctPos)
+ mod.Runtime.stackRestore(stack)
+ if (r) throw('callConvertFrom err')
+ return ct
+ }
+ const define_extra_functions = function(mod) {
+ capi.she_free = function(p) {
mod._free(p)
}
- sheSecretKey_malloc = function() {
+ capi.sheSecretKey_malloc = function() {
return mod._malloc(SHE_SECRETKEY_SIZE)
}
- shePublicKey_malloc = function() {
+ capi.shePublicKey_malloc = function() {
return mod._malloc(SHE_PUBLICKEY_SIZE)
}
- sheCipherTextG2_malloc = function() {
+ capi.sheCipherTextG2_malloc = function() {
return mod._malloc(SHE_CIPHERTEXT_G2_SIZE)
}
- sheCipherTextGT_malloc = function() {
+ capi.sheCipherTextGT_malloc = function() {
return mod._malloc(SHE_CIPHERTEXT_GT_SIZE)
}
- sheCipherTextG1_malloc = function() {
+ capi.sheCipherTextG1_malloc = function() {
return mod._malloc(SHE_CIPHERTEXT_G1_SIZE)
}
- sheSecretKeySerialize = wrap_outputArray(_sheSecretKeySerialize)
- sheSecretKeyDeserialize = wrap_input1(_sheSecretKeyDeserialize)
- shePublicKeySerialize = wrap_outputArray(_shePublicKeySerialize)
- shePublicKeyDeserialize = wrap_input1(_shePublicKeyDeserialize)
- sheCipherTextG1Serialize = wrap_outputArray(_sheCipherTextG1Serialize)
- sheCipherTextG1Deserialize = wrap_input1(_sheCipherTextG1Deserialize)
- sheDecG1 = wrap_dec(_sheDecG1)
- sheCipherTextG2Serialize = wrap_outputArray(_sheCipherTextG2Serialize)
- sheCipherTextG2Deserialize = wrap_input1(_sheCipherTextG2Deserialize)
- sheDecG2 = wrap_dec(_sheDecG2)
- sheCipherTextGTSerialize = wrap_outputArray(_sheCipherTextGTSerialize)
- sheCipherTextGTDeserialize = wrap_input1(_sheCipherTextGTDeserialize)
- sheDecGT = wrap_dec(_sheDecGT)
+ capi.sheSecretKeySerialize = wrap_outputArray(capi._sheSecretKeySerialize)
+ capi.sheSecretKeyDeserialize = wrap_input(capi._sheSecretKeyDeserialize, 1)
+ capi.shePublicKeySerialize = wrap_outputArray(capi._shePublicKeySerialize)
+ capi.shePublicKeyDeserialize = wrap_input(capi._shePublicKeyDeserialize, 1)
+ capi.sheCipherTextG1Serialize = wrap_outputArray(capi._sheCipherTextG1Serialize)
+ capi.sheCipherTextG1Deserialize = wrap_input(capi._sheCipherTextG1Deserialize, 1)
+ capi.sheDecG1 = wrap_dec(capi._sheDecG1)
+ capi.sheCipherTextG2Serialize = wrap_outputArray(capi._sheCipherTextG2Serialize)
+ capi.sheCipherTextG2Deserialize = wrap_input(capi._sheCipherTextG2Deserialize, 1)
+ capi.sheDecG2 = wrap_dec(capi._sheDecG2)
+ capi.sheCipherTextGTSerialize = wrap_outputArray(capi._sheCipherTextGTSerialize)
+ capi.sheCipherTextGTDeserialize = wrap_input(capi._sheCipherTextGTDeserialize, 1)
+ capi.sheDecGT = wrap_dec(capi._sheDecGT)
- sheInit = function(curveType = MCLBN_CURVE_FP254BNB) {
- let r = _sheInit(curveType, MCLBN_FP_UNIT_SIZE)
+ capi.sheInit = function(curveType = MCLBN_CURVE_FP254BNB) {
+ let r = capi._sheInit(curveType, MCLBN_FP_UNIT_SIZE)
console.log('sheInit ' + r)
if (r) throw('sheInit')
}
- she.SecretKey = function() {
- this.a_ = new Uint32Array(SHE_SECRETKEY_SIZE / 4)
- }
- she.SecretKey.prototype.serialize = function() {
- return callGetter(sheSecretKeySerialize, this.a_)
- }
- she.SecretKey.prototype.deserialize = function(s) {
- callSetter(sheSecretKeyDeserialize, this.a_, s)
- }
- she.SecretKey.prototype.fromHexStr = function(s) {
- this.deserialize(she.fromHexStr(s))
- }
- she.SecretKey.prototype.toHexStr = function() {
- return she.toHexStr(this.serialize())
+ class Common {
+ constructor(size) {
+ this.a_ = new Uint32Array(size / 4)
+ }
+ fromHexStr(s) {
+ this.deserialize(self.fromHexStr(s))
+ }
+ toHexStr() {
+ return self.toHexStr(this.serialize())
+ }
+ dump(msg = '') {
+ console.log(msg + this.toHexStr())
+ }
}
- she.SecretKey.prototype.dump = function(msg = 'sec ') {
- console.log(msg + this.toHexStr())
+ self.SecretKey = class extends Common {
+ constructor() {
+ super(SHE_SECRETKEY_SIZE)
+ }
+ serialize() {
+ return callGetter(capi.sheSecretKeySerialize, this.a_)
+ }
+ deserialize(s) {
+ callSetter(capi.sheSecretKeyDeserialize, this.a_, s)
+ }
+ setByCSPRNG() {
+ let stack = mod.Runtime.stackSave()
+ let secPos = mod.Runtime.stackAlloc(this.a_.length * 4)
+ capi.sheSecretKeySetByCSPRNG(secPos)
+ copyToUint32Array(this.a_, secPos)
+ mod.Runtime.stackRestore(stack)
+ }
+ getPublicKey() {
+ let pub = new self.PublicKey()
+ let stack = mod.Runtime.stackSave()
+ let secPos = mod.Runtime.stackAlloc(this.a_.length * 4)
+ let pubPos = mod.Runtime.stackAlloc(pub.a_.length * 4)
+ copyFromUint32Array(secPos, this.a_)
+ capi.sheGetPublicKey(pubPos, secPos)
+ copyToUint32Array(pub.a_, pubPos)
+ mod.Runtime.stackRestore(stack)
+ return pub
+ }
+ dec(c) {
+ let dec = null
+ if (self.CipherTextG1.prototype.isPrototypeOf(c)) {
+ dec = capi.sheDecG1
+ } else if (self.CipherTextG2.prototype.isPrototypeOf(c)) {
+ dec = capi.sheDecG2
+ } else if (self.CipherTextGT.prototype.isPrototypeOf(c)) {
+ dec = capi.sheDecGT
+ } else {
+ throw('self.SecretKey.dec:not supported')
+ }
+ return callDec(dec, this.a_, c.a_)
+ }
}
- she.getSecretKeyFromHexStr = function(s) {
- r = new she.SecretKey()
+
+ self.getSecretKeyFromHexStr = function(s) {
+ r = new self.SecretKey()
r.fromHexStr(s)
return r
}
- she.PublicKey = function() {
- this.a_ = new Uint32Array(SHE_PUBLICKEY_SIZE / 4)
- }
- she.PublicKey.prototype.serialize = function() {
- return callGetter(shePublicKeySerialize, this.a_)
- }
- she.PublicKey.prototype.deserialize = function(s) {
- callSetter(shePublicKeyDeserialize, this.a_, s)
- }
- she.PublicKey.prototype.fromHexStr = function(s) {
- this.deserialize(she.fromHexStr(s))
- }
- she.PublicKey.prototype.toHexStr = function() {
- return she.toHexStr(this.serialize())
- }
- she.PublicKey.prototype.dump = function(msg = 'pub ') {
- console.log(msg + this.toHexStr())
+ self.PublicKey = class extends Common {
+ constructor() {
+ super(SHE_PUBLICKEY_SIZE)
+ }
+ serialize() {
+ return callGetter(capi.shePublicKeySerialize, this.a_)
+ }
+ deserialize(s) {
+ callSetter(capi.shePublicKeyDeserialize, this.a_, s)
+ }
+ encG1(m) {
+ return callEnc(capi.sheEnc32G1, self.CipherTextG1, this.a_, m)
+ }
+ encG2(m) {
+ return callEnc(capi.sheEnc32G2, self.CipherTextG2, this.a_, m)
+ }
+ encGT(m) {
+ return callEnc(capi.sheEnc32GT, self.CipherTextGT, this.a_, m)
+ }
+ reRand(c) {
+ let reRand = null
+ if (self.CipherTextG1.prototype.isPrototypeOf(c)) {
+ reRand = capi.sheReRandG1
+ } else if (self.CipherTextG2.prototype.isPrototypeOf(c)) {
+ reRand = capi.sheReRandG2
+ } else if (self.CipherTextGT.prototype.isPrototypeOf(c)) {
+ reRand = capi.sheReRandGT
+ } else {
+ throw('self.PublicKey.reRand:not supported')
+ }
+ return callReRand(reRand, c.a_, this.a_)
+ }
+ convertToCipherTextGT(c) {
+ let convertFrom = null
+ if (self.CipherTextG1.prototype.isPrototypeOf(c)) {
+ convertFrom = capi.sheConvertFromG1
+ } else if (self.CipherTextG2.prototype.isPrototypeOf(c)) {
+ convertFrom = capi.sheConvertFromG2
+ } else {
+ throw('self.PublicKey.convertToCipherTextGT:not supported')
+ }
+ return callConvertFrom(convertFrom, this.a_, c.a_)
+ }
}
- she.getPublicKeyFromHexStr = function(s) {
- r = new she.PublicKey()
+
+ self.getPublicKeyFromHexStr = function(s) {
+ r = new self.PublicKey()
r.fromHexStr(s)
return r
}
- she.CipherTextG1 = function() {
- this.a_ = new Uint32Array(SHE_CIPHERTEXT_G1_SIZE / 4)
- }
- she.CipherTextG1.prototype.serialize = function() {
- return callGetter(sheCipherTextG1Serialize, this.a_)
- }
- she.CipherTextG1.prototype.deserialize = function(s) {
- callSetter(sheCipherTextG1Deserialize, this.a_, s)
- }
- she.CipherTextG1.prototype.fromHexStr = function(s) {
- this.deserialize(she.fromHexStr(s))
- }
- she.CipherTextG1.prototype.toHexStr = function() {
- return she.toHexStr(this.serialize())
- }
- she.CipherTextG1.prototype.dump = function(msg = 'ct1 ') {
- console.log(msg + this.toHexStr())
+ self.CipherTextG1 = class extends Common {
+ constructor() {
+ super(SHE_CIPHERTEXT_G1_SIZE)
+ }
+ serialize() {
+ return callGetter(capi.sheCipherTextG1Serialize, this.a_)
+ }
+ deserialize(s) {
+ callSetter(capi.sheCipherTextG1Deserialize, this.a_, s)
+ }
}
- she.getCipherTextG1FromHexStr = function(s) {
- r = new she.CipherTextG1()
+
+ self.getCipherTextG1FromHexStr = function(s) {
+ r = new self.CipherTextG1()
r.fromHexStr(s)
return r
}
- she.CipherTextG2 = function() {
- this.a_ = new Uint32Array(SHE_CIPHERTEXT_G2_SIZE / 4)
- }
- she.CipherTextG2.prototype.serialize = function() {
- return callGetter(sheCipherTextG2Serialize, this.a_)
- }
- she.CipherTextG2.prototype.deserialize = function(s) {
- callSetter(sheCipherTextG2Deserialize, this.a_, s)
- }
- she.CipherTextG2.prototype.fromHexStr = function(s) {
- this.deserialize(she.fromHexStr(s))
- }
- she.CipherTextG2.prototype.toHexStr = function() {
- return she.toHexStr(this.serialize())
- }
- she.CipherTextG2.prototype.dump = function(msg = 'ct2 ') {
- console.log(msg + this.toHexStr())
+ self.CipherTextG2 = class extends Common {
+ constructor() {
+ super(SHE_CIPHERTEXT_G2_SIZE)
+ }
+ serialize() {
+ return callGetter(capi.sheCipherTextG2Serialize, this.a_)
+ }
+ deserialize(s) {
+ callSetter(capi.sheCipherTextG2Deserialize, this.a_, s)
+ }
}
- she.getCipherTextG2FromHexStr = function(s) {
- r = new she.CipherTextG2()
+
+ self.getCipherTextG2FromHexStr = function(s) {
+ r = new self.CipherTextG2()
r.fromHexStr(s)
return r
}
- she.CipherTextGT = function() {
- this.a_ = new Uint32Array(SHE_CIPHERTEXT_GT_SIZE / 4)
- }
- she.CipherTextGT.prototype.serialize = function() {
- return callGetter(sheCipherTextGTSerialize, this.a_)
- }
- she.CipherTextGT.prototype.deserialize = function(s) {
- callSetter(sheCipherTextGTDeserialize, this.a_, s)
- }
- she.CipherTextGT.prototype.fromHexStr = function(s) {
- this.deserialize(she.fromHexStr(s))
- }
- she.CipherTextGT.prototype.toHexStr = function() {
- return she.toHexStr(this.serialize())
- }
- she.CipherTextGT.prototype.dump = function(msg = 'ctt ') {
- console.log(msg + this.toHexStr())
+
+ self.CipherTextGT = class extends Common {
+ constructor() {
+ super(SHE_CIPHERTEXT_GT_SIZE)
+ }
+ serialize() {
+ return callGetter(capi.sheCipherTextGTSerialize, this.a_)
+ }
+ deserialize(s) {
+ callSetter(capi.sheCipherTextGTDeserialize, this.a_, s)
+ }
}
- she.getCipherTextGTFromHexStr = function(s) {
- r = new she.CipherTextGT()
+
+ self.getCipherTextGTFromHexStr = function(s) {
+ r = new self.CipherTextGT()
r.fromHexStr(s)
return r
}
- she.SecretKey.prototype.setByCSPRNG = function() {
- let stack = mod.Runtime.stackSave()
- let secPos = mod.Runtime.stackAlloc(this.a_.length * 4)
- sheSecretKeySetByCSPRNG(secPos)
- copyToUint32Array(this.a_, secPos)
- mod.Runtime.stackRestore(stack)
- }
- she.SecretKey.prototype.getPublicKey = function() {
- let pub = new she.PublicKey()
- let stack = mod.Runtime.stackSave()
- let secPos = mod.Runtime.stackAlloc(this.a_.length * 4)
- let pubPos = mod.Runtime.stackAlloc(pub.a_.length * 4)
- copyFromUint32Array(secPos, this.a_)
- sheGetPublicKey(pubPos, secPos)
- copyToUint32Array(pub.a_, pubPos)
- mod.Runtime.stackRestore(stack)
- return pub
- }
- she.PublicKey.prototype.encG1 = function(m) {
- return callEnc(sheEnc32G1, she.CipherTextG1, this.a_, m)
- }
- she.PublicKey.prototype.encG2 = function(m) {
- return callEnc(sheEnc32G2, she.CipherTextG2, this.a_, m)
- }
- she.PublicKey.prototype.encGT = function(m) {
- return callEnc(sheEnc32GT, she.CipherTextGT, this.a_, m)
- }
// return x + y
- she.add = function(x, y) {
- if (x.a_.length != y.a_.length) throw('she.add:bad type')
+ self.add = function(x, y) {
+ if (x.a_.length != y.a_.length) throw('self.add:bad type')
let add = null
let cstr = null
- if (she.CipherTextG1.prototype.isPrototypeOf(x)) {
- add = sheAddG1
- cstr = she.CipherTextG1
- } else if (she.CipherTextG2.prototype.isPrototypeOf(x)) {
- add = sheAddG2
- cstr = she.CipherTextG2
- } else if (she.CipherTextGT.prototype.isPrototypeOf(x)) {
- add = sheAddGT
- cstr = she.CipherTextGT
+ if (self.CipherTextG1.prototype.isPrototypeOf(x)) {
+ add = capi.sheAddG1
+ cstr = self.CipherTextG1
+ } else if (self.CipherTextG2.prototype.isPrototypeOf(x)) {
+ add = capi.sheAddG2
+ cstr = self.CipherTextG2
+ } else if (self.CipherTextGT.prototype.isPrototypeOf(x)) {
+ add = capi.sheAddGT
+ cstr = self.CipherTextGT
} else {
- throw('she.add:not supported')
+ throw('self.add:not supported')
}
return callAddSub(add, cstr, x.a_, y.a_)
}
// return x - y
- she.sub = function(x, y) {
- if (x.a_.length != y.a_.length) throw('she.sub:bad type')
+ self.sub = function(x, y) {
+ if (x.a_.length != y.a_.length) throw('self.sub:bad type')
let sub = null
let cstr = null
- if (she.CipherTextG1.prototype.isPrototypeOf(x)) {
- sub = sheSubG1
- cstr = she.CipherTextG1
- } else if (she.CipherTextG2.prototype.isPrototypeOf(x)) {
- sub = sheSubG2
- cstr = she.CipherTextG2
- } else if (she.CipherTextGT.prototype.isPrototypeOf(x)) {
- sub = sheSubGT
- cstr = she.CipherTextGT
+ if (self.CipherTextG1.prototype.isPrototypeOf(x)) {
+ sub = capi.sheSubG1
+ cstr = self.CipherTextG1
+ } else if (self.CipherTextG2.prototype.isPrototypeOf(x)) {
+ sub = capi.sheSubG2
+ cstr = self.CipherTextG2
+ } else if (self.CipherTextGT.prototype.isPrototypeOf(x)) {
+ sub = capi.sheSubGT
+ cstr = self.CipherTextGT
} else {
- throw('she.sub:not supported')
+ throw('self.sub:not supported')
}
return callAddSub(sub, cstr, x.a_, y.a_)
}
// return x * (int)y
- she.mulInt = function(x, y) {
+ self.mulInt = function(x, y) {
let mulInt = null
let cstr = null
- if (she.CipherTextG1.prototype.isPrototypeOf(x)) {
- mulInt = sheMul32G1
- cstr = she.CipherTextG1
- } else if (she.CipherTextG2.prototype.isPrototypeOf(x)) {
- mulInt = sheMul32G2
- cstr = she.CipherTextG2
- } else if (she.CipherTextGT.prototype.isPrototypeOf(x)) {
- mulInt = sheMul32GT
- cstr = she.CipherTextGT
+ if (self.CipherTextG1.prototype.isPrototypeOf(x)) {
+ mulInt = capi.sheMul32G1
+ cstr = self.CipherTextG1
+ } else if (self.CipherTextG2.prototype.isPrototypeOf(x)) {
+ mulInt = capi.sheMul32G2
+ cstr = self.CipherTextG2
+ } else if (self.CipherTextGT.prototype.isPrototypeOf(x)) {
+ mulInt = capi.sheMul32GT
+ cstr = self.CipherTextGT
} else {
- throw('she.mulInt:not supported')
+ throw('self.mulInt:not supported')
}
return callMulInt(mulInt, cstr, x.a_, y)
}
// return (G1)x * (G2)y
- she.mul = function(x, y) {
- if (!she.CipherTextG1.prototype.isPrototypeOf(x)
- || !she.CipherTextG2.prototype.isPrototypeOf(y)) throw('she.mul:bad type')
- let z = new she.CipherTextGT()
+ self.mul = function(x, y) {
+ if (!self.CipherTextG1.prototype.isPrototypeOf(x)
+ || !self.CipherTextG2.prototype.isPrototypeOf(y)) throw('self.mul:bad type')
+ let z = new self.CipherTextGT()
let stack = mod.Runtime.stackSave()
let xPos = mod.Runtime.stackAlloc(x.a_.length * 4)
let yPos = mod.Runtime.stackAlloc(y.a_.length * 4)
let zPos = mod.Runtime.stackAlloc(z.a_.length * 4)
copyFromUint32Array(xPos, x.a_)
copyFromUint32Array(yPos, y.a_)
- sheMul(zPos, xPos, yPos)
+ capi.sheMul(zPos, xPos, yPos)
copyToUint32Array(z.a_, zPos)
mod.Runtime.stackRestore(stack)
return z
}
- // return dec(c)
- she.SecretKey.prototype.dec = function(c) {
- let dec = null
- if (she.CipherTextG1.prototype.isPrototypeOf(c)) {
- dec = sheDecG1
- } else if (she.CipherTextG2.prototype.isPrototypeOf(c)) {
- dec = sheDecG2
- } else if (she.CipherTextGT.prototype.isPrototypeOf(c)) {
- dec = sheDecGT
- } else {
- throw('she.SecretKey.dec:not supported')
- }
- return callDec(dec, this.a_, c.a_)
- }
- // rerand(c)
- she.PublicKey.prototype.reRand = function(c) {
- let reRand = null
- if (she.CipherTextG1.prototype.isPrototypeOf(c)) {
- reRand = sheReRandG1
- } else if (she.CipherTextG2.prototype.isPrototypeOf(c)) {
- reRand = sheReRandG2
- } else if (she.CipherTextGT.prototype.isPrototypeOf(c)) {
- reRand = sheReRandGT
- } else {
- throw('she.PublicKey.reRand:not supported')
- }
- return callReRand(reRand, c.a_, this.a_)
- }
- she.PublicKey.prototype.convertToCipherTextGT = function(c) {
- let convertFrom = null
- if (she.CipherTextG1.prototype.isPrototypeOf(c)) {
- convertFrom = sheConvertFromG1
- } else if (she.CipherTextG2.prototype.isPrototypeOf(c)) {
- convertFrom = sheConvertFromG2
- } else {
- throw('she.PublicKey.convertToCipherTextGT:not supported')
- }
- return callConvertFrom(convertFrom, this.a_, c.a_)
- }
- she.PublicKey.prototype.convertFromG2 = function(c) {
- return she.convertFrom(sheConvertFromG1, this.a_, c.a_)
- }
}
-
- she.init = function(range = 1024, tryNum = 1024, callback = null) {
+ self.init = function(range = 1024, tryNum = 1024, callback = null) {
setupWasm('mclshe.wasm', null, function(_mod, ns) {
mod = _mod
- define_exported_she(mod)
- define_she_extra_functions(mod)
- sheInit()
- console.log('initializing sheSetRangeForDLP')
- let r = sheSetRangeForDLP(range, tryNum)
- console.log('finished ' + r)
- if (callback) callback()
+ fetch('exported-she.json')
+ .then(response => response.json())
+ .then(json => {
+ mod.json = json
+ json.forEach(func => {
+ capi[func.exportName] = mod.cwrap(func.name, func.returns, func.args)
+ })
+ define_extra_functions(mod)
+ capi.sheInit()
+ console.log('initializing sheSetRangeForDLP')
+ let r = capi.sheSetRangeForDLP(range, tryNum)
+ console.log('finished ' + r)
+ if (callback) callback()
+ })
})
}
- return she
+ return self
})
diff --git a/docs/demo/she2.html b/docs/demo/she2.html
index ec5eaef..c2f6cc9 100644
--- a/docs/demo/she2.html
+++ b/docs/demo/she2.html
@@ -7,7 +7,6 @@
<title>Two-level Homomorphic Encryption demo</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script type='text/javascript' src="jquery-3.2.1.min.js"></script>
- <script type='text/javascript' src="exported-she.js"></script>
<script type='text/javascript' src="mclshe.js"></script>
<script type='text/javascript' src="she.js"></script>
<script type="text/javascript" src="she-demo2.js"></script>
diff --git a/ffi/js/export-functions.py b/ffi/js/export-functions.py
index fae9cb7..2a92956 100644
--- a/ffi/js/export-functions.py
+++ b/ffi/js/export-functions.py
@@ -2,11 +2,15 @@ import sys, re, argparse
#RE_PROTOTYPE = re.compile(r'MCLBN_DLL_API\s\w\s\w\([^)]*\);')
RE_PROTOTYPE = re.compile(r'\w*\s(\w*)\s(\w*)\(([^)]*)\);')
-def export_functions(modName, fileNames, reToAddUnderscore):
+def export_functions(args, fileNames, reToAddUnderscore):
+ modName = args.js
+ json = args.json
if not reToAddUnderscore:
reToAddUnderscore = r'(mclBn_init|setStr|getStr|[sS]erialize|setLittleEndian|setHashOf|hashAndMapTo|DecStr|HexStr|HashTo|blsSign|blsVerify|GetCurveOrder|GetFieldOrder|KeyShare|KeyRecover|blsSignatureRecover|blsInit)'
reSpecialFunctionName = re.compile(reToAddUnderscore)
- if modName:
+ if json:
+ print '['
+ elif modName:
print 'function define_exported_' + modName + '(mod) {'
comma = ''
for fileName in fileNames:
@@ -17,22 +21,38 @@ def export_functions(modName, fileNames, reToAddUnderscore):
ret = p.group(1)
name = p.group(2)
arg = p.group(3)
- if modName:
+ if json or modName:
retType = 'null' if ret == 'void' else 'number'
if arg == '' or arg == 'void':
- paramType = '[]'
+ paramNum = 0
else:
- paramType = '[' + ("'number', " * len(arg.split(','))) + ']'
+ paramNum = len(arg.split(','))
if reSpecialFunctionName.search(name):
exportName = '_' + name # to wrap function
else:
exportName = name
- print "{0} = mod.cwrap('{1}', '{2}', {3})".format(exportName, name, retType, paramType)
+ if json:
+ print comma + '{'
+ if comma == '':
+ comma = ','
+ print ' "name":"{0}",'.format(name)
+ print ' "exportName":"{0}",'.format(exportName)
+ print ' "ret":"{0}",'.format(retType)
+ print ' "args":[',
+ if paramNum > 0:
+ print '"number"' + (', "number"' * (paramNum - 1)),
+ print ']'
+ print '}'
+ else:
+ paramType = '[' + ("'number', " * paramNum) + ']'
+ print "{0} = mod.cwrap('{1}', '{2}', {3})".format(exportName, name, retType, paramType)
else:
print comma + "'_" + name + "'",
if comma == '':
comma = ','
- if modName:
+ if json:
+ print ']'
+ elif modName:
print '}'
def main():
@@ -40,12 +60,13 @@ def main():
p.add_argument('header', type=str, nargs='+', help='headers')
p.add_argument('-js', type=str, nargs='?', help='module name')
p.add_argument('-re', type=str, nargs='?', help='regular expression file to add underscore to function name')
+ p.add_argument('-json', action='store_true', help='output json')
args = p.parse_args()
reToAddUnderscore = ''
if args.re:
reToAddUnderscore = open(args.re).read().strip()
- export_functions(args.js, args.header, reToAddUnderscore)
+ export_functions(args, args.header, reToAddUnderscore)
if __name__ == '__main__':
main()