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, UInt32, and tests #307

Merged
merged 1 commit into from
Jul 27, 2022
Merged
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
116 changes: 46 additions & 70 deletions src/lib/int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ describe('int', () => {
expect(Int64.zero.sub(Int64.one).toString()).toEqual('-1');
});

// ERROR: Expected: -18446744073709552000 - Received: "-18446744073709551615"
it('(0-MAXINT) subs to -MAXINT', () => {
expect(Int64.zero.sub(UInt64.MAXINT()).toString()).toEqual(
'-' + UInt64.MAXINT().toString()
Expand Down Expand Up @@ -449,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 @@ -480,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 @@ -577,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 @@ -608,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 @@ -820,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 @@ -839,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 @@ -870,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 @@ -921,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 @@ -934,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 @@ -966,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 @@ -979,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 @@ -1274,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 @@ -1305,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 @@ -1402,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 @@ -1433,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 @@ -1645,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 @@ -1664,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 @@ -1695,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 @@ -1746,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 @@ -1759,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 @@ -1784,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
46 changes: 29 additions & 17 deletions src/lib/int.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Circuit, Field } from '../snarky';
import { Circuit, Field, Bool } from '../snarky';
import { CircuitValue, prop } from './circuit_value';
import { Types } from '../snarky/types';

Expand Down Expand Up @@ -140,14 +140,20 @@ class UInt64 extends CircuitValue {
}

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;
if (this.value.isConstant() && y.value.isConstant()) {
return Bool(this.value.toBigInt() <= y.value.toBigInt());
} else {
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;
}
}

assertLte(y: UInt64) {
Expand Down Expand Up @@ -291,14 +297,20 @@ class UInt32 extends CircuitValue {
}

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;
if (this.value.isConstant() && y.value.isConstant()) {
return Bool(this.value.toBigInt() <= y.value.toBigInt());
} else {
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 64 bits
return yMinusXFits;
}
}

assertLte(y: UInt32) {
Expand Down