/**
  * Generate a nonce based on the given $max
  *
  * {@inheritDoc}
  * @see \Mdanter\Ecc\Random\RandomNumberGeneratorInterface::generate()
  */
 public function generate($max)
 {
     if (is_null($this->result)) {
         $v = NumberSize::getCeiledByteSize($this->math, $max);
         while (true) {
             $hex = bin2hex($this->bytes($v));
             $rand = $this->math->hexDec($hex);
             // Check k is between [1, ... $max]
             if ($this->math->cmp(1, $rand) <= 0 && $this->math->cmp($rand, $max) < 0) {
                 break;
             }
             // Otherwise derive another and try again.
             $this->update(chr(0));
         }
         $this->result = $rand;
     }
     return $this->result;
 }