diff options
author | Jack Lloyd <[email protected]> | 2018-04-16 07:18:34 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-04-16 07:18:34 -0400 |
commit | c620c61cf5e9379a074a1ff8fa9fa913c27dd5eb (patch) | |
tree | c44e88542cf3b57dd0a6d8a8e624f3096d689f1f /src/lib | |
parent | 173fb17e576a76a0a9f4d0fc5933ec2876ee638f (diff) |
Add vars to split the two Karatsuba sub-workspaces
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/math/mp/mp_karat.cpp | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/src/lib/math/mp/mp_karat.cpp b/src/lib/math/mp/mp_karat.cpp index 2ac03ac1e..220bd8f9e 100644 --- a/src/lib/math/mp/mp_karat.cpp +++ b/src/lib/math/mp/mp_karat.cpp @@ -101,6 +101,9 @@ void karatsuba_mul(word z[], const word x[], const word y[], size_t N, word* z0 = z; word* z1 = z + N; + word* ws0 = workspace; + word* ws1 = workspace + N; + clear_mem(workspace, 2*N); /* @@ -116,25 +119,25 @@ void karatsuba_mul(word z[], const word x[], const word y[], size_t N, const int32_t cmp0 = bigint_sub_abs(z0, x0, x1, N2); const int32_t cmp1 = bigint_sub_abs(z1, y1, y0, N2); - karatsuba_mul(workspace, z0, z1, N2, workspace+N); + karatsuba_mul(ws0, z0, z1, N2, ws1); const bool is_negative = cmp0 != cmp1; // Compute X_lo * Y_lo - karatsuba_mul(z0, x0, y0, N2, workspace+N); + karatsuba_mul(z0, x0, y0, N2, ws1); // Compute X_hi * Y_hi - karatsuba_mul(z1, x1, y1, N2, workspace+N); + karatsuba_mul(z1, x1, y1, N2, ws1); - const word ws_carry = bigint_add3_nc(workspace + N, z0, N, z1, N); - word z_carry = bigint_add2_nc(z + N2, N, workspace + N, N); + const word ws_carry = bigint_add3_nc(ws1, z0, N, z1, N); + word z_carry = bigint_add2_nc(z + N2, N, ws1, N); z_carry += bigint_add2_nc(z + N + N2, N2, &ws_carry, 1); bigint_add2_nc(z + N + N2, N2, &z_carry, 1); if(is_negative) - bigint_sub2(z + N2, 2*N-N2, workspace, N); + bigint_sub2(z + N2, 2*N-N2, ws0, N); else - bigint_add2_nc(z + N2, 2*N-N2, workspace, N); + bigint_add2_nc(z + N2, 2*N-N2, ws0, N); } /* @@ -161,27 +164,30 @@ void karatsuba_sqr(word z[], const word x[], size_t N, word workspace[]) word* z0 = z; word* z1 = z + N; + word* ws0 = workspace; + word* ws1 = workspace + N; + clear_mem(workspace, 2*N); // See comment in karatsuba_mul bigint_sub_abs(z0, x0, x1, N2); - karatsuba_sqr(workspace, z0, N2, workspace+N); + karatsuba_sqr(ws0, z0, N2, ws1); - karatsuba_sqr(z0, x0, N2, workspace+N); - karatsuba_sqr(z1, x1, N2, workspace+N); + karatsuba_sqr(z0, x0, N2, ws1); + karatsuba_sqr(z1, x1, N2, ws1); - const word ws_carry = bigint_add3_nc(workspace + N, z0, N, z1, N); - word z_carry = bigint_add2_nc(z + N2, N, workspace + N, N); + const word ws_carry = bigint_add3_nc(ws1, z0, N, z1, N); + word z_carry = bigint_add2_nc(z + N2, N, ws1, N); z_carry += bigint_add2_nc(z + N + N2, N2, &ws_carry, 1); bigint_add2_nc(z + N + N2, N2, &z_carry, 1); /* * This is only actually required if cmp (result of bigint_sub_abs) is != 0, - * however if cmp==0 then workspace[0:N] == 0 and avoiding the jump hides a + * however if cmp==0 then ws0[0:N] == 0 and avoiding the jump hides a * timing channel. */ - bigint_sub2(z + N2, 2*N-N2, workspace, N); + bigint_sub2(z + N2, 2*N-N2, ws0, N); } /* |