-
Notifications
You must be signed in to change notification settings - Fork 14
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
Showing
2 changed files
with
74 additions
and
75 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
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,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, | ||
); | ||
} |