Skip to content

Commit

Permalink
Merge pull request #1666 from o1-labs/feature/indexed-merkle-map
Browse files Browse the repository at this point in the history
Indexed Merkle tree
  • Loading branch information
mitschabaude authored Jun 4, 2024
2 parents b0c2439 + d59e84b commit 8758daa
Show file tree
Hide file tree
Showing 15 changed files with 1,062 additions and 33 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased](https://github.com/o1-labs/o1js/compare/54d6545bf...HEAD)

### Added

- `Experimental.IndexedMerkleMap`, a better primitive for Merkleized storage which uses 4-8x fewer constraints than `MerkleMap` https://github.com/o1-labs/o1js/pull/1666
- In contrast to `MerkleTree` and `MerkleMap`, `IndexedMerkleMap` has a high-level API that can be used in provable code.

### Deprecated

- `Int64.isPositive()` and `Int64.mod()` deprecated because they behave incorrectly on `-0` https://github.com/o1-labs/o1js/pull/1660
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export { Gadgets } from './lib/provable/gadgets/gadgets.js';
export { Types } from './bindings/mina-transaction/types.js';

export { MerkleList, MerkleListIterator } from './lib/provable/merkle-list.js';
import { IndexedMerkleMap } from './lib/provable/merkle-tree-indexed.js';
export { Option } from './lib/provable/option.js';

export * as Mina from './lib/mina/mina.js';
Expand Down Expand Up @@ -133,6 +134,7 @@ export { Experimental };

const Experimental_ = {
memoizeWitness,
IndexedMerkleMap,
};

/**
Expand All @@ -142,6 +144,9 @@ const Experimental_ = {
namespace Experimental {
export let memoizeWitness = Experimental_.memoizeWitness;

// indexed merkle map
export let IndexedMerkleMap = Experimental_.IndexedMerkleMap;

// offchain state
export let OffchainState = OffchainState_.OffchainState;

Expand Down
1 change: 1 addition & 0 deletions src/lib/provable/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ class Bytes {
static _size?: number;
static _provable?: ProvablePureExtended<
Bytes,
{ bytes: { value: bigint }[] },
{ bytes: { value: string }[] }
>;

Expand Down
21 changes: 14 additions & 7 deletions src/lib/provable/core/provable-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export {
inCompileMode,
gatesFromJson,
printGates,
summarizeGates,
MlConstraintSystem,
};

Expand Down Expand Up @@ -167,13 +168,7 @@ function constraintSystemToJS(cs: MlConstraintSystem) {
printGates(gates);
},
summary() {
let gateTypes: Partial<Record<GateType | 'Total rows', number>> = {};
gateTypes['Total rows'] = rows;
for (let gate of gates) {
gateTypes[gate.type] ??= 0;
gateTypes[gate.type]!++;
}
return gateTypes;
return summarizeGates(gates);
},
};
}
Expand All @@ -188,6 +183,18 @@ function gatesFromJson(cs: { gates: JsonGate[]; public_input_size: number }) {
return { publicInputSize: cs.public_input_size, gates };
}

// collect a summary of the constraint system

function summarizeGates(gates: Gate[]) {
let gateTypes: Partial<Record<GateType | 'Total rows', number>> = {};
gateTypes['Total rows'] = gates.length;
for (let gate of gates) {
gateTypes[gate.type] ??= 0;
gateTypes[gate.type]!++;
}
return gateTypes;
}

// print a constraint system

function printGates(gates: Gate[]) {
Expand Down
1 change: 1 addition & 0 deletions src/lib/provable/crypto/foreign-curve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ class ForeignCurve {
static _Scalar?: typeof AlmostForeignField;
static _provable?: ProvablePureExtended<
ForeignCurve,
{ x: bigint; y: bigint },
{ x: string; y: string }
>;

Expand Down
1 change: 1 addition & 0 deletions src/lib/provable/crypto/foreign-ecdsa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ class EcdsaSignature {
static _Curve?: typeof ForeignCurve;
static _provable?: ProvablePureExtended<
EcdsaSignature,
{ r: bigint; s: bigint },
{ r: string; s: string }
>;

Expand Down
Loading

0 comments on commit 8758daa

Please sign in to comment.