Exemplo n.º 1
0
void test_num_add_sub(void) {
    int r = secp256k1_rand32();
    secp256k1_num_t n1;
    secp256k1_num_t n2;
    random_num_order_test(&n1); /* n1 = R1 */
    if (r & 1) {
        random_num_negate(&n1);
    }
    random_num_order_test(&n2); /* n2 = R2 */
    if (r & 2) {
        random_num_negate(&n2);
    }
    secp256k1_num_t n1p2, n2p1, n1m2, n2m1;
    secp256k1_num_add(&n1p2, &n1, &n2); /* n1p2 = R1 + R2 */
    secp256k1_num_add(&n2p1, &n2, &n1); /* n2p1 = R2 + R1 */
    secp256k1_num_sub(&n1m2, &n1, &n2); /* n1m2 = R1 - R2 */
    secp256k1_num_sub(&n2m1, &n2, &n1); /* n2m1 = R2 - R1 */
    CHECK(secp256k1_num_eq(&n1p2, &n2p1));
    CHECK(!secp256k1_num_eq(&n1p2, &n1m2));
    secp256k1_num_negate(&n2m1); /* n2m1 = -R2 + R1 */
    CHECK(secp256k1_num_eq(&n2m1, &n1m2));
    CHECK(!secp256k1_num_eq(&n2m1, &n1));
    secp256k1_num_add(&n2m1, &n2m1, &n2); /* n2m1 = -R2 + R1 + R2 = R1 */
    CHECK(secp256k1_num_eq(&n2m1, &n1));
    CHECK(!secp256k1_num_eq(&n2p1, &n1));
    secp256k1_num_sub(&n2p1, &n2p1, &n2); /* n2p1 = R2 + R1 - R2 = R1 */
    CHECK(secp256k1_num_eq(&n2p1, &n1));
}
Exemplo n.º 2
0
void test_num_add_sub() {
    secp256k1_num_t n1;
    secp256k1_num_t n2;
    secp256k1_num_init(&n1);
    secp256k1_num_init(&n2);
    random_num_order_test(&n1); // n1 = R1
    random_num_negate(&n1);
    random_num_order_test(&n2); // n2 = R2
    random_num_negate(&n2);
    secp256k1_num_t n1p2, n2p1, n1m2, n2m1;
    secp256k1_num_init(&n1p2);
    secp256k1_num_init(&n2p1);
    secp256k1_num_init(&n1m2);
    secp256k1_num_init(&n2m1);
    secp256k1_num_add(&n1p2, &n1, &n2); // n1p2 = R1 + R2
    secp256k1_num_add(&n2p1, &n2, &n1); // n2p1 = R2 + R1
    secp256k1_num_sub(&n1m2, &n1, &n2); // n1m2 = R1 - R2
    secp256k1_num_sub(&n2m1, &n2, &n1); // n2m1 = R2 - R1
    assert(secp256k1_num_cmp(&n1p2, &n2p1) == 0);
    assert(secp256k1_num_cmp(&n1p2, &n1m2) != 0);
    secp256k1_num_negate(&n2m1); // n2m1 = -R2 + R1
    assert(secp256k1_num_cmp(&n2m1, &n1m2) == 0);
    assert(secp256k1_num_cmp(&n2m1, &n1) != 0);
    secp256k1_num_add(&n2m1, &n2m1, &n2); // n2m1 = -R2 + R1 + R2 = R1
    assert(secp256k1_num_cmp(&n2m1, &n1) == 0);
    assert(secp256k1_num_cmp(&n2p1, &n1) != 0);
    secp256k1_num_sub(&n2p1, &n2p1, &n2); // n2p1 = R2 + R1 - R2 = R1
    assert(secp256k1_num_cmp(&n2p1, &n1) == 0);
    secp256k1_num_free(&n2m1);
    secp256k1_num_free(&n1m2);
    secp256k1_num_free(&n2p1);
    secp256k1_num_free(&n1p2);
    secp256k1_num_free(&n2);
    secp256k1_num_free(&n1);
}
Exemplo n.º 3
0
void test_num_negate(void) {
    secp256k1_num_t n1;
    secp256k1_num_t n2;
    random_num_order_test(&n1); /* n1 = R */
    random_num_negate(&n1);
    secp256k1_num_copy(&n2, &n1); /* n2 = R */
    secp256k1_num_sub(&n1, &n2, &n1); /* n1 = n2-n1 = 0 */
    CHECK(secp256k1_num_is_zero(&n1));
    secp256k1_num_copy(&n1, &n2); /* n1 = R */
    secp256k1_num_negate(&n1); /* n1 = -R */
    CHECK(!secp256k1_num_is_zero(&n1));
    secp256k1_num_add(&n1, &n2, &n1); /* n1 = n2+n1 = 0 */
    CHECK(secp256k1_num_is_zero(&n1));
    secp256k1_num_copy(&n1, &n2); /* n1 = R */
    secp256k1_num_negate(&n1); /* n1 = -R */
    CHECK(secp256k1_num_is_neg(&n1) != secp256k1_num_is_neg(&n2));
    secp256k1_num_negate(&n1); /* n1 = R */
    CHECK(secp256k1_num_eq(&n1, &n2));
}
Exemplo n.º 4
0
void test_num_negate() {
    secp256k1_num_t n1;
    secp256k1_num_t n2;
    secp256k1_num_init(&n1);
    secp256k1_num_init(&n2);
    random_num_order_test(&n1); // n1 = R
    random_num_negate(&n1);
    secp256k1_num_copy(&n2, &n1); // n2 = R
    secp256k1_num_sub(&n1, &n2, &n1); // n1 = n2-n1 = 0
    assert(secp256k1_num_is_zero(&n1));
    secp256k1_num_copy(&n1, &n2); // n1 = R
    secp256k1_num_negate(&n1); // n1 = -R
    assert(!secp256k1_num_is_zero(&n1));
    secp256k1_num_add(&n1, &n2, &n1); // n1 = n2+n1 = 0
    assert(secp256k1_num_is_zero(&n1));
    secp256k1_num_copy(&n1, &n2); // n1 = R
    secp256k1_num_negate(&n1); // n1 = -R
    assert(secp256k1_num_is_neg(&n1) != secp256k1_num_is_neg(&n2));
    secp256k1_num_negate(&n1); // n1 = R
    assert(secp256k1_num_cmp(&n1, &n2) == 0);
    assert(secp256k1_num_is_neg(&n1) == secp256k1_num_is_neg(&n2));
    secp256k1_num_free(&n2);
    secp256k1_num_free(&n1);
}