mirror of
https://github.com/arnaucube/miden-crypto.git
synced 2026-01-10 16:11:30 +01:00
refactor: clean up features
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
|
||||
#include <string.h>
|
||||
#include "randombytes.h"
|
||||
#include "api_rpo.h"
|
||||
#include "falcon.h"
|
||||
#include "inner.h"
|
||||
#include "rpo.h"
|
||||
|
||||
@@ -37,10 +37,12 @@
|
||||
* (signature length is 1+len(value), not counting the nonce)
|
||||
*/
|
||||
|
||||
/* see api_rpo.h */
|
||||
/* see falcon.h */
|
||||
int PQCLEAN_FALCON512_CLEAN_crypto_sign_keypair_from_seed_rpo(
|
||||
uint8_t *pk, uint8_t *sk, unsigned char *seed)
|
||||
{
|
||||
uint8_t *pk,
|
||||
uint8_t *sk,
|
||||
unsigned char *seed
|
||||
) {
|
||||
union
|
||||
{
|
||||
uint8_t b[FALCON_KEYGEN_TEMP_9];
|
||||
@@ -111,8 +113,9 @@ int PQCLEAN_FALCON512_CLEAN_crypto_sign_keypair_from_seed_rpo(
|
||||
}
|
||||
|
||||
int PQCLEAN_FALCON512_CLEAN_crypto_sign_keypair_rpo(
|
||||
uint8_t *pk, uint8_t *sk)
|
||||
{
|
||||
uint8_t *pk,
|
||||
uint8_t *sk
|
||||
) {
|
||||
unsigned char seed[48];
|
||||
|
||||
/*
|
||||
@@ -137,10 +140,14 @@ int PQCLEAN_FALCON512_CLEAN_crypto_sign_keypair_rpo(
|
||||
*
|
||||
* Return value: 0 on success, -1 on error.
|
||||
*/
|
||||
static int
|
||||
do_sign(uint8_t *nonce, uint8_t *sigbuf, size_t *sigbuflen,
|
||||
const uint8_t *m, size_t mlen, const uint8_t *sk)
|
||||
{
|
||||
static int do_sign(
|
||||
uint8_t *nonce,
|
||||
uint8_t *sigbuf,
|
||||
size_t *sigbuflen,
|
||||
const uint8_t *m,
|
||||
size_t mlen,
|
||||
const uint8_t *sk
|
||||
) {
|
||||
union
|
||||
{
|
||||
uint8_t b[72 * 512];
|
||||
@@ -261,11 +268,14 @@ do_sign(uint8_t *nonce, uint8_t *sigbuf, size_t *sigbuflen,
|
||||
* (of size sigbuflen) contains the signature value, not including the
|
||||
* header byte or nonce. Return value is 0 on success, -1 on error.
|
||||
*/
|
||||
static int
|
||||
do_verify(
|
||||
const uint8_t *nonce, const uint8_t *sigbuf, size_t sigbuflen,
|
||||
const uint8_t *m, size_t mlen, const uint8_t *pk)
|
||||
{
|
||||
static int do_verify(
|
||||
const uint8_t *nonce,
|
||||
const uint8_t *sigbuf,
|
||||
size_t sigbuflen,
|
||||
const uint8_t *m,
|
||||
size_t mlen,
|
||||
const uint8_t *pk
|
||||
) {
|
||||
union
|
||||
{
|
||||
uint8_t b[2 * 512];
|
||||
@@ -341,11 +351,14 @@ do_verify(
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* see api_rpo.h */
|
||||
/* see falcon.h */
|
||||
int PQCLEAN_FALCON512_CLEAN_crypto_sign_signature_rpo(
|
||||
uint8_t *sig, size_t *siglen,
|
||||
const uint8_t *m, size_t mlen, const uint8_t *sk)
|
||||
{
|
||||
uint8_t *sig,
|
||||
size_t *siglen,
|
||||
const uint8_t *m,
|
||||
size_t mlen,
|
||||
const uint8_t *sk
|
||||
) {
|
||||
/*
|
||||
* The PQCLEAN_FALCON512_CLEAN_CRYPTO_BYTES constant is used for
|
||||
* the signed message object (as produced by crypto_sign())
|
||||
@@ -369,11 +382,14 @@ int PQCLEAN_FALCON512_CLEAN_crypto_sign_signature_rpo(
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* see api_rpo.h */
|
||||
/* see falcon.h */
|
||||
int PQCLEAN_FALCON512_CLEAN_crypto_sign_verify_rpo(
|
||||
const uint8_t *sig, size_t siglen,
|
||||
const uint8_t *m, size_t mlen, const uint8_t *pk)
|
||||
{
|
||||
const uint8_t *sig,
|
||||
size_t siglen,
|
||||
const uint8_t *m,
|
||||
size_t mlen,
|
||||
const uint8_t *pk
|
||||
) {
|
||||
if (siglen < 1 + NONCELEN)
|
||||
{
|
||||
return -1;
|
||||
@@ -382,6 +398,5 @@ int PQCLEAN_FALCON512_CLEAN_crypto_sign_verify_rpo(
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return do_verify(sig + 1,
|
||||
sig + 1 + NONCELEN, siglen - 1 - NONCELEN, m, mlen, pk);
|
||||
return do_verify(sig + 1, sig + 1 + NONCELEN, siglen - 1 - NONCELEN, m, mlen, pk);
|
||||
}
|
||||
@@ -14,7 +14,7 @@
|
||||
#define M 12289
|
||||
|
||||
// From https://github.com/ncw/iprime/blob/master/mod_math_noasm.go
|
||||
uint64_t add_mod_p(uint64_t a, uint64_t b)
|
||||
static uint64_t add_mod_p(uint64_t a, uint64_t b)
|
||||
{
|
||||
a = P - a;
|
||||
uint64_t res = b - a;
|
||||
@@ -23,7 +23,7 @@ uint64_t add_mod_p(uint64_t a, uint64_t b)
|
||||
return res;
|
||||
}
|
||||
|
||||
uint64_t sub_mod_p(uint64_t a, uint64_t b)
|
||||
static uint64_t sub_mod_p(uint64_t a, uint64_t b)
|
||||
{
|
||||
uint64_t r = a - b;
|
||||
if (a < b)
|
||||
@@ -31,7 +31,7 @@ uint64_t sub_mod_p(uint64_t a, uint64_t b)
|
||||
return r;
|
||||
}
|
||||
|
||||
uint64_t reduce_mod_p(uint64_t b, uint64_t a)
|
||||
static uint64_t reduce_mod_p(uint64_t b, uint64_t a)
|
||||
{
|
||||
uint32_t d = b >> 32,
|
||||
c = b;
|
||||
@@ -43,7 +43,7 @@ uint64_t reduce_mod_p(uint64_t b, uint64_t a)
|
||||
return a;
|
||||
}
|
||||
|
||||
uint64_t mult_mod_p(uint64_t x, uint64_t y)
|
||||
static uint64_t mult_mod_p(uint64_t x, uint64_t y)
|
||||
{
|
||||
uint32_t a = x,
|
||||
b = x >> 32,
|
||||
@@ -85,7 +85,7 @@ static const uint64_t NUM_ROUNDS = 7;
|
||||
/*
|
||||
* MDS matrix
|
||||
*/
|
||||
const uint64_t MDS[12][12] = {
|
||||
static const uint64_t MDS[12][12] = {
|
||||
{ 7, 23, 8, 26, 13, 10, 9, 7, 6, 22, 21, 8 },
|
||||
{ 8, 7, 23, 8, 26, 13, 10, 9, 7, 6, 22, 21 },
|
||||
{ 21, 8, 7, 23, 8, 26, 13, 10, 9, 7, 6, 22 },
|
||||
@@ -103,7 +103,7 @@ const uint64_t MDS[12][12] = {
|
||||
/*
|
||||
* Round constants.
|
||||
*/
|
||||
const uint64_t ARK1[7][12] = {
|
||||
static const uint64_t ARK1[7][12] = {
|
||||
{
|
||||
5789762306288267392ULL,
|
||||
6522564764413701783ULL,
|
||||
@@ -304,7 +304,7 @@ const uint64_t ARK2[7][12] = {
|
||||
},
|
||||
};
|
||||
|
||||
void apply_sbox(uint64_t *const state)
|
||||
static void apply_sbox(uint64_t *const state)
|
||||
{
|
||||
for (uint64_t i = 0; i < STATE_WIDTH; i++)
|
||||
{
|
||||
@@ -315,7 +315,7 @@ void apply_sbox(uint64_t *const state)
|
||||
}
|
||||
}
|
||||
|
||||
void apply_mds(uint64_t *state)
|
||||
static void apply_mds(uint64_t *state)
|
||||
{
|
||||
uint64_t res[STATE_WIDTH];
|
||||
for (uint64_t i = 0; i < STATE_WIDTH; i++)
|
||||
@@ -336,7 +336,7 @@ void apply_mds(uint64_t *state)
|
||||
}
|
||||
}
|
||||
|
||||
void apply_constants(uint64_t *const state, const uint64_t *ark)
|
||||
static void apply_constants(uint64_t *const state, const uint64_t *ark)
|
||||
{
|
||||
for (uint64_t i = 0; i < STATE_WIDTH; i++)
|
||||
{
|
||||
@@ -344,7 +344,7 @@ void apply_constants(uint64_t *const state, const uint64_t *ark)
|
||||
}
|
||||
}
|
||||
|
||||
void exp_acc(const uint64_t m, const uint64_t *base, const uint64_t *tail, uint64_t *const res)
|
||||
static void exp_acc(const uint64_t m, const uint64_t *base, const uint64_t *tail, uint64_t *const res)
|
||||
{
|
||||
for (uint64_t i = 0; i < m; i++)
|
||||
{
|
||||
@@ -367,7 +367,7 @@ void exp_acc(const uint64_t m, const uint64_t *base, const uint64_t *tail, uint6
|
||||
}
|
||||
}
|
||||
|
||||
void apply_inv_sbox(uint64_t *const state)
|
||||
static void apply_inv_sbox(uint64_t *const state)
|
||||
{
|
||||
uint64_t t1[STATE_WIDTH];
|
||||
for (uint64_t i = 0; i < STATE_WIDTH; i++)
|
||||
@@ -435,7 +435,7 @@ void apply_inv_sbox(uint64_t *const state)
|
||||
}
|
||||
}
|
||||
|
||||
void apply_round(uint64_t *const state, const uint64_t round)
|
||||
static void apply_round(uint64_t *const state, const uint64_t round)
|
||||
{
|
||||
apply_mds(state);
|
||||
apply_constants(state, ARK1[round]);
|
||||
@@ -579,4 +579,4 @@ void PQCLEAN_FALCON512_CLEAN_hash_to_point_rpo(rpo128_context *rc, uint16_t *x,
|
||||
*x++ = (uint16_t)w;
|
||||
n--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ use libc::c_int;
|
||||
// C IMPLEMENTATION INTERFACE
|
||||
// ================================================================================================
|
||||
|
||||
#[link(name = "rpo_falcon512", kind = "static")]
|
||||
extern "C" {
|
||||
/// Generate a new key pair. Public key goes into pk[], private key in sk[].
|
||||
/// Key sizes are exact (in bytes):
|
||||
@@ -97,19 +98,18 @@ pub struct Rpo128Context {
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::dsa::rpo_falcon512::{NONCE_LEN, PK_LEN, SIG_LEN, SK_LEN};
|
||||
use rand::Rng;
|
||||
use rand_utils::{rand_array, rand_value, rand_vector};
|
||||
|
||||
#[test]
|
||||
fn falcon_ffi() {
|
||||
unsafe {
|
||||
let mut rng = rand::thread_rng();
|
||||
//let mut rng = rand::thread_rng();
|
||||
|
||||
// --- generate a key pair from a seed ----------------------------
|
||||
|
||||
let mut pk = [0u8; PK_LEN];
|
||||
let mut sk = [0u8; SK_LEN];
|
||||
let seed: [u8; NONCE_LEN] =
|
||||
(0..NONCE_LEN).map(|_| rng.gen()).collect::<Vec<u8>>().try_into().unwrap();
|
||||
let seed: [u8; NONCE_LEN] = rand_array();
|
||||
|
||||
assert_eq!(
|
||||
0,
|
||||
@@ -122,8 +122,8 @@ mod tests {
|
||||
|
||||
// --- sign a message and make sure it verifies -------------------
|
||||
|
||||
let mlen: usize = rng.gen::<u16>() as usize;
|
||||
let msg: Vec<u8> = (0..mlen).map(|_| rng.gen()).collect();
|
||||
let mlen: usize = rand_value::<u16>() as usize;
|
||||
let msg: Vec<u8> = rand_vector(mlen);
|
||||
let mut detached_sig = [0u8; NONCE_LEN + SIG_LEN];
|
||||
let mut siglen = 0;
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ use mds_freq::mds_multiply_freq;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
#[cfg(feature = "arch-arm64-sve")]
|
||||
#[cfg(all(target_feature = "sve", feature = "sve"))]
|
||||
#[link(name = "rpo_sve", kind = "static")]
|
||||
extern "C" {
|
||||
fn add_constants_and_apply_sbox(
|
||||
@@ -375,7 +375,7 @@ impl Rpo256 {
|
||||
// --------------------------------------------------------------------------------------------
|
||||
|
||||
#[inline(always)]
|
||||
#[cfg(feature = "arch-arm64-sve")]
|
||||
#[cfg(all(target_feature = "sve", feature = "sve"))]
|
||||
fn optimized_add_constants_and_apply_sbox(
|
||||
state: &mut [Felt; STATE_WIDTH],
|
||||
ark: &[Felt; STATE_WIDTH],
|
||||
@@ -386,7 +386,7 @@ impl Rpo256 {
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
#[cfg(not(feature = "arch-arm64-sve"))]
|
||||
#[cfg(not(all(target_feature = "sve", feature = "sve")))]
|
||||
fn optimized_add_constants_and_apply_sbox(
|
||||
_state: &mut [Felt; STATE_WIDTH],
|
||||
_ark: &[Felt; STATE_WIDTH],
|
||||
@@ -395,7 +395,7 @@ impl Rpo256 {
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
#[cfg(feature = "arch-arm64-sve")]
|
||||
#[cfg(all(target_feature = "sve", feature = "sve"))]
|
||||
fn optimized_add_constants_and_apply_inv_sbox(
|
||||
state: &mut [Felt; STATE_WIDTH],
|
||||
ark: &[Felt; STATE_WIDTH],
|
||||
@@ -409,7 +409,7 @@ impl Rpo256 {
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
#[cfg(not(feature = "arch-arm64-sve"))]
|
||||
#[cfg(not(all(target_feature = "sve", feature = "sve")))]
|
||||
fn optimized_add_constants_and_apply_inv_sbox(
|
||||
_state: &mut [Felt; STATE_WIDTH],
|
||||
_ark: &[Felt; STATE_WIDTH],
|
||||
|
||||
10
src/main.rs
10
src/main.rs
@@ -31,23 +31,23 @@ pub fn benchmark_tsmt() {
|
||||
let tree_size = args.size;
|
||||
|
||||
// prepare the `leaves` vector for tree creation
|
||||
let mut leaves = Vec::new();
|
||||
let mut entries = Vec::new();
|
||||
for i in 0..tree_size {
|
||||
let key = rand_value::<RpoDigest>();
|
||||
let value = [ONE, ONE, ONE, Felt::new(i)];
|
||||
leaves.push((key, value));
|
||||
entries.push((key, value));
|
||||
}
|
||||
|
||||
let mut tree = construction(leaves, tree_size).unwrap();
|
||||
let mut tree = construction(entries, tree_size).unwrap();
|
||||
insertion(&mut tree, tree_size).unwrap();
|
||||
proof_generation(&mut tree, tree_size).unwrap();
|
||||
}
|
||||
|
||||
/// Runs the construction benchmark for the Tiered SMT, returning the constructed tree.
|
||||
pub fn construction(leaves: Vec<(RpoDigest, Word)>, size: u64) -> Result<TieredSmt, MerkleError> {
|
||||
pub fn construction(entries: Vec<(RpoDigest, Word)>, size: u64) -> Result<TieredSmt, MerkleError> {
|
||||
println!("Running a construction benchmark:");
|
||||
let now = Instant::now();
|
||||
let tree = TieredSmt::with_leaves(leaves)?;
|
||||
let tree = TieredSmt::with_entries(entries)?;
|
||||
let elapsed = now.elapsed();
|
||||
println!(
|
||||
"Constructed a TSMT with {} key-value pairs in {:.3} seconds",
|
||||
|
||||
@@ -73,7 +73,7 @@ impl TieredSmt {
|
||||
///
|
||||
/// # Errors
|
||||
/// Returns an error if the provided entries contain multiple values for the same key.
|
||||
pub fn with_leaves<R, I>(entries: R) -> Result<Self, MerkleError>
|
||||
pub fn with_entries<R, I>(entries: R) -> Result<Self, MerkleError>
|
||||
where
|
||||
R: IntoIterator<IntoIter = I>,
|
||||
I: Iterator<Item = (RpoDigest, Word)> + ExactSizeIterator,
|
||||
|
||||
Reference in New Issue
Block a user