Skip to content
Merged
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This repository contains the standard library for [SimplicityHL](https://github.
## Modules

- asserts
- logical_operations
- binary
- op_return
- u8
- u16
Expand All @@ -23,9 +23,9 @@ This repository contains the standard library for [SimplicityHL](https://github.
Generic assertion helpers with equality checks between uint values and validation of Option uint variants.

---
`Logical operations`
`Binary`

Basic boolean logic operations: and, or, not.
Basic binary logic operations: and, or, not, xor.

---
`OP_RETURN`
Expand Down
7 changes: 6 additions & 1 deletion simf/logical_ops_test.simf → simf/binary_test.simf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::lib::logical_operations::{not, or, and};
use crate::lib::binary::{not, or, and, xor};

fn main() {
assert!(not(false));
Expand All @@ -13,4 +13,9 @@ fn main() {
assert!(not(and(true, false)));
assert!(not(and(false, true)));
assert!(not(and(false, false)));

assert!(xor(true, false));
assert!(xor(false, true));
assert!(not(xor(true, true)));
assert!(not(xor(false, false)));
}
4 changes: 2 additions & 2 deletions simf/helper.simf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::lib::logical_operations::not;
use crate::lib::binary::not;

pub fn if_test_this_function(index: u8, flag: u8) -> bool {
jet::eq_8(index, flag)
Expand All @@ -10,4 +10,4 @@ pub fn assert_bool(result: bool, expected_bool: bool) {
true => assert!(result),
false => assert!(not(result)),
}
}
}
5 changes: 5 additions & 0 deletions simf/lib/logical_operations.simf → simf/lib/binary.simf
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ pub fn or(a: bool, b: bool) -> bool {
pub fn and(a: bool, b: bool) -> bool {
<u1>::into(jet::and_1(<bool>::into(a), <bool>::into(b)))
}

/// Returns the result of the XOR operation on the provided values
pub fn xor(a: bool, b: bool) -> bool {
and(or(a, b), not(and(a, b)))
}
2 changes: 1 addition & 1 deletion simf/lib/secp256k1/operations.simf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::lib::logical_operations::and;
use crate::lib::binary::and;
use crate::lib::asserts::{assert_eq_1, assert_eq_256};

/// Compress an affine point to `(parity, x)`, where `parity = 1` iff `y` is odd.
Expand Down
3 changes: 2 additions & 1 deletion simf/lib/u128.simf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::lib::logical_operations::{not, or, and};
use crate::lib::binary::{not, or, and};

/// Bit logic

Expand Down Expand Up @@ -249,6 +249,7 @@ pub fn safe_mul_128(a: u128, b: u128) -> u128 {
}

/// Splits the u256 integer into four u64 integers
// TODO: Move to u256 once that module is added.
pub fn split_256_into_64(a: u256) -> ((u64, u64), (u64, u64)) {
let (high, low): (u128, u128) = <u256>::into(a);

Expand Down
2 changes: 1 addition & 1 deletion simf/u128_test.simf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::lib::u128::{ checked_add_128, safe_add_128, checked_sub_128, safe_sub_128, checked_mul_128, safe_mul_128, checked_div_128, safe_div_128, eq_128, gt_128, ge_128 };
use crate::lib::asserts::{assert_none_128, assert_eq_128};
use crate::lib::logical_operations::not;
use crate::lib::binary::not;
use crate::helper::if_test_this_function;

/// Asserts a `checked_*` result equals the expected Option.
Expand Down
2 changes: 1 addition & 1 deletion simf/u16_test.simf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::lib::u16::{checked_add_16, safe_add_16, checked_sub_16, safe_sub_16, checked_mul_16, safe_mul_16, checked_div_16, safe_div_16, gt_16, ge_16};
use crate::lib::asserts::{assert_none_16, assert_eq_16};
use crate::lib::logical_operations::not;
use crate::lib::binary::not;
use crate::helper::if_test_this_function;

/// Asserts a `checked_*` result equals the expected Option.
Expand Down
2 changes: 1 addition & 1 deletion simf/u32_test.simf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::lib::u32::{checked_add_32, safe_add_32, checked_sub_32, safe_sub_32, checked_mul_32, safe_mul_32, checked_div_32, safe_div_32, gt_32, ge_32};
use crate::lib::asserts::{assert_none_32, assert_eq_32};
use crate::lib::logical_operations::not;
use crate::lib::binary::not;
use crate::helper::if_test_this_function;

/// Asserts a `checked_*` result equals the expected Option.
Expand Down
2 changes: 1 addition & 1 deletion simf/u64_test.simf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::lib::u64::{
gt_64, ge_64, u64_into_u256,
};
use crate::lib::asserts::{assert_none_64, assert_eq_64};
use crate::lib::logical_operations::not;
use crate::lib::binary::not;
use crate::helper::if_test_this_function;

/// Asserts a `checked_*` result equals the expected Option.
Expand Down
2 changes: 1 addition & 1 deletion simf/u8_test.simf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::lib::u8::{checked_add_8, safe_add_8, checked_sub_8, safe_sub_8, checked_mul_8, safe_mul_8, checked_div_8, safe_div_8, gt_8, ge_8};
use crate::lib::asserts::{assert_none_8, assert_eq_8};
use crate::lib::logical_operations::not;
use crate::lib::binary::not;
use crate::helper::if_test_this_function;

/// Asserts a `checked_*` result equals the expected Option.
Expand Down
18 changes: 18 additions & 0 deletions tests/binary_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
mod common;

use common::core::{Expect, run};

use simplicityhl_std::artifacts::binary_test::BinaryTestProgram;
use simplicityhl_std::artifacts::binary_test::derived_binary_test::{
BinaryTestArguments, BinaryTestWitness,
};

mod binary_tests {
use super::*;

#[simplex::test]
fn binary_test(context: simplex::TestContext) -> anyhow::Result<()> {
let program = BinaryTestProgram::new(BinaryTestArguments {});
run(&context, program, BinaryTestWitness {}, Expect::Ok)
}
}
16 changes: 16 additions & 0 deletions tests/common/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ fn op(o: CommonOp) -> u8 {
pub fn checked_add_fitting<T: TestUint>(context: simplex::TestContext) -> anyhow::Result<()> {
let a = rand::thread_rng().gen_range(T::ZERO..=T::HALF_MAX);
let b = rand::thread_rng().gen_range(T::ZERO..=T::HALF_MAX);

run(
&context,
T::program(),
Expand All @@ -77,6 +78,7 @@ pub fn checked_add_fitting<T: TestUint>(context: simplex::TestContext) -> anyhow

pub fn checked_add_overflow<T: TestUint>(context: simplex::TestContext) -> anyhow::Result<()> {
let b = rand::thread_rng().gen_range(T::ONE..=T::MAX);

run(
&context,
T::program(),
Expand All @@ -88,6 +90,7 @@ pub fn checked_add_overflow<T: TestUint>(context: simplex::TestContext) -> anyho
pub fn safe_add_fitting<T: TestUint>(context: simplex::TestContext) -> anyhow::Result<()> {
let a = rand::thread_rng().gen_range(T::ZERO..=T::HALF_MAX);
let b = rand::thread_rng().gen_range(T::ZERO..=T::HALF_MAX);

run(
&context,
T::program(),
Expand All @@ -98,6 +101,7 @@ pub fn safe_add_fitting<T: TestUint>(context: simplex::TestContext) -> anyhow::R

pub fn safe_add_overflow<T: TestUint>(context: simplex::TestContext) -> anyhow::Result<()> {
let b = rand::thread_rng().gen_range(T::ONE..=T::MAX);

run(
&context,
T::program(),
Expand All @@ -110,6 +114,7 @@ pub fn safe_add_overflow<T: TestUint>(context: simplex::TestContext) -> anyhow::
pub fn checked_sub_fitting<T: TestUint>(context: simplex::TestContext) -> anyhow::Result<()> {
let a = rand::thread_rng().gen_range(T::ZERO..=T::MAX);
let b = rand::thread_rng().gen_range(T::ZERO..=a);

run(
&context,
T::program(),
Expand All @@ -121,6 +126,7 @@ pub fn checked_sub_fitting<T: TestUint>(context: simplex::TestContext) -> anyhow
pub fn checked_sub_overflow<T: TestUint>(context: simplex::TestContext) -> anyhow::Result<()> {
let a = rand::thread_rng().gen_range(T::ZERO..=T::MAX - T::ONE);
let b = rand::thread_rng().gen_range(a + T::ONE..=T::MAX);

run(
&context,
T::program(),
Expand All @@ -132,6 +138,7 @@ pub fn checked_sub_overflow<T: TestUint>(context: simplex::TestContext) -> anyho
pub fn safe_sub_fitting<T: TestUint>(context: simplex::TestContext) -> anyhow::Result<()> {
let a = rand::thread_rng().gen_range(T::ZERO..=T::MAX);
let b = rand::thread_rng().gen_range(T::ZERO..=a);

run(
&context,
T::program(),
Expand All @@ -143,6 +150,7 @@ pub fn safe_sub_fitting<T: TestUint>(context: simplex::TestContext) -> anyhow::R
pub fn safe_sub_overflow<T: TestUint>(context: simplex::TestContext) -> anyhow::Result<()> {
let a = rand::thread_rng().gen_range(T::ZERO..=T::MAX - T::ONE);
let b = rand::thread_rng().gen_range(a + T::ONE..=T::MAX);

run(
&context,
T::program(),
Expand All @@ -155,6 +163,7 @@ pub fn safe_sub_overflow<T: TestUint>(context: simplex::TestContext) -> anyhow::
pub fn checked_mul_fitting<T: TestUint>(context: simplex::TestContext) -> anyhow::Result<()> {
let a = rand::thread_rng().gen_range(T::ZERO..T::MUL_BOUND);
let b = rand::thread_rng().gen_range(T::ZERO..T::MUL_BOUND);

run(
&context,
T::program(),
Expand All @@ -166,6 +175,7 @@ pub fn checked_mul_fitting<T: TestUint>(context: simplex::TestContext) -> anyhow
pub fn checked_mul_overflow<T: TestUint>(context: simplex::TestContext) -> anyhow::Result<()> {
let two = T::ONE + T::ONE;
let b = rand::thread_rng().gen_range(two..=T::MAX);

run(
&context,
T::program(),
Expand All @@ -177,6 +187,7 @@ pub fn checked_mul_overflow<T: TestUint>(context: simplex::TestContext) -> anyho
pub fn safe_mul_fitting<T: TestUint>(context: simplex::TestContext) -> anyhow::Result<()> {
let a = rand::thread_rng().gen_range(T::ZERO..T::MUL_BOUND);
let b = rand::thread_rng().gen_range(T::ZERO..T::MUL_BOUND);

run(
&context,
T::program(),
Expand All @@ -188,6 +199,7 @@ pub fn safe_mul_fitting<T: TestUint>(context: simplex::TestContext) -> anyhow::R
pub fn safe_mul_overflow<T: TestUint>(context: simplex::TestContext) -> anyhow::Result<()> {
let two = T::ONE + T::ONE;
let b = rand::thread_rng().gen_range(two..=T::MAX);

run(
&context,
T::program(),
Expand All @@ -200,6 +212,7 @@ pub fn safe_mul_overflow<T: TestUint>(context: simplex::TestContext) -> anyhow::
pub fn checked_div_fitting<T: TestUint>(context: simplex::TestContext) -> anyhow::Result<()> {
let a = rand::thread_rng().gen_range(T::ZERO..=T::MAX);
let b = rand::thread_rng().gen_range(T::ONE..=T::MAX);

run(
&context,
T::program(),
Expand All @@ -210,6 +223,7 @@ pub fn checked_div_fitting<T: TestUint>(context: simplex::TestContext) -> anyhow

pub fn checked_div_by_zero<T: TestUint>(context: simplex::TestContext) -> anyhow::Result<()> {
let a = rand::thread_rng().gen_range(T::ZERO..=T::MAX);

run(
&context,
T::program(),
Expand All @@ -221,6 +235,7 @@ pub fn checked_div_by_zero<T: TestUint>(context: simplex::TestContext) -> anyhow
pub fn safe_div_fitting<T: TestUint>(context: simplex::TestContext) -> anyhow::Result<()> {
let a = rand::thread_rng().gen_range(T::ZERO..=T::MAX);
let b = rand::thread_rng().gen_range(T::ONE..=T::MAX);

run(
&context,
T::program(),
Expand All @@ -231,6 +246,7 @@ pub fn safe_div_fitting<T: TestUint>(context: simplex::TestContext) -> anyhow::R

pub fn safe_div_by_zero<T: TestUint>(context: simplex::TestContext) -> anyhow::Result<()> {
let a = rand::thread_rng().gen_range(T::ZERO..=T::MAX);

run(
&context,
T::program(),
Expand Down
18 changes: 0 additions & 18 deletions tests/logical_ops_test.rs

This file was deleted.