[js] enc/dec for G1 is ok

dev
MITSUNARI Shigeo 7 years ago
parent b9348ed313
commit 204126a143
  1. 26
      docs/demo/she-demo.js
  2. 8
      docs/demo/she.html
  3. 148
      docs/demo/she.js

@ -59,6 +59,7 @@ function onClickTestSHE() {
let m3 = getValue('msg3') let m3 = getValue('msg3')
let m4 = getValue('msg4') let m4 = getValue('msg4')
sheEnc32G1(c11, pub, m1) sheEnc32G1(c11, pub, m1)
console.log('dec c11=' + sheDecG1(sec, c11))
sheEnc32G1(c12, pub, m2) sheEnc32G1(c12, pub, m2)
sheEnc32G2(c21, pub, m3) sheEnc32G2(c21, pub, m3)
sheEnc32G2(c22, pub, m4) sheEnc32G2(c22, pub, m4)
@ -101,3 +102,28 @@ function HexStringToUint8Array(s) {
return a return a
} }
function onClickTestSHEclass() {
let sec = new she.SecretKey()
sec.setByCSPRNG()
setText('sec2', Uint8ArrayToHexString(sec.serialize()))
let pub = sec.getPublicKey()
setText('pub2', Uint8ArrayToHexString(pub.serialize()))
let m = 15
setText('m2', m)
let c = pub.enc(m)
setText('c2', Uint8ArrayToHexString(c.serialize()))
if (0) {
let s1 = sheSecretKey_malloc()
let p1 = shePublicKey_malloc()
sheSecretKeyDeserialize(s1, sec.serialize())
putSecretKey(s1)
sheGetPublicKey(p1, s1)
putPublicKey(p1)
let c1 = sheCipherTextG1_malloc()
sheCipherTextG1Deserialize(c1, c.serialize())
putCipherTextG1(c1)
console.log('dec c1=' + sheDecG1(s1, c1))
}
let d = sec.dec(c)
setText('d2', d)
}

@ -29,5 +29,13 @@ enc((msg1 + msg2) * (msg3 + msg4)) : <span name="encGT"></span><br>
dec : <span name="decMsg"></span><br> dec : <span name="decMsg"></span><br>
</div> </div>
<hr> <hr>
<button type="text" id="testSHE" onclick="onClickTestSHEclass()">test SHE class</button>
<div>
sec2 : <span name="sec2"></span><br>
pub2 : <span name="pub2"></span><br>
m2 : <span name="m2"></span><br>
c2 : <span name="c2"></span><br>
d2 : <span name="d2"></span><br>
</div>
</body> </body>
</html> </html>

@ -5,6 +5,8 @@
window.she = return_she() window.she = return_she()
} }
})(function() { })(function() {
const crypto = window.crypto || window.msCrypto
const MCLBN_CURVE_FP254BNB = 0 const MCLBN_CURVE_FP254BNB = 0
const MCLBN_FP_UNIT_SIZE = 4 const MCLBN_FP_UNIT_SIZE = 4
const MCLBN_FP_SIZE = MCLBN_FP_UNIT_SIZE * 8 const MCLBN_FP_SIZE = MCLBN_FP_UNIT_SIZE * 8
@ -18,8 +20,9 @@
const SHE_CIPHERTEXT_G2_SIZE = MCLBN_G2_SIZE * 2 const SHE_CIPHERTEXT_G2_SIZE = MCLBN_G2_SIZE * 2
const SHE_CIPHERTEXT_GT_SIZE = MCLBN_GT_SIZE * 4 const SHE_CIPHERTEXT_GT_SIZE = MCLBN_GT_SIZE * 4
let she = {}
let mod = {} let mod = {}
let she = {}
she.mod = mod
const setupWasm = function(fileName, nameSpace, setupFct) { const setupWasm = function(fileName, nameSpace, setupFct) {
console.log('setupWasm ' + fileName) console.log('setupWasm ' + fileName)
@ -36,6 +39,7 @@
}) })
return mod return mod
} }
const define_she_extra_functions = function(mod) { const define_she_extra_functions = function(mod) {
const ptrToStr = function(pos, n) { const ptrToStr = function(pos, n) {
let s = '' let s = ''
@ -136,14 +140,12 @@
return v return v
} }
} }
const crypto = window.crypto || window.msCrypto const copyToUint32Array = function(a, pos) {
let copyToUint32Array = function(a, pos) {
for (let i = 0; i < a.length; i++) { for (let i = 0; i < a.length; i++) {
a[i] = mod.HEAP32[pos / 4 + i] a[i] = mod.HEAP32[pos / 4 + i]
} }
} }
let copyFromUint32Array = function(pos, a) { const copyFromUint32Array = function(pos, a) {
for (let i = 0; i < a.length; i++) { for (let i = 0; i < a.length; i++) {
mod.HEAP32[pos / 4 + i] = a[i] mod.HEAP32[pos / 4 + i] = a[i]
} }
@ -205,39 +207,33 @@
copyToUint32Array(pub, pubPos) copyToUint32Array(pub, pubPos)
mod.Runtime.stackRestore(stack) mod.Runtime.stackRestore(stack)
} }
///////////////////////////////////////////////////////////////
she_free = function(p) { she_free = function(p) {
mod._free(p) mod._free(p)
} }
///////////////////////////////////////////////////////////////
sheSecretKey_malloc = function() { sheSecretKey_malloc = function() {
return mod._malloc(SHE_SECRETKEY_SIZE) return mod._malloc(SHE_SECRETKEY_SIZE)
} }
sheSecretKeySerialize = wrap_outputArray(_sheSecretKeySerialize)
sheSecretKeyDeserialize = wrap_input1(_sheSecretKeyDeserialize)
///////////////////////////////////////////////////////////////
shePublicKey_malloc = function() { shePublicKey_malloc = function() {
return mod._malloc(SHE_PUBLICKEY_SIZE) return mod._malloc(SHE_PUBLICKEY_SIZE)
} }
shePublicKeySerialize = wrap_outputArray(_shePublicKeySerialize) sheCipherTextG2_malloc = function() {
shePublicKeyDeserialize = wrap_input1(_shePublicKeyDeserialize) return mod._malloc(SHE_CIPHERTEXT_G2_SIZE)
/////////////////////////////////////////////////////////////// }
sheCipherTextGT_malloc = function() {
return mod._malloc(SHE_CIPHERTEXT_GT_SIZE)
}
sheCipherTextG1_malloc = function() { sheCipherTextG1_malloc = function() {
return mod._malloc(SHE_CIPHERTEXT_G1_SIZE) 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) sheCipherTextG1Serialize = wrap_outputArray(_sheCipherTextG1Serialize)
sheCipherTextG1Deserialize = wrap_input1(_sheCipherTextG1Deserialize) sheCipherTextG1Deserialize = wrap_input1(_sheCipherTextG1Deserialize)
sheDecG1 = wrap_dec(_sheDecG1) sheDecG1 = wrap_dec(_sheDecG1)
///////////////////////////////////////////////////////////////
sheCipherTextG2_malloc = function() {
return mod._malloc(SHE_CIPHERTEXT_G2_SIZE)
}
sheCipherTextG2Serialize = wrap_outputArray(_sheCipherTextG2Serialize) sheCipherTextG2Serialize = wrap_outputArray(_sheCipherTextG2Serialize)
sheCipherTextG2Deserialize = wrap_input1(_sheCipherTextG2Deserialize) sheCipherTextG2Deserialize = wrap_input1(_sheCipherTextG2Deserialize)
///////////////////////////////////////////////////////////////
sheCipherTextGT_malloc = function() {
return mod._malloc(SHE_CIPHERTEXT_GT_SIZE)
}
sheCipherTextGTSerialize = wrap_outputArray(_sheCipherTextGTSerialize) sheCipherTextGTSerialize = wrap_outputArray(_sheCipherTextGTSerialize)
sheCipherTextGTDeserialize = wrap_input1(_sheCipherTextGTDeserialize) sheCipherTextGTDeserialize = wrap_input1(_sheCipherTextGTDeserialize)
sheDecGT = wrap_dec(_sheDecGT) sheDecGT = wrap_dec(_sheDecGT)
@ -248,73 +244,69 @@
if (r) throw('sheInit') if (r) throw('sheInit')
// r = sheSetRangeForGTDLP(128, 1024) // r = sheSetRangeForGTDLP(128, 1024)
} }
she.SecretKey = function() {
this.a_ = new Uint32Array(SHE_SECRETKEY_SIZE / 4)
}
she.SecretKey.prototype.serialize = function() {
return she.callGetter(sheSecretKeySerialize, this.a_)
}
she.SecretKey.prototype.deserialize = function(s) {
return she.callSetter(sheSecretKeyDeserialize, this.a_, s)
}
she.PublicKey = function() {
this.a_ = new Uint32Array(SHE_PUBLICKEY_SIZE / 4)
}
she.PublicKey.prototype.serialize = function() {
return she.callGetter(shePublicKeySerialize, this.a_)
}
she.PublicKey.prototype.deserialize = function(s) {
return she.callSetter(shePublicKeyDeserialize, this.a_, s)
}
she.CipherTextG1 = function() {
this.a_ = new Uint32Array(SHE_CIPHERTEXT_G1_SIZE / 4)
}
she.CipherTextG1.prototype.serialize = function() {
return she.callGetter(sheCipherTextG1Serialize, this.a_)
}
she.CipherTextG1.prototype.deserialize = function(s) {
return she.callSetter(sheCipherTextG1Deserialize, this.a_, s)
}
she.SecretKey.prototype.setByCSPRNG = function() {
she.callSetByCSPRNG(this.a_)
}
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.enc = function(m) {
return she.callEnc(sheEnc32G1, she.CipherTextG1, this.a_, m)
}
she.SecretKey.prototype.dec = function(c) {
if (she.CipherTextG1.prototype.isPrototypeOf(c)) {
return she.callDec(sheDecG1, this.a_, c.a_)
}
throw('she.SecretKey.dec is not supported')
}
} }
she.init = function(callback = null) { she.init = function(callback = null) {
setupWasm('mclshe.wasm', null, function(mod, ns) { setupWasm('mclshe.wasm', null, function(_mod, ns) {
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, 2048) let r = sheSetRangeForDLP(256, 100)
console.log('finished ' + r) console.log('finished ' + r)
if (callback) callback() if (callback) callback()
}) })
} }
she.SecretKey = function() {
this.a_ = new Uint32Array(SHE_SECRETKEY_SIZE / 4)
}
she.SecretKey.prototype.serialize = function() {
return she.callGetter(sheSecretKeySerialize, this.a_)
}
she.SecretKey.prototype.deserialize = function(s) {
return she.callSetter(sheSecretKeyDeserialize, this.a_, s)
}
she.PublicKey = function() {
this.a_ = new Uint32Array(SHE_PUBLICKEY_SIZE / 4)
}
she.PublicKey.prototype.serialize = function() {
return she.callGetter(shePublicKeySerialize, this.a_)
}
she.PublicKey.prototype.deserialize = function(s) {
return she.callSetter(shePublicKeyDeserialize, this.a_, s)
}
she.CipherTextG1 = function() {
this.a_ = new Uint32Array(SHE_CIPHERTEXT_G1_SIZE / 4)
}
she.CipherTextG1.prototype.serialize = function() {
return she.callGetter(sheCipherTextG1Serialize, this.a_)
}
she.CipherTextG1.prototype.deserialize = function(s) {
return she.callSetter(sheCipherTextG1Deserialize, this.a_, s)
}
she.SecretKey.prototype.setByCSPRNG = function() {
she.callSetByCSPRNG(this.a_)
}
she.SecretKey.prototype.getPublicKey = function() {
let pub = new she.PublicKey()
she.callGetPublicKey(pub.a_, this.a_)
return pub
/*
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.enc = function(m) {
return she.callEnc(sheEnc32G1, she.CipherTextG1, this.a_, m)
}
she.SecretKey.prototype.dec = function(c) {
if (she.CipherTextG1.prototype.isPrototypeOf(c)) {
return she.callDec(sheDecG1, this.a_, c)
}
throw('she.SecretKey.dec is not supported')
}
return she return she
}) })

Loading…
Cancel
Save