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

Fix lte for UInt64 and UInt32 and tests #263

Closed
wants to merge 1 commit into from
Closed
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
115 changes: 46 additions & 69 deletions src/lib/int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,7 @@ describe('int', () => {
});

describe('assertLt', () => {
// rangeCheckHelper: Expected 28948022309329048855892746252171976963363056481941560715954676764349967630336 to fit in 64 bits
it.skip('1<2=true', () => {
it('1<2=true', () => {
expect(() => {
Circuit.runAndCheck(() => {
const x = Circuit.witness(UInt64, () => new UInt64(Field.one));
Expand Down Expand Up @@ -479,8 +478,7 @@ describe('int', () => {
}).toThrow();
});

// rangeCheckHelper: Expected 28948022309329048855892746252171976963363056481941560715954676764349967531337 to fit in 64 bits
it.skip('1000<100000=true', () => {
it('1000<100000=true', () => {
expect(() => {
Circuit.runAndCheck(() => {
const x = Circuit.witness(UInt64, () => new UInt64(Field(1000)));
Expand Down Expand Up @@ -576,8 +574,7 @@ describe('int', () => {
});

describe('assertGt', () => {
// rangeCheckHelper: Expected 28948022309329048855892746252171976963363056481941560715954676764349967630336 to fit in 64 bits
it.skip('2>1=true', () => {
it('2>1=true', () => {
expect(() => {
Circuit.runAndCheck(() => {
const x = Circuit.witness(UInt64, () => new UInt64(Field(2)));
Expand Down Expand Up @@ -607,8 +604,7 @@ describe('int', () => {
}).toThrow();
});

// rangeCheckHelper: Expected 28948022309329048855892746252171976963363056481941560715954676764349967630336 to fit in 64 bits
it.skip('100000>1000=true', () => {
it('100000>1000=true', () => {
expect(() => {
Circuit.runAndCheck(() => {
const x = Circuit.witness(
Expand Down Expand Up @@ -819,8 +815,7 @@ describe('int', () => {
});

describe('lt', () => {
// rangeCheckHelper: Expected 28948022309329048855892746252171976963363056481941560715954676764349967630336 to fit in 64 bits
it.skip('1<2=true', () => {
it('1<2=true', () => {
expect(new UInt64(Field.one).lt(new UInt64(Field(2)))).toEqual(
Bool(true)
);
Expand All @@ -838,8 +833,7 @@ describe('int', () => {
);
});

// rangeCheckHelper: Expected 28948022309329048855892746252171976963363056481941560715954676764349967531337 to fit in 64 bits
it.skip('1000<100000=true', () => {
it('1000<100000=true', () => {
expect(new UInt64(Field(1000)).lt(new UInt64(Field(100000)))).toEqual(
Bool(true)
);
Expand Down Expand Up @@ -869,8 +863,7 @@ describe('int', () => {
);
});

// rangeCheckHelper: Expected 28948022309329048855892746252171976963363056481941560715954676764349967531337 to fit in 64 bits
it.skip('1000<=100000=true', () => {
it('1000<=100000=true', () => {
expect(
new UInt64(Field(1000)).lte(new UInt64(Field(100000)))
).toEqual(Bool(true));
Expand Down Expand Up @@ -920,8 +913,7 @@ describe('int', () => {
});

describe('gt', () => {
// rangeCheckHelper: Expected 28948022309329048855892746252171976963363056481941560715954676764349967630336 to fit in 64 bits
it.skip('2>1=true', () => {
it('2>1=true', () => {
expect(new UInt64(Field(2)).gt(new UInt64(Field.one))).toEqual(
Bool(true)
);
Expand All @@ -933,15 +925,13 @@ describe('int', () => {
);
});

// rangeCheckHelper: Expected 28948022309329048855892746252171976963363056481941560715954676764349967630336 to fit in 64 bits
it.skip('1>2=false', () => {
expect(new UInt64(Field.one).lt(new UInt64(Field(2)))).toEqual(
it('1>2=false', () => {
expect(new UInt64(Field.one).gt(new UInt64(Field(2)))).toEqual(
Bool(false)
);
});

// rangeCheckHelper: Expected 28948022309329048855892746252171976963363056481941560715954676764349967531337 to fit in 64 bits
it.skip('100000>1000=true', () => {
it('100000>1000=true', () => {
expect(new UInt64(Field(100000)).gt(new UInt64(Field(1000)))).toEqual(
Bool(true)
);
Expand All @@ -965,11 +955,10 @@ describe('int', () => {
}).toThrow();
});

// rangeCheckHelper: Expected 28948022309329048855892746252171976963363056481941560715954676764349967630336 to fit in 64 bits
it.skip('2>1=true', () => {
expect(
new UInt64(Field(2)).assertGt(new UInt64(Field.one))
).not.toThrow();
it('2>1=true', () => {
expect(() => {
new UInt64(Field(2)).assertGt(new UInt64(Field.one));
}).not.toThrow();
});

it('1000>100000=false', () => {
Expand All @@ -978,11 +967,10 @@ describe('int', () => {
}).toThrow();
});

// rangeCheckHelper: Expected 28948022309329048855892746252171976963363056481941560715954676764349967531337 to fit in 64 bits
it.skip('100000>1000=true', () => {
expect(
new UInt64(Field(100000)).assertGt(new UInt64(Field(1000)))
).not.toThrow();
it('100000>1000=true', () => {
expect(() => {
new UInt64(Field(100000)).assertGt(new UInt64(Field(1000)));
}).not.toThrow();
});

it('MAXINT>MAXINT=false', () => {
Expand Down Expand Up @@ -1273,8 +1261,7 @@ describe('int', () => {
});

describe('assertLt', () => {
// rangeCheckHelper: Expected 28948022309329048855892746252171976963363056481941560715954676764349967630336 to fit in 64 bits
it.skip('1<2=true', () => {
it('1<2=true', () => {
expect(() => {
Circuit.runAndCheck(() => {
const x = Circuit.witness(UInt32, () => new UInt32(Field.one));
Expand Down Expand Up @@ -1304,8 +1291,7 @@ describe('int', () => {
}).toThrow();
});

// rangeCheckHelper: Expected 28948022309329048855892746252171976963363056481941560715954676764349967531337 to fit in 64 bits
it.skip('1000<100000=true', () => {
it('1000<100000=true', () => {
expect(() => {
Circuit.runAndCheck(() => {
const x = Circuit.witness(UInt32, () => new UInt32(Field(1000)));
Expand Down Expand Up @@ -1401,8 +1387,7 @@ describe('int', () => {
});

describe('assertGt', () => {
// rangeCheckHelper: Expected 28948022309329048855892746252171976963363056481941560715954676764349967630336 to fit in 64 bits
it.skip('2>1=true', () => {
it('2>1=true', () => {
expect(() => {
Circuit.runAndCheck(() => {
const x = Circuit.witness(UInt32, () => new UInt32(Field(2)));
Expand Down Expand Up @@ -1432,8 +1417,7 @@ describe('int', () => {
}).toThrow();
});

// rangeCheckHelper: Expected 28948022309329048855892746252171976963363056481941560715954676764349967630336 to fit in 64 bits
it.skip('100000>1000=true', () => {
it('100000>1000=true', () => {
expect(() => {
Circuit.runAndCheck(() => {
const x = Circuit.witness(
Expand Down Expand Up @@ -1644,8 +1628,7 @@ describe('int', () => {
});

describe('lt', () => {
// rangeCheckHelper: Expected 28948022309329048855892746252171976963363056481941560715954676764349967630336 to fit in 64 bits
it.skip('1<2=true', () => {
it('1<2=true', () => {
expect(new UInt32(Field.one).lt(new UInt32(Field(2)))).toEqual(
Bool(true)
);
Expand All @@ -1663,8 +1646,7 @@ describe('int', () => {
);
});

// rangeCheckHelper: Expected 28948022309329048855892746252171976963363056481941560715954676764349967531337 to fit in 64 bits
it.skip('1000<100000=true', () => {
it('1000<100000=true', () => {
expect(new UInt32(Field(1000)).lt(new UInt32(Field(100000)))).toEqual(
Bool(true)
);
Expand Down Expand Up @@ -1694,8 +1676,7 @@ describe('int', () => {
);
});

// rangeCheckHelper: Expected 28948022309329048855892746252171976963363056481941560715954676764349967531337 to fit in 64 bits
it.skip('1000<=100000=true', () => {
it('1000<=100000=true', () => {
expect(
new UInt32(Field(1000)).lte(new UInt32(Field(100000)))
).toEqual(Bool(true));
Expand Down Expand Up @@ -1745,8 +1726,7 @@ describe('int', () => {
});

describe('gt', () => {
// rangeCheckHelper: Expected 28948022309329048855892746252171976963363056481941560715954676764349967630336 to fit in 64 bits
it.skip('2>1=true', () => {
it('2>1=true', () => {
expect(new UInt32(Field(2)).gt(new UInt32(Field.one))).toEqual(
Bool(true)
);
Expand All @@ -1758,15 +1738,13 @@ describe('int', () => {
);
});

// rangeCheckHelper: Expected 28948022309329048855892746252171976963363056481941560715954676764349967630336 to fit in 64 bits
it.skip('1>2=false', () => {
expect(new UInt32(Field.one).lt(new UInt32(Field(2)))).toEqual(
it('1>2=false', () => {
expect(new UInt32(Field.one).gt(new UInt32(Field(2)))).toEqual(
Bool(false)
);
});

// rangeCheckHelper: Expected 28948022309329048855892746252171976963363056481941560715954676764349967531337 to fit in 64 bits
it.skip('100000>1000=true', () => {
it('100000>1000=true', () => {
expect(new UInt32(Field(100000)).gt(new UInt32(Field(1000)))).toEqual(
Bool(true)
);
Expand All @@ -1783,36 +1761,35 @@ describe('int', () => {
});
});

describe.skip('assertGt', () => {
describe('assertGt', () => {
it('1>1=false', () => {
expect(
new UInt32(Field.one).assertGt(new UInt32(Field.one))
).toThrow();
expect(() => {
new UInt32(Field.one).assertGt(new UInt32(Field.one));
}).toThrow();
});

// rangeCheckHelper: Expected 28948022309329048855892746252171976963363056481941560715954676764349967630336 to fit in 64 bits
it('2>1=true', () => {
expect(
new UInt32(Field(2)).assertGt(new UInt32(Field.one))
).not.toThrow();
expect(() => {
new UInt32(Field(2)).assertGt(new UInt32(Field.one));
}).not.toThrow();
});

// assert_equal: 0 != 1
it('1000>100000=false', () => {
expect(
new UInt32(Field(1000)).assertGt(new UInt32(Field(100000)))
).toThrow();
expect(() => {
new UInt32(Field(1000)).assertGt(new UInt32(Field(100000)));
}).toThrow();
});

// rangeCheckHelper: Expected 28948022309329048855892746252171976963363056481941560715954676764349967531337 to fit in 64 bits
it('100000>1000=true', () => {
expect(
new UInt32(Field(100000)).assertGt(new UInt32(Field(1000)))
).not.toThrow();
expect(() => {
new UInt32(Field(100000)).assertGt(new UInt32(Field(1000)));
}).not.toThrow();
});

it('MAXINT>MAXINT=false', () => {
expect(UInt32.MAXINT().assertGt(UInt32.MAXINT())).toThrow();
expect(() => {
UInt32.MAXINT().assertGt(UInt32.MAXINT());
}).toThrow();
});
});

Expand Down
22 changes: 6 additions & 16 deletions src/lib/int.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,9 @@ class UInt64 extends CircuitValue implements Types.UInt64 {
}

lte(y: UInt64) {
let xMinusY = this.value.sub(y.value).seal();
let xMinusYFits = xMinusY.rangeCheckHelper(UInt64.NUM_BITS).equals(xMinusY);
let yMinusXFits = xMinusY
.rangeCheckHelper(UInt64.NUM_BITS)
.equals(xMinusY.neg());
xMinusYFits.or(yMinusXFits).assertEquals(true);
// x <= y if y - x fits in 64 bits
return yMinusXFits;
this.value.rangeCheckHelper(UInt64.NUM_BITS).assertEquals(this.value);
y.value.rangeCheckHelper(UInt64.NUM_BITS).assertEquals(y.value);
return this.value.lte(y.value);
}

assertLte(y: UInt64) {
Expand Down Expand Up @@ -302,14 +297,9 @@ class UInt32 extends CircuitValue implements Types.UInt32 {
}

lte(y: UInt32) {
let xMinusY = this.value.sub(y.value).seal();
let xMinusYFits = xMinusY.rangeCheckHelper(UInt32.NUM_BITS).equals(xMinusY);
let yMinusXFits = xMinusY
.rangeCheckHelper(UInt32.NUM_BITS)
.equals(xMinusY.neg());
xMinusYFits.or(yMinusXFits).assertEquals(true);
// x <= y if y - x fits in 32 bits
return yMinusXFits;
this.value.rangeCheckHelper(UInt32.NUM_BITS).assertEquals(this.value);
y.value.rangeCheckHelper(UInt32.NUM_BITS).assertEquals(y.value);
return this.value.lte(y.value);
}

assertLte(y: UInt32) {
Expand Down