// An arbitrary number for Koblitz method of encoding string to point. 1024 is convenient compared to original 1000 to do bitshifts instead of multiplications/divisions
// Koblitz decoding method, adapted for this curve:
// Koblitz decoding method, adapted for this curve:
// message m must be < r/10000
// Try finding a point with y value m*10000+0, m*10000+1, .... m*10000+5617 (5617 are last four digits of prime r)
// There is an approximately 1/(2^1000) chance no point will be encodable,
// message m must be < r/(2^10) //using 2^10=1024 instead of Koblitz' parameter of 1000, so arithmetic can happen easily mod 2. This shouldn't have any (negative) impact: https://crypto.stackexchange.com/questions/103132/encoding-a-message-as-points-on-elliptic-curve
// Try finding a point with y value m*1024+0, m*1024+1, .... m*1024+5617 (5617 are last four digits of prime r)
// There is an approximately 1/(2^1024) chance no point will be encodable,
// since each y value has probability of about 1/2 of being on the curve
// since each y value has probability of about 1/2 of being on the curve
pubfnfrom_msg_vartime(msg: BigInt)-> Point{
letMAX_MSG: BigInt=BigInt::parse_bytes(
b"2188824287183927522224640574525727508854836440041603434369820418657580849",10// Prime r but missing last 4 digits
).unwrap();
letACC_UNDER=5617;// Last four digits of prime r. MAX_MSG * 10000 + ACC_UNDER = r
assert!(msg<=MAX_MSG);
pubfnfrom_msg_vartime(msg: &BigInt)-> Point{
letACC_UNDER=1024;// Last four digits of prime r. MAX_MSG * 1024 + ACC_UNDER = r
// Try with some more random numbers -- it's extremely unlikely to get lucky will with valid points 20 times in a row if it's not always producing valid points
// Try with some more random numbers -- it's extremely unlikely to get lucky will with valid points 20 times in a row if it's not always producing valid points
// Convert from msg to point back to msg and make sure it works:
letmsg=123456789.to_bigint().unwrap();
assert!(
Point::from_msg_vartime(&msg).to_msg()
.eq(
&Fr::from_str(&msg.to_string()).unwrap()
)
);
// Try with some more random numbers -- it's extremely unlikely to get lucky will with valid points 20 times in a row if it's not always producing valid points