Skip to content

Commit f2423e3

Browse files
committed
Merge branch 'main' into dw/reintro-commit-af6f5f3
2 parents 85ac923 + 7f5057d commit f2423e3

File tree

3 files changed

+112
-2
lines changed

3 files changed

+112
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
2626
to include the latest changes reg. the move of the Rust codebase to the
2727
repository proof-systems.
2828

29+
### Added
30+
31+
- Added bitwise operation methods (xor, not, and, or) to `UInt8` class. https://github.com/o1-labs/o1js/pull/2144$0
32+
2933
## [2.4.0](https://github.com/o1-labs/o1js/compare/fb625f...6ff7f8470a) - 2025-04-01
3034

3135
### Added

src/lib/provable/int.ts

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ class UInt64 extends CircuitValue {
286286
* ```ts
287287
* // NOTing 4 bits with the unchecked version
288288
* let a = UInt64.from(0b0101);
289-
* let b = a.not(false);
289+
* let b = a.not();
290290
*
291291
* console.log(b.toBigInt().toString(2));
292292
* // 1111111111111111111111111111111111111111111111111111111111111010
@@ -928,7 +928,7 @@ class UInt32 extends CircuitValue {
928928
* let a = UInt32.from(3); // ... 000011
929929
* let b = UInt32.from(5); // ... 000101
930930
*
931-
* let c = a.and(b, 2); // ... 000001
931+
* let c = a.and(b); // ... 000001
932932
* c.assertEquals(1);
933933
* ```
934934
*/
@@ -1670,6 +1670,106 @@ class UInt8 extends Struct({
16701670
return { quotient, remainder };
16711671
}
16721672

1673+
/**
1674+
* Bitwise XOR gadget on {@link Field} elements. Equivalent to the [bitwise XOR `^` operator in JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_XOR).
1675+
* A XOR gate works by comparing two bits and returning `1` if two bits differ, and `0` if two bits are equal.
1676+
*
1677+
* This gadget builds a chain of XOR gates recursively.
1678+
*
1679+
* You can find more details about the implementation in the [Mina book](https://o1-labs.github.io/proof-systems/specs/kimchi.html?highlight=gates#xor-1)
1680+
*
1681+
* @param x {@link UInt8} element to XOR.
1682+
*
1683+
* @example
1684+
* ```ts
1685+
* let a = UInt8.from(0b0101);
1686+
* let b = UInt8.from(0b0011);
1687+
*
1688+
* let c = a.xor(b);
1689+
* c.assertEquals(0b0110);
1690+
* ```
1691+
*/
1692+
xor(x: UInt8) {
1693+
return new UInt8(Bitwise.xor(this.value, x.value, UInt8.NUM_BITS).value);
1694+
}
1695+
1696+
/**
1697+
* Bitwise NOT gate on {@link Field} elements. Similar to the [bitwise
1698+
* NOT `~` operator in JavaScript](https://developer.mozilla.org/en-US/docs/
1699+
* Web/JavaScript/Reference/Operators/Bitwise_NOT).
1700+
*
1701+
* **Note:** The NOT gate operates over 8 bit for UInt8 types.
1702+
*
1703+
* A NOT gate works by returning `1` in each bit position if the
1704+
* corresponding bit of the operand is `0`, and returning `0` if the
1705+
* corresponding bit of the operand is `1`.
1706+
*
1707+
* NOT is implemented as a subtraction of the input from the all one bitmask
1708+
*
1709+
* You can find more details about the implementation in the [Mina book](https://o1-labs.github.io/proof-systems/specs/kimchi.html?highlight=gates#not)
1710+
*
1711+
* @example
1712+
* ```ts
1713+
* // NOTing 4 bits with the unchecked version
1714+
* let a = UInt8.from(0b0101);
1715+
* let b = a.not();
1716+
*
1717+
* console.log(b.toBigInt().toString(2));
1718+
* // 11111010
1719+
*
1720+
* ```
1721+
*
1722+
*/
1723+
not() {
1724+
return new UInt8(Bitwise.not(this.value, UInt8.NUM_BITS, false).value);
1725+
}
1726+
1727+
/**
1728+
* Bitwise AND gadget on {@link UInt8} elements. Equivalent to the [bitwise AND `&` operator in JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_AND).
1729+
* The AND gate works by comparing two bits and returning `1` if both bits are `1`, and `0` otherwise.
1730+
*
1731+
* It can be checked by a double generic gate that verifies the following relationship between the values below.
1732+
*
1733+
* The generic gate verifies:\
1734+
* `a + b = sum` and the conjunction equation `2 * and = sum - xor`\
1735+
* Where:\
1736+
* `a + b = sum`\
1737+
* `a ^ b = xor`\
1738+
* `a & b = and`
1739+
*
1740+
* You can find more details about the implementation in the [Mina book](https://o1-labs.github.io/proof-systems/specs/kimchi.html?highlight=gates#and)
1741+
*
1742+
*
1743+
* @example
1744+
* ```typescript
1745+
* let a = UInt8.from(3); // ... 000011
1746+
* let b = UInt8.from(5); // ... 000101
1747+
*
1748+
* let c = a.and(b); // ... 000001
1749+
* c.assertEquals(1);
1750+
* ```
1751+
*/
1752+
and(x: UInt8) {
1753+
return new UInt8(Bitwise.and(this.value, x.value, UInt8.NUM_BITS).value);
1754+
}
1755+
1756+
/**
1757+
* Bitwise OR gadget on {@link UInt8} elements. Equivalent to the [bitwise OR `|` operator in JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_OR).
1758+
* The OR gate works by comparing two bits and returning `1` if at least one bit is `1`, and `0` otherwise.
1759+
*
1760+
* @example
1761+
* ```typescript
1762+
* let a = UInt8.from(3); // ... 000011
1763+
* let b = UInt8.from(5); // ... 000101
1764+
*
1765+
* let c = a.or(b); // ... 000111
1766+
* c.assertEquals(7);
1767+
* ```
1768+
*/
1769+
or(x: UInt8) {
1770+
return new UInt8(Bitwise.or(this.value, x.value, UInt8.NUM_BITS).value);
1771+
}
1772+
16731773
/**
16741774
* Check if this {@link UInt8} is less than or equal to another {@link UInt8} value.
16751775
* Returns a {@link Bool}.

src/lib/provable/merkle-map.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ class MerkleMap {
1212

1313
/**
1414
* Creates a new, empty Merkle Map.
15+
*
16+
* A Merkle Map is a data structure that allows for efficient storage and
17+
* retrieval of key-value pairs. The values are stored in a Merkle tree,
18+
* and the keys are formed by using the first 254 bits of the key as an index.
19+
* The inner Merkle tree has a height of 256.
20+
*
1521
* @returns A new MerkleMap
1622
* @example
1723
* ```ts

0 commit comments

Comments
 (0)