Skip to content

Commit

Permalink
Cleaned the code
Browse files Browse the repository at this point in the history
  • Loading branch information
Sh0g0-1758 committed Jul 3, 2024
1 parent 6cbc111 commit 6835238
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
27 changes: 27 additions & 0 deletions pkg/hintrunner/utils/math_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,30 @@ func IsQuadResidue(x *fp.Element) bool {
return false
}
}

func YSquaredFromX(x, beta, fieldPrime *big.Int) *big.Int {
// Computes y^2 using the curve equation:
// y^2 = x^3 + alpha * x + beta (mod field_prime)
// We ignore alpha as it is a constant with a value of 1

ySquaredBigInt := new(big.Int).Set(x)
ySquaredBigInt.Mul(ySquaredBigInt, x).Mod(ySquaredBigInt, fieldPrime)
ySquaredBigInt.Mul(ySquaredBigInt, x).Mod(ySquaredBigInt, fieldPrime)
ySquaredBigInt.Add(ySquaredBigInt, x).Mod(ySquaredBigInt, fieldPrime)
ySquaredBigInt.Add(ySquaredBigInt, beta).Mod(ySquaredBigInt, fieldPrime)

return ySquaredBigInt
}

func Sqrt(x, p *big.Int) *big.Int {
// Finds the minimum non-negative integer m such that (m*m) % p == x.

halfPrimeBigInt := new(big.Int).Rsh(p, 1)
m := new(big.Int).ModSqrt(x, p)

if m.Cmp(halfPrimeBigInt) > 0 {
m.Sub(p, m)
}

return m
}
30 changes: 5 additions & 25 deletions pkg/hintrunner/zero/zerohint_ec.go
Original file line number Diff line number Diff line change
Expand Up @@ -917,37 +917,17 @@ func newRecoverYHint(x, p hinter.ResOperander) hinter.Hinter {
xFelt.BigInt(xBigInt)

// y^2 = x^3 + alpha * x + beta (mod field_prime)
ySquaredBigInt := new(big.Int).Set(xBigInt)
ySquaredBigInt.Mul(ySquaredBigInt, xBigInt).Mod(ySquaredBigInt, fieldPrimeBigInt)
ySquaredBigInt.Mul(ySquaredBigInt, xBigInt).Mod(ySquaredBigInt, fieldPrimeBigInt)
ySquaredBigInt.Add(ySquaredBigInt, xBigInt).Mod(ySquaredBigInt, fieldPrimeBigInt)
ySquaredBigInt.Add(ySquaredBigInt, betaBigInt).Mod(ySquaredBigInt, fieldPrimeBigInt)

var ySquaredFelt *fp.Element = new(fp.Element)
ySquaredFelt.SetBigInt(ySquaredBigInt)

var value = mem.MemoryValue{}
ySquaredBigInt := secp_utils.YSquaredFromX(xBigInt, betaBigInt, fieldPrimeBigInt)
ySquaredFelt := new(fp.Element).SetBigInt(ySquaredBigInt)

if secp_utils.IsQuadResidue(ySquaredFelt) {
// sqrt(y_squared, field_prime)

var result *fp.Element = new(fp.Element)
halfPrimeBigInt := new(big.Int).Rsh(fieldPrimeBigInt, 1)

tempResult := new(big.Int).ModSqrt(ySquaredBigInt, fieldPrimeBigInt)

if tempResult.Cmp(halfPrimeBigInt) > 0 {
tempResult.Sub(fieldPrimeBigInt, tempResult)
}

result.SetBigInt(tempResult)
value = mem.MemoryValueFromFieldElement(result)
result := new(fp.Element).SetBigInt(secp_utils.Sqrt(ySquaredBigInt, fieldPrimeBigInt))
value := mem.MemoryValueFromFieldElement(result)
return vm.Memory.WriteToAddress(&pYAddr, &value)
} else {
ySquaredString := ySquaredBigInt.String()
return fmt.Errorf("%s does not represent the x coordinate of a point on the curve", ySquaredString)
}

return vm.Memory.WriteToAddress(&pYAddr, &value)
},
}
}
Expand Down

0 comments on commit 6835238

Please sign in to comment.