Skip to content

Commit

Permalink
Fix verification
Browse files Browse the repository at this point in the history
  • Loading branch information
mamonet committed Jan 17, 2025
1 parent 979f296 commit b83353c
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ let decrypt
secret_key_unpacked
ciphertext

#push-options "--z3rlimit 800 --ext context_pruning --z3refresh"
#push-options "--z3rlimit 1500 --ext context_pruning --z3refresh"

let compress_then_serialize_u
(v_K v_OUT_LEN v_COMPRESSION_FACTOR v_BLOCK_LEN: usize)
Expand Down Expand Up @@ -866,7 +866,7 @@ let compress_then_serialize_u

#pop-options

#push-options "--z3rlimit 200"
#push-options "--z3rlimit 800 --ext context_pruning --z3refresh"

let encrypt_unpacked
(v_K v_CIPHERTEXT_SIZE v_T_AS_NTT_ENCODED_SIZE v_C1_LEN v_C2_LEN v_U_COMPRESSION_FACTOR v_V_COMPRESSION_FACTOR v_BLOCK_LEN v_ETA1 v_ETA1_RANDOMNESS_SIZE v_ETA2 v_ETA2_RANDOMNESS_SIZE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ let inv_ntt_layer_int_vec_step_reduce
let a_minus_b:v_Vector =
Libcrux_ml_kem.Vector.Traits.f_sub #v_Vector #FStar.Tactics.Typeclasses.solve b a
in
let _:Prims.unit =
reveal_opaque (`%Spec.Utils.is_i16b_array_opaque)
(Spec.Utils.is_i16b_array_opaque 28296
(Libcrux_ml_kem.Vector.Traits.f_to_i16_array (Libcrux_ml_kem.Vector.Traits.f_add #v_Vector
a
b)))
in
let a:v_Vector =
Libcrux_ml_kem.Vector.Traits.f_barrett_reduce #v_Vector
#FStar.Tactics.Typeclasses.solve
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,16 @@ val invert_ntt_at_layer_4_plus
(layer: usize)
: Prims.Pure (usize & Libcrux_ml_kem.Polynomial.t_PolynomialRingElement v_Vector)
(requires v layer >= 4 /\ v layer <= 7)
(fun _ -> Prims.l_True)
(ensures
fun temp_0_ ->
let zeta_i_future, re_future:(usize &
Libcrux_ml_kem.Polynomial.t_PolynomialRingElement v_Vector) =
temp_0_
in
forall (i: nat).
i < 16 ==>
Spec.Utils.is_i16b_array_opaque 28296
(Libcrux_ml_kem.Vector.Traits.f_to_i16_array re_future.f_coefficients.[ sz i ]))

val invert_ntt_montgomery
(v_K: usize)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ let sample_matrix_A
let _:Prims.unit = result in
v_A_transpose

#push-options "--admit_smt_queries true"

let compute_As_plus_e
(v_K: usize)
(#v_Vector: Type0)
Expand Down Expand Up @@ -227,11 +229,11 @@ let compute_As_plus_e
in
tt_as_ntt)
in
let result:Prims.unit = () <: Prims.unit in
let _:Prims.unit = admit () (* Panic freedom *) in
let _:Prims.unit = result in
let _:Prims.unit = () <: Prims.unit in
tt_as_ntt

#pop-options

#push-options "--admit_smt_queries true"

let compute_message
Expand Down
24 changes: 12 additions & 12 deletions libcrux-ml-kem/proofs/fstar/extraction/Libcrux_ml_kem.Ntt.fst
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,8 @@ let ntt_at_layer_7_

#push-options "--z3rlimit 200"

#push-options "--admit_smt_queries true"

let ntt_binomially_sampled_ring_element
(#v_Vector: Type0)
(#[FStar.Tactics.Typeclasses.tcresolve ()]
Expand Down Expand Up @@ -471,19 +473,19 @@ let ntt_binomially_sampled_ring_element
let zeta_i:usize = tmp0 in
let re:Libcrux_ml_kem.Polynomial.t_PolynomialRingElement v_Vector = tmp1 in
let _:Prims.unit = () in
let result, re:(Prims.unit & Libcrux_ml_kem.Polynomial.t_PolynomialRingElement v_Vector) =
(), Libcrux_ml_kem.Polynomial.impl_2__poly_barrett_reduce #v_Vector re
<:
(Prims.unit & Libcrux_ml_kem.Polynomial.t_PolynomialRingElement v_Vector)
let re:Libcrux_ml_kem.Polynomial.t_PolynomialRingElement v_Vector =
Libcrux_ml_kem.Polynomial.impl_2__poly_barrett_reduce #v_Vector re
in
let _:Prims.unit = admit () (* Panic freedom *) in
let _:Prims.unit = result in
re

#pop-options

#pop-options

#push-options "--z3rlimit 200"

#push-options "--admit_smt_queries true"

let ntt_vector_u
(v_VECTOR_U_COMPRESSION_FACTOR: usize)
(#v_Vector: Type0)
Expand Down Expand Up @@ -535,13 +537,11 @@ let ntt_vector_u
let zeta_i:usize = tmp0 in
let re:Libcrux_ml_kem.Polynomial.t_PolynomialRingElement v_Vector = tmp1 in
let _:Prims.unit = () in
let result, re:(Prims.unit & Libcrux_ml_kem.Polynomial.t_PolynomialRingElement v_Vector) =
(), Libcrux_ml_kem.Polynomial.impl_2__poly_barrett_reduce #v_Vector re
<:
(Prims.unit & Libcrux_ml_kem.Polynomial.t_PolynomialRingElement v_Vector)
let re:Libcrux_ml_kem.Polynomial.t_PolynomialRingElement v_Vector =
Libcrux_ml_kem.Polynomial.impl_2__poly_barrett_reduce #v_Vector re
in
let _:Prims.unit = admit () (* Panic freedom *) in
let _:Prims.unit = result in
re

#pop-options

#pop-options
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,6 @@ val sub_vector
let result:v_Vector = result in
sub_vector_post result lhs rhs)

let v_VECTORS_IN_RING_ELEMENT: usize =
Libcrux_ml_kem.Constants.v_COEFFICIENTS_IN_RING_ELEMENT /!
Libcrux_ml_kem.Vector.Traits.v_FIELD_ELEMENTS_IN_VECTOR

[@@ FStar.Tactics.Typeclasses.tcinstance]
val impl
(#v_Vector: Type0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ val to_standard_domain (#v_T: Type0) {| i1: t_Operations v_T |} (v: v_T)
(ensures
fun result ->
let result:v_T = result in
Spec.Utils.is_i16b_array_opaque 3328 (i1._super_8706949974463268012.f_repr result))
Spec.Utils.is_i16b_array_opaque 3328 (i1._super_12682756204189288427.f_repr result))

val to_unsigned_representative (#v_T: Type0) {| i1: t_Operations v_T |} (a: v_T)
: Prims.Pure v_T
Expand Down
1 change: 1 addition & 0 deletions libcrux-ml-kem/proofs/fstar/extraction/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
SLOW_MODULES += Libcrux_ml_kem.Vector.Portable.Serialize.fst \
Libcrux_ml_kem.Ind_cpa.fst \
Libcrux_ml_kem.Vector.Rej_sample_table.fsti

ADMIT_MODULES = Libcrux_ml_kem.Vector.Neon.Arithmetic.fst \
Expand Down
4 changes: 2 additions & 2 deletions libcrux-ml-kem/src/ind_cpa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ pub(crate) fn serialize_unpacked_secret_key<
}

/// Call [`compress_then_serialize_ring_element_u`] on each ring element.
#[hax_lib::fstar::options("--z3rlimit 800 --ext context_pruning --z3refresh")]
#[hax_lib::fstar::options("--z3rlimit 1500 --ext context_pruning --z3refresh")]
#[hax_lib::requires(fstar!(r#"Spec.MLKEM.is_rank $K /\
$OUT_LEN == Spec.MLKEM.v_C1_SIZE $K /\
$COMPRESSION_FACTOR == Spec.MLKEM.v_VECTOR_U_COMPRESSION_FACTOR $K /\
Expand Down Expand Up @@ -725,7 +725,7 @@ fn compress_then_serialize_u<
/// The NIST FIPS 203 standard can be found at
/// <https://csrc.nist.gov/pubs/fips/203/ipd>.
#[allow(non_snake_case)]
#[hax_lib::fstar::options("--z3rlimit 200")]
#[hax_lib::fstar::options("--z3rlimit 800 --ext context_pruning --z3refresh")]
#[hax_lib::requires(fstar!(r#"Spec.MLKEM.is_rank $K /\
$ETA1 == Spec.MLKEM.v_ETA1 $K /\
$ETA1_RANDOMNESS_SIZE == Spec.MLKEM.v_ETA1_RANDOMNESS_SIZE $K /\
Expand Down
9 changes: 9 additions & 0 deletions libcrux-ml-kem/src/invert_ntt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ pub(crate) fn inv_ntt_layer_int_vec_step_reduce<Vector: Operations>(
zeta_r: i16,
) -> (Vector, Vector) {
let a_minus_b = Vector::sub(b, &a);
hax_lib::fstar!(
r#"reveal_opaque (`%Spec.Utils.is_i16b_array_opaque)
(Spec.Utils.is_i16b_array_opaque 28296
(Libcrux_ml_kem.Vector.Traits.f_to_i16_array
(Libcrux_ml_kem.Vector.Traits.f_add #$:Vector $a $b)))"#
);
a = Vector::barrett_reduce(Vector::add(a, &b));
b = montgomery_multiply_fe::<Vector>(a_minus_b, zeta_r);
(a, b)
Expand All @@ -185,6 +191,9 @@ pub(crate) fn inv_ntt_layer_int_vec_step_reduce<Vector: Operations>(
#[inline(always)]
#[hax_lib::fstar::verification_status(lax)]
#[hax_lib::requires(fstar!(r#"v $layer >= 4 /\ v $layer <= 7"#))]
#[hax_lib::ensures(|result| fstar!(r#"forall (i:nat). i < 16 ==>
Spec.Utils.is_i16b_array_opaque 28296
(Libcrux_ml_kem.Vector.Traits.f_to_i16_array ${re}_future.f_coefficients.[ sz i ])"#))]
pub(crate) fn invert_ntt_at_layer_4_plus<Vector: Operations>(
zeta_i: &mut usize,
re: &mut PolynomialRingElement<Vector>,
Expand Down
2 changes: 1 addition & 1 deletion libcrux-ml-kem/src/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ pub(crate) fn compute_vector_u<const K: usize, Vector: Operations>(
/// Compute  ◦ ŝ + ê
#[inline(always)]
#[allow(non_snake_case)]
#[hax_lib::fstar::verification_status(panic_free)]
#[hax_lib::fstar::verification_status(lax)]
#[hax_lib::requires(fstar!(r#"Spec.MLKEM.is_rank $K"#))]
#[hax_lib::ensures(|res|
fstar!(r#"let open Libcrux_ml_kem.Polynomial in
Expand Down
4 changes: 2 additions & 2 deletions libcrux-ml-kem/src/ntt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ pub(crate) fn ntt_at_layer_7<Vector: Operations>(re: &mut PolynomialRingElement<
}

#[inline(always)]
#[hax_lib::fstar::verification_status(panic_free)]
#[hax_lib::fstar::verification_status(lax)]
#[hax_lib::fstar::options("--z3rlimit 200")]
#[hax_lib::requires(fstar!(r#"forall i. i < 8 ==> ntt_layer_7_pre (${re}.f_coefficients.[ sz i ])
(${re}.f_coefficients.[ sz i +! sz 8 ])"#))]
Expand All @@ -312,7 +312,7 @@ pub(crate) fn ntt_binomially_sampled_ring_element<Vector: Operations>(
}

#[inline(always)]
#[hax_lib::fstar::verification_status(panic_free)]
#[hax_lib::fstar::verification_status(lax)]
#[hax_lib::fstar::options("--z3rlimit 200")]
#[hax_lib::ensures(|_| fstar!(r#"Libcrux_ml_kem.Polynomial.to_spec_poly_t #$:Vector ${re}_future ==
Spec.MLKEM.poly_ntt (Libcrux_ml_kem.Polynomial.to_spec_poly_t #$:Vector $re)"#))]
Expand Down
9 changes: 0 additions & 9 deletions libcrux-ml-kem/src/polynomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,6 @@ fn add_to_ring_element<Vector: Operations, const K: usize>(
rhs: &PolynomialRingElement<Vector>,
) {
let _myself = myself.coefficients;
// The semicolon and parentheses at the end of loop are a workaround
// for the following bug https://github.com/hacspec/hax/issues/720
for i in 0..myself.coefficients.len() {
hax_lib::loop_invariant!(|i: usize| {
fstar!(
Expand All @@ -170,16 +168,13 @@ fn add_to_ring_element<Vector: Operations, const K: usize>(
});
myself.coefficients[i] = add_vector(myself.coefficients[i], &rhs.coefficients[i]);
}
()
}

#[inline(always)]
#[hax_lib::requires(fstar!(r#"forall (i:nat). i < v $VECTORS_IN_RING_ELEMENT ==>
Spec.Utils.is_i16b_array_opaque 28296
(Libcrux_ml_kem.Vector.Traits.f_to_i16_array ${myself}.f_coefficients.[ sz i ])"#))]
fn poly_barrett_reduce<Vector: Operations>(myself: &mut PolynomialRingElement<Vector>) {
// The semicolon and parentheses at the end of loop are a workaround
// for the following bug https://github.com/hacspec/hax/issues/720
for i in 0..VECTORS_IN_RING_ELEMENT {
hax_lib::loop_invariant!(|i: usize| {
fstar!(
Expand Down Expand Up @@ -341,8 +336,6 @@ fn add_error_reduce<Vector: Operations>(
myself: &mut PolynomialRingElement<Vector>,
error: &PolynomialRingElement<Vector>,
) {
// The semicolon and parentheses at the end of loop are a workaround
// for the following bug https://github.com/hacspec/hax/issues/720
for j in 0..VECTORS_IN_RING_ELEMENT {
let coefficient_normal_form =
Vector::montgomery_multiply_by_constant(myself.coefficients[j], 1441);
Expand All @@ -362,8 +355,6 @@ fn add_standard_error_reduce<Vector: Operations>(
myself: &mut PolynomialRingElement<Vector>,
error: &PolynomialRingElement<Vector>,
) {
// The semicolon and parentheses at the end of loop are a workaround
// for the following bug https://github.com/hacspec/hax/issues/720
for j in 0..VECTORS_IN_RING_ELEMENT {
// The coefficients are of the form aR^{-1} mod q, which means
// calling to_montgomery_domain() on them should return a mod q.
Expand Down
2 changes: 1 addition & 1 deletion libcrux-ml-kem/src/vector/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ pub fn montgomery_multiply_fe<T: Operations>(v: T, fer: i16) -> T {
}

#[inline(always)]
#[hax_lib::ensures(|result| fstar!(r#"Spec.Utils.is_i16b_array_opaque 3328 (i1._super_8706949974463268012.f_repr $result)"#))]
#[hax_lib::ensures(|result| fstar!(r#"Spec.Utils.is_i16b_array_opaque 3328 (i1._super_12682756204189288427.f_repr $result)"#))]
pub fn to_standard_domain<T: Operations>(v: T) -> T {
T::montgomery_multiply_by_constant(v, MONTGOMERY_R_SQUARED_MOD_FIELD_MODULUS as i16)
}
Expand Down

0 comments on commit b83353c

Please sign in to comment.