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

Add NOT gadget to Fields #1010

Closed
wants to merge 8 commits 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
37 changes: 37 additions & 0 deletions src/lib/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,43 @@ class Field {
return new Field(z);
}

/**
* Bitwise NOT gate on {@link Field} elements. Equivalent to the [bitwise
* NOT `~` operator in JavaScript](https://developer.mozilla.org/en-US/docs/
* Web/JavaScript/Reference/Operators/Bitwise_NOT).
* A NOT gate works by returning `1` in each bit position if the
* corresponding bit of the operand is `0`, and returning `0` if the
* corresponding bit of the operand is `1`.
*
* The `length` parameter lets you define how many bits to compare. It
* defaults to the size of the field in bits ({@link Fp.sizeInBits}).
*
* **Note:** Specifying a larger `length` parameter adds additional * * *
* constraints.
*
* @example
* ```ts
* let a = Field(5); // ... 000101
* let b = a.not(); // ... 111010
*
* b.assertEquals(-6);
* ```
*/
not(length: number = 32) {
if (this.isConstant()) {
let max = 1n << BigInt(length);
let thisBigint = this.toBigInt();

if (thisBigint >= max) {
throw Error(`${thisBigint} need to fit into ${length} bits.`);
}

return new Field(Fp.not(thisBigint));
} else {
return new Field(Snarky.field.not(this.value, length));
}
}

/**
* @deprecated use `x.equals(0)` which is equivalent
*/
Expand Down
15 changes: 15 additions & 0 deletions src/lib/field.unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ test(Random.field, Random.int(-5, 5), (x, k) => {
deepEqual(Field(x + BigInt(k) * Field.ORDER), Field(x));
});

// NOT with some common and odd lengths
[2, 4, 8, 16, 32, 64, 3, 5, 10, 15].forEach((length) => {
test(Random.field, Random.field, (x_, _, assert) => {
let modulus = 1n << BigInt(length);
let x = x_ % modulus;
let r1 = Fp.not(BigInt(x));

Provable.runAndCheck(() => {
let x_witness = Provable.witness(Field, () => Field(x));
let r2 = x_witness.not(length);
Provable.asProver(() => assert(r1 === r2.toBigInt()));
});
});
});

// special generator
let SmallField = Random.reject(
Random.field,
Expand Down
41 changes: 41 additions & 0 deletions src/lib/int.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,26 @@ class UInt64 extends CircuitValue {
return new UInt64(z);
}

/**
* Bitwise NOT gate on {@link UInt64} elements. Equivalent to the [bitwise
* NOT `~` operator in JavaScript](https://developer.mozilla.org/en-US/docs/
* Web/JavaScript/Reference/Operators/Bitwise_NOT).
* A NOT gate works by returning `1` in each bit position if the
* corresponding bit of the operand is `0`, and returning `0` if the
* corresponding bit of the operand is `1`.
*
* @example
* ```ts
* let a = UInt64(5); // ... 000101
* let b = a.not(); // ... 111010
*
* b.assertEquals(-6);
* ```
*/
not() {
return new UInt64(this.value.not(UInt64.NUM_BITS));
}

/**
* @deprecated Use {@link lessThanOrEqual} instead.
*
Expand Down Expand Up @@ -539,6 +559,27 @@ class UInt32 extends CircuitValue {
z.rangeCheckHelper(UInt32.NUM_BITS).assertEquals(z);
return new UInt32(z);
}

/**
* Bitwise NOT gate on {@link UInt32} elements. Equivalent to the [bitwise
* NOT `~` operator in JavaScript](https://developer.mozilla.org/en-US/docs/
* Web/JavaScript/Reference/Operators/Bitwise_NOT).
* A NOT gate works by returning `1` in each bit position if the
* corresponding bit of the operand is `0`, and returning `0` if the
* corresponding bit of the operand is `1`.
*
* @example
* ```ts
* let a = UInt32(5); // ... 000101
* let b = a.not(); // ... 111010
*
* b.assertEquals(-6);
* ```
*/
not() {
return new UInt32(this.value.not(UInt32.NUM_BITS));
}

/**
* @deprecated Use {@link lessThanOrEqual} instead.
*
Expand Down
2 changes: 2 additions & 0 deletions src/snarky.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ declare const Snarky: {
constant: MlOption<FieldConst>,
terms: MlList<MlTuple<FieldConst, number>>
];

not(left: FieldVar, length: number): FieldVar;
};

bool: {
Expand Down