Skip to content

Commit

Permalink
Merge pull request #1201 from o1-labs/feature/remove-rot-excess-rc
Browse files Browse the repository at this point in the history
Remove redundant range check in rotation gadget
  • Loading branch information
mitschabaude committed Nov 17, 2023
2 parents aec1e89 + 245f847 commit 6e30ce7
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 11 deletions.
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,
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

0 comments on commit 6e30ce7

Please sign in to comment.