Skip to content

Commit

Permalink
move test to ./tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hczphn committed Jan 13, 2025
1 parent c04a52d commit 3621177
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 75 deletions.
76 changes: 1 addition & 75 deletions circuit-std-rs/src/sha2_m31.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use extra::*;
use expander_compiler::frontend::*;
use sha2::{Digest, Sha256};
use crate::big_int::{to_binary_hint, big_endian_m31_array_put_uint32, bytes_to_bits, bit_array_to_m31, big_array_add, sigma0, sigma1, cap_sigma0, cap_sigma1, ch, maj, m31_to_bit_array};
use crate::big_int::{big_endian_m31_array_put_uint32, bytes_to_bits, bit_array_to_m31, big_array_add, sigma0, sigma1, cap_sigma0, cap_sigma1, ch, maj, m31_to_bit_array};

const SHA256LEN: usize = 32;
const CHUNK: usize = 64;
Expand Down Expand Up @@ -310,75 +308,3 @@ pub fn sha256_37bytes<C: Config, B:RootAPI<C>>(builder: &mut B, orign_data: &[Va
d.return_sum(builder).to_vec()
}


declare_circuit!(SHA25637BYTESCircuit {
input: [Variable;37],
output: [Variable;32],
});
pub fn check_sha256<C: Config, B: RootAPI<C>>(builder: &mut B, origin_data: &Vec<Variable>) ->Vec<Variable>{
let output = origin_data[37..].to_vec();
let result = sha256_37bytes(builder, &origin_data[..37]);
for i in 0..32 {
// println!("{}: {:?} {:?}", i, builder.value_of(result[i]), builder.value_of(output[i]));
builder.assert_is_equal(result[i], output[i]);
}
result
}
impl GenericDefine<M31Config> for SHA25637BYTESCircuit<Variable> {
fn define<Builder: RootAPI<M31Config>>(&self, builder: &mut Builder) {
for _ in 0..8 {
let mut data = self.input.to_vec();
data.append(&mut self.output.to_vec());
builder.memorized_simple_call(check_sha256, &data);
}
}
}



#[test]
fn test_sha256_37bytes(){
let mut hint_registry = HintRegistry::<M31>::new();
hint_registry.register("myhint.tobinary", to_binary_hint);
let compile_result = compile_generic(&SHA25637BYTESCircuit::default(),CompileOptions::default()).unwrap();
for i in 0..1{
let data = [i;37];
let mut hash = Sha256::new();
hash.update(&data);
let output = hash.finalize();
let mut assignment = SHA25637BYTESCircuit::default();
for i in 0..37 {
assignment.input[i] = M31::from(data[i] as u32);
}
for i in 0..32 {
assignment.output[i] = M31::from(output[i] as u32);
}
let witness = compile_result
.witness_solver
.solve_witness_with_hints(&assignment, &mut hint_registry)
.unwrap();
let output = compile_result.layered_circuit.run(&witness);
assert_eq!(output, vec![true]);
}
}
#[test]
fn debug_sha256_37bytes(){
let mut hint_registry = HintRegistry::<M31>::new();
hint_registry.register("myhint.tobinary", to_binary_hint);
let data = [255;37];
let mut hash = Sha256::new();
hash.update(&data);
let output = hash.finalize();
let mut assignment = SHA25637BYTESCircuit::default();
for i in 0..37 {
assignment.input[i] = M31::from(data[i] as u32);
}
for i in 0..32 {
assignment.output[i] = M31::from(output[i] as u32);
}
debug_eval(
&SHA25637BYTESCircuit::default(),
&assignment,
hint_registry,
);
}
73 changes: 73 additions & 0 deletions circuit-std-rs/tests/sha2_m31.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use extra::*;
use expander_compiler::frontend::*;
use sha2::{Digest, Sha256};
use circuit_std_rs::{big_int::to_binary_hint, sha2_m31::sha256_37bytes};


declare_circuit!(SHA25637BYTESCircuit {
input: [Variable;37],
output: [Variable;32],
});
pub fn check_sha256<C: Config, B: RootAPI<C>>(builder: &mut B, origin_data: &Vec<Variable>) ->Vec<Variable>{
let output = origin_data[37..].to_vec();
let result = sha256_37bytes(builder, &origin_data[..37]);
for i in 0..32 {
builder.assert_is_equal(result[i], output[i]);
}
result
}
impl GenericDefine<M31Config> for SHA25637BYTESCircuit<Variable> {
fn define<Builder: RootAPI<M31Config>>(&self, builder: &mut Builder) {
for _ in 0..8 {
let mut data = self.input.to_vec();
data.append(&mut self.output.to_vec());
builder.memorized_simple_call(check_sha256, &data);
}
}
}
#[test]
fn test_sha256_37bytes(){
let mut hint_registry = HintRegistry::<M31>::new();
hint_registry.register("myhint.tobinary", to_binary_hint);
let compile_result = compile_generic(&SHA25637BYTESCircuit::default(),CompileOptions::default()).unwrap();
for i in 0..1{
let data = [i;37];
let mut hash = Sha256::new();
hash.update(&data);
let output = hash.finalize();
let mut assignment = SHA25637BYTESCircuit::default();
for i in 0..37 {
assignment.input[i] = M31::from(data[i] as u32);
}
for i in 0..32 {
assignment.output[i] = M31::from(output[i] as u32);
}
let witness = compile_result
.witness_solver
.solve_witness_with_hints(&assignment, &mut hint_registry)
.unwrap();
let output = compile_result.layered_circuit.run(&witness);
assert_eq!(output, vec![true]);
}
}
#[test]
fn debug_sha256_37bytes(){
let mut hint_registry = HintRegistry::<M31>::new();
hint_registry.register("myhint.tobinary", to_binary_hint);
let data = [255;37];
let mut hash = Sha256::new();
hash.update(&data);
let output = hash.finalize();
let mut assignment = SHA25637BYTESCircuit::default();
for i in 0..37 {
assignment.input[i] = M31::from(data[i] as u32);
}
for i in 0..32 {
assignment.output[i] = M31::from(output[i] as u32);
}
debug_eval(
&SHA25637BYTESCircuit::default(),
&assignment,
hint_registry,
);
}

0 comments on commit 3621177

Please sign in to comment.