<commit_before>/*
 * RELIC is an Efficient LIbrary for Cryptography
 * Copyright (C) 2007-2011 RELIC Authors
 *
 * This file is part of RELIC. RELIC is legal property of its developers,
 * whose names are not listed here. Please refer to the COPYRIGHT file
 * for contact information.
 *
 * RELIC is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * RELIC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with RELIC. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @file
 *
 * Implementation of fixed point multiplication on binary elliptic curves.
 *
 * @version $Id$
 * @ingroup eb
 */

#include "string.h"

#include "relic_core.h"
#include "relic_eb.h"

/*============================================================================*/
/* Private definitions                                                        */
/*============================================================================*/

#if EB_FIX == LWNAF || !defined(STRIP)

#if defined(EB_KBLTZ)

/**
 * Multiplies a binary elliptic curve point by an integer using the w-TNAF
 * method.
 *
 * @param[out] r 				- the result.
 * @param[in] p					- the point to multiply.
 * @param[in] k					- the integer.
 * @param[in] w					- the window size.
 */
static void eb_mul_fix_kbltz(eb_t r, eb_t *table, bn_t k) {
	int len, i, n;
	signed char u, tnaf[FB_BITS + 8], *t;
	bn_t vm, s0, s1;

	bn_null(vm);
	bn_null(s0);
	bn_null(s1);

	TRY {
		bn_new(vm);
		bn_new(s0);
		bn_new(s1);

		/* Compute the w-TNAF representation of k. */
		if (eb_curve_opt_a() == OPT_ZERO) {
			u = -1;
		} else {
			u = 1;
		}

		eb_curve_get_vm(vm);
		eb_curve_get_s0(s0);
		eb_curve_get_s1(s1);
		/* Compute the w-TNAF representation of k. */
		bn_rec_tnaf(tnaf, &len, k, vm, s0, s1, u, FB_BITS, EB_DEPTH);

		t = tnaf + len - 1;
		eb_set_infty(r);
		for (i = len - 1; i >= 0; i--, t--) {
			eb_frb(r, r);

			n = *t;
			if (n > 0) {
				eb_add(r, r, table[n / 2]);
			}
			if (n < 0) {
				eb_sub(r, r, table[-n / 2]);
			}
		}
		/* Convert r to affine coordinates. */
		eb_norm(r, r);
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		bn_free(vm);
		bn_free(s0);
		bn_free(s1);
	}
}

#endif /* EB_KBLTZ */

#if defined(EB_ORDIN) || defined(EB_SUPER)

/**
 * Multiplies a binary elliptic curve point by an integer using the w-NAF
 * method.
 *
 * @param[out] r 				- the result.
 * @param[in] p					- the point to multiply.
 * @param[in] k					- the integer.
 */
static void eb_mul_fix_ordin(eb_t r, eb_t *table, bn_t k) {
	int len, i, n;
	signed char naf[FB_BITS + 1], *t;

	/* Compute the w-TNAF representation of k. */
	bn_rec_naf(naf, &len, k, EB_DEPTH);

	t = naf + len - 1;
	eb_set_infty(r);
	for (i = len - 1; i >= 0; i--, t--) {
		eb_dbl(r, r);

		n = *t;
		if (n > 0) {
			eb_add(r, r, table[n / 2]);
		}
		if (n < 0) {
			eb_sub(r, r, table[-n / 2]);
		}
	}
	/* Convert r to affine coordinates. */
	eb_norm(r, r);
}

#endif /* EB_ORDIN || EB_SUPER */
#endif /* EB_FIX == LWNAF */

/*============================================================================*/
/* Public definitions                                                         */
/*============================================================================*/

#if EB_FIX == BASIC || !defined(STRIP)

void eb_mul_pre_basic(eb_t *t, eb_t p) {
	bn_t n;

	bn_null(n);

	TRY {
		bn_new(n);

		eb_curve_get_ord(n);

		eb_copy(t[0], p);
		for (int i = 1; i < bn_bits(n); i++) {
			eb_dbl(t[i], t[i - 1]);
		}

		eb_norm_sim(t + 1, t + 1, bn_bits(n) - 1);
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		bn_free(n);
	}
}

void eb_mul_fix_basic(eb_t r, eb_t *t, bn_t k) {
	int i, l;

	l = bn_bits(k);

	eb_set_infty(r);

	for (i = 0; i < l; i++) {
		if (bn_test_bit(k, i)) {
			eb_add(r, r, t[i]);
		}
	}
	eb_norm(r, r);
}

#endif

#if EB_FIX == YAOWI || !defined(STRIP)

void eb_mul_pre_yaowi(eb_t *t, eb_t p) {
	int l;
	bn_t n;

	bn_null(n);

	TRY {
		bn_new(n);

		eb_curve_get_ord(n);
		l = bn_bits(n);
		l = ((l % EB_DEPTH) == 0 ? (l / EB_DEPTH) : (l / EB_DEPTH) + 1);

		eb_copy(t[0], p);
		for (int i = 1; i < l; i++) {
			eb_dbl(t[i], t[i - 1]);
			for (int j = 1; j < EB_DEPTH; j++) {
				eb_dbl(t[i], t[i]);
			}
		}

		eb_norm_sim(t + 1, t + 1, l - 1);
	} CATCH_ANY {
		THROW(ERR_CAUGHT);
	} FINALLY {
		bn_free(n);
	}
}

void eb_mul_fix_yaowi(eb_t r, eb_t *t, bn_t k) {
	int i, j, l;
	eb_t a;
	unsigned char win[FB_BITS + 1];

	eb_null(a);

	if (bn_is_zero(k)) {
		eb_set_infty(r);
		return;
	}

	TRY {
		eb_new(a);

		eb_set_infty(r);
		eb_set_infty(a);

		bn_rec_win(win, &l, k, EB_DEPTH);

		for (j = (1 << EB_DEPTH) - 1; j >= 1; j--) {
			for (i = 0; i < l; i++) {
				if (win[i] == j) {
					eb_add(a, a, t[i]);
				}
			}
			eb_add(r, r, a);
		}
		eb_norm(r, r);
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		eb_free(a);
	}
}

#endif

#if EB_FIX == NAFWI || !defined(STRIP)

void eb_mul_pre_nafwi(eb_t *t, eb_t p) {
	bn_t n;

	bn_null(n);

	TRY {
		int l;
		bn_new(n);

		eb_curve_get_ord(n);
		l = bn_bits(n) + 1;
		l = ((l % EB_DEPTH) == 0 ? (l / EB_DEPTH) : (l / EB_DEPTH) + 1);

		eb_copy(t[0], p);
		for (int i = 1; i < l; i++) {
			eb_dbl(t[i], t[i - 1]);
			for (int j = 1; j < EB_DEPTH; j++) {
				eb_dbl(t[i], t[i]);
			}
		}

		eb_norm_sim(t + 1, t + 1, l - 1);
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		bn_free(n);
	}
}

void eb_mul_fix_nafwi(eb_t r, eb_t *t, bn_t k) {
	int i, j, l, d, m;
	eb_t a;
	signed char naf[FB_BITS + 1];
	char w;

	eb_null(a);

	TRY {
		eb_new(a);

		eb_set_infty(r);
		eb_set_infty(a);

		bn_rec_naf(naf, &l, k, 2);

		d = ((l % EB_DEPTH) == 0 ? (l / EB_DEPTH) : (l / EB_DEPTH) + 1);

		for (i = 0; i < d; i++) {
			w = 0;
			for (j = EB_DEPTH - 1; j >= 0; j--) {
				if (i * EB_DEPTH + j < l) {
					w = (char)(w << 1);
					w = (char)(w + naf[i * EB_DEPTH + j]);
				}
			}
			naf[i] = w;
		}

		if (EB_DEPTH % 2 == 0) {
			m = ((1 << (EB_DEPTH + 1)) - 2) / 3;
		} else {
			m = ((1 << (EB_DEPTH + 1)) - 1) / 3;
		}

		for (j = m; j >= 1; j--) {
			for (i = 0; i < d; i++) {
				if (naf[i] == j) {
					eb_add(a, a, t[i]);
				}
				if (naf[i] == -j) {
					eb_sub(a, a, t[i]);
				}
			}
			eb_add(r, r, a);
		}
		eb_norm(r, r);
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		eb_free(a);
	}
}

#endif

#if EB_FIX == COMBS || !defined(STRIP)

void eb_mul_pre_combs(eb_t *t, eb_t p) {
	int i, j, l;
	bn_t ord;

	bn_null(ord);

	TRY {
		bn_new(ord);

		eb_curve_get_ord(ord);
		l = bn_bits(ord);
		l = ((l % EB_DEPTH) == 0 ? (l / EB_DEPTH) : (l / EB_DEPTH) + 1);

		eb_set_infty(t[0]);

		eb_copy(t[1], p);
		for (j = 1; j < EB_DEPTH; j++) {
			eb_dbl(t[1 << j], t[1 << (j - 1)]);
			for (i = 1; i < l; i++) {
				eb_dbl(t[1 << j], t[1 << j]);
			}
			for (i = 1; i < (1 << j); i++) {
				eb_add(t[(1 << j) + i], t[1 << j], t[i]);
			}
		}

		eb_norm_sim(t + 2, t + 2, EB_TABLE_COMBS - 2);
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		bn_free(ord);
	}
}

void eb_mul_fix_combs(eb_t r, eb_t *t, bn_t k) {
	int i, j, l, w, n, p0, p1;
	bn_t ord;

	bn_null(ord);

	TRY {
		bn_new(ord);

		eb_curve_get_ord(ord);
		l = bn_bits(ord);
		l = ((l % EB_DEPTH) == 0 ? (l / EB_DEPTH) : (l / EB_DEPTH) + 1);

		n = bn_bits(k);

		p0 = (EB_DEPTH) * l - 1;

		w = 0;<commit_msg>Fix case when STRIP=on EB_MIXED=on.<commit_after>/*
 * RELIC is an Efficient LIbrary for Cryptography
 * Copyright (C) 2007-2011 RELIC Authors
 *
 * This file is part of RELIC. RELIC is legal property of its developers,
 * whose names are not listed here. Please refer to the COPYRIGHT file
 * for contact information.
 *
 * RELIC is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * RELIC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with RELIC. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @file
 *
 * Implementation of fixed point multiplication on binary elliptic curves.
 *
 * @version $Id$
 * @ingroup eb
 */

#include "string.h"

#include "relic_core.h"
#include "relic_eb.h"

/*============================================================================*/
/* Private definitions                                                        */
/*============================================================================*/

#if EB_FIX == LWNAF || !defined(STRIP)

#if defined(EB_KBLTZ)

/**
 * Multiplies a binary elliptic curve point by an integer using the w-TNAF
 * method.
 *
 * @param[out] r 				- the result.
 * @param[in] p					- the point to multiply.
 * @param[in] k					- the integer.
 * @param[in] w					- the window size.
 */
static void eb_mul_fix_kbltz(eb_t r, eb_t *table, bn_t k) {
	int len, i, n;
	signed char u, tnaf[FB_BITS + 8], *t;
	bn_t vm, s0, s1;

	bn_null(vm);
	bn_null(s0);
	bn_null(s1);

	TRY {
		bn_new(vm);
		bn_new(s0);
		bn_new(s1);

		/* Compute the w-TNAF representation of k. */
		if (eb_curve_opt_a() == OPT_ZERO) {
			u = -1;
		} else {
			u = 1;
		}

		eb_curve_get_vm(vm);
		eb_curve_get_s0(s0);
		eb_curve_get_s1(s1);
		/* Compute the w-TNAF representation of k. */
		bn_rec_tnaf(tnaf, &len, k, vm, s0, s1, u, FB_BITS, EB_DEPTH);

		t = tnaf + len - 1;
		eb_set_infty(r);
		for (i = len - 1; i >= 0; i--, t--) {
			eb_frb(r, r);

			n = *t;
			if (n > 0) {
				eb_add(r, r, table[n / 2]);
			}
			if (n < 0) {
				eb_sub(r, r, table[-n / 2]);
			}
		}
		/* Convert r to affine coordinates. */
		eb_norm(r, r);
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		bn_free(vm);
		bn_free(s0);
		bn_free(s1);
	}
}

#endif /* EB_KBLTZ */

#if defined(EB_ORDIN) || defined(EB_SUPER)

/**
 * Multiplies a binary elliptic curve point by an integer using the w-NAF
 * method.
 *
 * @param[out] r 				- the result.
 * @param[in] p					- the point to multiply.
 * @param[in] k					- the integer.
 */
static void eb_mul_fix_ordin(eb_t r, eb_t *table, bn_t k) {
	int len, i, n;
	signed char naf[FB_BITS + 1], *t;

	/* Compute the w-TNAF representation of k. */
	bn_rec_naf(naf, &len, k, EB_DEPTH);

	t = naf + len - 1;
	eb_set_infty(r);
	for (i = len - 1; i >= 0; i--, t--) {
		eb_dbl(r, r);

		n = *t;
		if (n > 0) {
			eb_add(r, r, table[n / 2]);
		}
		if (n < 0) {
			eb_sub(r, r, table[-n / 2]);
		}
	}
	/* Convert r to affine coordinates. */
	eb_norm(r, r);
}

#endif /* EB_ORDIN || EB_SUPER */
#endif /* EB_FIX == LWNAF */

/*============================================================================*/
/* Public definitions                                                         */
/*============================================================================*/

#if EB_FIX == BASIC || !defined(STRIP)

void eb_mul_pre_basic(eb_t *t, eb_t p) {
	bn_t n;

	bn_null(n);

	TRY {
		bn_new(n);

		eb_curve_get_ord(n);

		eb_copy(t[0], p);
		for (int i = 1; i < bn_bits(n); i++) {
			eb_dbl(t[i], t[i - 1]);
		}

		eb_norm_sim(t + 1, t + 1, bn_bits(n) - 1);
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		bn_free(n);
	}
}

void eb_mul_fix_basic(eb_t r, eb_t *t, bn_t k) {
	int i, l;

	l = bn_bits(k);

	eb_set_infty(r);

	for (i = 0; i < l; i++) {
		if (bn_test_bit(k, i)) {
			eb_add(r, r, t[i]);
		}
	}
	eb_norm(r, r);
}

#endif

#if EB_FIX == YAOWI || !defined(STRIP)

void eb_mul_pre_yaowi(eb_t *t, eb_t p) {
	int l;
	bn_t n;

	bn_null(n);

	TRY {
		bn_new(n);

		eb_curve_get_ord(n);
		l = bn_bits(n);
		l = ((l % EB_DEPTH) == 0 ? (l / EB_DEPTH) : (l / EB_DEPTH) + 1);

		eb_copy(t[0], p);
		for (int i = 1; i < l; i++) {
			eb_dbl(t[i], t[i - 1]);
			for (int j = 1; j < EB_DEPTH; j++) {
				eb_dbl(t[i], t[i]);
			}
		}

		eb_norm_sim(t + 1, t + 1, l - 1);
	} CATCH_ANY {
		THROW(ERR_CAUGHT);
	} FINALLY {
		bn_free(n);
	}
}

void eb_mul_fix_yaowi(eb_t r, eb_t *t, bn_t k) {
	int i, j, l;
	eb_t a;
	unsigned char win[FB_BITS + 1];

	eb_null(a);

	if (bn_is_zero(k)) {
		eb_set_infty(r);
		return;
	}

	TRY {
		eb_new(a);

		eb_set_infty(r);
		eb_set_infty(a);

		bn_rec_win(win, &l, k, EB_DEPTH);

		for (j = (1 << EB_DEPTH) - 1; j >= 1; j--) {
			for (i = 0; i < l; i++) {
				if (win[i] == j) {
					eb_add(a, a, t[i]);
				}
			}
			eb_add(r, r, a);
		}
		eb_norm(r, r);
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		eb_free(a);
	}
}

#endif

#if EB_FIX == NAFWI || !defined(STRIP)

void eb_mul_pre_nafwi(eb_t *t, eb_t p) {
	bn_t n;

	bn_null(n);

	TRY {
		int l;
		bn_new(n);

		eb_curve_get_ord(n);
		l = bn_bits(n) + 1;
		l = ((l % EB_DEPTH) == 0 ? (l / EB_DEPTH) : (l / EB_DEPTH) + 1);

		eb_copy(t[0], p);
		for (int i = 1; i < l; i++) {
			eb_dbl(t[i], t[i - 1]);
			for (int j = 1; j < EB_DEPTH; j++) {
				eb_dbl(t[i], t[i]);
			}
		}

		eb_norm_sim(t + 1, t + 1, l - 1);
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		bn_free(n);
	}
}

void eb_mul_fix_nafwi(eb_t r, eb_t *t, bn_t k) {
	int i, j, l, d, m;
	eb_t a;
	signed char naf[FB_BITS + 1];
	char w;

	eb_null(a);

	TRY {
		eb_new(a);

		eb_set_infty(r);
		eb_set_infty(a);

		bn_rec_naf(naf, &l, k, 2);

		d = ((l % EB_DEPTH) == 0 ? (l / EB_DEPTH) : (l / EB_DEPTH) + 1);

		for (i = 0; i < d; i++) {
			w = 0;
			for (j = EB_DEPTH - 1; j >= 0; j--) {
				if (i * EB_DEPTH + j < l) {
					w = (char)(w << 1);
					w = (char)(w + naf[i * EB_DEPTH + j]);
				}
			}
			naf[i] = w;
		}

		if (EB_DEPTH % 2 == 0) {
			m = ((1 << (EB_DEPTH + 1)) - 2) / 3;
		} else {
			m = ((1 << (EB_DEPTH + 1)) - 1) / 3;
		}

		for (j = m; j >= 1; j--) {
			for (i = 0; i < d; i++) {
				if (naf[i] == j) {
					eb_add(a, a, t[i]);
				}
				if (naf[i] == -j) {
					eb_sub(a, a, t[i]);
				}
			}
			eb_add(r, r, a);
		}
		eb_norm(r, r);
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		eb_free(a);
	}
}

#endif

#if EB_FIX == COMBS || !defined(STRIP)

void eb_mul_pre_combs(eb_t *t, eb_t p) {
	int i, j, l;
	bn_t ord;

	bn_null(ord);

	TRY {
		bn_new(ord);

		eb_curve_get_ord(ord);
		l = bn_bits(ord);
		l = ((l % EB_DEPTH) == 0 ? (l / EB_DEPTH) : (l / EB_DEPTH) + 1);

		eb_set_infty(t[0]);

		eb_copy(t[1], p);
		for (j = 1; j < EB_DEPTH; j++) {
			eb_dbl(t[1 << j], t[1 << (j - 1)]);
			for (i = 1; i < l; i++) {
				eb_dbl(t[1 << j], t[1 << j]);
			}
#if defined(EB_MIXED)
			eb_norm(t[1 << j], t[1 << j]);
#endif
			for (i = 1; i < (1 << j); i++) {
				eb_add(t[(1 << j) + i], t[i], t[1 << j]);
			}
		}

		eb_norm_sim(t + 2, t + 2, EB_TABLE_COMBS - 2);
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		bn_free(ord);
	}
}

void eb_mul_fix_combs(eb_t r, eb_t *t, bn_t k) {
	int i, j, l, w, n, p0, p1;
	bn_t ord;

	bn_null(ord);

	TRY {
		bn_new(ord);

		eb_curve_get_ord(ord);
		l = bn_bits(ord);
		l = ((l % EB_DEPTH) == 0 ? (l / EB_DEPTH) : (l / EB_DEPTH) + 1);

		n = bn_bits(k);

		p0 = (EB_DEPTH) * l - 1;

		w = 0;
{'fraction_non_alphanumeric': 0.21675229238473046, 'fraction_numerical': 0.01560772582428302, 'mean_word_length': 2.5779432294090276, 'pattern_counts': {'":': 0, '<': 83, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4089374', 'n_tokens_mistral': 8045, 'n_tokens_neox': 7030, 'n_words': 1792}