MCPcopy Create free account
hub / github.com/PolyhedraZK/ExpanderCompilerCollection / add

Method add

ecgo/builder/api.go:55–93  ·  view source on GitHub ↗

returns res = Σ(vars) or res = vars[0] - Σ(vars[1:]) if sub == true.

(vars []int, sub bool)

Source from the content-addressed store, hash-verified

53
54// returns res = Σ(vars) or res = vars[0] - Σ(vars[1:]) if sub == true.
55func (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.
96func (builder *builder) Neg(i frontend.Variable) frontend.Variable {

Callers 2

AddMethod · 0.95
SubMethod · 0.95

Calls 6

constantValueMethod · 0.95
toVariableMethod · 0.95
addVarMethod · 0.95
SubMethod · 0.45
AddMethod · 0.45
NegMethod · 0.45

Tested by

no test coverage detected