Skip to content

Commit

Permalink
remove unnecessary calculations in proof generation
Browse files Browse the repository at this point in the history
  • Loading branch information
lovely-necromancer committed May 19, 2024
1 parent 9e0ea3e commit 4d4c33f
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 74 deletions.
137 changes: 82 additions & 55 deletions circuits/image_hash.circom
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,80 @@ include "node_modules/circomlib/circuits/bitify.circom";
include "node_modules/circomlib/circuits/poseidon.circom";


template GrayScale(n) {
signal input in[n][3];
// signal input gray[n];

signal output out[n];
// signal output n_check;

component lt[n][2];

for (var i = 0; i < n; i++) {
var gray_value = 299 * in[i][0] + 587 * in[i][1] + 114 * in[i][2];
out[i] <== gray_value;
}
}

template Resize(width, height){
// template DoubleShiftRight() {
// signal input in;
// signal output out;

// component toBits = Num2Bits(8);
// component toNum = Bits2Num(6);

// toBits.in <== in;
// toNum.in[0] <== toBits.out[2];
// toNum.in[1] <== toBits.out[3];
// toNum.in[2] <== toBits.out[4];
// toNum.in[3] <== toBits.out[5];
// toNum.in[4] <== toBits.out[6];
// toNum.in[5] <== toBits.out[7];

// out <== toNum.out;

// }

// template Resize(width, height){
// signal input in[height][width];
// var new_height = height/2;
// var new_width = width/2;
// signal output out[new_height][new_width];

// component divider[height][width];

// for(var i = 0; i < new_height; i++){
// for(var j=0; j< new_width; j++){
// divider[i*2][j*2] = DoubleShiftRight();
// divider[i*2][j*2].in <== in[i*2][j*2];
// divider[i*2][j*2+1] = DoubleShiftRight();
// divider[i*2][j*2+1].in <== in[i*2][j*2+1];
// divider[i*2+1][j*2] = DoubleShiftRight();
// divider[i*2+1][j*2].in <== in[i*2+1][j*2];
// divider[i*2+1][j*2+1] = DoubleShiftRight();
// divider[i*2+1][j*2+1].in <== in[i*2+1][j*2+1];

// var mean = divider[i*2][j*2].out + divider[i*2][j*2+1].out + divider[i*2+1][j*2].out + divider[i*2+1][j*2+1].out;
// // var mean = (in[i*2][j*2] + in[i*2][j*2+1] + in[i*2+1][j*2] + in[i*2+1][j*2+1]) \ 4;
// log(mean);
// out[i][j] <== mean;
// }
// }

// }


template ResizeNearest(width, height){
signal input in[height][width];
var new_height = height/2;
var new_width = width/2;
signal output out[new_height][new_width];


for(var i = 0; i < new_height; i++){
for(var j=0; j< new_width; j++){
var mean = (in[i][j*2] + in[i][j*2+1] + in[i+1][j*2] + in[i+1][j*2+1])/4;
out[i][j] <== mean;
out[i][j] <== in[i*2][j*2];
if (in[i*2][j*2] > 255) {
log(in[i*2][j*2]);
}
}
}

}

template CompressorGrey(size){
template CompressorGray(size){
signal input in[size];
signal output out;

component toBits[size];
component toNum = Bits2Num(size * 8);

for (var i=0; i<size; i++) {
var j=0;
toBits[i] = Num2Bits(8);
toBits[i] = Num2Bits(8);
toBits[i].in <== in[i];
toNum.in[i*8] <== toBits[i].out[0];
toNum.in[i*8+1] <== toBits[i].out[1];
Expand All @@ -65,59 +98,53 @@ template GrayScaleHash(width, height){
// public inputs
signal input step_in;

signal input orig[height][width][3];
// signal output hash_out;
signal gray_input [height][width];
signal input compressed [40];

//outputs
signal output step_out;

// grayscale code here ...
component grayscale[height];

signal prev_hash <== step_in;

for (var i = 0; i< height; i++){
grayscale[i] = GrayScale(width);
grayscale[i].in <== orig[i];
gray_input[i] <== grayscale[i].out;
}

component resize1 = Resize(640, 480);
resize1.in <== gray_input;
// component resize1 = ResizeNearest(640, 480);
// resize1.in <== gray_input;

component resize2 = Resize(320, 240);
resize2.in <== resize1.out;

component resize3 = Resize(160, 120);
resize3.in <== resize2.out;

component resize4 = Resize(80, 60);
resize4.in <== resize3.out;


component compressor[40];
for (var i=0; i < 40; i++) {
compressor[i] = CompressorGrey(30);
for (var j=0; j < 30; j++) {
compressor[i].in[j] <== resize4.out[j][i];
}

}
// component resize2 = ResizeNearest(320, 240);
// resize2.in <== resize1.out;

// component resize3 = ResizeNearest(160, 120);
// resize3.in <== resize2.out;

// component resize4 = ResizeNearest(80, 60);
// resize4.in <== resize3.out;

// component compressor[40];
// for (var i=0; i < 40; i++) {
// compressor[i] = CompressorGray(30);
// for (var j=0; j < 30; j++) {
// compressor[i].in[j] <== resize4.out[j][i];
// }
// }

component hasher[39];

for(var i=0; i < 39; i++) {
hasher[i] = Poseidon(2);
hasher[i].inputs[0] <== i == 0 ? compressor[0].out : hasher[i-1].out;
hasher[i].inputs[1] <== compressor[i+1].out;
hasher[i].inputs[0] <== i == 0 ? compressed[0] : hasher[i-1].out;
hasher[i].inputs[1] <== compressed[i+1];
}

component final_hash = Poseidon(2);
final_hash.inputs[0] <== prev_hash;
final_hash.inputs[1] <== hasher[38].out;
step_out <== final_hash.out;
// log(step_out);

log("----------------");
log(step_in);
log(step_out);


}


Expand Down
3 changes: 3 additions & 0 deletions circuits/merkle_hash.circom
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ template MerkleHash() {
lastDual.s <== lastPathSel;
lastHash.inputs <== lastDual.out;
step_out[1] <== lastHash.out;

log(step_in[0]);
log(step_in[1]);
}

component main {public [step_in]} = MerkleHash();
4 changes: 2 additions & 2 deletions nova/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct PathInput {

#[derive(Deserialize)]
struct FrameInput {
orig: Vec<Vec<Vec<String>>>,
compressed: Vec<String>,
}

fn fold_fold_fold(proof_type: String,
Expand Down Expand Up @@ -83,7 +83,7 @@ fn fold_fold_fold(proof_type: String,
let input_data: FrameInput = serde_json::from_str(&input_file_json_string).expect("Deserialization failed");

let mut private_input = HashMap::new();
private_input.insert("orig".to_string(), json!(input_data.orig));
private_input.insert("compressed".to_string(), json!(input_data.compressed));
private_inputs.push(private_input);
}

Expand Down
4 changes: 2 additions & 2 deletions python/prove_phase.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from utils.calc_merkle_path import calc_merkle_path
from utils.convert_to_sd import convert_to_sd
from utils.video_edit import trim
from utils.video_edit import trim_grc


def get_video_path():
Expand All @@ -34,7 +34,7 @@ def get_video_path():
convert_to_sd(video_path, sd_video, 30)

# Trim the video
start_frame, end_frame, total_frames = trim(sd_video, start_time, end_time, output_path, trimmed_video)
start_frame, end_frame, total_frames = trim_grc(sd_video, start_time, end_time, output_path, trimmed_video)

# # Step 2
# tmp, fps = grayscale_video(trimmed_video, gray_video)
Expand Down
38 changes: 23 additions & 15 deletions python/utils/video_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ def resize_frame(frame):
# Perform bilinear interpolation
for i in range(int(_height)):
for j in range(int(_width)):
summ = 0
for m in range (k):
for n in range (k):
summ += frame[i*k+n, j*k+m] / (k * k)
summ = frame[i*k, j*k]
new_img_array[i, j] = summ

return new_img_array
Expand Down Expand Up @@ -57,37 +54,37 @@ def grayscale_resize_compress(input_video_path):
# Process each frame of the video
# processed_frames = []
output_array = []
for frame in clip.iter_frames(dtype="uint8"):
# Convert the frame to grayscale
for i, frame in enumerate(clip.iter_frames(dtype="uint8")):
# Convert the frame to grayscale
image = Image.fromarray(frame)
grayscale_image = image.convert('L')

resized_frame = resize_frame(np.array(grayscale_image))

frame_array = []
for i in range(0, len(resized_frame)):
for ii in range(0, len(resized_frame[0])):
hexValue = ''
for j in range(0, len(resized_frame[i])):
hexValue = hex(int(resized_frame[i][j]))[2:].zfill(2) + hexValue
for j in range(0, len(resized_frame)):
hexValue = hex(int(resized_frame[j][ii]))[2:].zfill(2) + hexValue
frame_array.append("0x" + hexValue)
output_array.append(frame_array)
return output_array


def pixel_to_array(image_in):
array_in = image_in.tolist()
array_in = np.array(image_in).tolist()
output_array = []

for i in range(0, len(array_in)):
row = []
hexValue = ''
for j in range(0, len(array_in[i])):
row.append(["0x" + hex(int(array_in[i][j][k]))[2:].zfill(2) for k in range(0, 3)])
row.append(hex(int(array_in[i][j])))
output_array.append(row)
return output_array


def trim(input_video_path, start_time, end_time, output_path, output_video_path):
def trim_grc(input_video_path, start_time, end_time, output_path, output_video_path):

if os.path.exists(output_path):
shutil.rmtree(output_path)
Expand All @@ -111,20 +108,31 @@ def trim(input_video_path, start_time, end_time, output_path, output_video_path)
# Process each frame of the video
processed_frames = []
# original_frames = []

for i, frame in enumerate(clip.iter_frames(dtype="uint8")):
if i < start_frame:
continue
if i > end_frame:
break
# image = Image.fromarray(frame)
image = Image.fromarray(frame)
processed_frames.append(frame)
gray_frame = image.convert("L")
# processed_frames.append(image)

resized_frame = resize_frame(np.array(gray_frame))

frame_array = []
for ii in range(0, len(resized_frame[0])):
hexValue = ''
for j in range(0, len(resized_frame)):
hexValue = hex(int(resized_frame[j][ii]))[2:].zfill(2) + hexValue
frame_array.append("0x" + hexValue)

frame_output_path = os.path.join(output_path, f"frame_{i-start_frame}.json")
# cv2.imwrite(frame_output_path, frame)

frame_data = {
"orig": pixel_to_array(frame),
"compressed": frame_array,
}
with open(frame_output_path, 'w') as fp:
json.dump(frame_data, fp, indent=4)
Expand Down

0 comments on commit 4d4c33f

Please sign in to comment.