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

NAF representation in bn256 #27957

Open
Raneet10 opened this issue Aug 21, 2023 · 3 comments
Open

NAF representation in bn256 #27957

Raneet10 opened this issue Aug 21, 2023 · 3 comments

Comments

@Raneet10
Copy link

Raneet10 commented Aug 21, 2023

Hey folks!
In bn256/cloudflare, one thing that caught my eye was the NAF representation of 6u + 2 (u = 4965661367192848881) :

var sixuPlus2NAF = []int8{0, 0, 0, 1, 0, 1, 0, -1, 0, 0, 1, -1, 0, 0, 1, 0,
0, 1, 1, 0, -1, 0, 0, 1, 0, -1, 0, 0, 0, 0, 1, 1,
1, 0, 0, -1, 0, 0, 1, 0, 0, 0, 0, 0, -1, 0, 0, 1,
1, 0, 0, -1, 0, 0, 0, 1, 1, 0, -1, 0, 0, 1, 0, 1, 1}

Now , in the NAF representation aren't the non zero values not supposed to be adjacent ? Is this a different representation than NAF that's maybe faster ? Or I am interpreting it wrong (most likely) ?
Btw by using this https://github.com/Consensys/gnark-crypto/blob/30ae949dd2311dceba5069585b9b324d53dccf15/ecc/utils.go#L12 ,it came out to be [0 0 0 1 0 1 0 -1 0 0 -1 0 0 0 1 0 0 -1 0 -1 0 0 0 1 0 -1 0 0 0 0 -1 0 0 1 0 -1 0 0 1 0 0 0 0 0 -1 0 0 -1 0 1 0 -1 0 0 0 -1 0 -1 0 0 0 1 0 -1 0 1] on my end.

Apologies if this has already been discussed/answered!

EDIT: Probably this shouldn't be under docs type

@MariusVanDerWijden
Copy link
Member

MariusVanDerWijden commented Aug 22, 2023

We're just using bn256/cloudflare as a vendored in library, so all questions regarding the lib should be better asked here: https://github.com/cloudflare/bn256

I tried a few online naf calculators with 29793968203157093288 = 6u + 2 (u = 4965661367192848881) and they returned different naf representations (e.g. https://ngn.bitbucket.io/k/#eJxLs9LVVzeK0TVSNFYw1OICgjQFI0tzS2NLMwsjA2NDU3MDS2MjCwsuAKUCB70=)

@Raneet10
Copy link
Author

Raneet10 commented Aug 22, 2023

I see but isn't the cloudflare's lib using a different value for u ( u = 6518589491078791937 given here ; the NAF form also looks correct) than what we use ?

I guess the NAF representation might differ based on the algorithm used. Though my understanding was that in general in NAF the non-zero values can't be adjacent to each other (https://hackmd.io/@drouyang/S1GgWQvoj).

@Raneet10
Copy link
Author

Another thing I missed mentioning was that the number of non-zero bits currently is 26 as compared to 22 in the representation in my comment above and 20 in the calculator you shared. Since the hamming weight is lower in the other 2 alternative representations, we can theoretically optimise the Miller loop with lower number of additions.

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

No branches or pull requests

2 participants