gf128test.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the License); you may
  5. * not use this file except in compliance with the License.
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <assert.h>
  13. #include <gmssl/hex.h>
  14. #include <gmssl/gf128.h>
  15. #include <gmssl/error.h>
  16. int test_gf128_from_hex(void)
  17. {
  18. char *tests[] = {
  19. "00000000000000000000000000000000",
  20. "00000000000000000000000000000001",
  21. "10000000000000000000000000000000",
  22. "de300f9301a499a965f8bf677e99e80d",
  23. "14b267838ec9ef1bb7b5ce8c19e34bc6",
  24. };
  25. gf128_t a;
  26. int i;
  27. for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
  28. a = gf128_from_hex(tests[i]);
  29. if (gf128_equ_hex(a, tests[i]) != 1) {
  30. error_print();
  31. return -1;
  32. }
  33. }
  34. printf("%s() ok\n", __FUNCTION__);
  35. return 1;
  36. }
  37. int test_gf128_mul2(void)
  38. {
  39. char *tests[] = {
  40. "00000000000000000000000000000001",
  41. "de300f9301a499a965f8bf677e99e80d",
  42. };
  43. char *results[] = {
  44. "e1000000000000000000000000000000",
  45. "8e1807c980d24cd4b2fc5fb3bf4cf406",
  46. };
  47. gf128_t a;
  48. int i;
  49. for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
  50. a = gf128_from_hex(tests[i]);
  51. a = gf128_mul2(a);
  52. if (gf128_equ_hex(a, results[i]) != 1) {
  53. error_print();
  54. return -1;
  55. }
  56. }
  57. printf("%s() ok\n", __FUNCTION__);
  58. return 1;
  59. }
  60. int test_gf128_mul(void)
  61. {
  62. char *hex_a = "de300f9301a499a965f8bf677e99e80d";
  63. char *hex_b = "14b267838ec9ef1bb7b5ce8c19e34bc6";
  64. char *hex_add_a_b = "ca8268108f6d76b2d24d71eb677aa3cb";
  65. char *hex_mul_a_b = "7d87dda57a20b0c51d9743071ab14010";
  66. gf128_t a, b, r;
  67. a = gf128_from_hex(hex_a);
  68. b = gf128_from_hex(hex_b);
  69. r = gf128_add(a, b);
  70. if (gf128_equ_hex(r, hex_add_a_b) != 1) {
  71. error_print();
  72. return -1;
  73. }
  74. r = gf128_mul(a, b);
  75. if (gf128_equ_hex(r, hex_mul_a_b) != 1) {
  76. error_print();
  77. return -1;
  78. }
  79. printf("%s() ok\n", __FUNCTION__);
  80. return 1;
  81. }
  82. int main(void)
  83. {
  84. if (test_gf128_from_hex() != 1) goto err;
  85. if (test_gf128_mul2() != 1) goto err;
  86. if (test_gf128_mul() != 1) goto err;
  87. printf("%s all tests passed\n", __FILE__);
  88. return 0;
  89. err:
  90. error_print();
  91. return -1;
  92. }