blob: ae4d2fcb1aeb957c08250c38a313899e7118c9e8 [file] [log] [blame]
/**
* @file test_big_consistency.c
* @author Alessandro Budroni
* @brief Test for consistency with BIG_XXX
*
* 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 "big_XXX.h"
int main()
{
int i,j;
char raw[256], bytes[MODBYTES_XXX];
csprng rng;
BIG_XXX F,G,H,I,Z;
DBIG_XXX DF,DG;
/* Fake random source */
RAND_clean(&rng);
for (i=0; i<256; i++) raw[i]=(char)i;
RAND_seed(&rng,256,raw);
/* Set to zero */
BIG_XXX_zero(F);
BIG_XXX_zero(G);
BIG_XXX_dzero(DF);
BIG_XXX_dzero(DG);
/* Testing equal function and set zero function */
if(BIG_XXX_comp(G,F) | !BIG_XXX_iszilch(F) | !BIG_XXX_iszilch(G) | BIG_XXX_dcomp(DG,DF) | !BIG_XXX_diszilch(DF) | !BIG_XXX_diszilch(DG))
{
printf("ERROR comparing or setting zero BIG_XXX\n");
exit(EXIT_FAILURE);
}
/* Testing coping and equal function */
BIG_XXX_random(F,&rng);
BIG_XXX_random(DF,&rng);
BIG_XXX_copy(G,F);
BIG_XXX_dcopy(DG,DF);
if(BIG_XXX_comp(G,F) | BIG_XXX_dcomp(DG,DF))
{
printf("ERROR testing coping and equal BIG_XXX\n");
exit(EXIT_FAILURE);
}
/* Testing addition, subtraction */
for (i=0; i<100; i++)
{
BIG_XXX_random(F,&rng);
BIG_XXX_random(H,&rng);
BIG_XXX_copy(G,F);
BIG_XXX_add(G,G,H);
BIG_XXX_sub(G,G,H);
BIG_XXX_sub(H,H,H);
if(BIG_XXX_comp(G,F) | !BIG_XXX_iszilch(H))
{
printf("ERROR testing addition/subtraction BIG_XXX\n");
exit(EXIT_FAILURE);
}
}
BIG_XXX_one(I);
BIG_XXX_zero(Z);
BIG_XXX_zero(F);
BIG_XXX_add(F,F,F);
BIG_XXX_add(Z,I,Z);
if(BIG_XXX_comp(Z,I) | !BIG_XXX_iszilch(F))
{
printf("ERROR testing addition/subtraction BIG_XXX\n");
exit(EXIT_FAILURE);
}
/* Testing small multiplication and division by 3 */
for (i=0; i<100; i++)
{
BIG_XXX_random(F,&rng);
BIG_XXX_copy(G,F);
BIG_XXX_imul(G,G,3);
BIG_XXX_div3(G);
if(BIG_XXX_comp(G,F))
{
printf("ERROR testing small multiplication and division by 3 BIG_XXX\n");
exit(EXIT_FAILURE);
}
}
/* Testing small multiplication and addition */
BIG_XXX_random(F,&rng);
for (j = 1; j <= 20; ++j)
{
BIG_XXX_imul(H,F,j);
BIG_XXX_copy(G,F);
for (i = 1; i < j; ++i)
{
BIG_XXX_add(G,G,F);
}
BIG_XXX_norm(G);
BIG_XXX_norm(H);
if(BIG_XXX_comp(H,G) != 0)
{
printf("ERROR testing small multiplication and addition BIG_XXX, %d\n",j);
exit(EXIT_FAILURE);
}
}
/* Testing square */
for (i=0; i<100; i++)
{
BIG_XXX_random(F,&rng);
BIG_XXX_copy(G,F);
BIG_XXX_sqr(DG,G);
BIG_XXX_mul(DF,F,F);
if(BIG_XXX_dcomp(DG,DF))
{
printf("ERROR testing square BIG_XXX\n");
exit(EXIT_FAILURE);
}
}
/* Testing square mod */
for (i=0; i<100; i++)
{
BIG_XXX_random(H,&rng);
BIG_XXX_randomnum(F,H,&rng);
BIG_XXX_copy(G,F);
BIG_XXX_modsqr(G,G,H);
BIG_XXX_sqr(DF,F);
BIG_XXX_dmod(F,DF,H);
if(BIG_XXX_comp(G,F))
{
printf("ERROR testing mod square BIG_XXX\n");
exit(EXIT_FAILURE);
}
}
/* Testing from and to bytes conversion */
for (i=0; i<100; i++)
{
BIG_XXX_random(F,&rng);
BIG_XXX_copy(G,F);
BIG_XXX_toBytes(bytes,G);
BIG_XXX_fromBytes(G,bytes);
if(BIG_XXX_comp(G,F))
{
printf("ERROR testing from and to bytes conversion BIG_XXX\n");
exit(EXIT_FAILURE);
}
}
BIG_XXX_toBytes(bytes,G);
BIG_XXX_fromBytesLen(G,bytes,MODBYTES_XXX);
if(BIG_XXX_comp(G,F))
{
printf("ERROR testing from and to bytes conversion BIG_XXX\n");
exit(EXIT_FAILURE);
}
/* Testing small increment and decrement */
for (i=0; i<100; i++)
{
BIG_XXX_random(F,&rng);
BIG_XXX_copy(G,F);
BIG_XXX_inc(G,i);
BIG_XXX_dec(G,i);
if(BIG_XXX_comp(G,F))
{
printf("ERROR testing small increment and decrement BIG_XXX\n");
exit(EXIT_FAILURE);
}
}
/* Testing small increment and decrement */
for (i=0; i<100; i++)
{
BIG_XXX_random(F,&rng);
BIG_XXX_copy(G,F);
if(BIG_XXX_comp(G,F))
{
printf("ERROR testing small increment and decrement BIG_XXX\n");
exit(EXIT_FAILURE);
}
}
/* Testing random with modulo */
for (i=0; i<100; i++)
{
BIG_XXX_random(G,&rng);
BIG_XXX_randomnum(F,G,&rng);
if(BIG_XXX_comp(F,G)>0)
{
printf("ERROR testing random with modulo BIG_XXX\n");
exit(EXIT_FAILURE);
}
}
/* Testing mod neg */
for (i=0; i<100; i++)
{
BIG_XXX_random(H,&rng);
BIG_XXX_randomnum(F,H,&rng);
BIG_XXX_modneg(G,F,H);
BIG_XXX_modneg(G,G,H);
BIG_XXX_norm(G);
BIG_XXX_norm(F);
if(BIG_XXX_comp(F,G))
{
printf("ERROR testing mod neg BIG_XXX\n");
exit(EXIT_FAILURE);
}
}
/* Testing copy from/to BIG_XXX/DBIG_XXX */
for (i=0; i<100; i++)
{
BIG_XXX_random(F,&rng);
BIG_XXX_copy(G,F);
BIG_XXX_dzero(DF);
BIG_XXX_dsucopy(DF,F);
BIG_XXX_sducopy(F,DF);
if(BIG_XXX_comp(F,G))
{
printf("ERROR testing copy from/to BIG_XXX/DBIG_XXX\n");
exit(EXIT_FAILURE);
}
}
printf("SUCCESS TEST CONSISTENCY OF BIG_XXX PASSED\n");
exit(EXIT_SUCCESS);
}