Skip to content
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

Bounds check non-strict Num2Bits and Bits2Nums #69

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions circuits/bitify.circom
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ pragma circom 2.0.0;
include "comparators.circom";
include "aliascheck.circom";


template Num2Bits(n) {
assert(n <= 253);
signal input in;
signal output out[n];
var lc1=0;
Expand All @@ -41,18 +41,26 @@ template Num2Bits(n) {
template Num2Bits_strict() {
signal input in;
signal output out[254];
var lc1=0;

component aliasCheck = AliasCheck();
component n2b = Num2Bits(254);
in ==> n2b.in;
var e2=1;
for (var i = 0; i<254; i++) {
out[i] <-- (in >> i) & 1;
out[i] * (out[i] -1 ) === 0;
lc1 += out[i] * e2;
e2 = e2+e2;
}

lc1 === in;

component aliasCheck = AliasCheck();
for (var i=0; i<254; i++) {
n2b.out[i] ==> out[i];
n2b.out[i] ==> aliasCheck.in[i];
out[i] ==> aliasCheck.in[i];
}
}

template Bits2Num(n) {
assert(n <= 253);
signal input in[n];
signal output out;
var lc1=0;
Expand All @@ -69,19 +77,24 @@ template Bits2Num(n) {
template Bits2Num_strict() {
signal input in[254];
signal output out;
var lc1=0;

component aliasCheck = AliasCheck();
component b2n = Bits2Num(254);
var e2 = 1;
for (var i = 0; i<254; i++) {
lc1 += in[i] * e2;
e2 = e2 + e2;
}

lc1 ==> out;

component aliasCheck = AliasCheck();
for (var i=0; i<254; i++) {
in[i] ==> b2n.in[i];
in[i] ==> aliasCheck.in[i];
}

b2n.out ==> out;
}

template Num2BitsNeg(n) {
assert(n <= 253);
signal input in;
signal output out[n];
var lc1=0;
Expand Down
26 changes: 4 additions & 22 deletions circuits/pointbits.circom
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,7 @@ template Bits2Point_Strict() {

var i;

// Check aliasing
component aliasCheckY = AliasCheck();
for (i=0; i<254; i++) {
aliasCheckY.in[i] <== in[i];
}
in[254] === 0;

component b2nY = Bits2Num(254);
component b2nY = Bits2Num_strict();
for (i=0; i<254; i++) {
b2nY.in[i] <== in[i];
}
Expand All @@ -110,12 +103,8 @@ template Bits2Point_Strict() {
babyCheck.x <== out[0];
babyCheck.y <== out[1];

component n2bX = Num2Bits(254);
component n2bX = Num2Bits_strict();
n2bX.in <== out[0];
component aliasCheckX = AliasCheck();
for (i=0; i<254; i++) {
aliasCheckX.in[i] <== n2bX.out[i];
}

component signCalc = CompConstant(10944121435919637611123202872628637544274182200208017171849102093287904247808);
for (i=0; i<254; i++) {
Expand All @@ -139,18 +128,11 @@ template Point2Bits_Strict() {

var i;

component n2bX = Num2Bits(254);
component n2bX = Num2Bits_strict();
n2bX.in <== in[0];
component n2bY = Num2Bits(254);
component n2bY = Num2Bits_strict();
n2bY.in <== in[1];

component aliasCheckX = AliasCheck();
component aliasCheckY = AliasCheck();
for (i=0; i<254; i++) {
aliasCheckX.in[i] <== n2bX.out[i];
aliasCheckY.in[i] <== n2bY.out[i];
}

component signCalc = CompConstant(10944121435919637611123202872628637544274182200208017171849102093287904247808);
for (i=0; i<254; i++) {
signCalc.in[i] <== n2bX.out[i];
Expand Down