diff options
author | Joachim StroĢmbergson <joachim@secworks.se> | 2015-04-24 15:16:23 +0200 |
---|---|---|
committer | Joachim StroĢmbergson <joachim@secworks.se> | 2015-04-24 15:16:23 +0200 |
commit | 502f0f429a261628fe5e43582280012541c40804 (patch) | |
tree | a53cf2bac9474091157c4df56e75a333b3376e26 /src/model | |
parent | 7a6c9b5eb5a3d27be80c68eab583bd05e45bd310 (diff) |
(1) Adding auto generated testbench for verilog. (2) Update of the test generator. (3) Update of the Makefile to run test generator.
Diffstat (limited to 'src/model')
-rw-r--r-- | src/model/c/Debug/makefile | 11 | ||||
-rwxr-xr-x | src/model/c/Debug/modexp | bin | 0 -> 47444 bytes | |||
-rw-r--r-- | src/model/c/Debug/src/subdir.mk | 30 | ||||
-rw-r--r-- | src/model/c/src/ModExpTestBench.c | 13 | ||||
-rw-r--r-- | src/model/c/src/autogenerated_tests.c | 227 | ||||
-rw-r--r-- | src/model/c/src/autogenerated_tests.h | 6 | ||||
-rw-r--r-- | src/model/c/src/bajs.c | 226 | ||||
-rw-r--r-- | src/model/c/src/bignum_uint32_t.c | 47 | ||||
-rw-r--r-- | src/model/c/src/bignum_uint32_t.h | 19 | ||||
-rw-r--r-- | src/model/c/src/findn_test.c | 25 | ||||
-rw-r--r-- | src/model/c/src/montgomery_array.c | 49 | ||||
-rw-r--r-- | src/model/c/src/montgomery_array.h | 5 | ||||
-rw-r--r-- | src/model/c/src/montgomery_array_test.c | 165 | ||||
-rw-r--r-- | src/model/c/src/montgomery_array_test.h | 2 | ||||
-rw-r--r-- | src/model/c/src/simple_tests.c | 72 | ||||
-rw-r--r-- | src/model/c/src/simple_tests.h | 47 | ||||
-rwxr-xr-x | src/model/c/src/test | bin | 0 -> 8628 bytes |
17 files changed, 746 insertions, 198 deletions
diff --git a/src/model/c/Debug/makefile b/src/model/c/Debug/makefile index 92c7223..e4fb64d 100644 --- a/src/model/c/Debug/makefile +++ b/src/model/c/Debug/makefile @@ -5,6 +5,7 @@ -include ../makefile.init RM := rm -rf +CC = clang # All of the sources participating in the build are defined here -include sources.mk @@ -20,22 +21,22 @@ endif -include ../makefile.defs -# Add inputs and outputs from these tool invocations to the build variables +# Add inputs and outputs from these tool invocations to the build variables # All Target -all: rsa +all: modexp # Tool invocations -rsa: $(OBJS) $(USER_OBJS) +modexp: $(OBJS) $(USER_OBJS) @echo 'Building target: $@' @echo 'Invoking: MacOS X C Linker' - gcc -o "rsa" $(OBJS) $(USER_OBJS) $(LIBS) + $(CC) -o "modexp" $(OBJS) $(USER_OBJS) $(LIBS) @echo 'Finished building target: $@' @echo ' ' # Other Targets clean: - -$(RM) $(EXECUTABLES)$(OBJS)$(C_DEPS) rsa + -$(RM) $(EXECUTABLES)$(OBJS)$(C_DEPS) modexp -@echo ' ' .PHONY: all clean dependents diff --git a/src/model/c/Debug/modexp b/src/model/c/Debug/modexp Binary files differnew file mode 100755 index 0000000..a49298d --- /dev/null +++ b/src/model/c/Debug/modexp diff --git a/src/model/c/Debug/src/subdir.mk b/src/model/c/Debug/src/subdir.mk index 05aad58..c45f860 100644 --- a/src/model/c/Debug/src/subdir.mk +++ b/src/model/c/Debug/src/subdir.mk @@ -2,32 +2,40 @@ # Automatically-generated file. Do not edit! ################################################################################ -# Add inputs and outputs from these tool invocations to the build variables +CC = clang +CC_OPT = -O2 +CC_FLAGS = -g3 -pedantic -pedantic-errors -Wall -Wextra -Werror -Wconversion -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" + +# Add inputs and outputs from these tool invocations to the build variables C_SRCS += \ -../src/RSATestBench.c \ +../src/ModExpTestBench.c \ +../src/simple_tests.c \ +../src/autogenerated_tests.c \ ../src/bignum_uint32_t.c \ ../src/montgomery_array.c \ -../src/montgomery_array_test.c +../src/montgomery_array_test.c OBJS += \ -./src/RSATestBench.o \ +./src/ModExpTestBench.o \ +./src/simple_tests.o \ +./src/autogenerated_tests.o \ ./src/bignum_uint32_t.o \ ./src/montgomery_array.o \ -./src/montgomery_array_test.o +./src/montgomery_array_test.o C_DEPS += \ -./src/RSATestBench.d \ +./src/ModExpTestBench.d \ +./src/simple_tests.d \ +./src/autogenerated_tests.d \ ./src/bignum_uint32_t.d \ ./src/montgomery_array.d \ -./src/montgomery_array_test.d +./src/montgomery_array_test.d # Each subdirectory must supply rules for building sources it contributes src/%.o: ../src/%.c @echo 'Building file: $<' - @echo 'Invoking: GCC C Compiler' - gcc -O0 -g3 -pedantic -pedantic-errors -Wall -Wextra -Werror -Wconversion -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<" + @echo 'Invoking: clang C Compiler' + $(CC) $(CC_OPT) $(CC_FLAGS) -o "$@" "$<" @echo 'Finished building: $<' @echo ' ' - - diff --git a/src/model/c/src/ModExpTestBench.c b/src/model/c/src/ModExpTestBench.c new file mode 100644 index 0000000..ab236c1 --- /dev/null +++ b/src/model/c/src/ModExpTestBench.c @@ -0,0 +1,13 @@ +#include <stdio.h> +#include <stdlib.h> +#include "simple_tests.h" +#include "autogenerated_tests.h" +#include "montgomery_array_test.h" + +int main(void) { + simple_tests(); + autogenerated_tests(); + montgomery_array_tests(0); + + return EXIT_SUCCESS; +} diff --git a/src/model/c/src/autogenerated_tests.c b/src/model/c/src/autogenerated_tests.c new file mode 100644 index 0000000..e9ec0b7 --- /dev/null +++ b/src/model/c/src/autogenerated_tests.c @@ -0,0 +1,227 @@ +#include <stdio.h> +#include <stdlib.h> +#include "montgomery_array.h" +#include "bignum_uint32_t.h" +void autogenerated_BASIC_M4962768465676381896(void) { + printf("=== autogenerated_BASIC_M4962768465676381896 ===\n"); + uint32_t X[] = { 0x00000001, 0x946473e1 }; + uint32_t E[] = { 0x00000001, 0x0e85e74f }; + uint32_t M[] = { 0x00000001, 0x70754797 }; + uint32_t expected[] = { 0x00000000, 0x7761ed4f }; + uint32_t Z[] = { 0x00000000, 0x00000000 }; + mod_exp_array(2, X, E, M, Z); + assertArrayEquals(2, expected, Z); +} +void autogenerated_BASIC_8982867242010371843(void) { + printf("=== autogenerated_BASIC_8982867242010371843 ===\n"); + uint32_t X[] = { 0x00000001, 0x6eb4ac2d }; + uint32_t E[] = { 0x00000001, 0xbb200e41 }; + uint32_t M[] = { 0x00000001, 0x27347dc3 }; + uint32_t expected[] = { 0x00000000, 0x87d16204 }; + uint32_t Z[] = { 0x00000000, 0x00000000 }; + mod_exp_array(2, X, E, M, Z); + assertArrayEquals(2, expected, Z); +} +void autogenerated_BASIC_5090788032873075449(void) { + printf("=== autogenerated_BASIC_5090788032873075449 ===\n"); + uint32_t X[] = { 0x00000001, 0x9e504a03 }; + uint32_t E[] = { 0x00000001, 0x9bc057ef }; + uint32_t M[] = { 0x00000001, 0xc8b53fe5 }; + uint32_t expected[] = { 0x00000001, 0xc1a6494c }; + uint32_t Z[] = { 0x00000000, 0x00000000 }; + mod_exp_array(2, X, E, M, Z); + assertArrayEquals(2, expected, Z); +} +void autogenerated_BASIC_8448510918869952728(void) { + printf("=== autogenerated_BASIC_8448510918869952728 ===\n"); + uint32_t X[] = { 0x00000001, 0x73f7b309 }; + uint32_t E[] = { 0x00000001, 0x91c10f7f }; + uint32_t M[] = { 0x00000001, 0x4be322c9 }; + uint32_t expected[] = { 0x00000000, 0x9a155286 }; + uint32_t Z[] = { 0x00000000, 0x00000000 }; + mod_exp_array(2, X, E, M, Z); + assertArrayEquals(2, expected, Z); +} +void autogenerated_BASIC_4036237668019554146(void) { + printf("=== autogenerated_BASIC_4036237668019554146 ===\n"); + uint32_t X[] = { 0x00000001, 0xd0f3961d }; + uint32_t E[] = { 0x00000001, 0xcdbc9c9d }; + uint32_t M[] = { 0x00000001, 0x30367d5b }; + uint32_t expected[] = { 0x00000001, 0x15a9c15d }; + uint32_t Z[] = { 0x00000000, 0x00000000 }; + mod_exp_array(2, X, E, M, Z); + assertArrayEquals(2, expected, Z); +} +void autogenerated_BASIC_M8925041444689012509(void) { + printf("=== autogenerated_BASIC_M8925041444689012509 ===\n"); + uint32_t X[] = { 0x00000001, 0x34130e17 }; + uint32_t E[] = { 0x00000001, 0xf45e52c9 }; + uint32_t M[] = { 0x00000001, 0x9cb5c68d }; + uint32_t expected[] = { 0x00000000, 0x7c129d37 }; + uint32_t Z[] = { 0x00000000, 0x00000000 }; + mod_exp_array(2, X, E, M, Z); + assertArrayEquals(2, expected, Z); +} +void autogenerated_BASIC_M5713608137760059379(void) { + printf("=== autogenerated_BASIC_M5713608137760059379 ===\n"); + uint32_t X[] = { 0x00000001, 0x77505dbd }; + uint32_t E[] = { 0x00000001, 0xdb808627 }; + uint32_t M[] = { 0x00000001, 0xad1fed09 }; + uint32_t expected[] = { 0x00000001, 0x842cd733 }; + uint32_t Z[] = { 0x00000000, 0x00000000 }; + mod_exp_array(2, X, E, M, Z); + assertArrayEquals(2, expected, Z); +} +void autogenerated_BASIC_6816968587684568101(void) { + printf("=== autogenerated_BASIC_6816968587684568101 ===\n"); + uint32_t X[] = { 0x00000001, 0x3272b6ef }; + uint32_t E[] = { 0x00000001, 0x2cb6c09b }; + uint32_t M[] = { 0x00000001, 0xefbc64fd }; + uint32_t expected[] = { 0x00000001, 0x59c3b603 }; + uint32_t Z[] = { 0x00000000, 0x00000000 }; + mod_exp_array(2, X, E, M, Z); + assertArrayEquals(2, expected, Z); +} +void autogenerated_BASIC_4168013900853404774(void) { + printf("=== autogenerated_BASIC_4168013900853404774 ===\n"); + uint32_t X[] = { 0x00000001, 0x3c20bbcf }; + uint32_t E[] = { 0x00000001, 0xa495d8ab }; + uint32_t M[] = { 0x00000001, 0x75ddb9ef }; + uint32_t expected[] = { 0x00000001, 0x1413eac7 }; + uint32_t Z[] = { 0x00000000, 0x00000000 }; + mod_exp_array(2, X, E, M, Z); + assertArrayEquals(2, expected, Z); +} +void autogenerated_BASIC_M8394821325674331878(void) { + printf("=== autogenerated_BASIC_M8394821325674331878 ===\n"); + uint32_t X[] = { 0x00000001, 0x93d3d0d3 }; + uint32_t E[] = { 0x00000001, 0x43c2dfef }; + uint32_t M[] = { 0x00000001, 0x7443cbf1 }; + uint32_t expected[] = { 0x00000000, 0xc2eda7c3 }; + uint32_t Z[] = { 0x00000000, 0x00000000 }; + mod_exp_array(2, X, E, M, Z); + assertArrayEquals(2, expected, Z); +} +void autogenerated_BASIC_M2919828800172604435(void) { + printf("=== autogenerated_BASIC_M2919828800172604435 ===\n"); + uint32_t X[] = { 0x3d746ec5 }; + uint32_t E[] = { 0x3f7ea6d5 }; + uint32_t M[] = { 0x29b6675f }; + uint32_t expected[] = { 0x040c43d8 }; + uint32_t Z[] = { 0x00000000 }; + mod_exp_array(1, X, E, M, Z); + assertArrayEquals(1, expected, Z); +} +void autogenerated_BASIC_4770912732078070597(void) { + printf("=== autogenerated_BASIC_4770912732078070597 ===\n"); + uint32_t X[] = { 0x200c0f45 }; + uint32_t E[] = { 0x24774bab }; + uint32_t M[] = { 0x234ca073 }; + uint32_t expected[] = { 0x14505436 }; + uint32_t Z[] = { 0x00000000 }; + mod_exp_array(1, X, E, M, Z); + assertArrayEquals(1, expected, Z); +} +void autogenerated_BASIC_3593487472385409519(void) { + printf("=== autogenerated_BASIC_3593487472385409519 ===\n"); + uint32_t X[] = { 0x248819d1 }; + uint32_t E[] = { 0x2ad2b6ed }; + uint32_t M[] = { 0x269cc6bf }; + uint32_t expected[] = { 0x0f09d466 }; + uint32_t Z[] = { 0x00000000 }; + mod_exp_array(1, X, E, M, Z); + assertArrayEquals(1, expected, Z); +} +void autogenerated_BASIC_4981749054780354961(void) { + printf("=== autogenerated_BASIC_4981749054780354961 ===\n"); + uint32_t X[] = { 0x27bec4e7 }; + uint32_t E[] = { 0x36fe540f }; + uint32_t M[] = { 0x25a46d61 }; + uint32_t expected[] = { 0x0bab2269 }; + uint32_t Z[] = { 0x00000000 }; + mod_exp_array(1, X, E, M, Z); + assertArrayEquals(1, expected, Z); +} +void autogenerated_BASIC_7702189670289360961(void) { + printf("=== autogenerated_BASIC_7702189670289360961 ===\n"); + uint32_t X[] = { 0x302def29 }; + uint32_t E[] = { 0x25b9c233 }; + uint32_t M[] = { 0x33af5461 }; + uint32_t expected[] = { 0x0229dc08 }; + uint32_t Z[] = { 0x00000000 }; + mod_exp_array(1, X, E, M, Z); + assertArrayEquals(1, expected, Z); +} +void autogenerated_BASIC_M5169634701858105792(void) { + printf("=== autogenerated_BASIC_M5169634701858105792 ===\n"); + uint32_t X[] = { 0x240d8cf5 }; + uint32_t E[] = { 0x2a6a7381 }; + uint32_t M[] = { 0x3471d1e9 }; + uint32_t expected[] = { 0x244dec19 }; + uint32_t Z[] = { 0x00000000 }; + mod_exp_array(1, X, E, M, Z); + assertArrayEquals(1, expected, Z); +} +void autogenerated_BASIC_6469444563916025786(void) { + printf("=== autogenerated_BASIC_6469444563916025786 ===\n"); + uint32_t X[] = { 0x3cc9270b }; + uint32_t E[] = { 0x27858fdd }; + uint32_t M[] = { 0x21e65001 }; + uint32_t expected[] = { 0x17200d8c }; + uint32_t Z[] = { 0x00000000 }; + mod_exp_array(1, X, E, M, Z); + assertArrayEquals(1, expected, Z); +} +void autogenerated_BASIC_M2453278165832221565(void) { + printf("=== autogenerated_BASIC_M2453278165832221565 ===\n"); + uint32_t X[] = { 0x30ca6ceb }; + uint32_t E[] = { 0x212c387b }; + uint32_t M[] = { 0x2e07a7bb }; + uint32_t expected[] = { 0x0fc15a1f }; + uint32_t Z[] = { 0x00000000 }; + mod_exp_array(1, X, E, M, Z); + assertArrayEquals(1, expected, Z); +} +void autogenerated_BASIC_M1847183855567461116(void) { + printf("=== autogenerated_BASIC_M1847183855567461116 ===\n"); + uint32_t X[] = { 0x3d02c5a1 }; + uint32_t E[] = { 0x35f12b45 }; + uint32_t M[] = { 0x32f0b03f }; + uint32_t expected[] = { 0x2340f96f }; + uint32_t Z[] = { 0x00000000 }; + mod_exp_array(1, X, E, M, Z); + assertArrayEquals(1, expected, Z); +} +void autogenerated_BASIC_M7037130911981370263(void) { + printf("=== autogenerated_BASIC_M7037130911981370263 ===\n"); + uint32_t X[] = { 0x2692d1cd }; + uint32_t E[] = { 0x3b21ef8d }; + uint32_t M[] = { 0x2042c76d }; + uint32_t expected[] = { 0x1b753aea }; + uint32_t Z[] = { 0x00000000 }; + mod_exp_array(1, X, E, M, Z); + assertArrayEquals(1, expected, Z); +} +void autogenerated_tests(void) { + autogenerated_BASIC_M4962768465676381896(); + autogenerated_BASIC_8982867242010371843(); + autogenerated_BASIC_5090788032873075449(); + autogenerated_BASIC_8448510918869952728(); + autogenerated_BASIC_4036237668019554146(); + autogenerated_BASIC_M8925041444689012509(); + autogenerated_BASIC_M5713608137760059379(); + autogenerated_BASIC_6816968587684568101(); + autogenerated_BASIC_4168013900853404774(); + autogenerated_BASIC_M8394821325674331878(); + autogenerated_BASIC_M2919828800172604435(); + autogenerated_BASIC_4770912732078070597(); + autogenerated_BASIC_3593487472385409519(); + autogenerated_BASIC_4981749054780354961(); + autogenerated_BASIC_7702189670289360961(); + autogenerated_BASIC_M5169634701858105792(); + autogenerated_BASIC_6469444563916025786(); + autogenerated_BASIC_M2453278165832221565(); + autogenerated_BASIC_M1847183855567461116(); + autogenerated_BASIC_M7037130911981370263(); + +} diff --git a/src/model/c/src/autogenerated_tests.h b/src/model/c/src/autogenerated_tests.h new file mode 100644 index 0000000..ce7a89e --- /dev/null +++ b/src/model/c/src/autogenerated_tests.h @@ -0,0 +1,6 @@ +#ifndef AUTOGENERATED_TESTS_H_ +#define AUTOGENERATED_TESTS_H_ + +void autogenerated_tests(void); + +#endif /* AUTOGENERATED_TESTS_H_ */ diff --git a/src/model/c/src/bajs.c b/src/model/c/src/bajs.c new file mode 100644 index 0000000..e064fb8 --- /dev/null +++ b/src/model/c/src/bajs.c @@ -0,0 +1,226 @@ +#include <stdio.h> +#include <stdlib.h> +#include "montgomery_array.h" +#include "bignum_uint32_t.h" +void autogenerated_BASIC_M4962768465676381896(void) { + printf("=== autogenerated_BASIC_M4962768465676381896 ===\n"); + uint32_t X[] = { 0x00000001, 0x946473e1 }; + uint32_t E[] = { 0x00000001, 0x0e85e74f }; + uint32_t M[] = { 0x00000001, 0x70754797 }; + uint32_t expected[] = { 0x00000000, 0x7761ed4f }; + uint32_t Z[] = { 0x00000000, 0x00000000 }; + mod_exp_array(2, X, E, M, Z); + assertArrayEquals(2, expected, Z); +} +void autogenerated_BASIC_8982867242010371843(void) { + printf("=== autogenerated_BASIC_8982867242010371843 ===\n"); + uint32_t X[] = { 0x00000001, 0x6eb4ac2d }; + uint32_t E[] = { 0x00000001, 0xbb200e41 }; + uint32_t M[] = { 0x00000001, 0x27347dc3 }; + uint32_t expected[] = { 0x00000000, 0x87d16204 }; + uint32_t Z[] = { 0x00000000, 0x00000000 }; + mod_exp_array(2, X, E, M, Z); + assertArrayEquals(2, expected, Z); +} +void autogenerated_BASIC_5090788032873075449(void) { + printf("=== autogenerated_BASIC_5090788032873075449 ===\n"); + uint32_t X[] = { 0x00000001, 0x9e504a03 }; + uint32_t E[] = { 0x00000001, 0x9bc057ef }; + uint32_t M[] = { 0x00000001, 0xc8b53fe5 }; + uint32_t expected[] = { 0x00000001, 0xc1a6494c }; + uint32_t Z[] = { 0x00000000, 0x00000000 }; + mod_exp_array(2, X, E, M, Z); + assertArrayEquals(2, expected, Z); +} +void autogenerated_BASIC_8448510918869952728(void) { + printf("=== autogenerated_BASIC_8448510918869952728 ===\n"); + uint32_t X[] = { 0x00000001, 0x73f7b309 }; + uint32_t E[] = { 0x00000001, 0x91c10f7f }; + uint32_t M[] = { 0x00000001, 0x4be322c9 }; + uint32_t expected[] = { 0x00000000, 0x9a155286 }; + uint32_t Z[] = { 0x00000000, 0x00000000 }; + mod_exp_array(2, X, E, M, Z); + assertArrayEquals(2, expected, Z); +} +void autogenerated_BASIC_4036237668019554146(void) { + printf("=== autogenerated_BASIC_4036237668019554146 ===\n"); + uint32_t X[] = { 0x00000001, 0xd0f3961d }; + uint32_t E[] = { 0x00000001, 0xcdbc9c9d }; + uint32_t M[] = { 0x00000001, 0x30367d5b }; + uint32_t expected[] = { 0x00000001, 0x15a9c15d }; + uint32_t Z[] = { 0x00000000, 0x00000000 }; + mod_exp_array(2, X, E, M, Z); + assertArrayEquals(2, expected, Z); +} +void autogenerated_BASIC_M8925041444689012509(void) { + printf("=== autogenerated_BASIC_M8925041444689012509 ===\n"); + uint32_t X[] = { 0x00000001, 0x34130e17 }; + uint32_t E[] = { 0x00000001, 0xf45e52c9 }; + uint32_t M[] = { 0x00000001, 0x9cb5c68d }; + uint32_t expected[] = { 0x00000000, 0x7c129d37 }; + uint32_t Z[] = { 0x00000000, 0x00000000 }; + mod_exp_array(2, X, E, M, Z); + assertArrayEquals(2, expected, Z); +} +void autogenerated_BASIC_M5713608137760059379(void) { + printf("=== autogenerated_BASIC_M5713608137760059379 ===\n"); + uint32_t X[] = { 0x00000001, 0x77505dbd }; + uint32_t E[] = { 0x00000001, 0xdb808627 }; + uint32_t M[] = { 0x00000001, 0xad1fed09 }; + uint32_t expected[] = { 0x00000001, 0x842cd733 }; + uint32_t Z[] = { 0x00000000, 0x00000000 }; + mod_exp_array(2, X, E, M, Z); + assertArrayEquals(2, expected, Z); +} +void autogenerated_BASIC_6816968587684568101(void) { + printf("=== autogenerated_BASIC_6816968587684568101 ===\n"); + uint32_t X[] = { 0x00000001, 0x3272b6ef }; + uint32_t E[] = { 0x00000001, 0x2cb6c09b }; + uint32_t M[] = { 0x00000001, 0xefbc64fd }; + uint32_t expected[] = { 0x00000001, 0x59c3b603 }; + uint32_t Z[] = { 0x00000000, 0x00000000 }; + mod_exp_array(2, X, E, M, Z); + assertArrayEquals(2, expected, Z); +} +void autogenerated_BASIC_4168013900853404774(void) { + printf("=== autogenerated_BASIC_4168013900853404774 ===\n"); + uint32_t X[] = { 0x00000001, 0x3c20bbcf }; + uint32_t E[] = { 0x00000001, 0xa495d8ab }; + uint32_t M[] = { 0x00000001, 0x75ddb9ef }; + uint32_t expected[] = { 0x00000001, 0x1413eac7 }; + uint32_t Z[] = { 0x00000000, 0x00000000 }; + mod_exp_array(2, X, E, M, Z); + assertArrayEquals(2, expected, Z); +} +void autogenerated_BASIC_M8394821325674331878(void) { + printf("=== autogenerated_BASIC_M8394821325674331878 ===\n"); + uint32_t X[] = { 0x00000001, 0x93d3d0d3 }; + uint32_t E[] = { 0x00000001, 0x43c2dfef }; + uint32_t M[] = { 0x00000001, 0x7443cbf1 }; + uint32_t expected[] = { 0x00000000, 0xc2eda7c3 }; + uint32_t Z[] = { 0x00000000, 0x00000000 }; + mod_exp_array(2, X, E, M, Z); + assertArrayEquals(2, expected, Z); +} +void autogenerated_BASIC_M2919828800172604435(void) { + printf("=== autogenerated_BASIC_M2919828800172604435 ===\n"); + uint32_t X[] = { 0x3d746ec5 }; + uint32_t E[] = { 0x3f7ea6d5 }; + uint32_t M[] = { 0x29b6675f }; + uint32_t expected[] = { 0x040c43d8 }; + uint32_t Z[] = { 0x00000000 }; + mod_exp_array(1, X, E, M, Z); + assertArrayEquals(1, expected, Z); +} +void autogenerated_BASIC_4770912732078070597(void) { + printf("=== autogenerated_BASIC_4770912732078070597 ===\n"); + uint32_t X[] = { 0x200c0f45 }; + uint32_t E[] = { 0x24774bab }; + uint32_t M[] = { 0x234ca073 }; + uint32_t expected[] = { 0x14505436 }; + uint32_t Z[] = { 0x00000000 }; + mod_exp_array(1, X, E, M, Z); + assertArrayEquals(1, expected, Z); +} +void autogenerated_BASIC_3593487472385409519(void) { + printf("=== autogenerated_BASIC_3593487472385409519 ===\n"); + uint32_t X[] = { 0x248819d1 }; + uint32_t E[] = { 0x2ad2b6ed }; + uint32_t M[] = { 0x269cc6bf }; + uint32_t expected[] = { 0x0f09d466 }; + uint32_t Z[] = { 0x00000000 }; + mod_exp_array(1, X, E, M, Z); + assertArrayEquals(1, expected, Z); +} +void autogenerated_BASIC_4981749054780354961(void) { + printf("=== autogenerated_BASIC_4981749054780354961 ===\n"); + uint32_t X[] = { 0x27bec4e7 }; + uint32_t E[] = { 0x36fe540f }; + uint32_t M[] = { 0x25a46d61 }; + uint32_t expected[] = { 0x0bab2269 }; + uint32_t Z[] = { 0x00000000 }; + mod_exp_array(1, X, E, M, Z); + assertArrayEquals(1, expected, Z); +} +void autogenerated_BASIC_7702189670289360961(void) { + printf("=== autogenerated_BASIC_7702189670289360961 ===\n"); + uint32_t X[] = { 0x302def29 }; + uint32_t E[] = { 0x25b9c233 }; + uint32_t M[] = { 0x33af5461 }; + uint32_t expected[] = { 0x0229dc08 }; + uint32_t Z[] = { 0x00000000 }; + mod_exp_array(1, X, E, M, Z); + assertArrayEquals(1, expected, Z); +} +void autogenerated_BASIC_M5169634701858105792(void) { + printf("=== autogenerated_BASIC_M5169634701858105792 ===\n"); + uint32_t X[] = { 0x240d8cf5 }; + uint32_t E[] = { 0x2a6a7381 }; + uint32_t M[] = { 0x3471d1e9 }; + uint32_t expected[] = { 0x244dec19 }; + uint32_t Z[] = { 0x00000000 }; + mod_exp_array(1, X, E, M, Z); + assertArrayEquals(1, expected, Z); +} +void autogenerated_BASIC_6469444563916025786(void) { + printf("=== autogenerated_BASIC_6469444563916025786 ===\n"); + uint32_t X[] = { 0x3cc9270b }; + uint32_t E[] = { 0x27858fdd }; + uint32_t M[] = { 0x21e65001 }; + uint32_t expected[] = { 0x17200d8c }; + uint32_t Z[] = { 0x00000000 }; + mod_exp_array(1, X, E, M, Z); + assertArrayEquals(1, expected, Z); +} +void autogenerated_BASIC_M2453278165832221565(void) { + printf("=== autogenerated_BASIC_M2453278165832221565 ===\n"); + uint32_t X[] = { 0x30ca6ceb }; + uint32_t E[] = { 0x212c387b }; + uint32_t M[] = { 0x2e07a7bb }; + uint32_t expected[] = { 0x0fc15a1f }; + uint32_t Z[] = { 0x00000000 }; + mod_exp_array(1, X, E, M, Z); + assertArrayEquals(1, expected, Z); +} +void autogenerated_BASIC_M1847183855567461116(void) { + printf("=== autogenerated_BASIC_M1847183855567461116 ===\n"); + uint32_t X[] = { 0x3d02c5a1 }; + uint32_t E[] = { 0x35f12b45 }; + uint32_t M[] = { 0x32f0b03f }; + uint32_t expected[] = { 0x2340f96f }; + uint32_t Z[] = { 0x00000000 }; + mod_exp_array(1, X, E, M, Z); + assertArrayEquals(1, expected, Z); +} +void autogenerated_BASIC_M7037130911981370263(void) { + printf("=== autogenerated_BASIC_M7037130911981370263 ===\n"); + uint32_t X[] = { 0x2692d1cd }; + uint32_t E[] = { 0x3b21ef8d }; + uint32_t M[] = { 0x2042c76d }; + uint32_t expected[] = { 0x1b753aea }; + uint32_t Z[] = { 0x00000000 }; + mod_exp_array(1, X, E, M, Z); + assertArrayEquals(1, expected, Z); +} +void autogenerated_tests(void) { + autogenerated_BASIC_M4962768465676381896(); + autogenerated_BASIC_8982867242010371843(); + autogenerated_BASIC_5090788032873075449(); + autogenerated_BASIC_8448510918869952728(); + autogenerated_BASIC_4036237668019554146(); + autogenerated_BASIC_M8925041444689012509(); + autogenerated_BASIC_M5713608137760059379(); + autogenerated_BASIC_6816968587684568101(); + autogenerated_BASIC_4168013900853404774(); + autogenerated_BASIC_M8394821325674331878(); + autogenerated_BASIC_M2919828800172604435(); + autogenerated_BASIC_4770912732078070597(); + autogenerated_BASIC_3593487472385409519(); + autogenerated_BASIC_4981749054780354961(); + autogenerated_BASIC_7702189670289360961(); + autogenerated_BASIC_M5169634701858105792(); + autogenerated_BASIC_6469444563916025786(); + autogenerated_BASIC_M2453278165832221565(); + autogenerated_BASIC_M1847183855567461116(); + autogenerated_BASIC_M7037130911981370263(); +} diff --git a/src/model/c/src/bignum_uint32_t.c b/src/model/c/src/bignum_uint32_t.c index 75d27a8..c40b538 100644 --- a/src/model/c/src/bignum_uint32_t.c +++ b/src/model/c/src/bignum_uint32_t.c @@ -2,14 +2,27 @@ #include <stdlib.h> #include "bignum_uint32_t.h" -void copy_array(int length, uint32_t *src, uint32_t *dst) { - for (int i = 0; i < length; i++) +void assertArrayEquals(uint32_t length, uint32_t *expected, uint32_t *actual) { //needed in tests + int equals = 1; + for (uint32_t i = 0; i < length; i++) + equals &= expected[i] == actual[i]; + printf("%s expected: [", equals ? "PASS" : "FAIL"); + for (uint32_t i = 0; i < length - 1; i++) + printf("%8x, ", expected[i]); + printf("%8x] actual: [ ", expected[length - 1]); + for (uint32_t i = 0; i < length - 1; i++) + printf("%8x, ", actual[i]); + printf("%8x]\n", actual[length - 1]); +} + +void copy_array(uint32_t length, uint32_t *src, uint32_t *dst) { + for (uint32_t i = 0; i < length; i++) dst[i] = src[i]; } -void add_array(int length, uint32_t *a, uint32_t *b, uint32_t *result) { +void add_array(uint32_t length, uint32_t *a, uint32_t *b, uint32_t *result) { uint64_t carry = 0; - for (int i = length - 1; i >= 0; i--) { + for (int32_t i = ((int32_t) length) - 1; i >= 0; i--) { uint64_t r = carry; uint32_t aa = a[i]; uint32_t bb = b[i]; @@ -20,9 +33,9 @@ void add_array(int length, uint32_t *a, uint32_t *b, uint32_t *result) { } } -void sub_array(int length, uint32_t *a, uint32_t *b, uint32_t *result) { +void sub_array(uint32_t length, uint32_t *a, uint32_t *b, uint32_t *result) { uint64_t carry = 1; - for (int wordIndex = length - 1; wordIndex >= 0; wordIndex--) { + for (int32_t wordIndex = ((int32_t) length) - 1; wordIndex >= 0; wordIndex--) { uint64_t r = carry; uint32_t aa = a[wordIndex]; uint32_t bb = ~b[wordIndex]; @@ -33,18 +46,18 @@ void sub_array(int length, uint32_t *a, uint32_t *b, uint32_t *result) { } } -void shift_right_1_array(int length, uint32_t *a, uint32_t *result) { +void shift_right_1_array(uint32_t length, uint32_t *a, uint32_t *result) { uint32_t prev = 0; // MSB will be zero extended - for (int wordIndex = 0; wordIndex < length; wordIndex++) { + for (uint32_t wordIndex = 0; wordIndex < length; wordIndex++) { uint32_t aa = a[wordIndex]; result[wordIndex] = (aa >> 1) | (prev << 31); prev = aa & 1; // Lower word will be extended with LSB of this word } } -void shift_left_1_array(int length, uint32_t *a, uint32_t *result) { +void shift_left_1_array(uint32_t length, uint32_t *a, uint32_t *result) { uint32_t prev = 0; // LSB will be zero extended - for (int wordIndex = length - 1; wordIndex >= 0; wordIndex--) { + for (int32_t wordIndex = ((int32_t) length) - 1; wordIndex >= 0; wordIndex--) { uint32_t aa = a[wordIndex]; result[wordIndex] = (aa << 1) | prev; @@ -53,15 +66,15 @@ void shift_left_1_array(int length, uint32_t *a, uint32_t *result) { } } -void debugArray(char *msg, int length, uint32_t *array) { +void debugArray(char *msg, uint32_t length, uint32_t *array) { printf("%s ", msg); - for (int i = 0; i < length; i++) { + for (uint32_t i = 0; i < length; i++) { printf("%8x ", array[i]); } printf("\n"); } -void modulus_array(int length, uint32_t *a, uint32_t *modulus, uint32_t *temp, +void modulus_array(uint32_t length, uint32_t *a, uint32_t *modulus, uint32_t *temp, uint32_t *reminder) { copy_array(length, a, reminder); @@ -77,13 +90,13 @@ void modulus_array(int length, uint32_t *a, uint32_t *modulus, uint32_t *temp, } } -void zero_array(int length, uint32_t *a) { - for (int i = 0; i < length; i++) +void zero_array(uint32_t length, uint32_t *a) { + for (uint32_t i = 0; i < length; i++) a[i] = 0; } -int greater_than_array(int length, uint32_t *a, uint32_t *b) { - for (int i = 0; i < length; i++) { +int greater_than_array(uint32_t length, uint32_t *a, uint32_t *b) { + for (uint32_t i = 0; i < length; i++) { if (a[i] > b[i]) return 1; if (a[i] < b[i]) diff --git a/src/model/c/src/bignum_uint32_t.h b/src/model/c/src/bignum_uint32_t.h index c0fc991..e275fc4 100644 --- a/src/model/c/src/bignum_uint32_t.h +++ b/src/model/c/src/bignum_uint32_t.h @@ -8,15 +8,16 @@ #ifndef BIGNUM_UINT32_T_H_ #define BIGNUM_UINT32_T_H_ -void modulus_array(int length, uint32_t *a, uint32_t *modulus, uint32_t *temp, +void modulus_array(uint32_t length, uint32_t *a, uint32_t *modulus, uint32_t *temp, uint32_t *reminder); -int greater_than_array(int length, uint32_t *a, uint32_t *b); -void add_array(int length, uint32_t *a, uint32_t *b, uint32_t *result); -void sub_array(int length, uint32_t *a, uint32_t *b, uint32_t *result); -void shift_right_1_array(int length, uint32_t *a, uint32_t *result); -void shift_left_1_array(int length, uint32_t *a, uint32_t *result); -void zero_array(int length, uint32_t *a); -void copy_array(int length, uint32_t *src, uint32_t *dst); -void debugArray(char *msg, int length, uint32_t *array); +int greater_than_array(uint32_t length, uint32_t *a, uint32_t *b); +void add_array(uint32_t length, uint32_t *a, uint32_t *b, uint32_t *result); +void sub_array(uint32_t length, uint32_t *a, uint32_t *b, uint32_t *result); +void shift_right_1_array(uint32_t length, uint32_t *a, uint32_t *result); +void shift_left_1_array(uint32_t length, uint32_t *a, uint32_t *result); +void zero_array(uint32_t length, uint32_t *a); +void copy_array(uint32_t length, uint32_t *src, uint32_t *dst); +void debugArray(char *msg, uint32_t length, uint32_t *array); +void assertArrayEquals(uint32_t length, uint32_t *expected, uint32_t *actual); #endif /* BIGNUM_UINT32_T_H_ */ diff --git a/src/model/c/src/findn_test.c b/src/model/c/src/findn_test.c new file mode 100644 index 0000000..3c4a2fe --- /dev/null +++ b/src/model/c/src/findn_test.c @@ -0,0 +1,25 @@ +#include <stdio.h> +#include <stdint.h> + +uint32_t findN(uint32_t length, uint32_t *E) { + uint32_t n = -1; + for (int i = 0; i < 32 * length; i++) { + uint32_t ei_ = E[length - 1 - (i / 32)]; + uint32_t ei = (ei_ >> (i % 32)) & 1; + + printf("ei_ = 0x%08x, ei = 0x%08x\n", ei_, ei); + if (ei == 1) { + n = i; + } + } + return n + 1; +} + + +int main(void) { + uint32_t my_e[4] = {0x5a00aaaa, 0x555555, 0x80808080, 0x01010101}; + + printf("Result: %08d\n", findN(1, &my_e[0])); + + return 0; +} diff --git a/src/model/c/src/montgomery_array.c b/src/model/c/src/montgomery_array.c index 65e33e1..0ddc742 100644 --- a/src/model/c/src/montgomery_array.c +++ b/src/model/c/src/montgomery_array.c @@ -3,10 +3,10 @@ #include "bignum_uint32_t.h" #include "montgomery_array.h" -void mont_prod_array(int length, uint32_t *A, uint32_t *B, uint32_t *M, +void mont_prod_array(uint32_t length, uint32_t *A, uint32_t *B, uint32_t *M, uint32_t *temp, uint32_t *s) { zero_array(length, s); - for (int wordIndex = length - 1; wordIndex >= 0; wordIndex--) { + for (int32_t wordIndex = ((int32_t) length) - 1; wordIndex >= 0; wordIndex--) { for (int i = 0; i < 32; i++) { int b = (B[wordIndex] >> i) & 1; @@ -38,11 +38,11 @@ void mont_prod_array(int length, uint32_t *A, uint32_t *B, uint32_t *M, } } -void m_residue_2_2N_array(int length, int N, uint32_t *M, uint32_t *temp, +void m_residue_2_2N_array(uint32_t length, uint32_t N, uint32_t *M, uint32_t *temp, uint32_t *Nr) { zero_array(length, Nr); Nr[length - 1] = 1; // Nr = 1 == 2**(2N-2N) - for (int i = 0; i < 2 * N; i++) { + for (uint32_t i = 0; i < 2 * N; i++) { shift_left_1_array(length, Nr, Nr); modulus_array(length, Nr, M, temp, Nr); // debugArray(length, Nr); @@ -50,19 +50,19 @@ void m_residue_2_2N_array(int length, int N, uint32_t *M, uint32_t *temp, // Nr = (2 ** 2N) mod M } -int findN(int length, uint32_t *E) { - int n = -1; - for (int i = 0; i < 32 * length; i++) { +uint32_t findN(uint32_t length, uint32_t *E) { + uint32_t n = 0; + for (uint32_t i = 0; i < 32 * length; i++) { uint32_t ei_ = E[length - 1 - (i / 32)]; uint32_t ei = (ei_ >> (i % 32)) & 1; if (ei == 1) { - n = i; + n = i+1; } } - return n + 1; + return n; } -void mont_exp_array(int length, uint32_t *X, uint32_t *E, uint32_t *M, +void mont_exp_array(uint32_t length, uint32_t *X, uint32_t *E, uint32_t *M, uint32_t *Nr, uint32_t *P, uint32_t *ONE, uint32_t *temp, uint32_t *temp2, uint32_t *Z) { //debugArray("X ", length, X); @@ -70,7 +70,7 @@ void mont_exp_array(int length, uint32_t *X, uint32_t *E, uint32_t *M, //debugArray("M ", length, M); // 1. Nr := 2 ** 2N mod M - const int N = 32 * length; + const uint32_t N = 32 * length; m_residue_2_2N_array(length, N, M, temp, Nr); //debugArray("Nr", length, Nr); @@ -85,8 +85,8 @@ void mont_exp_array(int length, uint32_t *X, uint32_t *E, uint32_t *M, //debugArray("P0", length, P); // 4. for i = 0 to n-1 loop - const int n = findN(length, E); //loop optimization for low values of E. Not necessary. - for (int i = 0; i < n; i++) { + const uint32_t n = findN(length, E); //loop optimization for low values of E. Not necessary. + for (uint32_t i = 0; i < n; i++) { uint32_t ei_ = E[length - 1 - (i / 32)]; uint32_t ei = (ei_ >> (i % 32)) & 1; // 6. if (ei = 1) then Zi+1 := MontProd ( Zi, Pi, M) else Zi+1 := Zi @@ -109,3 +109,26 @@ void mont_exp_array(int length, uint32_t *X, uint32_t *E, uint32_t *M, } +void die(const char *c) { + printf("Fatal error: %s\n", c); + exit(1); +} + +void mod_exp_array(uint32_t length, uint32_t *X, uint32_t *E, uint32_t *M, uint32_t *Z) { + uint32_t *Nr = calloc(length, sizeof(uint32_t)); + uint32_t *P = calloc(length, sizeof(uint32_t)); + uint32_t *ONE = calloc(length, sizeof(uint32_t)); + uint32_t *temp = calloc(length, sizeof(uint32_t)); + uint32_t *temp2 = calloc(length, sizeof(uint32_t)); + if (Nr == NULL) die("calloc"); + if (P == NULL) die("calloc"); + if (ONE == NULL) die("calloc"); + if (temp == NULL) die("calloc"); + if (temp2 == NULL) die("calloc"); + mont_exp_array(length, X, E, M, Nr, P, ONE, temp, temp2, Z); + free(Nr); + free(P); + free(ONE); + free(temp); + free(temp2); +} diff --git a/src/model/c/src/montgomery_array.h b/src/model/c/src/montgomery_array.h index 2bafc56..82ef9de 100644 --- a/src/model/c/src/montgomery_array.h +++ b/src/model/c/src/montgomery_array.h @@ -8,9 +8,8 @@ #ifndef MONTGOMERY_ARRAY_H_ #define MONTGOMERY_ARRAY_H_ -void mont_prod_array(int length, uint32_t *A, uint32_t *B, uint32_t *M, +void mont_prod_array(uint32_t length, uint32_t *A, uint32_t *B, uint32_t *M, uint32_t *temp, uint32_t *s); -void mont_exp_array(int length, uint32_t *X, uint32_t *E, uint32_t *M, - uint32_t *Nr, uint32_t *P, uint32_t *ONE, uint32_t *temp, uint32_t *temp2, uint32_t *Z); +void mod_exp_array(uint32_t length, uint32_t *X, uint32_t *E, uint32_t *M, uint32_t *Z); #endif /* MONTGOMERY_ARRAY_H_ */ diff --git a/src/model/c/src/montgomery_array_test.c b/src/model/c/src/montgomery_array_test.c index ca76266..e865799 100644 --- a/src/model/c/src/montgomery_array_test.c +++ b/src/model/c/src/montgomery_array_test.c @@ -6,19 +6,6 @@ const uint32_t TEST_CONSTANT_PRIME_15_1 = 65537; const uint32_t TEST_CONSTANT_PRIME_31_1 = 2147483647u; // eighth Mersenne prime -void assertArrayEquals(int length, uint32_t *expected, uint32_t *actual) { - int equals = 1; - for (int i = 0; i < length; i++) - equals &= expected[i] == actual[i]; - printf("%s expected: [", equals ? "PASS" : "FAIL"); - for (int i = 0; i < length - 1; i++) - printf("%8x, ", expected[i]); - printf("%8x] actual: [ ", expected[length - 1]); - for (int i = 0; i < length - 1; i++) - printf("%8x, ", actual[i]); - printf("%8x]\n", actual[length - 1]); -} - void testShiftRight() { printf("=== Test shift right ===\n"); uint32_t a[] = { 0x01234567, 0x89abcdef }; @@ -168,23 +155,16 @@ void test_montgomery_modexp() { uint32_t M[] = { (1 << (89 - 64)) - 1, 0xffffffff, 0xffffffff }; //2^89-1 R. E. Powers uint32_t E[] = { 0, 0, (1 << 31) - 1 }; //Leonhard Euler - //temp variables - uint32_t Nr[] = { 0, 0, 0 }; - uint32_t ONE[] = { 0, 0, 0 }; - uint32_t P[] = { 0, 0, 0 }; - uint32_t temp[] = { 0, 0, 0 }; - uint32_t temp2[] = { 0, 0, 0 }; - - //output + uint32_t ONE[] = { 0, 0, 1 }; uint32_t Z[] = { 0, 0, 0 }; - mont_exp_array(3, ONE, ONE, M, Nr, P, ONE, temp, temp2, Z); + mod_exp_array(3, ONE, ONE, M, Z); assertArrayEquals(3, ONE, Z); - mont_exp_array(3, ONE, E, M, Nr, P, ONE, temp, temp2, Z); + mod_exp_array(3, ONE, E, M, Z); assertArrayEquals(3, ONE, Z); - mont_exp_array(3, X, E, M, Nr, P, ONE, temp, temp2, Z); + mod_exp_array(3, X, E, M, Z); uint32_t expected[] = { 0x0153db9b, 0x314b8066, 0x3462631f }; assertArrayEquals(3, expected, Z); } @@ -276,38 +256,6 @@ void test_modExp_4096bit_e65537() { 0x58fa13ff, 0x6cf0a5c2, 0x0ac24bf4, 0xd7901e58, 0x9b616f55, 0x2517443d, 0xb00a5613, 0x217b8957, 0x5a4ba6c4 }; - //temp variables - uint32_t Nr[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; - uint32_t ONE[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; - uint32_t P[] = { 0,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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; - //output uint32_t Z[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -316,7 +264,7 @@ void test_modExp_4096bit_e65537() { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; - mont_exp_array(129, X, E, M, Nr, P, ONE, temp, temp2, Z); + mod_exp_array(129, X, E, M, Z); assertArrayEquals(129, expected, Z); } @@ -491,68 +439,6 @@ void test_modExp_8192_e65537() { 0x5e6b03ab, 0x89fc8de9, 0x554365e2, 0x63f40c6c, 0x1246d838, 0x054a15e6, 0x9b8fdd15, 0x346ae73d }; - // temp variables - uint32_t Nr[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0,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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0 }; - // output uint32_t Z[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -567,7 +453,7 @@ void test_modExp_8192_e65537() { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; - mont_exp_array(257, X, E, M, Nr, P, ONE, temp, temp2, Z); + mod_exp_array(257, X, E, M, Z); assertArrayEquals(257, expected, Z); } @@ -784,29 +670,30 @@ void test_modExp_8192bit() { size_t length = 1 + ((8192 / 8) / sizeof(uint32_t)); - //temp variables - uint32_t *Nr = calloc(length, sizeof(uint32_t)); - uint32_t *ONE = calloc(length, sizeof(uint32_t)); - uint32_t *P = calloc(length, sizeof(uint32_t)); - uint32_t *temp = calloc(length, sizeof(uint32_t)); - uint32_t *temp2 = calloc(length, sizeof(uint32_t)); - //output uint32_t *Z = calloc(length, sizeof(uint32_t)); - mont_exp_array(257, X, E, M, Nr, P, ONE, temp, temp2, Z); + mod_exp_array(257, X, E, M, Z); assertArrayEquals(257, expected, Z); } -void montgomery_array_tests() { - test_montgomery_modexp(); - test_modExp_4096bit_e65537(); - test_modExp_8192_e65537(); - test_modExp_8192bit(); - testShiftRight(); - testAdd(); - testSub(); - test_montgomery_one_item_array(); - test_montgomery_modulus(); +void montgomery_array_tests(int bigtests) { + // Sub function tests. + testShiftRight(); + testAdd(); + testSub(); + test_montgomery_one_item_array(); + test_montgomery_modulus(); + + // modexp tests. + test_montgomery_modexp(); + + // Fairly big. + test_modExp_4096bit_e65537(); + test_modExp_8192_e65537(); + + // Bigtests. + if (bigtests) { + test_modExp_8192bit(); + } } - diff --git a/src/model/c/src/montgomery_array_test.h b/src/model/c/src/montgomery_array_test.h index 085d0ee..281a739 100644 --- a/src/model/c/src/montgomery_array_test.h +++ b/src/model/c/src/montgomery_array_test.h @@ -8,6 +8,6 @@ #ifndef MONTGOMERY_ARRAY_TEST_H_ #define MONTGOMERY_ARRAY_TEST_H_ -void montgomery_array_tests(); +void montgomery_array_tests(int bigtests); #endif /* MONTGOMERY_ARRAY_TEST_H_ */ diff --git a/src/model/c/src/simple_tests.c b/src/model/c/src/simple_tests.c new file mode 100644 index 0000000..402a8ac --- /dev/null +++ b/src/model/c/src/simple_tests.c @@ -0,0 +1,72 @@ +//====================================================================== +// +// simple_tests.h +// -------------- +// Header fil to export the simple tests of the modexp C model. +// +// +// Author: Joachim Strombergson +// Copyright (c) 2015, Assured AB +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or +// without modification, are permitted provided that the following +// conditions are met: +// +// 1. Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in +// the documentation and/or other materials provided with the +// distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +//====================================================================== + +#include <stdio.h> +#include <stdlib.h> +#include "montgomery_array.h" +#include "bignum_uint32_t.h" + +void simple_3_7_11(void) { + printf("=== Simple test with X = 3, E = 7 and M = 11 ===\n"); + uint32_t X[] = { 0x3 }; + uint32_t E[] = { 0x7 }; + uint32_t M[] = { 0xb }; + uint32_t expected[] = { 0x9 }; + uint32_t Z[] = { 0x00000000 }; + mod_exp_array(1, X, E, M, Z); + assertArrayEquals(1, expected, Z); +} + +void simple_251_251_257(void) { + printf("=== Simple test with X = 251, E = 251 and M = 257 ===\n"); + uint32_t X[] = { 0xfb }; + uint32_t E[] = { 0xfb }; + uint32_t M[] = { 0x101 }; + uint32_t expected[] = { 0xb7 }; + uint32_t Z[] = { 0x00000000 }; + mod_exp_array(1, X, E, M, Z); + assertArrayEquals(1, expected, Z); +} + +void simple_tests(void) { + simple_3_7_11(); + simple_251_251_257(); +} + +//====================================================================== +// EOF simple_tests.h +//====================================================================== diff --git a/src/model/c/src/simple_tests.h b/src/model/c/src/simple_tests.h new file mode 100644 index 0000000..23f6922 --- /dev/null +++ b/src/model/c/src/simple_tests.h @@ -0,0 +1,47 @@ +//====================================================================== +// +// simple_tests.h +// -------------- +// Header fil to export the simple tests of the modexp C model. +// +// +// Author: Joachim Strombergson +// Copyright (c) 2015, Assured AB +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or +// without modification, are permitted provided that the following +// conditions are met: +// +// 1. Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in +// the documentation and/or other materials provided with the +// distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +//====================================================================== + +#ifndef SIMPLE_TESTS_H_ +#define SIMPLE_TESTS_H_ + +void simple_tests(void); + +#endif // SIMPLE_TESTS_H_ + +//====================================================================== +// EOF simple_tests.h +//====================================================================== diff --git a/src/model/c/src/test b/src/model/c/src/test Binary files differnew file mode 100755 index 0000000..77faf32 --- /dev/null +++ b/src/model/c/src/test |