paulmillr / noble-curves

Audited & minimal JS implementation of elliptic curve cryptography.
https://paulmillr.com/noble
MIT License
623 stars 56 forks source link

Verifying HashToCurve results #50

Closed sundayScoop closed 1 year ago

sundayScoop commented 1 year ago

How can I verify that my HashToCurve results are correct as per these tests here: [https://github.com/paulmillr/noble-curves/blob/main/test/hash-to-curve/edwards25519_XMD%3ASHA-512_ELL2_RO_.json ]()?

This is the code I've implemented:

import { hashToCurve, encodeToCurve } from '@noble/curves/ed25519';
import { numberToBytesLE, bytesToHex } from '@noble/curves/abstract/utils';

let str = "";
let encoder = new TextEncoder(); 
let arr = encoder.encode(str);

var x = numberToBytesLE(hashToCurve(arr).toAffine().y);
var y = bytesToHex(x);
console.log(y); //0301ab58b84523561c1e1db48a76bcfe74c92f9c32856f7dbb683ffbfd513a5f

I've tried finding the value of y in the test pages linked above but couldn't seem to find it. I'm sure that I'm comparing the wrong values, so any help would be great.

paulmillr commented 1 year ago

https://github.com/paulmillr/noble-curves/blob/79dd7d342636f421d36acbf3dbe2014ad7ba7ece/test/hash-to-curve.test.js#L44

https://github.com/paulmillr/noble-curves/blob/79dd7d342636f421d36acbf3dbe2014ad7ba7ece/test/hash-to-curve.test.js#L143

https://github.com/paulmillr/noble-curves/blob/79dd7d342636f421d36acbf3dbe2014ad7ba7ece/test/hash-to-curve.test.js#L106

sundayScoop commented 1 year ago

My test wasn't working because I wasn't adding the DST to the hashToCurve function as done here https://github.com/paulmillr/noble-curves/blob/79dd7d342636f421d36acbf3dbe2014ad7ba7ece/test/hash-to-curve.test.js#L113

I just needed to modify my function above to look like this: hashToCurve(arr, {DST: "QUUX-V01-CS02-with-edwards25519_XMD:SHA-512_ELL2_RO_"}).toAffine().x then it matched what the tests had.

Thanks for the help