[js] add add/sub/mulInt

dev
MITSUNARI Shigeo 7 years ago
parent 12c0e1a02f
commit 2b44612208
  1. 3
      docs/demo/exported-she.js
  2. 2
      docs/demo/mclshe.js
  3. BIN
      docs/demo/mclshe.wasm
  4. 47
      docs/demo/she-demo.js
  5. 76
      docs/demo/she.js
  6. 5
      include/mcl/she.h
  7. 15
      src/she_c_impl.hpp

@ -34,6 +34,9 @@ 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', ])

File diff suppressed because one or more lines are too long

Binary file not shown.

@ -116,18 +116,43 @@ function onClickTestSHEclass() {
setText('sec2', Uint8ArrayToHexString(sec.serialize()))
let pub = sec.getPublicKey()
setText('pub2', Uint8ArrayToHexString(pub.serialize()))
let m = 15
setText('m2', m)
let c1 = pub.encG1(m)
console.log('dec c1 ' + sec.dec(c1))
let c2 = pub.encG2(m)
let ct = pub.encGT(m)
console.log('ct ' + Uint8ArrayToHexString(ct.serialize()))
setText('c2', Uint8ArrayToHexString(c1.serialize()))
let d = sec.dec(c1)
let m1 = 15
let m2 = 17
setText('m2', m1)
let c11 = pub.encG1(m1)
console.log('dec c11=' + sec.dec(c11))
let c21 = pub.encG2(m1)
let ct1 = pub.encGT(m1)
setText('c2', Uint8ArrayToHexString(c11.serialize()))
let d = sec.dec(c11)
setText('d2', d)
console.log('dec c2=' + sec.dec(c2))
console.log('dec ct=' + sec.dec(ct))
console.log('dec c21=' + sec.dec(c21))
console.log('dec ct1=' + sec.dec(ct1))
let c12 = pub.encG1(m2)
let c22 = pub.encG2(m2)
let ct2 = pub.encGT(m2)
she.add(c11, c11, c12)
she.add(c21, c21, c22)
she.add(ct1, ct1, ct2)
console.log('expect ' + (m1 + m2))
console.log('dec c11=' + sec.dec(c11))
console.log('dec c21=' + sec.dec(c21))
console.log('dec ct1=' + sec.dec(ct1))
she.sub(c12, c12, c11)
she.sub(c22, c22, c21)
she.sub(ct2, ct2, ct1)
console.log('expect ' + (-m1))
console.log('dec c11=' + sec.dec(c12))
console.log('dec c21=' + sec.dec(c22))
console.log('dec ct1=' + sec.dec(ct2))
she.mulInt(c12, c12, -3)
she.mulInt(c22, c22, -3)
she.mulInt(ct2, ct2, -3)
console.log('expect ' + m1 * 3)
console.log('dec c11=' + sec.dec(c12))
console.log('dec c21=' + sec.dec(c22))
console.log('dec ct1=' + sec.dec(ct2))
} catch (e) {
console.log('err ' + e)
}

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

@ -133,6 +133,11 @@ MCLSHE_DLL_API int sheMulG1(sheCipherTextG1 *z, const sheCipherTextG1 *x, int64_
MCLSHE_DLL_API int sheMulG2(sheCipherTextG2 *z, const sheCipherTextG2 *x, int64_t y);
MCLSHE_DLL_API int sheMulGT(sheCipherTextGT *z, const sheCipherTextGT *x, int64_t y);
// for JavaScript
MCLSHE_DLL_API int sheMul32G1(sheCipherTextG1 *z, const sheCipherTextG1 *x, int y);
MCLSHE_DLL_API int sheMul32G2(sheCipherTextG2 *z, const sheCipherTextG2 *x, int y);
MCLSHE_DLL_API int sheMul32GT(sheCipherTextGT *z, const sheCipherTextGT *x, int y);
// return 0 if success
// z = x * y
MCLSHE_DLL_API int sheMul(sheCipherTextGT *z, const sheCipherTextG1 *x, const sheCipherTextG2 *y);

@ -352,6 +352,21 @@ int sheMulGT(sheCipherTextGT *z, const sheCipherTextGT *x, int64_t y)
return mulT(*cast(z), *cast(x), y);
}
int sheMul32G1(sheCipherTextG1 *z, const sheCipherTextG1 *x, int y)
{
return mulT(*cast(z), *cast(x), y);
}
int sheMul32G2(sheCipherTextG2 *z, const sheCipherTextG2 *x, int y)
{
return mulT(*cast(z), *cast(x), y);
}
int sheMul32GT(sheCipherTextGT *z, const sheCipherTextGT *x, int y)
{
return mulT(*cast(z), *cast(x), y);
}
int sheMul(sheCipherTextGT *z, const sheCipherTextG1 *x, const sheCipherTextG2 *y)
{
return mulT(*cast(z), *cast(x), *cast(y));

Loading…
Cancel
Save