1 /* $OpenBSD: math_ec2n.h,v 1.7 2004/05/23 18:17:56 hshoexer Exp $ */ 2 /* $EOM: math_ec2n.h,v 1.4 1999/04/17 23:20:37 niklas Exp $ */ 3 4 /* 5 * Copyright (c) 1998 Niels Provos. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 /* 29 * This code was written under funding by Ericsson Radio Systems. 30 */ 31 32 #ifndef _MATH_EC2N_H 33 #define _MATH_EC2N_H_ 34 35 /* Definitions for points on an elliptic curve */ 36 37 typedef struct { 38 int inf; /* Are we the point at infinity ? */ 39 b2n_t x, y; 40 } _ec2n_point; 41 42 typedef _ec2n_point *ec2np_ptr; 43 typedef _ec2n_point ec2np_t[1]; 44 45 #define EC2NP_SWAP(k,n) do \ 46 { \ 47 int _i_; \ 48 \ 49 _i_ = (k)->inf; \ 50 (k)->inf = (n)->inf; \ 51 (n)->inf = _i_; \ 52 B2N_SWAP ((k)->x, (n)->x); \ 53 B2N_SWAP ((k)->y, (n)->y); \ 54 } \ 55 while (0) 56 57 void ec2np_init(ec2np_ptr); 58 void ec2np_clear(ec2np_ptr); 59 int ec2np_set(ec2np_ptr, ec2np_ptr); 60 61 #define ec2np_set_x_ui(n, y) b2n_set_ui ((n)->x, y) 62 #define ec2np_set_y_ui(n, x) b2n_set_ui ((n)->y, x) 63 #define ec2np_set_x_str(n, y) b2n_set_str ((n)->x, y) 64 #define ec2np_set_y_str(n, x) b2n_set_str ((n)->y, x) 65 66 /* Definitions for the group to which the points to belong to. */ 67 68 typedef struct { 69 b2n_t a, b, p; 70 } _ec2n_group; 71 72 typedef _ec2n_group *ec2ng_ptr; 73 typedef _ec2n_group ec2ng_t[1]; 74 75 void ec2ng_init(ec2ng_ptr); 76 void ec2ng_clear(ec2ng_ptr); 77 int ec2ng_set(ec2ng_ptr, ec2ng_ptr); 78 79 #define ec2ng_set_a_ui(n, x) b2n_set_ui ((n)->a, x) 80 #define ec2ng_set_b_ui(n, x) b2n_set_ui ((n)->b, x) 81 #define ec2ng_set_p_ui(n, x) b2n_set_ui ((n)->p, x) 82 #define ec2ng_set_a_str(n, x) b2n_set_str ((n)->a, x) 83 #define ec2ng_set_b_str(n, x) b2n_set_str ((n)->b, x) 84 #define ec2ng_set_p_str(n, x) b2n_set_str ((n)->p, x) 85 86 /* Functions for computing on the elliptic group. */ 87 88 int ec2np_add(ec2np_ptr, ec2np_ptr, ec2np_ptr, ec2ng_ptr); 89 int ec2np_find_y(ec2np_ptr, ec2ng_ptr); 90 int ec2np_ison(ec2np_ptr, ec2ng_ptr); 91 int ec2np_mul(ec2np_ptr, ec2np_ptr, b2n_ptr, ec2ng_ptr); 92 int ec2np_right(b2n_ptr n, ec2np_ptr, ec2ng_ptr); 93 94 #endif /* _MATH_2N_H_ */ 95