/******************************************************************************

  • BIKE -- Bit Flipping Key Encapsulation
  • Copyright (c) 2017 Nir Drucker, Shay Gueron, Rafael Misoczki
  • (drucker.nir@gmail.com, shay.gueron@gmail.com, rafaelmisoczki@google.com)
  • Permission to use this code for BIKE is granted.
  • Redistribution and use in source and binary forms, with or without
  • modification, are permitted provided that the following conditions are met:
    • Redistributions of source code must retain the above copyright notice,
  • this list of conditions and the following disclaimer.
    • 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.
    • The names of the contributors may not be used to endorse or promote
  • products derived from this software without specific prior written
  • permission.
  • THIS SOFTWARE IS PROVIDED BY THE AUTHORS ""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 AUTHORS CORPORATION OR
  • CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  • EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  • PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  • PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  • LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  • NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  • SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    ******************************************************************************/

include "stdio.h"

include "kem.h"

include "utilities.h"

include "measurements.h"

include <time.h>

////////////////////////////////////////////////////////////////
// Main function for testing
////////////////////////////////////////////////////////////////
int main(void)
{
sk_t sk = {0}; // private-key: (h0, h1)
pk_t pk = {0}; // public-key: (g0, g1)
ct_t ct = {0}; // ciphertext: (c0, c1)
ss_t k_enc = {0}; // shared secret after encapsulate
ss_t k_dec = {0}; // shared secret after decapsulate

clock_t start,end,start_2,start_3,end_2,end_3;
double duration,duration_2,duration_3;
uint32_t mismatch_count = 0; // 初始化密钥不匹配计数为0
uint32_t total_operations = NUM_OF_ENCRYPTION_TESTS * NUM_OF_CODE_TESTS; // 总封装解封装次数

MSG("BIKE Demo Test:\n");

for (uint32_t i=1; i <= NUM_OF_CODE_TESTS; ++i)
{
status_t res = SUCCESS;

MSG("r: %d Code test: %d \n", (int) R_BITS, i);

start=clock();

//Key generation.
MEASURE(" keygen", res = static_cast<status_t>(crypto_kem_keypair(pk.raw, sk.raw))😉;

end = clock();
duration = (double)(end-start) / CLOCKS_PER_SEC;
MSG("%f seconds\n" , duration);
if(res != SUCCESS)
{
MSG("Keypair failed with error: %d\n", res);
continue;
}

for (uint32_t j=1; j <= NUM_OF_ENCRYPTION_TESTS; ++j)
{
// MSG("Enc/Dec test: %d\n", j);
uint32_t dec_rc = 0;

start_2 = clock();
//Encapsulate
MEASURE(" encaps", res = static_cast<status_t>(crypto_kem_enc(ct.raw, k_enc.raw, pk.raw))😉;

end_2 = clock();
duration_2 = (double)(end_2-start_2) / CLOCKS_PER_SEC;
MSG("%f seconds\n" , duration_2);

if(res != SUCCESS)
{
MSG("encapsulate failed with error: %d\n", res);
continue;
}
start_3 = clock();
//Decapsulate
MEASURE(" decaps", dec_rc = crypto_kem_dec(k_dec.raw, ct.raw, sk.raw)😉;

end_3 = clock();
duration_3 = (double)(end_3-start_3) / CLOCKS_PER_SEC;
MSG("%f seconds\n" , duration_3);

if (dec_rc != 0)
{
MSG("Decoding failed after %d code tests and %d enc/dec tests!\n", i, j);
}
else
{
if (safe_cmp(k_enc.raw, k_dec.raw, sizeof(k_dec)/sizeof(uint64_t)))
{
MSG("Success! decapsulated key is the same as encapsulated key!\n");
} else {
MSG("Failure! decapsulated key is NOT the same as encapsulated key!\n");
mismatch_count++; // 密钥不匹配,计数加1
}
}

DMSG("Initiator's generated key (K) of 256 bits = ");
print((uint64_t)k_enc.raw, ELL_SIZE8);
DMSG("Responder's computed key (K) of 256 bits = ");
print((uint64_t)k_dec.raw, ELL_SIZE8);
}
}
// 输出密钥不匹配的次数和总封装解封装次数
MSG("Total key mismatches: %d\n", mismatch_count);
MSG("Total encapsulate/decapsulate operations: %d\n", total_operations);

// 输出密钥不匹配的百分比
double mismatch_percentage = ((double)mismatch_count / total_operations) * 100;
MSG("Mismatch percentage: %.2f%%\n", mismatch_percentage);

return 0;
}

posted on 2024-05-10 15:47  朴伤色  阅读(36)  评论(0)    收藏  举报