// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

import "poly1305.cry" as poly1305;

print "Proving Poly1305 spec...";
prove_print abc {{ poly1305::allTestsPass }};

print "Loading LLVM bitcode...";
m <- llvm_load_module "../../../dist/Debug/lib/libfreeblpriv3.so.bc";

let SpecPoly1305 n = do {
  llvm_ptr "out" (llvm_array 16 (llvm_int 8));
  out <- llvm_var "*out" (llvm_array 16 (llvm_int 8));

  llvm_ptr "ad" (llvm_array 16 (llvm_int 8));
  ad <- llvm_var "*ad" (llvm_array 16 (llvm_int 8));

  adLen <- llvm_var "adLen" (llvm_int 32);

  llvm_ptr "ciphertext" (llvm_array n (llvm_int 8));
  ciphertext <- llvm_var "*ciphertext" (llvm_array n (llvm_int 8));

  ciphertextLen <- llvm_var "ciphertextLen" (llvm_int 32);

  llvm_ptr "key" (llvm_array 32 (llvm_int 8));
  key <- llvm_var "*key" (llvm_array 32 (llvm_int 8));

  llvm_assert_eq "*ad" {{ zero : [16][8] }};
  llvm_assert_eq "adLen" {{ 16 : [32] }};

  llvm_assert_eq "*ciphertext" {{ zero : [n][8] }};
  llvm_assert_eq "ciphertextLen" {{ `n : [32] }};

  llvm_assert_eq "*key" {{ zero : [32][8] }};

  let res = {{ poly1305::Poly1305 (ad # ciphertext # [16, 0, 0, 0, 0, 0, 0, 0] # [`n, 0, 0, 0, 0, 0, 0, 0]) (take`{16} key) (drop`{16} key) }};
  llvm_ensure_eq "*out" {{ res }};

  llvm_verify_tactic abc;
};

print "Proving equality for a single block...";
// This is currently disabled as it takes way too long. We need to help Z3
// prove this before we can enable it on Taskcluster.
//time (llvm_verify m "Poly1305Do" [] (SpecPoly1305 16));