scalarmult.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #define TEST_NAME "scalarmult"
  2. #include "cmptest.h"
  3. static const unsigned char alicesk[crypto_scalarmult_BYTES] = {
  4. 0x77, 0x07, 0x6d, 0x0a, 0x73, 0x18, 0xa5, 0x7d, 0x3c, 0x16, 0xc1,
  5. 0x72, 0x51, 0xb2, 0x66, 0x45, 0xdf, 0x4c, 0x2f, 0x87, 0xeb, 0xc0,
  6. 0x99, 0x2a, 0xb1, 0x77, 0xfb, 0xa5, 0x1d, 0xb9, 0x2c, 0x2a
  7. };
  8. static const unsigned char bobsk[crypto_scalarmult_BYTES] = {
  9. 0x5d, 0xab, 0x08, 0x7e, 0x62, 0x4a, 0x8a, 0x4b, 0x79, 0xe1, 0x7f,
  10. 0x8b, 0x83, 0x80, 0x0e, 0xe6, 0x6f, 0x3b, 0xb1, 0x29, 0x26, 0x18,
  11. 0xb6, 0xfd, 0x1c, 0x2f, 0x8b, 0x27, 0xff, 0x88, 0xe0, 0xeb
  12. };
  13. static const unsigned char small_order_p[crypto_scalarmult_BYTES] = {
  14. 0xe0, 0xeb, 0x7a, 0x7c, 0x3b, 0x41, 0xb8, 0xae, 0x16, 0x56, 0xe3,
  15. 0xfa, 0xf1, 0x9f, 0xc4, 0x6a, 0xda, 0x09, 0x8d, 0xeb, 0x9c, 0x32,
  16. 0xb1, 0xfd, 0x86, 0x62, 0x05, 0x16, 0x5f, 0x49, 0xb8, 0x00
  17. };
  18. static char hex[crypto_scalarmult_BYTES * 2 + 1];
  19. int
  20. main(void)
  21. {
  22. unsigned char *alicepk =
  23. (unsigned char *) sodium_malloc(crypto_scalarmult_BYTES);
  24. unsigned char *bobpk =
  25. (unsigned char *) sodium_malloc(crypto_scalarmult_BYTES);
  26. unsigned char *k = (unsigned char *) sodium_malloc(crypto_scalarmult_BYTES);
  27. int ret;
  28. assert(alicepk != NULL && bobpk != NULL && k != NULL);
  29. crypto_scalarmult_base(alicepk, alicesk);
  30. sodium_bin2hex(hex, sizeof hex, alicepk, crypto_scalarmult_BYTES);
  31. printf("%s\n", hex);
  32. crypto_scalarmult_base(bobpk, bobsk);
  33. sodium_bin2hex(hex, sizeof hex, bobpk, crypto_scalarmult_BYTES);
  34. printf("%s\n", hex);
  35. ret = crypto_scalarmult(k, alicesk, bobpk);
  36. assert(ret == 0);
  37. sodium_bin2hex(hex, sizeof hex, k, crypto_scalarmult_BYTES);
  38. printf("%s\n", hex);
  39. ret = crypto_scalarmult(k, bobsk, alicepk);
  40. assert(ret == 0);
  41. sodium_bin2hex(hex, sizeof hex, k, crypto_scalarmult_BYTES);
  42. printf("%s\n", hex);
  43. ret = crypto_scalarmult(k, bobsk, small_order_p);
  44. assert(ret == -1);
  45. sodium_bin2hex(hex, sizeof hex, k, crypto_scalarmult_BYTES);
  46. printf("%s\n", hex);
  47. sodium_free(bobpk);
  48. sodium_free(alicepk);
  49. sodium_free(k);
  50. assert(crypto_scalarmult_bytes() > 0U);
  51. assert(crypto_scalarmult_scalarbytes() > 0U);
  52. assert(strcmp(crypto_scalarmult_primitive(), "curve25519") == 0);
  53. assert(crypto_scalarmult_bytes() == crypto_scalarmult_curve25519_bytes());
  54. assert(crypto_scalarmult_scalarbytes() ==
  55. crypto_scalarmult_curve25519_scalarbytes());
  56. assert(crypto_scalarmult_bytes() == crypto_scalarmult_scalarbytes());
  57. return 0;
  58. }