Skip to content

Commit

Permalink
Added mirror functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Sh0g0-1758 committed Sep 27, 2024
1 parent 84e76ab commit b86a637
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion pkg/vm/builtins/modulo.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,24 @@ func (m *ModBuiltin) fillValue(mem *memory.Memory, inputs ModBuiltinInputs, inde
}
}
} else {
value = *new(big.Int).Div(c, a)
x, _, gcd := utils.Igcdex(a, &inputs.p)
// if gcd != 1, the known value is 0, in which case the res must be 0
if gcd.Cmp(big.NewInt(1)) != 0 {
value = *new(big.Int).Div(&inputs.p, &gcd)
} else {
value = *new(big.Int).Mul(c, &x)
value = *value.Mod(&value, &inputs.p)
tmpK, err := utils.SafeDiv(new(big.Int).Sub(new(big.Int).Mul(a, &value), c), &inputs.p)
if err != nil {
return false, err
}
if tmpK.Cmp(kBound) >= 0 {
return false, fmt.Errorf("%s builtin: ((%d * q) - %d) / %d > %d for any q > 0, such that %d * q = %d (mod %d) ", m.String(), a, c, &inputs.p, kBound, a, c, &inputs.p)
}
if tmpK.Cmp(big.NewInt(0)) < 0 {
value = *value.Add(&value, new(big.Int).Mul(&inputs.p, new(big.Int).Div(new(big.Int).Sub(a, new(big.Int).Sub(&tmpK, big.NewInt(1))), a)))
}
}
}
if err := m.writeNWordsValue(mem, addresses[1], value); err != nil {
return false, err
Expand Down

0 comments on commit b86a637

Please sign in to comment.