Initial commit

master
madrocketsci 1 year ago
commit 3ba560205c

@ -0,0 +1,11 @@
Copyright 2021 Aaron M. Schinder
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.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
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 HOLDER 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.

@ -0,0 +1,10 @@
Aaron M. Schinder
Jan 2021
C Large Integer Library 1
Implementation of large integer manipulation algorithms.
Refer to the Handbook of Applied Cryptography
copyright Aaron M. Schinder, 2021
Released under the MIT License

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -0,0 +1,530 @@
#!/usr/bin/python3
#Python3 compilation library
#Aaron M. Schinder
#29 Dec 2020
#
#Cleanup and refactor from 2017 python2 version compilation libraries
import os,sys,math,subprocess
#####################
#Directory Functions#
#####################
##flist - list all files in a given directory pth
##optional arguments:
# recurse - (T/F): Whether to recursively search for files in directory tree
# exts - (list): A list of file extensions to filter on
# normpath (T/F): whether to normalize path variables after
#filelist = flist(pth,**kwargs):
def flist(pth,**kwargs):
flst = []
if(not('recurse' in kwargs)):
recurse_ = False
else:
recurse_ = kwargs['recurse']
if(not('exts' in kwargs)):
filterexts_ = False
else:
filterexts_ = True
exts = kwargs['exts']
if(not('normpath' in kwargs)):
normpath_ = True
else:
normpath_ = kwargs['normpath']
if(not('linuxpath' in kwargs)):
linuxpath_ = False
else:
linuxpath_ = kwargs['linuxpath']
if(not('followlinks' in kwargs)):
followlinks_ = False
else:
followlinks_ = kwargs['followlinks']
dirlist = []
rawlist = os.listdir(pth)
for F in rawlist:
F2 = os.path.join(pth,F)
if(os.path.isdir(F2)):
b = (followlinks_) or ((not followlinks_) and not(os.path.islink(F2)))
if(b):
if((F2!=".")&(F2!="..")):
dirlist.append(F2)
elif(os.path.isfile(F2)):
flst.append(F2)
#Recurse through directories
if(recurse_):
for D in dirlist:
lst = flist(D,**kwargs)
for L in lst:
flst.append(L)
#Postprocess:
#Filter out all extensions except the selected ext list
if(filterexts_):
flst = filterexts(flst,exts)
#Normalize filename path according to os
if(normpath_):
flst2 = list(flst)
for I in range(0,len(flst2)):
flst[I] = os.path.normpath(flst2[I])
#If linuxpath, convert all \\ to /
#if(linuxpath_):
# flst2 = list(flst)
# for I in range(0,len(flst2)):
# flst[I] = linuxpath(flst2[I])
return flst
#Filters by extensions in a list of files
#flst = def filterexts(flst,exts):
def filterexts(flst,exts):
flst2 = []
if(isinstance(exts,str)):
exts = list([exts])
for F in flst:
b = False
for ext in exts:
if(ext[0]!='.'):
ext = '.'+ext
F2 = os.path.splitext(F)
if(len(F2)>=2):
ex = F2[1]
if(len(ex)>0):
if(ex[0]!='.'):
ex = '.'+ex
if(ex==ext):
b = True
if(b):
flst2.append(F)
return flst2
#Find a file fname, starting in pth and recursing
#Used for finding library files to link
def findfile(fname,pth,**kwargs):
fullfname = ""
flst = flist(pth,recurse=True)
for F in flst:
F2 = os.path.split(F)[1]
if(F2 == fname):
fullfname = F
return fullfname
#List to space-seperated-string
def list_to_sss(lst):
lout = ""
for I in range(0,len(lst)-1):
lout = lout + lst[I] + " "
if(len(lst)>0):
lout = lout + lst[len(lst)-1]
return lout
def strip_whitespace(strin):
strout = ""
I1 = -1
I2 = -1
for I in range(0,len(strin)):
if(strin[I]!=' ' and strin[I]!='\t' and strin[I]!='\r'and strin[I]!='\n'):
I1 = I
break
q = list(range(0,len(strin)))
q.reverse()
for I in q:
if(strin[I]!=' ' and strin[I]!='\t' and strin[I]!='\r'and strin[I]!='\n'):
I2 = I+1
break
if(I1>=0 and I2>=0):
strout = strin[I1:I2]
return strout
def sss_to_list(sss):
lout = []
l1 = sss.split(' ')
for l in l1:
l2 = strip_whitespace(l)
lout.append(l2)
return lout
def replaceext(fname,ext):
fname2 = ""
if(len(ext)>0):
if(ext[0]!='.'):
ext = '.'+ext
fname2 = os.path.splitext(fname)[0]+ext
else:
fname2 = os.path.splitext(fname)[0]
return fname2
def replaceexts(fnamelist,ext):
fname2list = []
for F in fnamelist:
F2 = replaceext(F,ext)
fname2list.append(F2)
return fname2list
# def except_contains_oldv(lst1,exc):
# lst2 = []
# for item in lst1:
# b = 1
# for item2 in exc:
# if(item.find(item2)>=0):
# b = 0
# break
# if(b==1):
# lst2.append(item)
# return lst2
#filenames must match
def except_contains(lst1,exc):
lst2 = []
for item in lst1:
b = 1
for item2 in exc:
fsplit = os.path.split(item)
fn = fsplit[len(fsplit)-1]
if(fn==item2):
b = 0
break
if(b==1):
lst2.append(item)
return lst2
##########################
##System Call Procedures##
##########################
def callproc(cmd, **kwargs):
if(not('logfile' in kwargs)):
use_lf = False
else:
logfile = kwargs['logfile']
if(logfile!=""):
fp = open(kwargs['logfile'],'a+')
use_lf = True
else:
use_lf = False
if(not('echo' in kwargs)):
echo = True
else:
echo = kwargs['echo']
if(echo):
print(cmd)
#encoding/deconding to/from bytes is necessary to use the subprocess command
#in python3.7
cmd2 = cmd.encode(encoding='utf-8')
proc = subprocess.Popen(cmd2,stderr = subprocess.STDOUT, stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
out = out.decode(encoding='utf-8')
if(echo):
print(out)
#print(err);
if(use_lf):
fp.writelines(cmd+'\n')
fp.writelines(out+'\n')
if(use_lf):
fp.close()
#######################################
##Compiler, Archive, and Linker Calls##
#######################################
def smartcompile(srcfile,objext='.o'):
mtsrc = os.path.getmtime(srcfile)
objfile = replaceext(srcfile,objext)
objexists = os.path.exists(objfile)
ret = True
if(objexists):
mtobj = os.path.getmtime(objfile)
if(mtobj>=mtsrc):
ret = False
return ret
#gnu-style compiler compile: Should work with gcc, g++, gfortran
def gs_compile(compiler,srcfile,**kwargs):
if(not('include' in kwargs)):
include = ''
else:
include = kwargs['include']
if(isinstance(include,list)):
include = list_to_sss(include)
if(not('flags' in kwargs)):
flags = ''
else:
flags = kwargs['flags']
if(isinstance(flags,list)):
flags = list_to_sss(flags)
if(not('objext' in kwargs)):
objext = '.o'
else:
objext = kwargs['objext']
if(not('srcfileflag' in kwargs)):
srcfileflag = '-c'
else:
srcfileflag = kwargs['srcfileflag']
if(not('outfileflag' in kwargs)):
outfileflag = '-o'
else:
outfileflag = kwargs['outfileflag']
if(not('logfile' in kwargs)):
logfile = ""
else:
logfile = kwargs['logfile']
if(not('smartcompile' in kwargs)):
_smartcompile = True
else:
_smartcompile = kwargs['smartcompile']
#Do I want to make this thing this general?
if(not(_smartcompile) or smartcompile(srcfile,objext)):
outfile = replaceext(srcfile,objext)
ln = compiler+" "+flags+" " + outfileflag+" "+outfile+" "+srcfileflag+" "+srcfile
ln = ln + " " + include
callproc(ln,echo=True,logfile=logfile)
return
def gs_compile_list(compiler,srclist,**kwargs):
for S in srclist:
gs_compile(compiler,S,**kwargs)
return
def gs_compile_all(compiler,srcdir,srcexts,**kwargs):
if(not('recurse' in kwargs)):
recurse = True
else:
recurse = kwargs['recurse']
srcfils = flist(srcdir,exts=srcexts,recurse=recurse)
for S in srcfils:
gs_compile(compiler,S,**kwargs)
return
def gs_link_all(linker,srcpath,target,**kwargs):
if(not('objext' in kwargs)):
objext = '.o'
else:
objext = kwargs['objext']
if(not('recurse' in kwargs)):
recurse = True
else:
recurse = kwargs['recurse']
objfils = flist(srcpath,exts=objext,recurse=recurse)
oflst = list_to_sss(objfils)
gs_link_list(linker,oflst,target,**kwargs)
return
def gs_link_list(linker,objlist,target,**kwargs):
if(not('objext' in kwargs)):
objext = '.o'
else:
objext = kwargs['objext']
if(not('libdir' in kwargs)):
libdir = ''
else:
libdir = kwargs['libdir']
if(not('staticlibs' in kwargs)):
staticlibs = ''
else:
staticlibs = kwargs['staticlibs']
if(not('libflags' in kwargs)):
libflags = ''
else:
libflags = kwargs['libflags']
if(not('linkerflags' in kwargs)):
linkerflags = ''
else:
linkerflags = kwargs['linkerflags']
if(not('recurse' in kwargs)):
recurse = True
else:
recurse = kwargs['recurse']
if(not('logfile' in kwargs)):
logfile = ''
else:
logfile = kwargs['logfile']
ln = linker+" -o "+target+" "+libdir
ln = ln+" "+objlist+" "+staticlibs+" "+libflags+" "+linkerflags
callproc(ln,logfile=logfile)
return
def ar_all(srcpath,arname,**kwargs):
if(not('recurse' in kwargs)):
recurse = True
else:
recurse = kwargs['recurse']
if(not('objext' in kwargs)):
objext = '.o'
else:
objext = kwargs['objext']
objlist = flist(srcpath,exts=objext,recurse=recurse)
ar_list(objlist,arname,**kwargs)
return
def ar_list(objlist,arname,**kwargs):
objlist2 = list_to_sss(objlist)
ln = "ar cr "+ arname+" "+objlist2
callproc(ln)
return
def ar_add_list(objlist,arname,**kwargs):
objlist2 = list_to_sss(objlist)
ln = "ar t "+arname+" "+objlist2
callproc(ln)
return
##############################
##Derived Compiler Functions##
##############################
def gcc_compile(srcfile,**kwargs):
compiler = 'gcc'
kwargs['objext'] = '.o'
#srcexts = ['.c']
gs_compile(compiler,srcfile,**kwargs)
return
def gcc_compile_all(srcdir,**kwargs):
compiler = 'gcc'
kwargs['objext'] = '.o'
srcexts = ['.c']
gs_compile_all(compiler,srcdir,srcexts,**kwargs)
return
def gcc_compile_list(srclist,**kwargs):
compiler = 'gcc'
kwargs['objext'] = '.o'
#srcexts = ['.c']
gs_compile_list(compiler,srclist,**kwargs)
return
def gpp_compile(srcfile,**kwargs):
compiler = 'g++'
kwargs['objext'] = '.o'
#srcexts = ['.c','.cpp']
gs_compile(compiler,srcfile,**kwargs)
return
def gpp_compile_all(srcdir,**kwargs):
compiler = 'g++'
kwargs['objext'] = '.o'
srcexts = ['.c','.cpp']
gs_compile_all(compiler,srcdir,srcexts,**kwargs)
return
def gpp_compile_list(srclist,**kwargs):
compiler = 'g++'
kwargs['objext'] = '.o'
#srcexts = ['.c','.cpp']
gs_compile_list(compiler,srclist,**kwargs)
return
def gfortran_compile(srcfile,**kwargs):
compiler = 'gfortran'
kwargs['objext'] = '.o'
#srcexts = ['.f','.f90','.f77']
gs_compile(compiler,srcfile,**kwargs)
return
def gfortran_compile_all(srcdir,**kwargs):
compiler = 'gfortran'
kwargs['objext'] = '.o'
srcexts = ['.f','.f90','.f77']
gs_compile_all(compiler,srcdir,srcexts,**kwargs)
return
def gfortran_compile_list(srclist,**kwargs):
compiler = 'gfortran'
kwargs['objext'] = '.o'
#srcexts = ['.f','.f90','.f77']
gs_compile_list(compiler,srclist,**kwargs)
return
def clang_compile(srcfile,**kwargs):
compiler = 'clang++'
kwargs['objext'] = '.o'
#srcexts = ['.c','.cpp']
gs_compile(compiler,srcfile,**kwargs)
return
def clang_compile_all(srcdir,**kwargs):
compiler = 'clang++'
kwargs['objext'] = '.o'
srcexts = ['.c','.cpp']
gs_compile_all(compiler,srcdir,srcexts,**kwargs)
return
def clang_compile_list(srclist,**kwargs):
compiler = 'clang++'
kwargs['objext'] = '.o'
#srcexts = ['.c','.cpp']
gs_compile_list(compiler,srclist,**kwargs)
return

@ -0,0 +1,43 @@
#!/usr/bin/python3
import os,sys,subprocess,math
from complib2 import *
import shutil
from distutils.dir_util import copy_tree as copy_tree #this version does overwrites
libname = 'amsclil1.linux64' #prefix static library name to generate
targetname = 'tests' #create this executable when compiling tests
commonincdir = "../../linux64/include"
commonlibdir = "../../linux64/lib"
localbindir = "./bin_linux64"
cc = 'gcc' #compiler
srcexts = ['.c','.cpp']
mainsrc = ['main.c'] #ignore these files when compiling the static library
kwargs = dict()
include = "-I./include -I{}".format(commonincdir)
kwargs['include'] = include
kwargs['flags'] = "-O3"
kwargs['libdir'] = "-L{} -L{}".format(localbindir,commonlibdir)
kwargs['libflags'] = "-l{}".format(libname)
kwargs['linkerflags'] = "-Wl,-rpath=."
kwargs['recurse'] = True
#find all source files, except the main project files
files = flist('./src',exts = srcexts, recurse=True)
files = except_contains(files,mainsrc)
objfiles = replaceexts(files,'.o')
objfiles_sss = list_to_sss(objfiles)
#compile all the source files in the list
gs_compile_list(cc,files,**kwargs)
#archive all the source files into a static library
ar_list(objfiles,'{}/lib{}.a'.format(localbindir,libname))
#Push any libraries to the common lib folder
shutil.copy('{}/lib{}.a'.format(localbindir,libname),commonlibdir)
#Copy include files to the common include folder
copy_tree('./include/',commonincdir+'/')

@ -0,0 +1,38 @@
#!/usr/bin/python3
import os,sys,subprocess,math
from complib2 import *
import shutil
from distutils.dir_util import copy_tree as copy_tree #this version does overwrites
libname = 'amsclil1.linux64' #prefix static library name to generate
targetname = 'tests' #create this executable when compiling tests
commonincdir = "../../linux64/include"
commonlibdir = "../../linux64/lib"
localbindir = "./bin_linux64"
cc = 'gcc' #compiler
srcexts = ['.c','.cpp']
mainsrc = ['main.c'] #ignore these files when compiling the static library
kwargs = dict()
include = "-I./include -I{}".format(commonincdir)
kwargs['include'] = include
kwargs['flags'] = "-O3" #-fPIC
kwargs['libdir'] = "-L{} -L{}".format(localbindir,commonlibdir)
kwargs['libflags'] = "-lamscutil1.linux64 -l{}".format(libname)
kwargs['linkerflags'] = "-Wl,-rpath=."
kwargs['recurse'] = True
#Pull required binary dynamic libraries to the bin folder
#shutil.copy('{}/libamsimg.dll.a'.format(commonlibdir),localbindir);
#shutil.copy('{}/libamsimg.dll'.format(commonlibdir),localbindir);
#shutil.copy('../../lib_winx64/glew32.dll','./bin_winx64');
#Designate source files for main test program
fsrc = ['./src/main.c']
fobj = replaceexts(fsrc,'.o')
#Compile test programs
gs_compile_list(cc,fsrc,**kwargs)
gs_link_list(cc,list_to_sss(fobj),'{}/{}'.format(localbindir,targetname),**kwargs)

@ -0,0 +1,44 @@
#!/usr/bin/python3
import os,sys,subprocess,math
from complib2 import *
import shutil
from distutils.dir_util import copy_tree as copy_tree #this version does overwrites
libname = 'amsclil1.mingw64' #prefix static library name to generate
targetname = 'tests.exe' #create this executable when compiling tests
commonincdir = "../../winx64/include"
commonlibdir = "../../winx64/lib"
localbindir = "./bin_winx64"
cc = 'x86_64-w64-mingw32-gcc' #compiler
srcexts = ['.c','.cpp']
mainsrc = ['main.c'] #ignore these files when compiling the static library
kwargs = dict()
include = "-I./include -I{}".format(commonincdir)
kwargs['include'] = include
kwargs['flags'] = "-fPIC -O3"
kwargs['libdir'] = "-L{} -L{}".format(localbindir,commonlibdir)
kwargs['libflags'] = "-l{}".format(libname)
kwargs['linkerflags'] = "-static -static-libgcc -Wl,-rpath=."
kwargs['recurse'] = True
#find all source files, except the main project files
files = flist('./src',exts = srcexts, recurse=True)
files = except_contains(files,mainsrc)
objfiles = replaceexts(files,'.o')
objfiles_sss = list_to_sss(objfiles)
#compile all the source files in the list
gs_compile_list(cc,files,**kwargs)
#archive all the source files into a static library
ar_list(objfiles,'{}/lib{}.a'.format(localbindir,libname))
#Push any libraries to the common lib folder
shutil.copy('{}/lib{}.a'.format(localbindir,libname),commonlibdir)
#Copy include files to the common include folder
copy_tree('./include/',commonincdir+'/')

@ -0,0 +1,38 @@
#!/usr/bin/python3
import os,sys,subprocess,math
from complib2 import *
import shutil
from distutils.dir_util import copy_tree as copy_tree #this version does overwrites
libname = 'amsclil1.mingw64' #prefix static library name to generate
targetname = 'tests.exe' #create this executable when compiling tests
commonincdir = "../../winx64/include"
commonlibdir = "../../winx64/lib"
localbindir = "./bin_winx64"
cc = 'x86_64-w64-mingw32-gcc' #compiler
srcexts = ['.c','.cpp']
mainsrc = ['main.c'] #ignore these files when compiling the static library
kwargs = dict()
include = "-I./include -I{}".format(commonincdir)
kwargs['include'] = include
kwargs['flags'] = "-fPIC -O3"
kwargs['libdir'] = "-L{} -L{}".format(localbindir,commonlibdir)
kwargs['libflags'] = "-lamscutil1.mingw64 -l{}".format(libname)
kwargs['linkerflags'] = "-static -static-libgcc -Wl,-rpath=."
kwargs['recurse'] = True
#Pull required binary dynamic libraries to the bin folder
#shutil.copy('{}/libamsimg.dll.a'.format(commonlibdir),localbindir);
#shutil.copy('{}/libamsimg.dll'.format(commonlibdir),localbindir);
#shutil.copy('../../lib_winx64/glew32.dll','./bin_winx64');
#Designate source files for main test program
fsrc = ['./src/main.c']
fobj = replaceexts(fsrc,'.o')
#Compile test programs
gs_compile_list(cc,fsrc,**kwargs)
gs_link_list(cc,list_to_sss(fobj),'{}/{}'.format(localbindir,targetname),**kwargs)

@ -0,0 +1,42 @@
#ifndef __AMSCLIL1_H__
#define __AMSCLIL1_H__
// Aaron M. Schinder
// Jan 2021
//
// C Large Integer Library 1
/////////////////////////////
//Standard Library Includes//
/////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdint.h>
#include <time.h>
#include <string.h>
//Other Libraries//
#include <amscutil1/amscutil1.h>
///////////////////////
// Component Headers //
///////////////////////
static int amsclil1_verbose = 0;
#include <amsclil1/amsclil1_operations.h>
#include <amsclil1/amsclil1_fbops.h>
#include <amsclil1/amsclil1_tests.h>
#include <amsclil1/amsclil1_ulint.h>
#include <amsclil1/amsclil1_hash.h>
#include <amsclil1/amsclil1_gutmannprng.h>
#endif

@ -0,0 +1,140 @@
#ifndef __AMSCLIL_FBOPS_H__
#define __AMSCLIL_FBOPS_H__
///////////////////////////////
//AMS C Large Integer Library//
//Fixed Buffer Functions //
///////////////////////////////
//Fixed buffer unsigned large integer operations
//
//These are needed, because preallocating the working memory
//will be much faster than dynamically allocating within iterative
//operations
//For the convenience of the caller, these can be wrapped in
//operations acting on resizeable buffers. For speed, these can
//be set up and called directly, with allocation/deallocation taking
//place at the start and end of the wrapping routine.
//Uint32 Operators
// 01234567890123456789012345678901 - MISRA 31 character identifier limit
//Single "digit" operations - these are sometimes reimplimented directly for speed
void amsclil1_ui32_add(uint32_t op1, uint32_t op2, uint32_t *res, uint32_t *car);
void amsclil1_ui32_sub(uint32_t op1, uint32_t op2, uint32_t *res, uint32_t *car);
void amsclil1_ui32_mult(uint32_t op1, uint32_t op2, uint32_t *res, uint32_t *car);
void amsclil1_ui32_div(uint32_t op1, uint32_t op2, uint32_t *res, uint32_t *rem);
//comparison:
//0: a and b are equal, 1: a is greater, 2: b is greater
int amsclil1_fb32_cmp(uint32_t* a, uint32_t* b, long blen);
int amsclil1_fb32_iszero(uint32_t* a, long blen);
void amsclil1_fb32_setzero(uint32_t *a, long blen);
//most significant index
long amsclil1_fb32_msi(uint32_t*a, long blen);
//large integer display functions
void amsclil1_fb32_liprint(uint32_t* a, long blen); //print hex representation
void amsclil1_fb16_liprint(uint16_t* a, long blen); //print hex representation
void amsclil1_fb32_libinprint(uint32_t* a, long blen); //print binary representation
//bit shift operations
//lshift: <<: *2^shift
//wrk[blen]
void amsclil1_fb32_shiftl(uint32_t* a, long shift, uint32_t* wrk, long blen);
void amsclil1_fb32_shiftr(uint32_t* a, long shift, uint32_t* wrk, long blen);
//Fixed Buffer Operators
void amsclil1_fb32_add(uint32_t *a, uint32_t *b, uint32_t *c, long blen);
void amsclil1_fb32_sub(uint32_t *a, uint32_t *b, uint32_t *c, long blen);
//schoolbook multiplication
// karatsuba multiplication may be needed for large enough numbers
void amsclil1_fb32_mult(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *wrk, long blen);
//Division using algorithm [HAC 14.20]
int amsclil1_fb32_div(uint32_t *a, uint32_t *b, uint32_t *res, uint32_t *rem, uint32_t* wrk, long blen);
//Implement!
void amsclil1_fb32_decimal(uint32_t *a, uint8_t *dd, uint32_t* wrk, int blen);
//Modular exponentiation [now using HAC14.42 Barrett Reduction]
void amsclil1_fb32_modpower(uint32_t *a, uint32_t *pw, uint32_t *md, uint32_t *res, uint32_t *wrk, uint32_t *stor, int blen);
//non-cryptographic random integer for testing
void amsclil1_fb32_ncrandint(uint32_t *a, long blen);
//Euler greatest common divisor algorithm
void amsclil1_fb32_eulergcd(uint32_t *a, uint32_t *b, uint32_t *gcd, uint32_t *wrk, long blen);
//Least Common Multiple
void amsclil1_fb32_lcm(uint32_t *a, uint32_t *b, uint32_t *lcm, uint32_t *wrk, long blen);
//Multiplicative Inverse Algorithm
int amsclil1_fb32_multinv(uint32_t *z, uint32_t *dom, uint32_t *zinv, uint32_t *wrk, int blen);
//Euler totient function
// for prime numbers it's just n-1
//Carmichels Totient Function
////////////////////////////////////////
//Miscellaneous and Internal Functions//
////////////////////////////////////////
//16 bit fixed buffer operations
//used in HAC14.20 division algorithm
void amsclil1_fb16_mult(uint16_t *a, uint16_t *b, uint16_t *c, long blen);
//[HAC 14.42]: Barrett reduction of modular arithmetic
//Precomputation of mu divisor
void amsclil1_fb32_barrettmu(uint32_t *m, uint32_t *mu, uint32_t *wrk, long K, long blen);
//Barrett Reduced Modulus
//y = mod(x,m)
void amsclil1_fb32_barrettmod(uint32_t *x, uint32_t *m, uint32_t *y,
uint32_t *mu, uint32_t *wrk, long K, long Kx, long blen);
//Classical Fast Modular Exponentiation
void amsclil1_fb32_modpower_cls(uint32_t *a, uint32_t *pw, uint32_t *md, uint32_t *res, uint32_t *wrk, uint32_t *stor, int blen);
//Fast Modular Exponentiation with Barrett Reduction
void amsclil1_fb32_modpower_bar(uint32_t *a, uint32_t *pw, uint32_t *md, uint32_t *res, uint32_t *wrk, uint32_t *stor, int blen);
//Shifted Operations
//bit-shifted reads and writes
//don't move the array in memory, move the read/write
//perhaps it will save time within the division operation
int amsclil1_fb32_readshift(uint32_t* arr, long ind, long shift, uint32_t* val, long blen);
int amsclil1_fb32_writeshift(uint32_t* arr, long ind, long shift, uint32_t* val, long blen);
//Shifted Large Integer Display Function
void amsclil1_fb32_shiftliprint(uint32_t* a, long shift, long blen); //print hex representation
//Shifted integer, most significant index
long amsclil1_fb32_shiftmsi(uint32_t *a, long shift, long blen);
//Shifted comparison
int amsclil1_fb32_shiftcmp(uint32_t *a, uint32_t *b, long bshift, long blen);
//Shifted subtraction
void amsclil1_fb32_shiftsub(uint32_t *a, uint32_t *b, long bshift, uint32_t *c, long blen);
//internal digit shift subtraction
//this is supposed to be something like c = a - b*base^diff
void amsclil1_fb16_intlssub(uint16_t *a, uint16_t *b, long bshft, uint16_t *c, long blen);
//internal digit shift comparison
//this is supposed to be a <=> b*base^diff
int amsclil1_fb16_intlscmp(uint16_t *a, uint16_t *b, long bshft, long blen);
void amsclil1_fb16_intlmultsing(uint16_t *a, uint16_t b, long blen);
#endif

@ -0,0 +1,33 @@
#ifndef __AMSCLIL1_GUTMANNPRNG_H__
#define __AMSCLIL1_GUTMANNPRNG_H__
typedef struct amsclil1_gprng_state
{
int randpool_length;
uint8_t *randpool;
int write_pointer;
int cycle_pointer;
} amsclil1_gprng_state;
int amsclil1_gprng_state_new(amsclil1_gprng_state **s);
void amsclil1_gprng_state_delete(amsclil1_gprng_state **s);
//sets new random pool length (default is 640 bytes)
int amsclil1_gprng_state_resize(amsclil1_gprng_state *s, int N);
void amsclil1_gprng_add_byte(amsclil1_gprng_state *s, uint8_t byte);
void amsclil1_gprng_add_bytes(amsclil1_gprng_state *s, uint8_t *bytes, int N);
void amsclil1_gprng_mix_once(amsclil1_gprng_state *s);
void amsclil1_gprng_mix(amsclil1_gprng_state *s);
//Original gprng hash is MD5, set to take 84 bytes in, and provide 16 bytes out
void amsclil1_gprng_hash(uint8_t *bytes_in, int Nin, uint8_t *bytes_out);
//: A randompool of 640 bytes (5120 bits) and a hash algorithm
// outputing 16 bytes (128) bits, requires 40 cycles, not 30, to
// completely cover the random pool.
#endif

@ -0,0 +1,17 @@
#ifndef __AMSCLIL1_HASH_H__
#define __AMSCLIL1_HASH_H__
//////////////////
//Hash Functions//
//////////////////
//Low level components of many cryptographic operations
//MD5 Hash Algorithm
//Input: Some number of bytes in
//Output: 16 bytes (128 bits) digest out
int amsclil1_md5_hash(const uint8_t *bytes_in, size_t Nin, uint8_t *bytes16_out);
#endif

@ -0,0 +1,7 @@
#ifndef __AMSCLIL_OPERATIONS_H__
#define __AMSCLIL_OPERATIONS_H__
#endif

@ -0,0 +1,39 @@
#ifndef __AMSCLIL1_TESTS_H__
#define __AMSCLIL1_TESTS_H__
void test_fbops_printcmp();
void test_fbops_shiftread();
void test_fbops_shiftwrite();
void test_fbops_lshiftrshift();
void test_modtdiv();
void test_addsub();
void test_mult();
void test_div();
void test_modpower();
void test_mult_time();
void test_div_time();
//non_cryptographic test randomness
void amsclil1_fb32_testrandom(uint32_t* a,long blen);
void test_barrettmod();
void barret_modpower_test();
void barrett_mod_fuzztest();
void modpower_timetests();
void test_amsclil1_fb32_eulergcd();
void test_fb16_comparison_sub();
void test_div_stress_test();
void test_div_stress_test2();
void test_ui64bitshifts();
void amsclil1_test_fb32_lcm1();
#endif

@ -0,0 +1,82 @@
#ifndef __AMSCLIL1_ULINT_HPP__
#define __AMSCLIL1_ULINT_HPP__
///////////////////////////////////
// Unsigned Large Integer Object //
///////////////////////////////////
// This is an unsigned large integer object based on 32-bit words. 32 bits seems to be
// the fastest width for the underlying data.
typedef struct amsclil1_ulint
{
int N;
uint32_t *data;
} amsclil1_ulint;
// Container Class Routines
int amsclil1_ulint_new(amsclil1_ulint **pnt);
void amsclil1_ulint_delete(amsclil1_ulint **pnt);
int amsclil1_ulint_init(amsclil1_ulint *pnt);
void amsclil1_ulint_cleanup(amsclil1_ulint *pnt);
//resize integer buffer to new size _N
int amsclil1_ulint_resize(amsclil1_ulint *pnt, int _N);
//If _N is greater than current size, expand to larger size, otherwise do nothing
int amsclil1_ulint_resizetomin(amsclil1_ulint *pnt, int _N);
//Most significant index of number
int amsclil1_ulint_msindex(amsclil1_ulint *pnt);
//shrink memory to most significant index
int amsclil1_ulint_shrinktofit(amsclil1_ulint *pnt);
//sets all data to 0
void amsclil1_ulint_clear(amsclil1_ulint *pnt);
//copies src data to dest. resizes dest if necessary, otherwise does not.
int amsclil1_ulint_copy(amsclil1_ulint *dest, amsclil1_ulint *src);
// Display
void amsclil1_ulint_printhex(amsclil1_ulint *pnt);
void amsclil1_ulint_printbits(amsclil1_ulint *pnt);
// Conversion To/From amscutil bytebuffer
// Comparators
// Unsigned Large Integer Comparison Operator:
// 0 - a==b
// 1 - a>b
// 2 - a<b
int amsclil1_ulint_cmp(amsclil1_ulint *a, amsclil1_ulint *b);
int amsclil1_ulint_iszero(amsclil1_ulint *a);
// Math Operations
// These operations may be slower than the preallocated fixed buffer operations
// They are included for simplicity in constructing and testing algorithms.
// The necessary sizes and working memory is preallocated during execution.
// Inputs are copied internally, so output can point to the same integer as input.
int amsclil1_ulint_add(amsclil1_ulint *a, amsclil1_ulint *b, amsclil1_ulint *c);
int amsclil1_ulint_sub(amsclil1_ulint *a, amsclil1_ulint *b, amsclil1_ulint *c);
int amsclil1_ulint_mult(amsclil1_ulint *a, amsclil1_ulint *b, amsclil1_ulint *c);
//int amsclil1_ulint_div(amsclil1_ulint *a, amsclil1_ulint *b, amsclil1_ulint *q, amsclil1_ulint *r);
//modpower
//rep_modpower
//gcd
//lcm
//Tests
void amsclil1_ulint_tests1();
#endif

@ -0,0 +1,17 @@
#!/usr/bin/python3
import os,sys,math;
from compscripts.complib2 import *;
os.system('python3 ./compscripts/linux64.makelib.py')
os.system('python3 ./compscripts/linux64.maketest.py')
obj_list = flist('./',recurse=True,exts=['.o'])
for o in obj_list:
os.system('rm {}'.format(o))
os.chdir('./bin_linux64')
callproc('./tests')
os.chdir('..')

@ -0,0 +1,17 @@
#!/usr/bin/python3
import os,sys,math;
from compscripts.complib2 import *;
os.system('python3 ./compscripts/mingwcc.makelib.py')
os.system('python3 ./compscripts/mingwcc.maketest.py')
obj_list = flist('./',recurse=True,exts=['.o'])
for o in obj_list:
os.system('rm {}'.format(o))
os.chdir('./bin_winx64')
callproc('wine ./tests.exe')
os.chdir('..')

@ -0,0 +1,299 @@
#include <amsclil1/amsclil1.h>
int amsclil1_fb32_cmp(uint32_t* a, uint32_t* b, long blen)
{
int ret = 0;
long I;
for(I=blen-1;I>=0;I--)
{
if(a[I]>b[I])
{
ret = 1;
break;
}
if(b[I]>a[I])
{
ret = 2;
break;
}
}
return ret;
}
int amsclil1_fb32_iszero(uint32_t* a, long blen)
{
int ret = 1;
long I;
for(I=0;I<blen;I++)
{
if(a[I]!=0)
{
ret = 0;
break;
}
}
return ret;
}
void amsclil1_fb32_liprint(uint32_t* a, long blen)
{
long I;
printf("0x");
for(I=blen-1;I>=0;I--)
{
printf("%08x",a[I]);
}
return;
}
void amsclil1_fb16_liprint(uint16_t* a, long blen)
{
long I;
printf("0x");
for(I=blen-1;I>=0;I--)
{
printf("%04x",a[I]);
}
return;
}
void amsclil1_fb32_libinprint(uint32_t* a, long blen)
{
long I;
int J;
uint32_t bit;
printf("0b:");
for(I=blen-1;I>=0;I--)
{
for(J=32-1;J>=0;J--)
{
bit = (a[I] & (1<<J))>>J;
printf("%d",bit);
}
if(I!=0) printf(":");
}
}
//bit-shifted reads and writes
//don't move the array in memory, move the read/write
//perhaps it will save time within the division operation
int amsclil1_fb32_readshift(uint32_t* arr, long ind, long shift, uint32_t* val, long blen)
{
int ret = 0;
//long Itop = ind + amsc_ltdiv(-shift,32)+1;
//long Ibot = ind + amsc_ltdiv(-shift,32);
long Itop = amsc_ltdiv((ind+1)*32-shift,32);
long Ibot = amsc_ltdiv(ind*32-shift,32);
int smin = amsc_lmod(-shift,32);
//printf("\ndebug: %ld, %ld, %ld, %ld, %d\n",ind, shift, Itop, Ibot, smin);
uint64_t w1,w2;
*val = 0;
if(Itop>=0 && Itop<blen)
{
w1 = arr[Itop];
w1 = ((w1 << 32) >> smin); //why does this 32 bit shift work?
*val = *val | (uint32_t) w1;
}
if(Ibot>=0 && Ibot<blen)
{
w1 = arr[Ibot];
w1 = w1 >> smin;
*val = *val | (uint32_t) w1;
}
return ret;
}
int amsclil1_fb32_writeshift(uint32_t* arr, long ind, long shift, uint32_t* val, long blen)
{
int ret = 0;
long Itop = amsc_ltdiv((ind+1)*32+shift,32);
long Ibot = amsc_ltdiv(ind*32+shift,32);
int smin = amsc_lmod(shift,32);
int sminc = 32 - smin;
uint64_t w1,w2;
//weird problems with shift 32, even shifting uint64_t data types
//while theres a lot of RTFM garbage online, and everyone will roast you
//for shifting a type such as uint32_t 32 bits, no one will admit that
//shifting uint64_t by 32 bits, which by the standard should be defined
//behavior, fails on x86 processors. Some register is 32 bits wide, and
//so this doesn't work.
if(Itop>=0 && Itop<blen)
{
if(smin>0) w1 = (*val) >> (32-smin);
else w1 = 0;
//w2 = (arr[Itop] >> smin) << smin;
w2 = (uint64_t) arr[Itop];
w2 = w2 & (0x00000000ffffffff << smin);
arr[Itop] = (uint32_t) w1 | (uint32_t) w2;
}
if(Ibot>=0 && Ibot<blen)
{
w1 = (*val) << smin;
if(smin<32)
{
w2 = (uint64_t) arr[Ibot];
w2 = w2 & (0xffffffffffffffff - (0x00000000ffffffff << smin));
}
else w2 = (uint64_t) arr[Ibot];
arr[Ibot] = (uint32_t) w1 | (uint32_t) w2;
}
return ret;
}
//bit shift operations
//lshift: <<: *2^shift
//wrk[blen]
void amsclil1_fb32_shiftl(uint32_t* a, long shift, uint32_t* wrk, long blen)
{
long smaj;
int smin;
long I;
uint64_t w1;
if(shift<0)
{
amsclil1_fb32_shiftr(a,-shift,wrk,blen);
}
else
{
smaj = amsc_ltdiv(shift,32);
smin = amsc_lmod(shift,32);
amsc_ui32_memcpy(wrk,a,blen);
amsc_ui32_memset(a,0,blen);
for(I=0;I<blen;I++)
{
if(I-smaj>=0)
{
w1 = (uint64_t) wrk[I-smaj];
w1 = (w1 << smin) & 0x00000000ffffffff;
a[I] = a[I] | (uint32_t) w1;
}
if(I-smaj-1>=0)
{
w1 = (uint64_t) wrk[I-smaj-1];
w1 = ((w1 << smin) & 0xffffffff00000000);
w1 = (w1 >> 16) >> 16;
a[I] = a[I] | (uint32_t) w1;
}
}
}
}
void amsclil1_fb32_shiftr(uint32_t* a, long shift, uint32_t* wrk, long blen)
{
long smaj;
int smin;
long I;
uint64_t w1;
if(shift<0)
{
amsclil1_fb32_shiftl(a,-shift,wrk,blen);
}
else
{
smaj = amsc_ltdiv(shift,32);
smin = amsc_lmod(shift,32);
amsc_ui32_memcpy(wrk,a,blen);
amsc_ui32_memset(a,0,blen);
for(I=0;I<blen;I++)
{
if(I+smaj<blen)
{
w1 = (uint64_t) wrk[I+smaj];
w1 = (w1 >> smin);
a[I] = a[I] | (uint32_t) w1;
}
if(I+smaj+1<blen)
{
w1 = (uint64_t) wrk[I+smaj+1];
w1 = ((w1 << 16) << 16) >> smin;
a[I] = a[I] | (uint32_t) w1;
}
}
}
return;
}
void amsclil1_ui32_add(uint32_t op1, uint32_t op2, uint32_t *res, uint32_t *car)
{
uint64_t a,b,c;
a = (uint64_t) op1;
b = (uint64_t) op2;
c = a+b;
*res = (uint32_t) c;
*car = (uint32_t) ((c>>16)>>16);
return;
}
void amsclil1_ui32_sub(uint32_t op1, uint32_t op2, uint32_t *res, uint32_t *car)
{
uint64_t a;
if(op1>=op2)
{
*res = op1-op2;
*car = 0;
}
else
{
a = (0x0000000100000000 + (uint64_t) op1) - (uint64_t) op2;
*res=(uint32_t) a;
*car = 1;
}
return;
}
void amsclil1_ui32_mult(uint32_t op1, uint32_t op2, uint32_t *res, uint32_t *car)
{
uint64_t a,b,c;
a = (uint64_t) op1;
b = (uint64_t) op2;
c = a*b;
*res = (uint32_t) c;
*car = (uint32_t) ((c>>16)>>16);
return;
}
void amsclil1_ui32_div(uint32_t op1, uint32_t op2, uint32_t *res, uint32_t *rem)
{
if(op2>op1)
{
*res = 0;
*rem = op1;
}
else
{
*res = op1 / op2;
*rem = op1 % op2;
}
return;
}
long amsclil1_fb32_msi(uint32_t*a, long blen)
{
long ret = 0;
long I;
for(I=blen-1;I>=0;I--)
{
if(a[I] != 0)
{
ret = I;
break;
}
}
return ret;
}

File diff suppressed because it is too large Load Diff

@ -0,0 +1,300 @@
#include <amsclil1/amsclil1.h>
//precompute mu = b^(2*k)/m for Barrett Reduction algorithm [HAC 14.42]
//m[blen] - the modulus
//mu[2*(blen+1)] - precomputed factor for Barrett Reduction
//wrk[14*blen+19] - division working memory
//K - most significant index of m + 1 (at most blen)
//blen - length of m buffer
void amsclil1_fb32_barrettmu(uint32_t *m, uint32_t *mu, uint32_t *wrk, long K, long blen)
{
long I;
//intl buffer lengths 2*(blen+1)
//division working length 5*(2*(blen+1)+1): 10*blen+15
//total work length: 10*blen+15+4*(blen+1) 14*blen+19
const size_t divlen = 10*blen+15;
const size_t intblen = 2*(blen+1);
uint32_t *b2 = &(wrk[divlen+0*intblen]);
uint32_t *m2 = &(wrk[divlen+1*intblen]);
uint32_t *mu2 = &(wrk[divlen+2*intblen]);
uint32_t *rm2 = &(wrk[divlen+3*intblen]);
for(I=0;I<blen;I++)
{
b2[I] = 0;
m2[I] = m[I];
mu2[I] = 0;
}
for(I=blen;I<2*(blen+1);I++)
{
b2[I] = 0;
m2[I] = 0;
mu2[I] = 0;
}
b2[2*K] = 1;
//amsclil1_fb32_div(b2,m2,mu2,rm2,wrk,2*K+1);
amsclil1_fb32_div(b2,m2,mu2,rm2,wrk,2*(blen+1));
//if(amsclil1_fb32_iszero(mu2,2*(blen+1)))
//{
// printf("debug:mu="); amsclil1_fb32_liprint(mu2,2*(blen+1)); printf("\n");
// printf("debug:b2="); amsclil1_fb32_liprint(b2,2*(blen+1)); printf("\n");
// printf("debug:m2="); amsclil1_fb32_liprint(m2,2*(blen+1)); printf("\n");
//}
//printf("debug:mu="); amsclil1_fb32_liprint(mu2,2*K+2); printf("\n");
for(I=0;I<2*(blen+1);I++)
{
mu[I] = mu2[I];
}
return;
}
//compute y = mod(x,m) using Barrett Reduction Algorithm [HAC 14.42]
//x[blen] - x has (according to the algorithm) an msi of 2*k-1
// here, supply only k-1 digits to keep buffers even
//m[blen] - m has msi of (K-1) which must be <- blen-2
//mu[2*(blen+1)] - precompute mu=(1<<(2*32*blen) / m)
//y[blen] - output
//wrk[33*(blen+1)]
//K - provide most significant index of m +1
//Kx - provide most significant index of x +1
//blen - length of buffers
void amsclil1_fb32_barrettmod(uint32_t *x, uint32_t *m, uint32_t *y,
uint32_t *mu, uint32_t *wrk, long K, long Kx, long blen)
{
long I;
const size_t divlen = 5*(blen+1); //length of working buffer for division algorithm
//offset for beginning of mod buffer
const size_t intblen = 4*(blen+1);
//wrk size: 5*blen+5 + 7*(4*blen+4): 33*(blen+1)
int cmp;
uint32_t* bswap = NULL;
uint32_t* q1 = &(wrk[divlen+0*intblen]);
uint32_t* q2 = &(wrk[divlen+1*intblen]);
uint32_t* q3 = &(wrk[divlen+2*intblen]);
uint32_t* r1 = &(wrk[divlen+3*intblen]);
uint32_t* r2 = &(wrk[divlen+4*intblen]);
uint32_t* r3 = &(wrk[divlen+5*intblen]);
uint32_t* mi = &(wrk[divlen+6*intblen]);
long dbg;
//handle mod 1 and mod 0 cases fast
if(K==1)
{
if(m[0]==0 || m[0]==1)
{
for(I=0;I<blen;I++)
{
y[I] = 0;
}
return;
}
}
for(I=0;I<intblen;I++)
{
q1[I] = 0;
q2[I] = 0; //mult takes care of that
q3[I] = 0;
r1[I] = 0;
r2[I] = 0;
r3[I] = 0;
mi[I] = 0;
}
for(I=0;I<blen;I++) //copy m to larger internal buffer
{
mi[I] = m[I];
}
//1. q1 = x / b^(K-1)
for(I=Kx-1;I>=(K-1);I--)
//for(I=blen-1;I>=(K-1);I--)
{
q1[I-(K-1)] = x[I];
}
//2. q2 = q1*mu
amsclil1_fb32_mult(mu,q1,q2,wrk,2*(blen+1));
//3. q3 = q2/ b^(K+1)
for(I=intblen-1;I>=K+1;I--)
{
q3[I-(K+1)] = q2[I];
}
//4. r1 = x mod b^(k+1)
for(I=0;I<(K+1) && I<Kx;I++)
{
r1[I] = x[I];
}
//5. r2 = (q3 * m) mod b^(K+1)
amsclil1_fb32_mult(q3,mi,r2,wrk,2*(blen+1));
for(I=K+1;I<intblen;I++)
{
r2[I] = 0;
}
//printf("debug: r1="); amsclil1_fb32_liprint(r1,2*(blen+1)); printf("\n");
//printf("debug: r2="); amsclil1_fb32_liprint(r2,2*(blen+1)); printf("\n");
//printf("debug: md="); amsclil1_fb32_liprint(mi,2*(blen+1)); printf("\n");
//6. if(r1<r2) r3 = r3 + b^(K+1)
cmp = amsclil1_fb32_cmp(r1,r2,2*(blen+1));
if(cmp==2)
{
r3[K+1] = 1;
}
//r3 = r3-r2+r1
amsclil1_fb32_add(r3,r1,q1,2*(blen+1));
amsclil1_fb32_sub(q1,r2,r3,2*(blen+1));
//amsclil1_fb32_sub(r3,r2,q1,2*(blen+1));
//amsclil1_fb32_add(q1,r1,r3,2*(blen+1));
//printf("debug: r3="); amsclil1_fb32_liprint(r3,2*(blen+1)); printf("\n");
//while r3>=m r3 = r3-m
cmp = amsclil1_fb32_cmp(r3,mi,2*(blen+1));
//dbg = 0;
while(cmp!=2)
{
amsclil1_fb32_sub(r3,mi,q2,2*(blen+1));
bswap = r3; r3 = q2; q2 = bswap;
cmp = amsclil1_fb32_cmp(r3,mi,2*(blen+1));
//dbg++;
}
for(I=0;I<blen;I++)
{
y[I] = r3[I];
}
//if(dbg>2)
//{
// printf("debug: %ld iters, should be at most 2\n",dbg);
// printf("debug: x="); amsclil1_fb32_liprint(x,blen); printf("\n");
// printf("debug: m="); amsclil1_fb32_liprint(m,blen); printf("\n");
// printf("debug: y="); amsclil1_fb32_liprint(y,blen); printf("\n");
// printf("debug: mu="); amsclil1_fb32_liprint(mu,2*(blen+1)); printf("\n");
// printf("debug: r1="); amsclil1_fb32_liprint(r1,2*(blen+1)); printf("\n");
// printf("debug: r2="); amsclil1_fb32_liprint(r2,2*(blen+1)); printf("\n");
//}
return;
}
//Fast Modular Exponentiation using Barrett Reduction
//calculates mod(a^pw, md)
//a[blen] - base buffer
//pw[blen] - power
//md[blen] - modulus
//wrk[66*(blen+1)]
//stor[28*(blen+1)]
//blen - base buffer length
void amsclil1_fb32_modpower_bar(uint32_t *a, uint32_t *pw, uint32_t *md, uint32_t *res, uint32_t *wrk, uint32_t *stor, int blen)
{
long I;
long msipw, msia;
uint32_t bit;
long smaj;
int smin;
long Kmd,Kacc,Kbm;
const size_t intblen = 4*(blen+1);
uint32_t* bswap = NULL;
uint32_t* bm0 = &(stor[0]);
uint32_t* bm1 = &(stor[1*intblen]);
uint32_t* ra = &(stor[2*intblen]);
uint32_t* rb = &(stor[3*intblen]); //unused?
uint32_t* acc = &(stor[4*intblen]);
uint32_t* mdb = &(stor[5*intblen]);
uint32_t* mu = &(stor[6*intblen]);
msipw = amsclil1_fb32_msi(pw,blen);
msia = amsclil1_fb32_msi(a,blen);
Kmd = amsclil1_fb32_msi(md,blen)+1;
for(I=0;I<blen;I++)
{
mdb[I] = md[I]; //init mdb to md
acc[I] = 0;
bm0[I] = a[I]; //init bm0 to a
mu[I] = 0;
}
for(I=blen;I<intblen;I++)
{
mdb[I] = 0;
acc[I] = 0;
bm0[I] = 0;
mu[I] = 0;
}
acc[0] = 1; //init acc to 1
//precompute mu
amsclil1_fb32_barrettmu(mdb, mu, wrk, Kmd, blen);
//printf("debug: mu="); amsclil1_fb32_liprint(mu,2*blen); printf("\n");
for(I=0;I<32*(msipw+1);I++)
{
smaj = I/32;
smin = I%32;
bit = (pw[smaj] & (1<<smin))>>smin;
if(bit!=0)
{
amsclil1_fb32_mult(acc,bm0,ra,wrk,blen);
bswap = acc; acc = ra; ra = bswap; //swap(acc,ra)
Kacc = amsclil1_fb32_msi(acc,2*blen)+1;
//printf("debug: acc="); amsclil1_fb32_liprint(acc,2*blen); printf("\n");
//acc - [2*blen] buffer now
//so everything fed to barrettmod needs to be sized for 2*blen?
amsclil1_fb32_barrettmod(acc,mdb,ra,mu,wrk,Kmd,Kacc,2*blen);
bswap = acc; acc = ra; ra = bswap; //swap(acc,ra)
//printf("debug: acc="); amsclil1_fb32_liprint(acc,2*blen); printf("\n");
}
//printf("debug: acc="); amsclil1_fb32_liprint(acc,2*blen); printf("\n");
//bm0 = (bm0**2) % md
amsclil1_fb32_mult(bm0,bm0,bm1,wrk,blen);
Kbm = amsclil1_fb32_msi(bm1,2*blen)+1;
amsclil1_fb32_barrettmod(bm1,mdb,bm0,mu,wrk,Kmd,Kbm,2*blen);
//amsclil1_fb32_div(bm1,mdb,ra,bm0,wrk,2*blen);
}
return;
}
void amsclil1_fb32_ncrandint(uint32_t *a, long blen)
{
long I;
uint32_t r;
for(I=0;I<blen;I++)
{
r = rand();
a[I] = r;
}
return;
}

@ -0,0 +1,256 @@
#include <amsclil1/amsclil1.h>
//Euler greatest common divisor algorithm
//a [blen]
//b [blen]
//gcd [blen]
//use division algorithm for modulus - no need for modpower here
//wrk [9*(blen+1)] ....
void amsclil1_fb32_eulergcd(uint32_t *a, uint32_t *b, uint32_t *gcd, uint32_t *wrk, long blen)
{
long I;
uint32_t *ai,*bi,*ci,*tmp,*ri,*wrki;
int dbg;
ai = &(wrk[0*(blen+1)]); //[blen+1]
bi = &(wrk[1*(blen+1)]); //[blen+1]
ci = &(wrk[2*(blen+1)]); //[blen+1]
ri = &(wrk[3*(blen+1)]); //gcd; //[blen]
wrki = &(wrk[4*(blen+1)]); //5*(blen+1)
//total 9*(blen+1)
for(I=0;I<blen+1;I++)
{
ci[I] = 0;
}
// if a>b
if(amsclil1_fb32_cmp(a,b,blen)!=2)
{
//copy a to ai, b to bi
for(I=0;I<blen;I++)
{
ai[I] = a[I];
bi[I] = b[I];
ri[I] = 0;
}
}
else
{
//copy a to bi, b to ai
for(I=0;I<blen;I++)
{
bi[I] = a[I];
ai[I] = b[I];
ri[I] = 0;
}
}
ai[blen] = 0;
bi[blen] = 0;
ri[blen] = 0;
dbg = 0;
while(amsclil1_fb32_iszero(bi,blen)==0)
{
//ri = ai mod bi
//printf("%d pre div.\n", dbg);
//printf("a="); amsclil1_fb32_liprint(ai,blen); printf("\n");
//printf("b="); amsclil1_fb32_liprint(bi,blen); printf("\n");
amsclil1_fb32_div(ai,bi,ci,ri,wrki,blen);
//printf("post div.\n");
//printf("c="); amsclil1_fb32_liprint(ci,blen); printf("\n");
//printf("r="); amsclil1_fb32_liprint(ri,blen); printf("\n");
//printf("\n");
//a = b, b=r
tmp = ai;
ai = bi;
bi = ri;
ri = tmp;
dbg++;
}
//printf("post loop");
//gcd = a
for(I=0;I<blen;I++)
{
gcd[I] = ai[I];
}
return;
}
//Least Common Multiple
//
// returns lcm = lcm(a,b)
// lcm(0,0) = 0 (special case)
//
//a [blen]
//b [blen]
//lcm [2*blen]
//wrk [14*(blen+1)]
//blen - max number of digits in a and b
//
void amsclil1_fb32_lcm(uint32_t *a, uint32_t *b, uint32_t *lcm, uint32_t *wrk, long blen)
{
uint32_t *aint; //internal smallest input
uint32_t *bint; //internal largest input
uint32_t *gcd = &(wrk[0]); //2*blen
uint32_t *q = &(wrk[2*blen]); //1*blen
uint32_t *s = &(wrk[3*blen]); //1*blen
uint32_t *r = &(wrk[4*blen]); //1*blen
uint32_t *wrki = &(wrk[5*blen]); //9*(blen+1)
int cmp;
int msib;
int I;
for(I=0;I<13*(blen+1);I++) //init working memory to 0
wrk[I] = 0;
for(I=0;I<2*blen;I++) //init lcm=0
lcm[I] = 0;
cmp = amsclil1_fb32_cmp(a,b,blen); //bint is whichever is larger
if(cmp>1) //b>a
{
aint = a;
bint = b;
}
else //a>=b
{
aint = b;
bint = a;
}
if(amsclil1_fb32_iszero(bint,blen))
{
//a = b = 0
//set lcm to 0 (taken care of by default above)
return;
}
msib = amsclil1_fb32_msi(bint,blen);
//msib > msia by bint>aint
//gcd = gcd(aint,bint)
// actually requires 9*(blen+1)?
amsclil1_fb32_eulergcd(aint,bint,gcd,wrki,msib);
//s = bint / gcd
// requires 5*(blen+1) working memory
amsclil1_fb32_div(bint,gcd,q,r,wrki,msib);
//lcm = aint*s
// requires no working memory
amsclil1_fb32_mult(aint,s,lcm,wrki,msib);
return;
}
//Multiplicative Inverse Algorithm
//Refer to FIPS Pub 186-4, Algorithm C.1
//Inputs:
// z [blen] value to find the modular multiplicative inverse of
// dom [blen] modular domain
// wrk [17*(blen+1)] internal working memory
// blen - buffer length
//Outputs:
// zinv [blen] multiplicative inverse such that mod(z*zinv,dom)==1
//Return:
// 1 - success, 0 - failure
int amsclil1_fb32_multinv(uint32_t *z, uint32_t *dom, uint32_t *zinv, uint32_t *wrk, int blen)
{
int ret = 0;
int cmp,res,res2;
int I;
uint32_t *xi = &(wrk[0]); //blen
uint32_t *xj = &(wrk[1*blen]); //blen
uint32_t *y = &(wrk[2*blen]); //2*blen
uint32_t *y1 = &(wrk[4*blen]); //2*blen
uint32_t *y2 = &(wrk[6*blen]); //2*blen
uint32_t *y3 = &(wrk[8*blen]); //2*blen
uint32_t *q = &(wrk[10*blen]); //blen
uint32_t *r = &(wrk[11*blen]); //blen
uint32_t *wrki = &(wrk[12*blen]); //div: [5*(blen+1)]
//at least [17*(blen+1)]
uint32_t *tmp = NULL;
//An alternative way of handling this is take z --> mod(z,dom), then proceed
for(I=0;I<blen;I++) zinv[I] = 0; //init zinv to 0
//if z>=dom, error
cmp = amsclil1_fb32_cmp(z,dom,blen);
if(cmp!=1)
{
ret = 0;
return ret;
}
for(I=0;I<17*(blen+1);I++) wrk[I] = 0; //clean work buffer to start with
//xi = dom
for(I=0;I<blen;I++) xi[I] = dom[I];
//xj = z
for(I=0;I<blen;I++) xj[I] = z[I];
//y2 = 0
for(I=0;I<blen;I++) y2[I] = 0;
//y1 = 1
for(I=0;I<blen;I++) y1[I] = 0; y1[0] = 1;
res = amsclil1_fb32_iszero(xj,blen);
while(res!=1)
{
//(q,r) = xi/xj
res2 = amsclil1_fb32_div(xi,xj,q,r,wrki,blen);
//y = y2-y1*q
amsclil1_fb32_mult(y1,q,y3,wrki,blen);
amsclil1_fb32_sub(y2,y3,y,blen);
//xi = xj
tmp = xi; xi = xj; xj = tmp;
//xj = r
tmp = xj; xj = r; r = tmp;
//y2 = y1
tmp = y2; y2 = y1; y1 = tmp;
//y1 = y
tmp = y1; y1=y; y=tmp;
}
//if xi != 1, failure
res = amsclil1_fb32_msi(xi,blen);
if(res!=1)
{
ret = 0;
return ret;
}
if(xi[0]!=1)
{
ret = 0;
return ret;
}
ret = 1;
//zinv = y2, success
for(I=0;I<blen;I++)
{
zinv[I] = y2[I];
}
return ret;
}

@ -0,0 +1,12 @@
#include <amsclil1/amsclil1.h>
void amsclil1_gutmannprng(uint8_t *randompool)
{
return;
}

@ -0,0 +1,126 @@
#include <amsclil1/amsclil1.h>
/////////////////////
//Utility Functions//
/////////////////////
static void _local_pblocations(size_t N, int64_t *padbytes, int64_t *padstart, int64_t *padstop, uint64_t *sz)
{
int64_t N1 = N;
if(N1%64LL >= 56LL)
{
*padbytes = ((N1/64LL)+2LL)*64LL;
*padstart = N;
*padstop = *padbytes-8;
*sz = N*8;
}
else
{
*padbytes = ((N1/64LL)+1LL)*64LL;
*padstart = N;
*padstop = *padbytes-8;
*sz = N*8;
}
return;
}
static uint8_t _local_getpaddedbyte(const uint8_t *bytes, size_t Nin, size_t ind)
{
uint8_t ret = 0;
int64_t padbytes;
int64_t padstart;
int64_t padstop;
uint64_t sz;
uint64_t q;
uint64_t mask;
_local_pblocations(Nin,&padbytes,&padstart,&padstop,&sz);
if(ind>padbytes)
{
ret = 0; return ret;
}
if(ind<padstart && ind<Nin)
{
ret = bytes[ind];
}
else if(ind==padstart)
{
ret = 0b10000000;
}
else if(ind>padstart && ind<padstop)
{
ret = 0;
}
else
{
//one byte of the padded byte size
//0xaabb0111
q = padbytes-ind-1; //7, 6, ... 0
mask = 0x00000000000000ffLL;
ret = (uint8_t)(sz&(mask<<(q/8LL))>>(q/8LL))
}
return ret;
}
static uint32_t _local_getpaddedui32(const uint8_t *bytes, size_t Nin, size_t ui32ind)
{
uint32_t ret = 0;
size_t N2;
uint8_t b[4];
if(ui32ind>padbytes/4)
{
ret = 0; return ret;
}
b[0] = _local_getpaddedbyte(bytes,Nin,ui32ind*4+0);
b[1] = _local_getpaddedbyte(bytes,Nin,ui32ind*4+1);
b[2] = _local_getpaddedbyte(bytes,Nin,ui32ind*4+2);
b[3] = _local_getpaddedbyte(bytes,Nin,ui32ind*4+3);
ret = ((uint32_t b[0])<<24) + ((uint32_t b[1])<<16) + ((uint32_t b[2])<<8) + ((uint32_t b[3])<<0)
return ret;
}
/////////////////
//Main Function//
/////////////////
//MD5 Hash Algorithm
//Input: Some number of bytes in
//Output: 16 bytes (128 bits) digest out
int amsclil1_md5_hash(const uint8_t *bytes_in, size_t Nin, uint8_t *bytes16_out)
{
int ret = 0;
const uint8_t *pbytes = NULL; //I could do this without dynamic allocation, and it would be faster
size_t npbytes = 0;
size_t I;
return ret;
}
/////////
//Tests//
/////////
//For "official answers", look at:
// https://www.md5hashgenerator.com/
void amsclil1_md5_hash_tests()
{
char str1[250];
return;
}

@ -0,0 +1,193 @@
#include <amsclil1/amsclil1.h>
void test_fbops_printcmp()
{
long N = 2;
uint32_t a[N];
uint32_t b[N];
int c0;
printf("test_fbops_printcmp\n");
amsc_ui32_memset(a,0,N);
amsc_ui32_memset(b,0,N);
printf("a="); amsclil1_fb32_liprint(a,N); printf("\n");
c0 = amsclil1_fb32_iszero(a,N);
printf("a is zero?: %d\n",c0);
a[0] = 0x000000ff;
printf("a="); amsclil1_fb32_liprint(a,N); printf("\n");
c0 = amsclil1_fb32_iszero(a,N);
printf("a is zero?: %d\n",c0);
printf("\n");
printf("a="); amsclil1_fb32_liprint(a,N); printf("\n");
printf("b="); amsclil1_fb32_liprint(b,N); printf("\n");
c0 = amsclil1_fb32_cmp(a,b,N);
printf("cmp(a,b): %d\n",c0);
printf("\n");
b[0] = 0x000000ff;
printf("a="); amsclil1_fb32_liprint(a,N); printf("\n");
printf("b="); amsclil1_fb32_liprint(b,N); printf("\n");
c0 = amsclil1_fb32_cmp(a,b,N);
printf("cmp(a,b): %d\n",c0);
printf("\n");
b[0] = 0x00000fff;
printf("a="); amsclil1_fb32_liprint(a,N); printf("\n");
printf("b="); amsclil1_fb32_liprint(b,N); printf("\n");
printf("b="); amsclil1_fb32_libinprint(b,N); printf("\n");
c0 = amsclil1_fb32_cmp(a,b,N);
printf("cmp(a,b): %d\n",c0);
b[0] = 0b00000000111111110000000011111111; //checking to see whether C understands binary constants
printf("b="); amsclil1_fb32_libinprint(b,N); printf("\n");
fflush(stdout);
return;
}
void amsclil1_intl_printbin(uint32_t b)
{
int I;
uint32_t bit;
for(I=32-1;I>=0;I--)
{
bit = (b & (1<<I)) >> I;
printf("%d",bit);
}
}
void amsclil1_intl_printshift(uint32_t *a, long shift, long blen)
{
long I;
uint32_t r;
printf("0b:");
for(I=blen-1;I>=0;I--)
{
amsclil1_fb32_readshift(a,I,shift,&r,blen);
amsclil1_intl_printbin(r);
if(I!=0) printf(":");
}
}
void test_fbops_shiftread()
{
long N=2;
uint32_t a[N];
long I;
long shift;
uint32_t r;
amsc_ui32_memset(a,0b00001111000011110000111100001111,N);
a[0] = 0b01010101010101010101010101010101;
for(I=0;I<35;I++)
{
//something wrong when I=32
printf("a<<%3ld= ",I);
amsclil1_intl_printshift(a,I,N);
printf("\n");
}
for(I=35;I>-35;I--)
{
printf("a<<%3ld= ",I);
amsclil1_intl_printshift(a,I,N);
printf("\n");
}
return;
}
void test_fbops_shiftwrite()
{
long N = 2;
uint32_t a[N];
uint32_t r;
long I;
printf("hello?");
for(I=-40;I<40;I++)
{
//a[1] = 0b11111111111111111111111111111111;
//a[0] = 0b00000000000000000000000000000000;
a[0] = 0b11110000111100001111000011110000;
a[1] = 0b11111111000000001111111100000000;
//printf("a="); amsclil1_fb32_libinprint(a,N); printf("\n");
r = 0b11100011111111111111111111000111;
amsclil1_fb32_writeshift(a,0,I,&r,N);
printf("a w%3ld=",I); amsclil1_fb32_libinprint(a,N); printf("\n");
}
return;
}
void test_modtdiv()
{
long I,J,K;
printf("Tests of modular arithmetic function and true division.\n");
J = 5;
for(I=-20;I<20;I++)
{
K = amsc_ltdiv(I,J);
printf("%ld / %ld = %ld\n",I,J,K);
}
J = 5;
for(I=-20;I<20;I++)
{
K = amsc_lmod(I,J);
printf("%ld %% %ld = %ld\n",I,J,K);
}
return;
}
void test_fbops_lshiftrshift()
{
long N = 2;
uint32_t a[N],b[N];
long I;
uint32_t r;
printf("Test lshift and rshift.\n");
a[1] = 0xff00ffff;
a[0] = 0xf0f0ff00;
for(I=0;I<35;I++)
{
printf("a="); amsclil1_fb32_libinprint(a,N); printf("\n");
amsclil1_fb32_shiftl(a,1,b,N);
}
for(I=0;I<35;I++)
{
printf("a="); amsclil1_fb32_libinprint(a,N); printf("\n");
amsclil1_fb32_shiftr(a,1,b,N);
}
a[1] = 0xff00ffff;
a[0] = 0xf0f0ff00;
for(I=0;I<2;I++)
{
printf("a="); amsclil1_fb32_libinprint(a,N); printf("\n");
amsclil1_fb32_shiftl(a,32,b,N);
}
a[1] = 0xff00ffff;
a[0] = 0xf0f0ff00;
for(I=0;I<8;I++)
{
printf("a="); amsclil1_fb32_libinprint(a,N); printf("\n");
amsclil1_fb32_shiftl(a,8,b,N);
}
return;
}

@ -0,0 +1,340 @@
#include <amsclil1/amsclil1.h>
void test_addsub()
{
long N = 2;
uint32_t a[N],b[N],c[N];
printf("amsclil1: Test addition and subtraction.\n");
printf("\n");
amsc_ui32_memset(a,0,2);
amsc_ui32_memset(b,0,2);
amsc_ui32_memset(c,0,2);
a[0] = 0x01;
b[0] = 0x01;
printf("a="); amsclil1_fb32_liprint(a,N); printf("\n");
printf("b="); amsclil1_fb32_liprint(b,N); printf("\n");
printf("a+b=c\n");
amsclil1_fb32_add(a,b,c,N);
printf("c="); amsclil1_fb32_liprint(c,N); printf("\n");
printf("\n");
amsc_ui32_memset(a,0,2);
amsc_ui32_memset(b,0,2);
amsc_ui32_memset(c,0,2);
a[0] = 0xffffffff;
b[0] = 0x10000000;
printf("a="); amsclil1_fb32_liprint(a,N); printf("\n");
printf("b="); amsclil1_fb32_liprint(b,N); printf("\n");
printf("a+b=c\n");
amsclil1_fb32_add(a,b,c,N);
printf("c="); amsclil1_fb32_liprint(c,N); printf("\n");
printf("\n");
amsc_ui32_memset(a,0,2);
amsc_ui32_memset(b,0,2);
amsc_ui32_memset(c,0,2);
a[0] = 0xffffffff; a[1] = 0xf0;
b[0] = 0x01000000; b[1] = 0x01;
printf("a="); amsclil1_fb32_liprint(a,N); printf("\n");
printf("b="); amsclil1_fb32_liprint(b,N); printf("\n");
printf("a+b=c\n");
amsclil1_fb32_add(a,b,c,N);
printf("c="); amsclil1_fb32_liprint(c,N); printf("\n");
printf("\n");
amsc_ui32_memset(a,0,2);
amsc_ui32_memset(b,0,2);
amsc_ui32_memset(c,0,2);
a[0] = 0xffffffff; a[1] = 0xffffffff;
b[0] = 0xffffffff; b[1] = 0xffffffff;
printf("a="); amsclil1_fb32_liprint(a,N); printf("\n");
printf("b="); amsclil1_fb32_liprint(b,N); printf("\n");
printf("a+b=c\n");
amsclil1_fb32_add(a,b,c,N);
printf("c="); amsclil1_fb32_liprint(c,N); printf("\n");
printf("\n");
amsc_ui32_memset(a,0,2);
amsc_ui32_memset(b,0,2);
amsc_ui32_memset(c,0,2);
a[0] = 0x01;
b[0] = 0x01;
printf("a="); amsclil1_fb32_liprint(a,N); printf("\n");
printf("b="); amsclil1_fb32_liprint(b,N); printf("\n");
printf("a-b=c\n");
amsclil1_fb32_sub(a,b,c,N);
printf("c="); amsclil1_fb32_liprint(c,N); printf("\n");
printf("\n");
amsc_ui32_memset(a,0,2);
amsc_ui32_memset(b,0,2);
amsc_ui32_memset(c,0,2);
a[0] = 0xf0;
b[0] = 0x01;
printf("a="); amsclil1_fb32_liprint(a,N); printf("\n");
printf("b="); amsclil1_fb32_liprint(b,N); printf("\n");
printf("a-b=c\n");
amsclil1_fb32_sub(a,b,c,N);
printf("c="); amsclil1_fb32_liprint(c,N); printf("\n");
printf("\n");
amsc_ui32_memset(a,0,2);
amsc_ui32_memset(b,0,2);
amsc_ui32_memset(c,0,2);
a[0] = 0x01;
b[0] = 0xf0;
printf("a="); amsclil1_fb32_liprint(a,N); printf("\n");
printf("b="); amsclil1_fb32_liprint(b,N); printf("\n");
printf("a-b=c\n");
amsclil1_fb32_sub(a,b,c,N);
printf("c="); amsclil1_fb32_liprint(c,N); printf("\n");
printf("\n");
amsc_ui32_memset(a,0,2);
amsc_ui32_memset(b,0,2);
amsc_ui32_memset(c,0,2);
a[0] = 0x0fffffff; a[1] = 0xffffffff;
b[0] = 0x30000000; b[1] = 0x00000000;
printf("a="); amsclil1_fb32_liprint(a,N); printf("\n");
printf("b="); amsclil1_fb32_liprint(b,N); printf("\n");
printf("a+b=c\n");
amsclil1_fb32_sub(a,b,c,N);
printf("c="); amsclil1_fb32_liprint(c,N); printf("\n");
return;
}
void test_mult()
{
long N = 2;
uint32_t a[N],b[N],c[2*N];
uint32_t wrk[6*N];
printf("amsclil1: Test multiplication.\n");
printf("\n");
amsc_ui32_memset(a,0,2);
amsc_ui32_memset(b,0,2);
amsc_ui32_memset(c,0,2);
a[0] = 0xffffffff; a[1] = 0x00000000;
b[0] = 0xffffffff; b[1] = 0x00000000;
printf("a="); amsclil1_fb32_liprint(a,N); printf("\n");
printf("b="); amsclil1_fb32_liprint(b,N); printf("\n");
printf("a*b=c\n");
amsclil1_fb32_mult(a,b,c,wrk,N);
printf("c="); amsclil1_fb32_liprint(c,2*N); printf("\n");
printf("\n");
amsc_ui32_memset(a,0,2);
amsc_ui32_memset(b,0,2);
amsc_ui32_memset(c,0,2);
a[0] = 0x0fffffff; a[1] = 0xffffffff;
b[0] = 0x30000000; b[1] = 0x00000000;
printf("a="); amsclil1_fb32_liprint(a,N); printf("\n");
printf("b="); amsclil1_fb32_liprint(b,N); printf("\n");
printf("a*b=c\n");
amsclil1_fb32_mult(a,b,c,wrk,N);
printf("c="); amsclil1_fb32_liprint(c,2*N); printf("\n");
printf("\n");
amsc_ui32_memset(a,0,2);
amsc_ui32_memset(b,0,2);
amsc_ui32_memset(c,0,2);
a[0] = 0xffffffff; a[1] = 0xffffffff;
b[0] = 0xffffffff; b[1] = 0xffffffff;
printf("a="); amsclil1_fb32_liprint(a,N); printf("\n");
printf("b="); amsclil1_fb32_liprint(b,N); printf("\n");
printf("a*b=c\n");
amsclil1_fb32_mult(a,b,c,wrk,N);
printf("c="); amsclil1_fb32_liprint(c,2*N); printf("\n");
return;
}
void test_div()
{
long N = 2;
uint32_t a[N],b[N],c[N],d[N];
//uint32_t wrk[3*(N+1)];
uint32_t wrk[5*(N+1)];
printf("amsclil1: Test division.\n");
printf("\n");
amsc_ui32_memset(a,0,2);
amsc_ui32_memset(b,0,2);
amsc_ui32_memset(c,0,2);
a[0] = 0xffffaf0f; a[1] = 0x00000000;
b[0] = 0x00000001; b[1] = 0x00000000;
printf("a ="); amsclil1_fb32_liprint(a,N); printf("\n");
printf("b ="); amsclil1_fb32_liprint(b,N); printf("\n");
printf("a/b=res,rem\n");
amsclil1_fb32_div(a,b,c,d,wrk,N);
printf("res="); amsclil1_fb32_liprint(c,N); printf("\n");
printf("rem="); amsclil1_fb32_liprint(d,N); printf("\n");
printf("\n");
amsc_ui32_memset(a,0,2);
amsc_ui32_memset(b,0,2);
amsc_ui32_memset(c,0,2);
a[0] = 0xffffaf0f; a[1] = 0x00000000;
b[0] = 0x00000100; b[1] = 0x00000000;
printf("a ="); amsclil1_fb32_liprint(a,N); printf("\n");
printf("b ="); amsclil1_fb32_liprint(b,N); printf("\n");
printf("a/b=res,rem\n");
amsclil1_fb32_div(a,b,c,d,wrk,N);
printf("res="); amsclil1_fb32_liprint(c,N); printf("\n");
printf("rem="); amsclil1_fb32_liprint(d,N); printf("\n");
printf("\n");
amsc_ui32_memset(a,0,2);
amsc_ui32_memset(b,0,2);
amsc_ui32_memset(c,0,2);
a[0] = 0xffffaf0f; a[1] = 0x00000000;
b[0] = 0xf0000000; b[1] = 0x00000000;
printf("a ="); amsclil1_fb32_liprint(a,N); printf("\n");
printf("b ="); amsclil1_fb32_liprint(b,N); printf("\n");
printf("a/b=res,rem\n");
amsclil1_fb32_div(a,b,c,d,wrk,N);
printf("res="); amsclil1_fb32_liprint(c,N); printf("\n");
printf("rem="); amsclil1_fb32_liprint(d,N); printf("\n");
printf("\n");
amsc_ui32_memset(a,0,2);
amsc_ui32_memset(b,0,2);
amsc_ui32_memset(c,0,2);
a[0] = 0x00000001; a[1] = 0x00000000;
b[0] = 0xf0000000; b[1] = 0x00000000;
printf("a ="); amsclil1_fb32_liprint(a,N); printf("\n");
printf("b ="); amsclil1_fb32_liprint(b,N); printf("\n");
printf("a/b=res,rem\n");
amsclil1_fb32_div(a,b,c,d,wrk,N);
printf("res="); amsclil1_fb32_liprint(c,N); printf("\n");
printf("rem="); amsclil1_fb32_liprint(d,N); printf("\n");
printf("\n");
amsc_ui32_memset(a,0,2);
amsc_ui32_memset(b,0,2);
amsc_ui32_memset(c,0,2);
a[0] = 0x00000001; a[1] = 0x00000000;
b[0] = 0x00000000; b[1] = 0x00000000;
printf("a ="); amsclil1_fb32_liprint(a,N); printf("\n");
printf("b ="); amsclil1_fb32_liprint(b,N); printf("\n");
printf("a/b=res,rem\n");
amsclil1_fb32_div(a,b,c,d,wrk,N);
printf("res="); amsclil1_fb32_liprint(c,N); printf("\n");
printf("rem="); amsclil1_fb32_liprint(d,N); printf("\n");
printf("\n");
amsc_ui32_memset(a,0,2);
amsc_ui32_memset(b,0,2);
amsc_ui32_memset(c,0,2);
a[0] = 0x00000004; a[1] = 0xff0ff0ff;
b[0] = 0x00000004; b[1] = 0x00000000;
printf("a ="); amsclil1_fb32_liprint(a,N); printf("\n");
printf("b ="); amsclil1_fb32_liprint(b,N); printf("\n");
printf("a/b=res,rem\n");
amsclil1_fb32_div(a,b,c,d,wrk,N);
printf("res="); amsclil1_fb32_liprint(c,N); printf("\n");
printf("rem="); amsclil1_fb32_liprint(d,N); printf("\n");
printf("\n");
amsc_ui32_memset(a,0,2);
amsc_ui32_memset(b,0,2);
amsc_ui32_memset(c,0,2);
a[0] = 0x00000001; a[1] = 0x00000000;
b[0] = 0x00000000; b[1] = 0x00000000;
printf("a ="); amsclil1_fb32_liprint(a,N); printf("\n");
printf("b ="); amsclil1_fb32_liprint(b,N); printf("\n");
printf("a/b=res,rem\n");
amsclil1_fb32_div(a,b,c,d,wrk,N);
printf("res="); amsclil1_fb32_liprint(c,N); printf("\n");
printf("rem="); amsclil1_fb32_liprint(d,N); printf("\n");
return;
}
void test_modpower()
{
long N = 2;
uint32_t a[N],b[N],c[N],d[N];
uint32_t wrk[8*(N+2)];
uint32_t stor[12*(N+1)];
printf("amsclil1: Mod power tests.\n");
printf("\n");
amsc_ui32_memset(a,0,N);
amsc_ui32_memset(b,0,N);
amsc_ui32_memset(c,0,N);
amsc_ui32_memset(d,0,N);
a[0] = 0x00000010; a[1] = 0x00000000;
b[0] = 0x00000002; b[1] = 0x00000000;
c[0] = 0x000000ff; c[1] = 0x00000000;
printf("mod(a^b, c) = d\n");
printf("a="); amsclil1_fb32_liprint(a,N); printf("\n");
printf("b="); amsclil1_fb32_liprint(b,N); printf("\n");
printf("c="); amsclil1_fb32_liprint(c,N); printf("\n");
amsclil1_fb32_modpower(a,b,c,d,wrk,stor,N);
printf("d="); amsclil1_fb32_liprint(d,N); printf("\n"); //0x01 - check
printf("\n");
amsc_ui32_memset(a,0,N);
amsc_ui32_memset(b,0,N);
amsc_ui32_memset(c,0,N);
amsc_ui32_memset(d,0,N);
a[0] = 0x0000faf0; a[1] = 0x00000000;
b[0] = 0x00000c0a; b[1] = 0x00000000;
c[0] = 0x00ffaaff; c[1] = 0x00000000;
printf("mod(a^b, c) = d\n");
printf("a="); amsclil1_fb32_liprint(a,N); printf("\n");
printf("b="); amsclil1_fb32_liprint(b,N); printf("\n");
printf("c="); amsclil1_fb32_liprint(c,N); printf("\n");
amsclil1_fb32_modpower(a,b,c,d,wrk,stor,N);
printf("d="); amsclil1_fb32_liprint(d,N); printf("\n"); //0xf5ee89 - check
printf("\n");
amsc_ui32_memset(a,0,N);
amsc_ui32_memset(b,0,N);
amsc_ui32_memset(c,0,N);
amsc_ui32_memset(d,0,N);
a[0] = 0x0000afaf; a[1] = 0xafaf0000;
b[0] = 0xff00ff00; b[1] = 0xff00ff00;
c[0] = 0x10101010; c[1] = 0x10101010;
printf("mod(a^b, c) = d\n");
printf("a="); amsclil1_fb32_liprint(a,N); printf("\n");
printf("b="); amsclil1_fb32_liprint(b,N); printf("\n");
printf("c="); amsclil1_fb32_liprint(c,N); printf("\n");
amsclil1_fb32_modpower(a,b,c,d,wrk,stor,N);
printf("d="); amsclil1_fb32_liprint(d,N); printf("\n"); //0x6e10c57c1e7bc71 - check
//speed test - moment of truth: can you do 4096 bit (64) RSA?
long N2 = 64;
uint32_t a2[N2],b2[N2],c2[N2],d2[N2];
uint32_t wrk2[8*(N2+2)];
uint32_t stor2[12*(N2+1)];
printf("\n");
amsc_ui32_memset(a2,0,N2);
amsc_ui32_memset(b2,0,N2);
amsc_ui32_memset(c2,0,N2);
amsc_ui32_memset(d2,0,N2);
a2[0] = 0x00000077; a2[N2-1] = 0x70000000;
b2[0] = 0x00000066; b2[N2-1] = 0xff000000;
c2[0] = 0x00000013; c2[N2-1] = 0x13000000;
printf("mod(a^b, c) = d\n");
printf("a2="); amsclil1_fb32_liprint(a2,N2); printf("\n");
printf("b2="); amsclil1_fb32_liprint(b2,N2); printf("\n");
printf("c2="); amsclil1_fb32_liprint(c2,N2); printf("\n");
amsclil1_fb32_modpower(a2,b2,c2,d2,wrk2,stor2,N2);
printf("d2="); amsclil1_fb32_liprint(d2,N2); printf("\n");
//still takes too blasted long
return;
}

@ -0,0 +1,883 @@
#include <amsclil1/amsclil1.h>
#include <time.h>
//clock()
//CLOCKS_PER_SEC
//non_cryptographic test randomness
void amsclil1_fb32_testrandom(uint32_t* a,long blen)
{
long I;
for(I=0;I<blen;I++)
{
a[I] = (uint32_t) rand();
}
return;
}
void test_mult_time()
{
long I;
long N;
uint32_t* a = NULL;
uint32_t* b = NULL;
uint32_t* c = NULL;
uint32_t* d = NULL;
uint32_t* wrk = NULL;
double t0,t1;
printf("amsclil1: Multiplication time test.\n");
printf("bits \t time (msec)\n");
printf("-----------------------------\n");
for(I=1;I<=64;I++)
{
a = amsc_malloc(I*sizeof(uint32_t));
b = amsc_malloc(I*sizeof(uint32_t));
c = amsc_malloc(2*I*sizeof(uint32_t));
d = amsc_malloc(I*sizeof(uint32_t));
wrk = amsc_malloc(6*I*sizeof(uint32_t));
amsclil1_fb32_testrandom(a,I);
amsclil1_fb32_testrandom(b,I);
amsclil1_fb32_testrandom(c,I);
t0 = amsc_timemsec();
amsclil1_fb32_mult(a,b,c,wrk,I);
t1 = amsc_timemsec();
printf("%ld \t %1.4f\n",I*32,t1-t0);
free(a); a = NULL;
free(b); b = NULL;
free(c); c = NULL;
free(d); d = NULL;
}
return;
}
void test_div_time()
{
printf("amsclil1: division time test.\n");
long I;
long N;
uint32_t* a = NULL;
uint32_t* b = NULL;
uint32_t* c = NULL;
uint32_t* d = NULL;
uint32_t* wrk = NULL;
double t0,t1;
printf("bits \t time (msec)\n");
printf("-----------------------------\n");
for(I=1;I<=64;I++)
{
a = amsc_malloc(I*sizeof(uint32_t));
b = amsc_malloc(I*sizeof(uint32_t));
c = amsc_malloc(I*sizeof(uint32_t));
d = amsc_malloc(I*sizeof(uint32_t));
wrk = amsc_malloc(5*(I+1)*sizeof(uint32_t));
amsclil1_fb32_testrandom(a,I);
amsc_ui32_memset(b,0,I);
amsclil1_fb32_testrandom(b,I/2);
t0 = amsc_timemsec();
amsclil1_fb32_div(a,b,c,d,wrk,I);
t1 = amsc_timemsec();
printf("%ld \t %1.4f\n",I*32,t1-t0);
free(a); a = NULL;
free(b); b = NULL;
free(c); c = NULL;
free(d); d = NULL;
}
return;
}
void test_barrettmod()
{
long N = 2;
long K,Kx;
uint32_t x[N],y[N],z[N],res[N],rem[N],mu[2*(N+1)];
uint32_t wrk[33*(N+1)];
uint32_t stor[14*(N+1)];
printf("Barrett modulus tests.\n");
printf("\n");
amsc_ui32_memset(x,0,N); //x and y must have one leading zero digit
amsc_ui32_memset(y,0,N);
amsc_ui32_memset(mu,0,2*(N+1)); //because mu can have mis(y)+1 leading digit
x[1] = 0x000000ab; x[0] = 0x12344321;
y[1] = 0x00000001; y[0] = 0xf000000f;
K = amsclil1_fb32_msi(y,N)+1;
Kx = amsclil1_fb32_msi(x,N)+1;
printf("K=%ld\n",K);
printf("x="); amsclil1_fb32_liprint(x,N); printf("\n");
printf("y="); amsclil1_fb32_liprint(y,N); printf("\n");
amsclil1_fb32_barrettmu(y,mu,wrk,K,N);
printf("mu="); amsclil1_fb32_liprint(mu,2*(N+1)); printf("\n");
amsclil1_fb32_barrettmod(x,y,z,mu,wrk,K,Kx,N);
printf("z=mod(x,y):"); amsclil1_fb32_liprint(z,N); printf("\n");
//hangs with improper mu?
// x=0x00000000000000ab12344321
// y=0x0000000000000001f000000f
// debug:mu=0x0000000000000000000000008421083e1194e97a4e97e82c
// mu=0x8421083e1194e97a4e97e82c
// z=mod(x,y):0x000000000000000092343df9
//official answer: 92343df9 - okay, so that works
printf("\n");
amsc_ui32_memset(x,0,N); //x and y must have one leading zero digit
amsc_ui32_memset(y,0,N);
amsc_ui32_memset(mu,0,2*(N+1)); //because mu can have mis(y)+1 leading digit
x[1] = 0x00000000; x[0] = 0x00000100;
y[1] = 0x00000000; y[0] = 0x000000ff;
K = amsclil1_fb32_msi(y,N)+1;
Kx = amsclil1_fb32_msi(x,N)+1;
printf("K=%ld\n",K);
printf("x="); amsclil1_fb32_liprint(x,N); printf("\n");
printf("y="); amsclil1_fb32_liprint(y,N); printf("\n");
amsclil1_fb32_barrettmu(y,mu,wrk,K,N);
printf("mu="); amsclil1_fb32_liprint(mu,2*(N+1)); printf("\n");
amsclil1_fb32_barrettmod(x,y,z,mu,wrk,K,Kx,N);
printf("z=mod(x,y):"); amsclil1_fb32_liprint(z,N); printf("\n");
//answer should be 0x01
printf("\n");
amsc_ui32_memset(x,0,N); //x and y must have one leading zero digit
amsc_ui32_memset(y,0,N);
amsc_ui32_memset(mu,0,2*(N+1)); //because mu can have mis(y)+1 leading digit
x[1] = 0x00000000; x[0] = 0x00000007;
y[1] = 0x00000000; y[0] = 0x000000ff;
K = amsclil1_fb32_msi(y,N)+1;
Kx = amsclil1_fb32_msi(x,N)+1;
printf("K=%ld\n",K);
printf("x="); amsclil1_fb32_liprint(x,N); printf("\n");
printf("y="); amsclil1_fb32_liprint(y,N); printf("\n");
amsclil1_fb32_barrettmu(y,mu,wrk,K,N);
printf("mu="); amsclil1_fb32_liprint(mu,2*(N+1)); printf("\n");
amsclil1_fb32_barrettmod(x,y,z,mu,wrk,K,Kx,N);
printf("z=mod(x,y):"); amsclil1_fb32_liprint(z,N); printf("\n");
printf("\n");
amsc_ui32_memset(x,0,N); //x and y must have one leading zero digit
amsc_ui32_memset(y,0,N);
amsc_ui32_memset(mu,0,2*(N+1)); //because mu can have mis(y)+1 leading digit
x[1] = 0x00000000; x[0] = 0x00010000;
y[1] = 0x00000000; y[0] = 0x0000ff00;
K = amsclil1_fb32_msi(y,N)+1;
Kx = amsclil1_fb32_msi(x,N)+1;
printf("K=%ld\n",K);
printf("x="); amsclil1_fb32_liprint(x,N); printf("\n");
printf("y="); amsclil1_fb32_liprint(y,N); printf("\n");
amsclil1_fb32_barrettmu(y,mu,wrk,K,N);
printf("mu="); amsclil1_fb32_liprint(mu,2*(N+1)); printf("\n");
amsclil1_fb32_barrettmod(x,y,z,mu,wrk,K,Kx,N);
printf("z=mod(x,y):"); amsclil1_fb32_liprint(z,N); printf("\n");
return;
}
void barret_modpower_test()
{
long N = 2;
long K,Kx;
uint32_t x[N],y[N],z[N],res[N],rem[N],mu[2*(N+1)];
uint32_t wrk[66*(N+1)];
uint32_t stor[28*(N+1)];
uint32_t a[N],b[N],c[N], d[N];
printf("\nBarrett modpower tests.\n");
printf("\n");
amsc_ui32_memset(a,0,N);
amsc_ui32_memset(b,0,N);
amsc_ui32_memset(c,0,N);
amsc_ui32_memset(d,0,N);
a[0] = 0x00000010; a[1] = 0x00000000;
b[0] = 0x00000002; b[1] = 0x00000000;
c[0] = 0x000000ff; c[1] = 0x00000000;
printf("mod(a^b, c) = d\n");
printf("a="); amsclil1_fb32_liprint(a,N); printf("\n");
printf("b="); amsclil1_fb32_liprint(b,N); printf("\n");
printf("c="); amsclil1_fb32_liprint(c,N); printf("\n");
amsclil1_fb32_modpower_cls(a,b,c,d,wrk,stor,N);
printf("d="); amsclil1_fb32_liprint(d,N); printf("\n"); //0x01 - check
amsclil1_fb32_modpower_bar(a,b,c,d,wrk,stor,N);
printf("d2="); amsclil1_fb32_liprint(d,N); printf("\n");
printf("\n");
amsc_ui32_memset(a,0,N);
amsc_ui32_memset(b,0,N);
amsc_ui32_memset(c,0,N);
amsc_ui32_memset(d,0,N);
a[0] = 0x0000faf0; a[1] = 0x00000000;
b[0] = 0x00000c0a; b[1] = 0x00000000;
c[0] = 0x00ffaaff; c[1] = 0x00000000;
printf("mod(a^b, c) = d\n");
printf("a="); amsclil1_fb32_liprint(a,N); printf("\n");
printf("b="); amsclil1_fb32_liprint(b,N); printf("\n");
printf("c="); amsclil1_fb32_liprint(c,N); printf("\n");
amsclil1_fb32_modpower_cls(a,b,c,d,wrk,stor,N);
printf("d="); amsclil1_fb32_liprint(d,N); printf("\n"); //0xf5ee89 - check
amsclil1_fb32_modpower_bar(a,b,c,d,wrk,stor,N);
printf("d2="); amsclil1_fb32_liprint(d,N); printf("\n");
printf("\n");
amsc_ui32_memset(a,0,N);
amsc_ui32_memset(b,0,N);
amsc_ui32_memset(c,0,N);
amsc_ui32_memset(d,0,N);
a[0] = 0x0000afaf; a[1] = 0xafaf0000;
b[0] = 0xff00ff00; b[1] = 0xff00ff00;
c[0] = 0x10101010; c[1] = 0x10101010;
printf("mod(a^b, c) = d\n");
printf("a="); amsclil1_fb32_liprint(a,N); printf("\n");
printf("b="); amsclil1_fb32_liprint(b,N); printf("\n");
printf("c="); amsclil1_fb32_liprint(c,N); printf("\n");
amsclil1_fb32_modpower_cls(a,b,c,d,wrk,stor,N);
printf("d="); amsclil1_fb32_liprint(d,N); printf("\n"); //0x6e10c57c1e7bc71 - check
amsclil1_fb32_modpower_bar(a,b,c,d,wrk,stor,N);
printf("d2="); amsclil1_fb32_liprint(d,N); printf("\n");
//speed test - moment of truth: can you do 4096 bit (64) RSA?
long N2 = 64;
uint32_t a2[N2],b2[N2],c2[N2],d2[N2];
uint32_t wrk2[66*(N2+2)];
uint32_t stor2[28*(N2+1)];
printf("\n");
amsc_ui32_memset(a2,0,N2);
amsc_ui32_memset(b2,0,N2);
amsc_ui32_memset(c2,0,N2);
amsc_ui32_memset(d2,0,N2);
a2[0] = 0x00000077; a2[N2-1] = 0x70000000;
b2[0] = 0x00000066; b2[N2-1] = 0xff000000;
c2[0] = 0x00000013; c2[N2-1] = 0x13000000;
printf("mod(a^b, c) = d\n");
printf("a2="); amsclil1_fb32_liprint(a2,N2); printf("\n");
printf("b2="); amsclil1_fb32_liprint(b2,N2); printf("\n");
printf("c2="); amsclil1_fb32_liprint(c2,N2); printf("\n");
amsclil1_fb32_modpower_cls(a2,b2,c2,d2,wrk2,stor2,N2);
printf("d2="); amsclil1_fb32_liprint(d2,N2); printf("\n");
amsclil1_fb32_modpower_bar(a2,b2,c2,d2,wrk2,stor2,N2);
printf("d2="); amsclil1_fb32_liprint(d2,N2); printf("\n");
}
void barrett_mod_fuzztest()
{
long N = 32;
long K,Kx;
uint32_t x[N],y[N],z[N],res[N],rem[N],mu[2*(N+1)];
uint32_t wrk[33*(N+1)];
uint32_t stor[14*(N+1)];
long I;
int cmp;
long Nfail;
Nfail = 0;
for(I=0;I<100;I++)
{
printf("Test %ld\n",I);
amsc_ui32_memset(x,0,N);
amsc_ui32_memset(y,0,N);
amsc_ui32_memset(z,0,N);
amsc_ui32_memset(res,0,N);
amsc_ui32_memset(rem,0,N);
amsc_ui32_memset(mu,0,2*(N+1));
amsclil1_fb32_ncrandint(x,N);
amsclil1_fb32_ncrandint(y,N/2);
amsclil1_fb32_div(x,y,res,rem,wrk,N);
K = amsclil1_fb32_msi(y,N)+1;
Kx = amsclil1_fb32_msi(x,N)+1;
amsclil1_fb32_barrettmu(y,mu,wrk,K,N);
amsclil1_fb32_barrettmod(x,y,z,mu,wrk,K,Kx,N);
cmp = amsclil1_fb32_cmp(z,rem,N);
printf(" x="); amsclil1_fb32_liprint(x,N); printf("\n");
printf(" y="); amsclil1_fb32_liprint(y,N); printf("\n");
printf("rem="); amsclil1_fb32_liprint(rem,N); printf("\n");
printf(" z="); amsclil1_fb32_liprint(z,N); printf("\n");
if(cmp!=0)
{
printf("Mod accuracy failure:\n");
printf("x="); amsclil1_fb32_liprint(x,N); printf("\n");
printf("y="); amsclil1_fb32_liprint(y,N); printf("\n");
printf("rem="); amsclil1_fb32_liprint(rem,N); printf("\n");
printf("z="); amsclil1_fb32_liprint(z,N); printf("\n");
Nfail++;
}
}
printf("Nfailures = %ld out of %ld\n",Nfail,100L);
}
void modpower_timetests()
{
printf("Modpower Time Tests.\n");
long N;
long I;
uint32_t *x = NULL;
uint32_t *y = NULL;
uint32_t *md = NULL;
uint32_t *pw = NULL;
uint32_t *wrk = NULL;
uint32_t *stor = NULL;
double t0,t1;
printf("bits \t time (msec)\n");
printf("------------------");
for(I=1;I<=64;I++)
{
N = I;
x = amsc_malloc(I*sizeof(uint32_t));
y = amsc_malloc(I*sizeof(uint32_t));
md = amsc_malloc(I*sizeof(uint32_t));
pw = amsc_malloc(I*sizeof(uint32_t));
wrk = amsc_malloc(66*(I+1)*sizeof(uint32_t));
stor = amsc_malloc(28*(I+1)*sizeof(uint32_t));
amsclil1_fb32_testrandom(x,N);
amsclil1_fb32_testrandom(md,N);
amsclil1_fb32_testrandom(pw,N);
t0 = amsc_timemsec();
amsclil1_fb32_modpower(x,pw,md,y,wrk,stor,N);
t1 = amsc_timemsec();
printf("%ld \t %1.4f \n",N*32,t1-t0);
free(x); x=NULL;
free(y); y=NULL;
free(md); md=NULL;
free(pw); pw=NULL;
free(wrk); wrk=NULL;
free(stor); stor=NULL;
}
return;
}
void test_amsclil1_fb32_eulergcd()
{
printf("Test for amsclil1_fb32_eulergcd...\n");
long blen = 6;
uint32_t *a,*b,*c,*d,*e,*wrk;
long I;
int b1,b2;
a = (uint32_t*) malloc(sizeof(uint32_t)*blen);
b = (uint32_t*) malloc(sizeof(uint32_t)*blen);
c = (uint32_t*) malloc(sizeof(uint32_t)*blen);
d = (uint32_t*) malloc(sizeof(uint32_t)*blen);
e = (uint32_t*) malloc(sizeof(uint32_t)*blen);
wrk = (uint32_t*) malloc(10*sizeof(uint32_t)*(blen+1));
for(I=0;I<1000;I++)
{
printf("euler gcd test %ld\n",I);
amsclil1_fb32_ncrandint(a,blen);
amsclil1_fb32_ncrandint(b,blen);
//some sort of memory bug when blen>=6
//still hitting bad conditions for division
amsclil1_fb32_eulergcd(a,b,c,wrk,blen);
amsclil1_fb32_div(a,c,d,e,wrk,blen);
b1 = amsclil1_fb32_iszero(e,blen);
amsclil1_fb32_div(b,c,d,e,wrk,blen);
b2 = amsclil1_fb32_iszero(e,blen);
if(b1==1 && b2 == 1)
{
printf("\t pass.\n");
}
else
{
printf("\t FAIL.\n");
break;
}
}
free(a);
free(b);
free(c);
free(d);
free(e);
free(wrk);
return;
}
void test_fb16_comparison_sub()
{
printf("tests of fb16 subtraction and comparison ops.\n");
long blen = 4;
uint16_t *a,*b,*c,*wrk;
long diff;
int cmp;
a = (uint16_t*) malloc(sizeof(uint16_t)*blen);
b = (uint16_t*) malloc(sizeof(uint16_t)*blen);
c = (uint16_t*) malloc(sizeof(uint16_t)*blen);
//diff-1 = 0: a bug in subtraction
// debug: 1 y2=0x00000000000000000000000000000000000000000000000000377d3a
// debug: 1 x2=0x00000000000000000000000000000000000000000000000000a62c4b
// debug: 1 y2=0x00000000000000000000000000000000000000000000000000377d3a
// debug: 1 x2=0x000000000000000000000000000000000000000000000000006eaf11
// debug: 1 y2=0x00000000000000000000000000000000000000000000000000377d3a
// debug: 1 x2=0x000000000000000000000000000000000000000000000000003731d7
// debug: 1 y2=0x00000000000000000000000000000000000000000000000000377d3a
// debug: 1 x2=0xffffffffffffffffffffffffffffffffffffffffffffffffffffb49d
a[2] = 0;
a[1] = 0x0037;
a[0] = 0x7d3a;
b[2] = 0;
b[1] = 0x0037;
b[0] = 0x31d7;
printf("a="); amsclil1_fb16_liprint(a,blen); printf("\n");
printf("b="); amsclil1_fb16_liprint(b,blen); printf("\n");
diff = 0;
cmp = amsclil1_fb16_intlscmp(a,b,diff,blen);
printf("cmp(a,b) diff = %ld, cmp = %d\n",diff,cmp);
diff = 0;
cmp = amsclil1_fb16_intlscmp(b,a,diff,blen);
printf("cmp(b,a) diff = %ld, cmp = %d\n",diff,cmp);
diff = 1;
cmp = amsclil1_fb16_intlscmp(a,b,diff,blen);
printf("diff = %ld, cmp = %d\n",diff,cmp);
diff = -1;
cmp = amsclil1_fb16_intlscmp(a,b,diff,blen);
printf("diff = %ld, cmp = %d\n",diff,cmp);
diff = 2;
cmp = amsclil1_fb16_intlscmp(a,b,diff,blen);
printf("diff = %ld, cmp = %d\n",diff,cmp);
printf("subtraction a - b*base^diff\n");
diff = 0;
printf("diff=%ld\n",diff);
amsclil1_fb16_intlssub(a,b,diff,c,blen);
printf("a="); amsclil1_fb16_liprint(a,blen); printf("\n");
printf("b="); amsclil1_fb16_liprint(b,blen); printf("\n");
printf("c="); amsclil1_fb16_liprint(c,blen); printf("\n");
printf("subtraction a - b*base^diff\n");
diff = 1;
printf("diff=%ld\n",diff);
amsclil1_fb16_intlssub(a,b,diff,c,blen);
printf("a="); amsclil1_fb16_liprint(a,blen); printf("\n");
printf("b="); amsclil1_fb16_liprint(b,blen); printf("\n");
printf("c="); amsclil1_fb16_liprint(c,blen); printf("\n");
// printf("subtraction a - b*base^diff\n");
// diff = -1;
// printf("diff=%ld\n",diff);
// amsclil1_fb16_intlssub(a,b,diff,c,blen);
// printf("a="); amsclil1_fb16_liprint(a,blen); printf("\n");
// printf("b="); amsclil1_fb16_liprint(b,blen); printf("\n");
// printf("c="); amsclil1_fb16_liprint(c,blen); printf("\n");
printf("subtraction b - a*base^diff\n");
diff = 0;
printf("diff=%ld\n",diff);
amsclil1_fb16_intlssub(b,a,diff,c,blen);
printf("b="); amsclil1_fb16_liprint(b,blen); printf("\n");
printf("a="); amsclil1_fb16_liprint(a,blen); printf("\n");
printf("c="); amsclil1_fb16_liprint(c,blen); printf("\n");
free(a);
free(b);
free(c);
return;
}
void test_div_stress_test()
{
uint32_t *a,*b,*q,*r,*m1,*m2, *wrk;
long I;
int res;
long blen = 6;
a = (uint32_t*) malloc(2*sizeof(uint32_t)*blen);
b = (uint32_t*) malloc(2*sizeof(uint32_t)*blen);
q = (uint32_t*) malloc(2*sizeof(uint32_t)*blen);
r = (uint32_t*) malloc(2*sizeof(uint32_t)*blen);
m1 = (uint32_t*) malloc(2*sizeof(uint32_t)*(blen+1));
m2 = (uint32_t*) malloc(2*sizeof(uint32_t)*(blen+1));
wrk = (uint32_t*) malloc(10*sizeof(uint32_t)*(blen+1));
printf("Division stress test.\n");
long ntest = 10;
for(I=0;I<ntest;I++)
{
amsclil1_fb32_ncrandint(a,blen);
amsclil1_fb32_ncrandint(b,blen-1);
//a/b --> (q,r)
amsclil1_fb32_div(a,b,q,r,wrk,blen);
//q*b --> m1
// Now I have a memory bug in the multiplication routine
// probably m1 must be blen+1? (2*blen) - I wonder where else I've messed that up?
amsclil1_fb32_mult(q,b,m1,wrk,blen);
//m1+r --> m2
amsclil1_fb32_add(m1,r,m2,2*blen);
res = amsclil1_fb32_cmp(m2,a,2*blen);
printf("test %ld: %d\n",I,res);
printf("\ta= "); amsclil1_fb32_liprint(a,blen); printf("\n");
printf("\tm2="); amsclil1_fb32_liprint(m2,blen); printf("\n");
printf("\tb="); amsclil1_fb32_liprint(b,blen); printf("\n");
printf("\tq="); amsclil1_fb32_liprint(q,blen); printf("\n");
printf("\tr="); amsclil1_fb32_liprint(r,blen); printf("\n");
}
free(a);
free(b);
free(q);
free(r);
free(m1);
free(m2);
free(wrk);
return;
}
int test_div_stress_test_sub(uint32_t *a, uint32_t *b, long blen)
{
uint32_t *q,*r,*m1,*m2, *wrk;
long I;
int res,res2;
q = (uint32_t*) malloc(2*sizeof(uint32_t)*blen);
r = (uint32_t*) malloc(2*sizeof(uint32_t)*blen);
m1 = (uint32_t*) malloc(2*sizeof(uint32_t)*(blen+1));
m2 = (uint32_t*) malloc(2*sizeof(uint32_t)*(blen+1));
wrk = (uint32_t*) malloc(10*sizeof(uint32_t)*(blen+1));
amsclil1_fb32_setzero(q,2*blen);
amsclil1_fb32_setzero(r,2*blen);
amsclil1_fb32_setzero(m1,2*(blen+1));
amsclil1_fb32_setzero(m2,2*(blen+1));
amsclil1_fb32_setzero(wrk,10*(blen+1));
//printf("\tm2="); amsclil1_fb32_liprint(m2,2*blen); printf("\n");
//why is there extra shit in m2?
//a/b --> (q,r)
res2 = amsclil1_fb32_div(a,b,q,r,wrk,blen);
//q*b --> m1
// Now I have a memory bug in the multiplication routine
// probably m1 must be blen+1? (2*blen) - I wonder where else I've messed that up?
amsclil1_fb32_mult(q,b,m1,wrk,blen);
//m1+r --> m2
amsclil1_fb32_add(m1,r,m2,2*blen);
res = amsclil1_fb32_cmp(m2,a,2*blen);
if(res!=0 && res2!=0)
{
printf("Division test fail:\n");
printf("cmp=%d res=%d\n",res,res2);
printf("\ta= "); amsclil1_fb32_liprint(a,2*blen); printf("\n");
printf("\tm2="); amsclil1_fb32_liprint(m2,2*blen); printf("\n");
printf("\tb="); amsclil1_fb32_liprint(b,blen); printf("\n");
printf("\tq="); amsclil1_fb32_liprint(q,blen); printf("\n");
printf("\tr="); amsclil1_fb32_liprint(r,blen); printf("\n");
res = 0;
}
else
{
res = 1;
}
free(q);
free(r);
free(m1);
free(m2);
free(wrk);
return res;
}
void test_div_stress_test2()
{
long blenmax = 10;
uint32_t *a,*b;
long I,J,B,K,L;
long Nt;
int res;
a = (uint32_t*) malloc(2*blenmax*sizeof(uint32_t));
b = (uint32_t*) malloc(2*blenmax*sizeof(uint32_t));
Nt = 1000; //number of tests at each division
//test series 1
printf("Test series 1: random divisions.\n");
for(I=1;I<=blenmax;I++)
{
for(J=1;J<=blenmax;J++)
{
printf("\t divisions (%d,%d)\n",(int)I,(int)J);
for(L=0;L<Nt;L++)
{
for(K=0;K<blenmax;K++)
{
a[K] = 0;
b[K] = 0;
}
amsclil1_fb32_ncrandint(a,I);
amsclil1_fb32_ncrandint(b,J);
if(I>=J) B = I; else B = J;
res = test_div_stress_test_sub(a,b,B);
if(res<=0)
{
printf("stopping on test failure...\n");
exit(0);
}
}
}
}
printf("Test series 2: division by zero\n");
for(I=1;I<=blenmax;I++)
{
for(J=1;J<=blenmax;J++)
{
printf("\t divisions (%d,%d)\n",(int)I,(int)J);
for(L=0;L<Nt;L++)
{
for(K=0;K<blenmax;K++)
{
a[K] = 0;
b[K] = 0;
}
amsclil1_fb32_ncrandint(a,I);
//amsclil1_fb32_ncrandint(b,J);
if(I>=J) B = I; else B = J;
res = test_div_stress_test_sub(a,b,B);
if(res<=0)
{
printf("stopping on test failure...\n");
exit(0);
}
}
}
}
printf("Test series 3: division of zero\n");
for(I=1;I<=blenmax;I++)
{
for(J=1;J<=blenmax;J++)
{
printf("\t divisions (%d,%d)\n",(int)I,(int)J);
for(L=0;L<Nt;L++)
{
for(K=0;K<blenmax;K++)
{
a[K] = 0;
b[K] = 0;
}
//amsclil1_fb32_ncrandint(a,I);
amsclil1_fb32_ncrandint(b,J);
if(I>=J) B = I; else B = J;
res = test_div_stress_test_sub(a,b,B);
if(res<=0)
{
printf("stopping on test failure...\n");
exit(0);
}
}
}
}
printf("Test series 4: zero div zero\n");
for(I=1;I<=blenmax;I++)
{
for(J=1;J<=blenmax;J++)
{
printf("\t divisions (%d,%d)\n",(int)I,(int)J);
for(L=0;L<Nt;L++)
{
for(K=0;K<blenmax;K++)
{
a[K] = 0;
b[K] = 0;
}
//amsclil1_fb32_ncrandint(a,I);
//amsclil1_fb32_ncrandint(b,J);
if(I>=J) B = I; else B = J;
res = test_div_stress_test_sub(a,b,B);
if(res<=0)
{
printf("stopping on test failure...\n");
exit(0);
}
}
}
}
free(a);
free(b);
return;
}
void test_ui64bitshifts()
{
printf("Testing various aspects of uint64_t bit shifting...\n");
uint64_t a,b,c,d;
a = 0x01LL<<16;
b = 0x01LL<<32;
c = ((uint64_t)0x01)<<48;
d = ((uint64_t)0x01)<<63;
printf("%llx\n",(long long unsigned) a);
printf("%llx\n",(long long unsigned) b);
printf("%llx\n",(long long unsigned) c);
printf("%llx\n",(long long unsigned) d);
printf("%llx\n",(long long unsigned) (a+b+c+d));
return;
}
static int amsclil1_test_fb32_lcm1_sub()
{
int ret = 1;
int N = 2048/32;
uint32_t *a=NULL;
uint32_t *b=NULL;
uint32_t *c=NULL;
uint32_t *q=NULL;
uint32_t *r=NULL;
uint32_t *wrk=NULL;
int res1,res2;
a = (uint32_t*) malloc(sizeof(uint32_t)*N);
b = (uint32_t*) malloc(sizeof(uint32_t)*N);
c = (uint32_t*) malloc(sizeof(uint32_t)*2*(N));
q = (uint32_t*) malloc(sizeof(uint32_t)*2*N);
r = (uint32_t*) malloc(sizeof(uint32_t)*2*N);
wrk = (uint32_t*) malloc(sizeof(uint32_t)*14*(N+1));
amsclil1_fb32_ncrandint(a,N);
amsclil1_fb32_ncrandint(b,N);
amsclil1_fb32_lcm(a,b,c,wrk,N);
amsclil1_fb32_div(c,a,q,r,wrk,2*N);
res1 = amsclil1_fb32_iszero(r,2*N);
amsclil1_fb32_div(c,b,q,r,wrk,2*N);
res2 = amsclil1_fb32_iszero(r,2*N);
if(res1!=1 || res2!=1)
{
ret = 0;
}
free(a);
free(b);
free(c);
free(q);
free(r);
free(wrk);
return ret;
}
void amsclil1_test_fb32_lcm1()
{
printf("Test LCM:\n\n");
int I;
int N = 1000;
int ret;
printf("Perofmring %d tests of LCM function\n",N);
for(I=0;I<N;I++)
{
ret = amsclil1_test_fb32_lcm1_sub();
if(ret!=1)
{
printf("LCM Tests failed at run %d of %d\n",I,N);
break;
}
}
if(ret==1)
{
printf("All tests passed.\n");
}
return;
}

@ -0,0 +1,287 @@
#include <amsclil1/amsclil1.h>
int amsclil1_ulint_new(amsclil1_ulint **pnt)
{
int ret = 0;
if(*pnt!=NULL) amsclil1_ulint_delete(pnt);
*pnt = malloc(sizeof(amsclil1_ulint));
(*pnt)->N = 0;
(*pnt)->data = malloc(sizeof(uint32_t)*1);
if((*pnt)->data==NULL)
{
ret = -1;
return ret;
}
ret = 1;
(*pnt)->data[0] = 0;
return ret;
}
void amsclil1_ulint_delete(amsclil1_ulint **pnt)
{
if(*pnt==NULL) return;
if((*pnt)->data!=NULL) {free((*pnt)->data); (*pnt)->data=NULL;}
(*pnt)->N = 0;
free(*pnt); *pnt=NULL;
return;
}
int amsclil1_ulint_init(amsclil1_ulint *pnt)
{
int ret = 0;
if(pnt==NULL)
{
ret = -1; return ret;
}
amsclil1_ulint_cleanup(pnt);
pnt->N = 0;
pnt->data = malloc(sizeof(uint32_t)*1);
pnt->data[0] = 0;
if(pnt->data==NULL)
{
ret = -1;
}
else
{
ret = 1;
}
return ret;
}
void amsclil1_ulint_cleanup(amsclil1_ulint *pnt)
{
if(pnt!=NULL)
{
if(pnt->data!=NULL) {free(pnt->data); pnt->data=NULL;}
pnt->N = 0;
}
return;
}
int amsclil1_ulint_resize(amsclil1_ulint *pnt, int _N)
{
int ret = 0;
int I = 0;
uint32_t *newdata = NULL;
if(pnt==NULL) {ret = -1; return ret;}
if(_N<=0)
{
_N = 0;
newdata = malloc(sizeof(uint32_t)*1);
newdata[0] = 0;
}
else
{
newdata = malloc(sizeof(uint32_t)*_N);
}
if(newdata==NULL)
{
ret = -2;
return ret;
}
if(pnt->data==NULL)
{
pnt->data = malloc(sizeof(uint32_t)*1);
pnt->data[0] = 0;
pnt->N = 0;
}
if(pnt->data!=NULL)
{
for(I=0;I<pnt->N && I<_N;I++)
{
newdata[I] = pnt->data[I];
}
for(I=pnt->N;I<_N;I++)
{
newdata[I] = 0;
}
}
else
{
for(I=0;I<_N;I++)
{
newdata[I] = 0;
}
}
if(pnt->data!=NULL) {free(pnt->data); pnt->data=NULL;}
pnt->data = newdata;
pnt->N = _N;
ret = 1;
return ret;
}
int amsclil1_ulint_msindex(amsclil1_ulint *pnt)
{
int ret = 0;
int I = 0;
if(pnt==NULL) return ret;
for(I=pnt->N-1;I>=0;I--)
{
if(pnt->data[I]!=0)
{
ret = I;
break;
}
}
return ret;
}
int amsclil1_ulint_shrinktofit(amsclil1_ulint *pnt)
{
int ret = 0;
int msindex = 0;
if(pnt==NULL) return -1;
msindex = amsclil1_ulint_msindex(pnt);
ret = amsclil1_ulint_resize(pnt,msindex+1);
return ret;
}
void amsclil1_ulint_printhex(amsclil1_ulint *pnt)
{
int I = 0;
if(pnt==NULL) return;
printf("0x");
for(I=pnt->N-1;I>=0;I--)
{
printf("%08x",pnt->data[I]);
}
return;
}
void amsclil1_ulint_printbits(amsclil1_ulint *pnt)
{
int I = 0;
int J = 0;
if(pnt==NULL) return;
printf("0b ");
for(I=pnt->N-1;I>=0;I--)
{
for(J=31;J>=0;J--)
{
printf("%d", ((pnt->data[I]>>J) & 1) );
}
printf(" ");
}
return;
}
void amsclil1_ulint_clear(amsclil1_ulint *pnt)
{
int I = 0;
for(I=0;I<pnt->N;I++)
{
pnt->data[I] = 0;
}
return;
}
//If _N is greater than current size, expand to larger size, otherwise do nothing
int amsclil1_ulint_resizetomin(amsclil1_ulint *pnt, int _N)
{
int ret = 0;
if(pnt!=NULL)
{
ret = 1;
if(_N>pnt->N)
{
ret = amsclil1_ulint_resize(pnt,_N);
}
}
return ret;
}
int amsclil1_ulint_copy(amsclil1_ulint *dest, amsclil1_ulint *src)
{
int ret = 0;
int res = 0;
int I;
if(dest==NULL)
{
ret = -1;
return ret;
}
if(src==NULL)
{
amsclil1_ulint_clear(dest);
ret = -1;
return ret;
}
if(dest==src)
{
ret = 1;
return ret;
}
res = amsclil1_ulint_resizetomin(dest,src->N);
if(res<0)
{
ret = -1;
return ret;
}
amsclil1_ulint_clear(dest);
for(I=0;I<src->N;I++)
{
dest->data[I] = src->data[I];
}
return ret;
}
void amsclil1_ulint_tests1()
{
amsclil1_ulint *a = NULL;
amsclil1_ulint *b = NULL;
amsclil1_ulint_new(&a);
amsclil1_ulint_new(&b);
amsclil1_ulint_resize(a,2);
amsclil1_ulint_resize(b,2);
a->data[1] = 0x0000ffff;
a->data[0] = 1;
b->data[1] = 0x0000ff01;
printf("a="); amsclil1_ulint_printhex(a); printf("\n");
printf("b="); amsclil1_ulint_printbits(b); printf("\n");
amsclil1_ulint_resize(a,1);
amsclil1_ulint_copy(b,a);
printf("a="); amsclil1_ulint_printhex(a); printf("\n");
printf("b="); amsclil1_ulint_printbits(b); printf("\n");
amsclil1_ulint_delete(&a);
amsclil1_ulint_delete(&b);
return;
}

@ -0,0 +1,157 @@
#include <amsclil1/amsclil1.h>
// Unsigned Large Integer Comparison Operator:
// 0 - a==b
// 1 - a>b
// 2 - a<b
int amsclil1_ulint_cmp(amsclil1_ulint *a, amsclil1_ulint *b)
{
int ret = 0;
int msia = amsclil1_ulint_msindex(a);
int msib = amsclil1_ulint_msindex(b);
int msimax;
int I;
uint32_t ad,bd;
if(msia>msib) msimax = msia; else msimax = msib;
for(I=msimax;I>=0;I--)
{
if(I<a->N) ad = a->data[I]; else ad = 0;
if(I<b->N) bd = b->data[I]; else bd = 0;
if(ad>bd)
{
ret = 1;
break;
}
if(bd>ad)
{
ret = 2;
break;
}
}
return ret;
}
int amsclil1_ulint_iszero(amsclil1_ulint *a)
{
int ret = 1;
int I;
for(I=a->N-1;I>=0;I--)
{
if(a->data[I]!=0)
{
ret = 0;
break;
}
}
return ret;
}
int amsclil1_ulint_add(amsclil1_ulint *a, amsclil1_ulint *b, amsclil1_ulint *c)
{
int ret = 1;
amsclil1_ulint *aint = NULL;
amsclil1_ulint *bint = NULL;
int msia,msib,msimax;
if(c==NULL)
{
ret = 0;
return ret;
}
msia = amsclil1_ulint_msindex(a);
msib = amsclil1_ulint_msindex(b);
if(msia>msib) msimax = msia; else msimax = msib;
amsclil1_ulint_new(&aint);
amsclil1_ulint_new(&bint);
amsclil1_ulint_resize(aint,msimax+1);
amsclil1_ulint_resize(bint,msimax+1);
amsclil1_ulint_copy(aint,a);
amsclil1_ulint_copy(bint,b);
amsclil1_ulint_resize(c,msimax+1);
amsclil1_ulint_clear(c);
amsclil1_fb32_add(aint->data,bint->data,c->data,msimax+1);
amsclil1_ulint_delete(&aint);
amsclil1_ulint_delete(&bint);
return ret;
}
int amsclil1_ulint_sub(amsclil1_ulint *a, amsclil1_ulint *b, amsclil1_ulint *c)
{
int ret = 1;
amsclil1_ulint *aint = NULL;
amsclil1_ulint *bint = NULL;
int msia,msib,msimax;
if(c==NULL)
{
ret = 0;
return ret;
}
msia = amsclil1_ulint_msindex(a);
msib = amsclil1_ulint_msindex(b);
if(msia>msib) msimax = msia; else msimax = msib;
amsclil1_ulint_new(&aint);
amsclil1_ulint_new(&bint);
amsclil1_ulint_resize(aint,msimax);
amsclil1_ulint_resize(bint,msimax);
amsclil1_ulint_copy(aint,a);
amsclil1_ulint_copy(bint,b);
amsclil1_ulint_resize(c,msimax);
amsclil1_ulint_clear(c);
amsclil1_fb32_sub(aint->data,bint->data,c->data,msimax);
amsclil1_ulint_delete(&aint);
amsclil1_ulint_delete(&bint);
return ret;
}
int amsclil1_ulint_mult(amsclil1_ulint *a, amsclil1_ulint *b, amsclil1_ulint *c)
{
int ret = 1;
amsclil1_ulint *aint = NULL;
amsclil1_ulint *bint = NULL;
int msia,msib,msimax;
if(c==NULL)
{
ret = 0;
return ret;
}
msia = amsclil1_ulint_msindex(a);
msib = amsclil1_ulint_msindex(b);
if(msia>msib) msimax = msia; else msimax = msib;
amsclil1_ulint_new(&aint);
amsclil1_ulint_new(&bint);
amsclil1_ulint_resize(aint,msimax);
amsclil1_ulint_resize(bint,msimax);
amsclil1_ulint_copy(aint,a);
amsclil1_ulint_copy(bint,b);
amsclil1_ulint_resize(c,msimax);
amsclil1_ulint_clear(c);
amsclil1_fb32_sub(aint->data,bint->data,c->data,msimax);
amsclil1_ulint_delete(&aint);
amsclil1_ulint_delete(&bint);
return ret;
}

@ -0,0 +1,39 @@
#include <amsclil1/amsclil1.h>
int main(int argc, char* argv[])
{
printf("AMS C Large Integer Library Tests.\n");
//test_fbops_printcmp();
//test_fbops_shiftread();
//test_fbops_shiftwrite();
//test_fbops_lshiftrshift();
//test_modtdiv();
//test_addsub();
//test_mult();
//test_div();
//test_modpower();
//test_mult_time();
//test_div_time();
//test_barrettmod();
//barret_modpower_test();
//barrett_mod_fuzztest();
//modpower_timetests();
//test_amsclil1_fb32_eulergcd();
//test_fb16_comparison_sub();
//test_div_stress_test(); //<---revisit
//test_div_stress_test2();
//test_ui64bitshifts();
//amsclil1_ulint_tests1();
//amsclil1_test_fb32_lcm1();
return 0;
}
Loading…
Cancel
Save