[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 getText(name) { return document.getElementsByName(name)[0].innerText }
function setText(name, val) { document.getElementsByName(name)[0].innerText = val } function setText(name, val) { document.getElementsByName(name)[0].innerText = val }
(function() { she.init()
she.init(function() { .then(() => {
setText('status', 'ok') setText('status', 'ok')
}) })
})()
function bench(label, count, func) { function bench(label, count, func) {
let start = Date.now() 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 getText(name) { return document.getElementsByName(name)[0].innerText }
function setText(name, val) { document.getElementsByName(name)[0].innerText = val } function setText(name, val) { document.getElementsByName(name)[0].innerText = val }
function Uint8ArrayToHexString(a) { let sec = null
let s = '' let pub = null
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)
}
she.init(function() { she.init()
setText('status', 'ok') .then(() => {
sec = new she.SecretKey() setText('status', 'ok')
sec.setByCSPRNG() sec = new she.SecretKey()
setText('sec', sec.toHexStr()) sec.setByCSPRNG()
pub = sec.getPublicKey() sec.dump('sec=')
setText('pub', pub.toHexStr()) pub = sec.getPublicKey()
}) pub.dump('pub=')
})
function append() { function append() {
let v = getValue('append') let v = getValue('append')

@ -26,51 +26,48 @@
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 g_callback = null
let g_range = 0
let g_tryNum = 0
let capi = {} let capi = {}
exports.capi = capi exports.capi = capi
let mod = exports.mod 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') console.log('init')
g_callback = callback if (!isNodeJs) {
g_range = range
g_tryNum = tryNum
if (isNodeJs) {
} else {
fetch('she_c.wasm') fetch('she_c.wasm')
.then(response => response.arrayBuffer()) .then(response => response.arrayBuffer())
.then(buffer => new Uint8Array(buffer)) .then(buffer => new Uint8Array(buffer))
.then(binary => { Module(mod) }) .then(binary => { Module(mod) })
} }
} return new Promise((resolve) => {
mod.onRuntimeInitialized = () => {
mod.onRuntimeInitialized = function () { const f = (exportedFuncs) => {
const f = function(exportedFuncs) { exportedFuncs.forEach(func => {
exportedFuncs.forEach(func => { capi[func.exportName] = mod.cwrap(func.name, func.returns, func.args)
capi[func.exportName] = mod.cwrap(func.name, func.returns, func.args) })
}) define_extra_functions(mod)
define_extra_functions(mod) capi.sheInit()
capi.sheInit() console.log(`initializing sheSetRangeForDLP(range=${range}, tryNum=${tryNum})`)
console.log('initializing sheSetRangeForDLP') const r = capi.sheSetRangeForDLP(range, tryNum)
const r = capi.sheSetRangeForDLP(g_range, g_tryNum) console.log('finished ' + r)
console.log('finished ' + r) resolve()
if (g_callback) g_callback() }
} if (isNodeJs) {
const EXPORTED_JSON = 'exported-she.json' const fs = require('fs')
if (isNodeJs) { const jsonStr = fs.readFileSync('./exported-she.json')
const fs = require('fs') f(JSON.parse(jsonStr))
const path = require('path') } else {
const jsonStr = fs.readFileSync(path.resolve(__dirname, EXPORTED_JSON)) fetch('exported-she.json')
f(JSON.parse(jsonStr)) .then(response => response.json())
} else { .then(exportedFuncs => f(exportedFuncs))
fetch(EXPORTED_JSON) }
.then(response => response.json()) }
.then(exportedFuncs => f(exportedFuncs)) })
}
} }
const ptrToStr = function(pos, n) { const ptrToStr = function(pos, n) {

Loading…
Cancel
Save