using repeated square-and-multiply