diff --git a/docs/algorithms/kem/kyber.md b/docs/algorithms/kem/kyber.md
index 1595c38e9e..2666f70853 100644
--- a/docs/algorithms/kem/kyber.md
+++ b/docs/algorithms/kem/kyber.md
@@ -7,9 +7,9 @@
- **Authors' website**: https://pq-crystals.org/
- **Specification version**: NIST Round 3 submission.
- **Primary Source**:
- - **Source**: https://github.com/pq-crystals/kyber/commit/518de2414a85052bb91349bcbcc347f391292d5b with copy_from_upstream patches
+ - **Source**: https://github.com/pq-crystals/kyber/commit/dda29cc63af721981ee2c831cf00822e69be3220 with copy_from_upstream patches
- **Implementation license (SPDX-Identifier)**: CC0-1.0 or Apache-2.0
-- **Optimized Implementation sources**: https://github.com/pq-crystals/kyber/commit/518de2414a85052bb91349bcbcc347f391292d5b with copy_from_upstream patches
+- **Optimized Implementation sources**: https://github.com/pq-crystals/kyber/commit/dda29cc63af721981ee2c831cf00822e69be3220 with copy_from_upstream patches
- **oldpqclean-aarch64**:
- **Source**: https://github.com/PQClean/PQClean/commit/8e220a87308154d48fdfac40abbb191ac7fce06a with copy_from_upstream patches
- **Implementation license (SPDX-Identifier)**: CC0-1.0 and (CC0-1.0 or Apache-2.0) and (CC0-1.0 or MIT) and MIT
diff --git a/docs/algorithms/kem/kyber.yml b/docs/algorithms/kem/kyber.yml
index 46db8a0161..a04c44df6c 100644
--- a/docs/algorithms/kem/kyber.yml
+++ b/docs/algorithms/kem/kyber.yml
@@ -17,7 +17,7 @@ website: https://pq-crystals.org/
nist-round: 3
spec-version: NIST Round 3 submission
primary-upstream:
- source: https://github.com/pq-crystals/kyber/commit/518de2414a85052bb91349bcbcc347f391292d5b
+ source: https://github.com/pq-crystals/kyber/commit/dda29cc63af721981ee2c831cf00822e69be3220
with copy_from_upstream patches
spdx-license-identifier: CC0-1.0 or Apache-2.0
optimized-upstreams:
diff --git a/scripts/copy_from_upstream/copy_from_upstream.yml b/scripts/copy_from_upstream/copy_from_upstream.yml
index 96478418fd..4b7f0c1dc8 100644
--- a/scripts/copy_from_upstream/copy_from_upstream.yml
+++ b/scripts/copy_from_upstream/copy_from_upstream.yml
@@ -25,7 +25,7 @@ upstreams:
name: pqcrystals-kyber
git_url: https://github.com/pq-crystals/kyber.git
git_branch: master
- git_commit: 518de2414a85052bb91349bcbcc347f391292d5b
+ git_commit: dda29cc63af721981ee2c831cf00822e69be3220
kem_meta_path: '{pretty_name_full}_META.yml'
kem_scheme_path: '.'
patches: [pqcrystals-kyber-yml.patch, pqcrystals-kyber-ref-shake-aes.patch, pqcrystals-kyber-avx2-shake-aes.patch]
diff --git a/src/kem/kyber/pqcrystals-kyber_kyber1024_avx2/verify.c b/src/kem/kyber/pqcrystals-kyber_kyber1024_avx2/verify.c
index aa8e2850b1..06243b837f 100644
--- a/src/kem/kyber/pqcrystals-kyber_kyber1024_avx2/verify.c
+++ b/src/kem/kyber/pqcrystals-kyber_kyber1024_avx2/verify.c
@@ -57,6 +57,16 @@ void cmov(uint8_t * restrict r, const uint8_t *x, size_t len, uint8_t b)
size_t i;
__m256i xvec, rvec, bvec;
+#if defined(__GNUC__) || defined(__clang__)
+ // Prevent the compiler from
+ // 1) inferring that b is 0/1-valued, and
+ // 2) handling the two cases with a branch.
+ // This is not necessary when verify.c and kem.c are separate translation
+ // units, but we expect that downstream consumers will copy this code and/or
+ // change how it is built.
+ __asm__("" : "+r"(b) : /* no inputs */);
+#endif
+
bvec = _mm256_set1_epi64x(-(uint64_t)b);
for(i=0;icoeffs[8*i+j];
- t += ((int16_t)t >> 15) & KYBER_Q;
- t = (((t << 1) + KYBER_Q/2)/KYBER_Q) & 1;
+ // t += ((int16_t)t >> 15) & KYBER_Q;
+ // t = (((t << 1) + KYBER_Q/2)/KYBER_Q) & 1;
+ t <<= 1;
+ t += 1665;
+ t *= 80635;
+ t >>= 28;
+ t &= 1;
msg[i] |= t << j;
}
}
diff --git a/src/kem/kyber/pqcrystals-kyber_kyber1024_ref/verify.c b/src/kem/kyber/pqcrystals-kyber_kyber1024_ref/verify.c
index ed4a6541f8..1c43071ca0 100644
--- a/src/kem/kyber/pqcrystals-kyber_kyber1024_ref/verify.c
+++ b/src/kem/kyber/pqcrystals-kyber_kyber1024_ref/verify.c
@@ -41,6 +41,16 @@ void cmov(uint8_t *r, const uint8_t *x, size_t len, uint8_t b)
{
size_t i;
+#if defined(__GNUC__) || defined(__clang__)
+ // Prevent the compiler from
+ // 1) inferring that b is 0/1-valued, and
+ // 2) handling the two cases with a branch.
+ // This is not necessary when verify.c and kem.c are separate translation
+ // units, but we expect that downstream consumers will copy this code and/or
+ // change how it is built.
+ __asm__("" : "+r"(b) : /* no inputs */);
+#endif
+
b = -b;
for(i=0;icoeffs[8*i+j];
- t += ((int16_t)t >> 15) & KYBER_Q;
- t = (((t << 1) + KYBER_Q/2)/KYBER_Q) & 1;
+ // t += ((int16_t)t >> 15) & KYBER_Q;
+ // t = (((t << 1) + KYBER_Q/2)/KYBER_Q) & 1;
+ t <<= 1;
+ t += 1665;
+ t *= 80635;
+ t >>= 28;
+ t &= 1;
msg[i] |= t << j;
}
}
diff --git a/src/kem/kyber/pqcrystals-kyber_kyber512_ref/verify.c b/src/kem/kyber/pqcrystals-kyber_kyber512_ref/verify.c
index ed4a6541f8..1c43071ca0 100644
--- a/src/kem/kyber/pqcrystals-kyber_kyber512_ref/verify.c
+++ b/src/kem/kyber/pqcrystals-kyber_kyber512_ref/verify.c
@@ -41,6 +41,16 @@ void cmov(uint8_t *r, const uint8_t *x, size_t len, uint8_t b)
{
size_t i;
+#if defined(__GNUC__) || defined(__clang__)
+ // Prevent the compiler from
+ // 1) inferring that b is 0/1-valued, and
+ // 2) handling the two cases with a branch.
+ // This is not necessary when verify.c and kem.c are separate translation
+ // units, but we expect that downstream consumers will copy this code and/or
+ // change how it is built.
+ __asm__("" : "+r"(b) : /* no inputs */);
+#endif
+
b = -b;
for(i=0;icoeffs[8*i+j];
- t += ((int16_t)t >> 15) & KYBER_Q;
- t = (((t << 1) + KYBER_Q/2)/KYBER_Q) & 1;
+ // t += ((int16_t)t >> 15) & KYBER_Q;
+ // t = (((t << 1) + KYBER_Q/2)/KYBER_Q) & 1;
+ t <<= 1;
+ t += 1665;
+ t *= 80635;
+ t >>= 28;
+ t &= 1;
msg[i] |= t << j;
}
}
diff --git a/src/kem/kyber/pqcrystals-kyber_kyber768_ref/verify.c b/src/kem/kyber/pqcrystals-kyber_kyber768_ref/verify.c
index ed4a6541f8..1c43071ca0 100644
--- a/src/kem/kyber/pqcrystals-kyber_kyber768_ref/verify.c
+++ b/src/kem/kyber/pqcrystals-kyber_kyber768_ref/verify.c
@@ -41,6 +41,16 @@ void cmov(uint8_t *r, const uint8_t *x, size_t len, uint8_t b)
{
size_t i;
+#if defined(__GNUC__) || defined(__clang__)
+ // Prevent the compiler from
+ // 1) inferring that b is 0/1-valued, and
+ // 2) handling the two cases with a branch.
+ // This is not necessary when verify.c and kem.c are separate translation
+ // units, but we expect that downstream consumers will copy this code and/or
+ // change how it is built.
+ __asm__("" : "+r"(b) : /* no inputs */);
+#endif
+
b = -b;
for(i=0;i