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

Remove redundant range check in rotation gadget #1201

Merged
merged 9 commits into from
Nov 17, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Changed

- Expose raw provable methods of a `ZkProgram` on `zkProgram.rawMethods` https://github.com/o1-labs/o1js/pull/1241
- Reduce number of constraints needed by `rotate()`, `leftShift()` and, `rightShift()` gadgets https://github.com/o1-labs/o1js/pull/1201

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion src/bindings
6 changes: 4 additions & 2 deletions src/lib/gadgets/bitwise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,10 @@ function rot(
);
// Compute next row
rangeCheck64(shifted);
// Compute following row
rangeCheck64(excess);
// note: range-checking `shifted` and `field` is enough.
// * excess < 2^rot follows from the bound check and the rotation equation in the gate
// * rotated < 2^64 follows from rotated = excess + shifted (because shifted has to be a multiple of 2^rot)
// for a proof, see https://github.com/o1-labs/o1js/pull/1201
return [rotated, excess, shifted];
}

Expand Down
4 changes: 2 additions & 2 deletions src/lib/gadgets/bitwise.unit-test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ZkProgram } from '../proof_system.js';
import {
equivalent,
equivalentProvable as equivalent,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated fix: Unit tests need equivalentProvable which tests implementations in a circuit

equivalentAsync,
field,
fieldWithRng,
Expand Down Expand Up @@ -228,7 +228,7 @@ constraintSystem(
ifNotAllConstant(contains(xorChain(64)))
);

let rotChain: GateType[] = ['Rot64', 'RangeCheck0', 'RangeCheck0'];
let rotChain: GateType[] = ['Rot64', 'RangeCheck0'];
let isJustRotate = ifNotAllConstant(
and(contains(rotChain), withoutGenerics(equals(rotChain)))
);
Expand Down
1 change: 1 addition & 0 deletions tests/vk-regression/plain-constraint-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const GroupCS = constraintSystem('Group Primitive', {
const BitwiseCS = constraintSystem('Bitwise Primitive', {
rot() {
let a = Provable.witness(Field, () => new Field(12));
Gadgets.rangeCheck64(a); // `rotate()` doesn't do this
Gadgets.rotate(a, 2, 'left');
Gadgets.rotate(a, 2, 'right');
Gadgets.rotate(a, 4, 'left');
Expand Down
12 changes: 6 additions & 6 deletions tests/vk-regression/vk-regression.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@
"digest": "Bitwise Primitive",
"methods": {
"rot": {
"rows": 13,
"digest": "2c0dadbba96fd7ddb9adb7d643425ce3"
"rows": 10,
"digest": "c38703de755b10edf77bf24269089274"
},
"xor": {
"rows": 15,
Expand All @@ -185,12 +185,12 @@
"digest": "b12ad7e8a3fd28b765e059357dbe9e44"
},
"leftShift": {
"rows": 7,
"digest": "66de39ad3dd5807f760341ec85a6cc41"
"rows": 5,
"digest": "451f550bf73fecf53c9be82367572cb8"
},
"rightShift": {
"rows": 7,
"digest": "a32264f2d4c3092f30d600fa9506385b"
"rows": 5,
"digest": "d0793d4a326d480eaa015902dc34bc39"
},
"and": {
"rows": 19,
Expand Down
Loading