/*
* Use is subject to license terms.
*
* 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.
*
* This library 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 this library; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* *********************************************************************
*
* The Original Code is the elliptic curve math library.
*
* The Initial Developer of the Original Code is
* Sun Microsystems, Inc.
* Portions created by the Initial Developer are Copyright (C) 2003
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Stephen Fung <fungstep@hotmail.com> and
* Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories
*
*********************************************************************** */
#include "mpi.h"
#include "mp_gf2m.h"
#include "ecl-priv.h"
#include "mpi-priv.h"
#ifndef _KERNEL
#include <stdlib.h>
#endif
/* Allocate memory for a new GFMethod object. */
GFMethod *
{
#ifdef _KERNEL
#else
return NULL;
#endif
return NULL;
}
return meth;
}
/* Construct a generic GFMethod for arithmetic over prime fields with
* irreducible irr. */
GFMethod *
{
return NULL;
/* maybe we need 1 and 2 words here as well?*/
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
default:
}
return NULL;
}
return meth;
}
/* Construct a generic GFMethod for arithmetic over binary polynomial
* fields with irreducible irr that has array representation irr_arr (see
* ecl-priv.h for description of the representation). If irr_arr is NULL,
* then it is constructed from the bitstring representation. */
GFMethod *
{
int ret;
return NULL;
/* Irreducible polynomials are either trinomials or pentanomials. */
if (irr_arr[2] > 0) {
} else {
}
} else {
/* Irreducible polynomials are either trinomials or pentanomials. */
goto CLEANUP;
}
}
return NULL;
}
return meth;
}
/* Free the memory allocated (if any) to a GFMethod object. */
void
{
return;
return;
#ifdef _KERNEL
#else
#endif
}
/* Wrapper functions for generic prime field arithmetic. */
/* Add two field elements. Assumes that 0 <= a, b < meth->irr */
{
/* PRE: 0 <= a, b < p = meth->irr POST: 0 <= r < p, r = a + b (mod p) */
return res;
}
}
return res;
}
/* Negates a field element. Assumes that 0 <= a < meth->irr */
{
/* PRE: 0 <= a < p = meth->irr POST: 0 <= r < p, r = -a (mod p) */
if (mp_cmp_z(a) == 0) {
mp_zero(r);
return MP_OKAY;
}
}
/* Subtracts two field elements. Assumes that 0 <= a, b < meth->irr */
{
/* PRE: 0 <= a, b < p = meth->irr POST: 0 <= r < p, r = a - b (mod p) */
MP_CHECKOK(mp_sub(b, a, r));
if (mp_cmp_z(r) < 0) {
}
}
if (mp_cmp_z(r) < 0) {
}
return res;
}
/*
* Inline adds for small curve lengths.
*/
/* 3 words */
{
switch(MP_USED(a)) {
case 3:
case 2:
case 1:
}
switch(MP_USED(b)) {
case 3:
case 2:
case 1:
}
#ifndef MPI_AMD64_ADD
#else
__asm__ (
"xorq %3,%3 \n\t"
"addq %4,%0 \n\t"
"adcq %5,%1 \n\t"
"adcq %6,%2 \n\t"
"adcq $0,%3 \n\t"
: "%cc" );
#endif
MP_USED(r) = 3;
/* Do quick 'subract' if we've gone over
* (add the 2's complement of the curve field) */
#ifndef MPI_AMD64_ADD
#else
__asm__ (
"subq %3,%0 \n\t"
"sbbq %4,%1 \n\t"
"sbbq %5,%2 \n\t"
: "%cc" );
#endif
}
s_mp_clamp(r);
return res;
}
/* 4 words */
{
switch(MP_USED(a)) {
case 4:
case 3:
case 2:
case 1:
}
switch(MP_USED(b)) {
case 4:
case 3:
case 2:
case 1:
}
#ifndef MPI_AMD64_ADD
#else
__asm__ (
"xorq %4,%4 \n\t"
"addq %5,%0 \n\t"
"adcq %6,%1 \n\t"
"adcq %7,%2 \n\t"
"adcq %8,%3 \n\t"
"adcq $0,%4 \n\t"
: "%cc" );
#endif
MP_USED(r) = 4;
/* Do quick 'subract' if we've gone over
* (add the 2's complement of the curve field) */
#ifndef MPI_AMD64_ADD
#else
__asm__ (
"subq %4,%0 \n\t"
"sbbq %5,%1 \n\t"
"sbbq %6,%2 \n\t"
"sbbq %7,%3 \n\t"
: "%cc" );
#endif
}
s_mp_clamp(r);
return res;
}
/* 5 words */
{
switch(MP_USED(a)) {
case 5:
case 4:
case 3:
case 2:
case 1:
}
switch(MP_USED(b)) {
case 5:
case 4:
case 3:
case 2:
case 1:
}
MP_USED(r) = 5;
/* Do quick 'subract' if we've gone over
* (add the 2's complement of the curve field) */
}
s_mp_clamp(r);
return res;
}
/* 6 words */
{
switch(MP_USED(a)) {
case 6:
case 5:
case 4:
case 3:
case 2:
case 1:
}
switch(MP_USED(b)) {
case 6:
case 5:
case 4:
case 3:
case 2:
case 1:
}
MP_USED(r) = 6;
/* Do quick 'subract' if we've gone over
* (add the 2's complement of the curve field) */
}
s_mp_clamp(r);
return res;
}
/*
* The following subraction functions do in-line subractions based
* on our curve size.
*
* ... 3 words
*/
{
switch(MP_USED(a)) {
case 3:
case 2:
case 1:
}
switch(MP_USED(b)) {
case 3:
case 2:
case 1:
}
#ifndef MPI_AMD64_ADD
#else
__asm__ (
"xorq %3,%3 \n\t"
"subq %4,%0 \n\t"
"sbbq %5,%1 \n\t"
"sbbq %6,%2 \n\t"
"adcq $0,%3 \n\t"
: "%cc" );
#endif
/* Do quick 'add' if we've gone under 0
* (subtract the 2's complement of the curve field) */
if (borrow) {
#ifndef MPI_AMD64_ADD
#else
__asm__ (
"addq %3,%0 \n\t"
"adcq %4,%1 \n\t"
"adcq %5,%2 \n\t"
: "%cc" );
#endif
}
#ifdef MPI_AMD64_ADD
/* compiler fakeout? */
}
#endif
MP_USED(r) = 3;
s_mp_clamp(r);
return res;
}
/* 4 words */
{
switch(MP_USED(a)) {
case 4:
case 3:
case 2:
case 1:
}
switch(MP_USED(b)) {
case 4:
case 3:
case 2:
case 1:
}
#ifndef MPI_AMD64_ADD
#else
__asm__ (
"xorq %4,%4 \n\t"
"subq %5,%0 \n\t"
"sbbq %6,%1 \n\t"
"sbbq %7,%2 \n\t"
"sbbq %8,%3 \n\t"
"adcq $0,%4 \n\t"
: "%cc" );
#endif
/* Do quick 'add' if we've gone under 0
* (subtract the 2's complement of the curve field) */
if (borrow) {
#ifndef MPI_AMD64_ADD
#else
__asm__ (
"addq %4,%0 \n\t"
"adcq %5,%1 \n\t"
"adcq %6,%2 \n\t"
"adcq %7,%3 \n\t"
: "%cc" );
#endif
}
#ifdef MPI_AMD64_ADD
/* compiler fakeout? */
}
#endif
MP_USED(r) = 4;
s_mp_clamp(r);
return res;
}
/* 5 words */
{
switch(MP_USED(a)) {
case 5:
case 4:
case 3:
case 2:
case 1:
}
switch(MP_USED(b)) {
case 5:
case 4:
case 3:
case 2:
case 1:
}
/* Do quick 'add' if we've gone under 0
* (subtract the 2's complement of the curve field) */
if (borrow) {
}
MP_USED(r) = 5;
s_mp_clamp(r);
return res;
}
/* 6 words */
{
switch(MP_USED(a)) {
case 6:
case 5:
case 4:
case 3:
case 2:
case 1:
}
switch(MP_USED(b)) {
case 6:
case 5:
case 4:
case 3:
case 2:
case 1:
}
/* Do quick 'add' if we've gone under 0
* (subtract the 2's complement of the curve field) */
if (borrow) {
}
MP_USED(r) = 6;
s_mp_clamp(r);
return res;
}
/* Reduces an integer to a field element. */
{
}
/* Multiplies two field elements. */
{
}
/* Squares a field element. */
{
}
/* Divides two field elements. If a is NULL, then returns the inverse of
* b. */
{
mp_int t;
/* If a is NULL, then return the inverse of b, otherwise return a/b. */
if (a == NULL) {
} else {
/* MPI doesn't support divmod, so we implement it using invmod and
* mulmod. */
mp_clear(&t);
return res;
}
}
/* Wrapper functions for generic binary polynomial field arithmetic. */
/* Adds two field elements. */
{
return mp_badd(a, b, r);
}
/* Negates a field element. Note that for binary polynomial fields, the
* negation of a field element is the field element itself. */
{
if (a == r) {
return MP_OKAY;
} else {
return mp_copy(a, r);
}
}
/* Reduces a binary polynomial to a field element. */
{
}
/* Multiplies two field elements. */
{
}
/* Squares a field element. */
{
}
/* Divides two field elements. If a is NULL, then returns the inverse of
* b. */
{
mp_int t;
/* If a is NULL, then return the inverse of b, otherwise return a/b. */
if (a == NULL) {
/* The GF(2^m) portion of MPI doesn't support invmod, so we
* compute 1/b. */
mp_clear(&t);
return res;
} else {
}
}