#include "t.h" extern void sole_pass1_compose(blk256_t *, blk128_t *, blk128_t *); extern void sole_pass1_decompose(blk256_t *, blk128_t *, blk256_t *, blk128_t *, blk128_t *); extern void sole_pass2_regroup(blk256_t *, blk256_t *, blk128_t *, blk128_t *); extern void sole_pass2_rev_regroup(blk256_t *, blk128_t *, blk256_t *, blk128_t *); extern void sole_pass1_rev_decompose(blk256_t *, blk256_t *, blk128_t *, blk128_t *); extern void sole_pass1_rev_compose(blk256_t *, blk256_t *, blk256_t *); void sole_compose_5_7() { blk128_t x = {0, 5}; blk128_t y = {0, 7}; blk256_t r; sole_pass1_compose(&r, &x, &y); blk256_t expected; blk256_set(&expected, 0, 0, 5, 0, 12); assert_blk256_eq(expected, r); } void sole_compose_7_5() { blk128_t x = {0, 7}; blk128_t y = {0, 5}; blk256_t r; sole_pass1_compose(&r, &x, &y); blk256_t expected; blk256_set(&expected, 0, 0, 7, 0, 12); assert_blk256_eq(expected, r); } void sole_compose_u64max() { blk128_t x = {u64(~0), u64(~0)}; blk128_t y = {u64(~0), u64(~0)}; blk256_t r; sole_pass1_compose(&r, &x, &y); blk256_t expected; blk256_set(&expected, 1, 0, 0, u64(~0), u64(~0)-1); assert_blk256_eq(expected, r); //assert_true(r.mx == 1 && !r.x[3] && !r.x[2] && r.x[1] == u64(~0) && r.x[0] == u64(~0)-1); } /** * 2338328219631577172,6055780687925223521,6998720716286945640,7517645173256713248 */ void sole_compose_decompose_initial() { blk128_t x1 = {2338328219631577172, 6055780687925223521}; blk128_t x2 = {6998720716286945640, 7517645173256713248}; /* blk128_t x1 = {7517645173256713248, 6998720716286945640}; */ /* blk128_t x2 = {6055780687925223521, 2338328219631577172}; */ blk128_t i3 = {0, 0}; blk128_t b3i = {0, 0}; blk128_t r; blk256_t db, db2; blk256_zero(&db); blk256_zero(&db2); sole_pass1_compose(&db, &x1, &x2); sole_pass1_decompose(&db2, &r, &db, &b3i, &i3); if (r.hi == u64c(9337048935918522812) && r.lo == u64c(13573425861181936769)) assert_true(1); else { assert_fail("Expected values don't match"); blk128_print("r", r); blk256_print("q", db2); } } void sole_pass2_rev_regroup_1() { blk256_t w, q; blk256_set(&w, 0, 0, 11, 0, 38); blk128_t r; blk128_t i3 = {0, 3}; sole_pass2_rev_regroup(&q, &r, &w, &i3); // TODO assert result is as expected } void sole_rev_compose() { /** * Simple tests for the reversal of the first composition step */ blk256_t w, x, y; blk256_set(&w, 0, 0, 5, 0, 12); sole_pass1_rev_compose(&x, &y, &w); if (!x.mx && !x.x[3] && !x.x[2] && !x.x[1] && x.x[0] == 5 && !y.mx && !y.x[3] && !y.x[2] && !y.x[1] && y.x[0] == 7) { assert_true(1); } else { assert_true(0); assert_fail("Decomposing {0, [0, 5, 0, 12]} failed!"); blk256_print("w", w); blk256_print("x", x); blk256_print("y", y); } } void sole_rev_compose_overflow_1() { blk128_t x = { UINT64_MAX, UINT64_MAX }; blk128_t y = { UINT64_MAX, UINT64_MAX }; blk256_t c, cx, cy; sole_pass1_compose(&c, &x, &y); sole_pass1_rev_compose(&cx, &cy, &c); if (!cx.mx && !cx.x[3] && !cx.x[2] && cx.x[1] == UINT64_MAX && cx.x[0] == UINT64_MAX && !cy.mx && !cy.x[3] && !cy.x[2] && cy.x[1] == UINT64_MAX && cy.x[0] == UINT64_MAX) { assert_true(1); } else { assert_true(0); blk128_print(" x", x); blk128_print(" y", y); blk256_print(" c", c); blk256_print("cx", cx); blk256_print("cy", cy); } } void sole_rev_compose_overflow_2() { blk128_t x = { UINT64_MAX, UINT64_MAX - 5 }; blk128_t y = { UINT64_MAX, UINT64_MAX - 7 }; blk256_t c, cx, cy; sole_pass1_compose(&c, &x, &y); sole_pass1_rev_compose(&cx, &cy, &c); assert_true(!cx.mx && !cx.x[3] && !cx.x[2] && cx.x[1] == UINT64_MAX && cx.x[0] == UINT64_MAX - 5 && !cy.mx && !cy.x[3] && !cy.x[2] && cy.x[1] == UINT64_MAX && cy.x[0] == UINT64_MAX - 7); } void sole_rev_compose_overflow_3() { blk128_t x = { 5, 87 }; blk128_t y = { 3, 7 }; blk256_t c, cx, cy; sole_pass1_compose(&c, &x, &y); sole_pass1_rev_compose(&cx, &cy, &c); assert_true(!cx.mx && !cx.x[3] && !cx.x[2] && cx.x[1] == 5 && cx.x[0] == 87 && !cy.mx && !cy.x[3] && !cy.x[2] && cy.x[1] == 3 && cy.x[0] == 7); } void sole_rev_compose_overflow_4() { blk128_t x = { UINT64_MAX, UINT64_MAX - 5 }; blk128_t y = { UINT64_MAX, 0 }; blk256_t c, cx, cy; sole_pass1_compose(&c, &x, &y); sole_pass1_rev_compose(&cx, &cy, &c); if (!cx.mx && !cx.x[3] && !cx.x[2] && cx.x[1] == UINT64_MAX && cx.x[0] == UINT64_MAX - 5 && !cy.mx && !cy.x[3] && !cy.x[2] && cy.x[1] == UINT64_MAX && !cy.x[0]) { assert_true(1); } else { assert_true(0); blk128_print(" x", x); blk128_print(" y", y); blk256_print(" c", c); blk256_print("cx", cx); blk256_print("cy", cy); } } void sole_rev_compose_double_eof() { blk256_t c, cx, cy; blk256_set(&c, 1, 0, 2, 0, 0); sole_pass1_rev_compose(&cx, &cy, &c); if (!cx.mx && !cx.x[3] && cx.x[2] == 1 && !cx.x[1] && !cx.x[0] && !cy.mx && !cy.x[3] && cy.x[2] == 1 && !cy.x[1] && !cy.x[0]) { assert_true(1); } else { /* * Print information for debugging */ assert_fail("Double EOF decomposition failed"); blk256_print("cx", cx); blk256_print("cy", cy); } } void sole_rev_compose_single_eof() { blk256_t c, cx, cy; blk256_set(&c, 0, 5, 7, 5, 6); sole_pass1_rev_compose(&cx, &cy, &c); if (!cx.mx && !cx.x[3] && !cx.x[2] && cx.x[1] == 5 && cx.x[0] == 6 && !cy.mx && !cy.x[3] && cy.x[2] == 1 && !cy.x[1] && !cy.x[0]) { assert_true(1); } else { /* * Print information for debugging */ assert_fail("Double EOF decomposition failed"); blk256_print(" c", c); blk256_print("cx", cx); blk256_print("cy", cy); } } void sole_rev_compose_single_eof_u64max() { blk256_t c, cx, cy; blk256_set(&c, 1, 0, 0, UINT64_MAX, UINT64_MAX); sole_pass1_rev_compose(&cx, &cy, &c); if (!cx.mx && !cx.x[3] && !cx.x[2] && cx.x[1] == UINT64_MAX && cx.x[0] == UINT64_MAX && !cy.mx && !cy.x[3] && cy.x[2] == 1 && !cy.x[1] && !cy.x[0]) { assert_true(1); } else { /* * Print information for debugging */ assert_fail("Single EOF with UINT64_MAX decomposition failed"); blk256_print("cx", cx); blk256_print("cy", cy); } } void sole_rev_compose_zero_y() { /* * x = {4, 24} * y = 0 */ blk256_t c, cx, cy; blk256_set(&c, 0, 4, 24, 4, 24); sole_pass1_rev_compose(&cx, &cy, &c); if (!cx.mx && !cx.x[3] && !cx.x[2] && cx.x[1] == 4 && cx.x[0] == 24 && !cy.mx && !cy.x[3] && !cy.x[2] && !cy.x[1] && !cy.x[0]) { assert_true(1); } else { /* * Print information for debugging */ assert_true(0); blk256_print("cx", cx); blk256_print("cy", cy); } } void sole_rev_compose_zero_x() { /* * x = 0 * y = {4, 24} */ blk256_t c, cx, cy; blk256_set(&c, 0, 0, 0, 4, 24); sole_pass1_rev_compose(&cx, &cy, &c); if (!cx.mx && !cx.x[3] && !cx.x[2] && !cx.x[1] && !cx.x[0] && !cy.mx && !cy.x[3] && !cy.x[2] && cy.x[1] == 4 && cy.x[0] == 24) { assert_true(1); } else { assert_true(0); blk256_print("cx", cx); blk256_print("cy", cy); } } void sole_rev_compose_y_one() { /* * x = {23, 53} * y = 1 */ blk256_t c, cx, cy; blk256_set(&c, 0, 23, 53, 23, 54); sole_pass1_rev_compose(&cx, &cy, &c); if (!cx.mx && !cx.x[3] && !cx.x[2] && cx.x[1] == 23 && cx.x[0] == 53 && !cy.mx && !cy.x[3] && !cy.x[2] && !cy.x[1] && cy.x[0] == 1) { assert_true(1); } else { assert_true(0); blk256_print("cx", cx); blk256_print("cy", cy); } } void sole_rev_compose_tricky() { /* * x = {23, 53} * y = 1 */ blk128_t x = { 23, 53 }; blk128_t y = { 64, UINT64_MAX - 7 }; blk256_t c, cx, cy; sole_pass1_compose(&c, &x, &y); sole_pass1_rev_compose(&cx, &cy, &c); if (!cx.mx && !cx.x[3] && !cx.x[2] && cx.x[1] == 23 && cx.x[0] == 53 && !cy.mx && !cy.x[3] && !cy.x[2] && cy.x[1] == 64 && cy.x[0] == UINT64_MAX - 7) { assert_true(1); } else { assert_true(0); blk128_print(" x", x); blk128_print(" y", y); blk256_print(" c", c); blk256_print("cx", cx); blk256_print("cy", cy); } } /** * Encoding example: [5,7,2,4] * * Pass 1 encodes [0, 12, 0, 5, 0, 12] * [(0,12),(0,5),(0,12),(0,2),0,48,18446744073709551616,8] */ void sole_decompose_two_blocks_simple() { blk256_t b1, b2; blk256_set(&b1, 0, 0, 5, 0, 12); blk256_set(&b2, 0, 0, 2, 0, 6); blk128_t b3i = {0, 0}; blk128_t i3 = {0, 0}; blk256_t q; blk128_t r; sole_pass1_decompose(&q, &r, &b1, &b3i, &i3); if (!(!r.hi && r.lo == 12 && !q.mx && !q.x[3] && !q.x[2] && !q.x[1] && q.x[0] == 5)) { assert_fail("expected q = 5 and r = 12, but got:"); blk128_print("r", r); blk256_print("q", q); return; } /** * Manually set counters */ blk128_dec3(&b3i); blk128_inc3(&i3); sole_pass1_decompose(&q, &r, &b2, &b3i, &i3); if (!r.hi && r.lo == 12 && !q.mx && !q.x[3] && !q.x[2] && !q.x[1] && q.x[0] == 2) assert_true(1); else { assert_fail("expected q = 2 and r = 12, but got:"); blk128_print("r", r); blk256_print("q", q); } } /** * See sole_decompose_two_blocks_simple */ void sole_regroup_simple() { blk256_t q, w; blk256_set(&q, 0, 0, 0, 0, 5); blk128_t r = {0, 12}; blk128_t i3 = {0, 3}; // i = 1 sole_pass2_regroup(&w, &q, &r, &i3); if (!w.mx && !w.x[3] && w.x[2] == 12 && !w.x[1] && w.x[0] == 41) { assert_true(1); } else { assert_fail("expected w = {0, 12, 0, 41}, but got:"); blk256_print("w", w); } } void sole_rev_regroup_simple() { blk256_t w, q; blk256_set(&w, 0, 0, 12, 0, 41); blk128_t i3 = {0, 3}; blk128_t r; sole_pass2_rev_regroup(&q, &r, &w, &i3); if (!r.hi && r.lo == 12 && !q.mx && !q.x[3] && !q.x[2] && !q.x[1] && q.x[0] == 5) { assert_true(1); } else { assert_fail("expected r = {0, 12} and q = {0, [0, 0, 0, 5]}"); blk128_print("r", r); blk256_print("q", q); } } void sole_rev_regroup_1() { blk256_t op, q; blk256_set(&op, 0, u64c(17700521947704258072), u64c(11934908628227721528), 99661841615696542, 4967018425189284864); blk128_t r; blk128_t i3 = {0, 3}; sole_pass2_rev_regroup(&q, &r, &op, &i3); if (r.hi == u64c(17700521947704258072) && r.lo == u64c(11934908628227721525) && !q.mx && !q.x[3] && !q.x[2] && q.x[1] == u64c(2338328219631577172) && q.x[0] == u64c(6055780687925223521)) assert_true(1); else { assert_fail("sole_pass2_rev_regroup() failed!"); printf("Expected\n"); printf("r = {17700521947704258072, 11934908628227721525}\n"); printf("q = {2338328219631577172, 6055780687925223521}\n"); printf("But got:\n"); blk128_print("r", r); blk256_print("q", q); } } void sole_rev_pass1_decompose_1() { blk128_t r = {u64c(9337048935918522812), u64c(13573425861181936769)}; blk128_t i3 = {0, 3}; blk256_t q, x, e; blk256_set(&q, 0, 0, 0, 2338328219631577172, 6055780687925223521); blk256_set(&e, 0, 2338328219631577172,6055780687925223521,2322064277023791295,13852827871115817822); sole_pass1_rev_decompose(&x, &q, &r, &i3); assert_blk256_eq(e, x); } void sole_rev_pass1_compose_1() { blk256_t x, x1, x2, e1, e2; blk256_set(&x, 0, 2338328219631577172,6055780687925223521,2322064277023791295,13852827871115817822); blk256_set(&e1, 0, 0, 0, 2338328219631577172,6055780687925223520); blk256_set(&e2, 0, 0, 0, 16263942607785876,10649696890518957314); sole_pass1_rev_compose(&x1, &x2, &x); blk256_print("e1", e1); blk256_print("x1", x1); blk256_print("e2", e2); blk256_print("x2", x2); if (blk256_cmp(&e1, &x1) != 0) { assert_fail("x1 did not have expected value"); printf("Expected:\n"); blk256_print("e1", e1); blk256_print("e2", e2); printf("But got\n"); blk256_print("x1", x1); blk256_print("x2", x2); return; } assert_blk256_eq(e2, x2); } void sole_encode_extended_test() { /** * 7 blocks of input */ uint64_t input[14] = { 7956010481807946867, 2338615488418245744, 7874860801617168957, 4412750324381798228, 2308765963911831668, 6998711947288649773, 2340029477534396015, 7598807797348048999, 2338623232362770208, 2968197749036490859, 7161130725581808997, 7719297800551079968, 2338615488502986083, 2317439730694299764 }; /** * The seven blocks of input presented a little below should produce the * following nine output blocks: * */ uint64_t expected[18] = { 15830871283425115824ULL, 6751365812800043972ULL, 11575093333181722688ULL, 17146911512793096477ULL, 5787802333934011701ULL, 16885861879378431937ULL, 5084749278411648838ULL, 10049937970096964372ULL, 14370517560672173083ULL, 11958107546741781151ULL, 4939410811320309215ULL, 4727653233233446044ULL, 9899832386826449913ULL, 8623588700718402006ULL, 0, 0, 2338615488502986083ULL, 2317439730694299766ULL }; blk256_t db, q, res, e; blk128_t b1 = { input[0], input[1] }; blk128_t b2 = { input[2], input[3] }; blk128_t b3i = {0, 0}; blk128_t i3 = {0, 0}; blk128_t r; sole_pass1_compose(&db, &b1, &b2); sole_pass1_decompose(&q, &r, &db, &b3i, &i3); if (!(r.hi == expected[0] && r.lo == expected[1])) { assert_fail("First block encoding failed!\n"); printf("Expected:\nr = {%lu, %lu}\nBut got:", expected[0], expected[1]); blk128_print("r", r); return; } blk128_inc3(&i3); blk128_dec3(&b3i); /* Next two blocks */ blk128_set(&b1, input[4], input[5]); blk128_set(&b2, input[6], input[7]); sole_pass1_compose(&db, &b1, &b2); sole_pass1_decompose(&db, &r, &db, &b3i, &i3); sole_pass2_regroup(&res, &q, &r, &i3); /* Check double block ready for output */ blk256_set(&e, 0, expected[2], expected[3], expected[4], expected[5]); if (blk256_cmp(&e, &res) != 0) { assert_fail("Double block encoding failed!\n"); printf("Expected:\n"); blk256_print("expected", e); printf("But got:\n"); blk256_print("result ", res); return; } blk128_inc3(&i3); blk128_dec3(&b3i); blk256_cp(&q, &db); blk128_set(&b1, input[8], input[9]); blk128_set(&b2, input[10], input[11]); sole_pass1_compose(&db, &b1, &b2); sole_pass1_decompose(&db, &r, &db, &b3i, &i3); sole_pass2_regroup(&res, &q, &r, &i3); blk256_set(&e, 0, expected[6], expected[7], expected[8], expected[9]); if (blk256_cmp(&e, &res) != 0) { assert_fail("Double block encoding failed!\n"); printf("Expected:\n"); blk256_print("expected", e); printf("But got:\n"); blk256_print("result ", res); return; } assert_true(1); } void sole_tests() { test_fixture_start(); run_test(sole_compose_5_7); run_test(sole_compose_7_5); run_test(sole_compose_u64max); run_test(sole_compose_decompose_initial); run_test(sole_rev_compose); run_test(sole_rev_compose_overflow_1); run_test(sole_rev_compose_overflow_2); run_test(sole_rev_compose_overflow_3); run_test(sole_rev_compose_overflow_4); run_test(sole_rev_compose_double_eof); run_test(sole_rev_compose_single_eof); run_test(sole_rev_compose_single_eof_u64max); run_test(sole_rev_compose_zero_y); run_test(sole_rev_compose_zero_x); run_test(sole_rev_compose_y_one); run_test(sole_rev_compose_tricky); run_test(sole_decompose_two_blocks_simple); run_test(sole_regroup_simple); run_test(sole_rev_regroup_simple); run_test(sole_rev_regroup_1); run_test(sole_rev_pass1_decompose_1); run_test(sole_rev_pass1_compose_1); run_test(sole_encode_extended_test); test_fixture_end(); }