{"size": 1151, "ext": "js", "max_stars_repo_path": "combine.js", "max_stars_repo_name": "consento-org/shamirs-secret-sharing", "max_stars_repo_stars_event_min_datetime": "2021-01-02T16:28:37.000Z", "max_stars_repo_stars_event_max_datetime": "2021-01-02T16:28:37.000Z", "max_issues_repo_path": "combine.js", "max_issues_repo_name": "consento-org/shamirs-secret-sharing", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "combine.js", "max_forks_repo_name": "consento-org/shamirs-secret-sharing", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 22.568627451, "max_line_length": 59, "alphanum_fraction": 0.5690703736}
const { BIN_ENCODING } = require('./constants')
const { lagrange } = require('./lagrange')
const { parse } = require('./share')
const codec = require('./codec')
const { Buffer } = require('buffer')

/**
 * Reconstruct a secret from a distinct set of shares.
 * @public
 * @param {Array<String|Buffer>} shares
 * @return {Buffer}
 */
function combine(shares) {
  const chunks = []
  const x = []
  const y = []
  const t = shares.length

  for (let i = 0; i < t; ++i) {
    const share = parse(shares[i])

    if (-1 === x.indexOf(share.id)) {
      x.push(share.id)

      const bin = codec.bin(share.data, 16)
      const parts = codec.split(bin, 0, 2)

      for (let j = 0; j < parts.length; ++j) {
        if (!y[j]) { y[j] = [] }
        y[j][x.length - 1] = parts[j]
      }
    }
  }

  for (let i = 0; i < y.length; ++i) {
    const p = lagrange(0, [x, y[i]])
    chunks.unshift(codec.pad(p.toString(2)))
  }

  const string = chunks.join('')
  const bin = string.slice(1 + string.indexOf('1')) // >= 0
  const hex = codec.hex(bin, BIN_ENCODING)
  const value = codec.decode(hex)

  return Buffer.from(value)
}

module.exports = {
  combine
}
