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.
 
 
 

84 lines
1.6 KiB

  1. /*
  2. This file is for the Berlekamp-Massey algorithm
  3. see http://crypto.stanford.edu/~mironov/cs359/massey.pdf
  4. */
  5. #include "bm.h"
  6. #include "params.h"
  7. #define min(a, b) (((a) < (b)) ? (a) : (b))
  8. /* the Berlekamp-Massey algorithm */
  9. /* input: s, sequence of field elements */
  10. /* output: out, minimal polynomial of s */
  11. void PQCLEAN_MCELIECE6960119_CLEAN_bm(gf *out, gf *s) {
  12. int i;
  13. uint16_t N = 0;
  14. uint16_t L = 0;
  15. uint16_t mle;
  16. uint16_t mne;
  17. gf T[ SYS_T + 1 ];
  18. gf C[ SYS_T + 1 ];
  19. gf B[ SYS_T + 1 ];
  20. gf b = 1, d, f;
  21. //
  22. for (i = 0; i < SYS_T + 1; i++) {
  23. C[i] = B[i] = 0;
  24. }
  25. B[1] = C[0] = 1;
  26. //
  27. for (N = 0; N < 2 * SYS_T; N++) {
  28. d = 0;
  29. for (i = 0; i <= min(N, SYS_T); i++) {
  30. d ^= PQCLEAN_MCELIECE6960119_CLEAN_gf_mul(C[i], s[ N - i]);
  31. }
  32. mne = d;
  33. mne -= 1;
  34. mne >>= 15;
  35. mne -= 1;
  36. mle = N;
  37. mle -= 2 * L;
  38. mle >>= 15;
  39. mle -= 1;
  40. mle &= mne;
  41. for (i = 0; i <= SYS_T; i++) {
  42. T[i] = C[i];
  43. }
  44. f = PQCLEAN_MCELIECE6960119_CLEAN_gf_frac(b, d);
  45. for (i = 0; i <= SYS_T; i++) {
  46. C[i] ^= PQCLEAN_MCELIECE6960119_CLEAN_gf_mul(f, B[i]) & mne;
  47. }
  48. L = (L & ~mle) | ((N + 1 - L) & mle);
  49. for (i = 0; i <= SYS_T; i++) {
  50. B[i] = (B[i] & ~mle) | (T[i] & mle);
  51. }
  52. b = (b & ~mle) | (d & mle);
  53. for (i = SYS_T; i >= 1; i--) {
  54. B[i] = B[i - 1];
  55. }
  56. B[0] = 0;
  57. }
  58. for (i = 0; i <= SYS_T; i++) {
  59. out[i] = C[ SYS_T - i ];
  60. }
  61. }