[js] change she.init api

dev
MITSUNARI Shigeo 7 years ago
parent 4057edeeba
commit aa76e7ce48
  1. 9
      docs/demo/she-demo.js
  2. 60
      docs/demo/she-demo2.js
  3. 67
      docs/demo/she.js

@ -3,11 +3,10 @@ function setValue(name, val) { document.getElementsByName(name)[0].value = val }
function getText(name) { return document.getElementsByName(name)[0].innerText }
function setText(name, val) { document.getElementsByName(name)[0].innerText = val }
(function() {
she.init(function() {
setText('status', 'ok')
})
})()
she.init()
.then(() => {
setText('status', 'ok')
})
function bench(label, count, func) {
let start = Date.now()

@ -3,56 +3,18 @@ function setValue(name, val) { document.getElementsByName(name)[0].value = val }
function getText(name) { return document.getElementsByName(name)[0].innerText }
function setText(name, val) { document.getElementsByName(name)[0].innerText = val }
function Uint8ArrayToHexString(a) {
let s = ''
for (let i = 0; i < a.length; i++) {
s += ('0' + a[i].toString(16)).slice(-2)
}
return s
}
function HexStringToUint8Array(s) {
let a = new Uint8Array(s.length / 2)
for (let i = 0; i < s.length / 2; i++) {
a[i] = parseInt(s.slice(i * 2, i * 2 + 2), 16)
}
return a
}
function bin2hex(s) {
var o = ''
for (var i = 0; i < s.length; i++) {
n = s.charCodeAt(i).toString(16)
o += (n.length < 2 ? '0' + n : n) + ' '
}
return o
}
function strip(s) {
s = s.trim()
var begin = 0
var end = s.length
if (end == 0) {
return s
}
if (s[0] == '"') {
begin++
}
if (begin < end && s[end - 1] == '"') {
end--
}
return s.substr(begin, end - begin)
}
let sec = null
let pub = null
she.init(function() {
setText('status', 'ok')
sec = new she.SecretKey()
sec.setByCSPRNG()
setText('sec', sec.toHexStr())
pub = sec.getPublicKey()
setText('pub', pub.toHexStr())
})
she.init()
.then(() => {
setText('status', 'ok')
sec = new she.SecretKey()
sec.setByCSPRNG()
sec.dump('sec=')
pub = sec.getPublicKey()
pub.dump('pub=')
})
function append() {
let v = getValue('append')

@ -26,51 +26,48 @@
const SHE_CIPHERTEXT_G2_SIZE = MCLBN_G2_SIZE * 2
const SHE_CIPHERTEXT_GT_SIZE = MCLBN_GT_SIZE * 4
let g_callback = null
let g_range = 0
let g_tryNum = 0
let capi = {}
exports.capi = capi
let mod = exports.mod
exports.init = function(callback = null, range = 1024, tryNum = 1024) {
/*
init she
@param range [in] table size of DLP ; require 8 * table size
@param tryNum [in] how many search ; O(tryNum) time
can decrypt (range * tryNum) range value
*/
exports.init = (range = 1024, tryNum = 1024) => {
console.log('init')
g_callback = callback
g_range = range
g_tryNum = tryNum
if (isNodeJs) {
} else {
if (!isNodeJs) {
fetch('she_c.wasm')
.then(response => response.arrayBuffer())
.then(buffer => new Uint8Array(buffer))
.then(binary => { Module(mod) })
}
}
mod.onRuntimeInitialized = function () {
const f = function(exportedFuncs) {
exportedFuncs.forEach(func => {
capi[func.exportName] = mod.cwrap(func.name, func.returns, func.args)
})
define_extra_functions(mod)
capi.sheInit()
console.log('initializing sheSetRangeForDLP')
const r = capi.sheSetRangeForDLP(g_range, g_tryNum)
console.log('finished ' + r)
if (g_callback) g_callback()
}
const EXPORTED_JSON = 'exported-she.json'
if (isNodeJs) {
const fs = require('fs')
const path = require('path')
const jsonStr = fs.readFileSync(path.resolve(__dirname, EXPORTED_JSON))
f(JSON.parse(jsonStr))
} else {
fetch(EXPORTED_JSON)
.then(response => response.json())
.then(exportedFuncs => f(exportedFuncs))
}
return new Promise((resolve) => {
mod.onRuntimeInitialized = () => {
const f = (exportedFuncs) => {
exportedFuncs.forEach(func => {
capi[func.exportName] = mod.cwrap(func.name, func.returns, func.args)
})
define_extra_functions(mod)
capi.sheInit()
console.log(`initializing sheSetRangeForDLP(range=${range}, tryNum=${tryNum})`)
const r = capi.sheSetRangeForDLP(range, tryNum)
console.log('finished ' + r)
resolve()
}
if (isNodeJs) {
const fs = require('fs')
const jsonStr = fs.readFileSync('./exported-she.json')
f(JSON.parse(jsonStr))
} else {
fetch('exported-she.json')
.then(response => response.json())
.then(exportedFuncs => f(exportedFuncs))
}
}
})
}
const ptrToStr = function(pos, n) {

Loading…
Cancel
Save