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

Tokens #11449

Merged
merged 12 commits into from
Jul 25, 2022
Merged

Tokens #11449

merged 12 commits into from
Jul 25, 2022

Conversation

MartinMinkov
Copy link
Member

@MartinMinkov MartinMinkov commented Jul 5, 2022

Description

Adds custom token and token symbol support in SnarkyJS. This PR adds several helper functions for interacting with custom token ids and token symbols. Additionally adds token id and token symbol to the account type to enable querying the local ledger in SnarkyJS tests. Finally, this PR adds a test script for token and token symbol functionality.

These OCaml changes are needed for o1-labs/o1js#273

Tested

Implemented a JS test script and ran with succession.

@MartinMinkov MartinMinkov mentioned this pull request Jul 5, 2022
@MartinMinkov MartinMinkov force-pushed the test/tokens-playground branch 2 times, most recently from 8f6237d to 8bed816 Compare July 15, 2022 18:21
@MartinMinkov MartinMinkov marked this pull request as ready for review July 18, 2022 19:22
@MartinMinkov MartinMinkov requested review from a team, imeckler and mrmr1993 as code owners July 18, 2022 19:22
@MartinMinkov MartinMinkov changed the title [WIP] - Tokens Tokens Jul 18, 2022
Copy link
Member

@mitschabaude mitschabaude left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome!

Copy link
Member

@mitschabaude mitschabaude left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, one ask: could you bump the snarkyjs submodule to the tip of your token PR so we can run it against CI?

let custom_token_id pk token =
Mina_base.Account_id.derive_token_id
~owner:(Mina_base.Account_id.create (public_key pk) (token_id token))
|> Mina_base.Token_id.to_string |> Js.string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to cover the case where pk and token represent variables, see comments I'll add on the other PR. possibly cover that in a separate function custom_token_id_checked. so that both can have the same interface (pk: PublicKey, token: Field) => Field, I think probably this should return a Field as well

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's probably something like Mina_base.Account_id.Checked.derive_token_id in the code base that we can use

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it! Added support for a Checked version, mind taking a look if the new implementation is correct?

Copy link
Member

@mitschabaude mitschabaude left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good except for the two comments!

Mina_base.Account_id.Checked.derive_token_id
~owner:
(Mina_base.Account_id.Checked.create
(Signature_lib.Public_key.Compressed.var_of_t (public_key pk))
Copy link
Member

@mitschabaude mitschabaude Jul 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using var_of_t is wrong because it's only possible if the variable has been converted to a value first, which is what the public_key function does. The forced converting to a value that public_key does only works inside the prover, or if the pk is a constant, but not when compiling the circuit.

We want to preserve the same underlying variables that pk contains (two field elements; possibly variables) and convert them to a Public_key.Compressed.var in an in-snark way. This is code that I got to type-check that I think achieves that:

  let public_key_checked (pk : public_key) :
      Signature_lib.Public_key.Compressed.var =
    let x = pk##.g##.x##.value in
    let y = pk##.g##.y##.value in
    Signature_lib.Public_key.compress_var (x, y) |> Impl.run_checked

(although I'm unsure what run_checked means.. but it has the right type signature :D)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it! I changed the implementation and removed the use of var_of_t. The code snippet type checks and seems to pass existing test cases 👍


let default_token_id_js =
Mina_base.Token_id.default |> Mina_base.Token_id.to_field_unsafe
|> Field.constant |> to_js_field

let account_id_checked pk token =
Mina_base.Account_id.Checked.create
(Signature_lib.Public_key.Compressed.var_of_t (public_key pk))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same applies here about public_key + var_of_t not being the approach we want

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed implementation 👍

@MartinMinkov MartinMinkov requested a review from a team as a code owner July 19, 2022 20:32
@@ -46,6 +46,8 @@ module Digest : sig

val of_field : Pickles.Impls.Step.Field.t -> t

val to_field_unsafe : t -> Pickles.Impls.Step.Field.t
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels super hacky, does this work? I need a way to convert a Mina_base.Token_id.Checked.t to a Field.t. This type checks but I'm pretty sure the logic is wrong. If there is a better way, can someone advise how to do so?

cc @mitschabaude @bkase

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the right approach 👍🏻 Would've done the same

Copy link
Member

@mitschabaude mitschabaude left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@MartinMinkov MartinMinkov force-pushed the test/tokens-playground branch 3 times, most recently from 5f5cb41 to 464d2d9 Compare July 20, 2022 18:50
@mitschabaude mitschabaude added the ci-build-me Add this label to trigger a circle+buildkite build for this branch label Jul 20, 2022
@MartinMinkov MartinMinkov force-pushed the test/tokens-playground branch 3 times, most recently from dec6980 to 5d3a4f5 Compare July 21, 2022 01:58
@MartinMinkov MartinMinkov force-pushed the test/tokens-playground branch 2 times, most recently from 143f86f to e924994 Compare July 21, 2022 19:53
@MartinMinkov MartinMinkov merged commit 0aab6af into develop Jul 25, 2022
@MartinMinkov MartinMinkov deleted the test/tokens-playground branch July 25, 2022 16:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ci-build-me Add this label to trigger a circle+buildkite build for this branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants