http://git-wip-us.apache.org/repos/asf/incubator-milagro-crypto-c/blob/8d28d2c3/test/test_fp2_arithmetics_YYY.c.in ---------------------------------------------------------------------- diff --git a/test/test_fp2_arithmetics_YYY.c.in b/test/test_fp2_arithmetics_YYY.c.in new file mode 100644 index 0000000..c21c39c --- /dev/null +++ b/test/test_fp2_arithmetics_YYY.c.in @@ -0,0 +1,453 @@ +/** + * @file test_fp2_arithmetics_YYY.c + * @author Alessandro Budroni + * @brief Test for aritmetics with FP2_YYY + * + * LICENSE + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include "arch.h" +#include "amcl.h" +#include "utils.h" +#include "fp2_YYY.h" + +#define LINE_LEN 10000 +#define MAX_STRING 300 + +void read_BIG_XXX(BIG_XXX A, char* string) +{ + int len; + char support[LINE_LEN]; + BIG_XXX_zero(A); + len = strlen(string)+1; + amcl_hex2bin(string,support,len); + len = (len-1)/2;; + BIG_XXX_fromBytesLen(A,support,len); + BIG_XXX_norm(A); +} + +void read_FP2_YYY(FP2_YYY *fp2, char* stringx) +{ + char *stringy, *end; + BIG_XXX x,y; + + stringy = strchr(++stringx,','); + if (stringy == NULL) + { + printf("ERROR unexpected test vector\n"); + exit(EXIT_FAILURE); + } + *stringy = '\0'; + + end = strchr(++stringy,']'); + if (end == NULL) + { + printf("ERROR unexpected test vector\n"); + exit(EXIT_FAILURE); + } + *end = '\0'; + + read_BIG_XXX(x,stringx); + read_BIG_XXX(y,stringy); + + FP2_YYY_from_BIGs(fp2,x,y); +} + +int main(int argc, char** argv) +{ + if (argc != 2) + { + printf("usage: ./test_fp2_arithmetics_YYY [path to test vector file]\n"); + exit(EXIT_FAILURE); + } + + int i = 0, len = 0, j = 0; + FILE *fp; + + char line[LINE_LEN]; + char * linePtr = NULL; + + BIG_XXX BIGaux1, BIGaux2; + FP_YYY FPaux1; + FP2_YYY FP2aux1, FP2aux2, FP2aux3, FP2aux4; + + FP2_YYY FP2_1; + const char* FP2_1line = "FP2_1 = "; + FP2_YYY FP2_2; + const char* FP2_2line = "FP2_2 = "; + FP2_YYY FP2add; + const char* FP2addline = "FP2add = "; + FP2_YYY FP2neg; + const char* FP2negline = "FP2neg = "; + FP2_YYY FP2sub; + const char* FP2subline = "FP2sub = "; + FP2_YYY FP2conj; + const char* FP2conjline = "FP2conj = "; + BIG_XXX BIGsc; + const char* BIGscline = "BIGsc = "; + FP2_YYY FP2pmul; + const char* FP2pmulline = "FP2pmul = "; + FP2_YYY FP2imul; + const char* FP2imulline = "FP2imul = "; + FP2_YYY FP2sqr; + const char* FP2sqrline = "FP2sqr = "; + FP2_YYY FP2mul; + const char* FP2mulline = "FP2mul = "; + FP2_YYY FP2inv; + const char* FP2invline = "FP2inv = "; + FP2_YYY FP2div2; + const char* FP2div2line = "FP2div2 = "; + FP2_YYY FP2_YYY_mulip; + const char* FP2_YYY_mulipline = "FP2_YYY_mulip = "; + FP2_YYY FP2_divip; + const char* FP2_divipline = "FP2_divip = "; + FP2_YYY FP2pow; + const char* FP2powline = "FP2pow = "; + +// Set to zero + FP2_YYY_zero(&FP2aux1); + FP2_YYY_zero(&FP2aux2); + +// Testing equal function and set zero function + if(!FP2_YYY_equals(&FP2aux1,&FP2aux2) || !FP2_YYY_iszilch(&FP2aux1) || !FP2_YYY_iszilch(&FP2aux2)) + { + printf("ERROR comparing FP2s or setting FP_YYY to zero FP\n"); + exit(EXIT_FAILURE); + } + +// Set to one + FP2_YYY_one(&FP2aux1); + FP2_YYY_one(&FP2aux2); + +// Testing equal function and set one function + if(!FP2_YYY_equals(&FP2aux1,&FP2aux2) || !FP2_YYY_isunity(&FP2aux1) || !FP2_YYY_isunity(&FP2aux2)) + { + printf("ERROR comparing FP2s or setting FP_YYY to unity FP\n"); + exit(EXIT_FAILURE); + } + + + fp = fopen(argv[1], "r"); + if (fp == NULL) + { + printf("ERROR opening test vector file\n"); + exit(EXIT_FAILURE); + } + + while (fgets(line, LINE_LEN, fp) != NULL) + { + i++; +// Read first FP_YYY and perform some tests + if (!strncmp(line,FP2_1line, strlen(FP2_1line))) + { + len = strlen(FP2_1line); + linePtr = line + len; + read_FP2_YYY(&FP2_1,linePtr); + FP2_YYY_cmove(&FP2aux1,&FP2_1,0); + if(FP2_YYY_equals(&FP2aux1,&FP2_1) != 0) + { + printf("ERROR in conditional copy of FP2, line %d\n",i); + exit(EXIT_FAILURE); + } + FP2_YYY_cmove(&FP2aux1,&FP2_1,1); + if(FP2_YYY_equals(&FP2aux1,&FP2_1) != 1) + { + printf("ERROR in conditional copy of FP2, line %d\n",i); + exit(EXIT_FAILURE); + } + FP2_YYY_from_FPs(&FP2aux1,&FP2_1.a,&FP2_1.b); + if(FP2_YYY_equals(&FP2aux1,&FP2_1) != 1) + { + printf("ERROR in generating FP_YYY from two FPs, line %d\n",i); + exit(EXIT_FAILURE); + } + FP_YYY_redc(BIGaux1,&FP2_1.a); + FP_YYY_redc(BIGaux2,&FP2_1.b); + FP2_YYY_from_BIGs(&FP2aux1,BIGaux1,BIGaux2); + FP_YYY_reduce(&FP2aux1.a); + FP_YYY_reduce(&FP2aux1.b); + if(FP2_YYY_equals(&FP2aux1,&FP2_1) != 1) + { + printf("\ncomputed "); + FP2_YYY_output(&FP2aux1); + printf("\nexpected "); + FP2_YYY_output(&FP2_1); + printf("\n"); + printf("ERROR in generating FP_YYY from two BIGs, line %d\n",i); + exit(EXIT_FAILURE); + } + FP2_YYY_from_FP(&FP2aux1,&FP2_1.a); + FP2_YYY_copy(&FP2aux2,&FP2_1); + BIG_XXX_zero(FP2aux2.b.g); + if(FP2_YYY_equals(&FP2aux1,&FP2aux2) != 1) + { + printf("ERROR in generating FP_YYY from one FP, line %d\n",i); + exit(EXIT_FAILURE); + } + FP_YYY_redc(BIGaux1,&FP2_1.a); + FP2_YYY_from_BIG(&FP2aux1,BIGaux1); + FP2_YYY_copy(&FP2aux2,&FP2_1); + BIG_XXX_zero(FP2aux2.b.g); + if(FP2_YYY_equals(&FP2aux1,&FP2aux2) != 1) + { + printf("ERROR in generating FP_YYY from one BIG, line %d\n",i); + exit(EXIT_FAILURE); + } + } +// Read second FP2 + if (!strncmp(line,FP2_2line, strlen(FP2_2line))) + { + len = strlen(FP2_2line); + linePtr = line + len; + read_FP2_YYY(&FP2_2,linePtr); + } +// Addition tests + if (!strncmp(line,FP2addline, strlen(FP2addline))) + { + len = strlen(FP2addline); + linePtr = line + len; + read_FP2_YYY(&FP2add,linePtr); + FP2_YYY_copy(&FP2aux1,&FP2_1); + FP2_YYY_copy(&FP2aux2,&FP2_2); + FP2_YYY_add(&FP2aux1,&FP2aux1,&FP2aux2); +// test commutativity P+Q = Q+P + FP2_YYY_copy(&FP2aux3,&FP2_1); + FP2_YYY_add(&FP2aux2,&FP2aux2,&FP2aux3); + if(!FP2_YYY_equals(&FP2aux1,&FP2add) || !FP2_YYY_equals(&FP2aux2,&FP2add)) + { + printf("FP2: "); + FP2_YYY_output(&FP2aux1); + printf("FP2: "); + FP2_YYY_output(&FP2aux2); + printf("\nExp: "); + FP2_YYY_output(&FP2add); + printf("ERROR adding two FP2, line %d\n",i); + exit(EXIT_FAILURE); + } +// test associativity (P+Q)+R = P+(Q+R) + FP2_YYY_copy(&FP2aux1,&FP2_1); + FP2_YYY_copy(&FP2aux3,&FP2_1); + FP2_YYY_copy(&FP2aux2,&FP2_2); + FP2_YYY_copy(&FP2aux4,&FP2add); + FP2_YYY_add(&FP2aux1,&FP2aux1,&FP2aux2); + FP2_YYY_add(&FP2aux1,&FP2aux1,&FP2aux4); + FP2_YYY_add(&FP2aux2,&FP2aux2,&FP2aux4); + FP2_YYY_add(&FP2aux2,&FP2aux2,&FP2aux3); + if(!FP2_YYY_equals(&FP2aux1,&FP2aux2)) + { + printf("ERROR testing associativity between three FP2s, line %d\n",i); + exit(EXIT_FAILURE); + } + } +// Negative an FP2 + if (!strncmp(line,FP2negline, strlen(FP2negline))) + { + len = strlen(FP2negline); + linePtr = line + len; + read_FP2_YYY(&FP2neg,linePtr); + FP2_YYY_copy(&FP2aux1,&FP2_1); + FP2_YYY_neg(&FP2aux1,&FP2aux1); + FP2_YYY_reduce(&FP2aux1); + FP2_YYY_norm(&FP2aux1); + if(!FP2_YYY_equals(&FP2aux1,&FP2neg)) + { + printf("FP2: "); + FP2_YYY_output(&FP2aux1); + printf("\nExp: "); + FP2_YYY_output(&FP2neg); + printf("ERROR in computing negative of FP2, line %d\n",i); + exit(EXIT_FAILURE); + } + } +// Subtraction test + if (!strncmp(line,FP2subline, strlen(FP2subline))) + { + len = strlen(FP2subline); + linePtr = line + len; + read_FP2_YYY(&FP2sub,linePtr); + FP2_YYY_copy(&FP2aux1,&FP2_1); + FP2_YYY_copy(&FP2aux2,&FP2_2); + FP2_YYY_sub(&FP2aux1,&FP2aux1,&FP2aux2); + if(FP2_YYY_equals(&FP2aux1,&FP2sub) == 0) + { + printf("ERROR subtraction between two FP2, line %d\n",i); + exit(EXIT_FAILURE); + } + } +// Compute conjugate + if (!strncmp(line,FP2conjline, strlen(FP2conjline))) + { + len = strlen(FP2conjline); + linePtr = line + len; + read_FP2_YYY(&FP2conj,linePtr); + FP2_YYY_copy(&FP2aux1,&FP2_1); + FP2_YYY_conj(&FP2aux1,&FP2aux1); + if(!FP2_YYY_equals(&FP2aux1,&FP2conj)) + { + printf("ERROR computing conjugate of FP2, line %d\n",i); + exit(EXIT_FAILURE); + } + } +// Read multiplicator + if (!strncmp(line,BIGscline, strlen(BIGscline))) + { + len = strlen(BIGscline); + linePtr = line + len; + read_BIG_XXX(BIGsc,linePtr); + } +// Multiplication by BIGsc + if (!strncmp(line,FP2pmulline, strlen(FP2pmulline))) + { + len = strlen(FP2pmulline); + linePtr = line + len; + read_FP2_YYY(&FP2pmul,linePtr); + FP_YYY_nres(&FPaux1,BIGsc); + FP2_YYY_pmul(&FP2aux1,&FP2_1,&FPaux1); + if(!FP2_YYY_equals(&FP2aux1,&FP2pmul)) + { + printf("ERROR in multiplication by BIG, line %d\n",i); + exit(EXIT_FAILURE); + } + } +// Raise FP_YYY by power BIGsc + if (!strncmp(line,FP2powline, strlen(FP2powline))) + { + len = strlen(FP2powline); + linePtr = line + len; + read_FP2_YYY(&FP2pow,linePtr); + FP2_YYY_pow(&FP2aux1,&FP2_1,BIGsc); + if(!FP2_YYY_equals(&FP2aux1,&FP2pow)) + { + printf("ERROR in raising FP_YYY by power BIG, line %d\n",i); + exit(EXIT_FAILURE); + } + } +// Multiplication by j = 1..10 + if (!strncmp(line,FP2imulline, strlen(FP2imulline))) + { + len = strlen(FP2imulline); + linePtr = line + len; + read_FP2_YYY(&FP2imul,linePtr); + FP2_YYY_imul(&FP2aux1,&FP2_1,j); + j++; + if(!FP2_YYY_equals(&FP2aux1,&FP2imul)) + { + printf("ERROR in multiplication by small integer, line %d\n",i); + exit(EXIT_FAILURE); + } + } +// Square and square root + if (!strncmp(line,FP2sqrline, strlen(FP2sqrline))) + { + len = strlen(FP2sqrline); + linePtr = line + len; + read_FP2_YYY(&FP2sqr,linePtr); + FP2_YYY_copy(&FP2aux1,&FP2_1); + FP2_YYY_sqr(&FP2aux1,&FP2aux1); + if(!FP2_YYY_equals(&FP2aux1,&FP2sqr)) + { + printf("ERROR in squaring FP2, line %d\n",i); + exit(EXIT_FAILURE); + } + FP2_YYY_sqrt(&FP2aux1,&FP2aux1); + FP2_YYY_neg(&FP2aux2,&FP2aux1); + if(!FP2_YYY_equals(&FP2aux1,&FP2_1) && !FP2_YYY_equals(&FP2aux2,&FP2_1)) + { + printf("ERROR square/square root consistency FP2, line %d\n",i); + exit(EXIT_FAILURE); + } + } +// Multiplication between two FP2s + if (!strncmp(line,FP2mulline, strlen(FP2mulline))) + { + len = strlen(FP2mulline); + linePtr = line + len; + read_FP2_YYY(&FP2mul,linePtr); + FP2_YYY_mul(&FP2aux1,&FP2_1,&FP2_2); + if(!FP2_YYY_equals(&FP2aux1,&FP2mul)) + { + printf("ERROR in multiplication between two FP2s, line %d\n",i); + exit(EXIT_FAILURE); + } + } +// Inverse + if (!strncmp(line,FP2invline, strlen(FP2invline))) + { + len = strlen(FP2invline); + linePtr = line + len; + read_FP2_YYY(&FP2inv,linePtr); + FP2_YYY_copy(&FP2aux1,&FP2_1); + FP2_YYY_inv(&FP2aux1,&FP2aux1); + if(!FP2_YYY_equals(&FP2aux1,&FP2inv)) + { + printf("ERROR in computing inverse of FP2, line %d\n",i); + exit(EXIT_FAILURE); + } + } +// Divide an FP_YYY by 2 + if (!strncmp(line,FP2div2line, strlen(FP2div2line))) + { + len = strlen(FP2div2line); + linePtr = line + len; + read_FP2_YYY(&FP2div2,linePtr); + FP2_YYY_div2(&FP2aux1,&FP2_1); + if(!FP2_YYY_equals(&FP2aux1,&FP2div2)) + { + printf("ERROR in computing division FP_YYY by 2, line %d\n",i); + exit(EXIT_FAILURE); + } + } +// Multiply an FP_YYY by (1+sqrt(-1)) + if (!strncmp(line,FP2_YYY_mulipline, strlen(FP2_YYY_mulipline))) + { + len = strlen(FP2_YYY_mulipline); + linePtr = line + len; + read_FP2_YYY(&FP2_YYY_mulip,linePtr); + FP2_YYY_copy(&FP2aux1,&FP2_1); + FP2_YYY_mul_ip(&FP2aux1); + if(!FP2_YYY_equals(&FP2aux1,&FP2_YYY_mulip)) + { + printf("ERROR in computing multiplication of FP_YYY by (1+sqrt(-1)), line %d\n",i); + exit(EXIT_FAILURE); + } + } +// Divide an FP_YYY by (1+sqrt(-1)) + if (!strncmp(line,FP2_divipline, strlen(FP2_divipline))) + { + len = strlen(FP2_divipline); + linePtr = line + len; + read_FP2_YYY(&FP2_divip,linePtr); + FP2_YYY_copy(&FP2aux1,&FP2_1); + FP2_YYY_div_ip(&FP2aux1); + if(!FP2_YYY_equals(&FP2aux1,&FP2_divip)) + { + printf("ERROR in computing division of FP_YYY by (1+sqrt(-1)), line %d\n",i); + exit(EXIT_FAILURE); + } + } + } + fclose(fp); + + printf("SUCCESS TEST ARITMETIC OF FP PASSED\n"); + exit(EXIT_SUCCESS); +}
http://git-wip-us.apache.org/repos/asf/incubator-milagro-crypto-c/blob/8d28d2c3/test/test_fp48_arithmetics_YYY.c.in ---------------------------------------------------------------------- diff --git a/test/test_fp48_arithmetics_YYY.c.in b/test/test_fp48_arithmetics_YYY.c.in new file mode 100644 index 0000000..94bbaab --- /dev/null +++ b/test/test_fp48_arithmetics_YYY.c.in @@ -0,0 +1,541 @@ +/** + * @file test_fp_arithmetics.c + * @author Samuele Andreoli + * @brief Test for aritmetics with FP + * + * LICENSE + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include "arch.h" +#include "amcl.h" +#include "utils.h" +#include "fp48_YYY.h" + +#define LINE_LEN 10000 + +/* + * Skips n closed brackets. + * Null terminates after the nth bracket and + * returns a pointer to the next char + */ +char* skip_cb(char* str, int n) +{ + int i; + char* next=str; + + for(i=0; i<n; i++) + { + next = strchr(++next,']'); + if (next == NULL) + { + printf("ERROR unexpected test vector\n"); + exit(EXIT_FAILURE); + } + } + *(++next) = '\0'; + + return next++; +} + +void read_BIG_XXX(BIG_XXX A, char* string) +{ + int len; + char support[LINE_LEN]; + BIG_XXX_zero(A); + len = strlen(string)+1; + amcl_hex2bin(string,support,len); + len = (len-1)/2; + BIG_XXX_fromBytesLen(A,support,len); + BIG_XXX_norm(A); +} + +void read_FP2_YYY(FP2_YYY *fp2, char* stringx) +{ + char *stringy; + BIG_XXX x,y; + + stringy = strchr(++stringx,','); + if (stringy == NULL) + { + printf("ERROR unexpected test vector\n"); + exit(EXIT_FAILURE); + } + *(stringy)=0; + skip_cb(stringy++,1); + + read_BIG_XXX(x,stringx); + read_BIG_XXX(y,stringy); + + FP2_YYY_from_BIGs(fp2,x,y); +} + +void read_FP4_YYY(FP4_YYY *fp4, char* stringx) +{ + char *stringy; + FP2_YYY x,y; + stringy = skip_cb(stringx++,1); + skip_cb(stringy++,1); + + read_FP2_YYY(&x,stringx); + read_FP2_YYY(&y,stringy); + + FP4_YYY_from_FP2s(fp4,&x,&y); +} + +void read_FP8_YYY(FP8_YYY *fp8, char* stringx) +{ + char *stringy; + FP4_YYY x,y; + stringy = skip_cb(stringx++,3); + skip_cb(stringy++,3); + + read_FP4_YYY(&x,stringx); + read_FP4_YYY(&y,stringy); + + FP8_YYY_from_FP4s(fp8,&x,&y); +} + +void read_FP16_YYY(FP16_YYY *fp16, char* stringx) +{ + char *stringy; + FP8_YYY x,y; + stringy = skip_cb(stringx++,7); + skip_cb(stringy++,7); + + read_FP8_YYY(&x,stringx); + read_FP8_YYY(&y,stringy); + + FP16_YYY_from_FP8s(fp16,&x,&y); +} + +void read_FP48_YYY(FP48_YYY *fp48, char *stringx) +{ + char *stringy, *stringz; + FP16_YYY x,y,z; + + stringy = skip_cb(stringx++,15); + stringz = skip_cb(stringy++,15); + skip_cb(stringz++,15); + + read_FP16_YYY(&x,stringx); + read_FP16_YYY(&y,stringy); + read_FP16_YYY(&z,stringz); + + FP48_YYY_from_FP16s(fp48,&x,&y,&z); +} + +int main(int argc, char** argv) +{ + + if (argc != 2) + { + printf("usage: ./test_fp48_arithmetics_ZZZ [path to test vector file]\n"); + exit(EXIT_FAILURE); + } + + int i = 0, j = 1, k, len = 0; + FILE *fp; + + char line[LINE_LEN]; + char * linePtr = NULL; + + BIG_XXX M, Fr_a, Fr_b; + FP2_YYY Frob; + FP16_YYY FP16aux1; + FP48_YYY FP48aux1, FP48aux2; + char octaux[48*MODBYTES_XXX]; + octet OCTaux = {0,sizeof(octaux),octaux}; + + FP48_YYY FP_48[3], FP_48_frobs[16]; + const char* FP48_lines[3] = + { + "FP48_1 = ", + "FP48_2 = ", + "FP48_c = " + }; + FP48_YYY FP_48_smul_y_dtype; + const char* FP48smul_y_dtypeline = "FP48smul_y_dtype = "; + FP48_YYY FP_48_smul_y_mtype; + const char* FP48smul_y_mtypeline = "FP48smul_y_mtype = "; + FP48_YYY FP48conj; + const char* FP48conjline = "FP48conj = "; + FP48_YYY FP48usquare; + const char* FP48usquareline = "FP48usquare = "; + FP48_YYY FP48square; + const char* FP48squareline = "FP48square = "; + FP48_YYY FP48mul; + const char* FP48mulline = "FP48mul = "; + FP48_YYY FP48smul_dtype; + const char* FP48smuldtypeline = "FP48smul_dtype = "; + FP48_YYY FP48smul_mtype; + const char* FP48smulmtypeline = "FP48smul_mtype = "; + FP48_YYY FP48inv; + const char* FP48invline = "FP48inv = "; + BIG_XXX BIGsc[18]; + const char* BIGsclines[18] = + { + "BIGsc1 = ", + "BIGsc2 = ", + "BIGsc3 = ", + "BIGsc4 = ", + "BIGsc5 = ", + "BIGsc6 = ", + "BIGsc7 = ", + "BIGsc8 = ", + "BIGsc9 = ", + "BIGsc10 = ", + "BIGsc11 = ", + "BIGsc12 = ", + "BIGsc13 = ", + "BIGsc14 = ", + "BIGsc15 = ", + "BIGsc16 = ", + "BIGscs = ", + "BIGsco = " + }; + FP48_YYY FP48pow; + const char* FP48powline = "FP48pow = "; + FP48_YYY FP48pinpow; + const char* FP48pinpowline = "FP48pinpow = "; + FP16_YYY FP48compows; + const char* FP48compowsline = "FP48compows = "; + FP16_YYY FP48compow; + const char* FP48compowline = "FP48compow = "; + FP48_YYY FP48pow16; + const char* FP48pow16line = "FP48pow16 = "; + FP48_YYY FP48frob; + const char* FP48frobline = "FP48frob = "; + FP16_YYY FP16trace; + const char* FP16traceline = "FP16trace = "; + + BIG_XXX_rcopy(M,Modulus_YYY); + BIG_XXX_rcopy(Fr_a,Fra_YYY); + BIG_XXX_rcopy(Fr_b,Frb_YYY); + FP2_YYY_from_BIGs(&Frob,Fr_a,Fr_b); + + // Set to one + FP48_YYY_one(&FP48aux1); + FP48_YYY_copy(&FP48aux2,&FP48aux1); + + // Testing equal function + if(!FP48_YYY_equals(&FP48aux1,&FP48aux2)) + { + printf("ERROR comparing equal FP48s or copying FP48\n"); + exit(EXIT_FAILURE); + } + FP16_YYY_zero(&FP16aux1); + FP48_YYY_from_FP16(&FP48aux1,&FP16aux1); + if(FP48_YYY_equals(&FP48aux1,&FP48aux2)) + { + printf("ERROR comparing different FP48s\n"); + exit(EXIT_FAILURE); + } + + if(!FP48_YYY_iszilch(&FP48aux1) || FP48_YYY_iszilch(&FP48aux2) || FP48_YYY_isunity(&FP48aux1) || !FP48_YYY_isunity(&FP48aux2)) + { + printf("ERROR checking iszilch/isunity functions\n"); + exit(EXIT_FAILURE); + } + + fp = fopen(argv[1], "r"); + if (fp == NULL) + { + printf("ERROR opening test vector file\n"); + exit(EXIT_FAILURE); + } + + while (fgets(line, LINE_LEN, fp) != NULL) + { + i++; + // Read first FP48 and perform some tests on it + if (!strncmp(line,FP48_lines[0], strlen(FP48_lines[0]))) + { + len = strlen(FP48_lines[0]); + linePtr = line + len; + read_FP48_YYY(FP_48,linePtr); + + // Test setting functions + FP48_YYY_from_FP16(&FP48aux1,&FP_48->a); + if(!FP16_YYY_equals(&FP48aux1.a,&FP_48->a) || !FP16_YYY_iszilch(&FP48aux1.b) || !FP16_YYY_iszilch(&FP48aux1.c)) + { + printf("ERROR setting FP48 from a FP16, line %d\n",i); + exit(EXIT_FAILURE); + } + FP48_YYY_from_FP16s(&FP48aux1,&FP_48->a,&FP_48->b,&FP_48->c); + if(!FP48_YYY_equals(&FP48aux1,FP_48)) + { + printf("ERROR setting FP48 from three FP16s, line %d\n",i); + exit(EXIT_FAILURE); + } + + // Test octet conversion consistency + FP48_YYY_toOctet(&OCTaux,FP_48); + FP48_YYY_fromOctet(&FP48aux1,&OCTaux); + if(!FP48_YYY_equals(&FP48aux1,FP_48)) + { + printf("ERROR octet conversion consistency, line %d\n",i); + exit(EXIT_FAILURE); + } + FP48_YYY_copy(FP_48_frobs,FP_48); + for (k=1; k<16; k++) + { + FP48_YYY_copy(FP_48_frobs+k,FP_48_frobs+k-1); + FP48_YYY_frob(FP_48_frobs+k,&Frob,1); + } + } + // Read other FP48s. + for(k = 1; k<3; k++) + { + if (!strncmp(line,FP48_lines[k], strlen(FP48_lines[k]))) + { + len = strlen(FP48_lines[k]); + linePtr = line + len; + read_FP48_YYY(FP_48+k,linePtr); + } + } + // Read y for M-TYPE smul test + if (!strncmp(line,FP48smul_y_mtypeline, strlen(FP48smul_y_mtypeline))) + { + len = strlen(FP48smul_y_mtypeline); + linePtr = line + len; + read_FP48_YYY(&FP_48_smul_y_mtype,linePtr); + } + // Read y for D-TYPE smul test + if (!strncmp(line,FP48smul_y_dtypeline, strlen(FP48smul_y_dtypeline))) + { + len = strlen(FP48smul_y_dtypeline); + linePtr = line + len; + read_FP48_YYY(&FP_48_smul_y_dtype,linePtr); + } + // Test FP48_YYY_conj + if (!strncmp(line,FP48conjline, strlen(FP48conjline))) + { + len = strlen(FP48conjline); + linePtr = line + len; + read_FP48_YYY(&FP48conj,linePtr); + FP48_YYY_conj(&FP48aux1,FP_48); + if(!FP48_YYY_equals(&FP48aux1,&FP48conj)) + { + printf("ERROR computing conjugate of FP48, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Test multiplication and commutativity + if (!strncmp(line,FP48mulline, strlen(FP48mulline))) + { + len = strlen(FP48mulline); + linePtr = line + len; + read_FP48_YYY(&FP48mul,linePtr); + FP48_YYY_copy(&FP48aux1,FP_48); + FP48_YYY_mul(&FP48aux1,FP_48+1); + FP48_YYY_copy(&FP48aux2,FP_48+1); + FP48_YYY_mul(&FP48aux2,FP_48); + if(!FP48_YYY_equals(&FP48aux1,&FP48mul) || !FP48_YYY_equals(&FP48aux2,&FP48mul)) + { + printf("ERROR computing multiplication of two FP48s, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Test squaring + if (!strncmp(line,FP48squareline, strlen(FP48squareline))) + { + len = strlen(FP48squareline); + linePtr = line + len; + read_FP48_YYY(&FP48square,linePtr); + FP48_YYY_sqr(&FP48aux1,FP_48); + if(!FP48_YYY_equals(&FP48aux1,&FP48square)) + { + printf("ERROR computing square of FP48, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Test usquaring + if (!strncmp(line,FP48usquareline, strlen(FP48usquareline))) + { + len = strlen(FP48usquareline); + linePtr = line + len; + read_FP48_YYY(&FP48usquare,linePtr); + FP48_YYY_usqr(&FP48aux1,FP_48); + if(!FP48_YYY_equals(&FP48aux1,&FP48usquare)) + { + printf("ERROR computing usquare of FP48, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Test s-multiplication for D-TYPE + if (!strncmp(line,FP48smuldtypeline, strlen(FP48smuldtypeline))) + { + len = strlen(FP48smuldtypeline); + linePtr = line + len; + read_FP48_YYY(&FP48smul_dtype,linePtr); + FP48_YYY_copy(&FP48aux1,FP_48); + FP48_YYY_smul(&FP48aux1,&FP_48_smul_y_dtype,D_TYPE); + if(!FP48_YYY_equals(&FP48aux1,&FP48smul_dtype)) + { + printf("ERROR computing s-multiplication for D-TYPE, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Test s-multiplication for M-TYPE + if (!strncmp(line,FP48smulmtypeline, strlen(FP48smulmtypeline))) + { + len = strlen(FP48smulmtypeline); + linePtr = line + len; + read_FP48_YYY(&FP48smul_mtype,linePtr); + FP48_YYY_copy(&FP48aux1,FP_48); + FP48_YYY_smul(&FP48aux1,&FP_48_smul_y_mtype,M_TYPE); + if(!FP48_YYY_equals(&FP48aux1,&FP48smul_mtype)) + { + printf("ERROR computing s-multiplication for M-TYPE, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Test inverse fuction + if (!strncmp(line,FP48invline, strlen(FP48invline))) + { + len = strlen(FP48invline); + linePtr = line + len; + read_FP48_YYY(&FP48inv,linePtr); + FP48_YYY_inv(&FP48aux1,FP_48); + if(!FP48_YYY_equals(&FP48aux1,&FP48inv)) + { + printf("ERROR computing inverse of a FP48, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Read BIGS + for(k=0; k<18; k++) + { + if (!strncmp(line,BIGsclines[k], strlen(BIGsclines[k]))) + { + len = strlen(BIGsclines[k]); + linePtr = line + len; + read_BIG_XXX(BIGsc[k],linePtr); + } + } + // Test power by a BIG + if (!strncmp(line,FP48powline, strlen(FP48powline))) + { + len = strlen(FP48powline); + linePtr = line + len; + read_FP48_YYY(&FP48pow,linePtr); + FP48_YYY_pow(&FP48aux1,FP_48,BIGsc[0]); + if(!FP48_YYY_equals(&FP48aux1,&FP48pow)) + { + printf("ERROR computing power of a FP48 by a BIG, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Test power by a small integer + if (!strncmp(line,FP48pinpowline, strlen(FP48pinpowline))) + { + len = strlen(FP48pinpowline); + linePtr = line + len; + read_FP48_YYY(&FP48pinpow,linePtr); + FP48_YYY_copy(&FP48aux1,FP_48); + FP48_YYY_pinpow(&FP48aux1,j,10); + if(!FP48_YYY_equals(&FP48aux1,&FP48pinpow)) + { + printf("ERROR computing power of a FP48 by a small integer, line %d\n",i); + exit(EXIT_FAILURE); + } + j++; + } + // Test fucntion FP48_YYY_compow with small integer [< Modulus mod Curve_Order] + if (!strncmp(line,FP48compowsline, strlen(FP48compowsline))) + { + len = strlen(FP48compowsline); + linePtr = line + len; + read_FP16_YYY(&FP48compows,linePtr); + FP48_YYY_copy(&FP48aux1,FP_48+2); + FP48_YYY_compow(&FP16aux1,&FP48aux1,BIGsc[16],BIGsc[17]); + if(!FP16_YYY_equals(&FP16aux1,&FP48compows)) + { + printf("ERROR testing function FP48_compow with small integer, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Test fucntion FP48_YYY_compow with big integer [> Modulus mod Curve_Order] + if (!strncmp(line,FP48compowline, strlen(FP48compowline))) + { + len = strlen(FP48compowline); + linePtr = line + len; + read_FP16_YYY(&FP48compow,linePtr); + FP48_YYY_copy(&FP48aux1,FP_48+2); + FP48_YYY_compow(&FP16aux1,&FP48aux1,BIGsc[0],BIGsc[17]); + if(!FP16_YYY_equals(&FP16aux1,&FP48compow)) + { + printf("ERROR testing function FP48_compow with big integer, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Test function FP48_YYY_pow16 + if (!strncmp(line,FP48pow16line, strlen(FP48pow16line))) + { + len = strlen(FP48pow16line); + linePtr = line + len; + read_FP48_YYY(&FP48pow16,linePtr); + FP48_YYY_pow16(&FP48aux1,FP_48_frobs,BIGsc); + + if(!FP48_YYY_equals(&FP48aux1,&FP48pow16)) + { + printf("ERROR testing function FP48pow16, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Raises an FP48 to the power of the internal modulus p, using the Frobenius constant f + if (!strncmp(line,FP48frobline, strlen(FP48frobline))) + { + len = strlen(FP48frobline); + linePtr = line + len; + read_FP48_YYY(&FP48frob,linePtr); + FP48_YYY_copy(&FP48aux1,FP_48); + FP48_YYY_frob(&FP48aux1,&Frob,1); + if(!FP48_YYY_equals(&FP48aux1,&FP48frob) || !FP48_YYY_equals(FP_48_frobs+1,&FP48frob)) + { + printf("ERROR in raising FP48 by an internal modulus p, using the Frobenius constant f, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Test computing trace of FP48 + if (!strncmp(line,FP16traceline, strlen(FP16traceline))) + { + len = strlen(FP16traceline); + linePtr = line + len; + read_FP16_YYY(&FP16trace,linePtr); + FP48_YYY_copy(&FP48aux1,&FP_48[0]); + FP48_YYY_trace(&FP16aux1,&FP48aux1); + if(!FP16_YYY_equals(&FP16aux1,&FP16trace)) + { + printf("ERROR computing trace of FP48, line %d\n",i); + exit(EXIT_FAILURE); + } + } + } + + fclose(fp); + + printf("SUCCESS TEST ARITMETIC OF FP48 PASSED\n"); + exit(EXIT_SUCCESS); +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-milagro-crypto-c/blob/8d28d2c3/test/test_fp4_arithmetics_YYY.c.in ---------------------------------------------------------------------- diff --git a/test/test_fp4_arithmetics_YYY.c.in b/test/test_fp4_arithmetics_YYY.c.in new file mode 100644 index 0000000..64a37ae --- /dev/null +++ b/test/test_fp4_arithmetics_YYY.c.in @@ -0,0 +1,630 @@ +/** + * @file test_fp4_arithmetics_YYY.c + * @author Alessandro Budroni + * @brief Test for aritmetics with FP4_YYY + * + * LICENSE + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include "arch.h" +#include "amcl.h" +#include "utils.h" +#include "fp4_YYY.h" + +#define LINE_LEN 10000 +#define MAX_STRING 300 + +void read_BIG_XXX(BIG_XXX A, char* string) +{ + int len; + char support[LINE_LEN]; + BIG_XXX_zero(A); + len = strlen(string)+1; + amcl_hex2bin(string,support,len); + len = (len-1)/2; + BIG_XXX_fromBytesLen(A,support,len); + BIG_XXX_norm(A); +} + +void read_FP2_YYY(FP2_YYY *fp2, char* stringx) +{ + char *stringy, *end; + BIG_XXX x,y; + + stringy = strchr(++stringx,','); + if (stringy == NULL) + { + printf("ERROR unexpected test vector\n"); + exit(EXIT_FAILURE); + } + *stringy = '\0'; + + end = strchr(++stringy,']'); + if (end == NULL) + { + printf("ERROR unexpected test vector\n"); + exit(EXIT_FAILURE); + } + *end = '\0'; + + read_BIG_XXX(x,stringx); + read_BIG_XXX(y,stringy); + + FP2_YYY_from_BIGs(fp2,x,y); +} + +void read_FP4_YYY(FP4_YYY *fp4, char* stringx) +{ + char *stringy, *end; + FP2_YYY x,y; + + stringy = strchr(++stringx,']'); + if (stringy == NULL) + { + printf("ERROR unexpected test vector\n"); + exit(EXIT_FAILURE); + } + *(++stringy) = '\0'; + + end = strchr(++stringy,']'); + if (end == NULL) + { + printf("ERROR unexpected test vector\n"); + exit(EXIT_FAILURE); + } + *(++end) = '\0'; + + read_FP2_YYY(&x,stringx); + read_FP2_YYY(&y,stringy); + + FP4_YYY_from_FP2s(fp4,&x,&y); +} + +int main(int argc, char** argv) +{ + if (argc != 2) + { + printf("usage: ./test_fp4_arithmetics_YYY [path to test vector file]\n"); + exit(EXIT_FAILURE); + } + + int i = 0, len = 0, j = 0; + FILE *fp; + + char line[LINE_LEN]; + char * linePtr = NULL; + + FP4_YYY FP4aux1, FP4aux2, FP4aux3, FP4aux4; + + FP4_YYY FP4_1; + const char* FP4_1line = "FP4_1 = "; + FP4_YYY FP4_2; + const char* FP4_2line = "FP4_2 = "; + FP4_YYY FP12traces[4]; + const char* FP12_1line = "FP12_1 = "; + const char* FP12_2line = "FP12_2 = "; + const char* FP12_3line = "FP12_3 = "; + const char* FP12_4line = "FP12_4 = "; + FP4_YYY FP4add; + const char* FP4addline = "FP4add = "; + FP4_YYY FP4neg; + const char* FP4negline = "FP4neg = "; + FP4_YYY FP4sub; + const char* FP4subline = "FP4sub = "; + FP4_YYY FP4conj; + const char* FP4conjline = "FP4conj = "; + FP4_YYY FP4nconj; + const char* FP4nconjline = "FP4nconj = "; + FP2_YYY FP2sc; + const char* FP2scline = "FP2sc = "; + FP4_YYY FP4pmul; + const char* FP4pmulline = "FP4pmul = "; + FP4_YYY FP4imul; + const char* FP4imulline = "FP4imul = "; + FP4_YYY FP4sqr; + const char* FP4sqrline = "FP4sqr = "; + FP4_YYY FP4mul; + const char* FP4mulline = "FP4mul = "; + FP4_YYY FP4div2; + const char* FP4div2line = "FP4div2 = "; + FP4_YYY FP4inv; + const char* FP4invline = "FP4inv = "; + FP4_YYY FP4mulj; + const char* FP4muljline = "FP4mulj = "; + BIG_XXX BIGsc1; + const char* BIGsc1line = "BIGsc1 = "; + BIG_XXX BIGsc2; + const char* BIGsc2line = "BIGsc2 = "; + FP4_YYY FP4pow; + const char* FP4powline = "FP4pow = "; + FP4_YYY FP4_xtrA; + const char* FP4_xtrAline = "FP4_xtrA = "; + FP4_YYY FP4_xtrD; + const char* FP4_xtrDline = "FP4_xtrD = "; + FP4_YYY FP4_xtrpow; + const char* FP4_xtrpowline = "FP4_xtrpow = "; + FP4_YYY FP4_xtrpow2; + const char* FP4_xtrpow2line = "FP4_xtrpow2 = "; +#if CURVE_SECURITY_ZZZ >= 192 + FP4_YYY FP4sqrt; + const char* FP4sqrtline = "FP4sqrt = "; + FP4_YYY FP4divi; + const char* FP4diviline = "FP4divi = "; + FP4_YYY FP4div2i; + const char* FP4div2iline = "FP4div2i = "; +#endif + + // Set to zero + FP4_YYY_zero(&FP4aux1); + FP4_YYY_zero(&FP4aux2); + + // Testing equal function and set zero function + if(!FP4_YYY_equals(&FP4aux1,&FP4aux2) || !FP4_YYY_iszilch(&FP4aux1) || !FP4_YYY_isreal(&FP4aux1)) + { + printf("ERROR comparing FP4s or setting FP4 to zero FP\n"); + exit(EXIT_FAILURE); + } + + // Set to one + FP4_YYY_one(&FP4aux1); + + // Testing equal function and set one function + if(FP4_YYY_equals(&FP4aux1,&FP4aux2) || !FP4_YYY_isunity(&FP4aux1) || FP4_YYY_isunity(&FP4aux2) || FP4_YYY_iszilch(&FP4aux1) || !FP4_YYY_isreal(&FP4aux1)) + { + printf("ERROR comparing FP4s or setting FP4 to unity FP\n"); + exit(EXIT_FAILURE); + } + + + fp = fopen(argv[1], "r"); + if (fp == NULL) + { + printf("ERROR opening test vector file\n"); + exit(EXIT_FAILURE); + } + + while (fgets(line, LINE_LEN, fp) != NULL) + { + i++; + // Read first FP4 and perform some tests + if (!strncmp(line,FP4_1line, strlen(FP4_1line))) + { + len = strlen(FP4_1line); + linePtr = line + len; + read_FP4_YYY(&FP4_1,linePtr); + // test FP4_from_FP2s + FP4_YYY_from_FP2s(&FP4aux1,&FP4_1.a,&FP4_1.b); + if(!FP4_YYY_equals(&FP4aux1,&FP4_1)) + { + printf("ERROR in generating FP4 from two FP2s, line %d\n",i); + exit(EXIT_FAILURE); + } + // test FP4_from_FP2 + FP4_YYY_from_FP2(&FP4aux1,&FP4_1.a); + FP4_YYY_copy(&FP4aux2,&FP4_1); + FP2_YYY_zero(&FP4aux2.b); + if(!FP4_YYY_equals(&FP4aux1,&FP4aux2)) + { + printf("ERROR in generating FP4 from one FP2, line %d\n",i); + exit(EXIT_FAILURE); + } + // test FP4_from_FP2H + FP4_YYY_from_FP2H(&FP4aux1,&FP4_1.b); + FP4_YYY_copy(&FP4aux2,&FP4_1); + FP2_YYY_zero(&FP4aux2.a); + if(!FP4_YYY_equals(&FP4aux1,&FP4aux2)) + { + printf("ERROR in generating \"complex\" FP4 from one FP2 , line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Read second FP4 + if (!strncmp(line,FP4_2line, strlen(FP4_2line))) + { + len = strlen(FP4_2line); + linePtr = line + len; + read_FP4_YYY(&FP4_2,linePtr); + } + // Read first FP12 trace + if (!strncmp(line,FP12_1line, strlen(FP12_1line))) + { + len = strlen(FP12_1line); + linePtr = line + len; + read_FP4_YYY(FP12traces,linePtr); + } + // Read second FP12 trace + if (!strncmp(line,FP12_2line, strlen(FP12_2line))) + { + len = strlen(FP12_2line); + linePtr = line + len; + read_FP4_YYY(FP12traces+1,linePtr); + } + // Read third FP12 trace + if (!strncmp(line,FP12_3line, strlen(FP12_3line))) + { + len = strlen(FP12_3line); + linePtr = line + len; + read_FP4_YYY(FP12traces+2,linePtr); + } + // Read fourth FP12 trace + if (!strncmp(line,FP12_4line, strlen(FP12_4line))) + { + len = strlen(FP12_4line); + linePtr = line + len; + read_FP4_YYY(FP12traces+3,linePtr); + } + // Addition test + if (!strncmp(line,FP4addline, strlen(FP4addline))) + { + len = strlen(FP4addline); + linePtr = line + len; + read_FP4_YYY(&FP4add,linePtr); + FP4_YYY_copy(&FP4aux1,&FP4_1); + FP4_YYY_copy(&FP4aux2,&FP4_2); + FP4_YYY_add(&FP4aux1,&FP4aux1,&FP4aux2); + // test commutativity P+Q = Q+P + FP4_YYY_copy(&FP4aux3,&FP4_1); + FP4_YYY_add(&FP4aux2,&FP4aux2,&FP4aux3); + if(!FP4_YYY_equals(&FP4aux1,&FP4add) || !FP4_YYY_equals(&FP4aux2,&FP4add)) + { + printf("ERROR adding two FP4, line %d\n",i); + exit(EXIT_FAILURE); + } + // test associativity (P+Q)+R = P+(Q+R) + FP4_YYY_copy(&FP4aux1,&FP4_1); + FP4_YYY_copy(&FP4aux3,&FP4_1); + FP4_YYY_copy(&FP4aux2,&FP4_2); + FP4_YYY_copy(&FP4aux4,&FP4add); + FP4_YYY_add(&FP4aux1,&FP4aux1,&FP4aux2); + FP4_YYY_add(&FP4aux1,&FP4aux1,&FP4aux4); + FP4_YYY_add(&FP4aux2,&FP4aux2,&FP4aux4); + FP4_YYY_add(&FP4aux2,&FP4aux2,&FP4aux3); + FP4_YYY_reduce(&FP4aux1); + FP4_YYY_norm(&FP4aux2); + if(!FP4_YYY_equals(&FP4aux1,&FP4aux2)) + { + printf("ERROR testing associativity between three FP4s, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Test negative of an FP4 + if (!strncmp(line,FP4negline, strlen(FP4negline))) + { + len = strlen(FP4negline); + linePtr = line + len; + read_FP4_YYY(&FP4neg,linePtr); + FP4_YYY_copy(&FP4aux1,&FP4_1); + FP4_YYY_neg(&FP4aux1,&FP4aux1); + if(!FP4_YYY_equals(&FP4aux1,&FP4neg)) + { + printf("ERROR in computing negative of FP4, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Subtraction test + if (!strncmp(line,FP4subline, strlen(FP4subline))) + { + len = strlen(FP4subline); + linePtr = line + len; + read_FP4_YYY(&FP4sub,linePtr); + FP4_YYY_copy(&FP4aux1,&FP4_1); + FP4_YYY_copy(&FP4aux2,&FP4_2); + FP4_YYY_sub(&FP4aux1,&FP4aux1,&FP4aux2); + if(FP4_YYY_equals(&FP4aux1,&FP4sub) == 0) + { + printf("ERROR subtraction between two FP4, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Test conjugate + if (!strncmp(line,FP4conjline, strlen(FP4conjline))) + { + len = strlen(FP4conjline); + linePtr = line + len; + read_FP4_YYY(&FP4conj,linePtr); + FP4_YYY_copy(&FP4aux1,&FP4_1); + FP4_YYY_conj(&FP4aux1,&FP4aux1); + if(!FP4_YYY_equals(&FP4aux1,&FP4conj)) + { + printf("ERROR computing conjugate of FP4, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Test negative conjugate + if (!strncmp(line,FP4nconjline, strlen(FP4nconjline))) + { + len = strlen(FP4nconjline); + linePtr = line + len; + read_FP4_YYY(&FP4nconj,linePtr); + FP4_YYY_copy(&FP4aux1,&FP4_1); + FP4_YYY_nconj(&FP4aux1,&FP4aux1); + if(!FP4_YYY_equals(&FP4aux1,&FP4nconj)) + { + printf("ERROR computing negative conjugate of FP4, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Read multiplicator + if (!strncmp(line,FP2scline, strlen(FP2scline))) + { + len = strlen(FP2scline); + linePtr = line + len; + read_FP2_YYY(&FP2sc,linePtr); + } + // Multiplication by FP2 + if (!strncmp(line,FP4pmulline, strlen(FP4pmulline))) + { + len = strlen(FP4pmulline); + linePtr = line + len; + read_FP4_YYY(&FP4pmul,linePtr); + FP4_YYY_pmul(&FP4aux1,&FP4_1,&FP2sc); + if(!FP4_YYY_equals(&FP4aux1,&FP4pmul)) + { + printf("ERROR in multiplication by FP2, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Multiplication by j = 0..10 + if (!strncmp(line,FP4imulline, strlen(FP4imulline))) + { + len = strlen(FP4imulline); + linePtr = line + len; + read_FP4_YYY(&FP4imul,linePtr); + FP4_YYY_imul(&FP4aux1,&FP4_1,j); + j++; + if(!FP4_YYY_equals(&FP4aux1,&FP4imul)) + { + printf("ERROR in multiplication by small integer, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Square test + if (!strncmp(line,FP4sqrline, strlen(FP4sqrline))) + { + len = strlen(FP4sqrline); + linePtr = line + len; + read_FP4_YYY(&FP4sqr,linePtr); + FP4_YYY_copy(&FP4aux1,&FP4_1); + FP4_YYY_sqr(&FP4aux1,&FP4aux1); + if(!FP4_YYY_equals(&FP4aux1,&FP4sqr)) + { + printf("ERROR in squaring FP4, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Multiplication between two FP4s + if (!strncmp(line,FP4mulline, strlen(FP4mulline))) + { + len = strlen(FP4mulline); + linePtr = line + len; + read_FP4_YYY(&FP4mul,linePtr); + FP4_YYY_mul(&FP4aux1,&FP4_1,&FP4_2); + if(!FP4_YYY_equals(&FP4aux1,&FP4mul)) + { + printf("ERROR in multiplication between two FP4s, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Inverse + if (!strncmp(line,FP4invline, strlen(FP4invline))) + { + len = strlen(FP4invline); + linePtr = line + len; + read_FP4_YYY(&FP4inv,linePtr); + FP4_YYY_copy(&FP4aux1,&FP4_1); + FP4_YYY_inv(&FP4aux1,&FP4aux1); + if(!FP4_YYY_equals(&FP4aux1,&FP4inv)) + { + printf("ERROR in computing inverse of FP4, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Test multiplication of an FP4 instance by sqrt(1+sqrt(-1)) + if (!strncmp(line,FP4muljline, strlen(FP4muljline))) + { + len = strlen(FP4muljline); + linePtr = line + len; + read_FP4_YYY(&FP4mulj,linePtr); + FP4_YYY_copy(&FP4aux1,&FP4_1); + FP4_YYY_times_i(&FP4aux1); + if(!FP4_YYY_equals(&FP4aux1,&FP4mulj)) + { + printf("ERROR in multiplication of an FP4 instance by sqrt(1+sqrt(-1)), line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Read exponent + if (!strncmp(line,BIGsc1line, strlen(BIGsc1line))) + { + len = strlen(BIGsc1line); + linePtr = line + len; + read_BIG_XXX(BIGsc1,linePtr); + } + if (!strncmp(line,BIGsc2line, strlen(BIGsc2line))) + { + len = strlen(BIGsc2line); + linePtr = line + len; + read_BIG_XXX(BIGsc2,linePtr); + } + // Raise FP4 by BIG power + if (!strncmp(line,FP4powline, strlen(FP4powline))) + { + len = strlen(FP4powline); + linePtr = line + len; + read_FP4_YYY(&FP4pow,linePtr); + FP4_YYY_pow(&FP4aux1,&FP4_1,BIGsc1); + if(!FP4_YYY_equals(&FP4aux1,&FP4pow)) + { + printf("ERROR in raising FP4 by BIG power, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Test the XTR addition function r=w*x-conj(x)*y+z + if (!strncmp(line,FP4_xtrAline, strlen(FP4_xtrAline))) + { + len = strlen(FP4_xtrAline); + linePtr = line + len; + read_FP4_YYY(&FP4_xtrA,linePtr); + FP4_YYY_xtr_A(&FP4aux1,&FP4_1,&FP4_2,&FP4add,&FP4sub); + if(!FP4_YYY_equals(&FP4aux1,&FP4_xtrA)) + { + printf("ERROR in testing the XTR addition function r=w*x-conj(x)*y+z, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Test the XTR doubling function r=x^2-2*conj(x) + if (!strncmp(line,FP4_xtrDline, strlen(FP4_xtrDline))) + { + len = strlen(FP4_xtrDline); + linePtr = line + len; + read_FP4_YYY(&FP4_xtrD,linePtr); + FP4_YYY_xtr_D(&FP4aux1,&FP4_1); + if(!FP4_YYY_equals(&FP4aux1,&FP4_xtrD)) + { + printf("ERROR in testing the XTR doubling function r=x^2-2*conj(x), line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Calculates FP4 trace of an FP12 raised to the power of a BIG number + if (!strncmp(line,FP4_xtrpowline, strlen(FP4_xtrpowline))) + { + len = strlen(FP4_xtrpowline); + linePtr = line + len; + read_FP4_YYY(&FP4_xtrpow,linePtr); + FP4_YYY_xtr_pow(&FP4aux1,FP12traces,BIGsc1); + if(!FP4_YYY_equals(&FP4aux1,&FP4_xtrpow)) + { + printf("ERROR computing FP4 trace of an FP12 raised to the power of a BIG number, line %d\n",i); + exit(EXIT_FAILURE); + } + } + + // Calculates FP4 trace of two FP12 raised to the power of two BIG numbers + if (!strncmp(line,FP4_xtrpow2line, strlen(FP4_xtrpow2line))) + { + len = strlen(FP4_xtrpow2line); + linePtr = line + len; + read_FP4_YYY(&FP4_xtrpow2,linePtr); + FP4_YYY_xtr_pow2(&FP4aux1,FP12traces+1,FP12traces,FP12traces+2,FP12traces+3,BIGsc2,BIGsc1); + if(!FP4_YYY_equals(&FP4aux1,&FP4_xtrpow2)) + { + printf("ERROR computing FP4 trace of an FP12 raised to the power of a BIG number (Double), line %d\n",i); + exit(EXIT_FAILURE); + } + } + if(!strncmp(line,FP4div2line, strlen(FP4div2line))) + { + len = strlen(FP4div2line); + linePtr = line + len; + read_FP4_YYY(&FP4div2,linePtr); + FP4_YYY_div2(&FP4aux1,&FP4_1); + if(!FP4_YYY_equals(&FP4aux1,&FP4div2)) + { + printf("ERROR dividing FP4 by sqrt(sqrt(1+sqrt(-1)))/2, line %d\n",i); + exit(EXIT_FAILURE); + } + } +#if CURVE_SECURITY_ZZZ >= 192 + if(!strncmp(line,FP4sqrtline, strlen(FP4sqrtline))) + { + len = strlen(FP4sqrtline); + linePtr = line + len; + read_FP4_YYY(&FP4sqrt,linePtr); + FP4_YYY_sqrt(&FP4aux1,&FP4_1); + FP4_YYY_sqr(&FP4aux2,&FP4aux1); + if(!FP4_YYY_equals(&FP4aux2,&FP4_1)) + { + printf("ERROR consistency FP4_YYY_sqr and FP4_YYY_sqrt, line %d\n",i); + exit(EXIT_FAILURE); + } + FP4_YYY_neg(&FP4aux2,&FP4aux1); + if(!(FP4_YYY_equals(&FP4aux1,&FP4sqrt) || FP4_YYY_equals(&FP4aux2,&FP4sqrt))) + { + printf("ERROR computing square root of FP4, line %d\n",i); + exit(EXIT_FAILURE); + } + } + if(!strncmp(line,FP4diviline, strlen(FP4diviline))) + { + len = strlen(FP4diviline); + linePtr = line + len; + read_FP4_YYY(&FP4divi,linePtr); + FP4_YYY_copy(&FP4aux1,&FP4_1); + FP4_YYY_div_i(&FP4aux1); + FP4_YYY_copy(&FP4aux2,&FP4aux1); + FP4_YYY_times_i(&FP4aux2); + if(!FP4_YYY_equals(&FP4aux2,&FP4_1)) + { + printf("ERROR consistency FP4_YYY_times_i and FP4_YYY_div_i, line %d\n",i); + exit(EXIT_FAILURE); + } + if(!FP4_YYY_equals(&FP4aux1,&FP4divi)) + { + printf("ERROR dividing FP4 by sqrt(1+sqrt(-1)), line %d\n",i); + exit(EXIT_FAILURE); + } + } + if(!strncmp(line,FP4div2iline, strlen(FP4div2iline))) + { + len = strlen(FP4div2iline); + linePtr = line + len; + read_FP4_YYY(&FP4div2i,linePtr); + FP4_YYY_copy(&FP4aux1,&FP4_1); + FP4_YYY_div_2i(&FP4aux1); + if(!FP4_YYY_equals(&FP4aux1,&FP4div2i)) + { + printf("ERROR dividing FP4 by sqrt(1+sqrt(-1))/2, line %d\n",i); + exit(EXIT_FAILURE); + } + } +#endif + } + fclose(fp); + +#if CURVE_SECURITY_ZZZ >= 192 + /* Consistency tests for sqrt */ + + // Sqrt of 0 + FP4_YYY_zero(&FP4aux1); + + if(!(FP4_YYY_sqrt(&FP4aux1,&FP4aux1) && FP4_YYY_iszilch(&FP4aux1))) + { + printf("ERROR Handling 0 in FP4_YYY_sqrt"); + exit(EXIT_FAILURE); + } + + // Sqrt of "real" FP4 + // REMARK: Assume FP2sc is set during the TV test + FP4_YYY_from_FP2(&FP4aux1,&FP2sc); + FP4_YYY_sqrt(&FP4aux2,&FP4aux1); + FP4_YYY_sqr(&FP4aux3,&FP4aux2); + if(!FP4_YYY_equals(&FP4aux1,&FP4aux3)) + { + printf("ERROR consistency of FP4_YYY_sqrt for real FP4, line %d\n",i); + exit(EXIT_FAILURE); + } +#endif + + printf("SUCCESS TEST ARITMETIC OF FP PASSED\n"); + exit(EXIT_SUCCESS); +} http://git-wip-us.apache.org/repos/asf/incubator-milagro-crypto-c/blob/8d28d2c3/test/test_fp8_arithmetics_YYY.c.in ---------------------------------------------------------------------- diff --git a/test/test_fp8_arithmetics_YYY.c.in b/test/test_fp8_arithmetics_YYY.c.in new file mode 100644 index 0000000..8334dbb --- /dev/null +++ b/test/test_fp8_arithmetics_YYY.c.in @@ -0,0 +1,723 @@ +/** + * @file test_fp8_arithmetics_YYY.c + * @author Samuele Andreoli + * @brief Test for aritmetics with FP8_YYY + * + * LICENSE + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include "arch.h" +#include "amcl.h" +#include "utils.h" +#include "fp8_YYY.h" + +#define LINE_LEN 10000 +#define MAX_STRING 300 + +void read_BIG_XXX(BIG_XXX A, char* string) +{ + int len; + char support[LINE_LEN]; + len = strlen(string)+1; + amcl_hex2bin(string,support,len); + len = (len-1)/2; + BIG_XXX_fromBytesLen(A,support,len); + BIG_XXX_norm(A); +} + +void read_FP2_YYY(FP2_YYY *fp2, char* stringx) +{ + char *stringy, *end; + BIG_XXX x,y; + + stringy = strchr(++stringx,','); + if (stringy == NULL) + { + printf("ERROR unexpected test vector\n"); + exit(EXIT_FAILURE); + } + *stringy = '\0'; + + end = strchr(++stringy,']'); + if (end == NULL) + { + printf("ERROR unexpected test vector\n"); + exit(EXIT_FAILURE); + } + *end = '\0'; + + read_BIG_XXX(x,stringx); + read_BIG_XXX(y,stringy); + + FP2_YYY_from_BIGs(fp2,x,y); +} + +void read_FP4_YYY(FP4_YYY *fp4, char* stringx) +{ + char *stringy, *end; + FP2_YYY x,y; + + stringy = strchr(++stringx,']'); + if (stringy == NULL) + { + printf("ERROR unexpected test vector\n"); + exit(EXIT_FAILURE); + } + *(++stringy) = '\0'; + + end = strchr(++stringy,']'); + if (end == NULL) + { + printf("ERROR unexpected test vector\n"); + exit(EXIT_FAILURE); + } + *(++end) = '\0'; + + read_FP2_YYY(&x,stringx); + read_FP2_YYY(&y,stringy); + + FP4_YYY_from_FP2s(fp4,&x,&y); +} + +void read_FP8_YYY(FP8_YYY *fp8, char* stringx) +{ + char *stringy, *end; + FP4_YYY x,y; + + stringy = strchr(++stringx, ']'); + if (stringy == NULL) + { + printf("ERROR unexpected test vector\n"); + exit(EXIT_FAILURE); + } + stringy = strchr(++stringy, ']'); + if (stringy == NULL) + { + printf("ERROR unexpected test vector\n"); + exit(EXIT_FAILURE); + } + stringy+=2; + *(stringy)=0; + + end = strchr(++stringy, ']'); + if (end == NULL) + { + printf("ERROR unexpected test vector\n"); + exit(EXIT_FAILURE); + } + end = strchr(++end, ']'); + if (end == NULL) + { + printf("ERROR unexpected test vector\n"); + exit(EXIT_FAILURE); + } + end+=2; + *(end)=0; + + read_FP4_YYY(&x,stringx); + read_FP4_YYY(&y,stringy); + + FP8_YYY_from_FP4s(fp8,&x,&y); +} + +int main(int argc, char** argv) +{ + if (argc != 2) + { + printf("usage: ./test_fp4_arithmetics_YYY [path to test vector file]\n"); + exit(EXIT_FAILURE); + } + + int i = 0, len = 0, j = 0; + FILE *fp; + + char line[LINE_LEN]; + char * linePtr = NULL; + + FP8_YYY FP8aux1, FP8aux2, FP8aux3, FP8aux4; + + FP8_YYY FP8_1; + const char* FP8_1line = "FP8_1 = "; + FP8_YYY FP8_2; + const char* FP8_2line = "FP8_2 = "; + FP8_YYY FP8add; + const char* FP8addline = "FP8add = "; + FP8_YYY FP8neg; + const char* FP8negline = "FP8neg = "; + FP8_YYY FP8sub; + const char* FP8subline = "FP8sub = "; + FP8_YYY FP8conj; + const char* FP8conjline = "FP8conj = "; + FP8_YYY FP8nconj; + const char* FP8nconjline = "FP8nconj = "; + FP4_YYY FP4sc; + const char* FP4scline = "FP4sc = "; + FP8_YYY FP8pmul; + const char* FP8pmulline = "FP8pmul = "; + FP2_YYY FP2sc; + const char* FP2scline = "FP2sc = "; + FP8_YYY FP8qmul; + const char* FP8qmulline = "FP8qmul = "; + BIG_XXX bigFPsc; + FP_YYY FPsc; + const char* FPscline = "FPsc = "; + FP8_YYY FP8tmul; + const char* FP8tmulline = "FP8tmul = "; + FP8_YYY FP8imul; + const char* FP8imulline = "FP8imul = "; + FP8_YYY FP8sqr; + const char* FP8sqrline = "FP8sqr = "; + FP8_YYY FP8mul; + const char* FP8mulline = "FP8mul = "; + FP8_YYY FP8inv; + const char* FP8invline = "FP8inv = "; + FP8_YYY FP8mulj; + const char* FP8muljline = "FP8mulj = "; + BIG_XXX BIGsc1; + const char* BIGsc1line = "BIGsc1 = "; + BIG_XXX BIGsc2; + const char* BIGsc2line = "BIGsc2 = "; + FP8_YYY FP8pow; + const char* FP8powline = "FP8pow = "; +#if CURVE_SECURITY_ZZZ == 192 + FP8_YYY FP24traces[4]; + const char* FP24_1line = "FP24_1 = "; + const char* FP24_2line = "FP24_2 = "; + const char* FP24_3line = "FP24_3 = "; + const char* FP24_4line = "FP24_4 = "; + FP8_YYY FP8_xtrA; + const char* FP8_xtrAline = "FP8_xtrA = "; + FP8_YYY FP8_xtrD; + const char* FP8_xtrDline = "FP8_xtrD = "; + FP8_YYY FP8_xtrpow; + const char* FP8_xtrpowline = "FP8_xtrpow = "; + FP8_YYY FP8_xtrpow2; + const char* FP8_xtrpow2line = "FP8_xtrpow2 = "; +#elif CURVE_SECURITY_ZZZ == 256 + FP8_YYY FP8sqrt; + const char* FP8sqrtline = "FP8sqrt = "; + FP8_YYY FP8divi; + const char* FP8diviline = "FP8divi = "; + FP8_YYY FP8divi2; + const char* FP8divi2line = "FP8divi2 = "; + FP8_YYY FP8div2i; + const char* FP8div2iline = "FP8div2i = "; +#endif + + // Set to zero and one + FP8_YYY_zero(&FP8aux1); + FP8_YYY_zero(&FP8aux2); + + // Testing equal function and set zero function + if(!FP8_YYY_equals(&FP8aux1,&FP8aux2) || !FP8_YYY_iszilch(&FP8aux1) || !FP8_YYY_isreal(&FP8aux1)) + { + printf("ERROR comparing FP8s or setting FP8 to zero FP\n"); + exit(EXIT_FAILURE); + } + + // Set to one + FP8_YYY_one(&FP8aux1); + + // Testing equal function and set one function + if(FP8_YYY_equals(&FP8aux1,&FP8aux2) || !FP8_YYY_isunity(&FP8aux1) || FP8_YYY_isunity(&FP8aux2) || FP8_YYY_iszilch(&FP8aux1) || !FP8_YYY_isreal(&FP8aux1)) + { + printf("ERROR comparing FP8s or setting FP8 to unity FP\n"); + exit(EXIT_FAILURE); + } + + + fp = fopen(argv[1], "r"); + if (fp == NULL) + { + printf("ERROR opening test vector file\n"); + exit(EXIT_FAILURE); + } + + while (fgets(line, LINE_LEN, fp) != NULL) + { + i++; + // Read first FP8 and perform some tests + if (!strncmp(line,FP8_1line, strlen(FP8_1line))) + { + len = strlen(FP8_1line); + linePtr = line + len; + read_FP8_YYY(&FP8_1,linePtr); + // test FP8_from_FP4s + FP8_YYY_from_FP4s(&FP8aux1,&FP8_1.a,&FP8_1.b); + if(!FP8_YYY_equals(&FP8aux1,&FP8_1)) + { + printf("ERROR in generating FP8 from two FP4s, line %d\n",i); + exit(EXIT_FAILURE); + } + // test FP8_from_FP4 + FP8_YYY_from_FP4(&FP8aux1,&FP8_1.a); + FP8_YYY_copy(&FP8aux2,&FP8_1); + FP4_YYY_zero(&FP8aux2.b); + if(!FP8_YYY_equals(&FP8aux1,&FP8aux2)) + { + printf("ERROR in generating FP8 from one FP4, line %d\n",i); + exit(EXIT_FAILURE); + } + // test FP8_from_FP4 + FP8_YYY_from_FP4H(&FP8aux1,&FP8_1.b); + FP8_YYY_copy(&FP8aux2,&FP8_1); + FP4_YYY_zero(&FP8aux2.a); + if(!FP8_YYY_equals(&FP8aux1,&FP8aux2)) + { + printf("ERROR in generating FP8 from one FP4 as high part, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Read second FP8 + if (!strncmp(line,FP8_2line, strlen(FP8_2line))) + { + len = strlen(FP8_2line); + linePtr = line + len; + read_FP8_YYY(&FP8_2,linePtr); + } + // Addition test + if (!strncmp(line,FP8addline, strlen(FP8addline))) + { + len = strlen(FP8addline); + linePtr = line + len; + read_FP8_YYY(&FP8add,linePtr); + FP8_YYY_copy(&FP8aux1,&FP8_1); + FP8_YYY_copy(&FP8aux2,&FP8_2); + FP8_YYY_add(&FP8aux1,&FP8aux1,&FP8aux2); + // test commutativity P+Q = Q+P + FP8_YYY_copy(&FP8aux3,&FP8_1); + FP8_YYY_add(&FP8aux2,&FP8aux2,&FP8aux3); + if(!FP8_YYY_equals(&FP8aux1,&FP8add) || !FP8_YYY_equals(&FP8aux2,&FP8add)) + { + printf("ERROR adding two FP8, line %d\n",i); + exit(EXIT_FAILURE); + } + // test associativity (P+Q)+R = P+(Q+R) + FP8_YYY_copy(&FP8aux1,&FP8_1); + FP8_YYY_copy(&FP8aux3,&FP8_1); + FP8_YYY_copy(&FP8aux2,&FP8_2); + FP8_YYY_copy(&FP8aux4,&FP8add); + FP8_YYY_add(&FP8aux1,&FP8aux1,&FP8aux2); + FP8_YYY_add(&FP8aux1,&FP8aux1,&FP8aux4); + FP8_YYY_add(&FP8aux2,&FP8aux2,&FP8aux4); + FP8_YYY_add(&FP8aux2,&FP8aux2,&FP8aux3); + FP8_YYY_reduce(&FP8aux1); + FP8_YYY_norm(&FP8aux2); + if(!FP8_YYY_equals(&FP8aux1,&FP8aux2)) + { + printf("ERROR testing associativity between three FP8s, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Test negative of an FP8 + if (!strncmp(line,FP8negline, strlen(FP8negline))) + { + len = strlen(FP8negline); + linePtr = line + len; + read_FP8_YYY(&FP8neg,linePtr); + FP8_YYY_copy(&FP8aux1,&FP8_1); + FP8_YYY_neg(&FP8aux1,&FP8aux1); + if(!FP8_YYY_equals(&FP8aux1,&FP8neg)) + { + printf("ERROR in computing negative of FP8, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Subtraction test + if (!strncmp(line,FP8subline, strlen(FP8subline))) + { + len = strlen(FP8subline); + linePtr = line + len; + read_FP8_YYY(&FP8sub,linePtr); + FP8_YYY_copy(&FP8aux1,&FP8_1); + FP8_YYY_copy(&FP8aux2,&FP8_2); + FP8_YYY_sub(&FP8aux1,&FP8aux1,&FP8aux2); + if(FP8_YYY_equals(&FP8aux1,&FP8sub) == 0) + { + printf("ERROR subtraction between two FP8, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Test conjugate + if (!strncmp(line,FP8conjline, strlen(FP8conjline))) + { + len = strlen(FP8conjline); + linePtr = line + len; + read_FP8_YYY(&FP8conj,linePtr); + FP8_YYY_copy(&FP8aux1,&FP8_1); + FP8_YYY_conj(&FP8aux1,&FP8aux1); + if(!FP8_YYY_equals(&FP8aux1,&FP8conj)) + { + printf("ERROR computing conjugate of FP8, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Test negative conjugate + if (!strncmp(line,FP8nconjline, strlen(FP8nconjline))) + { + len = strlen(FP8nconjline); + linePtr = line + len; + read_FP8_YYY(&FP8nconj,linePtr); + FP8_YYY_copy(&FP8aux1,&FP8_1); + FP8_YYY_nconj(&FP8aux1,&FP8aux1); + if(!FP8_YYY_equals(&FP8aux1,&FP8nconj)) + { + printf("ERROR computing negative conjugate of FP8, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Read FP4 scalar + if (!strncmp(line,FP4scline, strlen(FP4scline))) + { + len = strlen(FP4scline); + linePtr = line + len; + read_FP4_YYY(&FP4sc,linePtr); + } + // Read FP2 scalar + if (!strncmp(line,FP2scline, strlen(FP2scline))) + { + len = strlen(FP2scline); + linePtr = line + len; + read_FP2_YYY(&FP2sc,linePtr); + } + // Read FP scalar + if (!strncmp(line,FPscline, strlen(FPscline))) + { + len = strlen(FPscline); + linePtr = line + len; + read_BIG_XXX(bigFPsc,linePtr); + FP_YYY_nres(&FPsc,bigFPsc); + } + // Multiplication by FP4 + if (!strncmp(line,FP8pmulline, strlen(FP8pmulline))) + { + len = strlen(FP8pmulline); + linePtr = line + len; + read_FP8_YYY(&FP8pmul,linePtr); + FP8_YYY_pmul(&FP8aux1,&FP8_1,&FP4sc); + if(!FP8_YYY_equals(&FP8aux1,&FP8pmul)) + { + printf("ERROR in multiplication by FP4, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Multiplication by FP2 + if (!strncmp(line,FP8qmulline, strlen(FP8qmulline))) + { + len = strlen(FP8qmulline); + linePtr = line + len; + read_FP8_YYY(&FP8qmul,linePtr); + FP8_YYY_qmul(&FP8aux1,&FP8_1,&FP2sc); + if(!FP8_YYY_equals(&FP8aux1,&FP8qmul)) + { + printf("ERROR in multiplication by FP2, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Multiplication by FP + if (!strncmp(line,FP8tmulline, strlen(FP8tmulline))) + { + len = strlen(FP8tmulline); + linePtr = line + len; + read_FP8_YYY(&FP8tmul,linePtr); + FP8_YYY_tmul(&FP8aux1,&FP8_1,&FPsc); + if(!FP8_YYY_equals(&FP8aux1,&FP8tmul)) + { + printf("ERROR in multiplication by FP, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Multiplication by j = 0..10 + if (!strncmp(line,FP8imulline, strlen(FP8imulline))) + { + len = strlen(FP8imulline); + linePtr = line + len; + read_FP8_YYY(&FP8imul,linePtr); + FP8_YYY_imul(&FP8aux1,&FP8_1,j); + j++; + if(!FP8_YYY_equals(&FP8aux1,&FP8imul)) + { + printf("ERROR in multiplication by small integer, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Square test + if (!strncmp(line,FP8sqrline, strlen(FP8sqrline))) + { + len = strlen(FP8sqrline); + linePtr = line + len; + read_FP8_YYY(&FP8sqr,linePtr); + FP8_YYY_sqr(&FP8aux1,&FP8_1); + if(!FP8_YYY_equals(&FP8aux1,&FP8sqr)) + { + printf("ERROR in squaring FP8, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Multiplication between two FP8s + if (!strncmp(line,FP8mulline, strlen(FP8mulline))) + { + len = strlen(FP8mulline); + linePtr = line + len; + read_FP8_YYY(&FP8mul,linePtr); + FP8_YYY_mul(&FP8aux1,&FP8_1,&FP8_2); + if(!FP8_YYY_equals(&FP8aux1,&FP8mul)) + { + printf("ERROR in multiplication between two FP8s, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Inverse + if (!strncmp(line,FP8invline, strlen(FP8invline))) + { + len = strlen(FP8invline); + linePtr = line + len; + read_FP8_YYY(&FP8inv,linePtr); + FP8_YYY_inv(&FP8aux1,&FP8_1); + if(!FP8_YYY_equals(&FP8aux1,&FP8inv)) + { + printf("ERROR in computing inverse of FP8, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Test multiplication of an FP8 instance by sqrt(sqrt(1+sqrt(-1))) + if (!strncmp(line,FP8muljline, strlen(FP8muljline))) + { + len = strlen(FP8muljline); + linePtr = line + len; + read_FP8_YYY(&FP8mulj,linePtr); + FP8_YYY_copy(&FP8aux1,&FP8_1); + FP8_YYY_times_i(&FP8aux1); + if(!FP8_YYY_equals(&FP8aux1,&FP8mulj)) + { + printf("ERROR in multiplication of an FP8 instance by sqrt(sqrt(1+sqrt(-1))), line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Read exponent + if (!strncmp(line,BIGsc1line, strlen(BIGsc1line))) + { + len = strlen(BIGsc1line); + linePtr = line + len; + read_BIG_XXX(BIGsc1,linePtr); + } + if (!strncmp(line,BIGsc2line, strlen(BIGsc2line))) + { + len = strlen(BIGsc2line); + linePtr = line + len; + read_BIG_XXX(BIGsc2,linePtr); + } + // Raise FP8 by BIG power + if (!strncmp(line,FP8powline, strlen(FP8powline))) + { + len = strlen(FP8powline); + linePtr = line + len; + read_FP8_YYY(&FP8pow,linePtr); + FP8_YYY_pow(&FP8aux1,&FP8_1,BIGsc1); + if(!FP8_YYY_equals(&FP8aux1,&FP8pow)) + { + printf("ERROR in raising FP8 by BIG power, line %d\n",i); + exit(EXIT_FAILURE); + } + } +#if CURVE_SECURITY_ZZZ == 192 + // Read first FP24 trace + if (!strncmp(line,FP24_1line, strlen(FP24_1line))) + { + len = strlen(FP24_1line); + linePtr = line + len; + read_FP8_YYY(FP24traces,linePtr); + } + // Read second FP24 trace + if (!strncmp(line,FP24_2line, strlen(FP24_2line))) + { + len = strlen(FP24_2line); + linePtr = line + len; + read_FP8_YYY(FP24traces+1,linePtr); + } + // Read third FP24 trace + if (!strncmp(line,FP24_3line, strlen(FP24_3line))) + { + len = strlen(FP24_3line); + linePtr = line + len; + read_FP8_YYY(FP24traces+2,linePtr); + } + // Read fourth FP24 trace + if (!strncmp(line,FP24_4line, strlen(FP24_4line))) + { + len = strlen(FP24_4line); + linePtr = line + len; + read_FP8_YYY(FP24traces+3,linePtr); + } + // Test the XTR addition function r=w*x-conj(x)*y+z + if (!strncmp(line,FP8_xtrAline, strlen(FP8_xtrAline))) + { + len = strlen(FP8_xtrAline); + linePtr = line + len; + read_FP8_YYY(&FP8_xtrA,linePtr); + FP8_YYY_xtr_A(&FP8aux1,&FP8_1,&FP8_2,&FP8add,&FP8sub); + if(!FP8_YYY_equals(&FP8aux1,&FP8_xtrA)) + { + printf("ERROR in testing the XTR addition function r=w*x-conj(x)*y+z, line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Test the XTR doubling function r=x^2-2*conj(x) + if (!strncmp(line,FP8_xtrDline, strlen(FP8_xtrDline))) + { + len = strlen(FP8_xtrDline); + linePtr = line + len; + read_FP8_YYY(&FP8_xtrD,linePtr); + FP8_YYY_xtr_D(&FP8aux1,&FP8_1); + if(!FP8_YYY_equals(&FP8aux1,&FP8_xtrD)) + { + printf("ERROR in testing the XTR doubling function r=x^2-2*conj(x), line %d\n",i); + exit(EXIT_FAILURE); + } + } + // Calculates FP8 trace of an FP24 raised to the power of a BIG number + if (!strncmp(line,FP8_xtrpowline, strlen(FP8_xtrpowline))) + { + len = strlen(FP8_xtrpowline); + linePtr = line + len; + read_FP8_YYY(&FP8_xtrpow,linePtr); + FP8_YYY_xtr_pow(&FP8aux1,FP24traces,BIGsc1); + if(!FP8_YYY_equals(&FP8aux1,&FP8_xtrpow)) + { + printf("ERROR computing FP8 trace of an FP24 raised to the power of a BIG number, line %d\n",i); + exit(EXIT_FAILURE); + } + } + + // Calculates FP8 trace of two FP24 raised to the power of two BIG numbers + if (!strncmp(line,FP8_xtrpow2line, strlen(FP8_xtrpow2line))) + { + len = strlen(FP8_xtrpow2line); + linePtr = line + len; + read_FP8_YYY(&FP8_xtrpow2,linePtr); + FP8_YYY_xtr_pow2(&FP8aux1,FP24traces+1,FP24traces,FP24traces+2,FP24traces+3,BIGsc2,BIGsc1); + if(!FP8_YYY_equals(&FP8aux1,&FP8_xtrpow2)) + { + printf("ERROR computing FP8 trace of an FP24 raised to the power of a BIG number (Double), line %d\n",i); + exit(EXIT_FAILURE); + } + } +#elif CURVE_SECURITY_ZZZ == 256 + if(!strncmp(line,FP8sqrtline, strlen(FP8sqrtline))) + { + len = strlen(FP8sqrtline); + linePtr = line + len; + read_FP8_YYY(&FP8sqrt,linePtr); + FP8_YYY_sqrt(&FP8aux1,&FP8_1); + FP8_YYY_sqr(&FP8aux2,&FP8aux1); + if(!FP8_YYY_equals(&FP8aux2,&FP8_1)) + { + printf("ERROR consistency FP8_YYY_sqr and FP8_YYY_sqrt, line %d\n",i); + exit(EXIT_FAILURE); + } + FP8_YYY_neg(&FP8aux2,&FP8aux1); + if(!(FP8_YYY_equals(&FP8aux1,&FP8sqrt) || FP8_YYY_equals(&FP8aux2,&FP8sqrt))) + { + printf("ERROR computing square root of FP8, line %d\n",i); + exit(EXIT_FAILURE); + } + } + if(!strncmp(line,FP8diviline, strlen(FP8diviline))) + { + len = strlen(FP8diviline); + linePtr = line + len; + read_FP8_YYY(&FP8divi,linePtr); + FP8_YYY_copy(&FP8aux1,&FP8_1); + FP8_YYY_div_i(&FP8aux1); + FP8_YYY_copy(&FP8aux2,&FP8aux1); + FP8_YYY_times_i(&FP8aux2); + if(!FP8_YYY_equals(&FP8aux2,&FP8_1)) + { + printf("ERROR consistency FP8_YYY_times_i and FP8_YYY_div_i, line %d\n",i); + exit(EXIT_FAILURE); + } + if(!FP8_YYY_equals(&FP8aux1,&FP8divi)) + { + printf("ERROR dividing FP8 by sqrt(sqrt(1+sqrt(-1))), line %d\n",i); + exit(EXIT_FAILURE); + } + } + if(!strncmp(line,FP8divi2line, strlen(FP8divi2line))) + { + len = strlen(FP8divi2line); + linePtr = line + len; + read_FP8_YYY(&FP8divi2,linePtr); + FP8_YYY_copy(&FP8aux1,&FP8_1); + FP8_YYY_div_i2(&FP8aux1); + if(!FP8_YYY_equals(&FP8aux1,&FP8divi2)) + { + printf("ERROR dividing FP8 by sqrt(1+sqrt(-1)) twice, line %d\n",i); + exit(EXIT_FAILURE); + } + } + if(!strncmp(line,FP8div2iline, strlen(FP8div2iline))) + { + len = strlen(FP8div2iline); + linePtr = line + len; + read_FP8_YYY(&FP8div2i,linePtr); + FP8_YYY_copy(&FP8aux1,&FP8_1); + FP8_YYY_div_2i(&FP8aux1); + if(!FP8_YYY_equals(&FP8aux1,&FP8div2i)) + { + printf("ERROR dividing FP8 by sqrt(sqrt(1+sqrt(-1)))/2, line %d\n",i); + exit(EXIT_FAILURE); + } + } +#endif + } + fclose(fp); + +#if CURVE_SECURITY_ZZZ == 256 + /* Consistency tests for sqrt */ + + // Sqrt of 0 + FP8_YYY_zero(&FP8aux1); + FP8_YYY_output(&FP8aux1); + printf("\n"); + + if(!(FP8_YYY_sqrt(&FP8aux1,&FP8aux1) && FP8_YYY_iszilch(&FP8aux1))) + { + printf("ERROR Handling 0 in FP8_YYY_sqrt"); + exit(EXIT_FAILURE); + } + + // Sqrt of "real" FP8 + // REMARK: Assume FP4sc is set during the TV test + FP8_YYY_from_FP4(&FP8aux1,&FP4sc); + FP8_YYY_sqrt(&FP8aux2,&FP8aux1); + FP8_YYY_sqr(&FP8aux3,&FP8aux2); + if(!FP8_YYY_equals(&FP8aux1,&FP8aux3)) + { + printf("ERROR consistency of FP8_YYY_sqrt for real FP8, line %d\n",i); + exit(EXIT_FAILURE); + } +#endif + + printf("SUCCESS TEST ARITMETIC OF FP PASSED\n"); + exit(EXIT_SUCCESS); +}
