-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
46115eb
commit db2389a
Showing
17 changed files
with
301 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include <tfhe.h> | ||
|
||
#include <assert.h> | ||
#include <inttypes.h> | ||
#include <stdio.h> | ||
|
||
int uint2048_client_key(const ClientKey *client_key) { | ||
int ok; | ||
FheUint2048 *lhs = NULL; | ||
FheUint2048 *rhs = NULL; | ||
FheBool *result = NULL; | ||
FheUint64 *cast_result = NULL; | ||
U2048 lhs_clear = {.words = {0}}; | ||
U2048 rhs_clear = {.words = {0}}; | ||
bool result_clear = true; | ||
|
||
for (size_t i = 0; i < 32; ++i) { | ||
lhs_clear.words[i] = i; | ||
rhs_clear.words[i] = UINT64_MAX - i; | ||
} | ||
|
||
ok = fhe_uint2048_try_encrypt_with_client_key_u2048(lhs_clear, client_key, &lhs); | ||
assert(ok == 0); | ||
|
||
ok = fhe_uint2048_try_encrypt_with_client_key_u2048(rhs_clear, client_key, &rhs); | ||
assert(ok == 0); | ||
|
||
ok = fhe_uint2048_eq(lhs, rhs, &result); | ||
assert(ok == 0); | ||
|
||
ok = fhe_bool_decrypt(result, client_key, &result_clear); | ||
assert(ok == 0); | ||
|
||
assert(result_clear == false); | ||
|
||
fhe_uint2048_destroy(lhs); | ||
fhe_uint2048_destroy(rhs); | ||
fhe_bool_destroy(result); | ||
return ok; | ||
} | ||
|
||
int main(void) { | ||
int ok = 0; | ||
ConfigBuilder *builder; | ||
Config *config; | ||
|
||
config_builder_default(&builder); | ||
config_builder_build(builder, &config); | ||
|
||
ClientKey *client_key = NULL; | ||
ServerKey *server_key = NULL; | ||
PublicKey *public_key = NULL; | ||
|
||
generate_keys(config, &client_key, &server_key); | ||
public_key_new(client_key, &public_key); | ||
|
||
set_server_key(server_key); | ||
|
||
uint2048_client_key(client_key); | ||
|
||
client_key_destroy(client_key); | ||
public_key_destroy(public_key); | ||
server_key_destroy(server_key); | ||
return ok; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use crate::c_api::utils::*; | ||
use std::os::raw::c_int; | ||
|
||
#[derive(Copy, Clone)] | ||
#[repr(C)] | ||
pub struct U2048 { | ||
words: [u64; 32], | ||
} | ||
|
||
impl From<crate::integer::bigint::U2048> for U2048 { | ||
fn from(value: crate::integer::bigint::U2048) -> Self { | ||
Self { words: value.0 } | ||
} | ||
} | ||
|
||
impl From<U2048> for crate::integer::bigint::U2048 { | ||
fn from(value: U2048) -> Self { | ||
Self(value.words) | ||
} | ||
} | ||
|
||
/// Creates a U2048 from little endian bytes | ||
/// | ||
/// len must be 256 | ||
#[no_mangle] | ||
pub unsafe extern "C" fn U2048_from_little_endian_bytes( | ||
input: *const u8, | ||
len: usize, | ||
result: *mut U2048, | ||
) -> c_int { | ||
catch_panic(|| { | ||
let mut inner = crate::integer::bigint::U2048::default(); | ||
|
||
let input = std::slice::from_raw_parts(input, len); | ||
inner.copy_from_le_byte_slice(input); | ||
|
||
*result = U2048::from(inner); | ||
}) | ||
} | ||
|
||
/// Creates a U2048 from big endian bytes | ||
/// | ||
/// len must be 256 | ||
#[no_mangle] | ||
pub unsafe extern "C" fn U2048_from_big_endian_bytes( | ||
input: *const u8, | ||
len: usize, | ||
result: *mut U2048, | ||
) -> c_int { | ||
catch_panic(|| { | ||
let mut inner = crate::integer::bigint::U2048::default(); | ||
|
||
let input = std::slice::from_raw_parts(input, len); | ||
inner.copy_from_be_byte_slice(input); | ||
|
||
*result = U2048::from(inner); | ||
}) | ||
} | ||
|
||
/// len must be 256 | ||
#[no_mangle] | ||
pub unsafe extern "C" fn U2048_little_endian_bytes( | ||
input: U2048, | ||
result: *mut u8, | ||
len: usize, | ||
) -> c_int { | ||
catch_panic(|| { | ||
check_ptr_is_non_null_and_aligned(result).unwrap(); | ||
|
||
let bytes = std::slice::from_raw_parts_mut(result, len); | ||
crate::integer::bigint::U2048::from(input).copy_to_le_byte_slice(bytes); | ||
}) | ||
} | ||
|
||
/// len must be 256 | ||
#[no_mangle] | ||
pub unsafe extern "C" fn U2048_big_endian_bytes( | ||
input: U2048, | ||
result: *mut u8, | ||
len: usize, | ||
) -> c_int { | ||
catch_panic(|| { | ||
check_ptr_is_non_null_and_aligned(result).unwrap(); | ||
|
||
let bytes = std::slice::from_raw_parts_mut(result, len); | ||
crate::integer::bigint::U2048::from(input).copy_to_be_byte_slice(bytes); | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.