o1js-pack 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,52 @@
1
+ import {
2
+ Experimental,
3
+ SelfProof,
4
+ Character,
5
+ Poseidon,
6
+ Provable,
7
+ Field,
8
+ } from 'o1js';
9
+ import { PackedStringFactory } from '../../src/lib/packed-types/PackedString';
10
+
11
+ const MAX_LENGTH = 32;
12
+ export class TextInput extends PackedStringFactory(MAX_LENGTH) {}
13
+
14
+ export const TextInputProgram = Experimental.ZkProgram({
15
+ publicInput: TextInput,
16
+
17
+ methods: {
18
+ init: {
19
+ privateInputs: [],
20
+ method(state: TextInput) {
21
+ const initState = TextInput.fromString('Mina Protocol');
22
+ state.assertEquals(initState);
23
+ },
24
+ },
25
+ changeFirstLetter: {
26
+ privateInputs: [
27
+ SelfProof,
28
+ Provable.Array(Character, MAX_LENGTH),
29
+ Character,
30
+ ],
31
+ method(
32
+ newState: TextInput,
33
+ oldProof: SelfProof<TextInput, TextInput>,
34
+ oldCharacters: Array<Character>,
35
+ newInput: Character
36
+ ) {
37
+ oldProof.verify();
38
+ const newCharacters = [
39
+ ...oldCharacters.map((x) => new Character(x.value)),
40
+ ];
41
+ const packed = TextInput.fromCharacters(oldCharacters);
42
+ packed.assertEquals(oldProof.publicInput);
43
+ newCharacters[0] = newInput;
44
+ const repacked = TextInput.fromCharacters(newCharacters);
45
+ newState.assertEquals(repacked);
46
+ },
47
+ },
48
+ },
49
+ });
50
+
51
+ export let TextInputProof_ = Experimental.ZkProgram.Proof(TextInputProgram);
52
+ export class TextInputProof extends TextInputProof_ {}
@@ -0,0 +1,45 @@
1
+ import { Experimental, SelfProof } from 'o1js';
2
+ import { PackedUInt32Factory } from '../../src/lib/packed-types/PackedUInt32';
3
+
4
+ export class Votes extends PackedUInt32Factory() {}
5
+
6
+ export const VotesProgram = Experimental.ZkProgram({
7
+ publicInput: Votes,
8
+
9
+ methods: {
10
+ init: {
11
+ privateInputs: [],
12
+ method(state: Votes) {
13
+ const initState = Votes.fromBigInts([0n, 0n]);
14
+ state.assertEquals(initState);
15
+ },
16
+ },
17
+ incrementIndex0: {
18
+ privateInputs: [SelfProof],
19
+ method(newState: Votes, oldProof: SelfProof<Votes, Votes>) {
20
+ oldProof.verify();
21
+ const expectedUnpacked = Votes.unpack(oldProof.publicInput.packed);
22
+ oldProof.publicInput.packed.assertEquals(
23
+ Votes.fromUInt32s(expectedUnpacked).packed
24
+ );
25
+ expectedUnpacked[0] = expectedUnpacked[0].add(1);
26
+ newState.assertEquals(Votes.fromUInt32s(expectedUnpacked));
27
+ },
28
+ },
29
+ incrementIndex1: {
30
+ privateInputs: [SelfProof],
31
+ method(newState: Votes, oldProof: SelfProof<Votes, Votes>) {
32
+ oldProof.verify();
33
+ const expectedUnpacked = Votes.unpack(oldProof.publicInput.packed);
34
+ oldProof.publicInput.packed.assertEquals(
35
+ Votes.fromUInt32s(expectedUnpacked).packed
36
+ );
37
+ expectedUnpacked[1] = expectedUnpacked[1].add(1);
38
+ newState.assertEquals(Votes.fromUInt32s(expectedUnpacked));
39
+ },
40
+ },
41
+ },
42
+ });
43
+
44
+ export let VotesProof_ = Experimental.ZkProgram.Proof(VotesProgram);
45
+ export class VotesProof extends VotesProof_ {}