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

how to get the preimage size for implementing sha256 #320

Closed
zhfnjust opened this issue Aug 2, 2022 · 7 comments
Closed

how to get the preimage size for implementing sha256 #320

zhfnjust opened this issue Aug 2, 2022 · 7 comments

Comments

@zhfnjust
Copy link

zhfnjust commented Aug 2, 2022

hello , I am implementing sha256.

before that i should know the preimage size to do the padding.

  export class Sha256 extends Circuit {
    @circuitMain
    static main(preimage: Field, @public_ hash: Field) {

      let size = preimage.toBits().length

    }
  }

but when calling toBits().length, it returns 255 all the tme.

so any idea to get the preimage size?

@zhfnjust zhfnjust changed the title how to get the field size? how to get the preimage size for sha256 Aug 2, 2022
@zhfnjust zhfnjust changed the title how to get the preimage size for sha256 how to get the preimage size for implementing sha256 Aug 2, 2022
@mitschabaude
Copy link
Collaborator

Is there anything wrong with using 255? That's the (max) bit length of a Field. (Sorry I'm not familiar with the sha256 algorithm)

@zhfnjust
Copy link
Author

zhfnjust commented Aug 2, 2022

sha256 algorithm need to append some padding bytes according the preimage length. but in this case I have no way of knowing the real size of the preimage

@zhfnjust
Copy link
Author

zhfnjust commented Aug 2, 2022

I also need to add a parameter preimageSize: Field to indicate.

@mitschabaude
Copy link
Collaborator

mitschabaude commented Aug 2, 2022

You could do that, yeah! But I still don't get what's wrong with always using 255 as the length.

EDIT: Is it a problem for sha256 if some significant bits of that 255 bits are zero? (this would surprise me, to be honest..)

@zhfnjust
Copy link
Author

zhfnjust commented Aug 2, 2022

another question:

if i use slice() on array when writing a circuit, it crash right?

image

@mitschabaude
Copy link
Collaborator

if i use slice() on array when writing a circuit, it crash right?

no! the code inside a circuit is normal JavaScript. Normal JS doesn't crash when you use slice on an array, except when that array is undefined or something like that.

@mitschabaude
Copy link
Collaborator

Closing this issue, let's continue debugging in discord

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants