Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

343 righe
12 KiB

  1. package sidh
  2. import (
  3. "bytes"
  4. "crypto/rand"
  5. "encoding/hex"
  6. "testing"
  7. )
  8. const (
  9. // PrA - Alice's Private Key: 2*randint(0,2^371)
  10. PrA = "C09957CC83045FB4C3726384D784476ACB6FFD92E5B15B3C2D451BA063F1BD4CED8FBCF682A98DD0954D3" +
  11. "7BCAF730E00"
  12. // PrB - Bob's Private Key: 3*randint(0,3^238)
  13. PrB = "393E8510E78A16D2DC1AACA9C9D17E7E78DB630881D8599C7040D05BB5557ECAE8165C45D5366ECB37B00" +
  14. "969740AF201"
  15. PkA = "74D8EF08CB74EC99BF08B6FBE4FB3D048873B67F018E44988B9D70C564D058401D20E093C7DF0C66F022C" +
  16. "823E5139D2EA0EE137804B4820E950B046A90B0597759A0B6A197C56270128EA089FA1A2007DDE3430B37" +
  17. "A3E6350BD47B7F513863741C125FA63DEDAFC475C13DB59E533055B7CBE4B2F32672DF2DF97E03E29617B" +
  18. "0E9B6A35B58ABB26527A721142701EB147C7050E1D9125DA577B08CD51C8BB50627B8B47FACFC9C7C07DD" +
  19. "00DD75115DD83719FD5F96115DED23ECAA50B1044C6BF3F27442DA284BA4A272D850F414FB185801BF2EF" +
  20. "7E628EDB5643E35694B992CF30A2C5120CAF9434F09ACFCA3645B3FFC3A308901FAC7B8955FD5C98576AE" +
  21. "FD03F5806CB7430F75B3431B75BEC080596ABCA26E637E6E8D4C25175A8C052C9CBE77900A863F83FAB00" +
  22. "95B32D9C3858EF8A35B9F163D429E71DBA47539EB4791D117FE39DDE94EA7801A42DB12D84DE4740ACF51" +
  23. "CD7C32BB854569D7D94E11E69D9663CC7ED02E78CF48F4069DF3D3E86198B307095C6B11D46C0DC849F9D" +
  24. "94C7693209E5B3848AFAA6DA6A8D73362D779CBC43515902ED2BCE3A748C537DE2FCF092FD3E91B790AF5" +
  25. "4E1092C5E5B89BE5BE23B955A52F769D97277EF69F820109042F28C316AC90AE69EB374C9280300B816E6" +
  26. "2494B2E01072D1CA96E4B284D2BE1368D6969744B614FACBC8C165864E26E33481D4FDC47B6E523954A25" +
  27. "C1A096A37CD23FB81AE64FB11BD0A439609F1CE40673B06DD96F698A910E935219D840F3D411EDFB00D98" +
  28. "065AB9868C32D3DA05FF415"
  29. PkB = "F6C260C4141E418457CB442E11F0F5558375437576E55D211D19EF83E2839E51D07A82765D8E7B6366FA7" +
  30. "0B56CDE3AD3B629ACF542A433369496EDA51EDFBE16EFA1B8DEE1CE46B37820ECBD0CD674AACD4F21FABC" +
  31. "2436651E3AF604356FF3EB2CA87976890E34A56FAEC9A2ACD9559B1BB67B69AC1A521342E1E787DA5D709" +
  32. "32B0F5842ECA1C99B269DB6C2ED8397F0FC49F114CF8B5AF327A698C0251575CDD1D67732668109A91A3B" +
  33. "FA5B47D413C7FAB8817FCBEBFE9BDD9C0B1F3B1934A7028A65233E8B58A92E7E9F66B68B2057ECBF7E44A" +
  34. "0EF6EFCC3C8AA5414E100FA0C24F7545324AD17062FC11377A2A4749DEE27E192460E099DBDA8E840EA11" +
  35. "AD9D5C83DF065AF77030E7FE18CE24CFC71D356B9B9601811B93676C12CB6B41747133D5259E7A20CC065" +
  36. "FAB99DF944FDB34ABB9A374F9E9CC8F9C186BD2181DC2771F69C02629C3E4801A7E7C21F6F3CFF7D257E2" +
  37. "257C88C015F0CC8DC0E7FB3373CF4ED6A786AB329E7F16895CA147AD91F6EAE1DFE38116580DF52381599" +
  38. "E4246278CB1848FE4A56ABF98652E9E7C2E681551A3D78FA033D932087D8B6567D779A56B726B153033D7" +
  39. "2231A1B5C16ED7DC4458308D6B64AF6723CC0F52C94E04C58FCA9739E890AA40CC05E22321F10129D2B59" +
  40. "1F317102034C109A56D711591E5B44C717CFC9C9B9461894767CAFA42D2B394194B03999C2A9EF48868F3" +
  41. "FB03D1A40F596613AF97F4ED7643A1C2D12692E959C6DEB8E72403ADC0E42204DBCE5056EEF0CC60B0C6E" +
  42. "83B8B55AC01F6C85644EE49"
  43. )
  44. var params *SidhParams
  45. // Use init() function to initialize params in order to avoid
  46. // static initialization order fiasco.
  47. func init() {
  48. params = Params(FP_751)
  49. }
  50. // Fail if err !=nil. Display msg as an error message
  51. func checkErr(t testing.TB, err error, msg string) {
  52. if err != nil {
  53. t.Error(msg)
  54. }
  55. }
  56. // Converts string to private key
  57. func convToPrv(s string, v KeyVariant) *PrivateKey {
  58. key := NewPrivateKey(params.Id, v)
  59. hex, e := hex.DecodeString(s)
  60. if e != nil {
  61. panic("non-hex number provided")
  62. }
  63. e = key.Import(hex)
  64. if e != nil {
  65. panic("Can't import private key")
  66. }
  67. return key
  68. }
  69. // Converts string to public key
  70. func convToPub(s string, v KeyVariant) *PublicKey {
  71. key := NewPublicKey(params.Id, v)
  72. hex, e := hex.DecodeString(s)
  73. if e != nil {
  74. panic("non-hex number provided")
  75. }
  76. e = key.Import(hex)
  77. if e != nil {
  78. panic("Can't import public key")
  79. }
  80. return key
  81. }
  82. func testKeygen(s *SidhParams, t *testing.T) {
  83. alicePrivate := convToPrv(PrA, KeyVariant_SIDH_A)
  84. bobPrivate := convToPrv(PrB, KeyVariant_SIDH_B)
  85. expPubA := convToPub(PkA, KeyVariant_SIDH_A)
  86. expPubB := convToPub(PkB, KeyVariant_SIDH_B)
  87. pubA := alicePrivate.GeneratePublicKey()
  88. pubB := bobPrivate.GeneratePublicKey()
  89. if !bytes.Equal(pubA.Export(), expPubA.Export()) {
  90. t.Fatalf("unexpected value of public key A")
  91. }
  92. if !bytes.Equal(pubB.Export(), expPubB.Export()) {
  93. t.Fatalf("unexpected value of public key B")
  94. }
  95. }
  96. func testRoundtrip(s *SidhParams, t *testing.T) {
  97. var err error
  98. prvA := NewPrivateKey(params.Id, KeyVariant_SIDH_A)
  99. prvB := NewPrivateKey(params.Id, KeyVariant_SIDH_B)
  100. // Generate private keys
  101. err = prvA.Generate(rand.Reader)
  102. checkErr(t, err, "key generation failed")
  103. err = prvB.Generate(rand.Reader)
  104. checkErr(t, err, "key generation failed")
  105. // Generate public keys
  106. pubA := prvA.GeneratePublicKey()
  107. pubB := prvB.GeneratePublicKey()
  108. // Derive shared secret
  109. s1, err := DeriveSecret(prvB, pubA)
  110. checkErr(t, err, "")
  111. s2, err := DeriveSecret(prvA, pubB)
  112. checkErr(t, err, "")
  113. if !bytes.Equal(s1[:], s2[:]) {
  114. t.Fatalf("Tthe two shared keys: \n%X, \n%X do not match", s1, s2)
  115. }
  116. }
  117. func testKeyAgreement(s *SidhParams, t testing.TB, pkA, pkB, prA, prB string) {
  118. var e error
  119. // KeyPairs
  120. alicePublic := convToPub(pkA, KeyVariant_SIDH_A)
  121. bobPublic := convToPub(pkB, KeyVariant_SIDH_B)
  122. alicePrivate := convToPrv(prA, KeyVariant_SIDH_A)
  123. bobPrivate := convToPrv(prB, KeyVariant_SIDH_B)
  124. // Do actual test
  125. s1, e := DeriveSecret(bobPrivate, alicePublic)
  126. checkErr(t, e, "derivation s1")
  127. s2, e := DeriveSecret(alicePrivate, bobPublic)
  128. checkErr(t, e, "derivation s1")
  129. if !bytes.Equal(s1[:], s2[:]) {
  130. t.Fatalf("two shared keys: %d, %d do not match", s1, s2)
  131. }
  132. // Negative case
  133. dec, e := hex.DecodeString(PkA)
  134. if e != nil {
  135. t.FailNow()
  136. }
  137. dec[0] = ^dec[0]
  138. e = alicePublic.Import(dec)
  139. if e != nil {
  140. t.FailNow()
  141. }
  142. s1, e = DeriveSecret(bobPrivate, alicePublic)
  143. checkErr(t, e, "derivation of s1 failed")
  144. s2, e = DeriveSecret(alicePrivate, bobPublic)
  145. checkErr(t, e, "derivation of s2 failed")
  146. if bytes.Equal(s1[:], s2[:]) {
  147. t.Fatalf("The two shared keys: %d, %d match", s1, s2)
  148. }
  149. }
  150. func TestKeygenP751(t *testing.T) {
  151. testKeygen(Params(FP_751), t)
  152. }
  153. func TestKeyAgreementP751(t *testing.T) {
  154. testKeyAgreement(Params(FP_751), t, PkA, PkB, PrA, PrB)
  155. }
  156. func TestRoundtripP751(t *testing.T) {
  157. testRoundtrip(Params(FP_751), t)
  158. }
  159. func TestKeyAgreementP751_AliceEvenNumber(t *testing.T) {
  160. // even alice
  161. prE := "C09957CC83045FB4C3726384D784476ACB6FFD92E5B15B3C2D451BA063F1BD4CED8FBCF682A98DD0954D37BCAF730F00"
  162. pkE := "8A2DE6FD963C475F7829B689C8B8306FC0917A39EBBC35CA171546269A85698FEC0379E2E1A3C567BE1B8EF5639F81F304889737E6CC444DBED4579DB204DC8C7928F5CBB1ECDD682A1B5C48C0DAF34208C06BF201BE4E6063B1BFDC42413B0537F8E76BEE645C1A24118301BAB17EB8D6E0F283BCB16EFB833E4BB3463953C93165A0DDAC55B385059F27FF7228486D0A733812C81C792BE9EC3A16A5DB0EB099EEA76AC0E59612251A3AD19F7CC567DA2AEBD7733171F48E471D17648692355164E27B515D2A47D7BA34B3B48A047BE7C09C4ABEE2FCC9ACA7396C8A8C9E73E29533FC7369094DFA7988778E55E53F309922C6E233F8F9C7936C3D29CEA640406FCA06450AA1978FF39F227BF06B1E072F1763447C6F513B23CDF3B0EC0379070AEE5A02D9AD8E0EB023461D631F4A9643A4C79921334945F6B33DDFC11D9703BD06B047B4DA404AB12EFD2C3A49E5C42D10DA063352748B21DE41C32A5693FE1C0DCAB111F4990CD58BECADB1892EE7A7E99C9DB4DA4E69C96E57138B99038BC9B877ECE75914EFB98DD08B9E4A2DCCB948A8F7D2F26678A9952BA0EFAB1E9CF6E51B557480DEC2BA30DE0FE4AFE30A6B30765EE75EF64F678316D81C72755AD2CFA0B8C7706B07BFA52FBC3DB84EF9E79796C0089305B1E13C78660779E0FF2A13820CE141104F976B1678990F85B2D3D2B89CD5BC4DD52603A5D24D3EFEDA44BAA0F38CDB75A220AF45EAB70F2799875D435CE50FC6315EDD4BB7AA7260AFD7CD0561B69B4FA3A817904322661C3108DA24"
  163. testKeyAgreement(Params(FP_751), t, pkE, PkB, prE, PrB)
  164. }
  165. func TestImportExport(t *testing.T) {
  166. var err error
  167. a := NewPublicKey(params.Id, KeyVariant_SIDH_A)
  168. b := NewPublicKey(params.Id, KeyVariant_SIDH_B)
  169. // Import keys
  170. a_hex, err := hex.DecodeString(PkA)
  171. checkErr(t, err, "invalid hex-number provided")
  172. err = a.Import(a_hex)
  173. checkErr(t, err, "import failed")
  174. b_hex, err := hex.DecodeString(PkB)
  175. checkErr(t, err, "invalid hex-number provided")
  176. err = b.Import(b_hex)
  177. checkErr(t, err, "import failed")
  178. // Export and check if same
  179. if !bytes.Equal(b.Export(), b_hex) || !bytes.Equal(a.Export(), a_hex) {
  180. t.Fatalf("export/import failed")
  181. }
  182. if (len(b.Export()) != b.Size()) || (len(a.Export()) != a.Size()) {
  183. t.Fatalf("wrong size of exported keys")
  184. }
  185. }
  186. func TestMultiplyByThree(t *testing.T) {
  187. // sage: repr((3^238 -1).digits(256))
  188. var three238minus1 = [48]byte{
  189. 248, 132, 131, 130, 138, 113, 205, 237, 20, 122, 66, 212, 191, 53, 59, 115, 56, 207,
  190. 215, 148, 207, 41, 130, 248, 214, 42, 124, 12, 153, 108, 197, 99, 199, 34, 66, 143,
  191. 126, 168, 88, 184, 245, 234, 37, 181, 198, 201, 84, 2}
  192. // sage: repr((3*(3^238 -1)).digits(256))
  193. var threeTimesThree238minus1 = [48]byte{
  194. 232, 142, 138, 135, 159, 84, 104, 201, 62, 110, 199, 124, 63, 161, 177, 89, 169, 109,
  195. 135, 190, 110, 125, 134, 233, 132, 128, 116, 37, 203, 69, 80, 43, 86, 104, 198, 173,
  196. 123, 249, 9, 41, 225, 192, 113, 31, 84, 93, 254, 6}
  197. multiplyByThree(three238minus1[:])
  198. for i := 0; i < 48; i++ {
  199. if three238minus1[i] != threeTimesThree238minus1[i] {
  200. t.Error("Digit", i, "error: found", three238minus1[i],
  201. "expected", threeTimesThree238minus1[i])
  202. }
  203. }
  204. }
  205. func TestCheckLessThanThree238(t *testing.T) {
  206. var three238minus1 = [48]byte{
  207. 248, 132, 131, 130, 138, 113, 205, 237, 20, 122, 66, 212, 191, 53, 59, 115,
  208. 56, 207, 215, 148, 207, 41, 130, 248, 214, 42, 124, 12, 153, 108, 197, 99,
  209. 199, 34, 66, 143, 126, 168, 88, 184, 245, 234, 37, 181, 198, 201, 84, 2}
  210. var three238 = [48]byte{
  211. 249, 132, 131, 130, 138, 113, 205, 237, 20, 122, 66, 212, 191, 53, 59, 115,
  212. 56, 207, 215, 148, 207, 41, 130, 248, 214, 42, 124, 12, 153, 108, 197, 99, 199,
  213. 34, 66, 143, 126, 168, 88, 184, 245, 234, 37, 181, 198, 201, 84, 2}
  214. var three238plus1 = [48]byte{250, 132, 131, 130, 138, 113, 205, 237, 20, 122, 66,
  215. 212, 191, 53, 59, 115, 56, 207, 215, 148, 207, 41, 130, 248, 214, 42, 124, 12,
  216. 153, 108, 197, 99, 199, 34, 66, 143, 126, 168, 88, 184, 245, 234, 37, 181, 198,
  217. 201, 84, 2}
  218. // makes second 64-bit digits bigger than in three238. checks if carries are correctly propagated
  219. var three238plus2power65 = [48]byte{249, 132, 131, 130, 138, 113, 205, 237, 22, 122,
  220. 66, 212, 191, 53, 59, 115, 56, 207, 215, 148, 207, 41, 130, 248, 214, 42, 124, 12,
  221. 153, 108, 197, 99, 199, 34, 66, 143, 126, 168, 88, 184, 245, 234, 37, 181, 198,
  222. 201, 84, 2}
  223. var result uint8
  224. result = checkLessThanThree238(three238minus1[:])
  225. if result != 0 {
  226. t.Error("expected 0, got", result)
  227. }
  228. result = checkLessThanThree238(three238[:])
  229. if result != 1 {
  230. t.Error("expected nonzero, got", result)
  231. }
  232. result = checkLessThanThree238(three238plus1[:])
  233. if result != 1 {
  234. t.Error("expected nonzero, got", result)
  235. }
  236. result = checkLessThanThree238(three238plus2power65[:])
  237. if result != 1 {
  238. t.Error("expected nonzero, got", result)
  239. }
  240. }
  241. func BenchmarkSidhKeyAgreement(b *testing.B) {
  242. // KeyPairs
  243. alicePublic := convToPub(PkA, KeyVariant_SIDH_A)
  244. bobPublic := convToPub(PkB, KeyVariant_SIDH_B)
  245. alicePrivate := convToPrv(PrA, KeyVariant_SIDH_A)
  246. bobPrivate := convToPrv(PrB, KeyVariant_SIDH_B)
  247. for i := 0; i < b.N; i++ {
  248. // Derive shared secret
  249. DeriveSecret(bobPrivate, alicePublic)
  250. DeriveSecret(alicePrivate, bobPublic)
  251. }
  252. }
  253. func BenchmarkAliceKeyGenPrv(b *testing.B) {
  254. prv := NewPrivateKey(params.Id, KeyVariant_SIDH_A)
  255. for n := 0; n < b.N; n++ {
  256. prv.Generate(rand.Reader)
  257. }
  258. }
  259. func BenchmarkBobKeyGenPrv(b *testing.B) {
  260. prv := NewPrivateKey(params.Id, KeyVariant_SIDH_B)
  261. for n := 0; n < b.N; n++ {
  262. prv.Generate(rand.Reader)
  263. }
  264. }
  265. func BenchmarkAliceKeyGenPub(b *testing.B) {
  266. prv := NewPrivateKey(params.Id, KeyVariant_SIDH_A)
  267. prv.Generate(rand.Reader)
  268. for n := 0; n < b.N; n++ {
  269. prv.GeneratePublicKey()
  270. }
  271. }
  272. func BenchmarkBobKeyGenPub(b *testing.B) {
  273. prv := NewPrivateKey(params.Id, KeyVariant_SIDH_B)
  274. prv.Generate(rand.Reader)
  275. for n := 0; n < b.N; n++ {
  276. prv.GeneratePublicKey()
  277. }
  278. }
  279. func BenchmarkSharedSecretAlice(b *testing.B) {
  280. aPr := convToPrv(PrA, KeyVariant_SIDH_A)
  281. bPk := convToPub(PkB, KeyVariant_SIDH_B)
  282. for n := 0; n < b.N; n++ {
  283. DeriveSecret(aPr, bPk)
  284. }
  285. }
  286. func BenchmarkSharedSecretBob(b *testing.B) {
  287. // m_B = 3*randint(0,3^238)
  288. aPk := convToPub(PkA, KeyVariant_SIDH_A)
  289. bPr := convToPrv(PrB, KeyVariant_SIDH_B)
  290. for n := 0; n < b.N; n++ {
  291. DeriveSecret(bPr, aPk)
  292. }
  293. }