[js] change api of enc/dec/mulInt ; add mul

dev
MITSUNARI Shigeo 7 years ago
parent 2b44612208
commit 3838d34613
  1. 23
      docs/demo/she-demo.js
  2. 67
      docs/demo/she.js

@ -131,28 +131,33 @@ function onClickTestSHEclass() {
let c12 = pub.encG1(m2) let c12 = pub.encG1(m2)
let c22 = pub.encG2(m2) let c22 = pub.encG2(m2)
let ct2 = pub.encGT(m2) let ct2 = pub.encGT(m2)
she.add(c11, c11, c12) c11 = she.add(c11, c12)
she.add(c21, c21, c22) c21 = she.add(c21, c22)
she.add(ct1, ct1, ct2) ct1 = she.add(ct1, ct2)
console.log('expect ' + (m1 + m2)) console.log('expect ' + (m1 + m2))
console.log('dec c11=' + sec.dec(c11)) console.log('dec c11=' + sec.dec(c11))
console.log('dec c21=' + sec.dec(c21)) console.log('dec c21=' + sec.dec(c21))
console.log('dec ct1=' + sec.dec(ct1)) console.log('dec ct1=' + sec.dec(ct1))
she.sub(c12, c12, c11) c12 = she.sub(c12, c11)
she.sub(c22, c22, c21) c22 = she.sub(c22, c21)
she.sub(ct2, ct2, ct1) ct2 = she.sub(ct2, ct1)
console.log('expect ' + (-m1)) console.log('expect ' + (-m1))
console.log('dec c11=' + sec.dec(c12)) console.log('dec c11=' + sec.dec(c12))
console.log('dec c21=' + sec.dec(c22)) console.log('dec c21=' + sec.dec(c22))
console.log('dec ct1=' + sec.dec(ct2)) console.log('dec ct1=' + sec.dec(ct2))
she.mulInt(c12, c12, -3) c12 = she.mulInt(c12, -3)
she.mulInt(c22, c22, -3) c22 = she.mulInt(c22, -3)
she.mulInt(ct2, ct2, -3) ct2 = she.mulInt(ct2, -3)
console.log('expect ' + m1 * 3) console.log('expect ' + m1 * 3)
console.log('dec c11=' + sec.dec(c12)) console.log('dec c11=' + sec.dec(c12))
console.log('dec c21=' + sec.dec(c22)) console.log('dec c21=' + sec.dec(c22))
console.log('dec ct1=' + sec.dec(ct2)) console.log('dec ct1=' + sec.dec(ct2))
c12 = pub.encG1(4)
c22 = pub.encG2(5)
ct2 = she.mul(c12, c22)
console.log('dec ct2=' + sec.dec(ct2))
console.log('ok')
} catch (e) { } catch (e) {
console.log('err ' + e) console.log('err ' + e)
} }

@ -199,27 +199,31 @@
mod.Runtime.stackRestore(stack) mod.Runtime.stackRestore(stack)
return c return c
} }
// z = func(x, y) // return func(x, y)
she.callAddSub = function(func, z, x, y) { she.callAddSub = function(func, cstr, x, y) {
let z = new cstr()
let stack = mod.Runtime.stackSave() let stack = mod.Runtime.stackSave()
let xPos = mod.Runtime.stackAlloc(x.length * 4) let xPos = mod.Runtime.stackAlloc(x.length * 4)
let yPos = mod.Runtime.stackAlloc(y.length * 4) let yPos = mod.Runtime.stackAlloc(y.length * 4)
let zPos = mod.Runtime.stackAlloc(z.length * 4) let zPos = mod.Runtime.stackAlloc(z.a_.length * 4)
copyFromUint32Array(xPos, x); copyFromUint32Array(xPos, x);
copyFromUint32Array(yPos, y); copyFromUint32Array(yPos, y);
func(zPos, xPos, yPos) func(zPos, xPos, yPos)
copyToUint32Array(z, zPos) copyToUint32Array(z.a_, zPos)
mod.Runtime.stackRestore(stack) mod.Runtime.stackRestore(stack)
return z
} }
// z = func(x, y) // z = func(x, y)
she.callMulInt = function(func, z, x, y) { she.callMulInt = function(func, cstr, x, y) {
let z = new cstr()
let stack = mod.Runtime.stackSave() let stack = mod.Runtime.stackSave()
let xPos = mod.Runtime.stackAlloc(x.length * 4) let xPos = mod.Runtime.stackAlloc(x.length * 4)
let zPos = mod.Runtime.stackAlloc(z.length * 4) let zPos = mod.Runtime.stackAlloc(z.a_.length * 4)
copyFromUint32Array(xPos, x); copyFromUint32Array(xPos, x);
func(zPos, xPos, y) func(zPos, xPos, y)
copyToUint32Array(z, zPos) copyToUint32Array(z.a_, zPos)
mod.Runtime.stackRestore(stack) mod.Runtime.stackRestore(stack)
return z
} }
she.callDec = function(func, sec, c) { she.callDec = function(func, sec, c) {
let stack = mod.Runtime.stackSave() let stack = mod.Runtime.stackSave()
@ -340,48 +344,77 @@
she.PublicKey.prototype.encGT = function(m) { she.PublicKey.prototype.encGT = function(m) {
return she.callEnc(sheEnc32GT, she.CipherTextGT, this.a_, m) return she.callEnc(sheEnc32GT, she.CipherTextGT, this.a_, m)
} }
// z = x + y // return x + y
she.add = function(z, x, y) { she.add = function(x, y) {
if (x.a_.length != y.a_.length || x.a_.length != z.a_.length) throw('she.add:bad type') if (x.a_.length != y.a_.length) throw('she.add:bad type')
let add = null let add = null
let cstr = null
if (she.CipherTextG1.prototype.isPrototypeOf(x)) { if (she.CipherTextG1.prototype.isPrototypeOf(x)) {
add = sheAddG1 add = sheAddG1
cstr = she.CipherTextG1
} else if (she.CipherTextG2.prototype.isPrototypeOf(x)) { } else if (she.CipherTextG2.prototype.isPrototypeOf(x)) {
add = sheAddG2 add = sheAddG2
cstr = she.CipherTextG2
} else if (she.CipherTextGT.prototype.isPrototypeOf(x)) { } else if (she.CipherTextGT.prototype.isPrototypeOf(x)) {
add = sheAddGT add = sheAddGT
cstr = she.CipherTextGT
} else { } else {
throw('she.add:not supported') throw('she.add:not supported')
} }
she.callAddSub(add, z.a_, x.a_, y.a_) return she.callAddSub(add, cstr, x.a_, y.a_)
} }
she.sub = function(z, x, y) { // return x - y
if (x.a_.length != y.a_.length || x.a_.length != z.a_.length) throw('she.sub:bad type') she.sub = function(x, y) {
if (x.a_.length != y.a_.length) throw('she.sub:bad type')
let sub = null let sub = null
let cstr = null
if (she.CipherTextG1.prototype.isPrototypeOf(x)) { if (she.CipherTextG1.prototype.isPrototypeOf(x)) {
sub = sheSubG1 sub = sheSubG1
cstr = she.CipherTextG1
} else if (she.CipherTextG2.prototype.isPrototypeOf(x)) { } else if (she.CipherTextG2.prototype.isPrototypeOf(x)) {
sub = sheSubG2 sub = sheSubG2
cstr = she.CipherTextG2
} else if (she.CipherTextGT.prototype.isPrototypeOf(x)) { } else if (she.CipherTextGT.prototype.isPrototypeOf(x)) {
sub = sheSubGT sub = sheSubGT
cstr = she.CipherTextGT
} else { } else {
throw('she.sub:not supported') throw('she.sub:not supported')
} }
she.callAddSub(sub, z.a_, x.a_, y.a_) return she.callAddSub(sub, cstr, x.a_, y.a_)
} }
she.mulInt = function(z, x, y) { // return x * (int)y
if (x.a_.length != z.a_.length) throw('she.mulInt:bad type') she.mulInt = function(x, y) {
let mulInt = null let mulInt = null
let cstr = null
if (she.CipherTextG1.prototype.isPrototypeOf(x)) { if (she.CipherTextG1.prototype.isPrototypeOf(x)) {
mulInt = sheMul32G1 mulInt = sheMul32G1
cstr = she.CipherTextG1
} else if (she.CipherTextG2.prototype.isPrototypeOf(x)) { } else if (she.CipherTextG2.prototype.isPrototypeOf(x)) {
mulInt = sheMul32G2 mulInt = sheMul32G2
cstr = she.CipherTextG2
} else if (she.CipherTextGT.prototype.isPrototypeOf(x)) { } else if (she.CipherTextGT.prototype.isPrototypeOf(x)) {
mulInt = sheMul32GT mulInt = sheMul32GT
cstr = she.CipherTextGT
} else { } else {
throw('she.mulInt:not supported') throw('she.mulInt:not supported')
} }
she.callMulInt(mulInt, z.a_, x.a_, y) return she.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()
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)
copyToUint32Array(z.a_, zPos)
mod.Runtime.stackRestore(stack)
return z
} }
she.SecretKey.prototype.dec = function(c) { she.SecretKey.prototype.dec = function(c) {
let dec = null let dec = null

Loading…
Cancel
Save