Skip to content

Commit

Permalink
aiken 0.0.19 in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Czeladka committed Oct 9, 2023
1 parent 8cc4f13 commit 038428f
Show file tree
Hide file tree
Showing 16 changed files with 1,660 additions and 693 deletions.
636 changes: 220 additions & 416 deletions lib/acca/collections/hash_tree.ak

Large diffs are not rendered by default.

98 changes: 50 additions & 48 deletions lib/acca/collections/mt.ak
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,29 @@ pub fn from_hash(hash: Hash<alg, a>) -> Root {
/// Deconstruct a [MerkleTree](#MerkleTree) back to a list of elements.
pub fn to_list(self: MerkleTree<a>) -> List<a> {
when self is {
Empty -> []
Leaf { value, .. } -> [value]
Empty ->
[]
Leaf { value, .. } ->
[value]
Node { left, right, .. } -> list.concat(to_list(left), to_list(right))
}
}

test to_list_1() {
let items = []
let items =
[]
to_list(from_list(items, identity)) == items
}

test to_list_2() {
let items = ["dog"]
let items =
["dog"]
to_list(from_list(items, identity)) == items
}

test to_list_3() {
let items = ["dog", "cat", "mouse"]
let items =
["dog", "cat", "mouse"]
to_list(from_list(items, identity)) == items
}

Expand All @@ -95,9 +100,9 @@ pub fn root(self: MerkleTree<a>) -> Root {

test root_1() {
from_list([], identity)
|> root
|> to_hash
|> is_none
|> root
|> to_hash
|> is_none
}

test root_2() {
Expand All @@ -113,11 +118,11 @@ test root_3() {

let hash =
"mouse"
|> sha2_256
|> bytearray.concat(sha2_256(cat), _)
|> sha2_256
|> bytearray.concat(sha2_256(dog), _)
|> sha2_256
|> sha2_256
|> bytearray.concat(sha2_256(cat), _)
|> sha2_256
|> bytearray.concat(sha2_256(dog), _)
|> sha2_256

to_hash(root(from_list([dog, cat, mouse], identity))) == Some(hash)
}
Expand All @@ -132,23 +137,26 @@ pub fn equals(left: MerkleTree<a>, right: MerkleTree<a>) -> Bool {
pub fn size(self: MerkleTree<a>) -> Int {
when self is {
Empty -> 0
Leaf{..} -> 1
Leaf { .. } -> 1
Node { left, right, .. } -> size(left) + size(right)
}
}

test size_1() {
let items = []
let items =
[]
size(from_list(items, identity)) == 0
}

test size_2() {
let items = ["dog"]
let items =
["dog"]
size(from_list(items, identity)) == 1
}

test size_3() {
let items = ["dog", "cat", "mouse"]
let items =
["dog", "cat", "mouse"]
size(from_list(items, identity)) == 3
}

Expand Down Expand Up @@ -206,14 +214,14 @@ fn do_get_proof(

test get_proof_1() {
from_list([], identity)
|> get_proof("dog", identity)
|> is_none
|> get_proof("dog", identity)
|> is_none
}

test get_proof_2() {
from_list(["dog", "cat", "mouse", "horse", "pig", "bull"], identity)
|> get_proof("parrot", identity)
|> is_none
|> get_proof("parrot", identity)
|> is_none
}

test get_proof_3() {
Expand All @@ -234,12 +242,12 @@ fn do_from_list(
let cutoff: Int = len / 2
let left =
items
|> list.take(cutoff)
|> do_from_list(cutoff, serialise)
|> list.take(cutoff)
|> do_from_list(cutoff, serialise)
let right =
items
|> list.drop(cutoff)
|> do_from_list(len - cutoff, serialise)
|> list.drop(cutoff)
|> do_from_list(len - cutoff, serialise)
let root = combine(root(left), root(right))
Node { root, left, right }
}
Expand Down Expand Up @@ -281,10 +289,10 @@ test from_list_4() {

let root_hash =
sha2_256(mouse)
|> bytearray.concat(sha2_256(cat), _)
|> sha2_256
|> bytearray.concat(sha2_256(dog), _)
|> sha2_256
|> bytearray.concat(sha2_256(cat), _)
|> sha2_256
|> bytearray.concat(sha2_256(dog), _)
|> sha2_256

root(from_list([dog, cat, mouse], identity)) == from_hash(root_hash)
}
Expand Down Expand Up @@ -337,23 +345,17 @@ test get_proof_is_member_1() {
expect Some(cat_proof) = get_proof(mt, cat, identity)
expect Some(mouse_proof) = get_proof(mt, mouse, identity)

let all_members =
list.and(
[
is_member(root(mt), dog, dog_proof, identity)?,
is_member(root(mt), cat, cat_proof, identity)?,
is_member(root(mt), mouse, mouse_proof, identity)?,
],
)

let check_sizes =
list.and(
[
(list.length(dog_proof) == 1)?,
(list.length(cat_proof) == 2)?,
(list.length(mouse_proof) == 2)?,
],
)
let all_members = and {
is_member(root(mt), dog, dog_proof, identity)?,
is_member(root(mt), cat, cat_proof, identity)?,
is_member(root(mt), mouse, mouse_proof, identity)?,
}

let check_sizes = and {
(list.length(dog_proof) == 1)?,
(list.length(cat_proof) == 2)?,
(list.length(mouse_proof) == 2)?,
}

all_members? && check_sizes?
}
Expand All @@ -373,6 +375,6 @@ test get_proof_is_member_2() {

fn combine(left: Root, right: Root) -> Root {
bytearray.concat(left.inner, right.inner)
|> sha2_256
|> Root
|> sha2_256
|> Root
}
48 changes: 22 additions & 26 deletions lib/acca/collections/stack.ak
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ pub fn push(self: Stack<a>, value: a) -> Stack<a> {
/// ```
pub fn pop(self: Stack<a>) -> (Option<a>, Stack<a>) {
when self.inner is {
[] ->
(None, self)
[h, ..t] ->
(Some(h), from_list(t))
[] -> (None, self)
[h, ..t] -> (Some(h), from_list(t))
}
}

Expand All @@ -72,10 +70,8 @@ pub fn size(self: Stack<a>) -> Int {
/// ```
pub fn peek(self: Stack<a>) -> Option<a> {
when self.inner is {
[] ->
None
[h, ..] ->
Some(h)
[] -> None
[h, ..] -> Some(h)
}
}

Expand All @@ -87,40 +83,40 @@ pub fn peek(self: Stack<a>) -> Option<a> {
/// ```
pub fn is_empty(self: Stack<a>) -> Bool {
when self.inner is {
[] ->
True
_ ->
False
[] -> True
_ -> False
}
}

test stack_0() {
let s =
new()
let s = new()

is_empty(s)
}

test stack_1() {
let s =
new() |> push(1) |> push(2) |> push(3)
let s = new() |> push(1) |> push(2) |> push(3)

is_empty(s) == False
}

test stack_2() {
let s =
new() |> push(1) |> push(2) |> push(3)

let (Some(x), stack) =
pop(s)

list.and([x == 3, size(stack) == 2])
let s = new() |> push(1) |> push(2) |> push(3)

when pop(s) is {
(Some(x), stack) -> and {
x == 3,
size(stack) == 2,
}
_ -> False
}
}

test stack_3() {
let s =
new() |> push(1) |> push(2) |> push(3)
let s = new() |> push(1) |> push(2) |> push(3)

list.and([peek(s) == Some(3), size(s) == 3])
and {
peek(s) == Some(3),
size(s) == 3,
}
}
Loading

0 comments on commit 038428f

Please sign in to comment.