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

Passing MerkleWitnesses to ZKProgram causes a Stack_overflow compilation error when compiling the first #1210

Closed
dfstio opened this issue Oct 31, 2023 · 2 comments · Fixed by #1334
Labels
bug Something isn't working

Comments

@dfstio
Copy link

dfstio commented Oct 31, 2023

When trying to compile ZKProgram MapCalculation, the following error occurs:

thrown: Array [
      248,
      MlBytes {
        "c": "Stack_overflow",
        "l": 14,
        "t": 0,
      },
      -9,
    ]

The error is caused by MerkleMapWitness being passed as an argument.
However, if uncomment the line

//await Key.compile();

the MapCalculation compiles without errors.
If you add some logic to the MapCalculation, errors always occur and do not disappear if MapCalculation compiles second.

The source code to reproduce the error:

import { describe, expect, it } from "@jest/globals";
import {
  Field,
  SmartContract,
  state,
  State,
  method,
  Experimental,
  Struct,
  MerkleMapWitness,
} from "o1js";

jest.setTimeout(1000 * 60 * 60); // 1 hour

class Key extends SmartContract {
  @state(Field) key = State<Field>();

  @method mint(key: Field) {
    this.key.assertEquals(Field(0));
    this.key.set(key);
  }
}

class MapElement extends Struct({
  originalRoot: Field,
  redactedRoot: Field,
  key: Field,
  value: Field,
  originalWitness: MerkleMapWitness,
  redactedWitness: MerkleMapWitness,
}) {}

class MapState extends Struct({
  originalRoot: Field,
  redactedRoot: Field,
}) {
  static create(element: MapElement) {
    return new MapState({
      originalRoot: element.originalRoot,
      redactedRoot: element.redactedRoot,
    });
  }

  static assertEquals(state1: MapState, state2: MapState) {
    state1.originalRoot.assertEquals(state2.originalRoot);
    state1.redactedRoot.assertEquals(state2.redactedRoot);
  }
}

const MapCalculation = Experimental.ZkProgram({
  publicInput: MapState,

  methods: {
    create: {
      privateInputs: [MapElement],

      method(state: MapState, element: MapElement) {
        const computedState = MapState.create(element);
        MapState.assertEquals(computedState, state);
      },
    },
  },
});

describe("Compile a contract", () => {
  it("should compile a contract", async () => {
    //await Key.compile();
    await MapCalculation.compile();
  });
});
@mitschabaude mitschabaude added the bug Something isn't working label Oct 31, 2023
@dfstio
Copy link
Author

dfstio commented Nov 28, 2023

The additional discussion that can be related to this bug: https://discord.com/channels/484437221055922177/1176627449375768626

@dfstio
Copy link
Author

dfstio commented Dec 15, 2023

I've checked it again with o1js 0.15.0, and it still has the same error. Compiling SmartContract before ZkProgram still works and resolves the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants