///|
/// WireGuard `Reject-After-Messages` ceiling (`2^64 - 2^13 - 1`, WireGuard ยง6.1).
pub const REJECT_AFTER_MESSAGES : UInt64 = 0xffffffffffffdfffUL

///|
/// WireGuard anti-replay sliding window width in packets (RFC 6479 / WireGuard `COUNTER_WINDOW_SIZE = 2048`).
pub const WINDOW_SIZE : UInt64 = 2048UL

///|
const RING_BLOCKS : UInt64 = 33UL

///|
/// RFC 6479 2048-packet anti-replay window without bit-shifting across words.
/// Separates read-only pre-decryption `check(counter)` from post-AEAD-authentication `accept(counter)`
/// so unauthenticated packets can never advance or poison the window.
#derive(Debug, Eq)
pub struct ReplayWindow {
  priv ring : FixedArray[UInt64]
  priv mut highest : UInt64
  priv mut initialized : Bool
}

///|
/// Create a fresh empty anti-replay window.
pub fn ReplayWindow::new() -> ReplayWindow {
  { ring: FixedArray::make(33, 0UL), highest: 0UL, initialized: false, }
}

///|
/// Reset the anti-replay window (e.g. when rotating to a new session).
pub fn ReplayWindow::reset(self : ReplayWindow) -> Unit {
  for i in 0..<33 {
    self.ring[i] = 0UL
  }
  self.highest = 0UL
  self.initialized = false
}

///|
/// Highest authenticated packet counter observed so far (`None` if no packet accepted yet).
pub fn ReplayWindow::highest_counter(self : ReplayWindow) -> UInt64? {
  if self.initialized {
    Some(self.highest)
  } else {
    None
  }
}

///|
/// Read-only pre-check before AEAD decryption: returns `true` iff `counter` is within valid bounds and not yet marked seen.
pub fn ReplayWindow::check(self : ReplayWindow, counter : UInt64) -> Bool {
  if counter >= REJECT_AFTER_MESSAGES {
    false
  } else if !self.initialized {
    true
  } else if counter > self.highest {
    true
  } else {
    let diff = self.highest - counter
    if diff >= WINDOW_SIZE {
      false
    } else {
      let blk = ((counter >> 6) % RING_BLOCKS).to_int()
      let bit = (counter & 63UL).to_int()
      ((self.ring[blk] >> bit) & 1UL) == 0UL
    }
  }
}

///|
/// Record `counter` as seen AFTER AEAD authentication succeeds.
/// Returns `true` if newly accepted, or `false` if replayed / out-of-window / above ceiling.
pub fn ReplayWindow::accept(self : ReplayWindow, counter : UInt64) -> Bool {
  if !self.check(counter) {
    false
  } else {
    let blk = ((counter >> 6) % RING_BLOCKS).to_int()
    let bit = (counter & 63UL).to_int()
    let mask = 1UL << bit
    if !self.initialized {
      for i in 0..<33 {
        self.ring[i] = 0UL
      }
      self.ring[blk] = mask
      self.highest = counter
      self.initialized = true
      true
    } else if counter > self.highest {
      let old_blk = self.highest >> 6
      let new_blk = counter >> 6
      let span = new_blk - old_blk
      if span >= RING_BLOCKS {
        for i in 0..<33 {
          self.ring[i] = 0UL
        }
      } else {
        let mut b = old_blk + 1UL
        while b <= new_blk {
          self.ring[(b % RING_BLOCKS).to_int()] = 0UL
          b = b + 1UL
        }
      }
      self.ring[blk] = self.ring[blk] | mask
      self.highest = counter
      true
    } else {
      self.ring[blk] = self.ring[blk] | mask
      true
    }
  }
}