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.
 
 
 

77 lines
2.4 KiB

  1. #include "verify.h"
  2. #include <immintrin.h>
  3. #include <stdint.h>
  4. #include <stdlib.h>
  5. /*************************************************
  6. * Name: verify
  7. *
  8. * Description: Compare two arrays for equality in constant time.
  9. *
  10. * Arguments: const uint8_t *a: pointer to first byte array
  11. * const uint8_t *b: pointer to second byte array
  12. * size_t len: length of the byte arrays
  13. *
  14. * Returns 0 if the byte arrays are equal, 1 otherwise
  15. **************************************************/
  16. uint8_t PQCLEAN_KYBER76890S_AVX2_verify(const uint8_t *a, const uint8_t *b, size_t len) {
  17. size_t pos;
  18. uint64_t r;
  19. __m256i avec, bvec, cvec;
  20. cvec = _mm256_setzero_si256();
  21. for (pos = 0; pos + 32 <= len; pos += 32) {
  22. avec = _mm256_loadu_si256((__m256i *)&a[pos]);
  23. bvec = _mm256_loadu_si256((__m256i *)&b[pos]);
  24. avec = _mm256_xor_si256(avec, bvec);
  25. cvec = _mm256_or_si256(cvec, avec);
  26. }
  27. cvec = _mm256_cmpeq_epi8(cvec, _mm256_setzero_si256());
  28. r = (uint32_t)(_mm256_movemask_epi8(cvec) ^ -1);
  29. while (pos < len) {
  30. r |= a[pos] ^ b[pos];
  31. pos += 1;
  32. }
  33. r = (-r) >> 63;
  34. return (uint8_t)r;
  35. }
  36. /*************************************************
  37. * Name: cmov
  38. *
  39. * Description: Copy len bytes from x to r if b is 1;
  40. * don't modify x if b is 0. Requires b to be in {0,1};
  41. * assumes two's complement representation of negative integers.
  42. * Runs in constant time.
  43. *
  44. * Arguments: uint8_t *r: pointer to output byte array
  45. * const uint8_t *x: pointer to input byte array
  46. * size_t len: Amount of bytes to be copied
  47. * uint8_t b: Condition bit; has to be in {0,1}
  48. **************************************************/
  49. void PQCLEAN_KYBER76890S_AVX2_cmov(uint8_t *r, const uint8_t *x, size_t len, uint8_t b) {
  50. size_t pos;
  51. __m256i xvec, rvec, bvec;
  52. b = -b;
  53. bvec = _mm256_set1_epi8((char)b);
  54. for (pos = 0; pos + 32 <= len; pos += 32) {
  55. rvec = _mm256_loadu_si256((__m256i *)&r[pos]);
  56. xvec = _mm256_loadu_si256((__m256i *)&x[pos]);
  57. xvec = _mm256_xor_si256(xvec, rvec);
  58. xvec = _mm256_and_si256(xvec, bvec);
  59. rvec = _mm256_xor_si256(rvec, xvec);
  60. _mm256_storeu_si256((__m256i *)&r[pos], rvec);
  61. }
  62. while (pos < len) {
  63. r[pos] ^= b & (x[pos] ^ r[pos]);
  64. pos += 1;
  65. }
  66. }