Skip to content
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
69 changes: 50 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ smplx-std = "0.0.8"
anyhow = { version = "1.0.101" }
rand = { version = "0.8.6" }
primitive-types = { version = "0.14" }
num-bigint = { version = "0.5.1" }
num-traits = { version = "0.2.19" }
secp256k1-zkp = { version = "0.11.0", features = ["rand"] }
34 changes: 21 additions & 13 deletions simf/asserts_test.simf
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
use crate::lib::asserts::{assert_eq_8, assert_eq_16, assert_eq_32, assert_eq_64,assert_eq_128, assert_eq_256, assert_none_8, assert_none_16, assert_none_32, assert_none_64, assert_none_128, assert_none_256};
use crate::lib::asserts::{
assert_eq_1, assert_eq_8, assert_eq_16, assert_eq_32, assert_eq_64,assert_eq_128, assert_eq_256,
assert_none_1, assert_none_8, assert_none_16, assert_none_32, assert_none_64, assert_none_128, assert_none_256
};
use crate::helper::if_test_this_function;

fn main() {
let fn_idx: u8 = witness::FUNCTION_INDEX;

let a_u1: Option<u1> = witness::FIRST_ARG_U1;
let b_u1: Option<u1> = witness::SECOND_ARG_U1;

let a_u8: Option<u8> = witness::FIRST_ARG_U8;
let b_u8: Option<u8> = witness::SECOND_ARG_U8;

Expand All @@ -23,18 +29,20 @@ fn main() {
let b_u256: Option<u256> = witness::SECOND_ARG_U256;

// Assert Eq
match if_test_this_function(0, fn_idx) { true => { assert_eq_8(unwrap(a_u8), unwrap(b_u8)); }, false => (), };
match if_test_this_function(1, fn_idx) { true => { assert_eq_16(unwrap(a_u16), unwrap(b_u16)); }, false => (), };
match if_test_this_function(2, fn_idx) { true => { assert_eq_32(unwrap(a_u32), unwrap(b_u32)); }, false => (), };
match if_test_this_function(3, fn_idx) { true => { assert_eq_64(unwrap(a_u64), unwrap(b_u64)); }, false => (), };
match if_test_this_function(4, fn_idx) { true => { assert_eq_128(unwrap(a_u128), unwrap(b_u128)); }, false => (), };
match if_test_this_function(5, fn_idx) { true => { assert_eq_256(unwrap(a_u256), unwrap(b_u256)); }, false => (), };
match if_test_this_function(0, fn_idx) { true => { assert_eq_1(unwrap(a_u1), unwrap(b_u1)); }, false => (), };
match if_test_this_function(1, fn_idx) { true => { assert_eq_8(unwrap(a_u8), unwrap(b_u8)); }, false => (), };
match if_test_this_function(2, fn_idx) { true => { assert_eq_16(unwrap(a_u16), unwrap(b_u16)); }, false => (), };
match if_test_this_function(3, fn_idx) { true => { assert_eq_32(unwrap(a_u32), unwrap(b_u32)); }, false => (), };
match if_test_this_function(4, fn_idx) { true => { assert_eq_64(unwrap(a_u64), unwrap(b_u64)); }, false => (), };
match if_test_this_function(5, fn_idx) { true => { assert_eq_128(unwrap(a_u128), unwrap(b_u128)); }, false => (), };
match if_test_this_function(6, fn_idx) { true => { assert_eq_256(unwrap(a_u256), unwrap(b_u256)); }, false => (), };

// Assert None
match if_test_this_function(6, fn_idx) { true => { assert_none_8(a_u8); }, false => (), };
match if_test_this_function(7, fn_idx) { true => { assert_none_16(a_u16); }, false => (), };
match if_test_this_function(8, fn_idx) { true => { assert_none_32(a_u32); }, false => (), };
match if_test_this_function(9, fn_idx) { true => { assert_none_64(a_u64); }, false => (), };
match if_test_this_function(10, fn_idx) { true => { assert_none_128(a_u128); }, false => (), };
match if_test_this_function(11, fn_idx) { true => { assert_none_256(a_u256); }, false => (), };
match if_test_this_function(7, fn_idx) { true => { assert_none_1(a_u1); }, false => (), };
match if_test_this_function(8, fn_idx) { true => { assert_none_8(a_u8); }, false => (), };
match if_test_this_function(9, fn_idx) { true => { assert_none_16(a_u16); }, false => (), };
match if_test_this_function(10, fn_idx) { true => { assert_none_32(a_u32); }, false => (), };
match if_test_this_function(11, fn_idx) { true => { assert_none_64(a_u64); }, false => (), };
match if_test_this_function(12, fn_idx) { true => { assert_none_128(a_u128); }, false => (), };
match if_test_this_function(13, fn_idx) { true => { assert_none_256(a_u256); }, false => (), };
}
12 changes: 11 additions & 1 deletion simf/lib/asserts.simf
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use crate::lib::u128::{eq_128};
use crate::lib::u128::eq_128;

/// Asserts that two `u1` are equal
pub fn assert_eq_1(a: u1, b: u1) {
assert!(jet::eq_1(a, b))
}

/// Asserts that two u8 are equal
pub fn assert_eq_8(a: u8, b: u8) {
Expand Down Expand Up @@ -30,6 +35,11 @@ pub fn assert_eq_256(a: u256, b: u256) {
assert!(jet::eq_256(a, b));
}

/// Asserts that provided `Option<u1>` value is a `None`
pub fn assert_none_1(val: Option<u1>) {
assert!(is_none::<u1>(val));
}

/// Asserts that provided u8 Option value is a None
pub fn assert_none_8(val: Option<u8>) {
assert!(is_none::<u8>(val));
Expand Down
109 changes: 109 additions & 0 deletions simf/lib/secp256k1/operations.simf
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use crate::lib::logical_operations::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.
pub fn ge_to_point(p: Ge) -> Point {
let (x, y): (Fe, Fe) = p;

match jet::fe_is_odd(y) {
true => (1, x),
false => (0, x),
}
}

/// Decompress a `Point` into a Jacobian point with `z = 1`.
pub fn point_to_gej(p: Point) -> Gej {
(unwrap(jet::decompress(p)), 1)
}

/// Convert the point into affine coordinates.
pub fn safe_gej_normalize(p: Gej) -> Ge {
unwrap(jet::gej_normalize(p))
}

/// Subtract two field elements.
pub fn fe_sub(a: Fe, b: Fe) -> Fe {
jet::fe_add(a, jet::fe_negate(b))
}

/// Subtract two scalars.
pub fn scalar_sub(a: Scalar, b: Scalar) -> Scalar {
jet::scalar_add(a, jet::scalar_negate(b))
}

/// Subtract two points.
pub fn gej_sub(p: Gej, q: Gej) -> Gej {
jet::gej_add(p, jet::gej_negate(q))
}

/// Field equality modulo `p`.
pub fn fe_eq(a: Fe, b: Fe) -> bool {
jet::fe_is_zero(fe_sub(a, b))
}

/// Scalar equality modulo `n`.
pub fn scalar_eq(a: Scalar, b: Scalar) -> bool {
jet::scalar_is_zero(scalar_sub(a, b))
}

/// Affine point equality.
pub fn ge_eq(a: Ge, b: Ge) -> bool {
let (ax, ay): (Fe, Fe) = a;
let (bx, by): (Fe, Fe) = b;
and(fe_eq(ax, bx), fe_eq(ay, by))
}

/// Check if two points are equal.
pub fn point_point_eq(a: Point, b: Point) -> bool {
let (ap, ax): (u1, u256) = a;
let (bp, bx): (u1, u256) = b;
and(jet::eq_256(ax, bx), jet::eq_1(ap, bp))
}

/// Check if two points represent the same point
pub fn gej_point_eq(a: Gej, p: Point) -> bool {
jet::gej_ge_equiv(a, unwrap(jet::decompress(p)))
}

/// Field equality modulo `p`.
pub fn assert_fe_eq(a: Fe, b: Fe) {
assert!(fe_eq(a, b));
}

/// Scalar equality modulo `n`.
pub fn assert_scalar_eq(a: Scalar, b: Scalar) {
assert!(scalar_eq(a, b));
}

/// Affine point equality.
pub fn assert_ge_eq(a: Ge, b: Ge) {
let (ax, ay): (Fe, Fe) = a;
let (bx, by): (Fe, Fe) = b;

assert_fe_eq(ax, bx);
assert_fe_eq(ay, by);
}

/// Check if two points are equal.
pub fn assert_point_eq(a: Point, b: Point) {
let (ap, ax): (u1, u256) = a;
let (bp, bx): (u1, u256) = b;

assert_eq_256(ax, bx);
assert_eq_1(ap, bp);
}

/// Check if two points represent the same point.
pub fn assert_gej_point_eq(a: Gej, p: Point) {
assert!(gej_point_eq(a, p));
}

/// Check if two points represent the same point.
pub fn assert_gej_eq(a: Gej, b: Gej) {
assert!(jet::gej_equiv(a, b));
}

/// Check if two points represent the same point.
pub fn assert_gej_ge_eq(a: Gej, b: Ge) {
assert!(jet::gej_ge_equiv(a, b));
}
5 changes: 5 additions & 0 deletions simf/lib/u64.simf
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,8 @@ pub fn gt_64(a: u64, b: u64) -> bool {
pub fn ge_64(a: u64, b: u64) -> bool {
jet::le_64(b, a)
}

/// Widen `u64` to a `u256` scalar.
pub fn u64_widen(val: u64) -> u256 {
<(u64, u64, u64, u64)>::into((0, 0, 0, val))
}
Loading