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.

94 lines
2.0 KiB

5 years ago
  1. ## RSA cryptosystem & Blind signature & Homomorphic Multiplication
  2. - https://en.wikipedia.org/wiki/RSA_(cryptosystem)#
  3. - https://en.wikipedia.org/wiki/Blind_signature
  4. - https://en.wikipedia.org/wiki/Homomorphic_encryption
  5. - [x] GenerateKeyPair
  6. - [x] Encrypt
  7. - [x] Decrypt
  8. - [x] Blind
  9. - [x] Blind Signature
  10. - [x] Unblind Signature
  11. - [x] Verify Signature
  12. - [x] Homomorphic Multiplication
  13. #### Usage
  14. - Key generation, Encryption, Decryption
  15. ```go
  16. // generate key pair
  17. key, err := GenerateKeyPair()
  18. if err!=nil {
  19. fmt.Println(err)
  20. }
  21. mBytes := []byte("Hi")
  22. m := new(big.Int).SetBytes(mBytes)
  23. // encrypt message
  24. c := Encrypt(m, key.PubK)
  25. // decrypt ciphertext
  26. d := Decrypt(c, key.PrivK)
  27. if m == d {
  28. fmt.Println("correctly decrypted")
  29. }
  30. ```
  31. - Blind signatures
  32. ```go
  33. // key generation [Alice]
  34. key, err := GenerateKeyPair()
  35. if err!=nil {
  36. fmt.Println(err)
  37. }
  38. // create new message [Alice]
  39. mBytes := []byte("Hi")
  40. m := new(big.Int).SetBytes(mBytes)
  41. // define r value [Alice]
  42. rVal := big.NewInt(int64(101))
  43. // blind message [Alice]
  44. mBlinded := Blind(m, rVal, key.PubK)
  45. // Blind Sign the blinded message [Bob]
  46. sigma := BlindSign(mBlinded, key.PrivK)
  47. // unblind the blinded signed message, and get the signature of the message [Alice]
  48. mSigned := Unblind(sigma, rVal, key.PubK)
  49. // verify the signature [Alice/Bob/Trudy]
  50. verified := Verify(m, mSigned, key.PubK)
  51. if !verified {
  52. fmt.Println("signature could not be verified")
  53. }
  54. ```
  55. - Homomorphic Multiplication
  56. ```go
  57. // key generation [Alice]
  58. key, err := GenerateKeyPair()
  59. if err!=nil {
  60. fmt.Println(err)
  61. }
  62. // define values [Alice]
  63. n1 := big.NewInt(int64(11))
  64. n2 := big.NewInt(int64(15))
  65. // encrypt the values [Alice]
  66. c1 := Encrypt(n1, key.PubK)
  67. c2 := Encrypt(n2, key.PubK)
  68. // compute homomorphic multiplication with the encrypted values [Bob]
  69. c3c4 := HomomorphicMul(c1, c2, key.PubK)
  70. // decrypt the result [Alice]
  71. d := Decrypt(c3c4, key.PrivK)
  72. // check that the result is the expected
  73. if !bytes.Equal(new(big.Int).Mul(n1, n2).Bytes(), d.Bytes()) {
  74. fmt.Println("decrypted result not equal to expected result")
  75. }
  76. ```