Number of wires in a ZKP Circom circuit is greater than expected - zk-snark

I compile this simple circuit:
pragma circom 2.0.0;
template Multiplier2() {
signal input a;
signal input b;
signal output c;
c <== a*b;
}
component main = Multiplier2();
and the compiler says 4 wires:
circom 1.circom --r1cs --wasm --sym --c
template instances: 1
non-linear constraints: 1
linear constraints: 0
public inputs: 0
public outputs: 1
private inputs: 2
private outputs: 0
wires: 4
labels: 4
I have expected that the number of wires will be the number of signals (3), I wonder where the 4th wire comes from?

Related

What exactly Circom circuit constraint a*b proves?

I use this circuit as per official documentation:
pragma circom 2.0.0;
/*This circuit template checks that c is the multiplication of a and b.*/
template Multiplier2 () {
// Declaration of signals.
signal input a;
signal input b;
signal output c;
// Constraints.
c <== a * b;
}
and feed the following input file (input.json):
{"a": "3", "b": "11"}
Then compile and generate witness/proof, and verify:
circom multiplier2.circom --r1cs --wasm --sym --c
node generate_witness.js multiplier2.wasm input.json witness.wtns
snarkjs powersoftau new bn128 12 pot12_0000.ptau -v
snarkjs powersoftau contribute pot12_0000.ptau pot12_0001.ptau --name="First contribution" -v
snarkjs powersoftau prepare phase2 pot12_0001.ptau pot12_final.ptau -v
snarkjs groth16 setup multiplier2.r1cs pot12_final.ptau multiplier2_0000.zkey
snarkjs zkey contribute multiplier2_0000.zkey multiplier2_0001.zkey --name="1st Contributor Name" -v
snarkjs zkey export verificationkey multiplier2_0001.zkey verification_key.json
snarkjs groth16 prove multiplier2_0001.zkey witness.wtns proof.json public.json
snarkjs groth16 verify verification_key.json public.json proof.json
It tells:
[INFO] snarkJS: OK!
The documentation says it proves that I know factors of 33. But I don't input 33 anywhere, moreover I get the same result if I change the circuit to:
template Multiplier2 () {
// Declaration of signals.
signal input a;
signal input b;
signal output c;
// Constraints.
c <== a * b * -1;
}
The question how I prove that I know factors of 33 if I don't input 33 anywhere?

64-bit ELF yielding unexplainable results

Can someone explain why the following code yields different results on the second printf if I comment the first printf line or not, in 64 bits?
/* gcc -O0 -o test test.c */
#include <stdio.h>
#include <stdlib.h>
int main() {
char a[20] = {0};
char b = 'a';
int count=-1;
// printf("%.16llx %.16llx\n", a, &b);
printf("%x\n", *(a+count));
return 0;
}
I get the following results for the second printf:
commented: 0
uncommented: 61
Thanks in advance!
iansus
Can someone explain why the following code yields different results on the second printf if I comment the first printf line or not
Your program uses a[-1], and thus exhibits undefined behavior. Anything can happen, and figuring out exactly why one or the other thing happenes is pointless.
The precise reason is that you are reading memory that gets written to by the first printf (when commented in).
I get a different result (which is expected with undefined behavior):
// with first `printf` commented out:
ffffffff
// with it commented in:
00007fffffffdd20 00007fffffffdd1b
ffffffff
You could see where that memory is written to by setting a GDB watchpoint on it:
(gdb) p a[-1]
$1 = 0 '\000'
(gdb) p &a[-1]
$2 = 0x7fffffffdd1f ""
(gdb) watch *(int*)0x7fffffffdd1f
Hardware watchpoint 4: *(int*)0x7fffffffdd1f
(gdb) c
Continuing.
Hardware watchpoint 4: *(int*)0x7fffffffdd1f
Old value = 0
New value = 255
main () at t.c:12
12 printf("%.16llx %.16llx\n", a, &b);
It my case above, the value is written as part of initializing count=-1. That is, with my version of gcc, count is located just before a[0]. But this may depend on compiler version, exactly how this compiler was built, etc. etc.

Modbus TCP/IP on Arduino

I am trying to implement Modbus TCP on arduino uno + ethernet shield using the following code.
I am using a modbus slave simulator on a pc to check the following code. However, the code doesn't seem to be working. I have downloaded the code & the libraries from http://myarduinoprojects.com/modbus.html. Please suggest me corrections if necessary. Also is there another working example available for modbus tcp/ip on arduino.
Thanks.
#include <SPI.h>
#include <Ethernet.h>
#include "MgsModbus.h"
MgsModbus Mb;
int inByte = 0; // incoming serial byte
// Ethernet settings (depending on MAC and Local network)
byte mac[] = {0x00, 0x1A, 0xB6, 0x02, 0xD1, 0x14 };
IPAddress ip(192, 168, 0, 35);
void setup()
{
// serial setup
Serial.begin(9600);
Serial.println("Serial interface started");
// initialize the ethernet device
Ethernet.begin(mac, ip); // start etehrnet interface
Serial.println("Ethernet interface started");
// print your local IP address:
Serial.print("My IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println();
// slave address
Mb.remSlaveIP = (192,168,0,1);
// Fill MbData
// Mb.SetBit(0,false);
Mb.MbData[0] = 1;
Mb.MbData[1] = 2;
Mb.MbData[2] = 3;
Mb.MbData[3] = 4;
Mb.MbData[4] = 5;
Mb.MbData[5] = 6;
Mb.MbData[6] = 0;
Mb.MbData[7] = 0;
Mb.MbData[8] = 0;
Mb.MbData[9] = 0;
Mb.MbData[10] = 0;
Mb.MbData[11] = 0;
// print MbData
for (int i=0;i<12;i++) {
Serial.print("address: "); Serial.print(i); Serial.print("Data: "); Serial.println(Mb.MbData[i]);
}
// print menu
Serial.println("0 - print the first 12 words of the MbData space");
Serial.println("1 - FC 1 - read the first 5 coils from the slave and store them in the lower byte of MbData[1]");
Serial.println("2 - FC 2 - read the first 5 discrete inputs from the slave and store them in the higer of the MbData[1]");
Serial.println("3 - FC 3 - read the first 5 registers from the slave and store them in MbData[3..7");
Serial.println("4 - FC 4 - read the first 5 input registers from the slave and store them in MbData[8..12]");
Serial.println("5 - FC 5 - write coil 0 of the slave with the bit valeu of MbData[0.0]");
Serial.println("6 - FC 6 - write register 0 of the slave with MbData[2]");
Serial.println("7 - FC 15 - write 5 coils of the slave starting with coil 0 with GetBit(16..20");
Serial.println("8 - Fc 16 - write 5 registers of the slave starting on register 0 with MbData[0..4]");
Serial.println(Mb.remSlaveIP);
}
void loop()
{
if (Serial.available() > 0) {
// get incoming byte:
inByte = Serial.read();
if (inByte == '0') { // print MbData
for (int i=0;i<12;i++) {
Serial.print("address: "); Serial.print(i); Serial.print("Data: "); Serial.println(Mb.MbData[i]);
}
}
if (inByte == '1') {Mb.Req(MB_FC_READ_COILS, 6,6,6);} // 1 // ref, count, pos
if (inByte == '2') {Mb.Req(MB_FC_READ_DISCRETE_INPUT, 6,6,6);} // 2
if (inByte == '3') {Mb.Req(MB_FC_READ_REGISTERS, 6,6,6);} // 3
if (inByte == '4') {Mb.Req(MB_FC_READ_INPUT_REGISTER, 6,6,6);} // 4
if (inByte == '5') {Mb.Req(MB_FC_WRITE_COIL, 0,0,0);} // 5 // count can be x
if (inByte == '6') {Mb.Req(MB_FC_WRITE_REGISTER, 7,0,0);} // 6 // count can be x
if (inByte == '7') {Mb.Req(MB_FC_WRITE_MULTIPLE_COILS, 0,6,0);} // 15
if (inByte == '8') {Mb.Req(MB_FC_WRITE_MULTIPLE_REGISTERS, 0,6,0);} // 16
}
Mb.MbmRun();
// Mb.MbsRun();
}
Serial monitor works fine. Following lines are printed on first debugging the program
Serial interface started
Ethernet interface started
My IP address: 192.168.0.35.
address: 0Data: 1
address: 1Data: 2
address: 2Data: 3
address: 3Data: 4
address: 4Data: 5
address: 5Data: 6
address: 6Data: 0
address: 7Data: 0
address: 8Data: 0
address: 9Data: 0
address: 10Data: 0
address: 11Data: 0
0 - print the first 12 words of the MbData space
1 - FC 1 - read the first 5 coils from the slave and store them in the lower byte of MbData[1]
2 - FC 2 - read the first 5 discrete inputs from the slave and store them in the higer of the MbData[1]
3 - FC 3 - read the first 5 registers from the slave and store them in MbData[3..7
4 - FC 4 - read the first 5 input registers from the slave and store them in MbData[8..12]
5 - FC 5 - write coil 0 of the slave with the bit valeu of MbData[0.0]
6 - FC 6 - write register 0 of the slave with MbData[2]
7 - FC 15 - write 5 coils of the slave starting with coil 0 with GetBit(16..20
8 - Fc 16 - write 5 registers of the slave starting on register 0 with MbData[0..4]
1.0.0.0
however it seems that the TCP communication does not work since there is nothing on the serial monitor after this #graham.reeds
I am using mgsmodbus for my modbuswork at office. What I have is a Master TI board and a Slave simulator. However I am using Wifi.h instead of ethernet.h. I am able to do all the operations.
Did you check if the ethernet connection is properly established?
If so try IBH Modbus slave simulator. That works perfect. You would have to change the setting to TCP and select the port. Sometimes the port number you are trying for could be wrong. I use port 502 which is the default one.
Also, make sure that the slave IP in the .cpp code is also changed.

Can't get touch event with Qt and Angstrom

My goal is to make a QT application for Technexion DevKit TDM-3730 BlizzardPack. Everything is OK, but it is impossible to click a button with a touchscreen, while with mouse it's ok.
I've tried evtest, here its output:
root#devkit:/dev/input# evtest touchscreen0
Input driver version is 1.0.1
Input device ID: bus 0x0 vendor 0x0 product 0x0 version 0x0
Input device name: "prism_st"
Supported events:
Event type 0 (Sync)
Event type 1 (Key)
Event code 330 (Touch)
Event type 3 (Absolute)
Event code 0 (X)
Value 115
Min 0
Max 1499
Event code 1 (Y)
Value 397
Min 0
Max 899
Event code 24 (Pressure)
Value 0
Min 0
Max 255
Testing ... (interrupt to exit)
Event: time 10551.906098, type 3 (Absolute), code 0 (X), value 735
Event: time 10551.906129, type 3 (Absolute), code 1 (Y), value 461
Event: time 10551.906129, type 3 (Absolute), code 24 (Pressure), value 255
Event: time 10551.906129, -------------- Report Sync ------------
Event: time 10551.915772, type 3 (Absolute), code 0 (X), value 734
Event: time 10551.915772, type 3 (Absolute), code 1 (Y), value 460
Event: time 10551.915802, -------------- Report Sync ------------
Event: time 10551.925201, type 3 (Absolute), code 0 (X), value 733
Event: time 10551.925201, type 3 (Absolute), code 1 (Y), value 459
Event: time 10551.925232, -------------- Report Sync ------------
Event: time 10551.934570, type 3 (Absolute), code 0 (X), value 732
Event: time 10551.934600, -------------- Report Sync ------------
Event: time 10551.943999, type 3 (Absolute), code 0 (X), value 730
Event: time 10551.944030, type 3 (Absolute), code 1 (Y), value 460
Event: time 10551.944030, -------------- Report Sync ------------
Event: time 10551.951659, type 3 (Absolute), code 0 (X), value 728
Event: time 10551.951690, type 3 (Absolute), code 1 (Y), value 462
Event: time 10551.951690, type 3 (Absolute), code 24 (Pressure), value 28
Event: time 10551.951690, -------------- Report Sync ------------
Event: time 10551.959014, type 3 (Absolute), code 0 (X), value 726
Event: time 10551.959044, type 3 (Absolute), code 1 (Y), value 464
Event: time 10551.959044, type 3 (Absolute), code 24 (Pressure), value 1
Event: time 10551.959044, -------------- Report Sync ------------
No matter how much I tapped there is no touch event (330).
I've made a GTK application and it's ok, but it isn't convenient at all. And it seems that touch-event processing realized inside GTK GUI. Am I right?
Here is my system parameters:
Host - Linux Mint 15, Linux version 3.8.0-19-generic, QT 5
Target - Angstrom, Linux version 2.6.37, qt4-embedded - 4.7.3-r33.1.9, tslib
Cpu - TI Sitara DM3730 # 1Ghz
DSP Core - TMS320C64x+™ # 800Mhz
TsLib environment variables are exported:
export TSLIB_TSDEVICE=/dev/input/touchscreen0
export QWS_MOUSE_PROTO=Tslib:/dev/input/touchscreen0
I'm a beginner at QT (and at Embedded systems), so I'm followwing Derek Molloy tutorial http://www.youtube.com/watch?v=kP7uvOu9hoQ on BeagleBone. But I have Technexion devkit. Beaglebone linux version is 3.2.34 and mine is 2.6.37.
Here is my test program main.cpp:
#include "mainwindow.h"
#include <QApplication>
#include <QTouchEvent>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
and here is mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
ui->lineEdit->setText("DevKit");
}
And in Derek's tutorial it works ok. Could anyone tell me how I can enable qt source code to accept touch screen events?
Thank you in advance.
Egor
I think you may find this video useful in setting up your QT environment and configuring QT embedded on the BBB.
[Beaglebone: Qt Creator for C++ ARM Embedded Linux Development ]

Qt4: Unresolved timer events

A mini sample file main.cpp:
#include <iostream>
#include <QCoreApplication>
class MyApp : public QCoreApplication
{
private:
int m_idtimer;
public:
MyApp(int nargs, char* argc[]) : QCoreApplication(nargs, argc)
{
m_idtimer = startTimer(3000); // 3 segs.
}
protected:
void timerEvent(QTimerEvent* e)
{
char c = '\0';
std::cout << "timerEvent" << std::endl;
std::cin >> c;
if (c == 'q') {
killTimer(m_idtimer);
quit();
}
}
};
int main(int nargs, char* argc[])
{
MyApp app(nargs, argc);
return app.exec();
}
Extra mini Makefile:
LDFLAGS = -I/usr/include/qt4 -I/usr/include/qt4/QtCore
LDLIBS = -lQtCore
Compiling and execution:
$ make main
g++ -I/usr/include/qt4/QtCore main.cpp -lQtCore -o main
$ ./main
timerEvent
1
timerEvent
2
timerEvent
3
timerEvent
q
$
Ok and then, my question. I’ve made this sample with the purpose of testing if timer events are accumulative.
When I executing the main program, the next occurs:
the first timerEvent message is shown after 3 seconds, and timerEvent() waits a character.
I press 1 inmediatly.
3 seconds later, the second timerEvent message appear (as expected).
I wait some seconds (15 or more) and I press 2
The third message is shown immediatly (one timer event accumulated).
I press 3 immediatly.
And 3 seconds later the fourth message appear (no more timer events accumulated).
I press q and the program ends.
Question: Why aren there no more timer events accumulated? Does this behaviour depend on the platform?
PD: My Qt version is 4.8, my SO Ubuntu 13.04, and my kernel (linux) 3.8.0-19-generic. The running graphic system is Gnome 3.
Your fifteen second wait will not accumulate timer events because your timerEvent code blocks waiting for the input. Qt can't get back into its event loop until you enter the input. When it does get back to the event loop it checks the elapsed time and notices that more than 3 seconds have elapsed and so it fires off a timer event. The fact that fifteen seconds has elapsed is irrelevant.
This is the expected behaviour and will not (should not) be platform-dependent.

Resources