-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ruanpc/pi compress #75
Open
RUAN0007
wants to merge
9
commits into
dev
Choose a base branch
from
ruanpc/pi_compress
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
38586c0
add split gates
RUAN0007 d766fd8
finish compressing pub input
RUAN0007 49cea36
Merge remote-tracking branch 'origin/dev' into ruanpc/pi_compress
RUAN0007 4bfa82f
fix e2e test
RUAN0007 2ad4f52
fix bug
RUAN0007 6c69261
fix hardhat config
cliff0412 6e80f01
Merge remote-tracking branch 'origin/ruanpc/pi_compress' into ruanpc/…
cliff0412 ec6a95b
put 3 to constants
RUAN0007 e4e25b0
Merge remote-tracking branch 'origin/ruanpc/pi_compress' into ruanpc/…
RUAN0007 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -71,3 +71,111 @@ template RandomAccess2(N, M) { | |
} | ||
} | ||
|
||
template ReverseEndian64() { | ||
signal input in; | ||
signal output out; | ||
|
||
var mask = ((1<<8) -1); | ||
var tmp = (in ) & mask; | ||
tmp = tmp * (1<<8) + ((in >> (8*1) ) & mask); | ||
tmp = tmp * (1<<8) + ((in >> (8*2) ) & mask); | ||
tmp = tmp * (1<<8) + ((in >> (8*3) ) & mask); | ||
tmp = tmp * (1<<8) + ((in >> (8*4) ) & mask); | ||
tmp = tmp * (1<<8) + ((in >> (8*5) ) & mask); | ||
tmp = tmp * (1<<8) + ((in >> (8*6) ) & mask); | ||
tmp = tmp * (1<<8) + ((in >> (8*7) ) & mask); | ||
|
||
out <--tmp; | ||
} | ||
|
||
// convert Bn128 Scalar in Big Endian to 3 gl numbers in Little Endian | ||
template BEBn128ToLEGl3() { | ||
signal input in; | ||
signal output low_le_gl; | ||
signal output mid_le_gl; | ||
signal output high_le_gl; | ||
|
||
signal low_be_bn128; | ||
signal mid_be_bn128; | ||
signal high_be_bn128; | ||
|
||
var acc=0; | ||
var mask = (1<<64) -1; | ||
low_be_bn128 <-- (in) & mask; | ||
acc += low_be_bn128; | ||
|
||
mid_be_bn128 <-- (in >> 64) & mask; | ||
acc += mid_be_bn128 * (1 << 64); | ||
|
||
high_be_bn128 <-- (in >> 128) & mask; | ||
acc += high_be_bn128 * (1 << 128); | ||
|
||
acc === in; | ||
// log("in ", in); | ||
// log("mask ", mask); | ||
// log("low_be_128 ", low_be_bn128); | ||
// log("mid_be_128 ", mid_be_bn128); | ||
// log("high_be_128 ", high_be_bn128); | ||
|
||
component reverse_endians[3]; | ||
for (var i = 0; i < 3; i++) { | ||
reverse_endians[i] = ReverseEndian64(); | ||
} | ||
|
||
reverse_endians[0].in <== low_be_bn128; | ||
high_le_gl <== reverse_endians[0].out; | ||
|
||
reverse_endians[1].in <== mid_be_bn128; | ||
mid_le_gl <== reverse_endians[1].out; | ||
|
||
reverse_endians[2].in <== high_be_bn128; | ||
low_le_gl <== reverse_endians[2].out; | ||
// log("reverse_endians[2].out ", reverse_endians[2].out); | ||
} | ||
|
||
template compress_3gl() { | ||
signal input low; | ||
signal input mid; | ||
signal input high; | ||
signal output out; | ||
|
||
var acc=0; | ||
acc += low; | ||
acc += mid * (1 << 64); | ||
acc += high * (1 << 128); | ||
out <== acc; | ||
} | ||
|
||
function NUM_COMPRESSED_INPUTS(num_input) { | ||
if (num_input % 3 == 0) { | ||
return num_input \ 3; | ||
} else { | ||
return (num_input \ 3) + 1; | ||
} | ||
} | ||
|
||
template compress_inputs() { | ||
var num_inputs = NUM_PUBLIC_INPUTS(); | ||
signal input public_inputs[num_inputs]; | ||
|
||
var num_compressed_inputs = NUM_COMPRESSED_INPUTS(num_inputs); | ||
signal output compressed_inputs[num_compressed_inputs]; | ||
signal pad0_public_inputs[num_compressed_inputs * 3]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here also |
||
|
||
for(var i = 0; i < num_inputs; i++){ | ||
pad0_public_inputs[i] <== public_inputs[i]; | ||
} | ||
for(var i = num_inputs; i < num_compressed_inputs * 3; i++){ | ||
pad0_public_inputs[i] <== 0; | ||
} | ||
|
||
var y = 0; | ||
component compressors[num_compressed_inputs]; | ||
for(var i = 0; i < num_compressed_inputs; i++){ | ||
compressors[i] = compress_3gl(); | ||
compressors[i].low <== pad0_public_inputs[3*i+0]; | ||
compressors[i].mid <== pad0_public_inputs[3*i+1]; | ||
compressors[i].high <== pad0_public_inputs[3*i+2]; | ||
compressed_inputs[i] <== compressors[i].out; | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can put 3 to constants, rather than hard code