diff --git a/source/curve/pairing/bls_pair.cpp b/source/curve/pairing/bls_pair.cpp index ffdae80..1908cdd 100644 --- a/source/curve/pairing/bls_pair.cpp +++ b/source/curve/pairing/bls_pair.cpp @@ -1,6 +1,6 @@ - /*************************************************************************** - * +This file has been modified by Raytheon BBN Technologies - January 2013. * + Copyright 2013 CertiVox IOM Ltd. * * This file is part of CertiVox MIRACL Crypto SDK. * @@ -1353,6 +1353,188 @@ void GT::restore(char *bytes) delete [] bytes; } +/* + * Serialization method for the points + * + */ +int GT::serialize(char *& bytes) +{ + int j=0; + int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1); + int len=24*bytes_per_big; + ZZn8 a,b,c; + ZZn4 f,s; + ZZn2 p,q; + Big x,y; + + bytes=new char[len]; + + g.get(a,b,c); + a.get(f,s); + f.get(p,q); + p.get(x,y); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + q.get(x,y); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + s.get(p,q); + p.get(x,y); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + q.get(x,y); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + b.get(f,s); + f.get(p,q); + p.get(x,y); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + q.get(x,y); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + s.get(p,q); + p.get(x,y); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + q.get(x,y); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + c.get(f,s); + f.get(p,q); + p.get(x,y); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + q.get(x,y); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + s.get(p,q); + p.get(x,y); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + q.get(x,y); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + + return len; +} +/* + * Deserialization method for the points + * This will reset the element precomp + * + */ +void GT::deserialize(char *bytes) +{ + int j=0; + int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1); + int len=24*bytes_per_big; + ZZn8 a,b,c; + ZZn4 f,s; + ZZn2 p,q; + Big x,y; + if (etable!=NULL){ + delete [] etable; + etable = NULL; + } + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + p.set(x,y); + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + q.set(x,y); + f.set(p,q); + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + p.set(x,y); + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + q.set(x,y); + s.set(p,q); + a.set(f,s); + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + p.set(x,y); + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + q.set(x,y); + f.set(p,q); + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + p.set(x,y); + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + q.set(x,y); + s.set(p,q); + b.set(f,s); + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + p.set(x,y); + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + q.set(x,y); + f.set(p,q); + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + p.set(x,y); + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + q.set(x,y); + s.set(p,q); + c.set(f,s); + + g.set(a,b,c); + + delete [] bytes; +} + G1 operator+(const G1& x,const G1& y) { G1 z=x; @@ -1418,6 +1600,52 @@ void G1::restore(char *bytes) delete [] bytes; } +/* + * Serialization method for the point x,y + * + */ +int G1::serialize(char *& bytes) +{ + int j=0; + int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1); + int len=2*bytes_per_big; + Big x,y; + + bytes=new char[len]; + + g.get(x,y); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + + return len; +} +/* + * Deserialization method for the point x,y + * This will reset the element to point x,y + * + */ +void G1::deserialize(char *bytes) +{ + int j=0; + int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1); + int len=2*bytes_per_big; + Big x,y; + //reset precomp + if (mtable!=NULL){ + delete [] mtable; + mtable=NULL; + } + + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + g.set(x,y); + + delete [] bytes; +} + + G2 operator+(const G2& x,const G2& y) { G2 z=x; @@ -1524,6 +1752,91 @@ void G2::restore(char *bytes) delete [] bytes; } +/* + * Serialization method for the points + * + */ +int G2::serialize(char *& bytes) +{ + int j=0; + int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1); + int len=8*bytes_per_big; + ZZn4 a,b; + ZZn2 f,s; + Big x,y; + + bytes=new char[len]; + + g.get(a,b); + a.get(f,s); + f.get(x,y); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + s.get(x,y); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + b.get(f,s); + f.get(x,y); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + s.get(x,y); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + + return len; +} +/* + * Deserialization method for the points + * This will reset the element precomp + * + */ +void G2::deserialize(char *bytes) +{ + int j=0; + int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1); + int len=8*bytes_per_big; + ZZn4 a,b; + ZZn2 f,s; + Big x,y; + //reset precomp + if (mtable!=NULL){ + delete [] mtable; + mtable=NULL; + } + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + f.set(x,y); + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + s.set(x,y); + a.set(f,s); + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + f.set(x,y); + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + s.set(x,y); + b.set(f,s); + g.set(a,b); + + delete [] bytes; +} // Fast group membership check for GT // check if r is of order q diff --git a/source/curve/pairing/bn_pair.cpp b/source/curve/pairing/bn_pair.cpp index b5a0ff1..02bffca 100644 --- a/source/curve/pairing/bn_pair.cpp +++ b/source/curve/pairing/bn_pair.cpp @@ -1,7 +1,8 @@ /*************************************************************************** - * -Copyright 2013 CertiVox IOM Ltd. * +This file has been modified by Raytheon BBN Technologies - January 2013. * + * +Copyright 2013 CertiVox IOM Ltd. * This file is part of CertiVox MIRACL Crypto SDK. * * @@ -1234,6 +1235,59 @@ int GT::spill(char *& bytes) return len; } +/* + * jkhoury@bbn.com + * Serialization method for the points + * + */ +int GT::serialize(char *& bytes) +{ + int j=0; + int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1); + int len=12*bytes_per_big; + ZZn4 a,b,c; + ZZn2 f,s; + Big x,y; + + bytes=new char[len]; + + g.get(a,b,c); + a.get(f,s); + f.get(x,y); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + s.get(x,y); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + b.get(f,s); + f.get(x,y); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + s.get(x,y); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + c.get(f,s); + f.get(x,y); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + s.get(x,y); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + + return len; +} + // // restore precomputation for GT from byte array // @@ -1289,6 +1343,62 @@ void GT::restore(char *bytes) delete [] bytes; } +/* + * jkhoury@bbn.com + * Deserialization method for the points + * This will reset the element precomp + * + */ +void GT::deserialize(char *bytes) +{ + int j=0; + int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1); + int len=12*bytes_per_big; + ZZn4 a,b,c; + ZZn2 f,s; + Big x,y; + if (etable!=NULL){ + delete [] etable; + etable = NULL; + } + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + f.set(x,y); + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + s.set(x,y); + a.set(f,s); + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + f.set(x,y); + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + s.set(x,y); + b.set(f,s); + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + f.set(x,y); + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + s.set(x,y); + c.set(f,s); + g.set(a,b,c); + + delete [] bytes; +} + // // spill precomputation on G1 to byte array // @@ -1340,6 +1450,53 @@ void G1::restore(char *bytes) delete [] bytes; } +/* + * jkhoury@bbn.com + * Serialization method for the point x,y + * + */ +int G1::serialize(char *& bytes) +{ + int j=0; + int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1); + int len=2*bytes_per_big; + Big x,y; + + bytes=new char[len]; + + g.get(x,y); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + + return len; +} +/* + * jkhoury@bbn.com + * Deserialization method for the point x,y + * This will reset the element to point x,y + * + */ +void G1::deserialize(char *bytes) +{ + int j=0; + int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1); + int len=2*bytes_per_big; + Big x,y; + //reset precomp + if (mtable!=NULL){ + delete [] mtable; + mtable=NULL; + } + + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + g.set(x,y); + + delete [] bytes; +} + G1 operator+(const G1& x,const G1& y) { G1 z=x; @@ -1392,6 +1549,35 @@ int G2::spill(char *& bytes) // restore precomputation for G2 from byte array // +/* + * jkhoury@bbn.com + * Serialization method for the 2 points + * + */ +int G2::serialize(char *& bytes) +{ + int j=0; + int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1); + int len=4*bytes_per_big; + Big x,y; + ZZn2 a,b; + + bytes=new char[len]; + + g.get(a,b); + a.get(x,y); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + b.get(x,y); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + + return len; +} + void G2::restore(char *bytes) { int i,j,n=(1<nib-1); + int len=4*bytes_per_big; + ZZn2 a,b; + Big x,y; + //reset precomp + if (mtable!=NULL){ + delete [] mtable; + mtable=NULL; + } + + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + a.set(x,y); + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + b.set(x,y); + g.set(a,b); + + delete [] bytes; +} + G2 operator+(const G2& x,const G2& y) { G2 z=x; diff --git a/source/curve/pairing/cp_pair.cpp b/source/curve/pairing/cp_pair.cpp index 045bb4a..6824ded 100644 --- a/source/curve/pairing/cp_pair.cpp +++ b/source/curve/pairing/cp_pair.cpp @@ -1,5 +1,7 @@ /* - * + +This file has been modified by Raytheon BBN Technologies - January 2013. + * cp_pair.cpp * * Cocks-Pinch curve, Tate pairing embedding degree 2, ideal for security level AES-80 @@ -751,6 +753,53 @@ int GT::spill(char *& bytes) // restore precomputation for GT from byte array // +/* + * jkhoury@bbn.com + * Serialization method for the points + * + */ +int GT::serialize(char *& bytes) +{ + int j=0; + int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1); + int len=2*bytes_per_big; + Big x,y; + + bytes=new char[len]; + + g.get(x,y); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + + return len; +} +/* + * jkhoury@bbn.com + * Deserialization method for the points + * This will reset the element precomp + * + */ +void GT::deserialize(char *bytes) +{ + int j=0; + int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1); + int len=2*bytes_per_big; + Big x,y; + if (etable!=NULL){ + delete [] etable; + etable = NULL; + } + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + g.set(x,y); + + delete [] bytes; +} + void GT::restore(char *bytes) { int i,j,n=(1<nib-1); + int len=2*bytes_per_big; + Big x,y; + + bytes=new char[len]; + + g.get(x,y); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + + return len; +} +/* + * jkhoury@bbn.com + * Deserialization method for the point x,y + * This will reset the element to point x,y + * + */ +void G1::deserialize(char *bytes) +{ + int j=0; + int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1); + int len=2*bytes_per_big; + Big x,y; + //reset precomp + if (mtable!=NULL){ + delete [] mtable; + mtable=NULL; + } + + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + g.set(x,y); + + delete [] bytes; +} + G2 operator+(const G2& x,const G2& y) { G2 z=x; @@ -912,6 +1008,66 @@ void G2::restore(char *bytes) delete [] bytes; } +// +// jkhoury@bbn.com +// serialize element to byte array +// + +int G2::serialize(char *& bytes) +{ + int j=0; + int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1); + int len=2*bytes_per_big; + Big x,y; + + bytes=new char[len]; + + + g.get(x,y); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + //x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + + return len; +} + +// jkhoury@bbn.com +// restore element from byte array +// reset the precomp +// + +void G2::deserialize(char *bytes) +{ + int j=0; + int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1); + int len=2*bytes_per_big; + Big x,y,B; + //reset precomp + if (mtable!=NULL){ + delete [] mtable; + mtable=NULL; + } + + + B=getB(); + B=-B; + ecurve((Big)-3,B,get_modulus(),MR_PROJECTIVE); // move to twist + + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + + g.set(x,y); + + + B=-B; + ecurve((Big)-3,B,get_modulus(),MR_PROJECTIVE); // move back + delete [] bytes; +} + BOOL PFC::member(const GT& z) { ZZn2 r=z.g; diff --git a/source/curve/pairing/kss_pair.cpp b/source/curve/pairing/kss_pair.cpp index 09a6d47..50199cd 100644 --- a/source/curve/pairing/kss_pair.cpp +++ b/source/curve/pairing/kss_pair.cpp @@ -1,6 +1,7 @@ /*************************************************************************** - * +This file has been modified by Raytheon BBN Technologies - January 2013. * + * Copyright 2013 CertiVox IOM Ltd. * * This file is part of CertiVox MIRACL Crypto SDK. * @@ -1399,6 +1400,146 @@ int GT::spill(char *& bytes) // restore precomputation for GT from byte array // +/* + * jkhoury@bbn.com + * Serialization method for the points + * + */ +int GT::serialize(char *& bytes) +{ + int j=0; + int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1); + int len=18*bytes_per_big; + ZZn6 a,b,c; + ZZn3 f,s; + ZZn x,y,z; + + bytes=new char[len]; + + g.get(a,b,c); + a.get(f,s); + f.get(x,y,z); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(z,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + s.get(x,y,z); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(z,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + b.get(f,s); + f.get(x,y,z); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(z,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + + s.get(x,y,z); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(z,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + + c.get(f,s); + f.get(x,y,z); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(z,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + + s.get(x,y,z); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(z,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + + return len; +} +/* + * jkhoury@bbn.com + * Deserialization method for the points + * This will reset the element precomp + * + */ +void GT::deserialize(char *bytes) +{ + int j=0; + int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1); + int len=18*bytes_per_big; + ZZn6 a,b,c; + ZZn3 f,s; + ZZn x,y,z; + if (etable!=NULL){ + delete [] etable; + etable = NULL; + } + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + z=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + f.set(x,y,z); + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + z=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + s.set(x,y,z); + a.set(f,s); + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + z=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + + f.set(x,y,z); + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + z=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + + s.set(x,y,z); + b.set(f,s); + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + z=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + + f.set(x,y,z); + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + z=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + + s.set(x,y,z); + c.set(f,s); + g.set(a,b,c); + + delete [] bytes; +} + void GT::restore(char *bytes) { int i,j,n=(1<nib-1); + int len=2*bytes_per_big; + Big x,y; + + bytes=new char[len]; + + g.get(x,y); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + + return len; +} +/* + * jkhoury@bbn.com + * Deserialization method for the point x,y + * This will reset the element to point x,y + * + */ +void G1::deserialize(char *bytes) +{ + int j=0; + int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1); + int len=2*bytes_per_big; + Big x,y; + //reset precomp + if (mtable!=NULL){ + delete [] mtable; + mtable=NULL; + } + + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + g.set(x,y); + + delete [] bytes; +} + + G2 operator+(const G2& x,const G2& y) { G2 z=x; @@ -1621,6 +1810,78 @@ void G2::restore(char *bytes) } +/* + * jkhoury@bbn.com + * Serialization method for the 2 points + * + */ +int G2::serialize(char *& bytes) +{ + int j=0; + int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1); + int len=6*bytes_per_big; + ZZn3 x,y; + ZZn a,b,c; + + + bytes=new char[len]; + + g.get(x,y); + x.get(a,b,c); + to_binary((Big)a,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary((Big)b,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary((Big)c,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + y.get(a,b,c); + to_binary((Big)a,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary((Big)b,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary((Big)c,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + + return len; +} +/* + * jkhoury@bbn.com + * Deserialization method for the 2 points + * This will reset the element precomp + * + */ +void G2::deserialize(char *bytes) +{ + int j=0; + int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1); + int len=6*bytes_per_big; + ZZn3 x,y; + ZZn a,b,c; + //reset precomp + if (mtable!=NULL){ + delete [] mtable; + mtable=NULL; + } + + a=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + b=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + c=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + x.set(a,b,c); + a=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + b=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + c=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y.set(a,b,c); + g.set(x,y); + + delete [] bytes; +} + // test if a ZZn18 element is of order q // can't think of a faster way to do this.. diff --git a/source/curve/pairing/mnt_pair.cpp b/source/curve/pairing/mnt_pair.cpp index 2922767..490022b 100644 --- a/source/curve/pairing/mnt_pair.cpp +++ b/source/curve/pairing/mnt_pair.cpp @@ -1,6 +1,7 @@ /*************************************************************************** - * +This file has been modified by Raytheon BBN Technologies - January 2013. * + * Copyright 2013 CertiVox IOM Ltd. * * This file is part of CertiVox MIRACL Crypto SDK. * @@ -984,6 +985,78 @@ int GT::spill(char *& bytes) return len; } +/* + * jkhoury@bbn.com + * Serialization method for the points + * + */ +int GT::serialize(char *& bytes) +{ + int j=0; + int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1); + int len=6*bytes_per_big; + ZZn2 a,b,c; + Big x,y; + + bytes=new char[len]; + + g.get(a,b,c); + a.get(x,y); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + b.get(x,y); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + c.get(x,y); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + + return len; +} +/* + * jkhoury@bbn.com + * Deserialization method for the points + * This will reset the element precomp + * + */ +void GT::deserialize(char *bytes) +{ + int j=0; + int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1); + int len=6*bytes_per_big; + ZZn2 a,b,c; + Big x,y; + if (etable!=NULL){ + delete [] etable; + etable = NULL; + } + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + a.set(x,y); + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + b.set(x,y); + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + c.set(x,y); + + g.set(a,b,c); + + delete [] bytes; +} + // // restore precomputation for GT from byte array // @@ -1086,6 +1159,53 @@ void G1::restore(char *bytes) delete [] bytes; } +/* + * jkhoury@bbn.com + * Serialization method for the point x,y + * + */ +int G1::serialize(char *& bytes) +{ + int j=0; + int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1); + int len=2*bytes_per_big; + Big x,y; + + bytes=new char[len]; + + g.get(x,y); + to_binary(x,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary(y,bytes_per_big,&bytes[j],TRUE); + + return len; +} +/* + * jkhoury@bbn.com + * Deserialization method for the point x,y + * This will reset the element to point x,y + * + */ +void G1::deserialize(char *bytes) +{ + int j=0; + int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1); + int len=2*bytes_per_big; + Big x,y; + //reset precomp + if (mtable!=NULL){ + delete [] mtable; + mtable=NULL; + } + + x=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y=from_binary(bytes_per_big,&bytes[j]); + g.set(x,y); + + delete [] bytes; +} + G2 operator+(const G2& x,const G2& y) { G2 z=x; @@ -1173,6 +1293,78 @@ void G2::restore(char *bytes) delete [] bytes; } +/* + * jkhoury@bbn.com + * Serialization method for the points + * + */ +int G2::serialize(char *& bytes) +{ + int j=0; + int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1); + int len=6*bytes_per_big; + ZZn3 x,y; + ZZn a,b,c; + + + bytes=new char[len]; + + g.get(x,y); + x.get(a,b,c); + to_binary((Big)a,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary((Big)b,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary((Big)c,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + y.get(a,b,c); + to_binary((Big)a,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary((Big)b,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + to_binary((Big)c,bytes_per_big,&bytes[j],TRUE); + j+=bytes_per_big; + + return len; +} +/* + * jkhoury@bbn.com + * Deserialization method for the points + * This will reset the element precomp + * + */ +void G2::deserialize(char *bytes) +{ + int j=0; + int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1); + int len=6*bytes_per_big; + ZZn3 x,y; + ZZn a,b,c; + //reset precomp + if (mtable!=NULL){ + delete [] mtable; + mtable=NULL; + } + + a=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + b=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + c=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + x.set(a,b,c); + a=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + b=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + c=from_binary(bytes_per_big,&bytes[j]); + j+=bytes_per_big; + y.set(a,b,c); + g.set(x,y); + + delete [] bytes; +} + BOOL PFC::member(const GT& z) { ZZn6 r=z.g; diff --git a/source/curve/pairing/pairing_3.h b/source/curve/pairing/pairing_3.h index ad102c1..71adca3 100644 --- a/source/curve/pairing/pairing_3.h +++ b/source/curve/pairing/pairing_3.h @@ -1,4 +1,3 @@ - /*************************************************************************** * Copyright 2013 CertiVox IOM Ltd. * @@ -131,6 +130,13 @@ class G1 } int spill(char *&); void restore(char *); + /* + * Added serialization methods for the element + * Spill/restore serialize the precomp only + */ + int serialize(char *&); + void deserialize(char *); + friend G1 operator-(const G1&); friend G1 operator+(const G1&,const G1&); friend BOOL operator==(const G1& x,const G1& y) @@ -164,6 +170,12 @@ class G2 } int spill(char *&); void restore(char *); + /* + * Added serialization methods for the element + * Spill/restore serialize the precomp only + */ + int serialize(char *&); + void deserialize(char *); friend G2 operator-(const G2&); friend G2 operator+(const G2&,const G2&); @@ -196,6 +208,13 @@ class GT } int spill(char *&); void restore(char *); + /* + * Added serialization methods for the element + * Spill/restore serialize the precomp only + */ + int serialize(char *&); + void deserialize(char *); + friend GT operator*(const GT&,const GT&); friend GT operator/(const GT&,const GT&); friend BOOL operator==(const GT& x,const GT& y) diff --git a/source/curve/pairing/zzn4.h b/source/curve/pairing/zzn4.h index 8044ee9..3317b8d 100644 --- a/source/curve/pairing/zzn4.h +++ b/source/curve/pairing/zzn4.h @@ -1,4 +1,3 @@ - /*************************************************************************** * Copyright 2013 CertiVox IOM Ltd. * @@ -191,8 +190,8 @@ class ZZn4 #endif } }; -//#ifndef MR_NO_RAND -//extern ZZn4 randn4(void); -//#endif +#ifndef MR_NO_RAND +extern ZZn4 randn4(void); +#endif #endif diff --git a/update.txt b/update.txt index 63f684d..5b5d3ce 100644 --- a/update.txt +++ b/update.txt @@ -1,4 +1,3 @@ - Version 4.0 The main changes from the earlier version 3.xx is the introduction of the @@ -440,3 +439,7 @@ Version 5.6.1 New MIRACL module mrzzn4.c +Version 5.6.1.1 (Raytheon BBN Technologies) + +Added functions serialize() and deserialize() to all pairing +files {cp,mnt,bn,bls,kss}_pair.cpp and to header