gf128.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. /* GF(2^128) defined by f(x) = x^128 + x^7 + x^2 + x + 1
  10. * A + B mod f(x) = a xor b
  11. * A * 2 mod f(x)
  12. */
  13. #ifndef GMSSL_GF128_H
  14. #define GMSSL_GF128_H
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <stdint.h>
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. //typedef unsigned __int128 gf128_t;
  23. typedef struct {
  24. uint64_t hi;
  25. uint64_t lo;
  26. } gf128_t;
  27. // Note: send by value is comptabile with uint128_t and sse2
  28. gf128_t gf128_from_hex(const char *s);
  29. int gf128_equ_hex(gf128_t a, const char *s);
  30. gf128_t gf128_zero(void);
  31. gf128_t gf128_add(gf128_t a, gf128_t b);
  32. gf128_t gf128_mul(gf128_t a, gf128_t b);
  33. gf128_t gf128_mul2(gf128_t a);
  34. gf128_t gf128_from_bytes(const uint8_t p[16]);
  35. void gf128_to_bytes(gf128_t a, uint8_t p[16]);
  36. int gf128_print(FILE *fp, int fmt ,int ind, const char *label, gf128_t a);
  37. #ifdef __cplusplus
  38. }
  39. #endif
  40. #endif