var sodium = require('chloride')
var scalarmult = sodium.crypto_scalarmult
var box  = sodium.crypto_box_easy
var secretbox = sodium.crypto_secretbox_easy
var secretbox_open = sodium.crypto_secretbox_open_easy
var keypair = sodium.crypto_box_keypair
var concat = Buffer.concat

function randombytes(n) {
  var b = new Buffer(n)
  sodium.randombytes(b)
  return b
}

function setMax (m) {
  m = m || DEFAULT_MAX
  if (m < 1 || m > 255)
    throw new Error('max recipients must be between 0 and 255.')
  return m
}


const DEFAULT_MAX = 7

exports.encrypt =
exports.multibox = function (msg, recipients, max) {

  max = setMax(max)

  if(recipients.length > max)
    throw new Error('max recipients is:'+max+' found:'+recipients.length)

  var nonce = randombytes(24)
  var key = randombytes(32)
  var onetime = keypair()

  var _key = concat([new Buffer([recipients.length & max]), key])
  return concat([
    nonce,
    onetime.publicKey,
    concat(recipients.map(function (r_pk, i) {
      return secretbox(_key, nonce, scalarmult(onetime.secretKey, r_pk))
    })),
    secretbox(msg, nonce, key)
  ])
}

function get_key(ctxt, my_key) {

}

exports.multibox_open_key = function (ctxt, sk, max) { //, groups...

  max = setMax(max)

  var nonce = ctxt.slice(0, 24)
  var onetime_pk = ctxt.slice(24, 24+32)
  var my_key = scalarmult(sk, onetime_pk)
  var _key, key, length, start = 24+32, size = 32+1+16
  for(var i = 0; i <= max; i++) {
    var s = start+size*i
    if(s + size > (ctxt.length - 16)) continue
    _key = secretbox_open(ctxt.slice(s, s + size), nonce, my_key)
    if(_key) return _key
  }

  if(!key) return
  return secretbox_open(ctxt.slice(start+length*size), nonce, key)
}

exports.multibox_open_body = function (ctxt, _key) { //, groups...
  if(!_key) return
  var key = _key.slice(1)
  var length = _key[0]
  var start = 24+32, size = 32+1+16
  var nonce = ctxt.slice(0, 24)
  return secretbox_open(ctxt.slice(start+length*size), nonce, key)
}

exports.decrypt =
exports.multibox_open = function (ctxt, sk, max) { //, groups...
  var _key = exports.multibox_open_key(ctxt, sk, max)
  if(_key) return exports.multibox_open_body(ctxt, _key)
}



@@ -815,24 +815,34 @@
 ir()%0A%0A  var 
+length_and
 _key = conca
@@ -877,14 +877,8 @@
 ngth
- & max
 %5D), 
@@ -1005,16 +1005,26 @@
 cretbox(
+length_and
 _key, no
@@ -1117,45 +1117,8 @@
 %0A%7D%0A%0A
-function get_key(ctxt, my_key) %7B%0A%0A%7D%0A%0A
 expo
@@ -1321,24 +1321,34 @@
 e_pk)%0A  var 
+length_and
 _key, key, l
@@ -1485,21 +1485,34 @@
 6)) 
-continue%0A    
+return null%0A    length_and
 _key
@@ -1580,115 +1580,49 @@
  if(
-_key) return _key%0A  %7D%0A%0A  if(!key) return%0A  return secretbox_open(ctxt.slice(start+length*size), nonce, key)
+length_and_key) return length_and_key%0A  %7D
 %0A%7D%0A%0A
@@ -1662,24 +1662,34 @@
 tion (ctxt, 
+length_and
 _key) %7B //, 
@@ -1704,16 +1704,26 @@
 .%0A  if(!
+length_and
 _key) re
@@ -1739,16 +1739,26 @@
 r key = 
+length_and
 _key.sli
@@ -1778,16 +1778,26 @@
 ength = 
+length_and
 _key%5B0%5D%0A
@@ -2130,10 +2130,8 @@
 _key)%0A%7D%0A
-%0A%0A

clearer names, remove dead code