Add Swap feature

This commit is contained in:
TamtamHero
2020-06-29 15:43:02 +02:00
parent 2027fecffa
commit 9d4dfbe1bf
31 changed files with 650 additions and 312 deletions

View File

@@ -325,98 +325,40 @@ void mul128(uint128_t *number1, uint128_t *number2, uint128_t *target) {
add128(&tmp, &tmp2, target);
}
void write_u64_be(uint8_t *buffer, uint64_t value) {
buffer[0] = ((value >> 56) & 0xff);
buffer[1] = ((value >> 48) & 0xff);
buffer[2] = ((value >> 40) & 0xff);
buffer[3] = ((value >> 32) & 0xff);
buffer[4] = ((value >> 24) & 0xff);
buffer[5] = ((value >> 16) & 0xff);
buffer[6] = ((value >> 8) & 0xff);
buffer[7] = (value & 0xff);
}
void read_u64_be(uint8_t *in, uint64_t *out) {
uint8_t *out_ptr = (uint8_t*)out;
*out_ptr++ = in[7];
*out_ptr++ = in[6];
*out_ptr++ = in[5];
*out_ptr++ = in[4];
*out_ptr++ = in[3];
*out_ptr++ = in[2];
*out_ptr++ = in[1];
*out_ptr = in[0];
}
void mul256(uint256_t *number1, uint256_t *number2, uint256_t *target) {
uint128_t top[4];
uint128_t bottom[4];
uint128_t products[4][4];
uint128_t tmp, tmp2, fourth64, third64, second64, first64;
uint256_t target1, target2;
UPPER(top[0]) = 0;
LOWER(top[0]) = UPPER(UPPER_P(number1));
UPPER(top[1]) = 0;
LOWER(top[1]) = LOWER(UPPER_P(number1));
UPPER(top[2]) = 0;
LOWER(top[2]) = UPPER(LOWER_P(number1));
UPPER(top[3]) = 0;
LOWER(top[3]) = LOWER(LOWER_P(number1));
UPPER(bottom[0]) = 0;
LOWER(bottom[0]) = UPPER(UPPER_P(number2));
UPPER(bottom[1]) = 0;
LOWER(bottom[1]) = LOWER(UPPER_P(number2));
UPPER(bottom[2]) = 0;
LOWER(bottom[2]) = UPPER(LOWER_P(number2));
UPPER(bottom[3]) = 0;
LOWER(bottom[3]) = LOWER(LOWER_P(number2));
for (int y = 3; y > -1; y--) {
for (int x = 3; x > -1; x--) {
mul128(&top[x], &bottom[y], &products[3 - x][y]);
}
uint8_t num1[32], num2[32], result[64];
os_memset(&result, 0, sizeof(result));
for(uint8_t i = 0; i<4; i++){
write_u64_be(num1+i*sizeof(uint64_t), number1->elements[i/2].elements[i%2]);
write_u64_be(num2+i*sizeof(uint64_t), number2->elements[i/2].elements[i%2]);
}
cx_math_mult(result, num1, num2, sizeof(num1));
for(uint8_t i = 0; i<4; i++){
read_u64_be(result+32+i*sizeof(uint64_t), &target->elements[i/2].elements[i%2]);
}
UPPER(fourth64) = 0;
LOWER(fourth64) = LOWER(products[0][3]);
UPPER(tmp) = 0;
LOWER(tmp) = LOWER(products[0][2]);
UPPER(tmp2) = 0;
LOWER(tmp2) = UPPER(products[0][3]);
add128(&tmp, &tmp2, &third64);
UPPER(tmp) = 0;
LOWER(tmp) = LOWER(products[0][1]);
UPPER(tmp2) = 0;
LOWER(tmp2) = UPPER(products[0][2]);
add128(&tmp, &tmp2, &second64);
UPPER(tmp) = 0;
LOWER(tmp) = LOWER(products[0][0]);
UPPER(tmp2) = 0;
LOWER(tmp2) = UPPER(products[0][1]);
add128(&tmp, &tmp2, &first64);
UPPER(tmp) = 0;
LOWER(tmp) = LOWER(products[1][3]);
add128(&tmp, &third64, &tmp2);
copy128(&third64, &tmp2);
UPPER(tmp) = 0;
LOWER(tmp) = LOWER(products[1][2]);
add128(&tmp, &second64, &tmp2);
UPPER(tmp) = 0;
LOWER(tmp) = UPPER(products[1][3]);
add128(&tmp, &tmp2, &second64);
UPPER(tmp) = 0;
LOWER(tmp) = LOWER(products[1][1]);
add128(&tmp, &first64, &tmp2);
UPPER(tmp) = 0;
LOWER(tmp) = UPPER(products[1][2]);
add128(&tmp, &tmp2, &first64);
UPPER(tmp) = 0;
LOWER(tmp) = LOWER(products[2][3]);
add128(&tmp, &second64, &tmp2);
copy128(&second64, &tmp2);
UPPER(tmp) = 0;
LOWER(tmp) = LOWER(products[2][2]);
add128(&tmp, &first64, &tmp2);
UPPER(tmp) = 0;
LOWER(tmp) = UPPER(products[2][3]);
add128(&tmp, &tmp2, &first64);
UPPER(tmp) = 0;
LOWER(tmp) = LOWER(products[3][3]);
add128(&tmp, &first64, &tmp2);
copy128(&first64, &tmp2);
clear256(&target1);
shiftl128(&first64, 64, &UPPER(target1));
clear256(&target2);
UPPER(UPPER(target2)) = UPPER(third64);
shiftl128(&third64, 64, &LOWER(target2));
add256(&target1, &target2, target);
clear256(&target1);
copy128(&UPPER(target1), &second64);
add256(&target1, target, &target2);
clear256(&target1);
copy128(&LOWER(target1), &fourth64);
add256(&target1, &target2, target);
}
void divmod128(uint128_t *l, uint128_t *r, uint128_t *retDiv,