From ef119aadfecaca79bfecca6916aca386132d37ac Mon Sep 17 00:00:00 2001 From: Martin Minkov Date: Mon, 27 Jun 2022 10:10:39 -0700 Subject: [PATCH] Fix lte for UInt64 and UInt32 and tests --- src/lib/int.test.ts | 115 ++++++++++++++++++-------------------------- src/lib/int.ts | 22 +++------ 2 files changed, 52 insertions(+), 85 deletions(-) diff --git a/src/lib/int.test.ts b/src/lib/int.test.ts index d32944655c..59efb3337d 100644 --- a/src/lib/int.test.ts +++ b/src/lib/int.test.ts @@ -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)); @@ -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))); @@ -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))); @@ -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( @@ -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) ); @@ -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) ); @@ -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)); @@ -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) ); @@ -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) ); @@ -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', () => { @@ -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', () => { @@ -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)); @@ -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))); @@ -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))); @@ -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( @@ -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) ); @@ -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) ); @@ -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)); @@ -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) ); @@ -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) ); @@ -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(); }); }); diff --git a/src/lib/int.ts b/src/lib/int.ts index 57b77e0342..a33f27fe29 100644 --- a/src/lib/int.ts +++ b/src/lib/int.ts @@ -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) { @@ -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) {