Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More tests to be fixed #70

Merged
merged 2 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ KRML_HOME ?= $(dir $(abspath $(lastword $(MAKEFILE_LIST))))/../karamel
EURYDICE ?= ./eurydice $(EURYDICE_FLAGS)
CHARON ?= $(CHARON_HOME)/bin/charon

BROKEN_TESTS = step_by where_clauses chunks mutable_slice_range closure
BROKEN_TESTS = step_by where_clauses chunks mutable_slice_range closure issue_49 symcrust
TEST_DIRS = $(filter-out $(BROKEN_TESTS),$(subst test/,,$(shell find test -maxdepth 1 -mindepth 1 -type d)))

.PHONY: all
Expand Down
7 changes: 7 additions & 0 deletions test/issue_49/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions test/issue_49/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "issue_49"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
9 changes: 9 additions & 0 deletions test/issue_49/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pub fn f(a: usize, b: usize) -> usize {
a.min(b)
}

fn main() {
let expected = 0;
let actual = f(0, 0);
assert_eq!(expected, actual);
}
7 changes: 7 additions & 0 deletions test/symcrust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions test/symcrust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "symcrust"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
6 changes: 6 additions & 0 deletions test/symcrust/c.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
files:
- name: symcrust
api:
- [ symcrust, "*" ]
private:
- [ "*" ]
71 changes: 71 additions & 0 deletions test/symcrust/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#[allow(non_snake_case)]
#[no_mangle]
pub fn SymCrustMlKemPolyElementCompressAndEncode(
coeffs: &[u16; 256],
nBitsPerCoefficient: u32,
dst: &mut [u8] )
{
let SYMCRYPT_MLKEM_COMPRESS_MULCONSTANT : u64 = 0x9d7dbb;
let SYMCRYPT_MLKEM_COMPRESS_SHIFTCONSTANT : u32 = 35;
let mut multiplication: u64;
let mut coefficient: u32;
let mut nBitsInCoefficient: u32;
let mut bitsToEncode: u32;
let mut nBitsToEncode: u32;
let mut cbDstWritten: usize = 0;
let mut accumulator: u32 = 0;
let mut nBitsInAccumulator: u32 = 0;

assert!(nBitsPerCoefficient > 0);
assert!(nBitsPerCoefficient <= 12);
assert!( dst.len() as u64 == (256*u64::from(nBitsPerCoefficient) / 8) );

for i in 0..256
{
nBitsInCoefficient = nBitsPerCoefficient;
coefficient = u32::from(coeffs[i]); // in range [0, Q-1]

// first compress the coefficient
// when nBitsPerCoefficient < 12 we compress per Compress_d in draft FIPS 203;
if nBitsPerCoefficient < 12
{
// Multiply by 2^(nBitsPerCoefficient+1) / Q by multiplying by constant and shifting right
multiplication = u64::from(coefficient) * SYMCRYPT_MLKEM_COMPRESS_MULCONSTANT;
coefficient = (multiplication >> (SYMCRYPT_MLKEM_COMPRESS_SHIFTCONSTANT-(nBitsPerCoefficient+1))) as u32;

// add "half" to round to nearest integer
coefficient += 1;

// final divide by two to get multiplication by 2^nBitsPerCoefficient / Q
coefficient >>= 1; // in range [0, 2^nBitsPerCoefficient]

// modular reduction by masking
coefficient &= (1u32<<nBitsPerCoefficient)-1; // in range [0, 2^nBitsPerCoefficient - 1]
}

// encode the coefficient
// simple loop to add bits to accumulator and write accumulator to output
while nBitsInCoefficient > 0
{
nBitsToEncode = nBitsInCoefficient.min(32-nBitsInAccumulator);

bitsToEncode = coefficient & ((1u32<<nBitsToEncode)-1);
coefficient >>= nBitsToEncode;
nBitsInCoefficient -= nBitsToEncode;

accumulator |= bitsToEncode << nBitsInAccumulator;
nBitsInAccumulator += nBitsToEncode;
if nBitsInAccumulator == 32
{
dst[cbDstWritten..cbDstWritten+4].copy_from_slice( &accumulator.to_le_bytes() );
cbDstWritten += 4;
accumulator = 0;
nBitsInAccumulator = 0;
}
}
}
}

fn main() {
println!("Hello, world!");
}
Loading