Skip to content

Commit 3ffc0c7

Browse files
committed
Merge branch 'develop' into feature/proven-settlemnet
# Conflicts: # packages/common/src/utils.ts # packages/protocol/src/prover/block/BlockProver.ts # packages/protocol/src/prover/statetransition/StateTransitionProvable.ts # packages/protocol/src/prover/statetransition/StateTransitionProver.ts # packages/sequencer/src/protocol/production/tasks/StateTransitionTask.ts
2 parents 4a3e15b + 04124a9 commit 3ffc0c7

21 files changed

+424
-207
lines changed

packages/common/src/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ type NonMethodKeys<Type> = {
167167
}[keyof Type];
168168
export type NonMethods<Type> = Pick<Type, NonMethodKeys<Type>>;
169169

170+
export const MAX_FIELD = Field(Field.ORDER - 1n);
171+
170172
/**
171173
* Returns a boolean indicating whether a given class is a subclass of another class,
172174
* indicated by the name parameter.

packages/library/src/hooks/RuntimeFeeAnalyzerService.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export class RuntimeFeeAnalyzerService extends ConfigurableModule<RuntimeFeeAnal
8181
});
8282

8383
container.resolve(RuntimeMethodExecutionContext).clear();
84-
84+
let methodCounter = 0;
8585
const [values, indexes] =
8686
await this.runtime.zkProgrammable.zkProgram.reduce<
8787
Promise<[FeeTreeValues, FeeIndexes]>
@@ -93,7 +93,7 @@ export class RuntimeFeeAnalyzerService extends ConfigurableModule<RuntimeFeeAnal
9393
[FeeTreeValues, FeeIndexes]
9494
>(
9595
// eslint-disable-next-line @typescript-eslint/no-shadow
96-
([values, indexes], combinedMethodName, index) => {
96+
([values, indexes], combinedMethodName) => {
9797
const { rows } = analyzedMethods[combinedMethodName];
9898
// const rows = 1000;
9999
const [moduleName, methodName] = combinedMethodName.split(".");
@@ -128,7 +128,8 @@ export class RuntimeFeeAnalyzerService extends ConfigurableModule<RuntimeFeeAnal
128128
},
129129
{
130130
...indexes,
131-
[methodId.toString()]: BigInt(index),
131+
// eslint-disable-next-line no-plusplus
132+
[methodId.toString()]: BigInt(methodCounter++),
132133
},
133134
];
134135
},

packages/protocol/src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ export * from "./prover/block/accummulators/BlockHashMerkleTree";
2424
export * from "./prover/block/services/RuntimeVerificationKeyRootService";
2525
export * from "./prover/statetransition/StateTransitionProver";
2626
export * from "./prover/statetransition/StateTransitionProvable";
27-
export * from "./prover/statetransition/StateTransitionWitnessProvider";
28-
export * from "./prover/statetransition/StateTransitionWitnessProviderReference";
2927
export * from "./protocol/Protocol";
3028
export * from "./protocol/ProtocolModule";
3129
export * from "./protocol/ProtocolEnvironment";

packages/protocol/src/model/StateTransitionProvableBatch.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { Bool, Provable, Struct } from "o1js";
2-
import { range } from "@proto-kit/common";
2+
import {
3+
InMemoryMerkleTreeStorage,
4+
range,
5+
RollupMerkleTree,
6+
RollupMerkleTreeWitness,
7+
} from "@proto-kit/common";
38

49
import { constants } from "../Constants";
510

@@ -60,16 +65,22 @@ export class StateTransitionProvableBatch extends Struct({
6065
ProvableStateTransitionType,
6166
constants.stateTransitionProverBatchSize
6267
),
68+
69+
merkleWitnesses: Provable.Array(
70+
RollupMerkleTreeWitness,
71+
constants.stateTransitionProverBatchSize
72+
),
6373
}) {
6474
public static fromMappings(
6575
transitions: {
6676
transition: ProvableStateTransition;
6777
type: ProvableStateTransitionType;
68-
}[]
78+
}[],
79+
merkleWitnesses: RollupMerkleTreeWitness[]
6980
): StateTransitionProvableBatch {
7081
const batch = transitions.map((entry) => entry.transition);
7182
const transitionTypes = transitions.map((entry) => entry.type);
72-
83+
const witnesses = merkleWitnesses.slice();
7384
// Check that order is correct
7485
let normalSTsStarted = false;
7586
transitionTypes.forEach((x) => {
@@ -84,16 +95,23 @@ export class StateTransitionProvableBatch extends Struct({
8495
while (batch.length < constants.stateTransitionProverBatchSize) {
8596
batch.push(ProvableStateTransition.dummy());
8697
transitionTypes.push(ProvableStateTransitionType.normal);
98+
witnesses.push(
99+
new RollupMerkleTree(new InMemoryMerkleTreeStorage()).getWitness(
100+
BigInt(0)
101+
)
102+
);
87103
}
88104
return new StateTransitionProvableBatch({
89105
batch,
90106
transitionTypes,
107+
merkleWitnesses: witnesses,
91108
});
92109
}
93110

94111
public static fromTransitions(
95112
transitions: ProvableStateTransition[],
96-
protocolTransitions: ProvableStateTransition[]
113+
protocolTransitions: ProvableStateTransition[],
114+
merkleWitnesses: RollupMerkleTreeWitness[]
97115
): StateTransitionProvableBatch {
98116
const array = transitions.slice().concat(protocolTransitions);
99117

@@ -113,12 +131,14 @@ export class StateTransitionProvableBatch extends Struct({
113131
return new StateTransitionProvableBatch({
114132
batch: array,
115133
transitionTypes,
134+
merkleWitnesses,
116135
});
117136
}
118137

119138
private constructor(object: {
120139
batch: ProvableStateTransition[];
121140
transitionTypes: ProvableStateTransitionType[];
141+
merkleWitnesses: RollupMerkleTreeWitness[];
122142
}) {
123143
super(object);
124144
}

packages/protocol/src/prover/block/BlockProvable.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export class BlockProverPublicInput extends Struct({
2424
blockHashRoot: Field,
2525
eternalTransactionsHash: Field,
2626
incomingMessagesHash: Field,
27+
blockNumber: Field,
2728
}) {}
2829

2930
export class BlockProverPublicOutput extends Struct({
@@ -36,15 +37,10 @@ export class BlockProverPublicOutput extends Struct({
3637
closed: Bool,
3738
blockNumber: Field,
3839
}) {
39-
public equals(
40-
input: BlockProverPublicInput,
41-
closed: Bool,
42-
blockNumber: Field
43-
): Bool {
40+
public equals(input: BlockProverPublicInput, closed: Bool): Bool {
4441
const output2 = BlockProverPublicOutput.toFields({
4542
...input,
4643
closed,
47-
blockNumber,
4844
});
4945
const output1 = BlockProverPublicOutput.toFields(this);
5046
return output1

packages/protocol/src/prover/block/BlockProver.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
CompilableModule,
1616
CompileArtifact,
1717
CompileRegistry,
18+
MAX_FIELD,
1819
PlainZkProgram,
1920
provableMethod,
2021
WithZkProgrammable,
@@ -128,10 +129,6 @@ export interface BlockProverState {
128129
incomingMessagesHash: Field;
129130
}
130131

131-
function maxField() {
132-
return Field(Field.ORDER - 1n);
133-
}
134-
135132
export type BlockProof = Proof<BlockProverPublicInput, BlockProverPublicOutput>;
136133
export type RuntimeProof = Proof<void, MethodPublicOutput>;
137134

@@ -422,6 +419,11 @@ export class BlockProverProgrammable extends ZkProgrammable<
422419
"ExecutionData Networkstate doesn't equal public input hash"
423420
);
424421

422+
publicInput.blockNumber.assertEquals(
423+
MAX_FIELD,
424+
"blockNumber has to be MAX for transaction proofs"
425+
);
426+
425427
// Verify the [methodId, vk] tuple against the baked-in vk tree root
426428
const { verificationKey, witness: verificationKeyTreeWitness } =
427429
verificationKeyWitness;
@@ -451,7 +453,7 @@ export class BlockProverProgrammable extends ZkProgrammable<
451453

452454
return new BlockProverPublicOutput({
453455
...stateTo,
454-
blockNumber: maxField(),
456+
blockNumber: publicInput.blockNumber,
455457
closed: Bool(false),
456458
});
457459
}
@@ -534,14 +536,14 @@ export class BlockProverProgrammable extends ZkProgrammable<
534536
// stateTransitionProof.verifyIf(Bool(false));
535537
// stateTransitionProof.verifyIf(stsEmitted);
536538

537-
// Verify Transaction proof if it has at least 1 tx
539+
// Verify Transaction proof if it has at least 1 tx - i.e. the
540+
// input and output doesn't match fully
538541
// We have to compare the whole input and output because we can make no
539542
// assumptions about the values, since it can be an arbitrary dummy-proof
540543
const txProofOutput = transactionProof.publicOutput;
541544
const isEmptyTransition = txProofOutput.equals(
542545
transactionProof.publicInput,
543-
txProofOutput.closed,
544-
txProofOutput.blockNumber
546+
txProofOutput.closed
545547
);
546548
Provable.log("VerifyIf 2", isEmptyTransition.not());
547549
transactionProof.verifyIf(isEmptyTransition.not());
@@ -626,6 +628,8 @@ export class BlockProverProgrammable extends ZkProgrammable<
626628
// Calculate the new block index
627629
const blockIndex = blockWitness.calculateIndex();
628630

631+
blockIndex.assertEquals(publicInput.blockNumber);
632+
629633
blockWitness
630634
.calculateRoot(Field(0))
631635
.assertEquals(
@@ -643,7 +647,7 @@ export class BlockProverProgrammable extends ZkProgrammable<
643647

644648
return new BlockProverPublicOutput({
645649
...state,
646-
blockNumber: blockIndex,
650+
blockNumber: blockIndex.add(1),
647651
closed: Bool(true),
648652
});
649653
}
@@ -750,19 +754,25 @@ export class BlockProverProgrammable extends ZkProgrammable<
750754
// assert proof1.height == proof2.height
751755
// }
752756

753-
const proof1Height = proof1.publicOutput.blockNumber;
754757
const proof1Closed = proof1.publicOutput.closed;
755-
const proof2Height = proof2.publicOutput.blockNumber;
756758
const proof2Closed = proof2.publicOutput.closed;
757759

758-
const isValidTransactionMerge = proof1Height
759-
.equals(maxField())
760-
.and(proof2Height.equals(proof1Height))
760+
const blockNumberProgressionValid = publicInput.blockNumber
761+
.equals(proof1.publicInput.blockNumber)
762+
.and(
763+
proof1.publicOutput.blockNumber.equals(proof2.publicInput.blockNumber)
764+
);
765+
766+
// For tx proofs, we check that the progression starts and end with MAX
767+
// in addition to that both proofs are non-closed
768+
const isValidTransactionMerge = publicInput.blockNumber
769+
.equals(MAX_FIELD)
770+
.and(blockNumberProgressionValid)
761771
.and(proof1Closed.or(proof2Closed).not());
762772

763773
const isValidClosedMerge = proof1Closed
764774
.and(proof2Closed)
765-
.and(proof1Height.add(1).equals(proof2Height));
775+
.and(blockNumberProgressionValid);
766776

767777
isValidTransactionMerge
768778
.or(isValidClosedMerge)
@@ -775,9 +785,8 @@ export class BlockProverProgrammable extends ZkProgrammable<
775785
blockHashRoot: proof2.publicOutput.blockHashRoot,
776786
eternalTransactionsHash: proof2.publicOutput.eternalTransactionsHash,
777787
incomingMessagesHash: proof2.publicOutput.incomingMessagesHash,
778-
// Provable.if(isValidClosedMerge, Bool(true), Bool(false));
779788
closed: isValidClosedMerge,
780-
blockNumber: proof2Height,
789+
blockNumber: proof2.publicOutput.blockNumber,
781790
});
782791
}
783792

packages/protocol/src/prover/statetransition/StateTransitionProvable.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import { WithZkProgrammable, CompilableModule } from "@proto-kit/common";
33

44
import { StateTransitionProvableBatch } from "../../model/StateTransitionProvableBatch";
55

6-
import { StateTransitionWitnessProviderReference } from "./StateTransitionWitnessProviderReference";
7-
86
export class StateTransitionProverPublicInput extends Struct({
97
stateTransitionsHash: Field,
108
protocolTransitionsHash: Field,
@@ -30,8 +28,6 @@ export interface StateTransitionProvable
3028
StateTransitionProverPublicOutput
3129
>,
3230
CompilableModule {
33-
witnessProviderReference: StateTransitionWitnessProviderReference;
34-
3531
runBatch: (
3632
publicInput: StateTransitionProverPublicInput,
3733
batch: StateTransitionProvableBatch

packages/protocol/src/prover/statetransition/StateTransitionProver.ts

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ import {
3030
StateTransitionProverPublicInput,
3131
StateTransitionProverPublicOutput,
3232
} from "./StateTransitionProvable";
33-
import { StateTransitionWitnessProvider } from "./StateTransitionWitnessProvider";
34-
import { StateTransitionWitnessProviderReference } from "./StateTransitionWitnessProviderReference";
3533

3634
const errors = {
3735
propertyNotMatching: (property: string, step: string) =>
@@ -78,8 +76,7 @@ export class StateTransitionProverProgrammable extends ZkProgrammable<
7876
StateTransitionProverPublicOutput
7977
> {
8078
public constructor(
81-
private readonly stateTransitionProver: StateTransitionProver,
82-
public readonly witnessProviderReference: StateTransitionWitnessProviderReference
79+
private readonly stateTransitionProver: StateTransitionProver
8380
) {
8481
super();
8582
}
@@ -147,14 +144,6 @@ export class StateTransitionProverProgrammable extends ZkProgrammable<
147144
];
148145
}
149146

150-
private get witnessProvider(): StateTransitionWitnessProvider {
151-
const provider = this.witnessProviderReference.getWitnessProvider();
152-
if (provider === undefined) {
153-
throw errors.noWitnessProviderSet();
154-
}
155-
return provider;
156-
}
157-
158147
/**
159148
* Applies the state transitions to the current stateRoot
160149
* and returns the new prover state
@@ -183,12 +172,19 @@ export class StateTransitionProverProgrammable extends ZkProgrammable<
183172

184173
const transitions = transitionBatch.batch;
185174
const types = transitionBatch.transitionTypes;
175+
const merkleWitness = transitionBatch.merkleWitnesses;
186176
for (
187177
let index = 0;
188178
index < constants.stateTransitionProverBatchSize;
189179
index++
190180
) {
191-
this.applyTransition(state, transitions[index], types[index], index);
181+
this.applyTransition(
182+
state,
183+
transitions[index],
184+
types[index],
185+
merkleWitness[index],
186+
index
187+
);
192188
}
193189

194190
return state;
@@ -202,13 +198,10 @@ export class StateTransitionProverProgrammable extends ZkProgrammable<
202198
state: StateTransitionProverExecutionState,
203199
transition: ProvableStateTransition,
204200
type: ProvableStateTransitionType,
201+
merkleWitness: RollupMerkleTreeWitness,
205202
index = 0
206203
) {
207-
const witness = Provable.witness(RollupMerkleTreeWitness, () =>
208-
this.witnessProvider.getWitness(Field(transition.path.toString()))
209-
);
210-
211-
const membershipValid = witness.checkMembership(
204+
const membershipValid = merkleWitness.checkMembership(
212205
state.stateRoot,
213206
transition.path,
214207
transition.from.value
@@ -218,7 +211,7 @@ export class StateTransitionProverProgrammable extends ZkProgrammable<
218211
.or(transition.from.isSome.not())
219212
.assertTrue(errors.merkleWitnessNotCorrect(index, type));
220213

221-
const newRoot = witness.calculateRoot(transition.to.value);
214+
const newRoot = merkleWitness.calculateRoot(transition.to.value);
222215

223216
state.stateRoot = Provable.if(
224217
transition.to.isSome,
@@ -356,15 +349,9 @@ export class StateTransitionProver
356349
{
357350
public zkProgrammable: StateTransitionProverProgrammable;
358351

359-
public constructor(
360-
// Injected
361-
public readonly witnessProviderReference: StateTransitionWitnessProviderReference
362-
) {
352+
public constructor() {
363353
super();
364-
this.zkProgrammable = new StateTransitionProverProgrammable(
365-
this,
366-
witnessProviderReference
367-
);
354+
this.zkProgrammable = new StateTransitionProverProgrammable(this);
368355
}
369356

370357
public async compile(

0 commit comments

Comments
 (0)