returns res = Σ(vars) or res = vars[0] - Σ(vars[1:]) if sub == true.
(vars []int, sub bool)
| 53 | |
| 54 | // returns res = Σ(vars) or res = vars[0] - Σ(vars[1:]) if sub == true. |
| 55 | func (builder *builder) add(vars []int, sub bool) frontend.Variable { |
| 56 | // check if all variables are constants |
| 57 | allConst := true |
| 58 | if sum, ok := builder.constantValue(vars[0]); ok { |
| 59 | for _, x := range vars[1:] { |
| 60 | if v, ok := builder.constantValue(x); ok { |
| 61 | if sub { |
| 62 | sum = builder.field.Sub(sum, v) |
| 63 | } else { |
| 64 | sum = builder.field.Add(sum, v) |
| 65 | } |
| 66 | } else { |
| 67 | allConst = false |
| 68 | break |
| 69 | } |
| 70 | } |
| 71 | if allConst { |
| 72 | return builder.toVariable(sum) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | coef := make([]constraint.Element, len(vars)) |
| 77 | coef[0] = builder.tOne |
| 78 | if sub { |
| 79 | for i := 1; i < len(vars); i++ { |
| 80 | coef[i] = builder.field.Neg(builder.tOne) |
| 81 | } |
| 82 | } else { |
| 83 | for i := 1; i < len(vars); i++ { |
| 84 | coef[i] = builder.tOne |
| 85 | } |
| 86 | } |
| 87 | builder.instructions = append(builder.instructions, irsource.Instruction{ |
| 88 | Type: irsource.LinComb, |
| 89 | Inputs: vars, |
| 90 | LinCombCoef: coef, |
| 91 | }) |
| 92 | return builder.addVar() |
| 93 | } |
| 94 | |
| 95 | // Neg returns the negation of the given variable. |
| 96 | func (builder *builder) Neg(i frontend.Variable) frontend.Variable { |
no test coverage detected