Skip to content

Commit

Permalink
Merge pull request #1021 from o1-labs/fix-snarkyjs-berkeley
Browse files Browse the repository at this point in the history
Fix SnarkyJS to be compatible with new Berkeley
  • Loading branch information
MartinMinkov committed Jul 12, 2023
2 parents 9d2b0da + 97869a6 commit 79a90a2
Show file tree
Hide file tree
Showing 21 changed files with 199 additions and 127 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
_Security_ in case of vulnerabilities.
-->

## [Unreleased](https://github.com/o1-labs/snarkyjs/compare/eaa39dca0...HEAD)
## [Unreleased](https://github.com/o1-labs/snarkyjs/compare/161b69d602...HEAD)

> No unreleased changes yet
## [0.12.0](https://github.com/o1-labs/snarkyjs/compare/eaa39dca0...161b69d602)

### Breaking Changes

- Fix the default verification key hash that was generated for AccountUpdates. This change adopts the default mechanism provided by Mina Protocol https://github.com/o1-labs/snarkyjs/pull/1021
- Please be aware that this alteration results in a breaking change affecting the verification key of already deployed contracts.

## [0.11.4](https://github.com/o1-labs/snarkyjs/compare/544489609...eaa39dca0)

### Fixed
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "snarkyjs",
"description": "TypeScript framework for zk-SNARKs and zkApps",
"version": "0.11.4",
"version": "0.12.0",
"license": "Apache-2.0",
"homepage": "https://github.com/o1-labs/snarkyjs/",
"keywords": [
Expand Down
2 changes: 1 addition & 1 deletion src/bindings
68 changes: 34 additions & 34 deletions src/examples/regression_test.json

Large diffs are not rendered by default.

14 changes: 11 additions & 3 deletions src/lib/account_update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ import { UInt64, UInt32, Int64, Sign } from './int.js';
import * as Mina from './mina.js';
import { SmartContract } from './zkapp.js';
import * as Precondition from './precondition.js';
import { dummyBase64Proof, Empty, Proof, Prover } from './proof_system.js';
import {
dummyBase64Proof,
dummyVerificationKeyHash,
Empty,
Proof,
Prover,
} from './proof_system.js';
import { Memo } from '../mina-signer/src/memo.js';
import {
Events,
Expand Down Expand Up @@ -1802,7 +1808,8 @@ const Authorization = {
signature ??= {};
accountUpdate.body.authorizationKind.isSigned = Bool(true);
accountUpdate.body.authorizationKind.isProved = Bool(false);
accountUpdate.body.authorizationKind.verificationKeyHash = Field(0);
accountUpdate.body.authorizationKind.verificationKeyHash =
dummyVerificationKeyHash();
accountUpdate.authorization = {};
accountUpdate.lazyAuthorization = { ...signature, kind: 'lazy-signature' };
},
Expand Down Expand Up @@ -1860,7 +1867,8 @@ const Authorization = {
setLazyNone(accountUpdate: AccountUpdate) {
accountUpdate.body.authorizationKind.isSigned = Bool(false);
accountUpdate.body.authorizationKind.isProved = Bool(false);
accountUpdate.body.authorizationKind.verificationKeyHash = Field(0);
accountUpdate.body.authorizationKind.verificationKeyHash =
dummyVerificationKeyHash();
accountUpdate.authorization = {};
accountUpdate.lazyAuthorization = { kind: 'lazy-none' };
},
Expand Down
9 changes: 9 additions & 0 deletions src/lib/account_update.unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '../index.js';
import { Test } from '../snarky.js';
import { expect } from 'expect';
import { dummyVerificationKeyHash } from './proof_system.js';

let address = PrivateKey.random().toPublicKey();

Expand Down Expand Up @@ -97,6 +98,14 @@ function createAccountUpdate() {
);
}

// creates the right empty vk hash
{
let accountUpdate = createAccountUpdate();
expect(
accountUpdate.body.authorizationKind.verificationKeyHash.toString()
).toEqual(dummyVerificationKeyHash().toString());
}

// does not throw an error if private key is missing unless if .send is executed
{
let Local = Mina.LocalBlockchain({ proofsEnabled: false });
Expand Down
6 changes: 6 additions & 0 deletions src/lib/proof_system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export {
isAsFields,
Prover,
dummyBase64Proof,
dummyVerificationKeyHash,
};

type Undefined = undefined;
Expand Down Expand Up @@ -808,6 +809,11 @@ function dummyBase64Proof() {
return withThreadPool(async () => Pickles.dummyBase64Proof());
}

function dummyVerificationKeyHash() {
let [, , hash] = Pickles.dummyVerificationKey();
return Field.fromBytes([...hash]);
}

// helpers for circuit context

function Prover<ProverData>() {
Expand Down
9 changes: 8 additions & 1 deletion src/lib/testing/random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
Field,
Actions,
ActionState,
VerificationKeyHash,
ReceiptChainHash,
Sign,
TokenId,
Expand All @@ -37,6 +38,7 @@ import { Memo } from '../../mina-signer/src/memo.js';
import { ProvableExtended } from '../../bindings/lib/provable-bigint.js';
import { tokenSymbolLength } from '../../bindings/mina-transaction/derived-leaves.js';
import { stringLengthInBytes } from '../../bindings/lib/binable.js';
import { mocks } from '../../bindings/crypto/constants.js';

export { Random, sample, withHardCoded };

Expand Down Expand Up @@ -103,6 +105,7 @@ const actions = mapWithInvalid(
Actions.fromList
);
const actionState = oneOf(ActionState.emptyValue(), field);
const verificationKeyHash = oneOf(VerificationKeyHash.emptyValue(), field);
const receiptChainHash = oneOf(ReceiptChainHash.emptyValue(), field);
const zkappUri = map(string(nat(50)), ZkappUri.fromJSON);

Expand All @@ -126,6 +129,7 @@ const Generators: Generators = {
Events: events,
Actions: actions,
ActionState: actionState,
VerificationKeyHash: verificationKeyHash,
ReceiptChainHash: receiptChainHash,
ZkappUri: zkappUri,
null: constant(null),
Expand Down Expand Up @@ -228,6 +232,7 @@ const JsonGenerators: JsonGenerators = {
Events: mapWithInvalid(events, Events.toJSON),
Actions: mapWithInvalid(actions, Actions.toJSON),
ActionState: mapWithInvalid(actionState, ActionState.toJSON),
VerificationKeyHash: mapWithInvalid(verificationKeyHash, Field.toJSON),
ReceiptChainHash: mapWithInvalid(receiptChainHash, ReceiptChainHash.toJSON),
ZkappUri: string(nat(50)),
null: constant(null),
Expand Down Expand Up @@ -255,8 +260,10 @@ const accountUpdateJson = mapWithInvalid(
if (isProved && isSigned) {
a.body.authorizationKind.isProved = false;
}

if (!a.body.authorizationKind.isProved) {
a.body.authorizationKind.verificationKeyHash = '0';
a.body.authorizationKind.verificationKeyHash =
mocks.dummyVerificationKeyHash;
}
// ensure mayUseToken is valid
let { inheritFromParent, parentsOwnToken } = a.body.mayUseToken;
Expand Down
12 changes: 6 additions & 6 deletions src/mina-signer/MinaSigner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class Client {
let signature = signPayment(
{
common: { fee, feePayer: from, nonce, validUntil, memo },
body: { source: from, receiver: to, amount },
body: { receiver: to, amount },
},
privateKey,
this.network
Expand Down Expand Up @@ -226,7 +226,7 @@ class Client {
return verifyPayment(
{
common: { fee, feePayer: from, nonce, validUntil, memo },
body: { source: from, receiver: to, amount },
body: { receiver: to, amount },
},
signature,
publicKey,
Expand Down Expand Up @@ -254,7 +254,7 @@ class Client {
let signature = signStakeDelegation(
{
common: { fee, feePayer: from, nonce, validUntil, memo },
body: { newDelegate: to, delegator: from },
body: { newDelegate: to },
},
privateKey,
this.network
Expand All @@ -281,7 +281,7 @@ class Client {
return verifyStakeDelegation(
{
common: { fee, feePayer: from, nonce, validUntil, memo },
body: { newDelegate: to, delegator: from },
body: { newDelegate: to },
},
signature,
publicKey,
Expand All @@ -306,7 +306,7 @@ class Client {
signature,
data: {
common: { fee, feePayer: from, nonce, validUntil, memo },
body: { source: from, receiver: to, amount },
body: { receiver: to, amount },
},
},
options
Expand All @@ -329,7 +329,7 @@ class Client {
signature,
data: {
common: { fee, feePayer: from, nonce, validUntil, memo },
body: { newDelegate: to, delegator: from },
body: { newDelegate: to },
},
},
options
Expand Down
2 changes: 0 additions & 2 deletions src/mina-signer/src/random-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const common = record({
const payment = record<PaymentJson>({
common,
body: record({
source: Random.json.publicKey,
receiver: Random.json.publicKey,
amount: Random.json.uint64,
}),
Expand All @@ -36,7 +35,6 @@ const signedPayment = record<SignedLegacy<PaymentJson>>({
const delegation = record<DelegationJson>({
common,
body: record({
delegator: Random.json.publicKey,
newDelegate: Random.json.publicKey,
}),
});
Expand Down
35 changes: 25 additions & 10 deletions src/mina-signer/src/sign-legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export {
delegationFromJson,
commonFromJson,
PaymentJson,
PaymentJsonV1,
DelegationJson,
DelegationJsonV1,
CommonJson,
Tag,
UserCommand,
Expand Down Expand Up @@ -146,7 +148,7 @@ function commonToInputLegacy({
HashInputLegacy.bits(legacyTokenId),
PublicKey.toInputLegacy(feePayer),
HashInputLegacy.bits(UInt32.toBits(nonce)),
HashInputLegacy.bits(UInt32.toBits(validUntil)),
HashInputLegacy.bits(UInt32.toBits(validUntil.value)),
HashInputLegacy.bits(Memo.toBits(memo)),
].reduce(HashInputLegacy.append);
}
Expand All @@ -160,13 +162,13 @@ const legacyTokenId = [true, ...Array<boolean>(63).fill(false)];

function paymentFromJson({
common,
body: { source, receiver, amount },
body: { receiver, amount },
}: PaymentJson): UserCommand {
return {
common: commonFromJson(common),
body: {
tag: 'Payment',
source: PublicKey.fromJSON(source),
source: PublicKey.fromJSON(common.feePayer),
receiver: PublicKey.fromJSON(receiver),
amount: UInt64.fromJSON(amount),
},
Expand All @@ -175,25 +177,25 @@ function paymentFromJson({

function delegationFromJson({
common,
body: { delegator, newDelegate },
body: { newDelegate },
}: DelegationJson): UserCommand {
return {
common: commonFromJson(common),
body: {
tag: 'StakeDelegation',
source: PublicKey.fromJSON(delegator),
source: PublicKey.fromJSON(common.feePayer),
receiver: PublicKey.fromJSON(newDelegate),
amount: UInt64(0),
},
};
}

function commonFromJson(c: CommonJson) {
function commonFromJson(c: CommonJson): Common {
return {
fee: UInt64.fromJSON(c.fee),
feePayer: PublicKey.fromJSON(c.feePayer),
nonce: UInt32.fromJSON(c.nonce),
validUntil: UInt32.fromJSON(c.validUntil),
validUntil: { type: 'SinceGenesis', value: UInt32.fromJSON(c.validUntil) },
// TODO: this might need to be fromBase58
memo: Memo.fromString(c.memo),
};
Expand Down Expand Up @@ -262,17 +264,15 @@ type Common = {
fee: UInt64;
feePayer: PublicKey;
nonce: UInt32;
validUntil: UInt32;
validUntil: { type: 'SinceGenesis'; value: UInt32 };
memo: string;
};

type Payment = {
source: PublicKey;
receiver: PublicKey;
amount: UInt64;
};
type Delegation = {
delegator: PublicKey;
newDelegate: PublicKey;
};

Expand All @@ -285,6 +285,14 @@ type CommonJson = {
};

type PaymentJson = {
common: CommonJson;
body: {
receiver: Json.PublicKey;
amount: Json.UInt64;
};
};

type PaymentJsonV1 = {
common: CommonJson;
body: {
source: Json.PublicKey;
Expand All @@ -294,6 +302,13 @@ type PaymentJson = {
};

type DelegationJson = {
common: CommonJson;
body: {
newDelegate: Json.PublicKey;
};
};

type DelegationJsonV1 = {
common: CommonJson;
body: {
delegator: Json.PublicKey;
Expand Down
14 changes: 0 additions & 14 deletions src/mina-signer/src/sign-legacy.unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,6 @@ let amountTooLarge = {
amount: (2n ** 64n).toString(),
},
};
let invalidPublicKey: PaymentJson = {
common: validPayment.common,
body: {
...validPayment.body,
source: PublicKey.toBase58({ x: 0n, isOdd: 0n }),
},
};
let signature = Signature.toJSON({ r: Field.random(), s: Scalar.random() });

expect(() => signPayment(amountTooLarge, privateKey, 'mainnet')).toThrow(
Expand All @@ -122,13 +115,6 @@ expect(verifyPayment(amountTooLarge, signature, publicKey, 'mainnet')).toEqual(
false
);

expect(() => signPayment(invalidPublicKey, privateKey, 'mainnet')).toThrow(
'not a valid group element'
);
expect(
verifyPayment(invalidPublicKey, signature, publicKey, 'mainnet')
).toEqual(false);

// negative tests with invalid signatures

let garbageSignature = { field: 'garbage', scalar: 'garbage' };
Expand Down
Loading

0 comments on commit 79a90a2

Please sign in to comment.