Skip to content

Commit

Permalink
test: add prove-verify test for Falcon DSA verification
Browse files Browse the repository at this point in the history
  • Loading branch information
Al-Kindi-0 committed Feb 2, 2024
1 parent 0a4fa7a commit ea17647
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 16 deletions.
59 changes: 44 additions & 15 deletions stdlib/tests/crypto/falcon.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,55 @@
use assembly::utils::Serializable;
use miden_air::{Felt, StarkField};
use processor::Digest;

use std::vec;
use assembly::{utils::Serializable, Assembler};
use miden_air::{Felt, ProvingOptions, StarkField};
use miden_stdlib::StdLibrary;
use processor::{AdviceInputs, DefaultHost, Digest, MemAdviceProvider, StackInputs};
use test_utils::{
crypto::{rpo_falcon512::KeyPair, MerkleStore},
rand::rand_vector,
Test, Word,
ProgramInfo, Word,
};

#[test]
fn test_falcon() {
fn falcon_execution() {
let keypair = KeyPair::new().unwrap();

let message = rand_vector::<Felt>(4).try_into().unwrap();
let (source, op_stack, adv_stack, store, advice_map) = generate_test(keypair, message);

let test = generate_test(keypair, message);
let test = build_test!(source, &op_stack, &adv_stack, store, advice_map.into_iter());
test.expect_stack(&[])
}

fn generate_test(keypair: KeyPair, message: Word) -> Test {
#[test]
fn falcon_prove_verify() {
let keypair = KeyPair::new().unwrap();
let message = rand_vector::<Felt>(4).try_into().unwrap();
let (source, op_stack, _, _, advice_map) = generate_test(keypair, message);

let program = Assembler::default()
.with_library(&StdLibrary::default())
.expect("failed to load stdlib")
.compile(&source)
.expect("failed to compile test source");

let stack_inputs =
StackInputs::try_from_values(op_stack).expect("failed to create stack inputs");
let advice_inputs = AdviceInputs::default().with_map(advice_map);
let advice_provider = MemAdviceProvider::from(advice_inputs);
let host = DefaultHost::new(advice_provider);

let options = ProvingOptions::with_96_bit_security(false);
let (stack_outputs, proof) = test_utils::prove(&program, stack_inputs.clone(), host, options)
.expect("failed to generate proof");

let program_info = ProgramInfo::from(program);
let result = test_utils::verify(program_info, stack_inputs, stack_outputs, proof);

assert!(result.is_ok(), "error: {result:?}");
}

fn generate_test(
keypair: KeyPair,
message: Word,
) -> (&'static str, Vec<u64>, Vec<u64>, MerkleStore, Vec<([u8; 32], Vec<Felt>)>) {
let source = "
use.std::crypto::dsa::rpo_falcon512
Expand All @@ -31,18 +61,17 @@ fn generate_test(keypair: KeyPair, message: Word) -> Test {
let pk: Word = keypair.public_key().into();
let pk: Digest = pk.into();
let pk_sk_bytes = keypair.to_bytes();
let to_adv_map = pk_sk_bytes.iter().map(|a| Felt::new(*a as u64)).collect::<Vec<Felt>>();

let to_adv_map = pk_sk_bytes.iter().map(|a| Felt::new(*a as u64)).collect::<Vec<Felt>>();
let advice_map: Vec<([u8; 32], Vec<Felt>)> = vec![(pk.as_bytes(), to_adv_map.into())];

let message = message.into_iter().map(|a| a.as_int() as u64).collect::<Vec<u64>>();

let mut op_stack = vec![];
let message = message.into_iter().map(|a| a.as_int() as u64).collect::<Vec<u64>>();
op_stack.extend_from_slice(&message);
op_stack.extend_from_slice(&pk.as_elements().iter().map(|a| a.as_int()).collect::<Vec<u64>>());

let adv_stack = vec![];
let store = MerkleStore::new();
let test = build_test!(source, &op_stack, &adv_stack, store, advice_map.into_iter());

test
(source, op_stack, adv_stack, store, advice_map)
}
2 changes: 1 addition & 1 deletion test-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub use processor::{
};
pub use prover::{prove, MemAdviceProvider, ProvingOptions};
pub use test_case::test_case;
pub use verifier::{AcceptableOptions, ProgramInfo, VerifierError};
pub use verifier::{verify, AcceptableOptions, ProgramInfo, VerifierError};
pub use vm_core::{
stack::STACK_TOP_SIZE,
utils::{collections, group_slice_elements, group_vector_elements, IntoBytes, ToElements},
Expand Down

0 comments on commit ea17647

Please sign in to comment.