[Test] Add examples to test framework. (#2527)

* Add tests

* Remove debug print

* Regen expectations

* Remove battleship example
This commit is contained in:
d0cd 2023-08-15 14:11:17 -04:00 committed by GitHub
parent 6bce40480a
commit 85d9a28ebb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
53 changed files with 2106 additions and 2 deletions

View File

@ -453,8 +453,6 @@ impl<'a> ExpressionVisitor<'a> for TypeChecker<'a> {
}
fn visit_call(&mut self, input: &'a CallExpression, expected: &Self::AdditionalInput) -> Self::Output {
println!("call_expression: {}", input);
println!("input function: {:?}", input.function);
match &*input.function {
// Note that the parser guarantees that `input.function` is always an identifier.
Expression::Identifier(ident) => {

View File

@ -0,0 +1,12 @@
---
namespace: Compile
expectation: Pass
outputs:
- - initial_ast: 488fa982ddbf04caf5103f4b0cc668f4f9680cc0631ff490db683fcb8f22df57
unrolled_ast: 488fa982ddbf04caf5103f4b0cc668f4f9680cc0631ff490db683fcb8f22df57
ssa_ast: 518d76e9043640ef4918a85bbe007e61e14d7874f586bb74ae842ab59880c4be
flattened_ast: aac61ef646dbf6dd8b5bd0b6d1d08d9220425dcdddc53a2fefcaa505d26156f6
inlined_ast: aac61ef646dbf6dd8b5bd0b6d1d08d9220425dcdddc53a2fefcaa505d26156f6
dce_ast: aac61ef646dbf6dd8b5bd0b6d1d08d9220425dcdddc53a2fefcaa505d26156f6
bytecode: ae52309998de7e291d82e92418fdbf583b182ce12e710e844550132d8743380e
warnings: ""

View File

@ -0,0 +1,12 @@
---
namespace: Compile
expectation: Pass
outputs:
- - initial_ast: 41c3a20f26c3fcd5b50600265222be40e6c816fd98a7b48c8f6b919ddd48150a
unrolled_ast: 10ee29926d7e4943001060baa5a676230692eb5106a96fd6a57d4f677abf40d1
ssa_ast: 723bbafebdcfd8919f420c4a4f0e19a0f0047f5bc5be92d504f233c0800f9e8a
flattened_ast: e357bb7b32a318e34d992f3a08a991f2a84a259ef2c2b9516cf44e51fb1ebbdf
inlined_ast: 0bd67c85431224ae28eb008210da8fee93bcef161f7d9151d3eb377c85fd407e
dce_ast: 0bd67c85431224ae28eb008210da8fee93bcef161f7d9151d3eb377c85fd407e
bytecode: be8569b9078a1e6b21fe4e02ce9ab03cbe599d3cddb777fb6a146da15ca71559
warnings: ""

View File

@ -0,0 +1,12 @@
---
namespace: Compile
expectation: Pass
outputs:
- - initial_ast: 846f1f965a6e347f8eda2908e1b1884d07564463e63666db67cda3f1a41c705b
unrolled_ast: 846f1f965a6e347f8eda2908e1b1884d07564463e63666db67cda3f1a41c705b
ssa_ast: 3ae722041170e0425672f25a30f3c1abbcd1f0f65895bb77873e565b09bfcd39
flattened_ast: 36da9aa2a2cb398f32bd45849d848ccd2ffd194a3e60cf8f8b1b28379d049894
inlined_ast: 36da9aa2a2cb398f32bd45849d848ccd2ffd194a3e60cf8f8b1b28379d049894
dce_ast: 36da9aa2a2cb398f32bd45849d848ccd2ffd194a3e60cf8f8b1b28379d049894
bytecode: aefb5e5a0f121ad8132981b01cc28fb487f749faf8306b7dc9d1b6c3400af180
warnings: ""

View File

@ -0,0 +1,12 @@
---
namespace: Compile
expectation: Pass
outputs:
- - initial_ast: c96e3280c8db92fd9853f05ccc63919b227c36bfa291e96c2ac9381f3bf3138a
unrolled_ast: c96e3280c8db92fd9853f05ccc63919b227c36bfa291e96c2ac9381f3bf3138a
ssa_ast: 536b70c879475f8ce3eb12d81b3da915c5a6e5aac542aeb70ce76dc23fc49479
flattened_ast: dd296212367338dc6b0696950ff6cad83ec5003a93b9922aabfb5cec177dd03e
inlined_ast: dd296212367338dc6b0696950ff6cad83ec5003a93b9922aabfb5cec177dd03e
dce_ast: dd296212367338dc6b0696950ff6cad83ec5003a93b9922aabfb5cec177dd03e
bytecode: 5adbf2387ed6209b64c8248741f74929f524771803ef803d5d9f9f8fb0d66ee7
warnings: ""

View File

@ -0,0 +1,12 @@
---
namespace: Compile
expectation: Pass
outputs:
- - initial_ast: 569f3a4ef4b0b9adeaf6633b03a390c539db014b9735d1daceb1b2e9ba5eaca9
unrolled_ast: 569f3a4ef4b0b9adeaf6633b03a390c539db014b9735d1daceb1b2e9ba5eaca9
ssa_ast: 72eff06c68605089db31b51d2146a6cf5e57536a5508ca6e62db9c4e9600283c
flattened_ast: 6f296fcf4fe73acc0329ea4ff620b66c07d1f99cda03520787045182008a3551
inlined_ast: 6f296fcf4fe73acc0329ea4ff620b66c07d1f99cda03520787045182008a3551
dce_ast: 6f296fcf4fe73acc0329ea4ff620b66c07d1f99cda03520787045182008a3551
bytecode: 97f5fb0da6f869aa976736f28bedc7d21d49fc7debdb7c5eda6789c82ec61d67
warnings: ""

View File

@ -0,0 +1,12 @@
---
namespace: Compile
expectation: Pass
outputs:
- - initial_ast: dc996e9af5d534e7cf9195f0a56324946a1c9a1afef58790b8c650a8b57d0372
unrolled_ast: fbbae2e10b49d55834380b84c51b333235755e54386f3e79da9f425afcde7a86
ssa_ast: a7d6b442de1fe56d192e9298ad86d94200ed84857285f8218c2e04e304ee1131
flattened_ast: 11aa1d857ac77010f9a23f1ea6a60ba0e1ed6103e5643f57ec236686c1ed5e73
inlined_ast: 3272a92f7c734ff9d8e57ff13c35cbf9ca29a215f7e7cf7ff8e20b187dfd93ca
dce_ast: cb1299f87f98d2e553cb557cb183c4556e4f9ee57debf6b522c3a25358ad3330
bytecode: 3b90abd4333a964993382d9f47ba381cdd732a342f8b28828b99870c6dfafffc
warnings: ""

View File

@ -0,0 +1,12 @@
---
namespace: Compile
expectation: Pass
outputs:
- - initial_ast: 4daac9ac0cfb3d789eca3415a8c93969e73932b0e8af9b7022a88ebc8b962808
unrolled_ast: 4daac9ac0cfb3d789eca3415a8c93969e73932b0e8af9b7022a88ebc8b962808
ssa_ast: 792289b3b95e7e84783eb4ccc4859bc3c6e47c498af982c4d35c7bfedbd126e1
flattened_ast: 3dc1e22cd1aa348aea3d44e5511a35cff83e22774885b919b02fd00e33b5a2fe
inlined_ast: 3dc1e22cd1aa348aea3d44e5511a35cff83e22774885b919b02fd00e33b5a2fe
dce_ast: 3dc1e22cd1aa348aea3d44e5511a35cff83e22774885b919b02fd00e33b5a2fe
bytecode: 45196976b60c465ad542b11fe200c16d15959a4bf4d4a48f348aab42df3407ef
warnings: ""

View File

@ -0,0 +1,12 @@
---
namespace: Compile
expectation: Pass
outputs:
- - initial_ast: ce145a4c6345a1f4fd6a9654b741085eca227ed6139a1f87477c53b425ddae27
unrolled_ast: ce145a4c6345a1f4fd6a9654b741085eca227ed6139a1f87477c53b425ddae27
ssa_ast: 6d5f714e2eb32059a1ff7dd71dc9bf7141a4ee508ecb53d1bf87734ebf0a78b3
flattened_ast: fcf93c13535b97b4a9344c5faec1fe31611dea5af71d0f9b8c5a0c902af855d8
inlined_ast: fcf93c13535b97b4a9344c5faec1fe31611dea5af71d0f9b8c5a0c902af855d8
dce_ast: fcf93c13535b97b4a9344c5faec1fe31611dea5af71d0f9b8c5a0c902af855d8
bytecode: 774672545059d524d17b2709ca4d2f66dcc7fca13c4199ff8b5bf4a03d4d6c6a
warnings: ""

View File

@ -0,0 +1,12 @@
---
namespace: Compile
expectation: Pass
outputs:
- - initial_ast: 88df12d34ab9342d3a3f5eb67edc7863d364d2c88f95aa894318cbe0f49ac38b
unrolled_ast: 75f92576c2352d0b5f95d8b2ed7a7d5cf4fceca8db45ec88e0d153e7fb734a87
ssa_ast: d9bfa7056f38a7e9e2c3f6a6dc79e2116c54da64759aeab39beae638d588eb29
flattened_ast: 1e3d3e25bec1dbc42b859234171828a48d35d382a82ea6920601039835519830
inlined_ast: 1e3d3e25bec1dbc42b859234171828a48d35d382a82ea6920601039835519830
dce_ast: 2e942a7f39cce403456bb5d500bfd23e829a846b2904e600ca2141a3581aaa6a
bytecode: 1425e7c87a38072c342ef9505090c0b039663ac1c5ce41632f999b376c81d348
warnings: ""

View File

@ -0,0 +1,12 @@
---
namespace: Compile
expectation: Pass
outputs:
- - initial_ast: 55a621ce4eaded6a1addf66967effac0c92df42ae52b692aba53b01a345dcdb1
unrolled_ast: 55a621ce4eaded6a1addf66967effac0c92df42ae52b692aba53b01a345dcdb1
ssa_ast: b099640b29fa1962444842a0eabc36468a2ae97e5535b3e5b34e21b6bebdab53
flattened_ast: 700323736afe0f4aded427165513eeb6e4041425cf89a059cadb263308f4452e
inlined_ast: 700323736afe0f4aded427165513eeb6e4041425cf89a059cadb263308f4452e
dce_ast: 700323736afe0f4aded427165513eeb6e4041425cf89a059cadb263308f4452e
bytecode: e37c8723337c177f6b9b7b5eac3e4b9aa8e45b28d26ea76439161cb97e4d251a
warnings: ""

View File

@ -0,0 +1,12 @@
---
namespace: Compile
expectation: Pass
outputs:
- - initial_ast: 09f0640c57c1180aca40950a22b8c1cc0097fca327f8febd16c90da502cab0b3
unrolled_ast: 09f0640c57c1180aca40950a22b8c1cc0097fca327f8febd16c90da502cab0b3
ssa_ast: 2553c4a5aafe6f160cbab75eba74adf81363325ae9dd7a8d39aa69f293c007ae
flattened_ast: a7cfb0cdd509b566ac2dcb7d584b805b34cc99100f780744ede7050384687e15
inlined_ast: a7cfb0cdd509b566ac2dcb7d584b805b34cc99100f780744ede7050384687e15
dce_ast: a7cfb0cdd509b566ac2dcb7d584b805b34cc99100f780744ede7050384687e15
bytecode: ecb647f74261a2c1212405edf2024aed89ab5e3c19353127dacdc9e31ccaf0f1
warnings: ""

View File

@ -0,0 +1,12 @@
---
namespace: Compile
expectation: Pass
outputs:
- - initial_ast: b0144d159de2d8181f7b5bc763b3725786ab753e2464f575559a63b69056fbc0
unrolled_ast: b0144d159de2d8181f7b5bc763b3725786ab753e2464f575559a63b69056fbc0
ssa_ast: 841bd1ce818dcc2c68eb334ee0a4c3a8e6e7ba8be5163c9158e7a6d05d659c40
flattened_ast: d3e2138eed0db2ef0f602303296e89da639172ce5c00b94a22b98b90f0dd3868
inlined_ast: d3e2138eed0db2ef0f602303296e89da639172ce5c00b94a22b98b90f0dd3868
dce_ast: d3e2138eed0db2ef0f602303296e89da639172ce5c00b94a22b98b90f0dd3868
bytecode: 6ea0a455c7cc5f2bd868d5780a7735c599fb95c99157997d156dce175d6c6e94
warnings: ""

View File

@ -0,0 +1,12 @@
---
namespace: Compile
expectation: Pass
outputs:
- - initial_ast: 3fb50c19686b9491a2fb28e09ec516b8bb0660d14c671d14b227590af200ffde
unrolled_ast: 3fb50c19686b9491a2fb28e09ec516b8bb0660d14c671d14b227590af200ffde
ssa_ast: a6b83f8abe2fa537e88d9897915c9e852093e0c492e734021770ade295a7e210
flattened_ast: 72bb0c3c7da582964137569874eddbf9faea74ff00c5d656222132ce5c844fe0
inlined_ast: 45a539d27328951ad7969bae4f75ecfac61d74a58a062e9a3702cb63fbde72a2
dce_ast: 45a539d27328951ad7969bae4f75ecfac61d74a58a062e9a3702cb63fbde72a2
bytecode: ecf52756cc54e0e43ccfeb4db8369ff820a309fd7061bfaad5dcf535b58782b3
warnings: ""

View File

@ -0,0 +1,12 @@
---
namespace: Compile
expectation: Pass
outputs:
- - initial_ast: cc477fe1115e114deb280718a6b4afaf4086a9c5946250391cca39f75cf8fc65
unrolled_ast: cc477fe1115e114deb280718a6b4afaf4086a9c5946250391cca39f75cf8fc65
ssa_ast: 935b7b49322c886490bee8c08cefa47a9cafd6cc43334e1bf918006bcc9ee636
flattened_ast: a9046be9e043c852c8fb5752a9ea3471885c98f691f5ded132ab05f193c4edce
inlined_ast: a9046be9e043c852c8fb5752a9ea3471885c98f691f5ded132ab05f193c4edce
dce_ast: a9046be9e043c852c8fb5752a9ea3471885c98f691f5ded132ab05f193c4edce
bytecode: 5fd0ec18707f7e6af93b8eacd72590b4bfd68559dba48ab95574a0b44a0b3313
warnings: ""

View File

@ -0,0 +1,12 @@
---
namespace: Compile
expectation: Pass
outputs:
- - initial_ast: 5ea4a7bb8c1c9fdab3ea6041061393cdffeb27a435e6619023d453a5f13baf70
unrolled_ast: de99997e3917d401c328120d40971e9c017cff959c4a5b60708713699ccc6b05
ssa_ast: 2cef04a3b62df151bef34671c5f91038731885fcf902b2d4ddffea27f8c7edbf
flattened_ast: b3c19c72a9b5576f1d48034b9e4232f716a7e380114ca2c59d6daeaf08976111
inlined_ast: b3c19c72a9b5576f1d48034b9e4232f716a7e380114ca2c59d6daeaf08976111
dce_ast: 8881cf357a23d9bfb87ca5c9de4aae9814888140c6c02e60e2453db0095dc2ec
bytecode: cca4637103b23653c5a99744693068186bc6d89052df73b09c1601c7f85f0eed
warnings: ""

View File

@ -0,0 +1,12 @@
---
namespace: Compile
expectation: Pass
outputs:
- - initial_ast: 30cdb3686ebc392c0d160cb83319d4fde5803b89bf5b712d234504e1df0d7aac
unrolled_ast: 30cdb3686ebc392c0d160cb83319d4fde5803b89bf5b712d234504e1df0d7aac
ssa_ast: f38e7c0b7f1067ae90a8574e9a6ac2bcde92378f4996d8154a8916af5a2aecf9
flattened_ast: e08aef327c91e58634d8e8373722d5039f8b68f0e8d9ee9853783bf6091fcb1c
inlined_ast: e08aef327c91e58634d8e8373722d5039f8b68f0e8d9ee9853783bf6091fcb1c
dce_ast: e08aef327c91e58634d8e8373722d5039f8b68f0e8d9ee9853783bf6091fcb1c
bytecode: 9aba49a906bfc3f931cb314bd970e04dc8b74966ec2888efecc4f0f8795dc368
warnings: ""

View File

@ -0,0 +1,12 @@
---
namespace: Compile
expectation: Pass
outputs:
- - initial_ast: 325393e353d5c5600e1bdda2eb80bf7ff93a07a984ed399e1b27b48fe1174c75
unrolled_ast: 325393e353d5c5600e1bdda2eb80bf7ff93a07a984ed399e1b27b48fe1174c75
ssa_ast: 6b9bc0852f6fd99c1ea182713658a5c7b038a3c3f772cd67386d0f2eef975bb0
flattened_ast: 7e7a3d85c8b2866e5861ff3a939798ca73e4631c6e9b82bd5f77dc48fd1b9c93
inlined_ast: 9490f8b7a959d46206fe06377c5d9c45af2d18f36c40bba3572851b25f882fd1
dce_ast: 9490f8b7a959d46206fe06377c5d9c45af2d18f36c40bba3572851b25f882fd1
bytecode: 38e21ed198874357b0a83d451d9498a59838a7f5ad979d372a405b5e6e5a4e17
warnings: ""

View File

@ -0,0 +1,12 @@
---
namespace: Compile
expectation: Pass
outputs:
- - initial_ast: 66e1c5c7e80b1d64c1dab63525cf953ceb781b958e2dbabc84ca9236fcb7de43
unrolled_ast: 66e1c5c7e80b1d64c1dab63525cf953ceb781b958e2dbabc84ca9236fcb7de43
ssa_ast: c0a513083204cd89a39b0d900aabdb3b36e8ecfb9f69c584c477622463f15ca3
flattened_ast: 7d5cd9818642f3280991d79009b5b39f8c611fca877c7b798488b7f531732ae2
inlined_ast: db466e4f2650dd789227ccaddbd8779101ef2a874341a0b3a72c18877e5ab8e9
dce_ast: db466e4f2650dd789227ccaddbd8779101ef2a874341a0b3a72c18877e5ab8e9
bytecode: d2f0d0e9487f69b3c04cf702ee2d6a8d780ed928cee6d3d05a0fe423b3ad3c6b
warnings: ""

View File

@ -0,0 +1,12 @@
---
namespace: Compile
expectation: Pass
outputs:
- - initial_ast: f4dc53169099fc3b0fa881037c129a2fb60feef43a7b38d59fcc6ab9a5a1defc
unrolled_ast: f4dc53169099fc3b0fa881037c129a2fb60feef43a7b38d59fcc6ab9a5a1defc
ssa_ast: bcfd6276d070bb0d71fd75906a9fd9993ff3e0361445ff75ce392336edf313e5
flattened_ast: 60a48de74f05d87e731d9843e719d68a3b216e06b062e978fa523cbc035f8583
inlined_ast: 60a48de74f05d87e731d9843e719d68a3b216e06b062e978fa523cbc035f8583
dce_ast: 60a48de74f05d87e731d9843e719d68a3b216e06b062e978fa523cbc035f8583
bytecode: 3516104be238849345d986d90ff7aa2bc01fe31609f34330e279eef25edb7752
warnings: ""

View File

@ -0,0 +1,12 @@
---
namespace: Compile
expectation: Pass
outputs:
- - initial_ast: f72c51049824b8b9ea0fcf4be617223c7b892ecddd3329dbb9db2c6d7182192c
unrolled_ast: f72c51049824b8b9ea0fcf4be617223c7b892ecddd3329dbb9db2c6d7182192c
ssa_ast: b24fe1ccd9545d1e3809863817bb2c5752c83569d30844cf95df272afee361c1
flattened_ast: 3b1587db79efedc180c239bf1ad3b5e0bd412c437d3e8a3560ab20fa9fd15196
inlined_ast: 3b1587db79efedc180c239bf1ad3b5e0bd412c437d3e8a3560ab20fa9fd15196
dce_ast: 3b1587db79efedc180c239bf1ad3b5e0bd412c437d3e8a3560ab20fa9fd15196
bytecode: 447867c0cff55fb14313d71ddd48f1a8fbee509cd357304c12fba830841dcd09
warnings: ""

View File

@ -0,0 +1,12 @@
---
namespace: Compile
expectation: Pass
outputs:
- - initial_ast: 9abcbbc86b5a396c14bae1f63f9279492491333c3e938c632f745c0deaa37000
unrolled_ast: 9abcbbc86b5a396c14bae1f63f9279492491333c3e938c632f745c0deaa37000
ssa_ast: 4ed46240df8b26562290175f51f974d25b206e826d22914bea9b670cc3223aa7
flattened_ast: 13201a740fd9eec1dc432d6ece2e555588e5979fe27f51bc88e9fb7a9f87ceed
inlined_ast: 13201a740fd9eec1dc432d6ece2e555588e5979fe27f51bc88e9fb7a9f87ceed
dce_ast: 13201a740fd9eec1dc432d6ece2e555588e5979fe27f51bc88e9fb7a9f87ceed
bytecode: 1fb1eb1a0d28634e2e0ac374be81010d733d3749be3b2700cead1f03266ddfb0
warnings: ""

View File

@ -0,0 +1,12 @@
---
namespace: Compile
expectation: Pass
outputs:
- - initial_ast: 445728bf577887e245e9e4fc69d77b5c352449594ac0d836d736cce2c4eecff5
unrolled_ast: 445728bf577887e245e9e4fc69d77b5c352449594ac0d836d736cce2c4eecff5
ssa_ast: 5253a2ab30e82c18a94e9c5c15f7556296120b3b33cd71877a16b6a43c742f3a
flattened_ast: a5b770b7c2c22a8dca36985bb771cbe576b2928259b36e1d879efde3dc3bcf7c
inlined_ast: a5b770b7c2c22a8dca36985bb771cbe576b2928259b36e1d879efde3dc3bcf7c
dce_ast: d71991bf377ed1aeedec58111f269aaa48bb17a147d61390617fd301cb8f855c
bytecode: 82d12cfea48eff976f9f70a6846c7f25870209fc3edf10b45b5f862a25ad3f40
warnings: ""

View File

@ -0,0 +1,12 @@
---
namespace: Compile
expectation: Pass
outputs:
- - initial_ast: 5b8963aa0268edeb94e7e6b86ba3695dff6663f649f760c1e3b57f47714faf19
unrolled_ast: 5b8963aa0268edeb94e7e6b86ba3695dff6663f649f760c1e3b57f47714faf19
ssa_ast: 378a86f7ac8b4539d5dc0a11468a07fb79a09ccd27ffa01045873034faaccf3a
flattened_ast: ac3e006e622979a3193e78974b573b7351d75495329d00ad0b487a2aae4dfff6
inlined_ast: ac3e006e622979a3193e78974b573b7351d75495329d00ad0b487a2aae4dfff6
dce_ast: ac3e006e622979a3193e78974b573b7351d75495329d00ad0b487a2aae4dfff6
bytecode: d3a0ab755832ab3078442873d2740259f9e7d344c3f2d628dfea477a2bb86d86
warnings: ""

View File

@ -0,0 +1,12 @@
---
namespace: Compile
expectation: Pass
outputs:
- - initial_ast: 0eff69a3337e386d398f6e65af9d297ae3a9e5d41e761a318226d7aee105cdce
unrolled_ast: e161d165930fae52dbc037e474d6a045493b7472a249e57eb8575ee5014d5b88
ssa_ast: 2caa5a8d664e429843fef2e78b71d704c52bc62750ef8a752863c00f53ec9493
flattened_ast: 077a694c050495eae1c2ea4888b49201021d7a867b44d4e26079d5c819f3c128
inlined_ast: 3704526b2fa81c702a76af5e692360993a90408199f8b93ce666ea0bbfdb56a8
dce_ast: cfa175a7813d9865b783936b28e61fbec74003d7f7108553db6342d8be34e18a
bytecode: c5073e255b7504fbc368079e634a99935c6c645c9db6830212e2c6077f8ebf3f
warnings: ""

View File

@ -0,0 +1,12 @@
---
namespace: Compile
expectation: Pass
outputs:
- - initial_ast: 277e82a60d41220c32c91a9b993a5115cf843d467cfa124d848fde0748b8e465
unrolled_ast: 277e82a60d41220c32c91a9b993a5115cf843d467cfa124d848fde0748b8e465
ssa_ast: 739853035542faa800879df82b1bf4d8b5fdd9656b9fef88b86dddd6abf50571
flattened_ast: 49fb28e4e3b3f6db53c6191f85ca243ee86204972d57a56d0ecd7c8221d9d832
inlined_ast: 49fb28e4e3b3f6db53c6191f85ca243ee86204972d57a56d0ecd7c8221d9d832
dce_ast: 49fb28e4e3b3f6db53c6191f85ca243ee86204972d57a56d0ecd7c8221d9d832
bytecode: 153cfd2616e879c311c136713624e83ef42642241ffebf540e308a29a610b058
warnings: ""

View File

@ -0,0 +1,12 @@
---
namespace: Compile
expectation: Pass
outputs:
- - initial_ast: 572c6d5e9fcb80d37d20b9be077d0634e15fd1a4450b69d2d1285265786df279
unrolled_ast: 572c6d5e9fcb80d37d20b9be077d0634e15fd1a4450b69d2d1285265786df279
ssa_ast: 8c1d72734789480828df90732d3c2a2637212ee7b4a25e8cba4cd694018a6ca3
flattened_ast: 13722640975be076b56ea00fef2cf93bd004273741013fa69d119ff9c00fdccb
inlined_ast: 13722640975be076b56ea00fef2cf93bd004273741013fa69d119ff9c00fdccb
dce_ast: 13722640975be076b56ea00fef2cf93bd004273741013fa69d119ff9c00fdccb
bytecode: 60c1cfd246e9ec22ae7489ba2712e71fbc832552d2aa0af46013c4a398c02fc6
warnings: ""

View File

@ -0,0 +1,71 @@
/*
namespace: Compile
expectation: Pass
*/
program test.aleo {
// A bid in an auction.
// - `owner` : The address of the account that owns the record associated with this bid.
// This is separate from the address of the account that placed the bid.
// - `bidder` : The address of the account that placed the bid.
// - `amount` : The amount of the bid.
// - `is_winner` : Whether the bid is the winning bid.
record Bid {
owner: address,
bidder: address,
amount: u64,
is_winner: bool,
}
// Returns a new bid.
// - `bidder` : The address of the account that placed the bid.
// - `amount` : The amount of the bid.
// Requires that `bidder` matches the function caller.
// The owner of the record is set to the entity responsible for running the auction (auction runner).
// The address of the auction runner is aleo1fxs9s0w97lmkwlcmgn0z3nuxufdee5yck9wqrs0umevp7qs0sg9q5xxxzh.
transition place_bid(bidder: address, amount: u64) -> Bid {
// Ensure the caller is the auction bidder.
assert_eq(self.caller, bidder);
// Return a new 'Bid' record for the auction bidder.
return Bid {
owner: aleo1fxs9s0w97lmkwlcmgn0z3nuxufdee5yck9wqrs0umevp7qs0sg9q5xxxzh,
bidder: bidder,
amount: amount,
is_winner: false,
};
}
// Returns the winning bid.
// - `first` : The first bid.
// - `second` : The second bid.
// Requires that the function caller is the auction runner.
// Assumes that the function is invoked only after the bidding period has ended.
// In the event of a tie, the first bid is selected.
transition resolve(first: Bid, second: Bid) -> Bid {
// Ensure the caller is the auctioneer.
assert_eq(self.caller, aleo1fxs9s0w97lmkwlcmgn0z3nuxufdee5yck9wqrs0umevp7qs0sg9q5xxxzh);
// Resolve the winner of the auction.
if (first.amount >= second.amount) {
return first;
} else {
return second;
}
}
// Returns ownership of the bid to bidder.
// - `bid` : The winning bid.
// Requires that the function caller is the auction runner.
// Assumes that the function is invoked only after all bids have been resolved.
transition finish(bid: Bid) -> Bid {
// Ensure the caller is the auctioneer.
assert_eq(self.caller, aleo1fxs9s0w97lmkwlcmgn0z3nuxufdee5yck9wqrs0umevp7qs0sg9q5xxxzh);
// Return 'is_winner' as 'true' in the winning 'Bid'.
return Bid {
owner: bid.bidder,
bidder: bid.bidder,
amount: bid.amount,
is_winner: true,
};
}
}

View File

@ -0,0 +1,101 @@
/*
namespace: Compile
expectation: Pass
*/
program test.aleo {
// A token, issued by a bank.
// - 'owner' : The address of the account that owns the record associated with this token.
// - 'amount' : The amount of tokens owned by the account.
record Token {
owner: address,
amount: u64,
}
// An on-chain mapping, storing the amount of tokens owned by each account
// The account is stored as a to preserve user privacy.
mapping balances: field => u64;
// Returns a new Token.
// - `owner` : The address of the account to issue the token to.
// - `amount`: The amount of tokens to issue.
// Requires that the function caller is the bank.
// The bank's address is aleo1t0uer3jgtsgmx5tq6x6f9ecu8tr57rzzfnc2dgmcqldceal0ls9qf6st7a.
transition issue(owner: address, amount: u64) -> Token {
assert_eq(self.caller, aleo1t0uer3jgtsgmx5tq6x6f9ecu8tr57rzzfnc2dgmcqldceal0ls9qf6st7a);
return Token {
owner: owner,
amount: amount,
};
}
// Deposits some amount of money into the bank.
// Returns a new Token with the remaining amount of money.
// - `token` : A record containing tokens to deposit.
// - `amount`: The amount of tokens to deposit.
transition deposit(token: Token, amount: u64) -> Token {
let difference: u64 = token.amount - amount;
let remaining: Token = Token {
owner: token.owner,
amount: difference,
};
// Compute the hash of the token owner.
let hash: field = BHP256::hash_to_field(token.owner);
return remaining then finalize(hash, amount);
}
// Updates on-chain state by the amount of tokens deposited.
// - `hash` : The hash of the token owner.
// - `amount`: The amount of tokens that were deposited.
finalize deposit(hash: field, amount: u64) {
let current_amount: u64 = Mapping::get_or_use(balances, hash, 0u64);
Mapping::set(balances, hash, current_amount + amount);
}
// Returns a new Token containing the amount of money withdrawn.
// - `recipient`: The address of the account to withdraw the tokens to.
// - `amount` : The amount of tokens to withdraw.
// - `rate` : The compound interest rate.
// - `periods` : The number of periods to compound the interest over.
// Requires that the function caller is the bank.
transition withdraw(recipient: address, amount: u64, rate: u64, periods: u64) -> Token {
assert_eq(self.caller, aleo1t0uer3jgtsgmx5tq6x6f9ecu8tr57rzzfnc2dgmcqldceal0ls9qf6st7a);
let hash: field = BHP256::hash_to_field(recipient);
let total: u64 = calculate_interest(amount, rate, periods);
let token: Token = Token {
owner: recipient,
amount: total,
};
return token then finalize(hash, amount);
}
// Updates on-chain state by the amount of tokens withdrawn.
// - `hash` : The hash of the token owner.
// - `amount`: The amount of tokens that were withdrawn.
finalize withdraw(hash: field, amount: u64) {
let current_amount: u64 = Mapping::get_or_use(balances, hash, 0u64);
Mapping::set(balances, hash, current_amount - amount);
}
// Returns the total amount of tokens after compounding interest.
// - `principal`: The amount of tokens to compound interest over.
// - `rate` : The compound interest rate.
// - `periods` : The number of periods to compound the interest over.
function calculate_interest(principal: u64, rate: u64, periods: u64) -> u64 {
let amount: u64 = principal;
for i:u64 in 0u64..100u64 {
if i < periods {
amount += (amount * rate) / 10000u64;
}
}
return amount;
}
}

View File

@ -0,0 +1,115 @@
/*
namespace: Compile
expectation: Pass
*/
program test.aleo {
// Battleship boards are represented by 8x8 squares.
// A u64 is all that is required to represent a hit or a miss on a single board.
// Starting from the top row, left to right, a hit is 1 and a miss is 0.
// A first move resulting in a hit in row 1, column 3 would be:
// 00100000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
// A second u64 is needed to represent which squares have been played, with 1s being played squares and 0s being
// unplayed squares.
record board_state {
owner: address,
// The hits and misses registered on the opponent's board.
hits_and_misses: u64,
// The squares that have been played on the opponent's board.
played_tiles: u64,
// The ship bitstring representing all ship positions on your own board
ships: u64,
player_1: address,
player_2: address,
game_started: bool,
}
// Returns a new board_state.
transition new_board_state(
ships: u64,
opponent: address,
) -> board_state {
return board_state {
owner: self.caller,
hits_and_misses: 0u64,
played_tiles: 0u64,
ships,
player_1: self.caller,
player_2: opponent,
game_started: false,
};
}
// Returns a new board state that has been started.
// Fails if this board has been started before.
transition start_board(
// The record of the board to start. A board can only be started once.
board: board_state,
) -> board_state {
// Ensure this board hasn't been used to start a game before.
assert(!board.game_started);
return board_state {
owner: board.owner,
hits_and_misses: board.hits_and_misses,
played_tiles: board.played_tiles,
ships: board.ships,
player_1: board.player_1,
player_2: board.player_2,
game_started: true,
};
}
// Returns a new board state record that includes all the played tiles.
// Fails if r1 has been played before.
transition update_played_tiles(
// The record of the board to update.
board: board_state,
// The u64 equivalent of a bitstring fire coordinate to send to the opponent.
shoot: u64,
) -> board_state {
// Need to make sure r1 is a valid move. Only one bit of r1 should be flipped.
let flip_bit: u64 = shoot - 1u64;
// bitwise and operation
let check_move: u64 = shoot & flip_bit;
assert_eq(check_move, 0u64);
// Need to make sure r1 is a valid move given the played_tiles. no bits should overlap.
let check_tiles: u64 = shoot & board.played_tiles;
assert_eq(check_tiles, 0u64);
// Update played tiles.
let played_tiles: u64 = board.played_tiles | shoot;
return board_state {
owner: board.owner,
hits_and_misses: board.hits_and_misses,
played_tiles,
ships: board.ships,
player_1: board.player_1,
player_2: board.player_2,
game_started: board.game_started,
};
}
// Returns a new board state record that includes all the hits and misses.
transition update_hits_and_misses(
// The record of the board to update.
board: board_state,
// The u64 equivalent of a bitstring of whether this player's previous move was a hit or miss.
hit_or_miss: u64,
) -> board_state {
// Update hits and misses.
let hits_and_misses: u64 = board.hits_and_misses | hit_or_miss;
return board_state {
owner: board.owner,
hits_and_misses,
played_tiles: board.played_tiles,
ships: board.ships,
player_1: board.player_1,
player_2: board.player_2,
game_started: board.game_started,
};
}
}

View File

@ -0,0 +1,292 @@
/*
namespace: Compile
expectation: Pass
*/
program test.aleo {
// Executes the bubble sorting algorithm.
// The original algorithm is given below.
//
// for i in 0..7 {
// for j in 0..7-i {
// // Move the smaller elements forward
// if arr[j+1] < arr[j] {
// // Swap the elements at indexes j and j+1
// let swap = arr[j];
// arr[j] = arr[j+1];
// arr[j+1] = swap;
// }
// }
// }
//
// Note that the implementation below uses tuples instead of arrays.
// The implementation also manually unrolls the loop.
transition bubble_sort(
arr0: u32,
arr1: u32,
arr2: u32,
arr3: u32,
arr4: u32,
arr5: u32,
arr6: u32,
arr7: u32,
) -> (u32, u32, u32, u32, u32, u32, u32, u32) {
// Unroll the loops.
// (i, j) = (0, 0).
// Move the smaller elements forward.
if arr1 < arr0 {
// Swap the elements at indexes j and j+1
let temp: u32 = arr0;
arr0 = arr1;
arr1 = temp;
}
// (i, j) = (0, 1).
// Move the smaller elements forward.
if arr2 < arr1 {
// Swap the elements at indexes j and j+1
let temp: u32 = arr1;
arr1 = arr2;
arr2 = temp;
}
// (i, j) = (0, 2).
// Move the smaller elements forward.
if arr3 < arr2 {
// Swap the elements at indexes j and j+1
let temp: u32 = arr2;
arr2 = arr3;
arr3 = temp;
}
// (i, j) = (0, 3).
// Move the smaller elements forward.
if arr4 < arr3 {
// Swap the elements at indexes j and j+1
let temp: u32 = arr3;
arr3 = arr4;
arr4 = temp;
}
// (i, j) = (0, 4).
// Move the smaller elements forward.
if arr5 < arr4 {
// Swap the elements at indexes j and j+1
let temp: u32 = arr4;
arr4 = arr5;
arr5 = temp;
}
// (i, j) = (0, 5).
// Move the smaller elements forward.
if arr6 < arr5 {
// Swap the elements at indexes j and j+1
let temp: u32 = arr5;
arr5 = arr6;
arr6 = temp;
}
// (i, j) = (0, 6).
// Move the smaller elements forward.
if arr7 < arr6 {
// Swap the elements at indexes j and j+1
let temp: u32 = arr6;
arr6 = arr7;
arr7 = temp;
}
// (i, j) = (1, 0).
// Move the smaller elements forward.
if arr1 < arr0 {
// Swap the elements at indexes j and j+1
let temp: u32 = arr0;
arr0 = arr1;
arr1 = temp;
}
// (i, j) = (1, 1).
// Move the smaller elements forward.
if arr2 < arr1 {
// Swap the elements at indexes j and j+1
let temp: u32 = arr1;
arr1 = arr2;
arr2 = temp;
}
// (i, j) = (1, 2).
// Move the smaller elements forward.
if arr3 < arr2 {
// Swap the elements at indexes j and j+1
let temp: u32 = arr2;
arr2 = arr3;
arr3 = temp;
}
// (i, j) = (1, 3).
// Move the smaller elements forward.
if arr4 < arr3 {
// Swap the elements at indexes j and j+1
let temp: u32 = arr3;
arr3 = arr4;
arr4 = temp;
}
// (i, j) = (1, 4).
// Move the smaller elements forward.
if arr5 < arr4 {
// Swap the elements at indexes j and j+1
let temp: u32 = arr4;
arr4 = arr5;
arr5 = temp;
}
// (i, j) = (1, 5).
// Move the smaller elements forward.
if arr6 < arr5 {
// Swap the elements at indexes j and j+1
let temp: u32 = arr5;
arr5 = arr6;
arr6 = temp;
}
// (i, j) = (2, 0).
// Move the smaller elements forward.
if arr1 < arr0 {
// Swap the elements at indexes j and j+1
let temp: u32 = arr0;
arr0 = arr1;
arr1 = temp;
}
// (i, j) = (2, 1).
// Move the smaller elements forward.
if arr2 < arr1 {
// Swap the elements at indexes j and j+1
let temp: u32 = arr1;
arr1 = arr2;
arr2 = temp;
}
// (i, j) = (2, 2).
// Move the smaller elements forward.
if arr3 < arr2 {
// Swap the elements at indexes j and j+1
let temp: u32 = arr2;
arr2 = arr3;
arr3 = temp;
}
// (i, j) = (2, 3).
// Move the smaller elements forward.
if arr4 < arr3 {
// Swap the elements at indexes j and j+1
let temp: u32 = arr3;
arr3 = arr4;
arr4 = temp;
}
// (i, j) = (2, 4).
// Move the smaller elements forward.
if arr5 < arr4 {
// Swap the elements at indexes j and j+1
let temp: u32 = arr4;
arr4 = arr5;
arr5 = temp;
}
// (i, j) = (3, 0).
// Move the smaller elements forward.
if arr1 < arr0 {
// Swap the elements at indexes j and j+1
let temp: u32 = arr0;
arr0 = arr1;
arr1 = temp;
}
// (i, j) = (3, 1).
// Move the smaller elements forward.
if arr2 < arr1 {
// Swap the elements at indexes j and j+1
let temp: u32 = arr1;
arr1 = arr2;
arr2 = temp;
}
// (i, j) = (3, 2).
// Move the smaller elements forward.
if arr3 < arr2 {
// Swap the elements at indexes j and j+1
let temp: u32 = arr2;
arr2 = arr3;
arr3 = temp;
}
// (i, j) = (3, 3).
// Move the smaller elements forward.
if arr4 < arr3 {
// Swap the elements at indexes j and j+1
let temp: u32 = arr3;
arr3 = arr4;
arr4 = temp;
}
// (i, j) = (4, 0).
// Move the smaller elements forward.
if arr1 < arr0 {
// Swap the elements at indexes j and j+1
let temp: u32 = arr0;
arr0 = arr1;
arr1 = temp;
}
// (i, j) = (4, 1).
// Move the smaller elements forward.
if arr2 < arr1 {
// Swap the elements at indexes j and j+1
let temp: u32 = arr1;
arr1 = arr2;
arr2 = temp;
}
// (i, j) = (4, 2).
// Move the smaller elements forward.
if arr3 < arr2 {
// Swap the elements at indexes j and j+1
let temp: u32 = arr2;
arr2 = arr3;
arr3 = temp;
}
// (i, j) = (5, 0).
// Move the smaller elements forward.
if arr1 < arr0 {
// Swap the elements at indexes j and j+1
let temp: u32 = arr0;
arr0 = arr1;
arr1 = temp;
}
// (i, j) = (5, 1).
// Move the smaller elements forward.
if arr2 < arr1 {
// Swap the elements at indexes j and j+1
let temp: u32 = arr1;
arr1 = arr2;
arr2 = temp;
}
// (i, j) = (6, 0).
// Move the smaller elements forward.
if arr1 < arr0 {
// Swap the elements at indexes j and j+1
let temp: u32 = arr0;
arr0 = arr1;
arr1 = temp;
}
return (arr0, arr1, arr2, arr3, arr4, arr5, arr6, arr7);
}
}

View File

@ -0,0 +1,18 @@
/*
namespace: Compile
expectation: Pass
*/
program test.aleo {
// This function takes as input a field `a` and calls several core functions.
// Core functions are built-in to the Leo language and call handwritten, optimized circuits in the AVM.
// To call a core function, use the correct capitalized identifier followed by two colons
// and then the function name. Example: `Pedersen64::hash_to_field()`.
transition main(a: field) -> field {
let b: field = BHP256::hash_to_field(a);
let c: field = Poseidon2::hash_to_field(b);
let d: field = BHP256::commit_to_field(c, 1scalar);
return d;
}
}

View File

@ -0,0 +1,53 @@
/*
namespace: Compile
expectation: Pass
*/
program test.aleo {
// This calculates the n-th fibonacci number (up to 64th)
transition fibonacci(public n: u8) -> u128 {
assert(n <= 64u8);
let f0: u128 = 0u128;
let f1: u128 = 1u128;
let c: u8 = 0u8;
let z: u8 = reverse_bits(n);
for i:u8 in 0u8..8u8 {
if n > 0u8 {
let f2i1: u128 = f1 * f1 + f0 * f0;
let f2i: u128 = f0 * (2u128 * f1 - f0);
if z & 1u8.shl(c) == 0u8 {
f0 = f2i;
f1 = f2i1;
} else {
f0 = f2i1;
f1 = f2i + f2i1;
}
c = c + 1u8;
n = n >> 1u8;
}
}
return f0;
}
function reverse_bits(n: u8) -> u8 {
let reverse: u8 = 0u8;
for i:u8 in 0u8..8u8 {
if n > 0u8 {
reverse = reverse << 1u8;
if n & 1u8 == 1u8 {
reverse ^= 1u8;
}
n = n >> 1u8;
}
}
return reverse;
}
}

View File

@ -0,0 +1,24 @@
/*
namespace: Compile
expectation: Pass
*/
program test.aleo {
// This function takes a group coordinate as input `a` and performs several operations which should output the `0group`.
// Note that the operations can be called as associated functions on the `a` variable.
transition main(a: group) -> group {
// unary
let b: group = a.double(); // 2a
let c: group = b.neg(); // -2a
// binary
let d: group = (a * 2scalar).add(c);
// generator
let e: group = group::GEN;
return d + e;
}
}

View File

@ -0,0 +1,12 @@
/*
namespace: Compile
expectation: Pass
*/
program test.aleo {
// The 'helloworld' main function.
transition main(public a: u32, b: u32) -> u32 {
return a + b;
}
}

View File

@ -0,0 +1,42 @@
/*
namespace: Compile
expectation: Pass
*/
program test.aleo {
// This function calculates the interest accrued
// over ten iterations for some `capital` and `rate`.
transition fixed_iteration_interest(capital: u32, public rate: u32) -> u32 {
let amount: u32 = capital;
// Accrue for exactly 10 iterations.
for i:u8 in 0u8..10u8 {
// Note that the added amount is rounded down.
amount += (amount * rate) / 100u32;
}
return amount;
}
// This function calculates the interest accrued
// over a variable number of iterations (max 50) for some `capital` and `rate`.
transition bounded_iteration_interest(capital: u32,
public rate: u32,
iterations: u8) -> u32 {
assert(iterations <= 50u8);
let amount: u32 = capital;
// Accrue for up to 50 iterations.
for i:u8 in 0u8..50u8 {
if i < iterations {
// Note that the added amount is rounded down.
amount += (amount * rate) / 100u32;
} // Skip the remaining iterations.
if i == 40u8 {
return amount;
}
}
return amount;
}
}

View File

@ -0,0 +1,35 @@
/*
namespace: Compile
expectation: Pass
*/
// The 'lottery' program.
program test.aleo {
mapping num_winners: u8 => u8;
record Ticket {
owner: address,
}
transition play() -> Ticket {
let ticket: Ticket = Ticket {
owner: self.caller,
};
return ticket then finalize();
}
finalize play() {
// Check that the lottery has not expired.
assert(block.height <= 1000u32);
// Randomly select whether or not the ticket is a winner.
assert(ChaCha::rand_bool());
// Check that the maximum number of winners have not been reached.
let winners: u8 = num_winners.get_or_use(0u8, 0u8);
assert(winners < 5u8);
num_winners.set(0u8, winners + 1u8);
}
}

View File

@ -0,0 +1,32 @@
/*
namespace: Compile
expectation: Pass
*/
// This example demonstrates the definition and initialization of a "struct" in Leo.
program test.aleo {
// The "Message" struct.
struct Message {
// A struct member named "first" with type "field".
first: field,
// A struct member named "second" with type "field".
second: field,
}
// The "main" function of this Leo program takes a "Message" struct type as input.
// To see how to input variable "m" is passed in open up `inputs/message.in`.
transition main(m: Message) -> field {
// 1. Define the "Message" type.
// 2. Use brackets `{ }` to enclose the struct members.
// 3. Define each struct member `name : value`.
let m1: Message = Message {
first: m.first,
second: m.second,
};
// Access the members of a struct with dot syntax.
// `struct_name.member`
return m1.first + m1.second;
}
}

View File

@ -0,0 +1,51 @@
/*
namespace: Compile
expectation: Pass
*/
program test.aleo {
record move {
owner: address,
incoming_fire_coordinate: u64,
player_1: address,
player_2: address,
// One flipped bit indicates a hit. No flipped bits indicates a miss.
prev_hit_or_miss: u64,
}
// Returns new move record owned by the opponent.
transition create_move(
// The move record created by the opponent.
move_record: move,
// The u64 representation of incoming_fire_coordinate, the bitstring fire coordinate to send to the opponent.
incoming_fire_coordinate: u64,
// The u64 representation of prev_hit_or_miss, this player's previous fire coordinate as a hit or miss.
prev_hit_or_miss: u64,
) -> move {
// A new move record should be created and owned by the opponent.
let one_is_owner: bool = move_record.player_1 == move_record.owner;
let opponent: address = one_is_owner ? move_record.player_2 : move_record.player_1;
return move {
owner: opponent,
incoming_fire_coordinate,
player_1: move_record.player_2,
player_2: move_record.player_1,
prev_hit_or_miss,
};
}
// Returns the move record owned by the opponent.
// Note, this move record contains dummy fire coordinates and previous hit or miss.
transition start_game(player_2: address) -> move {
return move {
owner: player_2,
incoming_fire_coordinate: 0u64,
player_1: self.caller,
player_2: player_2,
prev_hit_or_miss: 0u64,
};
}
}

View File

@ -0,0 +1,59 @@
/*
namespace: Compile
expectation: Pass
*/
program test.aleo {
// The 'ntzdebruijn' main function.
// From Hacker's Delight 2nd ed. figure 5-26
transition main(public x: u32) -> u8 {
if x == 0u32 {return 32u8;}
// 0x04D7651F = 81224991
x = (x & 0u32.sub_wrapped(x)).mul_wrapped(81224991u32);
let i: u32 = x >> 27u8;
return deBruijnTableLookup(i);
}
// { 0, 1, 2,24, 3,19, 6,25, 22, 4,20,10,16, 7,12,26,
// 31,23,18, 5,21, 9,15,11, 30,17, 8,14,29,13,28,27};
function deBruijnTableLookup(i: u32) -> u8 {
if i == 0u32 {return 0u8;} else
if i == 1u32 {return 1u8;} else
if i == 2u32 {return 2u8;} else
if i == 3u32 {return 24u8;} else
if i == 4u32 {return 3u8;} else
if i == 5u32 {return 19u8;} else
if i == 6u32 {return 6u8;} else
if i == 7u32 {return 25u8;} else
if i == 8u32 {return 22u8;} else
if i == 9u32 {return 4u8;} else
if i == 10u32 {return 20u8;} else
if i == 11u32 {return 10u8;} else
if i == 12u32 {return 16u8;} else
if i == 13u32 {return 7u8;} else
if i == 14u32 {return 12u8;} else
if i == 15u32 {return 26u8;} else
if i == 16u32 {return 31u8;} else
if i == 17u32 {return 23u8;} else
if i == 18u32 {return 18u8;} else
if i == 19u32 {return 5u8;} else
if i == 20u32 {return 21u8;} else
if i == 21u32 {return 9u8;} else
if i == 22u32 {return 15u8;} else
if i == 23u32 {return 11u8;} else
if i == 24u32 {return 30u8;} else
if i == 25u32 {return 17u8;} else
if i == 26u32 {return 8u8;} else
if i == 27u32 {return 14u8;} else
if i == 28u32 {return 29u8;} else
if i == 29u32 {return 13u8;} else
if i == 30u32 {return 28u8;} else
if i == 31u32 {return 27u8;} else
{return 0u8;} // unused
}
}

View File

@ -0,0 +1,24 @@
/*
namespace: Compile
expectation: Pass
*/
program test.aleo {
// The 'ntzgaudet' main function.
// From Hacker's Delight 2nd ed. figure 5-24
transition main(public x: u32) -> u8 {
let y: u32 = x & 0u32.sub_wrapped(x); // Isolate rightmost 1-bit
let bz: u8 = (y != 0u32) ? 0u8 : 1u8;
// 0x0000FFFF = 65535
let b4: u8 = (y & 65535u32 != 0u32) ? 0u8 : 16u8;
// 0x00FF00FF = 16711935
let b3: u8 = (y & 16711935u32 != 0u32) ? 0u8 : 8u8;
// 0x0F0F0F0F = 252645135
let b2: u8 = (y & 252645135u32 != 0u32) ? 0u8 : 4u8;
// 0x33333333 = 858993459
let b1: u8 = (y & 858993459u32 != 0u32) ? 0u8 : 2u8;
// 0x55555555 = 1431655765
let b0: u8 = (y & 1431655765u32 != 0u32) ? 0u8 : 1u8;
return bz + b4 + b3 + b2 + b1 + b0;
}
}

View File

@ -0,0 +1,20 @@
/*
namespace: Compile
expectation: Pass
*/
program test.aleo {
// The 'ntzloops' main function.
// From Hacker's Delight 2nd ed. figure 5-23
transition main(public x: u32) -> u8 {
x = !x & x.sub_wrapped(1u32);
let n: u8 = 0u8;
for i:u8 in 0u8..32u8 {
if x != 0u32 {
n += 1u8;
x = x >> 1u8;
}
}
return n;
}
}

View File

@ -0,0 +1,22 @@
/*
namespace: Compile
expectation: Pass
*/
program test.aleo {
// The 'ntzmasks' main function.
// From Hacker's Delight 2nd ed. figure 5-20
transition main(public x: u32) -> u8 {
if (x == 0u32) {return 32u8;}
let n: u8 = 1u8;
// x >>= 16u8 wasn't working, and I don't want to use
// to u32 as a shift operand, so I do x = x >> 16u8
if ((x & 65535u32) == 0u32) {n += 16u8; x = x >> 16u8;}
if ((x & 255u32) == 0u32) {n += 8u8; x = x >> 8u8;}
if ((x & 15u32) == 0u32) {n += 4u8; x = x >> 4u8;}
if ((x & 3u32) == 0u32) {n += 2u8; x = x >> 2u8;}
// can't do `return n - (x & 1u32);` because no typecasts, so:
if ((x & 1u32) == 1u32) {n -= 1u8;}
return n;
}
}

View File

@ -0,0 +1,65 @@
/*
namespace: Compile
expectation: Pass
*/
program test.aleo {
// The 'ntzreisers' main function.
// From Hacker's Delight 2nd ed. figure 5-27
transition main(public x: u32) -> u8 {
x = (x & 0u32.sub_wrapped(x)).rem_wrapped(37u32);
return reisersTableLookup(x);
}
// There are 37 entries here
// {32, 0, 1, 26, 2, 23, 27,
// u, 3, 16, 24, 30, 28, 11, u, 13, 4,
// 7, 17, u, 25, 22, 31, 15, 29, 10, 12,
// 6, u, 21, 14, 9, 5, 20, 8, 19, 18};
function reisersTableLookup(i: u32) -> u8 {
if i == 0u32 {return 32u8;} else
if i == 1u32 {return 0u8;} else
if i == 2u32 {return 1u8;} else
if i == 3u32 {return 26u8;} else
if i == 4u32 {return 2u8;} else
if i == 5u32 {return 23u8;} else
if i == 6u32 {return 27u8;} else
if i == 7u32 {return 0u8;} else // unused
if i == 8u32 {return 3u8;} else
if i == 9u32 {return 16u8;} else
if i == 10u32 {return 24u8;} else
if i == 11u32 {return 30u8;} else
if i == 12u32 {return 28u8;} else
if i == 13u32 {return 11u8;} else
if i == 14u32 {return 0u8;} else // unused
if i == 15u32 {return 13u8;} else
if i == 16u32 {return 4u8;} else
if i == 17u32 {return 7u8;} else
if i == 18u32 {return 17u8;} else
if i == 19u32 {return 0u8;} else // unused
if i == 20u32 {return 25u8;} else
if i == 21u32 {return 22u8;} else
if i == 22u32 {return 31u8;} else
if i == 23u32 {return 15u8;} else
if i == 24u32 {return 29u8;} else
if i == 25u32 {return 10u8;} else
if i == 26u32 {return 12u8;} else
if i == 27u32 {return 6u8;} else
if i == 28u32 {return 0u8;} else // unused
if i == 29u32 {return 21u8;} else
if i == 30u32 {return 14u8;} else
if i == 31u32 {return 9u8;} else
if i == 32u32 {return 5u8;} else
if i == 33u32 {return 20u8;} else
if i == 34u32 {return 8u8;} else
if i == 35u32 {return 19u8;} else
if i == 36u32 {return 18u8;} else
{return 0u8;} // unused
}
}

View File

@ -0,0 +1,97 @@
/*
namespace: Compile
expectation: Pass
*/
program test.aleo {
// The 'nztseals' main function.
// From Hacker's Delight 2nd ed. figure 5-25
transition main(public x: u32) -> u8 {
// 0x0450FBAF = 72416175
x = (x & 0u32.sub_wrapped(x)).mul_wrapped(72416175u32);
return sealsTableLookup(x >> 26u8);
}
// Right now we do not have any structure that allows
// computable indexing, so simulate that with a function.
// {32, 0, 1,12, 2, 6, u,13, 3, u, 7, u, u, u, u,14,
// 10, 4, u, u, 8, u, u,25, u, u, u, u, u,21,27,15,
// 31,11, 5, u, u, u, u, u, 9, u, u,24, u, u,20,26,
// 30, u, u, u, u,23, u,19, 29, u,22,18,28,17,16, u};
function sealsTableLookup(i: u32) -> u8 {
if i == 0u32 {return 32u8;} else
if i == 1u32 {return 0u8;} else
if i == 2u32 {return 1u8;} else
if i == 3u32 {return 12u8;} else
if i == 4u32 {return 2u8;} else
if i == 5u32 {return 6u8;} else
if i == 6u32 {return 0u8;} else // unused
if i == 7u32 {return 13u8;} else
if i == 8u32 {return 3u8;} else
if i == 9u32 {return 0u8;} else // unused
if i == 10u32 {return 7u8;} else
if i == 11u32 {return 0u8;} else // unused
if i == 12u32 {return 0u8;} else // unused
if i == 13u32 {return 0u8;} else // unused
if i == 14u32 {return 0u8;} else // unused
if i == 15u32 {return 14u8;} else
if i == 16u32 {return 10u8;} else
if i == 17u32 {return 4u8;} else
if i == 18u32 {return 0u8;} else // unused
if i == 19u32 {return 0u8;} else // unused
if i == 20u32 {return 8u8;} else
if i == 21u32 {return 0u8;} else // unused
if i == 22u32 {return 0u8;} else // unused
if i == 23u32 {return 25u8;} else
if i == 24u32 {return 0u8;} else // unused
if i == 25u32 {return 0u8;} else // unused
if i == 26u32 {return 0u8;} else // unused
if i == 27u32 {return 0u8;} else // unused
if i == 28u32 {return 0u8;} else // unused
if i == 29u32 {return 21u8;} else
if i == 30u32 {return 27u8;} else
if i == 31u32 {return 15u8;} else
if i == 32u32 {return 31u8;} else
if i == 33u32 {return 11u8;} else
if i == 34u32 {return 5u8;} else
if i == 35u32 {return 0u8;} else // unused
if i == 36u32 {return 0u8;} else // unused
if i == 37u32 {return 0u8;} else // unused
if i == 38u32 {return 0u8;} else // unused
if i == 39u32 {return 0u8;} else // unused
if i == 40u32 {return 9u8;} else
if i == 41u32 {return 0u8;} else // unused
if i == 42u32 {return 0u8;} else // unused
if i == 43u32 {return 24u8;} else
if i == 44u32 {return 0u8;} else // unused
if i == 45u32 {return 0u8;} else // unused
if i == 46u32 {return 20u8;} else
if i == 47u32 {return 26u8;} else
if i == 48u32 {return 30u8;} else
if i == 49u32 {return 0u8;} else // unused
if i == 50u32 {return 0u8;} else // unused
if i == 51u32 {return 0u8;} else // unused
if i == 52u32 {return 0u8;} else // unused
if i == 53u32 {return 23u8;} else
if i == 54u32 {return 0u8;} else // unused
if i == 55u32 {return 19u8;} else
if i == 56u32 {return 29u8;} else
if i == 57u32 {return 0u8;} else // unused
if i == 58u32 {return 22u8;} else
if i == 59u32 {return 18u8;} else
if i == 60u32 {return 28u8;} else
if i == 61u32 {return 17u8;} else
if i == 62u32 {return 16u8;} else
if i == 63u32 {return 0u8;} else // unused
{return 0u8;} // unused
}
}

View File

@ -0,0 +1,81 @@
/*
namespace: Compile
expectation: Pass
*/
program test.aleo {
// The 'ntzsearchtree' main function.
// From Hacker's Delight 2nd ed. figure 5-22,
// expanded to a 32-bit version.
transition main(public x: u32) -> u8 {
if (x & 65535u32 != 0u32) {
if (x & 255u32 != 0u32) {
if (x & 15u32 != 0u32) {
if (x & 3u32 != 0u32) {
if (x & 1u32 != 0u32) {return 0u8;}
else {return 1u8;} }
else {
if (x & 4u32 != 0u32) {return 2u8;}
else {return 3u8;} } }
else { // low 4 bits are 0 but low 8 bits are not zero
if (x & 48u32 != 0u32) { // 48 = 0011 0000
if (x & 16u32 != 0u32) {return 4u8;}
else {return 5u8;} }
else {if (x & 64u32 != 0u32) {return 6u8;}
else {return 7u8;} } } }
else { // low 8 bits are 0 but low 16 bits are not zero
// 3840 = 00001111 00000000
if (x & 3840u32 != 0u32) {
// 768 = 00000011 00000000
if (x & 768u32 != 0u32) {
if (x & 256u32 != 0u32) {return 8u8;}
else {return 9u8;} }
else { // 1024 = 00000100 00000000
if (x & 1024u32 != 0u32) {return 10u8;}
else {return 11u8;} } }
else { // low 12 bits are 0 but low 16 bits are not zero
// 12288 = 0011 0000 0000 0000
if (x & 12288u32 != 0u32) {
// 4096 = 0001 0000 0000 0000
if (x & 4096u32 != 0u32) {return 12u8;}
else {return 13u8;} }
else { // low 14 bits are 0 but low 16 bits are not zero
// 16384 = 0100 0000 0000 0000
if (x & 16384u32 != 0u32) {return 14u8;}
else {return 15u8;} } } } }
else { // low 16 bits are zero, now look at high 16 bits
// Simply by multiplying the previous constants by 65536
if (x & (255u32 * 65536u32) != 0u32) {
if (x & (15u32 * 65536u32) != 0u32) {
if (x & (3u32 * 65536u32) != 0u32) {
if (x & (1u32 * 65536u32) != 0u32) {return 16u8;}
else {return 17u8;} }
else {
if (x & (4u32 * 65536u32) != 0u32) {return 18u8;}
else {return 19u8;} } }
else {
if (x & (48u32 * 65536u32) != 0u32) {
if (x & (16u32 * 65536u32) != 0u32) {return 20u8;}
else {return 21u8;} }
else {
if (x & (64u32 * 65536u32) != 0u32) {return 22u8;}
else {return 23u8;} } } }
else {
if (x & (3840u32 * 65536u32) != 0u32) {
if (x & (768u32 * 65536u32) != 0u32) {
if (x & (256u32 * 65536u32) != 0u32) {return 24u8;}
else {return 25u8;} }
else {
if (x & (1024u32 * 65536u32) != 0u32) {return 26u8;}
else {return 27u8;} } }
else {
if (x & (12288u32 * 65536u32) != 0u32) {
if (x & (4096u32 * 65536u32) != 0u32) {return 28u8;}
else {return 29u8;} }
else {
if (x & (16384u32 * 65536u32) != 0u32) {return 30u8;}
else {
if (x != 0u32) {return 31u8;}
else {return 32u8;} } } } } }
}
}

View File

@ -0,0 +1,19 @@
/*
namespace: Compile
expectation: Pass
*/
program test.aleo {
// The 'ntzsmallvals' main function.
// From Hacker's Delight 2nd ed. figure 5-21
transition main(public x: u32) -> u8 {
if (x == 0u32) {return 32u8;}
let n: u8 = 31u8;
let y: u32 = x.shl_wrapped(16u8); if (y != 0u32) {n = n - 16u8; x = y;}
y = x.shl_wrapped(8u8); if (y != 0u32) {n = n - 8u8; x = y;}
y = x.shl_wrapped(4u8); if (y != 0u32) {n = n - 4u8; x = y;}
y = x.shl_wrapped(2u8); if (y != 0u32) {n = n - 2u8; x = y;}
y = x.shl_wrapped(1u8); if (y != 0u32) {n = n - 1u8;}
return n;
}
}

View File

@ -0,0 +1,50 @@
/*
namespace: Compile
expectation: Pass
*/
// This example demonstrates an example of a minting and transferring a token in Leo.
program test.aleo {
// The `Token` record datatype.
record Token {
// The token owner.
owner: address,
// The token amount.
amount: u64,
}
// The `mint` function initializes a new record with the
// specified number of tokens assigned to the specified receiver.
transition mint(owner: address, amount: u64) -> Token {
return Token {
owner: owner,
amount: amount,
};
}
// The `transfer` function sends the specified number of tokens
// to the receiver from the provided token record.
transition transfer(token: Token, to: address, amount: u64) -> (Token, Token) {
// Checks the given token record has sufficient balance.
// This `sub` operation is safe, and the proof will fail
// if an overflow occurs.
// `difference` holds the change amount to be returned to sender.
let difference: u64 = token.amount - amount;
// Produce a token record with the change amount for the sender.
let remaining: Token = Token {
owner: token.owner,
amount: difference,
};
// Produce a token record for the specified receiver.
let transferred: Token = Token {
owner: to,
amount: amount,
};
// Output the sender's change record and the receiver's record.
return (remaining, transferred);
}
}

View File

@ -0,0 +1,116 @@
/*
namespace: Compile
expectation: Pass
*/
program test.aleo {
// A row in a tic tac toe board.
// - `c1` : The first entry in the row.
// - `c2` : The second entry in the row.
// - `c3` : The third entry in the row.
// A valid entry is either 0, 1, or 2, where 0 is empty, 1 corresponds to player 1, and 2 corresponds to player 2.
// Any other values are invalid.
struct Row {
c1: u8,
c2: u8,
c3: u8
}
// A tic tac toe board.
// - `r1` : The first row in the board.
// - `r2` : The second row in the board.
// - `r3` : The third row in the board.
struct Board {
r1: Row,
r2: Row,
r3: Row,
}
// Returns an empty board.
transition new() -> Board {
return Board {
r1: Row { c1: 0u8, c2: 0u8, c3: 0u8 },
r2: Row { c1: 0u8, c2: 0u8, c3: 0u8 },
r3: Row { c1: 0u8, c2: 0u8, c3: 0u8 },
};
}
// Returns `true` if there exists a row, column, or diagonal with all entries occupied by the same player.
// - `b` : A tic tac toe board.
// - `p` : A number corresponding to a player.
function check_for_win(b: Board, p: u8) -> bool {
return
(b.r1.c1 == p && b.r1.c2 == p && b.r1.c3 == p) || // row 1
(b.r2.c1 == p && b.r2.c2 == p && b.r2.c3 == p) || // row 2
(b.r3.c1 == p && b.r3.c3 == p && b.r3.c3 == p) || // row 3
(b.r1.c1 == p && b.r2.c1 == p && b.r3.c1 == p) || // column 1
(b.r1.c2 == p && b.r2.c3 == p && b.r3.c2 == p) || // column 2
(b.r1.c3 == p && b.r2.c3 == p && b.r3.c3 == p) || // column 3
(b.r1.c1 == p && b.r2.c2 == p && b.r3.c3 == p) || // diagonal
(b.r1.c3 == p && b.r2.c2 == p && b.r3.c1 == p); // other diagonal
}
// Returns an updated tic tac toe board with a move made by a player.
// Returns a `u8` corresponding to the player who won the game, or 0 if no one has won yet.
// - `player` : A number corresponding to a player.
// - `row` : The row of the move.
// - `col` : The column of the move.
// - `board` : A tic tac toe board.
// Assumes that `player` is either 1 or 2.
// Assumes that `row` and `col` are valid indices into the board.
// If an entry is already occupied, the move is invalid and the board is returned unchanged.
transition make_move(player: u8, row: u8, col: u8, board: Board) -> (Board, u8) {
// Check that inputs are valid.
assert(player == 1u8 || player == 2u8);
assert(1u8 <= row && row <= 3u8);
assert(1u8 <= col && col <= 3u8);
// Unpack the entries in the board into variables.
let r1c1: u8 = board.r1.c1;
let r1c2: u8 = board.r1.c2;
let r1c3: u8 = board.r1.c3;
let r2c1: u8 = board.r2.c1;
let r2c2: u8 = board.r2.c2;
let r2c3: u8 = board.r2.c3;
let r3c1: u8 = board.r3.c1;
let r3c2: u8 = board.r3.c2;
let r3c3: u8 = board.r3.c3;
// Update the appropriate entry with the given move.
if row == 1u8 && col == 1u8 && r1c1 == 0u8 {
r1c1 = player;
} else if row == 1u8 && col == 2u8 && r1c2 == 0u8 {
r1c2 = player;
} else if row == 1u8 && col == 3u8 && r1c3 == 0u8 {
r1c3 = player;
} else if row == 2u8 && col == 1u8 && r2c1 == 0u8 {
r2c1 = player;
} else if row == 2u8 && col == 2u8 && r2c2 == 0u8 {
r2c2 = player;
} else if row == 2u8 && col == 3u8 && r2c3 == 0u8 {
r2c3 = player;
} else if row == 3u8 && col == 1u8 && r3c1 == 0u8 {
r3c1 = player;
} else if row == 3u8 && col == 2u8 && r3c2 == 0u8 {
r3c2 = player;
} else if row == 3u8 && col == 3u8 && r3c3 == 0u8 {
r3c3 = player;
}
// Construct the updated game board.
let updated: Board = Board {
r1: Row { c1: r1c1, c2: r1c2, c3: r1c3 },
r2: Row { c1: r2c1, c2: r2c2, c3: r2c3 },
r3: Row { c1: r3c1, c2: r2c2, c3: r2c3 },
};
// Check if the game is over.
if check_for_win(updated, 1u8) {
return (updated, 1u8);
} else if check_for_win(updated, 2u8) {
return (updated, 2u8);
} else {
return (updated, 0u8);
}
}
}

View File

@ -0,0 +1,133 @@
/*
namespace: Compile
expectation: Pass
*/
program test.aleo {
// On-chain storage of an `account` map, with `address` as the key,
// and `u64` as the value.
mapping account: address => u64;
record token {
// The token owner.
owner: address,
// The token amount.
amount: u64,
}
/* Mint */
// The function `mint_public` issues the specified token amount for the token receiver publicly on the network.
transition mint_public(public receiver: address, public amount: u64) {
// Mint the tokens publicly by invoking the computation on-chain.
return then finalize(receiver, amount);
}
finalize mint_public(public receiver: address, public amount: u64) {
// Increments `account[receiver]` by `amount`.
// If `account[receiver]` does not exist, it will be created.
// If `account[receiver] + amount` overflows, `mint_public` is reverted.
let current_amount: u64 = Mapping::get_or_use(account, receiver, 0u64);
Mapping::set(account, receiver, current_amount + amount);
}
// The function `mint_private` initializes a new record with the specified amount of tokens for the receiver.
transition mint_private(receiver: address, amount: u64) -> token {
return token {
owner: receiver,
amount: amount,
};
}
/* Transfer */
transition transfer_public(public receiver: address, public amount: u64) {
// Transfer the tokens publicly, by invoking the computation on-chain.
return then finalize(self.caller, receiver, amount);
}
finalize transfer_public(public sender: address, public receiver: address, public amount: u64) {
// Decrements `account[sender]` by `amount`.
// If `account[sender]` does not exist, it will be created.
// If `account[sender] - amount` underflows, `transfer_public` is reverted.
let sender_amount: u64 = Mapping::get_or_use(account, sender, 0u64);
Mapping::set(account, sender, sender_amount - amount);
// Increments `account[receiver]` by `amount`.
// If `account[receiver]` does not exist, it will be created.
// If `account[receiver] + amount` overflows, `transfer_public` is reverted.
let receiver_amount: u64 = Mapping::get_or_use(account, receiver, 0u64);
Mapping::set(account, receiver, receiver_amount + amount);
}
// The function `transfer_private` sends the specified token amount to the token receiver from the specified token record.
transition transfer_private(sender: token, receiver: address, amount: u64) -> (token, token) {
// Checks the given token record has sufficient balance.
// This `sub` operation is safe, and the proof will fail if an overflow occurs.
// `difference` holds the change amount to be returned to sender.
let difference: u64 = sender.amount - amount;
// Produce a token record with the change amount for the sender.
let remaining: token = token {
owner: sender.owner,
amount: difference,
};
// Produce a token record for the specified receiver.
let transferred: token = token {
owner: receiver,
amount: amount,
};
// Output the sender's change record and the receiver's record.
return (remaining, transferred);
}
// The function `transfer_private_to_public` turns a specified token amount from a token record into public tokens for the specified receiver.
// This function preserves privacy for the sender's record, however it publicly reveals the token receiver and the token amount.
transition transfer_private_to_public(sender: token, public receiver: address, public amount: u64) -> token {
// Checks the given token record has a sufficient token amount.
// This `sub` operation is safe, and the proof will fail if an underflow occurs.
// `difference` holds the change amount for the caller.
let difference: u64 = sender.amount - amount;
// Produces a token record with the change amount for the caller.
let remaining: token = token {
owner: sender.owner,
amount: difference,
};
// Output the sender's change record.
// Increment the token amount publicly for the token receiver.
return remaining then finalize(receiver, amount);
}
finalize transfer_private_to_public(public receiver: address, public amount: u64) {
// Increments `account[receiver]` by `amount`.
// If `account[receiver]` does not exist, it will be created.
// If `account[receiver] + amount` overflows, `transfer_private_to_public` is reverted.
let current_amount: u64 = Mapping::get_or_use(account, receiver, 0u64);
Mapping::set(account, receiver, current_amount + amount);
}
// The function `transfer_public_to_private` turns a specified token amount from `account` into a token record for the specified receiver.
// This function preserves privacy for the receiver's record, however it publicly reveals the caller and the specified token amount.
transition transfer_public_to_private(public receiver: address, public amount: u64) -> token {
// Produces a token record for the token receiver.
let transferred: token = token {
owner: receiver,
amount: amount,
};
// Output the receiver's record.
// Decrement the token amount of the caller publicly.
return transferred then finalize(self.caller, amount);
}
finalize transfer_public_to_private(public sender: address, public amount: u64) {
// Decrements `account[sender]` by `amount`.
// If `account[sender]` does not exist, it will be created.
// If `account[sender] - amount` underflows, `transfer_public_to_private` is reverted.
let current_amount: u64 = Mapping::get_or_use(account, sender, 0u64);
Mapping::set(account, sender, current_amount - amount);
}
}

View File

@ -0,0 +1,33 @@
/*
namespace: Compile
expectation: Pass
*/
program test.aleo {
// This function calculates the number of powers of two ("twoadicity")
// in the prime factorization of the input number `n`.
transition main(public n: field) -> u8 {
let remaining_n: field = n;
let powers_of_two: u8 = 0u8;
// Since field ints are 253 bits or fewer, any number in the field
// will have at most 252 powers of two in its prime factoring.
for i:u8 in 0u8..252u8 {
if is_even_and_nonzero(remaining_n) {
remaining_n = remaining_n / 2field;
powers_of_two = powers_of_two + 1u8;
}
}
return powers_of_two;
}
/* We define the is_even predicate on fields as follows.
If n is even and nonzero, clearly n/2 < n.
If n is odd, n-p is a field-equivalent negative number that is even, and
(n-p)/2 is a field-equivalent negative number closer to 0, greater than n-p.
If we add p to both of these negative numbers, we have
n/2 = (n-p)/2 + p = (n+p)/2 is greater than n and still less than p.
*/
function is_even_and_nonzero (n: field) -> bool {
return n / 2field < n;
}
}

View File

@ -0,0 +1,130 @@
/*
namespace: Compile
expectation: Pass
*/
program test.aleo {
// Returns the number of flipped bits.
// E.g. 17870283321406128128u64, in binary 11111000 00000000 00000000 00000000 00000000 00000000 00000000 00000000,
// returns 5u64;
function bitcount(bits: u64) -> u64 {
let r1: u64 = bits / 2u64;
let r2: u64 = bits / 4u64;
let r3: u64 = bits / 8u64;
let r4: u64 = r1 & 8608480567731124087u64;
let r5: u64 = r2 & 3689348814741910323u64;
let r6: u64 = r3 & 1229782938247303441u64;
let r7: u64 = bits - r4 - r5 - r6;
let r8: u64 = r7 / 16u64;
let r9: u64 = r7 + r8;
let r10: u64 = r9 & 1085102592571150095u64;
let r11: u64 = r10 % 255u64;
return r11;
}
// Returns boolean of whether all the flipped bits in location are "adjacent". Horizontally, this means all flipped bits are
// directly next to each other (111). Vertically, this means all flipped bits are separated by 7 unflipped bits
// (10000000100000001).
function adjacency_check(
// The u64 representation of a ship's placement in an 8x8 grid.
ship: u64,
// The u64 representation of a ship's bitstring, either horizontally or vertically.
// E.g. a ship of length 3's bit string horizontally would be: 000111 = 7u64. Vertically, the bit string would be:
// 10000000100000001 = 65793u64.
orientation: u64,
) -> bool {
// This may result in 0.
let division: u64 = ship / orientation;
// subtracting 1 from 0 will cause an underflow, so we should check for this edge case.
let is_eq: bool = division == 0u64;
// if the above division resulted in 0, we know the adjacency check should return false.
// Setting to r4 to 3 (11) will guarantee failure here.
let ternary: u64 = is_eq ? 3u64 : division;
let subtraction: u64 = ternary - 1u64;
let and: u64 = subtraction & ternary;
let bits_are_adjacent: bool = and == 0u64;
return bits_are_adjacent;
}
// Returns boolean of whether adjacent flipped bits don't split a row of size 8.
// E.g. 111000000 has adjacent flipped bits but splits a row: 00000001 11000000
function horizontal_check(
// The u64 representation of a ship's placement in an 8x8 grid.
ship: u64,
// The u64 representation of a ship's bitstring horizontally.
horizontal: u64,
) -> bool {
let remainder: u64 = ship % 255u64;
// This may result in 0.
let division: u64 = remainder / horizontal;
// Subtracting 1 from 0 will cause an underflow.
let is_eq: bool = division == 0u64;
// Setting to 3 will guarantee failure.
let ternary: u64 = is_eq ? 3u64 : division;
let subtraction: u64 = ternary - 1u64;
let and: u64 = subtraction & ternary;
let bits_split_row: bool = and == 0u64;
return bits_split_row;
}
// Returns `true` if the ship placement is valid.
transition validate_ship(
// The u64 representation of a ship's placement in an 8x8 grid.
ship: u64,
// The length of the placed ship.
length: u64,
// The u64 equivalent of a ship's horizontal bitstring representation.
horizontal: u64,
// The u64 equivalent of a ship's vertical bitstring representation.
vertical: u64,
) -> bool {
// Check bitcount -- all other validations depend on the bitcount being correct.
let num_bits: u64 = bitcount(ship);
assert_eq(num_bits, length);
// Check horizontal bits of ship.
let is_adjacent: bool = adjacency_check(ship, horizontal); // True if bits are adjacent horizontally.
let is_horizontal: bool = horizontal_check(ship, horizontal); // True if those horizontal bits are not split across rows.
let valid_horizontal: bool = is_adjacent && is_horizontal; // True if bits are adjacent horizontally and not split across rows.
// Check vertical bits of ship.
let valid_vertical: bool = adjacency_check(ship, vertical); // True if bits are adjacent vertically.
let ship_is_valid: bool = valid_horizontal || valid_vertical; // Ship is valid if it is vertically or horizontally valid.
return ship_is_valid;
}
// Returns the u64 representation of all the ships' placements in an 8x8 grid. This function will fail
// if any of the ship placements overlap each other.
transition create_board(
// The u64 representation of a carrier's placement in an 8x8 grid. Length = 5.
carrier: u64,
// The u64 representation of a battleship's placement in an 8x8 grid. Length = 4.
battleship: u64,
// The u64 representation of a cruiser's placement in an 8x8 grid. Length = 3.
cruiser: u64,
// The u64 representation of a destroyer's placement in an 8x8 grid. Length = 2.
destroyer: u64,
) -> u64 {
// Bitwise combine the ship placements together
let ships: u64 = carrier | battleship | cruiser | destroyer;
let num_bits: u64 = bitcount(ships);
assert_eq(num_bits, 14u64); // Given 4 individually-valid ships, a valid combination should yield exactly 14 flipped bits.
return ships;
}
}

View File

@ -0,0 +1,99 @@
/*
namespace: Compile
expectation: Pass
*/
// The 'vote.leo' program.
program test.aleo {
// Proposal details
struct ProposalInfo {
title: field,
content: field,
proposer: address,
}
// Proposal record records proposal info publicly
record Proposal {
owner: address,
id: field,
info: ProposalInfo,
}
// Save proposal info in public storage.
mapping proposals: field => ProposalInfo;
// Privacy tickets to vote
record Ticket {
owner: address,
pid: field,
}
// Count the total tickets issued for each proposal
mapping tickets: field => u64;
mapping agree_votes: field => u64;
mapping disagree_votes: field => u64;
// Propose a new proposal to vote on.
transition propose(public info: ProposalInfo) -> Proposal {
// Authenticate proposer.
assert_eq(self.caller, info.proposer);
// Generate a new proposal id.
let id: field = BHP256::hash_to_field(info.title);
// Return a new record for the proposal.
// Finalize the proposal id.
return Proposal {
owner: self.caller,
id,
info,
} then finalize(id);
}
// Create a new proposal in the "tickets" mapping.
finalize propose(public id: field) {
Mapping::set(tickets, id, 0u64);
}
// Create a new ticket to vote with.
transition new_ticket(
public pid: field,
public voter: address,
) -> Ticket {
// Finalize the proposal id for the ticket.
return Ticket {
owner: voter,
pid,
} then finalize(pid);
}
// Create a new ticket on a proposal in the "tickets" mapping.
finalize new_ticket(public pid: field) {
let current: u64 = Mapping::get_or_use(tickets, pid, 0u64);
Mapping::set(tickets, pid, current + 1u64);
}
// Vote privately to agree with a proposal.
transition agree(ticket: Ticket) {
// Finalize this vote.
return then finalize(ticket.pid);
}
finalize agree(public pid: field) {
// Publicly increment the number of agree votes.
let current: u64 = Mapping::get_or_use(agree_votes, pid, 0u64);
Mapping::set(agree_votes, pid, current + 1u64);
}
// Vote privately to disagree with a proposal.
transition disagree(ticket: Ticket) {
// Finalize this vote.
return then finalize(ticket.pid);
}
finalize disagree(pid: field) {
// Publicly increment the number of disagree votes.
let current: u64 = Mapping::get_or_use(disagree_votes, pid, 0u64);
Mapping::set(disagree_votes, pid, current + 1u64);
}
}