cryptofun/README.md
README.mdRaw
1# cryptofun [](https://goreportcard.com/report/github.com/arnaucube/cryptofun)
2
3Crypto algorithms from scratch. Academic purposes only.
4
5
6
7- [RSA cryptosystem & Blind signature & Homomorphic Multiplication](#rsa-cryptosystem--blind-signature--homomorphic-multiplication)
8- [Paillier cryptosystem & Homomorphic Addition](#paillier-cryptosystem--homomorphic-addition)
9- [Shamir Secret Sharing](#shamir-secret-sharing)
10- [Diffie-Hellman](#diffie-hellman)
11- [ECC](#ecc)
12- [ECC ElGamal](#ecc-elgamal)
13- [ECC ECDSA](#ecc-ecdsa)
14- [Schnorr signature](#schnorr-signature)
15- [Bn128](#bn128)
16
17---
18
19## RSA cryptosystem & Blind signature & Homomorphic Multiplication
20- https://en.wikipedia.org/wiki/RSA_(cryptosystem)#
21- https://en.wikipedia.org/wiki/Blind_signature
22- https://en.wikipedia.org/wiki/Homomorphic_encryption
23
24- [x] GenerateKeyPair
25- [x] Encrypt
26- [x] Decrypt
27- [x] Blind
28- [x] Blind Signature
29- [x] Unblind Signature
30- [x] Verify Signature
31- [x] Homomorphic Multiplication
32
33
34#### Usage
35- Key generation, Encryption, Decryption
36```go
37// generate key pair
38key, err := GenerateKeyPair()
39if err!=nil {
40 fmt.Println(err)
41}
42mBytes := []byte("Hi")
43m := new(big.Int).SetBytes(mBytes)
44
45// encrypt message
46c := Encrypt(m, key.PubK)
47
48// decrypt ciphertext
49d := Decrypt(c, key.PrivK)
50if m == d {
51 fmt.Println("correctly decrypted")
52}
53```
54
55- Blind signatures
56```go
57// key generation [Alice]
58key, err := GenerateKeyPair()
59if err!=nil {
60 fmt.Println(err)
61}
62
63// create new message [Alice]
64mBytes := []byte("Hi")
65m := new(big.Int).SetBytes(mBytes)
66
67// define r value [Alice]
68rVal := big.NewInt(int64(101))
69
70// blind message [Alice]
71mBlinded := Blind(m, rVal, key.PubK)
72
73// Blind Sign the blinded message [Bob]
74sigma := BlindSign(mBlinded, key.PrivK)
75
76// unblind the blinded signed message, and get the signature of the message [Alice]
77mSigned := Unblind(sigma, rVal, key.PubK)
78
79// verify the signature [Alice/Bob/Trudy]
80verified := Verify(m, mSigned, key.PubK)
81if !verified {
82 fmt.Println("signature could not be verified")
83}
84```
85
86- Homomorphic Multiplication
87```go
88// key generation [Alice]
89key, err := GenerateKeyPair()
90if err!=nil {
91 fmt.Println(err)
92}
93
94// define values [Alice]
95n1 := big.NewInt(int64(11))
96n2 := big.NewInt(int64(15))
97
98// encrypt the values [Alice]
99c1 := Encrypt(n1, key.PubK)
100c2 := Encrypt(n2, key.PubK)
101
102// compute homomorphic multiplication with the encrypted values [Bob]
103c3c4 := HomomorphicMul(c1, c2, key.PubK)
104
105// decrypt the result [Alice]
106d := Decrypt(c3c4, key.PrivK)
107
108// check that the result is the expected
109if !bytes.Equal(new(big.Int).Mul(n1, n2).Bytes(), d.Bytes()) {
110 fmt.Println("decrypted result not equal to expected result")
111}
112```
113
114## Paillier cryptosystem & Homomorphic Addition
115- https://en.wikipedia.org/wiki/Paillier_cryptosystem
116- https://en.wikipedia.org/wiki/Homomorphic_encryption
117
118- [x] GenerateKeyPair
119- [x] Encrypt
120- [x] Decrypt
121- [x] Homomorphic Addition
122
123#### Usage
124- Encrypt, Decrypt
125```go
126// key generation
127key, err := GenerateKeyPair()
128if err!=nil {
129 fmt.Println(err)
130}
131
132mBytes := []byte("Hi")
133m := new(big.Int).SetBytes(mBytes)
134
135// encryption
136c := Encrypt(m, key.PubK)
137
138// decryption
139d := Decrypt(c, key.PubK, key.PrivK)
140if m == d {
141 fmt.Println("ciphertext decrypted correctly")
142}
143```
144
145- Homomorphic Addition
146```go
147// key generation [Alice]
148key, err := GenerateKeyPair()
149if err!=nil {
150 fmt.Println(err)
151}
152
153// define values [Alice]
154n1 := big.NewInt(int64(110))
155n2 := big.NewInt(int64(150))
156
157// encrypt values [Alice]
158c1 := Encrypt(n1, key.PubK)
159c2 := Encrypt(n2, key.PubK)
160
161// compute homomorphic addition [Bob]
162c3c4 := HomomorphicAddition(c1, c2, key.PubK)
163
164// decrypt the result [Alice]
165d := Decrypt(c3c4, key.PubK, key.PrivK)
166if !bytes.Equal(new(big.Int).Add(n1, n2).Bytes(), d.Bytes()) {
167 fmt.Println("decrypted result not equal to expected result")
168}
169```
170
171
172## Shamir Secret Sharing
173- https://en.wikipedia.org/wiki/Shamir%27s_Secret_Sharing
174
175- [x] create secret sharing from number of secrets needed, number of shares, random point p, secret to share
176- [x] Lagrange Interpolation to restore the secret from the shares
177
178#### Usage
179```go
180// define secret to share
181k := 123456789
182
183// define random prime
184p, err := rand.Prime(rand.Reader, bits/2)
185if err!=nil {
186 fmt.Println(err)
187}
188
189// define how many shares want to generate
190nShares := big.NewInt(int64(6))
191
192// define how many shares are needed to recover the secret
193nNeededShares := big.NewInt(int64(3))
194
195// create the shares
196shares, err := Create(
197 nNeededShares,
198 nShares,
199 p,
200 big.NewInt(int64(k)))
201assert.Nil(t, err)
202if err!=nil {
203 fmt.Println(err)
204}
205
206// select shares to use
207var sharesToUse [][]*big.Int
208sharesToUse = append(sharesToUse, shares[2])
209sharesToUse = append(sharesToUse, shares[1])
210sharesToUse = append(sharesToUse, shares[0])
211
212// recover the secret using Lagrange Interpolation
213secr := LagrangeInterpolation(sharesToUse, p)
214
215// check that the restored secret matches the original secret
216if !bytes.Equal(k.Bytes(), secr.Bytes()) {
217 fmt.Println("reconstructed secret not correspond to original secret")
218}
219```
220
221## Diffie-Hellman
222- https://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange
223
224- [x] key exchange
225
226## ECC
227- https://en.wikipedia.org/wiki/Elliptic-curve_cryptography
228
229- [x] define elliptic curve
230- [x] get point at X
231- [x] get order of a Point on the elliptic curve
232- [x] Add two points on the elliptic curve
233- [x] Multiply a point n times on the elliptic curve
234
235#### Usage
236- ECC basic operations
237```go
238// define new ec
239ec := NewEC(big.NewInt(int64(0)), big.NewInt(int64(7)), big.NewInt(int64(11)))
240
241// define two points over the curve
242p1 := Point{big.NewInt(int64(4)), big.NewInt(int64(7))}
243p2 := Point{big.NewInt(int64(2)), big.NewInt(int64(2))}
244
245// add the two points
246q, err := ec.Add(p1, p2)
247if err!=nil {
248 fmt.Println(err)
249}
250
251// multiply the two points
252q, err := ec.Mul(p, big.NewInt(int64(1)))
253if err!=nil {
254 fmt.Println(err)
255}
256
257// get order of a generator point over the elliptic curve
258g := Point{big.NewInt(int64(7)), big.NewInt(int64(8))}
259order, err := ec.Order(g)
260if err!=nil {
261 fmt.Println(err)
262}
263```
264
265
266
267
268## ECC ElGamal
269- https://en.wikipedia.org/wiki/ElGamal_encryption
270
271- [x] ECC ElGamal key generation
272- [x] ECC ElGamal Encrypton
273- [x] ECC ElGamal Decryption
274
275
276#### Usage
277- NewEG, Encryption, Decryption
278```go
279// define new elliptic curve
280ec := ecc.NewEC(big.NewInt(int64(1)), big.NewInt(int64(18)), big.NewInt(int64(19)))
281
282// define new point
283g := ecc.Point{big.NewInt(int64(7)), big.NewInt(int64(11))}
284
285// define new ElGamal crypto system with the elliptic curve and the point
286eg, err := NewEG(ec, g)
287if err!=nil {
288 fmt.Println(err)
289}
290
291// define privK&pubK over the elliptic curve
292privK := big.NewInt(int64(5))
293pubK, err := eg.PubK(privK)
294if err!=nil {
295 fmt.Println(err)
296}
297
298// define point to encrypt
299m := ecc.Point{big.NewInt(int64(11)), big.NewInt(int64(12))}
300
301// encrypt
302c, err := eg.Encrypt(m, pubK, big.NewInt(int64(15)))
303if err!=nil {
304 fmt.Println(err)
305}
306
307// decrypt
308d, err := eg.Decrypt(c, privK)
309if err!=nil {
310 fmt.Println(err)
311}
312
313// check that decryption is correct
314if !m.Equal(d) {
315 fmt.Println("decrypted not equal to original")
316}
317```
318
319
320
321## ECC ECDSA
322- https://en.wikipedia.org/wiki/Elliptic_Curve_Digital_Signature_Algorithm
323
324- [x] define ECDSA data structure
325- [x] ECDSA Sign
326- [x] ECDSA Verify signature
327
328
329#### Usage
330```go
331// define new elliptic curve
332ec := ecc.NewEC(big.NewInt(int64(1)), big.NewInt(int64(18)), big.NewInt(int64(19)))
333// define new point
334g := ecc.Point{big.NewInt(int64(7)), big.NewInt(int64(11))}
335
336// define new ECDSA system
337dsa, err := NewDSA(ec, g)
338if err!=nil {
339 fmt.Println(err)
340}
341
342// define privK&pubK over the elliptic curve
343privK := big.NewInt(int64(5))
344pubK, err := dsa.PubK(privK)
345if err!=nil {
346 fmt.Println(err)
347}
348
349// hash value to sign
350hashval := big.NewInt(int64(40))
351
352// define r
353r := big.NewInt(int64(11))
354
355// sign hashed value
356sig, err := dsa.Sign(hashval, privK, r)
357if err!=nil {
358 fmt.Println(err)
359}
360
361// verify signature
362verified, err := dsa.Verify(hashval, sig, pubK)
363if err!=nil {
364 fmt.Println(err)
365}
366if verified {
367 fmt.Println("signature correctly verified")
368}
369```
370
371## Schnorr signature
372- https://en.wikipedia.org/wiki/Schnorr_signature
373
374- [x] Hash[M || R] (where M is the msg bytes and R is a Point on the ECC, using sha256 hash function)
375- [x] Generate Schnorr scheme
376- [x] Sign
377- [x] Verify signature
378
379
380#### Usage
381```go
382// define new elliptic curve
383ec := ecc.NewEC(big.NewInt(int64(0)), big.NewInt(int64(7)), big.NewInt(int64(11)))
384// define new point
385g := ecc.Point{big.NewInt(int64(11)), big.NewInt(int64(27))} // Generator
386// define new random r
387r := big.NewInt(int64(23)) // random r
388
389// define new Schnorr crypto system using the values
390schnorr, sk, err := Gen(ec, g, r)
391if err!=nil {
392 fmt.println(err)
393}
394
395// define message to sign
396m := []byte("hola")
397
398// also we can hash the message, but it's not mandatory, as it will be done inside the schnorr.Sign, but we can perform it now, just to check the function
399h := Hash([]byte("hola"), c)
400if h.String() != "34719153732582497359642109898768696927847420320548121616059449972754491425079") {
401 fmt.Println("not correctly hashed")
402}
403
404s, rPoint, err := schnorr.Sign(sk, m)
405if err!=nil {
406 fmt.println(err)
407}
408
409// verify Schnorr signature
410verified, err := Verify(schnorr.EC, sk.PubK, m, s, rPoint)
411if err!=nil {
412 fmt.println(err)
413}
414if verified {
415 fmt.Println("Schnorr signature correctly verified")
416}
417```
418
419
420
421## Bn128
422Implementation of the bn128 pairing.
423Code moved to https://github.com/arnaucube/bn128
424
425
426---
427
428To run all tests:
429```
430go test ./... -v
431```