Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1.  
  2. uint32_t mul_mod(uint32_t a, uint32_t b, uint32_t m) {
  3. uint32_t result = 0;
  4. uint32_t x = b % m;
  5.  
  6. for (int i = 0; i < 32; ++i) {
  7. uint32_t myBit = 31 - i;
  8. //~ Serial.print(i);
  9. //~ Serial.print(": ");
  10. //~ Serial.println(((a << myBit) >> 31));
  11. if (((a << myBit) >> 31) == 1) {
  12. result = (result + x) % m;
  13. }
  14. x = (x << 1) % m;
  15. }
  16.  
  17. return result;
  18. }
  19.  
  20. uint32_t pow_mod(uint32_t base, uint32_t power, uint32_t m) {
  21. uint32_t result = 1;
  22. uint32_t x = base % m;
  23. for (int i = 0; i < 32; ++i ) {
  24. if ((power & (1UL << i)) != 0) {
  25. result = mul_mod(result, x, m);
  26. }
  27. x = mul_mod(x, x, m);
  28. }
  29. return result;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement