[js] add getXXXFromHexStr

dev
MITSUNARI Shigeo 7 years ago
parent 2b95f08ab2
commit fc058f0c4d
  1. 25
      docs/demo/she-demo2.js
  2. 25
      docs/demo/she.js

@ -88,22 +88,22 @@ function send() {
$('.encG2y').each(function() { $('.encG2y').each(function() {
ct2.push($(this).text()) ct2.push($(this).text())
}) })
var obj = $('#server_table') let obj = $('#server_table')
obj.html('') obj.html('')
{ {
var header = [ let header = [
'EncG1(x)', 'EncG2(y)', 'EncGT(x * y)' 'EncG1(x)', 'EncG2(y)', 'EncGT(x * y)'
] ]
var t = $('<tr>').attr('id', 'header') let t = $('<tr>').attr('id', 'header')
for (var i = 0; i < header.length; i++) { for (let i = 0; i < header.length; i++) {
t.append( t.append(
$('<th>').append(header[i]) $('<th>').append(header[i])
) )
} }
obj.append(t) obj.append(t)
} }
for (var i = 0; i < ct1.length; i++) { for (let i = 0; i < ct1.length; i++) {
var t = $('<tr>') let t = $('<tr>')
t.append( t.append(
$('<td class="encG1xS">').append(ct1[i]) $('<td class="encG1xS">').append(ct1[i])
).append( ).append(
@ -116,22 +116,20 @@ function send() {
} }
function mul() { function mul() {
let c1 = new she.CipherTextG1()
let c2 = new she.CipherTextG2()
$('.encG1xS').each(function() { $('.encG1xS').each(function() {
let o = $(this) let o = $(this)
c1.fromHexStr(o.text()) let c1 = she.getCipherTextG1FromHexStr(o.text())
c2.fromHexStr(o.next().text()) let c2 = she.getCipherTextG2FromHexStr(o.next().text())
let ct = she.mul(c1, c2) let ct = she.mul(c1, c2)
o.next().next().text(ct.toHexStr()) o.next().next().text(ct.toHexStr())
}) })
} }
function sum() { function sum() {
let ct = new she.CipherTextGT()
let csum = pub.encGT(0) let csum = pub.encGT(0)
$('.encGTxyS').each(function() { $('.encGTxyS').each(function() {
ct.fromHexStr($(this).text()) let s = $(this).text()
let ct = she.getCipherTextGTFromHexStr(s)
csum = she.add(csum, ct) csum = she.add(csum, ct)
}) })
setText('encSumS', csum.toHexStr()) setText('encSumS', csum.toHexStr())
@ -143,8 +141,7 @@ function recv() {
function dec() { function dec() {
let s = getText('encSumC') let s = getText('encSumC')
let ct = new she.CipherTextGT() let ct = she.getCipherTextGTFromHexStr(s)
ct.fromHexStr(s)
let v = sec.dec(ct) let v = sec.dec(ct)
setText('ret', v) setText('ret', v)
} }

@ -308,6 +308,11 @@
she.SecretKey.prototype.dump = function(msg = 'sec ') { she.SecretKey.prototype.dump = function(msg = 'sec ') {
console.log(msg + this.toHexStr()) console.log(msg + this.toHexStr())
} }
she.getSecretKeyFromHexStr = function(s) {
r = new she.SecretKey()
r.fromHexStr(s)
return r
}
she.PublicKey = function() { she.PublicKey = function() {
this.a_ = new Uint32Array(SHE_PUBLICKEY_SIZE / 4) this.a_ = new Uint32Array(SHE_PUBLICKEY_SIZE / 4)
} }
@ -326,6 +331,11 @@
she.PublicKey.prototype.dump = function(msg = 'pub ') { she.PublicKey.prototype.dump = function(msg = 'pub ') {
console.log(msg + this.toHexStr()) console.log(msg + this.toHexStr())
} }
she.getPublicKeyFromHexStr = function(s) {
r = new she.PublicKey()
r.fromHexStr(s)
return r
}
she.CipherTextG1 = function() { she.CipherTextG1 = function() {
this.a_ = new Uint32Array(SHE_CIPHERTEXT_G1_SIZE / 4) this.a_ = new Uint32Array(SHE_CIPHERTEXT_G1_SIZE / 4)
} }
@ -344,6 +354,11 @@
she.CipherTextG1.prototype.dump = function(msg = 'ct1 ') { she.CipherTextG1.prototype.dump = function(msg = 'ct1 ') {
console.log(msg + this.toHexStr()) console.log(msg + this.toHexStr())
} }
she.getCipherTextG1FromHexStr = function(s) {
r = new she.CipherTextG1()
r.fromHexStr(s)
return r
}
she.CipherTextG2 = function() { she.CipherTextG2 = function() {
this.a_ = new Uint32Array(SHE_CIPHERTEXT_G2_SIZE / 4) this.a_ = new Uint32Array(SHE_CIPHERTEXT_G2_SIZE / 4)
} }
@ -362,6 +377,11 @@
she.CipherTextG2.prototype.dump = function(msg = 'ct2 ') { she.CipherTextG2.prototype.dump = function(msg = 'ct2 ') {
console.log(msg + this.toHexStr()) console.log(msg + this.toHexStr())
} }
she.getCipherTextG2FromHexStr = function(s) {
r = new she.CipherTextG2()
r.fromHexStr(s)
return r
}
she.CipherTextGT = function() { she.CipherTextGT = function() {
this.a_ = new Uint32Array(SHE_CIPHERTEXT_GT_SIZE / 4) this.a_ = new Uint32Array(SHE_CIPHERTEXT_GT_SIZE / 4)
} }
@ -380,6 +400,11 @@
she.CipherTextGT.prototype.dump = function(msg = 'ctt ') { she.CipherTextGT.prototype.dump = function(msg = 'ctt ') {
console.log(msg + this.toHexStr()) console.log(msg + this.toHexStr())
} }
she.getCipherTextGTFromHexStr = function(s) {
r = new she.CipherTextGT()
r.fromHexStr(s)
return r
}
she.SecretKey.prototype.setByCSPRNG = function() { she.SecretKey.prototype.setByCSPRNG = function() {
let stack = mod.Runtime.stackSave() let stack = mod.Runtime.stackSave()
let secPos = mod.Runtime.stackAlloc(this.a_.length * 4) let secPos = mod.Runtime.stackAlloc(this.a_.length * 4)

Loading…
Cancel
Save