arnaucube
arnaucube/sonobe-playgroundPublic Self-hosted repository
sonobe-playground/circuit/grayscale_step.circom
circuit/grayscale_step.circomRaw
 1pragma circom 2.0.0;
 2
 3include "utils/row_hasher.circom";
 4include "utils/pixels.circom";
 5include "node_modules/circomlib/circuits/bitify.circom";
 6
 7
 8template GrayscaleChecker(n) {
 9    signal input orig[n][3];
10    signal input gray[n];
11
12    signal output n_check;
13 
14    component lt[n][2];
15
16    for (var i = 0; i < n; i++) {      
17        var inter = 299 * orig[i][0] + 587 * orig[i][1] + 114 * orig[i][2];
18
19        lt[i][0] = LessEqThan(18);
20        lt[i][1] = LessEqThan(18);
21
22        lt[i][0].in[1] <== 1000;
23        lt[i][0].in[0] <== inter - 1000 * gray[i];
24        lt[i][0].out === 1;
25
26        lt[i][1].in[1] <== 1000;
27        lt[i][1].in[0] <== 1000 * gray[i] - inter;
28        lt[i][1].out === 1; 
29    }
30    n_check <== n;
31}
32
33template GrayScale(width){
34    
35    signal input original[width];
36    signal input transformed[width];
37    
38    component decompressor[width];
39    component decompressor_grey[width];
40    component greychecker[width];
41
42    for (var j=0; j<width; j++) {
43        decompressor[j] = Decompressor();
44        decompressor[j].in <== original[j];
45
46        decompressor_grey[j] = DecompressorGrey();
47        decompressor_grey[j].in <== transformed[j];
48        
49        greychecker[j] = GrayscaleChecker(10);
50        greychecker[j].orig <== decompressor[j].out;
51        greychecker[j].gray <== decompressor_grey[j].out;
52    }
53
54}
55
56template GrayScaleHash(width){
57    signal input ivc_input[2];
58    // signal input prev_orig_hash;
59    // signal input prev_gray_hash;
60    signal output ivc_output[2];
61    // signal output next_orig_hash;
62    // signal output next_gray_hash;
63    
64    // Private inputs
65    signal input external_inputs [2 * width];
66
67    signal row_orig [width];
68    signal row_tran [width];
69
70    for (var i = 0; i < width; i++) {
71        row_orig[i] <== external_inputs[i];
72        row_tran[i] <== external_inputs[i + width];
73    }
74
75    component orig_row_hasher = RowHasher(width);
76    component gray_row_hasher = RowHasher(width);
77    component orig_hasher = Hasher(2);
78    component gray_hasher = Hasher(2);
79
80    orig_row_hasher.img <== row_orig;
81    orig_hasher.values[0] <== ivc_input[0]; // prev_orig_hash
82    orig_hasher.values[1] <== orig_row_hasher.hash;
83    ivc_output[0] <== orig_hasher.hash; // next_orig_hash
84
85    gray_row_hasher.img <== row_tran;
86    gray_hasher.values[0] <== ivc_input[1]; // prev_gray_hash
87    gray_hasher.values[1] <== gray_row_hasher.hash;
88    ivc_output[1] <== gray_hasher.hash; // next_grey_hash
89
90    // grayscale code here ...
91    component checker = GrayScale(width);
92    checker.original <== row_orig;
93    checker.transformed <== row_tran;
94
95}
96
97component main { public [ivc_input] } = GrayScaleHash(128);