From ba5ce518ea1f66cae0bbac4b3f790cd9d766b263 Mon Sep 17 00:00:00 2001 From: benr-ml Date: Thu, 14 Mar 2024 11:29:44 +0200 Subject: [PATCH] address comments --- .../dependencies/sui-framework/random.md | 41 ++++-- .../docs/sui-framework/random.md | 43 +++++-- .../dependencies/sui-framework/random.md | 41 ++++-- .../sui-framework/sources/random.move | 28 +++-- .../sui-framework/tests/random_tests.move | 119 ++++++++---------- .../examples/games/sources/raffles.move | 12 +- .../examples/games/sources/slot_machine.move | 3 +- .../examples/games/tests/raffles_tests.move | 9 +- .../games/tests/slot_machine_tests.move | 3 +- .../examples/nfts/sources/random_nft.move | 92 ++++++-------- 10 files changed, 223 insertions(+), 168 deletions(-) diff --git a/crates/sui-framework/docs/deepbook/dependencies/sui-framework/random.md b/crates/sui-framework/docs/deepbook/dependencies/sui-framework/random.md index 7df679482efbe..76c843d4560ad 100644 --- a/crates/sui-framework/docs/deepbook/dependencies/sui-framework/random.md +++ b/crates/sui-framework/docs/deepbook/dependencies/sui-framework/random.md @@ -197,6 +197,15 @@ The actual state is stored in a versioned inner field. + + + + +
const EInvalidLength: u64 = 4;
+
+ + + @@ -224,6 +233,15 @@ The actual state is stored in a versioned inner field. + + + + +
const U16_MAX: u64 = 65535;
+
+ + + ## Function `create` @@ -360,7 +378,7 @@ transaction. let epoch = tx_context::epoch(ctx); let inner = load_inner_mut(self); if (inner.randomness_round == 0 && inner.epoch == 0 && - vector::is_empty(&inner.random_bytes)) { + vector::is_empty(&inner.random_bytes)) { // First update should be for round zero. assert!(new_round == 0, EInvalidRandomnessUpdate); } else { @@ -486,11 +504,12 @@ transaction. vector::append(&mut result, derive_next_block(g)); num_of_blocks = num_of_blocks - 1; }; - // Take remaining bytes from the generator's buffer. + // Fill the generator's buffer if needed. let num_of_bytes = (num_of_bytes as u64); if (vector::length(&g.buffer) < (num_of_bytes - vector::length(&result))) { fill_buffer(g); }; + // Take remaining bytes from the generator's buffer. while (vector::length(&result) < num_of_bytes) { vector::push_back(&mut result, vector::pop_back(&mut g.buffer)); }; @@ -724,9 +743,12 @@ transaction. if (min == max) { return min }; - let diff = ((max - min) as u256) + 1; + // Pick a random number in [0, max - min] by generating a random number that is larger than max-min, and taking + // the modulo of the random number by the range size. Then add the min to the result to get a number in + // [min, max]. + let range_size = ((max - min) as u256) + 1; let rand = u256_from_bytes(g, num_of_bytes); - min + ((rand % diff) as u128) + min + ((rand % range_size) as u128) } @@ -870,13 +892,16 @@ transaction.
public fun shuffle<T>(g: &mut RandomGenerator, v: &mut vector<T>) {
-    let n = (vector::length(v) as u32);
+    let n = vector::length(v);
     if (n == 0) {
         return
     };
-    let i: u32 = 0;
-    while (i < (n - 1)) {
-        let j = generate_u32_in_range(g, i, n-1);
+    assert!(n <= U16_MAX, EInvalidLength);
+    let n = (n as u16);
+    let i: u16 = 0;
+    let end = n - 1;
+    while (i < end) {
+        let j = generate_u16_in_range(g, i, end);
         vector::swap(v, (i as u64), (j as u64));
         i = i + 1;
     };
diff --git a/crates/sui-framework/docs/sui-framework/random.md b/crates/sui-framework/docs/sui-framework/random.md
index d5110f6125b93..36d0711189b95 100644
--- a/crates/sui-framework/docs/sui-framework/random.md
+++ b/crates/sui-framework/docs/sui-framework/random.md
@@ -199,6 +199,15 @@ Unique randomness generator, derived from the global randomness.
 
 
 
+
+
+
+
+
const EInvalidLength: u64 = 4;
+
+ + + @@ -226,6 +235,15 @@ Unique randomness generator, derived from the global randomness. + + + + +
const U16_MAX: u64 = 65535;
+
+ + + ## Function `create` @@ -362,7 +380,7 @@ transaction. let epoch = tx_context::epoch(ctx); let inner = load_inner_mut(self); if (inner.randomness_round == 0 && inner.epoch == 0 && - vector::is_empty(&inner.random_bytes)) { + vector::is_empty(&inner.random_bytes)) { // First update should be for round zero. assert!(new_round == 0, EInvalidRandomnessUpdate); } else { @@ -490,11 +508,12 @@ Generate n random bytes. vector::append(&mut result, derive_next_block(g)); num_of_blocks = num_of_blocks - 1; }; - // Take remaining bytes from the generator's buffer. + // Fill the generator's buffer if needed. let num_of_bytes = (num_of_bytes as u64); if (vector::length(&g.buffer) < (num_of_bytes - vector::length(&result))) { fill_buffer(g); }; + // Take remaining bytes from the generator's buffer. while (vector::length(&result) < num_of_bytes) { vector::push_back(&mut result, vector::pop_back(&mut g.buffer)); }; @@ -735,9 +754,12 @@ Generate a boolean. if (min == max) { return min }; - let diff = ((max - min) as u256) + 1; + // Pick a random number in [0, max - min] by generating a random number that is larger than max-min, and taking + // the modulo of the random number by the range size. Then add the min to the result to get a number in + // [min, max]. + let range_size = ((max - min) as u256) + 1; let rand = u256_from_bytes(g, num_of_bytes); - min + ((rand % diff) as u128) + min + ((rand % range_size) as u128) }
@@ -873,7 +895,7 @@ Generate a random u8 in [min, max] (with a bias of 2^{-64}). ## Function `shuffle` -Shuffle a vector using the random generator. +Shuffle a vector using the random generator (Fisher–Yates/Knuth shuffle).
public fun shuffle<T>(g: &mut random::RandomGenerator, v: &mut vector<T>)
@@ -886,13 +908,16 @@ Shuffle a vector using the random generator.
 
 
 
public fun shuffle<T>(g: &mut RandomGenerator, v: &mut vector<T>) {
-    let n = (vector::length(v) as u32);
+    let n = vector::length(v);
     if (n == 0) {
         return
     };
-    let i: u32 = 0;
-    while (i < (n - 1)) {
-        let j = generate_u32_in_range(g, i, n-1);
+    assert!(n <= U16_MAX, EInvalidLength);
+    let n = (n as u16);
+    let i: u16 = 0;
+    let end = n - 1;
+    while (i < end) {
+        let j = generate_u16_in_range(g, i, end);
         vector::swap(v, (i as u64), (j as u64));
         i = i + 1;
     };
diff --git a/crates/sui-framework/docs/sui-system/dependencies/sui-framework/random.md b/crates/sui-framework/docs/sui-system/dependencies/sui-framework/random.md
index 7df679482efbe..76c843d4560ad 100644
--- a/crates/sui-framework/docs/sui-system/dependencies/sui-framework/random.md
+++ b/crates/sui-framework/docs/sui-system/dependencies/sui-framework/random.md
@@ -197,6 +197,15 @@ The actual state is stored in a versioned inner field.
 
 
 
+
+
+
+
+
const EInvalidLength: u64 = 4;
+
+ + + @@ -224,6 +233,15 @@ The actual state is stored in a versioned inner field. + + + + +
const U16_MAX: u64 = 65535;
+
+ + + ## Function `create` @@ -360,7 +378,7 @@ transaction. let epoch = tx_context::epoch(ctx); let inner = load_inner_mut(self); if (inner.randomness_round == 0 && inner.epoch == 0 && - vector::is_empty(&inner.random_bytes)) { + vector::is_empty(&inner.random_bytes)) { // First update should be for round zero. assert!(new_round == 0, EInvalidRandomnessUpdate); } else { @@ -486,11 +504,12 @@ transaction. vector::append(&mut result, derive_next_block(g)); num_of_blocks = num_of_blocks - 1; }; - // Take remaining bytes from the generator's buffer. + // Fill the generator's buffer if needed. let num_of_bytes = (num_of_bytes as u64); if (vector::length(&g.buffer) < (num_of_bytes - vector::length(&result))) { fill_buffer(g); }; + // Take remaining bytes from the generator's buffer. while (vector::length(&result) < num_of_bytes) { vector::push_back(&mut result, vector::pop_back(&mut g.buffer)); }; @@ -724,9 +743,12 @@ transaction. if (min == max) { return min }; - let diff = ((max - min) as u256) + 1; + // Pick a random number in [0, max - min] by generating a random number that is larger than max-min, and taking + // the modulo of the random number by the range size. Then add the min to the result to get a number in + // [min, max]. + let range_size = ((max - min) as u256) + 1; let rand = u256_from_bytes(g, num_of_bytes); - min + ((rand % diff) as u128) + min + ((rand % range_size) as u128) }
@@ -870,13 +892,16 @@ transaction.
public fun shuffle<T>(g: &mut RandomGenerator, v: &mut vector<T>) {
-    let n = (vector::length(v) as u32);
+    let n = vector::length(v);
     if (n == 0) {
         return
     };
-    let i: u32 = 0;
-    while (i < (n - 1)) {
-        let j = generate_u32_in_range(g, i, n-1);
+    assert!(n <= U16_MAX, EInvalidLength);
+    let n = (n as u16);
+    let i: u16 = 0;
+    let end = n - 1;
+    while (i < end) {
+        let j = generate_u16_in_range(g, i, end);
         vector::swap(v, (i as u64), (j as u64));
         i = i + 1;
     };
diff --git a/crates/sui-framework/packages/sui-framework/sources/random.move b/crates/sui-framework/packages/sui-framework/sources/random.move
index 671d35baa468a..625ca6c5f4566 100644
--- a/crates/sui-framework/packages/sui-framework/sources/random.move
+++ b/crates/sui-framework/packages/sui-framework/sources/random.move
@@ -17,9 +17,11 @@ module sui::random {
     const EWrongInnerVersion: u64 = 1;
     const EInvalidRandomnessUpdate: u64 = 2;
     const EInvalidRange: u64 = 3;
+    const EInvalidLength: u64 = 4;
 
     const CURRENT_VERSION: u64 = 1;
     const RAND_OUTPUT_LEN: u16 = 32;
+    const U16_MAX: u64 = 0xFFFF;
 
     /// Singleton shared object which stores the global randomness state.
     /// The actual state is stored in a versioned inner field.
@@ -173,11 +175,12 @@ module sui::random {
             vector::append(&mut result, derive_next_block(g));
             num_of_blocks = num_of_blocks - 1;
         };
-        // Take remaining bytes from the generator's buffer.
+        // Fill the generator's buffer if needed.
         let num_of_bytes = (num_of_bytes as u64);
         if (vector::length(&g.buffer) < (num_of_bytes - vector::length(&result))) {
             fill_buffer(g);
         };
+        // Take remaining bytes from the generator's buffer.
         while (vector::length(&result) < num_of_bytes) {
             vector::push_back(&mut result, vector::pop_back(&mut g.buffer));
         };
@@ -186,6 +189,7 @@ module sui::random {
 
     // Helper function that extracts the given number of bytes from the random generator and returns it as u256.
     // Assumes that the caller has already checked that num_of_bytes is valid.
+    // TODO: Replace with a macro when we have support for it.
     fun u256_from_bytes(g: &mut RandomGenerator, num_of_bytes: u8): u256 {
         if (vector::length(&g.buffer) < (num_of_bytes as u64)) {
             fill_buffer(g);
@@ -236,15 +240,20 @@ module sui::random {
     }
 
     // Helper function to generate a random u128 in [min, max] using a random number with num_of_bytes bytes.
-    // Assumes that the caller verified the inputs, and uses num_of_bytes to control the bias.
+    // Assumes that the caller verified the inputs, and uses num_of_bytes to control the bias (e.g., 8 bytes larger
+    // than the actual type used by the caller function to limit the bias by 2^{-64}).
+    // TODO: Replace with a macro when we have support for it.
     fun u128_in_range(g: &mut RandomGenerator, min: u128, max: u128, num_of_bytes: u8): u128 {
         assert!(min <= max, EInvalidRange);
         if (min == max) {
             return min
         };
-        let diff = ((max - min) as u256) + 1;
+        // Pick a random number in [0, max - min] by generating a random number that is larger than max-min, and taking
+        // the modulo of the random number by the range size. Then add the min to the result to get a number in
+        // [min, max].
+        let range_size = ((max - min) as u256) + 1;
         let rand = u256_from_bytes(g, num_of_bytes);
-        min + ((rand % diff) as u128)
+        min + ((rand % range_size) as u128)
     }
 
     /// Generate a random u128 in [min, max] (with a bias of 2^{-64}).
@@ -274,13 +283,16 @@ module sui::random {
 
     /// Shuffle a vector using the random generator (Fisher–Yates/Knuth shuffle).
     public fun shuffle(g: &mut RandomGenerator, v: &mut vector) {
-        let n = (vector::length(v) as u32);
+        let n = vector::length(v);
         if (n == 0) {
             return
         };
-        let i: u32 = 0;
-        while (i < (n - 1)) {
-            let j = generate_u32_in_range(g, i, n - 1);
+        assert!(n <= U16_MAX, EInvalidLength);
+        let n = (n as u16);
+        let i: u16 = 0;
+        let end = n - 1;
+        while (i < end) {
+            let j = generate_u16_in_range(g, i, end);
             vector::swap(v, (i as u64), (j as u64));
             i = i + 1;
         };
diff --git a/crates/sui-framework/packages/sui-framework/tests/random_tests.move b/crates/sui-framework/packages/sui-framework/tests/random_tests.move
index 21177a02ce74d..882c75370854a 100644
--- a/crates/sui-framework/packages/sui-framework/tests/random_tests.move
+++ b/crates/sui-framework/packages/sui-framework/tests/random_tests.move
@@ -1,4 +1,3 @@
-
 // Copyright (c) Mysten Labs, Inc.
 // SPDX-License-Identifier: Apache-2.0
 
@@ -34,7 +33,7 @@ module sui::random_tests {
             &mut random_state,
             0,
             x"1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F",
-            test_scenario::ctx(scenario)
+            test_scenario::ctx(scenario),
         );
 
         let gen = new_generator(&random_state, test_scenario::ctx(scenario));
@@ -63,10 +62,10 @@ module sui::random_tests {
             &mut random_state,
             0,
             global_random1,
-            test_scenario::ctx(scenario)
+            test_scenario::ctx(scenario),
         );
         test_scenario::next_tx(scenario, @0x0);
-        let gen1= new_generator(&random_state, test_scenario::ctx(scenario));
+        let gen1 = new_generator(&random_state, test_scenario::ctx(scenario));
         test_scenario::return_shared(random_state);
         test_scenario::end(scenario_val);
 
@@ -78,10 +77,10 @@ module sui::random_tests {
             &mut random_state,
             1,
             global_random1,
-            test_scenario::ctx(scenario)
+            test_scenario::ctx(scenario),
         );
         test_scenario::next_tx(scenario, @0x0);
-        let gen2= new_generator(&random_state, test_scenario::ctx(scenario));
+        let gen2 = new_generator(&random_state, test_scenario::ctx(scenario));
         test_scenario::return_shared(random_state);
         test_scenario::end(scenario_val);
 
@@ -93,16 +92,16 @@ module sui::random_tests {
             &mut random_state,
             2,
             global_random2,
-            test_scenario::ctx(scenario)
+            test_scenario::ctx(scenario),
         );
         test_scenario::next_tx(scenario, @0x0);
-        let gen3= new_generator(&random_state, test_scenario::ctx(scenario));
-        let gen4= new_generator(&random_state, test_scenario::ctx(scenario));
+        let gen3 = new_generator(&random_state, test_scenario::ctx(scenario));
+        let gen4 = new_generator(&random_state, test_scenario::ctx(scenario));
         test_scenario::return_shared(random_state);
         test_scenario::end(scenario_val);
 
         assert!(generator_counter(&gen1) == 0, 0);
-        assert!(*generator_buffer(&gen1) == vector::empty(), 0);
+        assert!(vector::is_empty(generator_buffer(&gen1)), 0);
         assert!(generator_seed(&gen1) == generator_seed(&gen2), 0);
         assert!(generator_seed(&gen1) != generator_seed(&gen3), 0);
         assert!(generator_seed(&gen3) != generator_seed(&gen4), 0);
@@ -121,7 +120,7 @@ module sui::random_tests {
             &mut random_state,
             0,
             x"1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F",
-            test_scenario::ctx(scenario)
+            test_scenario::ctx(scenario),
         );
 
         // Regression (not critical for security, but still an indication that something is wrong).
@@ -170,13 +169,13 @@ module sui::random_tests {
             &mut random_state,
             0,
             x"1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F",
-            test_scenario::ctx(scenario)
+            test_scenario::ctx(scenario),
         );
 
         let gen = new_generator(&random_state, test_scenario::ctx(scenario));
 
         // Check the output size & internal generator state
-        assert!(*generator_buffer(&gen) == vector::empty(), 0);
+        assert!(vector::is_empty(generator_buffer(&gen)), 0);
         let output = generate_bytes(&mut gen, 1);
         assert!(generator_counter(&gen) == 1, 0);
         assert!(vector::length(generator_buffer(&gen)) == 31, 0);
@@ -205,9 +204,9 @@ module sui::random_tests {
         // Sanity check that the output is not all zeros.
         let output = generate_bytes(&mut gen, 10);
         let i = 0;
-        while (true) { // should break before the overflow
-            if (*vector::borrow(&output, i) != 0u8)
-                break;
+        loop {
+            // should break before the overflow
+            if (*vector::borrow(&output, i) != 0u8) break;
             i = i + 1;
         };
 
@@ -215,9 +214,9 @@ module sui::random_tests {
         let output1 = generate_bytes(&mut gen, 10);
         let output2 = generate_bytes(&mut gen, 10);
         i = 0;
-        while (true) { // should break before the overflow
-            if (vector::borrow(&output1, i) != vector::borrow(&output2, i))
-                break;
+        loop {
+            // should break before the overflow
+            if (vector::borrow(&output1, i) != vector::borrow(&output2, i)) break;
             i = i + 1;
         };
 
@@ -238,12 +237,12 @@ module sui::random_tests {
             &mut random_state,
             0,
             x"1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F",
-            test_scenario::ctx(scenario)
+            test_scenario::ctx(scenario),
         );
 
         // u256
         let gen = new_generator(&random_state, test_scenario::ctx(scenario));
-        assert!(*generator_buffer(&gen) == vector::empty(), 0);
+        assert!(vector::is_empty(generator_buffer(&gen)), 0);
         let output1 = generate_u256(&mut gen);
         assert!(generator_counter(&gen) == 1, 0);
         assert!(vector::length(generator_buffer(&gen)) == 0, 0);
@@ -260,13 +259,12 @@ module sui::random_tests {
         while (i < 32) {
             let x = generate_u256(&mut gen);
             let x_bytes = bcs::to_bytes(&x);
-            if (*vector::borrow(&x_bytes, i) != 0u8)
-                i = i + 1;
+            if (*vector::borrow(&x_bytes, i) != 0u8) i = i + 1;
         };
 
         // u128
         gen = new_generator(&random_state, test_scenario::ctx(scenario));
-        assert!(*generator_buffer(&gen) == vector::empty(), 0);
+        assert!(vector::is_empty(generator_buffer(&gen)), 0);
         let output1 = generate_u128(&mut gen);
         assert!(generator_counter(&gen) == 1, 0);
         assert!(vector::length(generator_buffer(&gen)) == 16, 0);
@@ -282,13 +280,12 @@ module sui::random_tests {
         while (i < 16) {
             let x = generate_u128(&mut gen);
             let x_bytes = bcs::to_bytes(&x);
-            if (*vector::borrow(&x_bytes, i) != 0u8)
-                i = i + 1;
+            if (*vector::borrow(&x_bytes, i) != 0u8) i = i + 1;
         };
 
         // u64
         gen = new_generator(&random_state, test_scenario::ctx(scenario));
-        assert!(*generator_buffer(&gen) == vector::empty(), 0);
+        assert!(vector::is_empty(generator_buffer(&gen)), 0);
         let output1 = generate_u64(&mut gen);
         assert!(generator_counter(&gen) == 1, 0);
         assert!(vector::length(generator_buffer(&gen)) == 24, 0);
@@ -304,13 +301,12 @@ module sui::random_tests {
         while (i < 8) {
             let x = generate_u64(&mut gen);
             let x_bytes = bcs::to_bytes(&x);
-            if (*vector::borrow(&x_bytes, i) != 0u8)
-                i = i + 1;
+            if (*vector::borrow(&x_bytes, i) != 0u8) i = i + 1;
         };
 
         // u32
         gen = new_generator(&random_state, test_scenario::ctx(scenario));
-        assert!(*generator_buffer(&gen) == vector::empty(), 0);
+        assert!(vector::is_empty(generator_buffer(&gen)), 0);
         let output1 = generate_u32(&mut gen);
         assert!(generator_counter(&gen) == 1, 0);
         assert!(vector::length(generator_buffer(&gen)) == 28, 0);
@@ -326,13 +322,12 @@ module sui::random_tests {
         while (i < 4) {
             let x = generate_u32(&mut gen);
             let x_bytes = bcs::to_bytes(&x);
-            if (*vector::borrow(&x_bytes, i) != 0u8)
-                i = i + 1;
+            if (*vector::borrow(&x_bytes, i) != 0u8) i = i + 1;
         };
 
         // u16
         gen = new_generator(&random_state, test_scenario::ctx(scenario));
-        assert!(*generator_buffer(&gen) == vector::empty(), 0);
+        assert!(vector::is_empty(generator_buffer(&gen)), 0);
         let output1 = generate_u16(&mut gen);
         assert!(generator_counter(&gen) == 1, 0);
         assert!(vector::length(generator_buffer(&gen)) == 30, 0);
@@ -348,13 +343,12 @@ module sui::random_tests {
         while (i < 2) {
             let x = generate_u16(&mut gen);
             let x_bytes = bcs::to_bytes(&x);
-            if (*vector::borrow(&x_bytes, i) != 0u8)
-                i = i + 1;
+            if (*vector::borrow(&x_bytes, i) != 0u8) i = i + 1;
         };
 
         // u8
         gen = new_generator(&random_state, test_scenario::ctx(scenario));
-        assert!(*generator_buffer(&gen) == vector::empty(), 0);
+        assert!(vector::is_empty(generator_buffer(&gen)), 0);
         let output1 = generate_u8(&mut gen);
         assert!(generator_counter(&gen) == 1, 0);
         assert!(vector::length(generator_buffer(&gen)) == 31, 0);
@@ -366,15 +360,14 @@ module sui::random_tests {
         let _output4 = generate_u8(&mut gen);
         assert!(generator_counter(&gen) == 1, 0);
         assert!(vector::length(generator_buffer(&gen)) == 13, 0);
-        while (true) {
+        loop {
             let x = generate_u8(&mut gen);
-            if (x != 0u8)
-                break
+            if (x != 0u8) break
         };
 
         // bool
         gen = new_generator(&random_state, test_scenario::ctx(scenario));
-        assert!(*generator_buffer(&gen) == vector::empty(), 0);
+        assert!(vector::is_empty(generator_buffer(&gen)), 0);
         let output1 = generate_bool(&mut gen);
         assert!(generator_counter(&gen) == 1, 0);
         assert!(vector::length(generator_buffer(&gen)) == 31, 0);
@@ -387,9 +380,9 @@ module sui::random_tests {
         assert!(generator_counter(&gen) == 1, 0);
         assert!(vector::length(generator_buffer(&gen)) == 13, 0);
         let saw_false = false;
-        while (true) {
+        loop {
             let x = generate_bool(&mut gen);
-            if (!x) saw_false = true;
+            saw_false = saw_false || !x;
             if (x && saw_false) break;
         };
 
@@ -410,11 +403,11 @@ module sui::random_tests {
             &mut random_state,
             0,
             x"1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F",
-            test_scenario::ctx(scenario)
+            test_scenario::ctx(scenario),
         );
 
         let gen = new_generator(&random_state, test_scenario::ctx(scenario));
-        let v: vector = vector[0, 1, 2, 3, 4,];
+        let v: vector = vector[0, 1, 2, 3, 4];
         shuffle(&mut gen, &mut v);
         assert!(vector::length(&v) == 5, 0);
         let i: u16 = 0;
@@ -424,15 +417,13 @@ module sui::random_tests {
         };
 
         // check that numbers indeed eventaually move to all positions
-        while (true) {
+        loop {
             shuffle(&mut gen, &mut v);
-            if ((*vector::borrow(&v, 4) == 1u16))
-                break;
+            if ((*vector::borrow(&v, 4) == 1u16)) break;
         };
-        while (true) {
+        loop {
             shuffle(&mut gen, &mut v);
-            if ((*vector::borrow(&v, 0) == 2u16))
-                break;
+            if ((*vector::borrow(&v, 0) == 2u16)) break;
         };
 
         let v: vector = vector[];
@@ -461,7 +452,7 @@ module sui::random_tests {
             &mut random_state,
             0,
             x"1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F",
-            test_scenario::ctx(scenario)
+            test_scenario::ctx(scenario),
         );
 
         // generate_u128_in_range
@@ -564,8 +555,8 @@ module sui::random_tests {
         let i = 0;
         while (i < 50) {
             let (min, max) = (generate_u8(&mut gen), generate_u8(&mut gen));
-            let (min, max) = if (min < max) { (min, max) } else { (max, min) };
-            let (min, max) = if (min == max) { (min, max + 1) } else { (min, max) };
+            let (min, max) = if (min < max) (min, max) else (max, min);
+            let (min, max) = if (min == max) (min, max + 1) else (min, max);
             let output = generate_u8_in_range(&mut gen, min, max);
             assert!(output >= min, 0);
             assert!(output <= max, 0);
@@ -593,7 +584,7 @@ module sui::random_tests {
             &mut random_state,
             0,
             x"1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F",
-            test_scenario::ctx(scenario)
+            test_scenario::ctx(scenario),
         );
 
         let gen = new_generator(&random_state, test_scenario::ctx(scenario));
@@ -616,13 +607,13 @@ module sui::random_tests {
             &mut random_state,
             0,
             vector[0, 1, 2, 3],
-            test_scenario::ctx(scenario)
+            test_scenario::ctx(scenario),
         );
         update_randomness_state_for_testing(
             &mut random_state,
             1,
             vector[4, 5, 6, 7],
-            test_scenario::ctx(scenario)
+            test_scenario::ctx(scenario),
         );
 
         test_scenario::next_epoch(scenario, @0x0);
@@ -631,7 +622,7 @@ module sui::random_tests {
             &mut random_state,
             0,
             vector[8, 9, 10, 11],
-            test_scenario::ctx(scenario)
+            test_scenario::ctx(scenario),
         );
 
         test_scenario::return_shared(random_state);
@@ -650,15 +641,15 @@ module sui::random_tests {
         let random_state = test_scenario::take_shared(scenario);
         update_randomness_state_for_testing(
             &mut random_state,
-            1,
+            0,
             vector[0, 1, 2, 3],
-            test_scenario::ctx(scenario)
+            test_scenario::ctx(scenario),
         );
         update_randomness_state_for_testing(
             &mut random_state,
-            1,
+            0,
             vector[0, 1, 2, 3],
-            test_scenario::ctx(scenario)
+            test_scenario::ctx(scenario),
         );
 
         test_scenario::return_shared(random_state);
@@ -677,15 +668,15 @@ module sui::random_tests {
         let random_state = test_scenario::take_shared(scenario);
         update_randomness_state_for_testing(
             &mut random_state,
-            1,
+            0,
             vector[0, 1, 2, 3],
-            test_scenario::ctx(scenario)
+            test_scenario::ctx(scenario),
         );
         update_randomness_state_for_testing(
             &mut random_state,
             3,
             vector[0, 1, 2, 3],
-            test_scenario::ctx(scenario)
+            test_scenario::ctx(scenario),
         );
 
         test_scenario::return_shared(random_state);
diff --git a/sui_programmability/examples/games/sources/raffles.move b/sui_programmability/examples/games/sources/raffles.move
index ab609b35d4286..630f5684f7528 100644
--- a/sui_programmability/examples/games/sources/raffles.move
+++ b/sui_programmability/examples/games/sources/raffles.move
@@ -11,15 +11,11 @@
 
 module games::raffle_with_tickets {
     use std::option::{Self, Option};
-    use sui::balance;
-    use sui::balance::Balance;
-    use sui::clock;
-    use sui::clock::Clock;
-    use sui::coin;
-    use sui::coin::Coin;
+    use sui::balance::{Self, Balance};
+    use sui::clock::{Self, Clock};
+    use sui::coin::{Self, Coin};
     use sui::object::{Self, ID, UID};
-    use sui::random;
-    use sui::random::{Random, new_generator};
+    use sui::random::{Self, Random, new_generator};
     use sui::sui::SUI;
     use sui::transfer;
     use sui::tx_context::TxContext;
diff --git a/sui_programmability/examples/games/sources/slot_machine.move b/sui_programmability/examples/games/sources/slot_machine.move
index ded3c0a0bf616..774dd24141222 100644
--- a/sui_programmability/examples/games/sources/slot_machine.move
+++ b/sui_programmability/examples/games/sources/slot_machine.move
@@ -34,7 +34,7 @@ module games::slot_machine {
     /// Create a new game with a given initial reward for the current epoch.
     public fun create(
         reward: Coin,
-        ctx: &mut TxContext
+        ctx: &mut TxContext,
     ) {
         let amount = coin::value(&reward);
         assert!(amount > 0, EInvalidAmount);
@@ -89,5 +89,4 @@ module games::slot_machine {
     public fun get_epoch(game: &Game): u64 {
         game.epoch
     }
-
 }
diff --git a/sui_programmability/examples/games/tests/raffles_tests.move b/sui_programmability/examples/games/tests/raffles_tests.move
index 831a1aba87410..60a84d32eb089 100644
--- a/sui_programmability/examples/games/tests/raffles_tests.move
+++ b/sui_programmability/examples/games/tests/raffles_tests.move
@@ -3,7 +3,6 @@
 
 #[test_only]
 module games::raffles_tests {
-
     use std::option;
     use sui::clock;
     use sui::coin::{Self, Coin};
@@ -38,7 +37,7 @@ module games::raffles_tests {
             &mut random_state,
             0,
             x"1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F",
-            test_scenario::ctx(scenario)
+            test_scenario::ctx(scenario),
         );
 
         // Create the game and get back the output objects.
@@ -81,7 +80,7 @@ module games::raffles_tests {
         test_scenario::next_tx(scenario, user4);
         mint(user4, 10, scenario);
         let coin = test_scenario::take_from_sender>(scenario);
-        let t4 = raffle_with_tickets::buy_ticket( &mut game, coin, &clock, test_scenario::ctx(scenario));
+        let t4 = raffle_with_tickets::buy_ticket(&mut game, coin, &clock, test_scenario::ctx(scenario));
         assert!(raffle_with_tickets::get_participants(&game) == 4, 1);
         // this is the winner
 
@@ -119,7 +118,7 @@ module games::raffles_tests {
             &mut random_state,
             0,
             x"1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F",
-            test_scenario::ctx(scenario)
+            test_scenario::ctx(scenario),
         );
 
         // Create the game and get back the output objects.
@@ -158,7 +157,7 @@ module games::raffles_tests {
         test_scenario::next_tx(scenario, user4);
         mint(user4, 10, scenario);
         let coin = test_scenario::take_from_sender>(scenario);
-        small_raffle::play( &mut game, coin, &clock, test_scenario::ctx(scenario));
+        small_raffle::play(&mut game, coin, &clock, test_scenario::ctx(scenario));
         assert!(small_raffle::get_participants(&game) == 4, 1);
 
         // Determine the winner (-> user4)
diff --git a/sui_programmability/examples/games/tests/slot_machine_tests.move b/sui_programmability/examples/games/tests/slot_machine_tests.move
index b29ea893ebe80..9d14c5bc6c78f 100644
--- a/sui_programmability/examples/games/tests/slot_machine_tests.move
+++ b/sui_programmability/examples/games/tests/slot_machine_tests.move
@@ -8,7 +8,6 @@ module games::slot_machine_tests {
     use sui::sui::SUI;
     use sui::test_scenario::{Self, Scenario};
     use sui::transfer;
-
     use games::slot_machine;
 
     fun mint(addr: address, amount: u64, scenario: &mut Scenario) {
@@ -31,7 +30,7 @@ module games::slot_machine_tests {
             &mut random_state,
             0,
             x"1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F",
-            test_scenario::ctx(scenario)
+            test_scenario::ctx(scenario),
         );
 
         // Create the game and get back the output objects.
diff --git a/sui_programmability/examples/nfts/sources/random_nft.move b/sui_programmability/examples/nfts/sources/random_nft.move
index 90a0c66f7d5db..d8146762fac9a 100644
--- a/sui_programmability/examples/nfts/sources/random_nft.move
+++ b/sui_programmability/examples/nfts/sources/random_nft.move
@@ -28,14 +28,15 @@ module nfts::random_nft_airdrop {
     }
 
     struct MintingCapability has key {
-        id: UID
+        id: UID,
     }
 
     #[allow(unused_function)]
     fun init(ctx: &mut TxContext) {
-        transfer::transfer(MintingCapability {
-            id: object::new(ctx)
-        }, tx_context::sender(ctx));
+        transfer::transfer(
+            MintingCapability { id: object::new(ctx) },
+            tx_context::sender(ctx),
+        );
     }
 
     public fun mint(_cap: &MintingCapability, n: u16, ctx: &mut TxContext): vector {
@@ -64,10 +65,10 @@ module nfts::random_nft_airdrop {
         let is_bronze = (1 - is_gold) * (1 - is_silver); // probability of 60%
         let metal = is_gold * GOLD + is_silver * SILVER + is_bronze * BRONZE;
 
-        transfer::public_transfer(MetalNFT {
-            id: object::new(ctx),
-            metal,
-        }, tx_context::sender(ctx));
+        transfer::public_transfer(
+            MetalNFT { id: object::new(ctx), metal, },
+            tx_context::sender(ctx)
+        );
     }
 
     // Implements "is v < w? where v <= v_max" using integer arithmetic. Returns 1 if true, 0 otherwise.
@@ -91,26 +92,20 @@ module nfts::random_nft_airdrop {
         let v = random::generate_u8_in_range(&mut generator, 1, 100);
 
         if (v <= 60) {
-            transfer::public_transfer(MetalNFT {
-                id: object::new(ctx),
-                metal: BRONZE,
-            }, tx_context::sender(ctx));
-            return
-        };
-
-        if (v <= 90) {
-            transfer::public_transfer(MetalNFT {
-                id: object::new(ctx),
-                metal: SILVER,
-            }, tx_context::sender(ctx));
-            return
-        };
-
-        if (v <= 100) {
-            transfer::public_transfer(MetalNFT {
-                id: object::new(ctx),
-                metal: GOLD,
-            }, tx_context::sender(ctx));
+            transfer::public_transfer(
+                MetalNFT { id: object::new(ctx), metal: BRONZE, },
+                tx_context::sender(ctx),
+            );
+        } else if (v <= 90) {
+            transfer::public_transfer(
+                MetalNFT { id: object::new(ctx), metal: SILVER, },
+                tx_context::sender(ctx),
+            );
+        } else if (v <= 100) {
+            transfer::public_transfer(
+                MetalNFT { id: object::new(ctx), metal: GOLD, },
+                tx_context::sender(ctx),
+            );
         };
     }
 
@@ -129,23 +124,20 @@ module nfts::random_nft_airdrop {
         let generator = new_generator(r, ctx);
         let v = random::generate_u8_in_range(&mut generator, 1, 100);
 
-        transfer::public_transfer(RandomnessNFT {
-            id: object::new(ctx),
-            value: v,
-        }, tx_context::sender(ctx));
+        transfer::public_transfer(
+            RandomnessNFT { id: object::new(ctx), value: v, },
+            tx_context::sender(ctx),
+        );
     }
 
     public fun reveal_alternative2_step2(nft: RandomnessNFT, ctx: &mut TxContext): MetalNFT {
         let RandomnessNFT { id, value } = nft;
         delete(id);
 
-        let metal = BRONZE;
-        if (value <= 10) {
-            metal = GOLD;
-        };
-        if (10 < value && value <= 40) {
-            metal = SILVER;
-        };
+        let metal =
+            if (value <= 10) GOLD
+            else if (10 < value && value <= 40) SILVER
+            else BRONZE;
 
         MetalNFT {
             id: object::new(ctx),
@@ -159,13 +151,9 @@ module nfts::random_nft_airdrop {
     }
 
     public fun metal_string(nft: &MetalNFT): string::String {
-        if (nft.metal == GOLD) {
-            return string::utf8(b"Gold")
-        };
-        if (nft.metal == SILVER) {
-            return string::utf8(b"Silver")
-        };
-        string::utf8(b"Bronze")
+        if (nft.metal == GOLD) string::utf8(b"Gold")
+        else if (nft.metal == SILVER) string::utf8(b"Silver")
+        else string::utf8(b"Bronze")
     }
 
     #[test_only]
@@ -188,8 +176,7 @@ module nfts::random_nft_airdrop_tests {
     use sui::random;
     use sui::random::{Random, update_randomness_state_for_testing};
     use sui::test_scenario::{ctx, take_from_sender, next_tx, return_to_sender};
-    use nfts::random_nft_airdrop::{MintingCapability, MetalNFT};
-    use nfts::random_nft_airdrop;
+    use nfts::random_nft_airdrop::{Self, MintingCapability, MetalNFT};
 
     #[test]
     fun test_e2e() {
@@ -206,7 +193,7 @@ module nfts::random_nft_airdrop_tests {
             &mut random_state,
             0,
             x"1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F",
-            test_scenario::ctx(scenario)
+            test_scenario::ctx(scenario),
         );
 
         test_scenario::next_tx(scenario, user1);
@@ -221,11 +208,8 @@ module nfts::random_nft_airdrop_tests {
         let seen_bronze = false;
         let i = 0;
         while (i < 20) {
-            if (i % 2 == 1) {
-                random_nft_airdrop::reveal(vector::pop_back(&mut nfts), &random_state, ctx(scenario));
-            } else {
-                random_nft_airdrop::reveal_alternative1(vector::pop_back(&mut nfts), &random_state, ctx(scenario));
-            };
+            if (i % 2 == 1) random_nft_airdrop::reveal(vector::pop_back(&mut nfts), &random_state, ctx(scenario))
+            else random_nft_airdrop::reveal_alternative1(vector::pop_back(&mut nfts), &random_state, ctx(scenario));
             next_tx(scenario, user1);
             let nft = take_from_sender(scenario);
             let metal = random_nft_airdrop::metal_string(&nft);