title: Modular Exponentiation in Java
body: <p>I need a way to calculate:</p>
  
  <pre><code>(g^u * y^v) mod p
  </code></pre>
  
  <p>in Java.</p>
  
  <p>I've found this algorithm for calculating (g^u) mod p:</p>
  
  <pre><code>int modulo(int a,int b,int c) {
      long x=1
      long y=a;
      while(b &gt; 0){
          if(b%2 == 1){
              x=(x*y)%c;
          }
          y = (y*y)%c; // squaring the base
          b /= 2;
      }
      return (int) x%c;
  }
  </code></pre>
  
  <p>and it works great, but I can't seem to find a way to do this for</p>
  
  <pre><code>(g^u * y^v) mod p
  </code></pre>
  
  <p>as my math skills are lackluster.</p>
  
  <p>To put it in context, it's for a java implementation of a "reduced" DSA - the verifying part requires this to be solved.</p>