
var c = require('../crypto')

var tape = require('tape')

var sodium      = require('chloride')

var keypair     = sodium.crypto_box_seed_keypair
var from_seed   = sodium.crypto_sign_seed_keypair
var shared      = sodium.crypto_scalarmult
var hash        = sodium.crypto_hash_sha256
var sign        = sodium.crypto_sign_detached
var verify      = sodium.crypto_sign_verify_detached
var auth        = sodium.crypto_auth
var verify_auth = sodium.crypto_auth_verify
var curvify_pk  = sodium.crypto_sign_ed25519_pk_to_curve25519
var curvify_sk  = sodium.crypto_sign_ed25519_sk_to_curve25519
var box         = sodium.crypto_secretbox_easy
var unbox       = sodium.crypto_secretbox_open_easy


var low_order = [
  '0000000000000000000000000000000000000000000000000000000000000000',
  '0100000000000000000000000000000000000000000000000000000000000000'
].map(function (e) {
  return Buffer.from(e, 'hex')
})


var app_key = hash(Buffer.from('test app key', 'utf8'))

low_order.map(function (v, i) {

  tape('test low order keys:'+i, function (t) {

    var kx_pk = low_order[i]
    var kx_sk = low_order[i]

    //evil client state
    var _state = {
      app_key: app_key,
      local: {
        kx_pk: kx_pk,
        kx_sk: kx_sk,
        publicKey: low_order[i],
        secretKey: low_order[i],
        app_mac: auth(kx_pk, app_key)
      },
      remote: {}
    }

    var challenge = c.createChallenge(_state)


    //honest server state
    var state = c.initialize({
        app_key: app_key,
        seed: hash(Buffer.from('honest server seed', 'utf8')),
        random: hash(Buffer.from('honest server random', 'utf8'))
      })


    t.throws(function () {
      try {
      console.log(
        c.verifyChallenge(state, challenge)
      )
      } catch(err) {
        console.error(err.stack)
        throw err
      }
    })


    t.end()


  })

})


