/*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* 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
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Sun elects to use this software under the MPL license.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#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 {
}
}