You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by an...@apache.org on 2022/03/10 11:52:54 UTC

[mynewt-nimble] 03/07: nimble/ll: Refactor CSA2 prng a bit

This is an automated email from the ASF dual-hosted git repository.

andk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-nimble.git

commit 742bf62ea8f4a32a5c32440c13aee9c11f54e909
Author: Andrzej Kaczmarek <an...@codecoup.pl>
AuthorDate: Mon Feb 28 11:37:25 2022 +0100

    nimble/ll: Refactor CSA2 prng a bit
    
    Prng calculates prn_e, but we also need prn_s (for iso subevents) which
    is genreated before final xor in prng. Add helper to calculate prn_s
    and make prng use it.
    
    Also split mam to separate helper just to make it look nicer.
---
 nimble/controller/src/ble_ll_utils.c | 36 ++++++++++++++++++++++++++----------
 1 file changed, 26 insertions(+), 10 deletions(-)

diff --git a/nimble/controller/src/ble_ll_utils.c b/nimble/controller/src/ble_ll_utils.c
index 7b3a863..ee25f83 100644
--- a/nimble/controller/src/ble_ll_utils.c
+++ b/nimble/controller/src/ble_ll_utils.c
@@ -232,23 +232,39 @@ ble_ll_utils_csa2_perm(uint32_t in)
 }
 #endif
 
+static inline uint32_t
+ble_ll_utils_csa2_mam(uint32_t a, uint32_t b)
+{
+    return (17 * a + b) % 65536;
+}
+
 static uint16_t
-ble_ll_utils_csa2_prng(uint16_t counter, uint16_t ch_id)
+ble_ll_utils_csa2_prn_s(uint16_t counter, uint16_t ch_id)
 {
-    uint16_t prn_e;
+    uint32_t prn_s;
+
+    prn_s = counter ^ ch_id;
 
-    prn_e = counter ^ ch_id;
+    prn_s = ble_ll_utils_csa2_perm(prn_s);
+    prn_s = ble_ll_utils_csa2_mam(prn_s, ch_id);
 
-    prn_e = ble_ll_utils_csa2_perm(prn_e);
-    prn_e = (prn_e * 17) + ch_id;
+    prn_s = ble_ll_utils_csa2_perm(prn_s);
+    prn_s = ble_ll_utils_csa2_mam(prn_s, ch_id);
 
-    prn_e = ble_ll_utils_csa2_perm(prn_e);
-    prn_e = (prn_e * 17) + ch_id;
+    prn_s = ble_ll_utils_csa2_perm(prn_s);
+    prn_s = ble_ll_utils_csa2_mam(prn_s, ch_id);
 
-    prn_e = ble_ll_utils_csa2_perm(prn_e);
-    prn_e = (prn_e * 17) + ch_id;
+    return prn_s;
+}
+
+static uint16_t
+ble_ll_utils_csa2_prng(uint16_t counter, uint16_t ch_id)
+{
+    uint16_t prn_s;
+    uint16_t prn_e;
 
-    prn_e = prn_e ^ ch_id;
+    prn_s = ble_ll_utils_csa2_prn_s(counter, ch_id);
+    prn_e = prn_s ^ ch_id;
 
     return prn_e;
 }