Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bitwise math #106

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion aiken.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "aiken-lang/stdlib"
version = "main"
compiler = "v1.1.9"
compiler = "v1.1.11"
plutus = "v3"
description = "The Aiken Standard Library"

Expand Down
127 changes: 127 additions & 0 deletions lib/aiken/math/bitwise.ak
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
use aiken/builtin

/// Addition will at most add a single byte to a bytearray. This will prepad the
/// bytearrays to the correct length for addition and it will correct for mismatched
/// lengths.
///
/// ```aiken
/// bitwise.pad_for_addition(#"acab", #"cafe") == (#"00acab", #"00cafe")
/// ```
pub fn pad_for_addition(a: ByteArray, b: ByteArray) -> (ByteArray, ByteArray) {
let length_a: Int = builtin.length_of_bytearray(a)
let length_b: Int = builtin.length_of_bytearray(b)
if length_a >= length_b {
let diff: Int = length_a - length_b
(
builtin.cons_bytearray(0, a),
builtin.append_bytearray(builtin.replicate_byte(diff + 1, 0), b),
)
} else {
let diff: Int = length_b - length_a
(
builtin.append_bytearray(builtin.replicate_byte(diff + 1, 0), a),
builtin.cons_bytearray(0, b),
)
}
}

/// Add two ByteArrays, a + b, using bitwise operations. The function
/// assumes fixed-width (modular) arithmetic. If arbitrary precision is required
/// use `pad_for_addition`.
///
/// -- pad inputs for arbitrary precision
///
/// ```aiken
/// bitwise.add(#"00acab", #"00cafe") == #"0177A9"
/// ```
///
/// -- otherwise
///
/// ```aiken
/// bitwise.add(#"acab", #"cafe") == #"77A9"
/// ```
pub fn add(a: ByteArray, b: ByteArray) -> ByteArray {
if builtin.count_set_bits(b) == 0 {
a
} else {
let carry: ByteArray =
builtin.and_bytearray(True, a, b) |> builtin.shift_bytearray(1)
let sum_without_carry: ByteArray = builtin.xor_bytearray(True, a, b)
add(sum_without_carry, carry)
}
}

/// Subtracts two ByteArrays, a - b, using bitwise operations.
///
/// ```aiken
/// bitwise.subtract(#"77a9", #"cafe") == #"acab"
/// ```
pub fn subtract(a: ByteArray, b: ByteArray) -> ByteArray {
if builtin.count_set_bits(b) == 0 {
a
} else {
let borrow: ByteArray =
builtin.and_bytearray(True, builtin.complement_bytearray(a), b)
|> builtin.shift_bytearray(1)
let diff_without_borrow: ByteArray = builtin.xor_bytearray(True, a, b)
subtract(diff_without_borrow, borrow)
}
}

/// Multiplication will at most add N bytes to a bytearray for a bytearray of size N.
/// This will prepad the bytearrays to the correct length for multiplication and it
/// will correct for mismatched lengths.
///
/// ```aiken
/// bitwise.pad_for_multiply(#"acab", #"cafe") == (#"0000acab", #"0000cafe")
/// ```
pub fn pad_for_multiply(a: ByteArray, b: ByteArray) -> (ByteArray, ByteArray) {
let length_a: Int = builtin.length_of_bytearray(a)
let length_b: Int = builtin.length_of_bytearray(b)
if length_a >= length_b {
let diff: Int = length_a - length_b
(
builtin.append_bytearray(builtin.replicate_byte(length_a, 0), a),
builtin.append_bytearray(builtin.replicate_byte(diff + length_a, 0), b),
)
} else {
let diff: Int = length_b - length_a
(
builtin.append_bytearray(builtin.replicate_byte(diff + length_b, 0), a),
builtin.append_bytearray(builtin.replicate_byte(length_b, 0), b),
)
}
}

pub fn multiply(a: ByteArray, b: ByteArray) -> ByteArray {
let length_a: Int = builtin.length_of_bytearray(a)
let zero: ByteArray = builtin.replicate_byte(length_a, 0)
let one: ByteArray =
builtin.append_bytearray(builtin.replicate_byte(length_a, 0), #"01")
do_multiply(a, b, zero, one)
}

fn do_multiply(
a: ByteArray,
b: ByteArray,
acc: ByteArray,
one: ByteArray,
) -> ByteArray {
if builtin.count_set_bits(b) == 0 {
acc
} else {
let bit_set = builtin.and_bytearray(True, b, #"00000001")
let new_acc =
if builtin.count_set_bits(bit_set) > 0 {
add(acc, a)
} else {
acc
}

let new_a = builtin.shift_bytearray(a, 1)
// left shift: multiply a by 2
let new_b = builtin.shift_bytearray(b, -1)
// right shift: divide b by 2
do_multiply(new_a, new_b, new_acc, one)
}
}
123 changes: 123 additions & 0 deletions lib/aiken/math/bitwise.tests.ak
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
use aiken/math/bitwise.{
add, multiply, pad_for_addition, pad_for_multiply, subtract,
}

test equal_pad_for_addition() {
let a: ByteArray = #"acab"
let b: ByteArray = #"cafe"
let (new_a, new_b) = pad_for_addition(a, b)
and {
new_a == #"00acab",
new_b == #"00cafe",
}
}

test unequal_pad_for_addition1() {
let a: ByteArray = #"acabbeefface"
let b: ByteArray = #"cafe"
let (new_a, new_b) = pad_for_addition(a, b)
and {
new_a == #"00acabbeefface",
new_b == #"0000000000cafe",
}
}

test unequal_pad_for_addition2() {
let b: ByteArray = #"acabbeefface"
let a: ByteArray = #"cafe"
let (new_a, new_b) = pad_for_addition(a, b)
and {
new_a == #"0000000000cafe",
new_b == #"00acabbeefface",
}
}

test pad_for_addition_does_works() {
let b: ByteArray = #"acab"
let a: ByteArray = #"cafe"
let (new_a, new_b) = pad_for_addition(a, b)
and {
add(a, b) == #"77a9",
add(new_a, new_b) == #"0177a9",
}
}

test emptiness_is_empty() {
add(#"", #"") == #""
}

test adding_not_equal_length_does_not_work() fail {
add(#"00", #"acab") == #"acab"
}

test add_communitive() {
add(#"acab", #"cafe") == add(#"cafe", #"acab")
}

test add_associativity() {
( add(#"0101", #"0202") |> add(#"0303") ) == (
add(#"0202", #"0303") |> add(#"0101")
)
}

test add_identity() {
add(#"00", #"01") == #"01"
}

test subtracting_does_work1() {
subtract(#"0177a9", #"00cafe") == #"00acab"
}

test subtracting_does_work2() {
subtract(#"77a9", #"cafe") == #"acab"
}

test subtracting_not_equal_length_does_not_work() fail {
subtract(#"0177a9", #"cafe") == #"acab"
}

test subtract_is_not_communitive() fail {
subtract(#"acab", #"cafe") == add(#"cafe", #"acab")
}

test subtract_identity() {
subtract(#"10", #"00") == #"10"
}

test equal_pad_for_multiply() {
let a: ByteArray = #"acab"
let b: ByteArray = #"cafe"
let (new_a, new_b) = pad_for_multiply(a, b)
and {
new_a == #"0000acab",
new_b == #"0000cafe",
}
}

test unequal_pad_for_multiply1() {
let a: ByteArray = #"acabbeefface"
let b: ByteArray = #"cafe"
let (new_a, new_b) = pad_for_multiply(a, b)
and {
new_a == #"000000000000acabbeefface",
new_b == #"00000000000000000000cafe",
}
}

test unequal_pad_for_multiply2() {
let b: ByteArray = #"acabbeefface"
let a: ByteArray = #"cafe"
let (new_a, new_b) = pad_for_multiply(a, b)
and {
new_a == #"00000000000000000000cafe",
new_b == #"000000000000acabbeefface",
}
}

test simple_multiply1() {
multiply(#"0000ffff", #"0000ffff") == #"fffe0001"
}

test multiply_communitive() {
multiply(#"0000acab", #"0000cafe") == multiply(#"0000cafe", #"0000acab")
}
10 changes: 5 additions & 5 deletions lib/cardano/transaction/script_purpose.ak
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,23 @@ pub fn compare(left: ScriptPurpose, right: ScriptPurpose) -> Ordering {
_ -> Less
}

Publish(left, _) ->
Publish { at: left, .. } ->
when right is {
Publish(right, _) -> int.compare(left, right)
Publish { at: right, .. } -> int.compare(left, right)
Spend(_) | Mint(_) | Withdraw(_) -> Greater
_ -> Less
}

Vote(left) ->
when right is {
Vote(right) -> voter.compare(left, right)
Propose(..) -> Less
Propose { .. } -> Less
_ -> Greater
}

Propose(left, _) ->
Propose { at: left, .. } ->
when right is {
Propose(right, _) -> int.compare(left, right)
Propose { at: right, .. } -> int.compare(left, right)
_ -> Greater
}
}
Expand Down