square-and-multiply algorithm