From c40106c15da680cf5ec6ba93dd6a4ec13affe094 Mon Sep 17 00:00:00 2001 From: damirka Date: Wed, 8 Sep 2021 19:26:27 +0300 Subject: [PATCH] adds more examples from playground, updates manifests --- examples/hello-world/Leo.toml | 4 ++ examples/linear-regression/.gitignore | 1 + examples/linear-regression/Leo.toml | 12 ++++ examples/linear-regression/README.md | 20 ++++++ .../inputs/linear-regression.in | 7 ++ .../inputs/linear-regression.state | 26 ++++++++ examples/linear-regression/src/main.leo | 65 +++++++++++++++++++ examples/palindrome/.gitignore | 1 + examples/palindrome/Leo.toml | 15 +++++ examples/palindrome/README.md | 20 ++++++ examples/palindrome/inputs/palindrome.in | 5 ++ examples/palindrome/inputs/palindrome.state | 26 ++++++++ examples/palindrome/src/main.leo | 59 +++++++++++++++++ examples/pedersen-hash/Leo.toml | 4 ++ examples/silly-sudoku/Leo.toml | 4 ++ 15 files changed, 269 insertions(+) create mode 100644 examples/linear-regression/.gitignore create mode 100644 examples/linear-regression/Leo.toml create mode 100644 examples/linear-regression/README.md create mode 100644 examples/linear-regression/inputs/linear-regression.in create mode 100644 examples/linear-regression/inputs/linear-regression.state create mode 100644 examples/linear-regression/src/main.leo create mode 100644 examples/palindrome/.gitignore create mode 100644 examples/palindrome/Leo.toml create mode 100644 examples/palindrome/README.md create mode 100644 examples/palindrome/inputs/palindrome.in create mode 100644 examples/palindrome/inputs/palindrome.state create mode 100644 examples/palindrome/src/main.leo diff --git a/examples/hello-world/Leo.toml b/examples/hello-world/Leo.toml index 7255a801f9..b481087ab6 100644 --- a/examples/hello-world/Leo.toml +++ b/examples/hello-world/Leo.toml @@ -6,3 +6,7 @@ license = "LICENSE-MIT" [remote] author = "aleo" + +[target] +curve = "bls12_377" +proving_system = "groth16" diff --git a/examples/linear-regression/.gitignore b/examples/linear-regression/.gitignore new file mode 100644 index 0000000000..17aa483ab4 --- /dev/null +++ b/examples/linear-regression/.gitignore @@ -0,0 +1 @@ +outputs/ diff --git a/examples/linear-regression/Leo.toml b/examples/linear-regression/Leo.toml new file mode 100644 index 0000000000..052350b291 --- /dev/null +++ b/examples/linear-regression/Leo.toml @@ -0,0 +1,12 @@ +[project] +name = "linear-regression" +version = "0.1.0" +description = "The linear-regression package" +license = "MIT" + +[remote] +author = "aleo" + +[target] +curve = "bls12_377" +proving_system = "groth16" diff --git a/examples/linear-regression/README.md b/examples/linear-regression/README.md new file mode 100644 index 0000000000..58b8463cbc --- /dev/null +++ b/examples/linear-regression/README.md @@ -0,0 +1,20 @@ +# linear-regression + +## Build Guide + +To compile this Leo program, run: +```bash +leo build +``` + +To test this Leo program, run: +```bash +leo test +``` + +## Development + +To output the number of constraints, run: +```bash +leo build -d +``` diff --git a/examples/linear-regression/inputs/linear-regression.in b/examples/linear-regression/inputs/linear-regression.in new file mode 100644 index 0000000000..2969aa3b83 --- /dev/null +++ b/examples/linear-regression/inputs/linear-regression.in @@ -0,0 +1,7 @@ +// The program input for light-cuddly-cyan-polo/src/main.leo +[main] +x: i32 = 1i32; +y: i32 = 2i32; + +[registers] +r0: [i32; 2] = [1i32, 2i32]; diff --git a/examples/linear-regression/inputs/linear-regression.state b/examples/linear-regression/inputs/linear-regression.state new file mode 100644 index 0000000000..3a4a276e17 --- /dev/null +++ b/examples/linear-regression/inputs/linear-regression.state @@ -0,0 +1,26 @@ +// The program state for linear-regression/src/main.leo +[[public]] + +[state] +leaf_index: u32 = 0; +root: [u8; 32] = [0; 32]; + +[[private]] + +[record] +serial_number: [u8; 64] = [0; 64]; +commitment: [u8; 32] = [0; 32]; +owner: address = aleo1daxej63vwrmn2zhl4dymygagh89k5d2vaw6rjauueme7le6k2q8sjn0ng9; +is_dummy: bool = false; +value: u64 = 0; +payload: [u8; 32] = [0; 32]; +birth_program_id: [u8; 48] = [0; 48]; +death_program_id: [u8; 48] = [0; 48]; +serial_number_nonce: [u8; 32] = [0; 32]; +commitment_randomness: [u8; 32] = [0; 32]; + +[state_leaf] +path: [u8; 128] = [0; 128]; +memo: [u8; 32] = [0; 32]; +network_id: u8 = 0; +leaf_randomness: [u8; 32] = [0; 32]; diff --git a/examples/linear-regression/src/main.leo b/examples/linear-regression/src/main.leo new file mode 100644 index 0000000000..51dc4dd908 --- /dev/null +++ b/examples/linear-regression/src/main.leo @@ -0,0 +1,65 @@ +circuit Point { + x: i32, + y: i32, + + function new(x: i32, y: i32) -> Self { + return Self { x, y }; + } +} + +circuit LinearRegression { + points: [Point; 5], + + // Instantiates a linear regression circuit. + function new(points: [Point; 5]) -> Self { + return Self { points }; + } + + // Return the slope of the linear regression. + function slope(self) -> i32 { + + let num_points = 5i32; + // Calculate the sums. + let x_sum = 0i32; + let y_sum = 0i32; + let xy_sum = 0i32; + let x2_sum = 0i32; + for i in 0..5 { + x_sum += self.points[i].x; + y_sum += self.points[i].y; + xy_sum += self.points[i].x * self.points[i].y; + x2_sum += self.points[i].x * self.points[i].x; + } + let numerator = (num_points * xy_sum) - (x_sum * y_sum); + let denominator = (num_points * x2_sum) - (x_sum * x_sum); + let slope = numerator / denominator; + return slope; + } + // Return the offset of the linear regression. + function offset(self, slope: i32) -> i32 { + let num_points = 5i32; + // Calculate the sum. + let x_sum = 0i32; + let y_sum = 0i32; + for i in 0..5 { + x_sum += self.points[i].x; + y_sum += self.points[i].y; + } + return (y_sum - slope * x_sum) / num_points; + } +} + + +function main (x: i32, y: i32) -> [i32; 2] { + let points: [Point; 5] = [ + Point{x: x + 1, y: y + 1}, + Point{x: x + 2, y: y + 2}, + Point{x: x + 3, y: y + 3}, + Point{x: x + 4, y: y + 4}, + Point{x: x + 5, y: y + 5} + ]; + let reg = LinearRegression::new(points); + let slope = reg.slope(); + let offset = reg.offset(slope); + return [slope, offset]; +} diff --git a/examples/palindrome/.gitignore b/examples/palindrome/.gitignore new file mode 100644 index 0000000000..17aa483ab4 --- /dev/null +++ b/examples/palindrome/.gitignore @@ -0,0 +1 @@ +outputs/ diff --git a/examples/palindrome/Leo.toml b/examples/palindrome/Leo.toml new file mode 100644 index 0000000000..ffcfe51757 --- /dev/null +++ b/examples/palindrome/Leo.toml @@ -0,0 +1,15 @@ +[project] +name = "palindrome" +version = "0.1.0" +description = "The palindrome package" +license = "MIT" + +[remote] +author = "aleo" + +[target] +curve = "bls12_377" +proving_system = "groth16" + +[dependencies] +# none diff --git a/examples/palindrome/README.md b/examples/palindrome/README.md new file mode 100644 index 0000000000..4703046c4e --- /dev/null +++ b/examples/palindrome/README.md @@ -0,0 +1,20 @@ +# palindrome + +## Build Guide + +To compile this Leo program, run: +```bash +leo build +``` + +To test this Leo program, run: +```bash +leo test +``` + +## Development + +To output the number of constraints, run: +```bash +leo build -d +``` diff --git a/examples/palindrome/inputs/palindrome.in b/examples/palindrome/inputs/palindrome.in new file mode 100644 index 0000000000..0e175a1c35 --- /dev/null +++ b/examples/palindrome/inputs/palindrome.in @@ -0,0 +1,5 @@ +[main] +str: [char; 20] = "borrow or rob "; // char array can be defined as a string + +[registers] +r0: bool = false; diff --git a/examples/palindrome/inputs/palindrome.state b/examples/palindrome/inputs/palindrome.state new file mode 100644 index 0000000000..ffb7590fa9 --- /dev/null +++ b/examples/palindrome/inputs/palindrome.state @@ -0,0 +1,26 @@ +// The program state for palindrome/src/main.leo +[[public]] + +[state] +leaf_index: u32 = 0; +root: [u8; 32] = [0; 32]; + +[[private]] + +[record] +serial_number: [u8; 64] = [0; 64]; +commitment: [u8; 32] = [0; 32]; +owner: address = aleo1daxej63vwrmn2zhl4dymygagh89k5d2vaw6rjauueme7le6k2q8sjn0ng9; +is_dummy: bool = false; +value: u64 = 0; +payload: [u8; 32] = [0; 32]; +birth_program_id: [u8; 48] = [0; 48]; +death_program_id: [u8; 48] = [0; 48]; +serial_number_nonce: [u8; 32] = [0; 32]; +commitment_randomness: [u8; 32] = [0; 32]; + +[state_leaf] +path: [u8; 128] = [0; 128]; +memo: [u8; 32] = [0; 32]; +network_id: u8 = 0; +leaf_randomness: [u8; 32] = [0; 32]; diff --git a/examples/palindrome/src/main.leo b/examples/palindrome/src/main.leo new file mode 100644 index 0000000000..f2b1872733 --- /dev/null +++ b/examples/palindrome/src/main.leo @@ -0,0 +1,59 @@ +// This Program takes in any 20-byte low register string and tells +// whether a string is a palindrome ignoring any spaces. + +function main(str: [char; 20]) -> bool { + return is_palindrome(str); +} + +function is_palindrome(str: [char; 20]) -> bool { + const str_len = 20u32; // saving const for convenience + + // By default we assume that input is a palindrome. + let result = true; + let processed = 0u8; + + for start in 0..(str_len / 2) { + let start_sym = str[start]; + if start_sym != ' ' { + let skipped = 0u8; + let end_empty = 0u8; + let end_sym = ' '; + + for end in (str_len - 1)..start { + if str[end] != ' ' && skipped == processed && end_sym == ' ' { + end_sym = str[end]; + } else { + end_empty = end_empty + 1; + if str[end] != ' ' { + skipped = skipped + 1; + } + } + } + + // If there are symbols left to the right from the start. + if end_sym != ' ' { + console.log("Comparing: {} ? {}", start_sym, end_sym); + + if result { + result = (start_sym == end_sym); + } + + processed = processed + 1; + } + } + } + + console.log("Result is: {}", result); + + return result; +} + +@test +function test_is_palindrome() { + console.assert(is_palindrome("a b a ")); + console.assert(is_palindrome("😀😀😀😀😀 😀😀😀😀😀")); + console.assert(is_palindrome("borrow or rob ")); + console.assert(is_palindrome("bbbb aaaa aaaa bbbb")); + console.assert(is_palindrome("aaaaaaaaaaaaaaaaaaaa")); + console.assert(is_palindrome("taco cat ")); +} diff --git a/examples/pedersen-hash/Leo.toml b/examples/pedersen-hash/Leo.toml index 5076066fa2..a217cf0127 100644 --- a/examples/pedersen-hash/Leo.toml +++ b/examples/pedersen-hash/Leo.toml @@ -6,3 +6,7 @@ license = "LICENSE-MIT" [remote] author = "aleo" + +[target] +curve = "bls12_377" +proving_system = "groth16" diff --git a/examples/silly-sudoku/Leo.toml b/examples/silly-sudoku/Leo.toml index aecf26ff25..386a97dec0 100644 --- a/examples/silly-sudoku/Leo.toml +++ b/examples/silly-sudoku/Leo.toml @@ -6,3 +6,7 @@ license = "MIT" [remote] author = "howard" + +[target] +curve = "bls12_377" +proving_system = "groth16"