Skip to content

Commit

Permalink
feat: building byte array findings
Browse files Browse the repository at this point in the history
  • Loading branch information
HinsonSIDAN committed Dec 11, 2023
1 parent a7ad921 commit 81ff9f4
Show file tree
Hide file tree
Showing 18 changed files with 204 additions and 45 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!
7 changes: 6 additions & 1 deletion lib/exunits-research/mock-data/placeholder.ak
Original file line number Diff line number Diff line change
@@ -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}

Expand Down Expand Up @@ -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))
}
7 changes: 0 additions & 7 deletions lib/exunits-research/plutus-types/address/address.ak

This file was deleted.

22 changes: 22 additions & 0 deletions lib/exunits-research/plutus-types/address/full-address.ak
Original file line number Diff line number Diff line change
@@ -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)
}
38 changes: 38 additions & 0 deletions lib/exunits-research/plutus-types/address/payment-address.ak
Original file line number Diff line number Diff line change
@@ -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)
}
25 changes: 25 additions & 0 deletions lib/exunits-research/plutus-types/address/script-hash.ak
Original file line number Diff line number Diff line change
@@ -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)
}
81 changes: 62 additions & 19 deletions lib/exunits-research/primitives/byte-array.ak
Original file line number Diff line number Diff line change
@@ -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
}
16 changes: 0 additions & 16 deletions report/report.md

This file was deleted.

Empty file added reports/report-control-flow.md
Empty file.
Empty file.
Empty file added reports/report-plutus-types.md
Empty file.
39 changes: 39 additions & 0 deletions reports/report-primitives.md
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions reports/report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# All Tests

## Simple True or False assertion

- [mem: 200, cpu: 23100]

0 comments on commit 81ff9f4

Please sign in to comment.