[js] enc/dec for G1/G2/GT

dev
MITSUNARI Shigeo 7 years ago
parent c5af29d908
commit 12c0e1a02f
  1. 2
      Makefile
  2. 1
      docs/demo/exported-she.js
  3. 2
      docs/demo/mclshe.js
  4. BIN
      docs/demo/mclshe.wasm
  5. 51
      docs/demo/she-demo.js
  6. 1
      docs/demo/she.html
  7. 72
      docs/demo/she.js
  8. 12
      docs/demo/style.css
  9. 1
      include/mcl/she.h
  10. 5
      src/she_c_impl.hpp

@ -217,7 +217,7 @@ $(EXPORTED_SHE_JS): ./include/mcl/she.h
EXPORTED_SHE=$(shell cat $(SHE_TXT)) EXPORTED_SHE=$(shell cat $(SHE_TXT))
docs/demo/mclshe.js: src/fp.cpp src/she_c256.cpp $(SHE_TXT) $(EXPORTED_SHE_JS) docs/demo/mclshe.js: src/fp.cpp src/she_c256.cpp $(SHE_TXT) $(EXPORTED_SHE_JS) src/she_c_impl.hpp include/mcl/she.hpp
emcc -o $@ src/fp.cpp src/she_c256.cpp -I./include -I./src -I../cybozulib/include -s WASM=1 -s "MODULARIZE=1" -s "EXPORTED_FUNCTIONS=[$(EXPORTED_SHE)]" -O3 -DNDEBUG -DMCLBN_FP_UNIT_SIZE=4 -DMCL_MAX_BIT_SIZE=256 -s DISABLE_EXCEPTION_CATCHING=0 -s NO_EXIT_RUNTIME=1 -DMCLSHE_WIN_SIZE=8 emcc -o $@ src/fp.cpp src/she_c256.cpp -I./include -I./src -I../cybozulib/include -s WASM=1 -s "MODULARIZE=1" -s "EXPORTED_FUNCTIONS=[$(EXPORTED_SHE)]" -O3 -DNDEBUG -DMCLBN_FP_UNIT_SIZE=4 -DMCL_MAX_BIT_SIZE=256 -s DISABLE_EXCEPTION_CATCHING=0 -s NO_EXIT_RUNTIME=1 -DMCLSHE_WIN_SIZE=8
demo: demo:

@ -23,6 +23,7 @@ sheEnc32G1 = mod.cwrap('sheEnc32G1', 'number', ['number', 'number', 'number', ])
sheEnc32G2 = mod.cwrap('sheEnc32G2', 'number', ['number', 'number', 'number', ]) sheEnc32G2 = mod.cwrap('sheEnc32G2', 'number', ['number', 'number', 'number', ])
sheEnc32GT = mod.cwrap('sheEnc32GT', 'number', ['number', 'number', 'number', ]) sheEnc32GT = mod.cwrap('sheEnc32GT', 'number', ['number', 'number', 'number', ])
_sheDecG1 = mod.cwrap('sheDecG1', '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', ]) _sheDecGT = mod.cwrap('sheDecGT', 'number', ['number', 'number', 'number', ])
sheAddG1 = mod.cwrap('sheAddG1', 'number', ['number', 'number', 'number', ]) sheAddG1 = mod.cwrap('sheAddG1', 'number', ['number', 'number', 'number', ])
sheAddG2 = mod.cwrap('sheAddG2', 'number', ['number', 'number', 'number', ]) sheAddG2 = mod.cwrap('sheAddG2', 'number', ['number', 'number', 'number', ])

File diff suppressed because one or more lines are too long

Binary file not shown.

@ -3,7 +3,11 @@ function setValue(name, val) { document.getElementsByName(name)[0].value = val }
function getText(name) { return document.getElementsByName(name)[0].innerText } function getText(name) { return document.getElementsByName(name)[0].innerText }
function setText(name, val) { document.getElementsByName(name)[0].innerText = val } function setText(name, val) { document.getElementsByName(name)[0].innerText = val }
she.init(function() { setText('status', 'ok')}) (function() {
const range = 2048
const tryNum = 100
she.init(range, tryNum, function() { setText('status', 'ok')})
}())
function putSecretKey(x, msg = "") { function putSecretKey(x, msg = "") {
console.log(msg + ' sk=' + Uint8ArrayToHexString(sheSecretKeySerialize(x))) console.log(msg + ' sk=' + Uint8ArrayToHexString(sheSecretKeySerialize(x)))
@ -61,8 +65,11 @@ function onClickTestSHE() {
sheEnc32G1(c11, pub, m1) sheEnc32G1(c11, pub, m1)
console.log('dec c11=' + sheDecG1(sec, c11)) console.log('dec c11=' + sheDecG1(sec, c11))
sheEnc32G1(c12, pub, m2) sheEnc32G1(c12, pub, m2)
console.log('dec c12=' + sheDecG1(sec, c12))
sheEnc32G2(c21, pub, m3) sheEnc32G2(c21, pub, m3)
console.log('dec c21=' + sheDecG2(sec, c21))
sheEnc32G2(c22, pub, m4) sheEnc32G2(c22, pub, m4)
console.log('dec c22=' + sheDecG2(sec, c22))
setText('encG11', Uint8ArrayToHexString(sheCipherTextG1Serialize(c11))) setText('encG11', Uint8ArrayToHexString(sheCipherTextG1Serialize(c11)))
setText('encG12', Uint8ArrayToHexString(sheCipherTextG1Serialize(c12))) setText('encG12', Uint8ArrayToHexString(sheCipherTextG1Serialize(c12)))
setText('encG21', Uint8ArrayToHexString(sheCipherTextG2Serialize(c21))) setText('encG21', Uint8ArrayToHexString(sheCipherTextG2Serialize(c21)))
@ -103,27 +110,25 @@ function HexStringToUint8Array(s) {
} }
function onClickTestSHEclass() { function onClickTestSHEclass() {
let sec = new she.SecretKey() try {
sec.setByCSPRNG() let sec = new she.SecretKey()
setText('sec2', Uint8ArrayToHexString(sec.serialize())) sec.setByCSPRNG()
let pub = sec.getPublicKey() setText('sec2', Uint8ArrayToHexString(sec.serialize()))
setText('pub2', Uint8ArrayToHexString(pub.serialize())) let pub = sec.getPublicKey()
let m = 15 setText('pub2', Uint8ArrayToHexString(pub.serialize()))
setText('m2', m) let m = 15
let c = pub.encG1(m) setText('m2', m)
setText('c2', Uint8ArrayToHexString(c.serialize())) let c1 = pub.encG1(m)
if (0) { console.log('dec c1 ' + sec.dec(c1))
let s1 = sheSecretKey_malloc() let c2 = pub.encG2(m)
let p1 = shePublicKey_malloc() let ct = pub.encGT(m)
sheSecretKeyDeserialize(s1, sec.serialize()) console.log('ct ' + Uint8ArrayToHexString(ct.serialize()))
putSecretKey(s1) setText('c2', Uint8ArrayToHexString(c1.serialize()))
sheGetPublicKey(p1, s1) let d = sec.dec(c1)
putPublicKey(p1) setText('d2', d)
let c1 = sheCipherTextG1_malloc() console.log('dec c2=' + sec.dec(c2))
sheCipherTextG1Deserialize(c1, c.serialize()) console.log('dec ct=' + sec.dec(ct))
putCipherTextG1(c1) } catch (e) {
console.log('dec c1=' + sheDecG1(s1, c1)) console.log('err ' + e)
} }
let d = sec.dec(c)
setText('d2', d)
} }

@ -3,6 +3,7 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>SHE with 1multi sample</title> <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="./exported-she.js"></script>
<script type='text/javascript' src="./mclshe.js"></script> <script type='text/javascript' src="./mclshe.js"></script>
<script type='text/javascript' src="./she.js"></script> <script type='text/javascript' src="./she.js"></script>

@ -170,10 +170,17 @@
copyToUint32Array(a, pos) copyToUint32Array(a, pos)
mod._free(pos) mod._free(pos)
} }
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
}
she.callEnc = function(func, cstr, pub, m) { she.callEnc = function(func, cstr, pub, m) {
let c = new cstr() let c = new cstr()
let stack = mod.Runtime.stackSave() let stack = mod.Runtime.stackSave()
let cPos = mod.Runtime.stackAlloc(c.length * 4) let cPos = mod.Runtime.stackAlloc(c.a_.length * 4)
let pubPos = mod.Runtime.stackAlloc(pub.length * 4) let pubPos = mod.Runtime.stackAlloc(pub.length * 4)
copyFromUint32Array(pubPos, pub); copyFromUint32Array(pubPos, pub);
func(cPos, pubPos, m) func(cPos, pubPos, m)
@ -191,22 +198,6 @@
mod.Runtime.stackRestore(stack) mod.Runtime.stackRestore(stack)
return r return r
} }
she.callSetByCSPRNG = function(sec) {
let stack = mod.Runtime.stackSave()
let secPos = mod.Runtime.stackAlloc(sec.length * 4)
sheSecretKeySetByCSPRNG(secPos)
copyToUint32Array(sec, secPos)
mod.Runtime.stackRestore(stack)
}
she.callGetPublicKey = function(pub, sec) {
let stack = mod.Runtime.stackSave()
let secPos = mod.Runtime.stackAlloc(sec.length * 4)
let pubPos = mod.Runtime.stackAlloc(pub.length * 4)
copyFromUint32Array(secPos, sec)
sheGetPublicKey(pubPos, secPos)
copyToUint32Array(pub, pubPos)
mod.Runtime.stackRestore(stack)
}
she_free = function(p) { she_free = function(p) {
mod._free(p) mod._free(p)
} }
@ -234,6 +225,7 @@
sheDecG1 = wrap_dec(_sheDecG1) sheDecG1 = wrap_dec(_sheDecG1)
sheCipherTextG2Serialize = wrap_outputArray(_sheCipherTextG2Serialize) sheCipherTextG2Serialize = wrap_outputArray(_sheCipherTextG2Serialize)
sheCipherTextG2Deserialize = wrap_input1(_sheCipherTextG2Deserialize) sheCipherTextG2Deserialize = wrap_input1(_sheCipherTextG2Deserialize)
sheDecG2 = wrap_dec(_sheDecG2)
sheCipherTextGTSerialize = wrap_outputArray(_sheCipherTextGTSerialize) sheCipherTextGTSerialize = wrap_outputArray(_sheCipherTextGTSerialize)
sheCipherTextGTDeserialize = wrap_input1(_sheCipherTextGTDeserialize) sheCipherTextGTDeserialize = wrap_input1(_sheCipherTextGTDeserialize)
sheDecGT = wrap_dec(_sheDecGT) sheDecGT = wrap_dec(_sheDecGT)
@ -242,7 +234,6 @@
let r = _sheInit(curveType, MCLBN_FP_UNIT_SIZE) let r = _sheInit(curveType, MCLBN_FP_UNIT_SIZE)
console.log('sheInit ' + r) console.log('sheInit ' + r)
if (r) throw('sheInit') if (r) throw('sheInit')
// r = sheSetRangeForGTDLP(128, 1024)
} }
she.SecretKey = function() { she.SecretKey = function() {
this.a_ = new Uint32Array(SHE_SECRETKEY_SIZE / 4) this.a_ = new Uint32Array(SHE_SECRETKEY_SIZE / 4)
@ -271,8 +262,30 @@
she.CipherTextG1.prototype.deserialize = function(s) { she.CipherTextG1.prototype.deserialize = function(s) {
return she.callSetter(sheCipherTextG1Deserialize, this.a_, s) return she.callSetter(sheCipherTextG1Deserialize, this.a_, s)
} }
she.CipherTextG2 = function() {
this.a_ = new Uint32Array(SHE_CIPHERTEXT_G2_SIZE / 4)
}
she.CipherTextG2.prototype.serialize = function() {
return she.callGetter(sheCipherTextG2Serialize, this.a_)
}
she.CipherTextG2.prototype.deserialize = function(s) {
return she.callSetter(sheCipherTextG2Deserialize, this.a_, s)
}
she.CipherTextGT = function() {
this.a_ = new Uint32Array(SHE_CIPHERTEXT_GT_SIZE / 4)
}
she.CipherTextGT.prototype.serialize = function() {
return she.callGetter(sheCipherTextGTSerialize, this.a_)
}
she.CipherTextGT.prototype.deserialize = function(s) {
return she.callSetter(sheCipherTextGTDeserialize, this.a_, s)
}
she.SecretKey.prototype.setByCSPRNG = function() { she.SecretKey.prototype.setByCSPRNG = function() {
she.callSetByCSPRNG(this.a_) 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() { she.SecretKey.prototype.getPublicKey = function() {
let pub = new she.PublicKey() let pub = new she.PublicKey()
@ -288,22 +301,35 @@
she.PublicKey.prototype.encG1 = function(m) { she.PublicKey.prototype.encG1 = function(m) {
return she.callEnc(sheEnc32G1, she.CipherTextG1, this.a_, m) return she.callEnc(sheEnc32G1, she.CipherTextG1, this.a_, m)
} }
she.PublicKey.prototype.encG2 = function(m) {
return she.callEnc(sheEnc32G2, she.CipherTextG2, this.a_, m)
}
she.PublicKey.prototype.encGT = function(m) {
return she.callEnc(sheEnc32GT, she.CipherTextGT, this.a_, m)
}
she.SecretKey.prototype.dec = function(c) { she.SecretKey.prototype.dec = function(c) {
let dec = null
if (she.CipherTextG1.prototype.isPrototypeOf(c)) { if (she.CipherTextG1.prototype.isPrototypeOf(c)) {
return she.callDec(sheDecG1, this.a_, c.a_) 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 is not supported')
} }
throw('she.SecretKey.dec is not supported') return she.callDec(dec, this.a_, c.a_)
} }
} }
she.init = function(callback = null) { she.init = function(range = 1024, tryNum = 1024, callback = null) {
setupWasm('mclshe.wasm', null, function(_mod, ns) { setupWasm('mclshe.wasm', null, function(_mod, ns) {
mod = _mod mod = _mod
define_exported_she(mod) define_exported_she(mod)
define_she_extra_functions(mod) define_she_extra_functions(mod)
sheInit() sheInit()
console.log('initializing sheSetRangeForDLP') console.log('initializing sheSetRangeForDLP')
let r = sheSetRangeForDLP(256, 100) let r = sheSetRangeForDLP(range, tryNum)
console.log('finished ' + r) console.log('finished ' + r)
if (callback) callback() if (callback) callback()
}) })

@ -0,0 +1,12 @@
li {
list-style-type : none;
padding:5px 10px;
border-top:1px solid black;
border-left:1px solid black;
}
span {
max-width: 600px;
display: inline-block;
overflow: hidden;
}

@ -112,6 +112,7 @@ MCLSHE_DLL_API int sheEnc32GT(sheCipherTextGT *c, const shePublicKey *pub, int m
return 0 if success return 0 if success
*/ */
MCLSHE_DLL_API int sheDecG1(int64_t *m, const sheSecretKey *sec, const sheCipherTextG1 *c); MCLSHE_DLL_API int sheDecG1(int64_t *m, const sheSecretKey *sec, const sheCipherTextG1 *c);
MCLSHE_DLL_API int sheDecG2(int64_t *m, const sheSecretKey *sec, const sheCipherTextG2 *c);
MCLSHE_DLL_API int sheDecGT(int64_t *m, const sheSecretKey *sec, const sheCipherTextGT *c); MCLSHE_DLL_API int sheDecGT(int64_t *m, const sheSecretKey *sec, const sheCipherTextGT *c);
// return 0 if success // return 0 if success

@ -264,6 +264,11 @@ int sheDecG1(int64_t *m, const sheSecretKey *sec, const sheCipherTextG1 *c)
return decT(m, sec, c); return decT(m, sec, c);
} }
int sheDecG2(int64_t *m, const sheSecretKey *sec, const sheCipherTextG2 *c)
{
return decT(m, sec, c);
}
int sheDecGT(int64_t *m, const sheSecretKey *sec, const sheCipherTextGT *c) int sheDecGT(int64_t *m, const sheSecretKey *sec, const sheCipherTextGT *c)
{ {
return decT(m, sec, c); return decT(m, sec, c);

Loading…
Cancel
Save