You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.1 KiB

5 years ago
  1. ## Paillier cryptosystem & Homomorphic Addition
  2. - https://en.wikipedia.org/wiki/Paillier_cryptosystem
  3. - https://en.wikipedia.org/wiki/Homomorphic_encryption
  4. - [x] GenerateKeyPair
  5. - [x] Encrypt
  6. - [x] Decrypt
  7. - [x] Homomorphic Addition
  8. #### Usage
  9. - Encrypt, Decrypt
  10. ```go
  11. // key generation
  12. key, err := GenerateKeyPair()
  13. if err!=nil {
  14. fmt.Println(err)
  15. }
  16. mBytes := []byte("Hi")
  17. m := new(big.Int).SetBytes(mBytes)
  18. // encryption
  19. c := Encrypt(m, key.PubK)
  20. // decryption
  21. d := Decrypt(c, key.PubK, key.PrivK)
  22. if m == d {
  23. fmt.Println("ciphertext decrypted correctly")
  24. }
  25. ```
  26. - Homomorphic Addition
  27. ```go
  28. // key generation [Alice]
  29. key, err := GenerateKeyPair()
  30. if err!=nil {
  31. fmt.Println(err)
  32. }
  33. // define values [Alice]
  34. n1 := big.NewInt(int64(110))
  35. n2 := big.NewInt(int64(150))
  36. // encrypt values [Alice]
  37. c1 := Encrypt(n1, key.PubK)
  38. c2 := Encrypt(n2, key.PubK)
  39. // compute homomorphic addition [Bob]
  40. c3c4 := HomomorphicAddition(c1, c2, key.PubK)
  41. // decrypt the result [Alice]
  42. d := Decrypt(c3c4, key.PubK, key.PrivK)
  43. if !bytes.Equal(new(big.Int).Add(n1, n2).Bytes(), d.Bytes()) {
  44. fmt.Println("decrypted result not equal to expected result")
  45. }
  46. ```