Skip to contentSkip to conversation

Improve shuffling algorithm of connection list

PATCH In review Hurukawa2121 3 participants 3 messages Nov 18, 2024 – Dec 29, 2024

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Improve shuffling algorithm of connection list

---
Improve shuffling algorithm of connection list

This patch implements the Fisher-Yates shuffle algorithm to ensure that all permutations of the connection target list are generated with equal probability, eliminating biases present in the previous shuffling method. In the Fisher-Yates algorithm, there's only one way to obtain each permutation through a series of element swaps, so all permutations occur with equal probability in theory.

 src/openvpn/init.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
src/openvpn/init.c +10 -3
index 9371024e..c4fb5cd7 100644
@@ -467,7 +467,14 @@ ce_management_query_remote(struct context *c)
 #endif /* ENABLE_MANAGEMENT */
 
 /*
- * Initialize and possibly randomize connection list.
+ * Initialize and randomize the connection list.
+ * 
+ * Applies the Fisher-Yates shuffle algorithm to ensure all permutations are equally probable,
+ * thereby eliminating shuffling bias in the previous method.
+ * 
+ * The algorithm randomly selects an element from the unshuffled portion and places it at position i.
+ * There's only one way to obtain each permutation through these swaps.
+ * This guarantees that each permutation occurs with equal probability in theory.
  */
 static void
 init_connection_list(struct context *c)
@@ -478,9 +485,9 @@ init_connection_list(struct context *c)
     if (c->options.remote_random)
     {
         int i;
-        for (i = 0; i < l->len; ++i)
+        for (i = l->len - 1; i > 0; --i)
         {
-            const int j = get_random() % l->len;
+            const int j = get_random() % (i + 1);
             if (i != j)
             {
                 struct connection_entry *tmp;
Signature · 1 line
2.39.3 (Apple Git-146)
Acked-by on v2 1/1
#1 lore raw

Thanks for resending the patch!

Quoted 1 line · On 18/11/2024 15:20, Hurukawa2121 wrote:

---

However, please note that anything you write should go *before* these three dashes, otherwise the text is ignored by git on commit.

Anyway, no need to resend the patch for this.
Probably Gert can easily fix this before moving forward with your patch.

Quoted 3 lines

Improve shuffling algorithm of connection list

This patch implements the Fisher-Yates shuffle algorithm to ensure that all permutations of the connection target list are generated with equal probability, eliminating biases present in the previous shuffling method. In the Fisher-Yates algorithm, there's only one way to obtain each permutation through a series of element swaps, so all permutations occur with equal probability in theory.

Also one single loooong line in the commit message is absolutely not acceptable. Better to split the text in lines no longer than 72 chars. Technically Gert can fix this too upon commit.

But please do remember these details for your next patch.

Quoted 13 lines
  src/openvpn/init.c | 13 ++++++++++---
  1 file changed, 10 insertions(+), 3 deletions(-)
src/openvpn/init.c +0 -0
index 9371024e..c4fb5cd7 100644
@@ -467,7 +467,14 @@ ce_management_query_remote(struct context *c)
  #endif /* ENABLE_MANAGEMENT */
 

src/openvpn/init.c ce_management_query_remote(struct context *c) · line 477

Quoted 10 lines
src/openvpn/init.c +8 -1
  /*
- * Initialize and possibly randomize connection list.
+ * Initialize and randomize the connection list.
+ *
+ * Applies the Fisher-Yates shuffle algorithm to ensure all permutations are equally probable,
+ * thereby eliminating shuffling bias in the previous method.
+ *
+ * The algorithm randomly selects an element from the unshuffled portion and places it at position i.
+ * There's only one way to obtain each permutation through these swaps.
+ * This guarantees that each permutation occurs with equal probability in theory.

Same goes for comments (and any other line of code):
maximum line length is 80 chars..

This said, the patch looks good to me and I can apply my

However, I live it to Gert if he wants a new patch with these layout errors fixed or not.

Regards,

Quoted 15 lines
src/openvpn/init.c +2 -2
   */
  static void
  init_connection_list(struct context *c)
@@ -478,9 +485,9 @@ init_connection_list(struct context *c)
      if (c->options.remote_random)
      {
          int i;
-        for (i = 0; i < l->len; ++i)
+        for (i = l->len - 1; i > 0; --i)
          {
-            const int j = get_random() % l->len;
+            const int j = get_random() % (i + 1);
              if (i != j)
              {
                  struct connection_entry *tmp;
Signature · 1 line
Antonio Quartulli
#2 lore raw
In reply to Hurukawa2121

Thanks for your patch.

I have reformatted the comment section (line length was too long) and brought back the word "possibly" ("possibly randomize", it's not always randomized). I have not done any detailed tests on randomness and distribution of permutations, though :-)

Your patch has been applied to the master branch.

commit 9502e5257c59bfd23b9afcdb28f7994470591430 (master)

Author: Shuji Furukawa
Date:   Mon Nov 18 23:20:20 2024 +0900
     Improve shuffling algorithm of connection list

     Signed-off-by: Shuji Furukawa <[email protected]...>
     Acked-by: Antonio Quartulli <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg29837.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering