From 81ff9f4bef82cf86610bd40998e2018c720881da Mon Sep 17 00:00:00 2001 From: SIDANWhatever Date: Tue, 12 Dec 2023 01:46:28 +0800 Subject: [PATCH] feat: building byte array findings --- README.md | 9 ++- .../constructor-deconstruction.ak | 0 .../empty-primitives.ak | 0 .../list-deconstruction.ak | 0 .../pattern-matching-discord.ak | 0 .../wild-card-deconstruction.ak | 0 lib/exunits-research/mock-data/placeholder.ak | 7 +- .../plutus-types/address/address.ak | 7 -- .../plutus-types/address/full-address.ak | 22 +++++ .../plutus-types/address/payment-address.ak | 38 +++++++++ .../plutus-types/address/script-hash.ak | 25 ++++++ lib/exunits-research/primitives/byte-array.ak | 81 ++++++++++++++----- report/report.md | 16 ---- reports/report-control-flow.md | 0 reports/report-deserialization.md | 0 reports/report-plutus-types.md | 0 reports/report-primitives.md | 39 +++++++++ reports/report.md | 5 ++ 18 files changed, 204 insertions(+), 45 deletions(-) rename lib/exunits-research/{deserilization => deserialization}/constructor-deconstruction.ak (100%) rename lib/exunits-research/{deserilization => deserialization}/empty-primitives.ak (100%) rename lib/exunits-research/{deserilization => deserialization}/list-deconstruction.ak (100%) rename lib/exunits-research/{deserilization => deserialization}/pattern-matching-discord.ak (100%) rename lib/exunits-research/{deserilization => deserialization}/wild-card-deconstruction.ak (100%) delete mode 100644 lib/exunits-research/plutus-types/address/address.ak create mode 100644 lib/exunits-research/plutus-types/address/full-address.ak create mode 100644 lib/exunits-research/plutus-types/address/payment-address.ak create mode 100644 lib/exunits-research/plutus-types/address/script-hash.ak delete mode 100644 report/report.md create mode 100644 reports/report-control-flow.md create mode 100644 reports/report-deserialization.md create mode 100644 reports/report-plutus-types.md create mode 100644 reports/report-primitives.md create mode 100644 reports/report.md diff --git a/README.md b/README.md index 85c2efc..a46abde 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,13 @@ This is a repo of conducting execution unit comparison in different coding patte ## How to contribute 1. Build a test case with test function prefix of `xu_{category}_` -2. Add the finding to the [core report](./report/report.md) -3. Add any other report inside the `report` directory +2. Add the finding to the reports + - Major comprehensive findings: [core report](./reports/report.md) + - On primitives: [report](./reports/report-primitives.md) + - On Plutus types: [report](./reports/report-plutus-types.md) + - On deserialization: [report](./reports/report-deserialization.md) + - On control flow: [report](./reports/report-control-flow.md) +3. Add any other report inside the [`other-reports` directory](./reports/other-reports/) 4. Submit a PR Have fun! diff --git a/lib/exunits-research/deserilization/constructor-deconstruction.ak b/lib/exunits-research/deserialization/constructor-deconstruction.ak similarity index 100% rename from lib/exunits-research/deserilization/constructor-deconstruction.ak rename to lib/exunits-research/deserialization/constructor-deconstruction.ak diff --git a/lib/exunits-research/deserilization/empty-primitives.ak b/lib/exunits-research/deserialization/empty-primitives.ak similarity index 100% rename from lib/exunits-research/deserilization/empty-primitives.ak rename to lib/exunits-research/deserialization/empty-primitives.ak diff --git a/lib/exunits-research/deserilization/list-deconstruction.ak b/lib/exunits-research/deserialization/list-deconstruction.ak similarity index 100% rename from lib/exunits-research/deserilization/list-deconstruction.ak rename to lib/exunits-research/deserialization/list-deconstruction.ak diff --git a/lib/exunits-research/deserilization/pattern-matching-discord.ak b/lib/exunits-research/deserialization/pattern-matching-discord.ak similarity index 100% rename from lib/exunits-research/deserilization/pattern-matching-discord.ak rename to lib/exunits-research/deserialization/pattern-matching-discord.ak diff --git a/lib/exunits-research/deserilization/wild-card-deconstruction.ak b/lib/exunits-research/deserialization/wild-card-deconstruction.ak similarity index 100% rename from lib/exunits-research/deserilization/wild-card-deconstruction.ak rename to lib/exunits-research/deserialization/wild-card-deconstruction.ak diff --git a/lib/exunits-research/mock-data/placeholder.ak b/lib/exunits-research/mock-data/placeholder.ak index e80c217..78a8e08 100644 --- a/lib/exunits-research/mock-data/placeholder.ak +++ b/lib/exunits-research/mock-data/placeholder.ak @@ -1,6 +1,7 @@ use aiken/transaction.{NoDatum, Output, OutputReference, TransactionId} use aiken/transaction/credential.{ - Address, Credential, ScriptCredential, VerificationKeyCredential, + Address, Credential, Inline, ScriptCredential, StakeCredential, + VerificationKeyCredential, } use aiken/transaction/value.{PolicyId, from_lovelace} @@ -123,3 +124,7 @@ pub fn mock_script_address_3(stake_credential) -> Address { stake_credential, } } + +pub fn mock_stake_cred(cred_hex: ByteArray) -> StakeCredential { + Inline(VerificationKeyCredential(cred_hex)) +} diff --git a/lib/exunits-research/plutus-types/address/address.ak b/lib/exunits-research/plutus-types/address/address.ak deleted file mode 100644 index 359a047..0000000 --- a/lib/exunits-research/plutus-types/address/address.ak +++ /dev/null @@ -1,7 +0,0 @@ -use exunits_research/mock_data/placeholder.{mock_pub_key_address} - -test xu_address_enterprize_address_comparison() { - let address1 = mock_pub_key_address(None) - let address2 = mock_pub_key_address(None) - address1 == address2 -} diff --git a/lib/exunits-research/plutus-types/address/full-address.ak b/lib/exunits-research/plutus-types/address/full-address.ak new file mode 100644 index 0000000..67eee56 --- /dev/null +++ b/lib/exunits-research/plutus-types/address/full-address.ak @@ -0,0 +1,22 @@ +use aiken/transaction/credential.{Address} +use exunits_research/mock_data/placeholder.{ + mock_pub_key_address, mock_script_address, mock_stake_cred, +} + +test xu_full_address_pub_key() { + let function = + fn(_address: Address) { True } + let stake_cred = + mock_stake_cred(#"52e322a5dd87fe977c5ee5b91e3f42eedeccd6795a7ef4f158bd8892") + let address = mock_pub_key_address(Some(stake_cred)) + function(address) +} + +test xu_full_address_script() { + let function = + fn(_address: Address) { True } + let stake_cred = + mock_stake_cred(#"52e322a5dd87fe977c5ee5b91e3f42eedeccd6795a7ef4f158bd8892") + let address = mock_script_address(Some(stake_cred)) + function(address) +} diff --git a/lib/exunits-research/plutus-types/address/payment-address.ak b/lib/exunits-research/plutus-types/address/payment-address.ak new file mode 100644 index 0000000..1eec66d --- /dev/null +++ b/lib/exunits-research/plutus-types/address/payment-address.ak @@ -0,0 +1,38 @@ +use aiken/transaction/credential.{Address} +use exunits_research/mock_data/placeholder.{ + mock_pub_key_address, mock_pub_key_address_2, mock_script_address, +} + +test xu_payment_address_pub_key() { + let function = + fn(_address: Address) { True } + let address = mock_pub_key_address(None) + function(address) +} + +test xu_payment_address_script() { + let function = + fn(_address: Address) { True } + let address = mock_script_address(None) + function(address) +} + +test xu_payment_address_pub_key_equal() { + let address1 = mock_pub_key_address(None) + let address2 = mock_pub_key_address(None) + address1 == address2 +} + +test xu_payment_address_pub_key_unequal() { + let address1 = mock_pub_key_address(None) + let address2 = mock_pub_key_address_2(None) + address1 != address2 +} + +test xu_payment_address_pass_in_pub_key_equal() { + let compare = + fn(address1: Address, address2: Address) { address1 == address2 } + let address = mock_pub_key_address(None) + let address2 = mock_pub_key_address(None) + compare(address, address2) +} diff --git a/lib/exunits-research/plutus-types/address/script-hash.ak b/lib/exunits-research/plutus-types/address/script-hash.ak new file mode 100644 index 0000000..d1af4da --- /dev/null +++ b/lib/exunits-research/plutus-types/address/script-hash.ak @@ -0,0 +1,25 @@ +use aiken/transaction/credential.{Address, ScriptCredential} +use exunits_research/mock_data/placeholder.{mock_script_address, + mock_script_hex} + +test xu_script_hash() { + let get_script_hash = + fn(address: Address) { + let Address { payment_credential, .. } = address + expect ScriptCredential(_pub_key_hash) = payment_credential + True + } + let address = mock_script_address(None) + get_script_hash(address) +} + +test xu_script_hash_get_and_check() { + let get_script_hash = + fn(address: Address) { + let Address { payment_credential, .. } = address + expect ScriptCredential(pub_key_hash) = payment_credential + pub_key_hash == mock_script_hex() + } + let address = mock_script_address(None) + get_script_hash(address) +} diff --git a/lib/exunits-research/primitives/byte-array.ak b/lib/exunits-research/primitives/byte-array.ak index f572988..332264a 100644 --- a/lib/exunits-research/primitives/byte-array.ak +++ b/lib/exunits-research/primitives/byte-array.ak @@ -1,40 +1,83 @@ -test xu_bytearray_1() { - let one_byte: ByteArray = #[0] - one_byte == one_byte +test xu_byte_1() { + let deser = + fn(_b) { True } + deser(#[0]) } -test xu_byte_array_32() { - let bytestring: ByteArray = +test xu_byte_24() { + let deser = + fn(_b) { True } + deser(#"52e322a5dd87fe977c5ee5b91e3f42eedeccd6795a7ef4f158bd8892") +} + +test xu_byte_1_equal() { + let one_byte1: ByteArray = #[0] + let one_byte2: ByteArray = #[0] + one_byte1 == one_byte2 +} + +test xu_byte_24_equal() { + let bytestring1: ByteArray = + #"52e322a5dd87fe977c5ee5b91e3f42eedeccd6795a7ef4f158bd8892" + let bytestring2: ByteArray = #"52e322a5dd87fe977c5ee5b91e3f42eedeccd6795a7ef4f158bd8892" - bytestring == bytestring + bytestring1 == bytestring2 } -test xu_byte1_incorrect() { - let bytestring: ByteArray = #"52" - let bytestring2: ByteArray = #"52" - bytestring == bytestring2 +test xu_byte_1_incorrect() { + let bytestring1: ByteArray = #"00" + let bytestring2: ByteArray = #"01" + bytestring1 != bytestring2 } -test xu_byte32_incorrect_at_head() { - let bytestring: ByteArray = +test xu_byte_24_incorrect_at_head() { + let bytestring1: ByteArray = #"52e322a5dd87fe977c5ee5b91e3f42eedeccd6795a7ef4f158bd8892" let bytestring2: ByteArray = #"42e322a5dd87fe977c5ee5b91e3f42eedeccd6795a7ef4f158bd8892" - bytestring != bytestring2 + bytestring1 != bytestring2 } -test xu_byte32_incorrect_at_tail() { - let bytestring: ByteArray = +test xu_byte_24_incorrect_at_tail() { + let bytestring1: ByteArray = #"52e322a5dd87fe977c5ee5b91e3f42eedeccd6795a7ef4f158bd8892" let bytestring2: ByteArray = #"52e322a5dd87fe977c5ee5b91e3f42eedeccd6795a7ef4f158bd8893" - bytestring != bytestring2 + bytestring1 != bytestring2 } -test xu_byte32_incorrect_at_middle() { - let bytestring: ByteArray = +test xu_byte_24_incorrect_at_middle() { + let bytestring1: ByteArray = #"52e322a5dd87fe977c5ee5b91e4f42eedeccd6795a7ef4f158bd8893" let bytestring2: ByteArray = #"52e322a5dd87fe977c5ee5b91e3f42eedeccd6795a7ef4f158bd8893" - bytestring != bytestring2 + bytestring1 != bytestring2 +} + +test xu_byte_3_bytes_diff_in_length_incorrect_long() { + let bytestring1: ByteArray = + #"52e322a5dd87fe977c5ee5b91e3f42eedeccd6795a7ef4f158bd8892" + let bytestring3: ByteArray = + #"52e322a5dd87fe977c5ee5b91e3f42eedeccd6795a7ef4f158" + bytestring1 != bytestring3 +} + +test xu_byte_4_bytes_diff_in_length_incorrect_long() { + let bytestring1: ByteArray = + #"52e322a5dd87fe977c5ee5b91e3f42eedeccd6795a7ef4f158bd8892" + let bytestring3: ByteArray = + #"52e322a5dd87fe977c5ee5b91e3f42eedeccd6795a7ef4f1" + bytestring1 != bytestring3 +} + +test xu_byte_7_bytes_diff_in_length_incorrect_short() { + let bytestring1: ByteArray = #"52e322a5dd87fe97" + let bytestring2: ByteArray = #"52" + bytestring1 != bytestring2 +} + +test xu_byte_8_bytes_diff_in_length_incorrect_short() { + let bytestring1: ByteArray = #"52e322a5dd87fe97aa" + let bytestring2: ByteArray = #"52" + bytestring1 != bytestring2 } diff --git a/report/report.md b/report/report.md deleted file mode 100644 index aa4894e..0000000 --- a/report/report.md +++ /dev/null @@ -1,16 +0,0 @@ -# All Tests - -## Simple True or False assertion - -- [mem: 200, cpu: 23100] - -## ByteArray - -- All `ByteArray` correct checking has [mem: 601] -- Steps slightly different with length, where - - - 1 Byte: [cpu: 331935] - - 32 Byte: [cpu: 332121] - -- Incorrect `ByteArray checking` would take (regardless of length) - - [mem: 1302, cpu: 573677] at length of 32 diff --git a/reports/report-control-flow.md b/reports/report-control-flow.md new file mode 100644 index 0000000..e69de29 diff --git a/reports/report-deserialization.md b/reports/report-deserialization.md new file mode 100644 index 0000000..e69de29 diff --git a/reports/report-plutus-types.md b/reports/report-plutus-types.md new file mode 100644 index 0000000..e69de29 diff --git a/reports/report-primitives.md b/reports/report-primitives.md new file mode 100644 index 0000000..c9b7d4f --- /dev/null +++ b/reports/report-primitives.md @@ -0,0 +1,39 @@ +# Primitives Test Report + +## ByteArray + +### Run the test result + +```sh +aiken check -m xu_byte +``` + +### Findings + +1. Passing `ByteArray` in a function does not cost any mem and step + + - Same as True or False comparison: [mem: 200, cpu: 23100] + - Tests + - [x] `xu_byte_1` + - [x] `xu_byte_24` + +2. All `ByteArray` correct checking has [mem: 601], and Steps slightly different with length, where + + - 1 Byte: [cpu: 331935] + - 32 Byte: [cpu: 332121] + - Tests + - [x] `xu_byte_1_equal` + - [x] `xu_byte_24_equal` + +3. Incorrect `ByteArray checking` would take somewhat similar ExUnits regardless of length. Just up to some 4 bytes difference in length would cause a jump in around ~30000 steps. + + - Range of ExUnits: [mem: 1302, cpu: 573491 - 601656] + - Tests + - [x] xu_byte_1_incorrect + - [x] xu_byte_24_incorrect_at_head + - [x] xu_byte_24_incorrect_at_tail + - [x] xu_byte_24_incorrect_at_middle + - [x] xu_byte_3_bytes_diff_in_length_incorrect_long + - [x] xu_byte_4_bytes_diff_in_length_incorrect_long + - [x] xu_byte_7_bytes_diff_in_length_incorrect_short + - [x] xu_byte_8_bytes_diff_in_length_incorrect_short diff --git a/reports/report.md b/reports/report.md new file mode 100644 index 0000000..4cb01f4 --- /dev/null +++ b/reports/report.md @@ -0,0 +1,5 @@ +# All Tests + +## Simple True or False assertion + +- [mem: 200, cpu: 23100]