Question: Is there any freely available verilog implementaion of Diffie-Hellman key exchange?

I couldn't find anything using google. So, assuming its not there I started implementing on my own.
The code is hosted at <https://github.com/nulpoet/verilog_diffiehellman>.
Behavioral simulation in xilinx works.
However it cannot synthesize the remainder(%) operator.

So, I does anybody know how to design a module for remainder operation in verilog that computes the remainder in a single clock cycle? 
Any links to literature discussing the algorithm would suffice.

Solution to any of the above two question would solve my problem.  

Thanks in advance!

Answer: 
Slight revision based on Paulo's remark in the comments - in a public key system a [chosen *plaintext* attack](https://en.wikipedia.org/wiki/Chosen-plaintext_attack) is pretty much part of the design - arbitrary plaintexts can be encrypted to produce ciphertexts at will - by design, however, these shouldn't give any information that will allow you to deduce the private key.

A [chosen *ciphertext* attack](https://en.wikipedia.org/wiki/Chosen-ciphertext_attack) can be used with careful selection of the plaintext, however, to perform an attack - it's actually fairly straightforward on textbook RSA. Firstly, we have a piece of ciphertext we'll denote by:

$$C = t^e \mod n$$
Which is RSA as we know and love. Now, Eve has $C$ - this is perfectly ordinary, since Eve is supposed to be able to see $C$. Now eve has the ability to chose a plaintext - so, she choses $2$ as her plaintext and computes $C\_a = 2^e \mod n$. However, to our unsuspecting victim she sends $C\_b = C\_a \* C$, so:

$$C\_b = C\cdot 2^e = t^e 2^e\mod n$$

All good so far. Now since this is a chosen plaintext attack we're reliant on having access to the decryption of our substituted value - so now the unsuspecting victim computes:

$$(C\_b)^d = [t^e 2^e]^d = (t^e)^d \cdot (2^e)^d \mod n$$

However for any plaintext $x$, we know $C \equiv x^e$ and $x \equiv C^d$; so: $$(x^e)^d \equiv x \mod n$$ 

Therefore from the preceding result: $$(C\_b)^d = t \cdot 2 \mod n$$

Hence when we get that value back, we can quite easily compute $t$. We simply need to halve it.

This can actually apply for any plaintext we wish to chose - I simply chose $2$ because I like it as a number. [This paper](http://www.dtc.umn.edu/~odlyzko/doc/arch/rsa.attack.pdf) covers the general technique and a lot more.

This **only applies** to textbook RSA. The proper application of RSA in the wild involves the use of padding schemes which defeat this attack by ensuring the ciphertext is not malleable in this way.
A chosen plaintext attack means that the attacker, Eve, can encrypt the plaintext of her choice. Since in public key cryptosystems this is always possible, Eve can always acquire Bob's encryption exponent $e$ and encrypt any plaintext. What does this mean when the plaintext space is small?

Say Alice and Bob decide on textbook RSA and they generate the needed public and private keys. Alice sends Bob her age. Eve gets the message $m$ (Alice's age) as $m^e \mod N$. Eve knows $m^e$ decrypts to an age (plaintext space is small). Eve could launch a chosen plaintext attack by encrypting numbers 1-200 and matching it with the cipher text $m^e$.

Here is a [SageMath](https://www.sagemath.org/) script that shows this attack on textbook RSA.

```
p,q = next_prime(2^50), next_prime(2^30)
N = p*q
phi = ((p-1)*(q-1))
e = randint(0, phi)
while gcd(e, phi) != 1:
    e = randint(0, phi)
e = Integers(phi)(e)
d = e^(-1)
print('p = {}\nq = {}\nN = {}\nphi(N) = {}\n'.format(p, q, N, phi))
print('Bob\'s private key: d = {}, public key: e = {}'.format(d, e))

# BLOCK: Alice sends Bob a message
m = Integers(N)(39)
c = m^e
print('Alice sends m = {} to Bob as m^e = {}'.format(m, c))

# BLOCK: Bob decrypts c
print('Bob decrypts c = {} to m = c^d = {}'.format(c, c^d))

# BLOCK: Chosen Plaintext Attack on Textbook RSA
plaintext_space = range(200)
for t in plaintext_space:
    t = Integers(N)(t)
    if t^e == c:
        print('Eve has found the message: {}'.format(t))

```

Here is the Output:

```
 ---- Bob computes his public and private key ---- 
p = 1125899906842679
q = 1073741827
N = 1208925822992387951034533
phi(N) = 1208925821866486970450028

Bob's private key: d = 663674456840375780568965, public key: e = 761705651416514555276069
 ---- Alice sends Bob a message ---- 
Alice sends m = 39 to Bob as m^e = 25719553046482356303827
 ---- Chosen Plaintext Attack on Textbook RSA ---- 
Eve has found the message: 39

```