Skip to content

Commit 48f7448

Browse files
committed
test: add tests for Sha2 guest
1 parent 4507b2b commit 48f7448

File tree

2 files changed

+76
-12
lines changed

2 files changed

+76
-12
lines changed

.github/workflows/guest-lib-tests.cuda.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ jobs:
141141
elif [[ "$crate" == "ruint" ]]; then
142142
# only run integration tests because other tests are not OpenVM related
143143
SPECIAL_ARGS="test_matrix_power"
144+
elif [[ "$crate" == "sha2" ]]; then
145+
# also run proving tests
146+
SPECIAL_ARGS="--run-ignored=all"
144147
fi
145148
146149
${{ env.NEXTEST_ENV }} cargo nextest run --cargo-profile=fast ${{ env.FEATURE_ARGS }} $SPECIAL_ARGS --no-tests=pass --test-threads=1

guest-libs/sha2/tests/lib.rs

Lines changed: 73 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#[cfg(test)]
22
mod tests {
3+
use std::fs;
4+
35
use eyre::Result;
46
use hex::FromHex;
5-
use openvm_circuit::{arch::Streams, utils::air_test_with_min_segments};
7+
use openvm_circuit::{arch::VmExecutor, utils::air_test_with_min_segments};
68
use openvm_instructions::exe::VmExe;
79
use openvm_rv32im_transpiler::{
810
Rv32ITranspilerExtension, Rv32IoTranspilerExtension, Rv32MTranspilerExtension,
@@ -13,7 +15,6 @@ mod tests {
1315
use openvm_stark_sdk::p3_baby_bear::BabyBear;
1416
use openvm_toolchain_tests::{build_example_program_at_path, get_programs_dir};
1517
use openvm_transpiler::{transpiler::Transpiler, FromElf};
16-
use std::fs;
1718

1819
type F = BabyBear;
1920

@@ -28,6 +29,7 @@ mod tests {
2829
expected_output: Vec<u8>,
2930
}
3031

32+
// test vectors are from https://csrc.nist.gov/projects/cryptographic-algorithm-validation-program/secure-hashing
3133
fn parse_test_vectors(file_name: &str) -> Vec<TestVector> {
3234
let mut test_vectors = Vec::new();
3335

@@ -65,7 +67,7 @@ mod tests {
6567
test_vectors
6668
}
6769

68-
fn test_sha2(test_vector_file_name: &str, sha2_type: Sha2Type) -> Result<()> {
70+
fn test_sha2_base(test_vector_file_name: &str, sha2_type: Sha2Type, prove: bool) -> Result<()> {
6971
let config = Sha2Rv32Config::default();
7072
let elf =
7173
build_example_program_at_path(get_programs_dir!("tests/programs"), "sha2", &config)?;
@@ -88,20 +90,79 @@ mod tests {
8890
stdin.write(&test_vector.input);
8991
stdin.write(&test_vector.expected_output);
9092

91-
air_test_with_min_segments(
92-
Sha2Rv32Builder,
93-
config.clone(),
94-
openvm_exe.clone(),
95-
<Streams<F> as From<StdIn<F>>>::from(stdin),
96-
1,
97-
);
93+
if prove {
94+
air_test_with_min_segments(
95+
Sha2Rv32Builder,
96+
config.clone(),
97+
openvm_exe.clone(),
98+
stdin,
99+
1,
100+
);
101+
} else {
102+
let executor = VmExecutor::new(config.clone())?;
103+
let interpreter = executor.instance(&openvm_exe)?;
104+
#[allow(unused_variables)]
105+
let state = interpreter.execute(stdin.clone(), None)?;
106+
107+
#[cfg(feature = "aot")]
108+
{
109+
use openvm_circuit::{arch::VmState, system::memory::online::GuestMemory};
110+
let naive_interpreter = executor.interpreter_instance(&exe)?;
111+
let naive_state = naive_interpreter.execute(stdin, None)?;
112+
let assert_vm_state_eq =
113+
|lhs: &VmState<BabyBear, GuestMemory>,
114+
rhs: &VmState<BabyBear, GuestMemory>| {
115+
assert_eq!(lhs.pc(), rhs.pc());
116+
for r in 0..32 {
117+
let a = unsafe { lhs.memory.read::<u8, 1>(1, r as u32) };
118+
let b = unsafe { rhs.memory.read::<u8, 1>(1, r as u32) };
119+
assert_eq!(a, b);
120+
}
121+
};
122+
assert_vm_state_eq(&state, &naive_state);
123+
}
124+
}
98125
}
99126

100127
Ok(())
101128
}
102129

103130
#[test]
104-
fn test_sha256_short() -> Result<()> {
105-
test_sha2("SHA256ShortMsg.rsp", Sha2Type::Sha256)
131+
fn test_sha256_run() -> Result<()> {
132+
test_sha2_base("SHA256ShortMsg.rsp", Sha2Type::Sha256, false)?;
133+
test_sha2_base("SHA256LongMsg.rsp", Sha2Type::Sha256, false)
134+
}
135+
136+
#[test]
137+
fn test_sha384_run() -> Result<()> {
138+
test_sha2_base("SHA384ShortMsg.rsp", Sha2Type::Sha384, false)?;
139+
test_sha2_base("SHA384LongMsg.rsp", Sha2Type::Sha384, false)
140+
}
141+
142+
#[test]
143+
fn test_sha512_run() -> Result<()> {
144+
test_sha2_base("SHA512ShortMsg.rsp", Sha2Type::Sha512, false)?;
145+
test_sha2_base("SHA512LongMsg.rsp", Sha2Type::Sha512, false)
146+
}
147+
148+
#[test]
149+
#[ignore = "proving on CPU is slow"]
150+
fn test_sha256_prove() -> Result<()> {
151+
test_sha2_base("SHA256ShortMsg.rsp", Sha2Type::Sha256, true)?;
152+
test_sha2_base("SHA256LongMsg.rsp", Sha2Type::Sha256, true)
153+
}
154+
155+
#[test]
156+
#[ignore = "proving on CPU is slow"]
157+
fn test_sha384_prove() -> Result<()> {
158+
test_sha2_base("SHA384ShortMsg.rsp", Sha2Type::Sha384, true)?;
159+
test_sha2_base("SHA384LongMsg.rsp", Sha2Type::Sha384, true)
160+
}
161+
162+
#[test]
163+
#[ignore = "proving on CPU is slow"]
164+
fn test_sha512_prove() -> Result<()> {
165+
test_sha2_base("SHA512ShortMsg.rsp", Sha2Type::Sha512, true)?;
166+
test_sha2_base("SHA512LongMsg.rsp", Sha2Type::Sha512, true)
106167
}
107168
}

0 commit comments

Comments
 (0)