5 #ifndef CRYPTOPP_IMPORTS
12 NAMESPACE_BEGIN(CryptoPP)
14 void TestInstantiations_gfpcrypt()
36 int modulusSize = 1024;
39 if (!DSA::IsValidPrimeLength(modulusSize))
53 h.Randomize(rng, 2, p-2);
54 g = a_exp_b_mod_c(h, (p-1)/q, p);
64 pass = pass && DSA::IsValidPrimeLength(GetModulus().BitCount());
65 pass = pass && GetSubgroupOrder().
BitCount() == 160;
70 const byte *recoverableMessage,
size_t recoverableMessageLength,
72 byte *representative,
size_t representativeBitLength)
const
74 assert(recoverableMessageLength == 0);
75 assert(hashIdentifier.second == 0);
76 const size_t representativeByteLength = BitsToBytes(representativeBitLength);
78 const size_t paddingLength = SaturatingSubtract(representativeByteLength, digestSize);
80 memset(representative, 0, paddingLength);
81 hash.
TruncatedFinal(representative+paddingLength, STDMIN(representativeByteLength, digestSize));
83 if (digestSize*8 > representativeBitLength)
85 Integer h(representative, representativeByteLength);
86 h >>= representativeByteLength*8 - representativeBitLength;
87 h.Encode(representative, representativeByteLength);
92 const byte *recoverableMessage,
size_t recoverableMessageLength,
94 byte *representative,
size_t representativeBitLength)
const
96 assert(recoverableMessageLength == 0);
97 assert(hashIdentifier.second == 0);
98 const size_t representativeByteLength = BitsToBytes(representativeBitLength);
100 const size_t paddingLength = SaturatingSubtract(representativeByteLength, digestSize);
102 memset(representative, 0, paddingLength);
103 hash.
TruncatedFinal(representative+paddingLength, STDMIN(representativeByteLength, digestSize));
105 if (digestSize*8 >= representativeBitLength)
107 Integer h(representative, representativeByteLength);
108 h >>= representativeByteLength*8 - representativeBitLength + 1;
109 h.Encode(representative, representativeByteLength);
113 bool DL_GroupParameters_IntegerBased::ValidateGroup(
RandomNumberGenerator &rng,
unsigned int level)
const
115 const Integer &p = GetModulus(), &q = GetSubgroupOrder();
124 pass = pass && VerifyPrime(rng, q, level-2) && VerifyPrime(rng, p, level-2);
131 const Integer &p = GetModulus(), &q = GetSubgroupOrder();
134 pass = pass && GetFieldType() == 1 ? g.IsPositive() : g.NotNegative();
135 pass = pass && g < p && !IsIdentity(g);
140 pass = pass && gpc->Exponentiate(GetGroupPrecomputation(),
Integer::One()) == g;
144 if (GetFieldType() == 2)
145 pass = pass && Jacobi(g*g-4, p)==-1;
149 bool fullValidate = (GetFieldType() == 2 && level >= 3) || !FastSubgroupCheckAvailable();
151 if (fullValidate && pass)
153 Integer gp = gpc ? gpc->Exponentiate(GetGroupPrecomputation(), q) : ExponentiateElement(g, q);
154 pass = pass && IsIdentity(gp);
156 else if (GetFieldType() == 1)
157 pass = pass && Jacobi(g, p) == 1;
173 int modulusSize, subgroupOrderSize;
178 if (!alg.
GetIntValue(
"SubgroupOrderSize", subgroupOrderSize))
179 subgroupOrderSize = GetDefaultSubgroupOrderSize(modulusSize);
182 pg.Generate(GetFieldType() == 1 ? 1 : -1, rng, modulusSize, subgroupOrderSize);
191 Integer DL_GroupParameters_IntegerBased::DecodeElement(
const byte *encoded,
bool checkForGroupMembership)
const
193 Integer g(encoded, GetModulus().ByteCount());
194 if (!ValidateElement(1, g, NULL))
205 if (parameters.EndReached())
208 q = ComputeGroupOrder(p) / 2;
211 g.BERDecode(parameters);
212 parameters.MessageEnd();
214 SetModulusAndSubgroupGenerator(p, g);
223 GetSubgroupGenerator().
DEREncode(parameters);
224 parameters.MessageEnd();
229 return GetValueHelper<DL_GroupParameters<Element> >(
this, name, valueType, pValue)
230 CRYPTOPP_GET_FUNCTION_ENTRY(Modulus);
235 AssignFromHelper(
this, source)
236 CRYPTOPP_SET_FUNCTION_ENTRY2(Modulus, SubgroupGenerator)
237 CRYPTOPP_SET_FUNCTION_ENTRY(SubgroupOrder)
241 OID DL_GroupParameters_IntegerBased::GetAlgorithmID()
const
243 return ASN1::id_dsa();
246 void DL_GroupParameters_GFP::SimultaneousExponentiate(Element *results,
const Element &base,
const Integer *exponents,
unsigned int exponentsCount)
const
249 ma.SimultaneousExponentiate(results, base, exponents, exponentsCount);
254 return a_times_b_mod_c(a, b, GetModulus());
260 return ma.CascadeExponentiate(element1, exponent1, element2, exponent2);
263 Integer DL_GroupParameters_IntegerBased::GetMaxExponent()
const
265 return STDMIN(GetSubgroupOrder()-1,
Integer::Power2(2*DiscreteLogWorkFactor(GetFieldType()*GetModulus().BitCount())));
268 unsigned int DL_GroupParameters_IntegerBased::GetDefaultSubgroupOrderSize(
unsigned int modulusSize)
const
270 return 2*DiscreteLogWorkFactor(GetFieldType()*modulusSize);