Skip to content

Commit

Permalink
Rename coeff_signed_to_unsigned -> scalar_signed_to_unsigned_q_16
Browse files Browse the repository at this point in the history
Also, uniformize file structure of proof subdirctory for
scalar_signed_to_unsigned_q_16 with those of other functions.

Signed-off-by: Hanno Becker <[email protected]>
  • Loading branch information
hanno-becker committed Jul 2, 2024
1 parent 6446621 commit bf75581
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 48 deletions.
27 changes: 0 additions & 27 deletions cbmc/proofs/coeff_signed_to_unsigned/desktop.mk

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
include ../Makefile_params.common

HARNESS_ENTRY = harness
HARNESS_FILE = coeff_signed_to_unsigned_harness
HARNESS_FILE = scalar_signed_to_unsigned_q_16_harness

# This should be a unique identifier for this proof, and will appear on the
# Litani dashboard. It can be human-readable and contain spaces if you wish.
PROOF_UID = coeff_signed_to_unsigned
PROOF_UID = scalar_signed_to_unsigned_q_16

DEFINES +=
INCLUDES +=
Expand All @@ -18,7 +18,7 @@ UNWINDSET +=
PROOF_SOURCES += $(PROOFDIR)/$(HARNESS_FILE).c
PROJECT_SOURCES += $(SRCDIR)/mlkem/poly.c

CHECK_FUNCTION_CONTRACTS=coeff_signed_to_unsigned
CHECK_FUNCTION_CONTRACTS=$(KYBER_NAMESPACE)scalar_signed_to_unsigned_q_16
USE_FUNCTION_CONTRACTS=
APPLY_LOOP_CONTRACTS=on
USE_DYNAMIC_FRAMES=1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# SPDX-License-Identifier: Apache-2.0

coeff_signed_to_unsigned proof
scalar_signed_to_unsigned_q_16 proof
==============

This directory contains a memory safety proof for coeff_signed_to_unsigned.
This directory contains a memory safety proof for scalar_signed_to_unsigned_q_16.

To run the proof.
-------------
Expand Down
7 changes: 7 additions & 0 deletions cbmc/proofs/scalar_signed_to_unsigned_q_16/cbmc-viewer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{ "expected-missing-functions":
[

],
"proof-name": "scalar_signed_to_unsigned_q_16",
"proof-root": "cbmc/proofs"
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/

/**
* @file coeff_signed_to_unsigned.c
* @brief Implements the proof harness for coeff_signed_to_unsigned function.
* @file scalar_signed_to_unsigned_q_16.c
* @brief Implements the proof harness for scalar_signed_to_unsigned_q_16 function.
*/

/*
Expand All @@ -26,5 +26,5 @@ void harness(void)
int16_t u;

/* Contracts for this function are in poly.h */
uint16_t d = coeff_signed_to_unsigned(u);
uint16_t d = scalar_signed_to_unsigned_q_16(u);
}
16 changes: 8 additions & 8 deletions mlkem/poly.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ uint32_t scalar_decompress_q_32(uint32_t u)
}

/************************************************************
* Name: coeff_signed_to_unsigned
* Name: scalar_signed_to_unsigned_q_16
*
* Description: converts signed polynomial coefficient
* from signed (-3328 .. 3328) form to
Expand All @@ -105,16 +105,16 @@ uint32_t scalar_decompress_q_32(uint32_t u)
*
* Arguments: c: signed coefficient to be converted
************************************************************/
uint16_t coeff_signed_to_unsigned (int16_t c)
uint16_t scalar_signed_to_unsigned_q_16 (int16_t c)
{
int32_t r = (int32_t) c;

// Add Q if r is negative
int32_t factor = (r < 0); // 1 if r < 0; 0 if r >= 0
r = r + (factor * KYBER_Q);

__CPROVER_assert(r >= 0, "coeff_signed_to_unsigned result lower bound");
__CPROVER_assert(r < KYBER_Q, "coeff_signed_to_unsigned result upper bound");
__CPROVER_assert(r >= 0, "scalar_signed_to_unsigned_q_16 result lower bound");
__CPROVER_assert(r < KYBER_Q, "scalar_signed_to_unsigned_q_16 result upper bound");

// and therefore cast to uint16_t is safe.
return (uint16_t) r;
Expand Down Expand Up @@ -145,7 +145,7 @@ void poly_compress(uint8_t r[KYBER_POLYCOMPRESSEDBYTES], const poly *a)
{
// map to positive standard representatives
// REF-CHANGE: Hoist signed-to-unsigned conversion into separate function
int32_t u = (int32_t) coeff_signed_to_unsigned(a->coeffs[8 * i + j]);
int32_t u = (int32_t) scalar_signed_to_unsigned_q_16(a->coeffs[8 * i + j]);
// REF-CHANGE: Hoist scalar compression into separate function
t[j] = scalar_compress_q_16(u);
}
Expand All @@ -163,7 +163,7 @@ void poly_compress(uint8_t r[KYBER_POLYCOMPRESSEDBYTES], const poly *a)
{
// map to positive standard representatives
// REF-CHANGE: Hoist signed-to-unsigned conversion into separate function
int32_t u = (int32_t) coeff_signed_to_unsigned(a->coeffs[8 * i + j]);
int32_t u = (int32_t) scalar_signed_to_unsigned_q_16(a->coeffs[8 * i + j]);
// REF-CHANGE: Hoist scalar compression into separate function
t[j] = scalar_compress_q_32(u);
}
Expand Down Expand Up @@ -266,8 +266,8 @@ void poly_tobytes(uint8_t r[KYBER_POLYBYTES], const poly *a)
{
// map to positive standard representatives
// REF-CHANGE: Hoist signed-to-unsigned conversion into separate function
t0 = coeff_signed_to_unsigned(a->coeffs[2 * i]);
t1 = coeff_signed_to_unsigned(a->coeffs[2 * i + 1]);
t0 = scalar_signed_to_unsigned_q_16(a->coeffs[2 * i]);
t1 = scalar_signed_to_unsigned_q_16(a->coeffs[2 * i + 1]);
r[3 * i + 0] = (t0 >> 0);
r[3 * i + 1] = (t0 >> 8) | (t1 << 4);
r[3 * i + 2] = (t1 >> 4);
Expand Down
11 changes: 6 additions & 5 deletions mlkem/poly.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ typedef struct
int16_t coeffs[KYBER_N];
} poly;

#define scalar_compress_q_16 KYBER_NAMESPACE(scalar_compress_q_16)
#define scalar_decompress_q_16 KYBER_NAMESPACE(scalar_decompress_q_16)
#define scalar_compress_q_32 KYBER_NAMESPACE(scalar_compress_q_32)
#define scalar_decompress_q_32 KYBER_NAMESPACE(scalar_decompress_q_32)
#define scalar_compress_q_16 KYBER_NAMESPACE(scalar_compress_q_16)
#define scalar_decompress_q_16 KYBER_NAMESPACE(scalar_decompress_q_16)
#define scalar_compress_q_32 KYBER_NAMESPACE(scalar_compress_q_32)
#define scalar_decompress_q_32 KYBER_NAMESPACE(scalar_decompress_q_32)
#define scalar_signed_to_unsigned_q_16 KYBER_NAMESPACE(scalar_signed_to_unsigned_q_16)

uint32_t scalar_compress_q_16 (int32_t u)
/* INDENT-OFF */
Expand Down Expand Up @@ -46,7 +47,7 @@ __CPROVER_requires(0 <= u && u < 32)
__CPROVER_ensures(__CPROVER_return_value < KYBER_Q);
/* INDENT-ON */

uint16_t coeff_signed_to_unsigned (int16_t c)
uint16_t scalar_signed_to_unsigned_q_16 (int16_t c)
/* *INDENT-OFF* */
__CPROVER_requires(c > -KYBER_Q) // c >= -3328
__CPROVER_requires(c < KYBER_Q) // c <= 3328
Expand Down

0 comments on commit bf75581

Please sign in to comment.