int main() { write_montgomery(message, r2modm, modulus); montgomery(); read_r(); for (i = 0; i < SIZE; i++) { mont_message[i] = r[i]; } for (i = 0; i < SIZE; i++) { a[i] = rmodm[i]; } for (i = 0; i < 2; i++) { for (j = 0; j < 8; j++) { write_montgomery(a, a, modulus); montgomery(); read_r(); for (i = 0; i < SIZE; i++) { a[i] = r[i]; } if (get_bit_value(enc_exp[i], 7-j) == 1) { write_montgomery(a, mont_message, modulus); montgomery(); read_r(); for (i = 0; i < SIZE; i++) { a[i] = r[i]; } } } } write_montgomery(a, one, modulus); montgomery(); read_r(); for (i = 0; i < SIZE; i++) { a[i] = r[i]; } terminate(); return 0; }
/* * mg_scale - calculates x * P with montgomery formula. * * assumes: * out != P * P->z must be 1 */ static void mg_scale(struct mg *out, const struct mg *P, const uint8_t x[X25519_KEY_LEN]) { struct mg T; int8_t foo; int i, j; fld_set0(out->x, 1); fld_set0(out->z, 0); memcpy(&T, P, sizeof(struct mg)); for (i = X25519_KEY_LEN-1; i >= 0; i--) { foo = x[i]; for (j = 8; j > 0; j--, foo <<= 1) { ctmemswap(out, &T, sizeof(struct mg), foo >> 7); montgomery(out, &T, P); ctmemswap(out, &T, sizeof(struct mg), foo >> 7); } } }