Problem: fast_exponential

Your task is to implement the "Exponentiation by Squaring" Algorithm and verify its functional correctness in Dafny. Unlike the naive approach, this algorithm computes $b^e$ in $O(\log e)$ time using binary decomposition of the exponent. Preconditions: The input base b and exponent e are of type int and are non-negative. To ensure safety, you may assume that the mathematical result $b^e$ fits within the u64 range (i.e., $spec\_pow(b, e) \le \text{0xffff\_ffff\_ffff\_ffff}$). Requirements: Implement the function to compute the exponential which returns an int. (1) Functional Correctness: You must use the provided recursive ghost function spec_pow(b, e) and prove that your implementation returns exactly this value. (2) Safety: You must ensure the return value is non-negative. Verification Challenges: (1) Non-Linear Arithmetic Lemmas: The core logic relies on the algebraic identity $(b^2)^k = b^{2k}$. SMT solvers struggle with non-linear arithmetic. You must explicitly state and verify helper lemmas (using induction) to teach the solver that squaring the base compensates for halving the exponent. (2) Conservation Invariant: You must define and prove a sophisticated loop invariant. Unlike the naive case, the accumulator does not simply equal $b^i$. Instead, you must prove that at every step, $current\_res \times spec\_pow(current\_base, current\_exp) == spec\_pow(original\_base, original\_exp)$.

// Following is the block for necessary definitions
// <preamble>
// Pure mathematical definition of power
function spec_pow(b: nat, e: nat): nat
  decreases e
{
  if e == 0 then
    1
  else
    b * spec_pow(b, e - 1)
}
// </preamble>

// Following is the block for potential helper specifications
// <helpers>

// </helpers>

// Following is the block for proofs of lemmas
// <proofs>

// </proofs>

// Following is the block for the main specification
// <spec>
method exponentiation(b: int, e: int) returns (res: int)
  // Simulate u64 types (non-negative)
  requires b >= 0 
  requires e >= 0
  // Precondition: Result fits in u64 (0xffff_ffff_ffff_ffff)
  requires spec_pow(b, e) <= 0xffff_ffff_ffff_ffff
  
  ensures res == spec_pow(b, e)
  // Ensure return value preserves non-negativity
  ensures res >= 0
// </spec>
// <code>
{
  // Implement and verify the algorithm to compute the exponential
}
// </code>

method Main() {
  // Empty main
}