Skip to content

Commit

Permalink
refactor: biguint take out param in all operations (#36)
Browse files Browse the repository at this point in the history
* refactor: biguint accept an *out in all operation instead of mutating

it also account for the differences in sizes

* refactor: biguint benchmarks adapt to new api

* refactor: math lib adapt to new biguint api

* refactor: digital-signature lib adapt to new biguint api

* fix: biguint header file
  • Loading branch information
MarcosNicolau authored Jan 31, 2025
1 parent 75fdf7b commit ffea907
Show file tree
Hide file tree
Showing 8 changed files with 356 additions and 362 deletions.
7 changes: 3 additions & 4 deletions libs/digital-signature/src/rsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ void rsa_gen_key_pair(RSAKeyPair *key_pair) {
biguint_random_prime(&q);

BigUint n = biguint_new_heap(key_size_in_bytes);
biguint_cpy(&n, p);
biguint_mul(&n, q);
biguint_mul(p, q, &n);

// Carmichael's totient (lambda) of n outputs the smallest integer m, such that for every integer coprime to n, it
// holds that:
Expand All @@ -47,8 +46,8 @@ void rsa_gen_key_pair(RSAKeyPair *key_pair) {
// https://en.wikipedia.org/wiki/Carmichael_function
BigUint one = biguint_new_heap(key_size_in_bytes);
biguint_one(&one);
biguint_sub(&p, one);
biguint_sub(&q, one);
biguint_sub(p, one, &p);
biguint_sub(q, one, &q);

BigUint lambda_n = biguint_new_heap(key_size_in_bytes);
biguint_lcm(p, q, &lambda_n);
Expand Down
25 changes: 9 additions & 16 deletions libs/math/src/arithmetics.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void biguint_lcm(BigUint a, BigUint b, BigUint *out) {

BigUint gcd = biguint_new_heap(out->size);
biguint_gcd(x, y, &gcd);
biguint_mul(&x, y);
biguint_mul(x, y, &x);
biguint_div(x, gcd, out);

biguint_free(&gcd, &x, &y);
Expand All @@ -40,8 +40,7 @@ void biguint_lcm(BigUint a, BigUint b, BigUint *out) {
// https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Modular_integers
int biguint_bezout_identity_mod_holds(BigUint a, BigUint t, BigUint m, BigUint gcd) {
BigUint at = biguint_new_heap(a.size);
biguint_cpy(&at, a);
biguint_mul(&at, t);
biguint_mul(a, t, &at);
biguint_mod(at, m, &at);
if (biguint_cmp(at, gcd) != 0) {
return 0;
Expand Down Expand Up @@ -78,22 +77,16 @@ void biguint_extended_euclidean_algorithm(BigUint a, BigUint b, ExtendedEuclidea
biguint_div(rp, ri, &quot);

// r = r_{i-1} - q_i * r_i
biguint_cpy(&qr, ri);
biguint_mul(&qr, quot);
biguint_cpy(&r, rp);
biguint_sub(&r, qr);
biguint_mul(ri, quot, &qr);
biguint_sub(rp, qr, &r);

// s = s_{i-1} - q_i * s_i
biguint_cpy(&qs, si);
biguint_mul(&qs, quot);
biguint_cpy(&s, sp);
biguint_sub(&s, qs);
biguint_mul(si, quot, &qs);
biguint_sub(sp, qs, &s);

// t = t_{i-1} - q_i * t_i
biguint_cpy(&qt, ti);
biguint_mul(&qt, quot);
biguint_cpy(&t, tp);
biguint_sub(&t, qt);
biguint_mul(ti, quot, &qt);
biguint_sub(tp, qt, &t);

// update values for next iteration
biguint_cpy(&rp, ri);
Expand Down Expand Up @@ -130,7 +123,7 @@ void biguint_inverse_mod(BigUint a, BigUint n, BigUint *out) {
biguint_zero(out);
} else {
if (alg.sk_sign == -1) {
biguint_add(&alg.sk, n);
biguint_add(alg.sk, n, &alg.sk);
}
biguint_cpy(out, alg.sk);
}
Expand Down
33 changes: 15 additions & 18 deletions libs/math/src/primes.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,12 @@ int biguint_is_prime_solovay_strassen(BigUint p) {
BigUint two = biguint_new_heap(p.size);
biguint_from_u64(2, &two);

BigUint exponent = biguint_new_heap(p.size);
biguint_cpy(&exponent, p);
biguint_sub(&exponent, one);
biguint_div(exponent, two, &exponent);

BigUint p_minus_one = biguint_new_heap(p.size);
biguint_cpy(&p_minus_one, p);
biguint_sub(&p_minus_one, one);
biguint_sub(p, one, &p_minus_one);

BigUint exponent = biguint_new_heap(p.size);
biguint_div(p_minus_one, two, &exponent);

BigUint a = biguint_new_heap(p.size);
BigUint rem = biguint_new_heap(p.size);
Expand All @@ -87,7 +85,7 @@ int biguint_is_prime_solovay_strassen(BigUint p) {
// since we check that gcd(a, p) == 1
// j = {-1,1} so we don't have to check if it == 0
int j = jacobi(a, p);
biguint_pow_mod(&a, exponent, p);
biguint_pow_mod(a, exponent, p, &a);
biguint_mod(a, p, &rem);

// (p - 1) mod p = -1 mod p
Expand Down Expand Up @@ -152,23 +150,22 @@ int jacobi(BigUint a, BigUint n) {

BigUint exponent = biguint_new_heap(a.size);
BigUint exponent_two = biguint_new_heap(a.size);
BigUint exp_result = biguint_new_heap(a.size);
BigUint next = biguint_new_heap(a.size);

int result;

if (biguint_is_even(a)) {
biguint_cpy(&exponent, n);
biguint_from_u64(2, &num);
biguint_pow(&exponent, num);
biguint_pow(exponent, num, &exponent);
biguint_from_u64(1, &num);
biguint_sub(&exponent, num);
biguint_sub(exponent, num, &exponent);
biguint_from_u64(8, &num);
biguint_div(exponent, num, &exp_result);
biguint_div(exponent, num, &exponent);

// (-1)^n = { - 1 if n is even, - -1 if n is odd }
int calc;
if (biguint_is_even(exp_result))
if (biguint_is_even(exponent))
calc = 1;
else
calc = -1;
Expand All @@ -179,18 +176,18 @@ int jacobi(BigUint a, BigUint n) {
result = calc * jacobi(next, n);
} else {
biguint_cpy(&exponent, a);
biguint_sub(&exponent, num);
biguint_sub(exponent, num, &exponent);

biguint_cpy(&exponent_two, n);
biguint_sub(&exponent_two, num);
biguint_sub(exponent_two, num, &exponent_two);

biguint_mul(&exponent, exponent_two);
biguint_mul(exponent, exponent_two, &exponent);
biguint_from_u64(4, &num);
biguint_div(exponent, num, &exp_result);
biguint_div(exponent, num, &exponent);

// (-1)^n = { - 1 if n is even, - -1 if n is odd }
int calc;
if (biguint_is_even(exp_result))
if (biguint_is_even(exponent))
calc = 1;
else
calc = -1;
Expand All @@ -200,7 +197,7 @@ int jacobi(BigUint a, BigUint n) {
result = calc * jacobi(next, a);
}

biguint_free(&exponent, &exponent_two, &exp_result, &next, &num);
biguint_free(&exponent, &exponent_two, &next, &num);

return result;
}
10 changes: 5 additions & 5 deletions libs/primitive-types/benchmarks/biguint.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ void benchmark_add() {
BigUint b = biguint_new(16);
biguint_random(&a);
biguint_random(&b);
biguint_add(&a, b);
biguint_add(a, b, &a);
}

void benchmark_sub() {
BigUint a = biguint_new(16);
BigUint b = biguint_new(16);
biguint_random(&a);
biguint_random(&b);
biguint_sub(&a, b);
biguint_sub(a, b, &a);
}

void benchmark_divmod() {
Expand All @@ -33,15 +33,15 @@ void benchmark_mul() {
BigUint b = biguint_new(16);
biguint_random(&a);
biguint_random(&b);
biguint_mul(&a, b);
biguint_mul(a, b, &a);
}

void benchmark_pow() {
BigUint a = biguint_new(16);
BigUint b = biguint_new(16);
biguint_random(&a);
biguint_random(&b);
biguint_pow(&a, b);
biguint_pow(a, b, &a);
}

void benchmark_pow_mod() {
Expand All @@ -51,7 +51,7 @@ void benchmark_pow_mod() {
biguint_random(&a);
biguint_random(&b);
biguint_random(&m);
biguint_pow_mod(&a, b, m);
biguint_pow_mod(a, b, m, &a);
}

int main() {
Expand Down
Loading

0 comments on commit ffea907

Please sign in to comment.