diff --git a/bubblesort/build/main.aleo b/bubblesort/build/main.aleo index 0fb642b..d52cd7b 100644 --- a/bubblesort/build/main.aleo +++ b/bubblesort/build/main.aleo @@ -101,3 +101,21 @@ function bubble_sort: output r61 as u32.private; output r46 as u32.private; output r28 as u32.private; + +closure is_sorted: + input r0 as [u32; 8u32]; + gt r0[0u32] r0[1u32] into r1; + gt r0[1u32] r0[2u32] into r2; + gt r0[2u32] r0[3u32] into r3; + gt r0[3u32] r0[4u32] into r4; + gt r0[4u32] r0[5u32] into r5; + gt r0[5u32] r0[6u32] into r6; + gt r0[6u32] r0[7u32] into r7; + ternary r7 false true into r8; + ternary r6 false r8 into r9; + ternary r5 false r9 into r10; + ternary r4 false r10 into r11; + ternary r3 false r11 into r12; + ternary r2 false r12 into r13; + ternary r1 false r13 into r14; + output r14 as boolean; diff --git a/bubblesort/src/main.leo b/bubblesort/src/main.leo index 05ca264..6d805b9 100644 --- a/bubblesort/src/main.leo +++ b/bubblesort/src/main.leo @@ -17,6 +17,15 @@ program bubblesort.aleo { // Note that the implementation below uses tuples instead of arrays. // The implementation also manually unrolls the loop. + function is_sorted(arr: [u32; 8]) -> bool { + for i: u32 in 0u32..7u32 { + if arr[i] > arr[i + 1u32] { + return false; + } + } + return true; + } + transition bubble_sort( arr0: u32, arr1: u32, @@ -282,6 +291,8 @@ program bubblesort.aleo { arr1 = temp; } + assert(is_sorted([arr0, arr1, arr2, arr3, arr4, arr5, arr6, arr7])); + return (arr0, arr1, arr2, arr3, arr4, arr5, arr6, arr7); } }