MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / modularExponentiation

Function modularExponentiation

cipher/diffiehellman/diffiehellmankeyexchange.go:28–45  ·  view source on GitHub ↗

r = (b^e)%mod

(b, e, mod int64)

Source from the content-addressed store, hash-verified

26
27// r = (b^e)%mod
28func modularExponentiation(b, e, mod int64) int64 {
29
30 //runs in O(log(n)) where n = e
31 //uses exponentiation by squaring to speed up the process
32 if mod == 1 {
33 return 0
34 }
35 var r int64 = 1
36 b = b % mod
37 for e > 0 {
38 if e&1 == 1 {
39 r = (r * b) % mod
40 }
41 e = e >> 1
42 b = (b * b) % mod
43 }
44 return r
45}

Callers 3

GenerateShareKeyFunction · 0.85
GenerateMutualKeyFunction · 0.85

Calls

no outgoing calls

Tested by 1